@@ -94,7 +94,7 @@ static HTAB *seqhashtab = NULL; /* hash table for SeqTable items */
9494 */
9595static 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 );
9898static Relation lock_and_open_sequence (SeqTable seq );
9999static void create_seq_hashtable (void );
100100static 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 */
342342static 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 )
0 commit comments