@@ -94,7 +94,7 @@ static HTAB *seqhashtab = NULL; /* hash table for SeqTable items */
94
94
*/
95
95
static SeqTableData * last_used_seq = NULL ;
96
96
97
- static void fill_seq_with_data (Relation rel , HeapTuple tuple );
97
+ static void fill_seq_with_data (Relation rel , HeapTuple tuple , Buffer buf );
98
98
static Relation lock_and_open_sequence (SeqTable seq );
99
99
static void create_seq_hashtable (void );
100
100
static void init_sequence (Oid relid , SeqTable * p_elm , Relation * p_rel );
@@ -222,7 +222,7 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
222
222
223
223
/* now initialize the sequence's data */
224
224
tuple = heap_form_tuple (tupDesc , value , null );
225
- fill_seq_with_data (rel , tuple );
225
+ fill_seq_with_data (rel , tuple , InvalidBuffer );
226
226
227
227
/* process OWNED BY if given */
228
228
if (owned_by )
@@ -327,7 +327,7 @@ ResetSequence(Oid seq_relid)
327
327
/*
328
328
* Insert the modified tuple into the new storage file.
329
329
*/
330
- fill_seq_with_data (seq_rel , tuple );
330
+ fill_seq_with_data (seq_rel , tuple , InvalidBuffer );
331
331
332
332
/* Clear local cache so that we don't think we have cached numbers */
333
333
/* Note that we do not change the currval() state */
@@ -340,18 +340,21 @@ ResetSequence(Oid seq_relid)
340
340
* Initialize a sequence's relation with the specified tuple as content
341
341
*/
342
342
static void
343
- fill_seq_with_data (Relation rel , HeapTuple tuple )
343
+ fill_seq_with_data (Relation rel , HeapTuple tuple , Buffer buf )
344
344
{
345
- Buffer buf ;
346
345
Page page ;
347
346
sequence_magic * sm ;
348
347
OffsetNumber offnum ;
348
+ bool lockBuffer = false;
349
349
350
350
/* Initialize first page of relation with special magic number */
351
351
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
+ }
355
358
page = BufferGetPage (buf );
356
359
357
360
PageInit (page , BufferGetPageSize (buf ), sizeof (sequence_magic ));
@@ -360,7 +363,8 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
360
363
361
364
/* Now insert sequence tuple */
362
365
363
- LockBuffer (buf , BUFFER_LOCK_EXCLUSIVE );
366
+ if (lockBuffer )
367
+ LockBuffer (buf , BUFFER_LOCK_EXCLUSIVE );
364
368
365
369
/*
366
370
* 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)
410
414
411
415
END_CRIT_SECTION ();
412
416
413
- UnlockReleaseBuffer (buf );
417
+ if (lockBuffer )
418
+ UnlockReleaseBuffer (buf );
414
419
}
415
420
416
421
/*
@@ -502,7 +507,7 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
502
507
/*
503
508
* Insert the modified tuple into the new storage file.
504
509
*/
505
- fill_seq_with_data (seqrel , newdatatuple );
510
+ fill_seq_with_data (seqrel , newdatatuple , InvalidBuffer );
506
511
}
507
512
508
513
/* process OWNED BY if given */
@@ -1178,6 +1183,16 @@ read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
1178
1183
LockBuffer (* buf , BUFFER_LOCK_EXCLUSIVE );
1179
1184
1180
1185
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
+
1181
1196
sm = (sequence_magic * ) PageGetSpecialPointer (page );
1182
1197
1183
1198
if (sm -> magic != SEQ_MAGIC )
0 commit comments