Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 42bc751

Browse files
committed
[refer #PGPRO-2887] Fix ON COMMIT clause for global temp tables
1 parent 507838e commit 42bc751

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/backend/commands/sequence.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static HTAB *seqhashtab = NULL; /* hash table for SeqTable items */
9494
*/
9595
static SeqTableData *last_used_seq = NULL;
9696

97-
static void fill_seq_with_data(Relation rel, HeapTuple tuple);
97+
static void fill_seq_with_data(Relation rel, HeapTuple tuple, Buffer buf);
9898
static Relation lock_and_open_sequence(SeqTable seq);
9999
static void create_seq_hashtable(void);
100100
static void init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel);
@@ -222,7 +222,7 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
222222

223223
/* now initialize the sequence's data */
224224
tuple = heap_form_tuple(tupDesc, value, null);
225-
fill_seq_with_data(rel, tuple);
225+
fill_seq_with_data(rel, tuple, InvalidBuffer);
226226

227227
/* process OWNED BY if given */
228228
if (owned_by)
@@ -327,7 +327,7 @@ ResetSequence(Oid seq_relid)
327327
/*
328328
* Insert the modified tuple into the new storage file.
329329
*/
330-
fill_seq_with_data(seq_rel, tuple);
330+
fill_seq_with_data(seq_rel, tuple, InvalidBuffer);
331331

332332
/* Clear local cache so that we don't think we have cached numbers */
333333
/* Note that we do not change the currval() state */
@@ -340,18 +340,21 @@ ResetSequence(Oid seq_relid)
340340
* Initialize a sequence's relation with the specified tuple as content
341341
*/
342342
static void
343-
fill_seq_with_data(Relation rel, HeapTuple tuple)
343+
fill_seq_with_data(Relation rel, HeapTuple tuple, Buffer buf)
344344
{
345-
Buffer buf;
346345
Page page;
347346
sequence_magic *sm;
348347
OffsetNumber offnum;
348+
bool lockBuffer = false;
349349

350350
/* Initialize first page of relation with special magic number */
351351

352-
buf = ReadBuffer(rel, P_NEW);
353-
Assert(BufferGetBlockNumber(buf) == 0);
354-
352+
if (buf == InvalidBuffer)
353+
{
354+
buf = ReadBuffer(rel, P_NEW);
355+
Assert(BufferGetBlockNumber(buf) == 0);
356+
lockBuffer = true;
357+
}
355358
page = BufferGetPage(buf);
356359

357360
PageInit(page, BufferGetPageSize(buf), sizeof(sequence_magic));
@@ -360,7 +363,8 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
360363

361364
/* Now insert sequence tuple */
362365

363-
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
366+
if (lockBuffer)
367+
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
364368

365369
/*
366370
* Since VACUUM does not process sequences, we have to force the tuple to
@@ -410,7 +414,8 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
410414

411415
END_CRIT_SECTION();
412416

413-
UnlockReleaseBuffer(buf);
417+
if (lockBuffer)
418+
UnlockReleaseBuffer(buf);
414419
}
415420

416421
/*
@@ -502,7 +507,7 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
502507
/*
503508
* Insert the modified tuple into the new storage file.
504509
*/
505-
fill_seq_with_data(seqrel, newdatatuple);
510+
fill_seq_with_data(seqrel, newdatatuple, InvalidBuffer);
506511
}
507512

508513
/* process OWNED BY if given */
@@ -1178,6 +1183,16 @@ read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
11781183
LockBuffer(*buf, BUFFER_LOCK_EXCLUSIVE);
11791184

11801185
page = BufferGetPage(*buf);
1186+
if (rel->rd_rel->relpersistence == RELPERSISTENCE_SESSION && PageIsNew(page))
1187+
{
1188+
/* Initialize sequence for global temporary tables */
1189+
Datum value[SEQ_COL_LASTCOL] = {0};
1190+
bool null[SEQ_COL_LASTCOL] = {false};
1191+
value[SEQ_COL_LASTVAL-1] = Int64GetDatumFast(1); /* start sequence with 1 */
1192+
HeapTuple tuple = heap_form_tuple(RelationGetDescr(rel), value, null);
1193+
fill_seq_with_data(rel, tuple, *buf);
1194+
}
1195+
11811196
sm = (sequence_magic *) PageGetSpecialPointer(page);
11821197

11831198
if (sm->magic != SEQ_MAGIC)

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14583,14 +14583,7 @@ PreCommit_on_commit_actions(void)
1458314583
/* Do nothing (there shouldn't be such entries, actually) */
1458414584
break;
1458514585
case ONCOMMIT_DELETE_ROWS:
14586-
14587-
/*
14588-
* If this transaction hasn't accessed any temporary
14589-
* relations, we can skip truncating ON COMMIT DELETE ROWS
14590-
* tables, as they must still be empty.
14591-
*/
14592-
if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPNAMESPACE))
14593-
oids_to_truncate = lappend_oid(oids_to_truncate, oc->relid);
14586+
oids_to_truncate = lappend_oid(oids_to_truncate, oc->relid);
1459414587
break;
1459514588
case ONCOMMIT_DROP:
1459614589
oids_to_drop = lappend_oid(oids_to_drop, oc->relid);

src/backend/parser/parse_utilcmd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
435435
seqstmt->for_identity = for_identity;
436436
seqstmt->sequence = makeRangeVar(snamespace, sname, -1);
437437
seqstmt->options = seqoptions;
438+
seqstmt->sequence->relpersistence = cxt->relation->relpersistence;
438439

439440
/*
440441
* If a sequence data type was specified, add it to the options. Prepend

0 commit comments

Comments
 (0)