PostgreSQL Source Code git master
sequence.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/multixact.h"
#include "access/relation.h"
#include "access/sequence.h"
#include "access/table.h"
#include "access/transam.h"
#include "access/xact.h"
#include "access/xloginsert.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_sequence.h"
#include "catalog/pg_type.h"
#include "catalog/storage_xlog.h"
#include "commands/defrem.h"
#include "commands/sequence.h"
#include "commands/sequence_xlog.h"
#include "commands/tablecmds.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/smgr.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/pg_lsn.h"
#include "utils/resowner.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
Include dependency graph for sequence.c:

Go to the source code of this file.

Data Structures

struct  SeqTableData
 

Macros

#define SEQ_LOG_VALS   32
 
#define PG_GET_SEQUENCE_DATA_COLS   3
 

Typedefs

typedef struct SeqTableData SeqTableData
 
typedef SeqTableDataSeqTable
 

Functions

static void fill_seq_with_data (Relation rel, HeapTuple tuple)
 
static void fill_seq_fork_with_data (Relation rel, HeapTuple tuple, ForkNumber forkNum)
 
static Relation lock_and_open_sequence (SeqTable seq)
 
static void create_seq_hashtable (void)
 
static void init_sequence (Oid relid, SeqTable *p_elm, Relation *p_rel)
 
static Form_pg_sequence_data read_seq_tuple (Relation rel, Buffer *buf, HeapTuple seqdatatuple)
 
static void init_params (ParseState *pstate, List *options, bool for_identity, bool isInit, Form_pg_sequence seqform, int64 *last_value, bool *reset_state, bool *is_called, bool *need_seq_rewrite, List **owned_by)
 
static void process_owned_by (Relation seqrel, List *owned_by, bool for_identity)
 
ObjectAddress DefineSequence (ParseState *pstate, CreateSeqStmt *seq)
 
void ResetSequence (Oid seq_relid)
 
ObjectAddress AlterSequence (ParseState *pstate, AlterSeqStmt *stmt)
 
void SequenceChangePersistence (Oid relid, char newrelpersistence)
 
void DeleteSequenceTuple (Oid relid)
 
Datum nextval (PG_FUNCTION_ARGS)
 
Datum nextval_oid (PG_FUNCTION_ARGS)
 
int64 nextval_internal (Oid relid, bool check_permissions)
 
Datum currval_oid (PG_FUNCTION_ARGS)
 
Datum lastval (PG_FUNCTION_ARGS)
 
void SetSequence (Oid relid, int64 next, bool iscalled)
 
Datum setval_oid (PG_FUNCTION_ARGS)
 
Datum setval3_oid (PG_FUNCTION_ARGS)
 
Listsequence_options (Oid relid)
 
Datum pg_sequence_parameters (PG_FUNCTION_ARGS)
 
Datum pg_get_sequence_data (PG_FUNCTION_ARGS)
 
Datum pg_sequence_last_value (PG_FUNCTION_ARGS)
 
void ResetSequenceCaches (void)
 

Variables

static HTABseqhashtab = NULL
 
static SeqTableDatalast_used_seq = NULL
 

Macro Definition Documentation

◆ PG_GET_SEQUENCE_DATA_COLS

#define PG_GET_SEQUENCE_DATA_COLS   3

◆ SEQ_LOG_VALS

#define SEQ_LOG_VALS   32

Definition at line 58 of file sequence.c.

Typedef Documentation

◆ SeqTable

Definition at line 79 of file sequence.c.

◆ SeqTableData

typedef struct SeqTableData SeqTableData

Function Documentation

◆ AlterSequence()

ObjectAddress AlterSequence ( ParseState pstate,
AlterSeqStmt stmt 
)

Definition at line 429 of file sequence.c.

430{
431 Oid relid;
432 SeqTable elm;
433 Relation seqrel;
434 Buffer buf;
435 HeapTupleData datatuple;
436 Form_pg_sequence seqform;
437 Form_pg_sequence_data newdataform;
438 bool need_seq_rewrite;
439 List *owned_by;
440 ObjectAddress address;
441 Relation rel;
442 HeapTuple seqtuple;
443 bool reset_state = false;
444 bool is_called;
445 int64 last_value;
446 HeapTuple newdatatuple;
447
448 /* Open and lock sequence, and check for ownership along the way. */
449 relid = RangeVarGetRelidExtended(stmt->sequence,
451 stmt->missing_ok ? RVR_MISSING_OK : 0,
453 NULL);
454 if (relid == InvalidOid)
455 {
457 (errmsg("relation \"%s\" does not exist, skipping",
458 stmt->sequence->relname)));
460 }
461
462 init_sequence(relid, &elm, &seqrel);
463
464 rel = table_open(SequenceRelationId, RowExclusiveLock);
465 seqtuple = SearchSysCacheCopy1(SEQRELID,
466 ObjectIdGetDatum(relid));
467 if (!HeapTupleIsValid(seqtuple))
468 elog(ERROR, "cache lookup failed for sequence %u",
469 relid);
470
471 seqform = (Form_pg_sequence) GETSTRUCT(seqtuple);
472
473 /* lock page buffer and read tuple into new sequence structure */
474 (void) read_seq_tuple(seqrel, &buf, &datatuple);
475
476 /* copy the existing sequence data tuple, so it can be modified locally */
477 newdatatuple = heap_copytuple(&datatuple);
478 newdataform = (Form_pg_sequence_data) GETSTRUCT(newdatatuple);
479 last_value = newdataform->last_value;
480 is_called = newdataform->is_called;
481
483
484 /* Check and set new values */
485 init_params(pstate, stmt->options, stmt->for_identity, false,
486 seqform, &last_value, &reset_state, &is_called,
487 &need_seq_rewrite, &owned_by);
488
489 /* If needed, rewrite the sequence relation itself */
490 if (need_seq_rewrite)
491 {
492 /* check the comment above nextval_internal()'s equivalent call. */
493 if (RelationNeedsWAL(seqrel))
495
496 /*
497 * Create a new storage file for the sequence, making the state
498 * changes transactional.
499 */
500 RelationSetNewRelfilenumber(seqrel, seqrel->rd_rel->relpersistence);
501
502 /*
503 * Ensure sequence's relfrozenxid is at 0, since it won't contain any
504 * unfrozen XIDs. Same with relminmxid, since a sequence will never
505 * contain multixacts.
506 */
507 Assert(seqrel->rd_rel->relfrozenxid == InvalidTransactionId);
508 Assert(seqrel->rd_rel->relminmxid == InvalidMultiXactId);
509
510 /*
511 * Insert the modified tuple into the new storage file.
512 */
513 newdataform->last_value = last_value;
514 newdataform->is_called = is_called;
515 if (reset_state)
516 newdataform->log_cnt = 0;
517 fill_seq_with_data(seqrel, newdatatuple);
518 }
519
520 /* Clear local cache so that we don't think we have cached numbers */
521 /* Note that we do not change the currval() state */
522 elm->cached = elm->last;
523
524 /* process OWNED BY if given */
525 if (owned_by)
526 process_owned_by(seqrel, owned_by, stmt->for_identity);
527
528 /* update the pg_sequence tuple (we could skip this in some cases...) */
529 CatalogTupleUpdate(rel, &seqtuple->t_self, seqtuple);
530
531 InvokeObjectPostAlterHook(RelationRelationId, relid, 0);
532
533 ObjectAddressSet(address, RelationRelationId, relid);
534
536 sequence_close(seqrel, NoLock);
537
538 return address;
539}
void sequence_close(Relation relation, LOCKMODE lockmode)
Definition: sequence.c:58
int Buffer
Definition: buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5383
int64_t int64
Definition: c.h:538
static void fill_seq_with_data(Relation rel, HeapTuple tuple)
Definition: sequence.c:331
static void init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel)
Definition: sequence.c:1130
static Form_pg_sequence_data read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
Definition: sequence.c:1191
static void process_owned_by(Relation seqrel, List *owned_by, bool for_identity)
Definition: sequence.c:1598
static void init_params(ParseState *pstate, List *options, bool for_identity, bool isInit, Form_pg_sequence seqform, int64 *last_value, bool *reset_state, bool *is_called, bool *need_seq_rewrite, List **owned_by)
Definition: sequence.c:1260
FormData_pg_sequence_data * Form_pg_sequence_data
Definition: sequence.h:28
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:150
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:778
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
#define stmt
Definition: indent_codes.h:59
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition: indexing.c:313
#define NoLock
Definition: lockdefs.h:34
#define ShareRowExclusiveLock
Definition: lockdefs.h:41
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidMultiXactId
Definition: multixact.h:25
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:440
@ RVR_MISSING_OK
Definition: namespace.h:90
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:197
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
FormData_pg_sequence * Form_pg_sequence
Definition: pg_sequence.h:40
static char * buf
Definition: pg_test_fsync.c:72
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationNeedsWAL(relation)
Definition: rel.h:638
void RelationSetNewRelfilenumber(Relation relation, char persistence)
Definition: relcache.c:3773
ItemPointerData t_self
Definition: htup.h:65
Definition: pg_list.h:54
Form_pg_class rd_rel
Definition: rel.h:111
int64 cached
Definition: sequence.c:73
int64 last
Definition: sequence.c:72
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:91
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
void RangeVarCallbackOwnsRelation(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: tablecmds.c:19544
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionId(void)
Definition: xact.c:427

References Assert(), buf, SeqTableData::cached, CatalogTupleUpdate(), elog, ereport, errmsg(), ERROR, fill_seq_with_data(), GETSTRUCT(), GetTopTransactionId(), heap_copytuple(), HeapTupleIsValid, init_params(), init_sequence(), InvalidMultiXactId, InvalidObjectAddress, InvalidOid, InvalidTransactionId, InvokeObjectPostAlterHook, FormData_pg_sequence_data::is_called, SeqTableData::last, FormData_pg_sequence_data::last_value, FormData_pg_sequence_data::log_cnt, NoLock, NOTICE, ObjectAddressSet, ObjectIdGetDatum(), process_owned_by(), RangeVarCallbackOwnsRelation(), RangeVarGetRelidExtended(), RelationData::rd_rel, read_seq_tuple(), RelationNeedsWAL, RelationSetNewRelfilenumber(), RowExclusiveLock, RVR_MISSING_OK, SearchSysCacheCopy1, sequence_close(), ShareRowExclusiveLock, stmt, HeapTupleData::t_self, table_close(), table_open(), and UnlockReleaseBuffer().

Referenced by ProcessUtilitySlow().

◆ create_seq_hashtable()

static void create_seq_hashtable ( void  )
static

Definition at line 1114 of file sequence.c.

1115{
1116 HASHCTL ctl;
1117
1118 ctl.keysize = sizeof(Oid);
1119 ctl.entrysize = sizeof(SeqTableData);
1120
1121 seqhashtab = hash_create("Sequence values", 16, &ctl,
1123}
static HTAB * seqhashtab
Definition: sequence.c:81
struct SeqTableData SeqTableData
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:358
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
tree ctl
Definition: radixtree.h:1838

References ctl, HASH_BLOBS, hash_create(), HASH_ELEM, and seqhashtab.

Referenced by init_sequence().

◆ currval_oid()

Datum currval_oid ( PG_FUNCTION_ARGS  )

Definition at line 867 of file sequence.c.

868{
869 Oid relid = PG_GETARG_OID(0);
870 int64 result;
871 SeqTable elm;
872 Relation seqrel;
873
874 /* open and lock sequence */
875 init_sequence(relid, &elm, &seqrel);
876
880 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
881 errmsg("permission denied for sequence %s",
882 RelationGetRelationName(seqrel))));
883
884 if (!elm->last_valid)
886 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
887 errmsg("currval of sequence \"%s\" is not yet defined in this session",
888 RelationGetRelationName(seqrel))));
889
890 result = elm->last;
891
892 sequence_close(seqrel, NoLock);
893
894 PG_RETURN_INT64(result);
895}
@ ACLCHECK_OK
Definition: acl.h:183
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4037
int errcode(int sqlerrcode)
Definition: elog.c:863
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
Oid GetUserId(void)
Definition: miscinit.c:469
#define ACL_USAGE
Definition: parsenodes.h:84
#define ACL_SELECT
Definition: parsenodes.h:77
#define RelationGetRelationName(relation)
Definition: rel.h:549
bool last_valid
Definition: sequence.c:71

References ACL_SELECT, ACL_USAGE, ACLCHECK_OK, ereport, errcode(), errmsg(), ERROR, GetUserId(), init_sequence(), SeqTableData::last, SeqTableData::last_valid, NoLock, pg_class_aclcheck(), PG_GETARG_OID, PG_RETURN_INT64, RelationGetRelationName, SeqTableData::relid, and sequence_close().

◆ DefineSequence()

ObjectAddress DefineSequence ( ParseState pstate,
CreateSeqStmt seq 
)

Definition at line 112 of file sequence.c.

113{
114 FormData_pg_sequence seqform;
115 int64 last_value;
116 bool reset_state;
117 bool is_called;
118 bool need_seq_rewrite;
119 List *owned_by;
121 Oid seqoid;
122 ObjectAddress address;
123 Relation rel;
124 HeapTuple tuple;
125 TupleDesc tupDesc;
127 bool null[SEQ_COL_LASTCOL];
128 Datum pgs_values[Natts_pg_sequence];
129 bool pgs_nulls[Natts_pg_sequence];
130 int i;
131
132 /*
133 * If if_not_exists was given and a relation with the same name already
134 * exists, bail out. (Note: we needn't check this when not if_not_exists,
135 * because DefineRelation will complain anyway.)
136 */
137 if (seq->if_not_exists)
138 {
140 if (OidIsValid(seqoid))
141 {
142 /*
143 * If we are in an extension script, insist that the pre-existing
144 * object be a member of the extension, to avoid security risks.
145 */
146 ObjectAddressSet(address, RelationRelationId, seqoid);
148
149 /* OK to skip */
151 (errcode(ERRCODE_DUPLICATE_TABLE),
152 errmsg("relation \"%s\" already exists, skipping",
153 seq->sequence->relname)));
155 }
156 }
157
158 /* Check and set all option values */
159 init_params(pstate, seq->options, seq->for_identity, true,
160 &seqform, &last_value, &reset_state, &is_called,
161 &need_seq_rewrite, &owned_by);
162
163 /*
164 * Create relation (and fill value[] and null[] for the tuple)
165 */
166 stmt->tableElts = NIL;
167 for (i = SEQ_COL_FIRSTCOL; i <= SEQ_COL_LASTCOL; i++)
168 {
169 ColumnDef *coldef = NULL;
170
171 switch (i)
172 {
173 case SEQ_COL_LASTVAL:
174 coldef = makeColumnDef("last_value", INT8OID, -1, InvalidOid);
175 value[i - 1] = Int64GetDatumFast(last_value);
176 break;
177 case SEQ_COL_LOG:
178 coldef = makeColumnDef("log_cnt", INT8OID, -1, InvalidOid);
179 value[i - 1] = Int64GetDatum((int64) 0);
180 break;
181 case SEQ_COL_CALLED:
182 coldef = makeColumnDef("is_called", BOOLOID, -1, InvalidOid);
183 value[i - 1] = BoolGetDatum(false);
184 break;
185 }
186
187 coldef->is_not_null = true;
188 null[i - 1] = false;
189
190 stmt->tableElts = lappend(stmt->tableElts, coldef);
191 }
192
193 stmt->relation = seq->sequence;
194 stmt->inhRelations = NIL;
195 stmt->constraints = NIL;
196 stmt->options = NIL;
197 stmt->oncommit = ONCOMMIT_NOOP;
198 stmt->tablespacename = NULL;
199 stmt->if_not_exists = seq->if_not_exists;
200
201 address = DefineRelation(stmt, RELKIND_SEQUENCE, seq->ownerId, NULL, NULL);
202 seqoid = address.objectId;
203 Assert(seqoid != InvalidOid);
204
205 rel = sequence_open(seqoid, AccessExclusiveLock);
206 tupDesc = RelationGetDescr(rel);
207
208 /* now initialize the sequence's data */
209 tuple = heap_form_tuple(tupDesc, value, null);
210 fill_seq_with_data(rel, tuple);
211
212 /* process OWNED BY if given */
213 if (owned_by)
214 process_owned_by(rel, owned_by, seq->for_identity);
215
217
218 /* fill in pg_sequence */
219 rel = table_open(SequenceRelationId, RowExclusiveLock);
220 tupDesc = RelationGetDescr(rel);
221
222 memset(pgs_nulls, 0, sizeof(pgs_nulls));
223
224 pgs_values[Anum_pg_sequence_seqrelid - 1] = ObjectIdGetDatum(seqoid);
225 pgs_values[Anum_pg_sequence_seqtypid - 1] = ObjectIdGetDatum(seqform.seqtypid);
226 pgs_values[Anum_pg_sequence_seqstart - 1] = Int64GetDatumFast(seqform.seqstart);
227 pgs_values[Anum_pg_sequence_seqincrement - 1] = Int64GetDatumFast(seqform.seqincrement);
228 pgs_values[Anum_pg_sequence_seqmax - 1] = Int64GetDatumFast(seqform.seqmax);
229 pgs_values[Anum_pg_sequence_seqmin - 1] = Int64GetDatumFast(seqform.seqmin);
230 pgs_values[Anum_pg_sequence_seqcache - 1] = Int64GetDatumFast(seqform.seqcache);
231 pgs_values[Anum_pg_sequence_seqcycle - 1] = BoolGetDatum(seqform.seqcycle);
232
233 tuple = heap_form_tuple(tupDesc, pgs_values, pgs_nulls);
234 CatalogTupleInsert(rel, tuple);
235
236 heap_freetuple(tuple);
238
239 return address;
240}
Relation sequence_open(Oid relationId, LOCKMODE lockmode)
Definition: sequence.c:37
#define OidIsValid(objectId)
Definition: c.h:777
#define SEQ_COL_LASTVAL
Definition: sequence.h:34
#define SEQ_COL_CALLED
Definition: sequence.h:36
#define SEQ_COL_LASTCOL
Definition: sequence.h:39
#define SEQ_COL_LOG
Definition: sequence.h:35
#define SEQ_COL_FIRSTCOL
Definition: sequence.h:38
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
static struct @171 value
int i
Definition: isn.c:77
List * lappend(List *list, void *datum)
Definition: list.c:339
#define AccessExclusiveLock
Definition: lockdefs.h:43
ColumnDef * makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
Definition: makefuncs.c:565
Oid RangeVarGetAndCheckCreationNamespace(RangeVar *relation, LOCKMODE lockmode, Oid *existing_relation_id)
Definition: namespace.c:738
#define makeNode(_type_)
Definition: nodes.h:161
void checkMembershipInCurrentExtension(const ObjectAddress *object)
Definition: pg_depend.c:258
#define NIL
Definition: pg_list.h:68
FormData_pg_sequence
Definition: pg_sequence.h:33
static Datum Int64GetDatum(int64 X)
Definition: postgres.h:403
#define Int64GetDatumFast(X)
Definition: postgres.h:515
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
uint64_t Datum
Definition: postgres.h:70
@ ONCOMMIT_NOOP
Definition: primnodes.h:58
#define RelationGetDescr(relation)
Definition: rel.h:541
bool is_not_null
Definition: parsenodes.h:759
bool if_not_exists
Definition: parsenodes.h:3229
List * options
Definition: parsenodes.h:3226
RangeVar * sequence
Definition: parsenodes.h:3225
char * relname
Definition: primnodes.h:83
ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ObjectAddress *typaddress, const char *queryString)
Definition: tablecmds.c:764

References AccessExclusiveLock, Assert(), BoolGetDatum(), CatalogTupleInsert(), checkMembershipInCurrentExtension(), DefineRelation(), ereport, errcode(), errmsg(), fill_seq_with_data(), CreateSeqStmt::for_identity, FormData_pg_sequence, heap_form_tuple(), heap_freetuple(), i, CreateSeqStmt::if_not_exists, init_params(), Int64GetDatum(), Int64GetDatumFast, InvalidObjectAddress, InvalidOid, ColumnDef::is_not_null, lappend(), makeColumnDef(), makeNode, NIL, NoLock, NOTICE, ObjectAddressSet, ObjectAddress::objectId, ObjectIdGetDatum(), OidIsValid, ONCOMMIT_NOOP, CreateSeqStmt::options, CreateSeqStmt::ownerId, process_owned_by(), RangeVarGetAndCheckCreationNamespace(), RelationGetDescr, RangeVar::relname, RowExclusiveLock, SEQ_COL_CALLED, SEQ_COL_FIRSTCOL, SEQ_COL_LASTCOL, SEQ_COL_LASTVAL, SEQ_COL_LOG, CreateSeqStmt::sequence, sequence_close(), sequence_open(), stmt, table_close(), table_open(), and value.

Referenced by ProcessUtilitySlow().

◆ DeleteSequenceTuple()

void DeleteSequenceTuple ( Oid  relid)

Definition at line 571 of file sequence.c.

572{
573 Relation rel;
574 HeapTuple tuple;
575
576 rel = table_open(SequenceRelationId, RowExclusiveLock);
577
578 tuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(relid));
579 if (!HeapTupleIsValid(tuple))
580 elog(ERROR, "cache lookup failed for sequence %u", relid);
581
582 CatalogTupleDelete(rel, &tuple->t_self);
583
584 ReleaseSysCache(tuple);
586}
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition: indexing.c:365
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

References CatalogTupleDelete(), elog, ERROR, HeapTupleIsValid, ObjectIdGetDatum(), ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

◆ fill_seq_fork_with_data()

static void fill_seq_fork_with_data ( Relation  rel,
HeapTuple  tuple,
ForkNumber  forkNum 
)
static

Definition at line 352 of file sequence.c.

353{
354 Buffer buf;
355 Page page;
356 sequence_magic *sm;
357 OffsetNumber offnum;
358
359 /* Initialize first page of relation with special magic number */
360
361 buf = ExtendBufferedRel(BMR_REL(rel), forkNum, NULL,
364
365 page = BufferGetPage(buf);
366
369 sm->magic = SEQ_MAGIC;
370
371 /* Now insert sequence tuple */
372
373 /*
374 * Since VACUUM does not process sequences, we have to force the tuple to
375 * have xmin = FrozenTransactionId now. Otherwise it would become
376 * invisible to SELECTs after 2G transactions. It is okay to do this
377 * because if the current transaction aborts, no other xact will ever
378 * examine the sequence tuple anyway.
379 */
386
387 /* check the comment above nextval_internal()'s equivalent call. */
388 if (RelationNeedsWAL(rel))
390
392
394
395 offnum = PageAddItem(page, tuple->t_data, tuple->t_len, InvalidOffsetNumber, false, false);
396 if (offnum != FirstOffsetNumber)
397 elog(ERROR, "failed to add sequence tuple to page");
398
399 /* XLOG stuff */
400 if (RelationNeedsWAL(rel) || forkNum == INIT_FORKNUM)
401 {
402 xl_seq_rec xlrec;
403 XLogRecPtr recptr;
404
407
408 xlrec.locator = rel->rd_locator;
409
410 XLogRegisterData(&xlrec, sizeof(xl_seq_rec));
411 XLogRegisterData(tuple->t_data, tuple->t_len);
412
413 recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG);
414
415 PageSetLSN(page, recptr);
416 }
417
419
421}
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:4223
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:845
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2943
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:436
static Size BufferGetPageSize(Buffer buffer)
Definition: bufmgr.h:425
@ EB_SKIP_EXTENSION_LOCK
Definition: bufmgr.h:75
@ EB_LOCK_FIRST
Definition: bufmgr.h:87
#define BMR_REL(p_rel)
Definition: bufmgr.h:114
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
#define PageGetSpecialPointer(page)
Definition: bufpage.h:338
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:390
PageData * Page
Definition: bufpage.h:81
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:471
#define FirstCommandId
Definition: c.h:676
static void HeapTupleHeaderSetXminFrozen(HeapTupleHeaderData *tup)
Definition: htup_details.h:370
static void HeapTupleHeaderSetCmin(HeapTupleHeaderData *tup, CommandId cid)
Definition: htup_details.h:422
#define HEAP_XMAX_INVALID
Definition: htup_details.h:208
static void HeapTupleHeaderSetXmin(HeapTupleHeaderData *tup, TransactionId xid)
Definition: htup_details.h:331
static void HeapTupleHeaderSetXmax(HeapTupleHeaderData *tup, TransactionId xid)
Definition: htup_details.h:383
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
#define START_CRIT_SECTION()
Definition: miscadmin.h:150
#define END_CRIT_SECTION()
Definition: miscadmin.h:152
#define InvalidOffsetNumber
Definition: off.h:26
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
@ INIT_FORKNUM
Definition: relpath.h:61
#define SEQ_MAGIC
Definition: sequence_xlog.h:26
#define XLOG_SEQ_LOG
Definition: sequence_xlog.h:21
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
ItemPointerData t_ctid
Definition: htup_details.h:161
RelFileLocator rd_locator
Definition: rel.h:57
RelFileLocator locator
Definition: sequence_xlog.h:36
#define FrozenTransactionId
Definition: transam.h:33
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:478
void XLogRegisterData(const void *data, uint32 len)
Definition: xloginsert.c:368
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:245
void XLogBeginInsert(void)
Definition: xloginsert.c:152
#define REGBUF_WILL_INIT
Definition: xloginsert.h:34

References Assert(), BMR_REL, buf, BufferGetBlockNumber(), BufferGetPage(), BufferGetPageSize(), EB_LOCK_FIRST, EB_SKIP_EXTENSION_LOCK, elog, END_CRIT_SECTION, ERROR, ExtendBufferedRel(), FirstCommandId, FirstOffsetNumber, FrozenTransactionId, GetTopTransactionId(), HEAP_XMAX_INVALID, HeapTupleHeaderSetCmin(), HeapTupleHeaderSetXmax(), HeapTupleHeaderSetXmin(), HeapTupleHeaderSetXminFrozen(), INIT_FORKNUM, InvalidOffsetNumber, InvalidTransactionId, ItemPointerSet(), xl_seq_rec::locator, sequence_magic::magic, MarkBufferDirty(), PageAddItem, PageGetSpecialPointer, PageInit(), PageSetLSN(), RelationData::rd_locator, REGBUF_WILL_INIT, RelationNeedsWAL, SEQ_MAGIC, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, UnlockReleaseBuffer(), XLOG_SEQ_LOG, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by fill_seq_with_data().

◆ fill_seq_with_data()

static void fill_seq_with_data ( Relation  rel,
HeapTuple  tuple 
)
static

Definition at line 331 of file sequence.c.

332{
334
335 if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
336 {
337 SMgrRelation srel;
338
340 smgrcreate(srel, INIT_FORKNUM, false);
344 smgrclose(srel);
345 }
346}
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:4942
static void fill_seq_fork_with_data(Relation rel, HeapTuple tuple, ForkNumber forkNum)
Definition: sequence.c:352
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
@ MAIN_FORKNUM
Definition: relpath.h:58
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition: smgr.c:240
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:481
void smgrclose(SMgrRelation reln)
Definition: smgr.c:374
void log_smgrcreate(const RelFileLocator *rlocator, ForkNumber forkNum)
Definition: storage.c:187

References fill_seq_fork_with_data(), FlushRelationBuffers(), INIT_FORKNUM, INVALID_PROC_NUMBER, log_smgrcreate(), MAIN_FORKNUM, RelationData::rd_locator, RelationData::rd_rel, smgrclose(), smgrcreate(), and smgropen().

Referenced by AlterSequence(), DefineSequence(), ResetSequence(), and SequenceChangePersistence().

◆ init_params()

static void init_params ( ParseState pstate,
List options,
bool  for_identity,
bool  isInit,
Form_pg_sequence  seqform,
int64 last_value,
bool *  reset_state,
bool *  is_called,
bool *  need_seq_rewrite,
List **  owned_by 
)
static

Definition at line 1260 of file sequence.c.

1268{
1269 DefElem *as_type = NULL;
1270 DefElem *start_value = NULL;
1271 DefElem *restart_value = NULL;
1272 DefElem *increment_by = NULL;
1273 DefElem *max_value = NULL;
1274 DefElem *min_value = NULL;
1275 DefElem *cache_value = NULL;
1276 DefElem *is_cycled = NULL;
1278 bool reset_max_value = false;
1279 bool reset_min_value = false;
1280
1281 *need_seq_rewrite = false;
1282 *owned_by = NIL;
1283
1284 foreach(option, options)
1285 {
1286 DefElem *defel = (DefElem *) lfirst(option);
1287
1288 if (strcmp(defel->defname, "as") == 0)
1289 {
1290 if (as_type)
1291 errorConflictingDefElem(defel, pstate);
1292 as_type = defel;
1293 *need_seq_rewrite = true;
1294 }
1295 else if (strcmp(defel->defname, "increment") == 0)
1296 {
1297 if (increment_by)
1298 errorConflictingDefElem(defel, pstate);
1299 increment_by = defel;
1300 *need_seq_rewrite = true;
1301 }
1302 else if (strcmp(defel->defname, "start") == 0)
1303 {
1304 if (start_value)
1305 errorConflictingDefElem(defel, pstate);
1306 start_value = defel;
1307 *need_seq_rewrite = true;
1308 }
1309 else if (strcmp(defel->defname, "restart") == 0)
1310 {
1311 if (restart_value)
1312 errorConflictingDefElem(defel, pstate);
1313 restart_value = defel;
1314 *need_seq_rewrite = true;
1315 }
1316 else if (strcmp(defel->defname, "maxvalue") == 0)
1317 {
1318 if (max_value)
1319 errorConflictingDefElem(defel, pstate);
1320 max_value = defel;
1321 *need_seq_rewrite = true;
1322 }
1323 else if (strcmp(defel->defname, "minvalue") == 0)
1324 {
1325 if (min_value)
1326 errorConflictingDefElem(defel, pstate);
1327 min_value = defel;
1328 *need_seq_rewrite = true;
1329 }
1330 else if (strcmp(defel->defname, "cache") == 0)
1331 {
1332 if (cache_value)
1333 errorConflictingDefElem(defel, pstate);
1334 cache_value = defel;
1335 *need_seq_rewrite = true;
1336 }
1337 else if (strcmp(defel->defname, "cycle") == 0)
1338 {
1339 if (is_cycled)
1340 errorConflictingDefElem(defel, pstate);
1341 is_cycled = defel;
1342 *need_seq_rewrite = true;
1343 }
1344 else if (strcmp(defel->defname, "owned_by") == 0)
1345 {
1346 if (*owned_by)
1347 errorConflictingDefElem(defel, pstate);
1348 *owned_by = defGetQualifiedName(defel);
1349 }
1350 else if (strcmp(defel->defname, "sequence_name") == 0)
1351 {
1352 /*
1353 * The parser allows this, but it is only for identity columns, in
1354 * which case it is filtered out in parse_utilcmd.c. We only get
1355 * here if someone puts it into a CREATE SEQUENCE, where it'd be
1356 * redundant. (The same is true for the equally-nonstandard
1357 * LOGGED and UNLOGGED options, but for those, the default error
1358 * below seems sufficient.)
1359 */
1360 ereport(ERROR,
1361 (errcode(ERRCODE_SYNTAX_ERROR),
1362 errmsg("invalid sequence option SEQUENCE NAME"),
1363 parser_errposition(pstate, defel->location)));
1364 }
1365 else
1366 elog(ERROR, "option \"%s\" not recognized",
1367 defel->defname);
1368 }
1369
1370 /*
1371 * We must reset the state of the sequence when isInit or when changing
1372 * any parameters that would affect future nextval allocations.
1373 */
1374 if (isInit)
1375 *reset_state = true;
1376
1377 /* AS type */
1378 if (as_type != NULL)
1379 {
1380 Oid newtypid = typenameTypeId(pstate, defGetTypeName(as_type));
1381
1382 if (newtypid != INT2OID &&
1383 newtypid != INT4OID &&
1384 newtypid != INT8OID)
1385 ereport(ERROR,
1386 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1387 for_identity
1388 ? errmsg("identity column type must be smallint, integer, or bigint")
1389 : errmsg("sequence type must be smallint, integer, or bigint")));
1390
1391 if (!isInit)
1392 {
1393 /*
1394 * When changing type and the old sequence min/max values were the
1395 * min/max of the old type, adjust sequence min/max values to
1396 * min/max of new type. (Otherwise, the user chose explicit
1397 * min/max values, which we'll leave alone.)
1398 */
1399 if ((seqform->seqtypid == INT2OID && seqform->seqmax == PG_INT16_MAX) ||
1400 (seqform->seqtypid == INT4OID && seqform->seqmax == PG_INT32_MAX) ||
1401 (seqform->seqtypid == INT8OID && seqform->seqmax == PG_INT64_MAX))
1402 reset_max_value = true;
1403 if ((seqform->seqtypid == INT2OID && seqform->seqmin == PG_INT16_MIN) ||
1404 (seqform->seqtypid == INT4OID && seqform->seqmin == PG_INT32_MIN) ||
1405 (seqform->seqtypid == INT8OID && seqform->seqmin == PG_INT64_MIN))
1406 reset_min_value = true;
1407 }
1408
1409 seqform->seqtypid = newtypid;
1410 }
1411 else if (isInit)
1412 {
1413 seqform->seqtypid = INT8OID;
1414 }
1415
1416 /* INCREMENT BY */
1417 if (increment_by != NULL)
1418 {
1419 seqform->seqincrement = defGetInt64(increment_by);
1420 if (seqform->seqincrement == 0)
1421 ereport(ERROR,
1422 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1423 errmsg("INCREMENT must not be zero")));
1424 *reset_state = true;
1425 }
1426 else if (isInit)
1427 {
1428 seqform->seqincrement = 1;
1429 }
1430
1431 /* CYCLE */
1432 if (is_cycled != NULL)
1433 {
1434 seqform->seqcycle = boolVal(is_cycled->arg);
1435 Assert(BoolIsValid(seqform->seqcycle));
1436 *reset_state = true;
1437 }
1438 else if (isInit)
1439 {
1440 seqform->seqcycle = false;
1441 }
1442
1443 /* MAXVALUE (null arg means NO MAXVALUE) */
1444 if (max_value != NULL && max_value->arg)
1445 {
1446 seqform->seqmax = defGetInt64(max_value);
1447 *reset_state = true;
1448 }
1449 else if (isInit || max_value != NULL || reset_max_value)
1450 {
1451 if (seqform->seqincrement > 0 || reset_max_value)
1452 {
1453 /* ascending seq */
1454 if (seqform->seqtypid == INT2OID)
1455 seqform->seqmax = PG_INT16_MAX;
1456 else if (seqform->seqtypid == INT4OID)
1457 seqform->seqmax = PG_INT32_MAX;
1458 else
1459 seqform->seqmax = PG_INT64_MAX;
1460 }
1461 else
1462 seqform->seqmax = -1; /* descending seq */
1463 *reset_state = true;
1464 }
1465
1466 /* Validate maximum value. No need to check INT8 as seqmax is an int64 */
1467 if ((seqform->seqtypid == INT2OID && (seqform->seqmax < PG_INT16_MIN || seqform->seqmax > PG_INT16_MAX))
1468 || (seqform->seqtypid == INT4OID && (seqform->seqmax < PG_INT32_MIN || seqform->seqmax > PG_INT32_MAX)))
1469 ereport(ERROR,
1470 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1471 errmsg("MAXVALUE (%" PRId64 ") is out of range for sequence data type %s",
1472 seqform->seqmax,
1473 format_type_be(seqform->seqtypid))));
1474
1475 /* MINVALUE (null arg means NO MINVALUE) */
1476 if (min_value != NULL && min_value->arg)
1477 {
1478 seqform->seqmin = defGetInt64(min_value);
1479 *reset_state = true;
1480 }
1481 else if (isInit || min_value != NULL || reset_min_value)
1482 {
1483 if (seqform->seqincrement < 0 || reset_min_value)
1484 {
1485 /* descending seq */
1486 if (seqform->seqtypid == INT2OID)
1487 seqform->seqmin = PG_INT16_MIN;
1488 else if (seqform->seqtypid == INT4OID)
1489 seqform->seqmin = PG_INT32_MIN;
1490 else
1491 seqform->seqmin = PG_INT64_MIN;
1492 }
1493 else
1494 seqform->seqmin = 1; /* ascending seq */
1495 *reset_state = true;
1496 }
1497
1498 /* Validate minimum value. No need to check INT8 as seqmin is an int64 */
1499 if ((seqform->seqtypid == INT2OID && (seqform->seqmin < PG_INT16_MIN || seqform->seqmin > PG_INT16_MAX))
1500 || (seqform->seqtypid == INT4OID && (seqform->seqmin < PG_INT32_MIN || seqform->seqmin > PG_INT32_MAX)))
1501 ereport(ERROR,
1502 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1503 errmsg("MINVALUE (%" PRId64 ") is out of range for sequence data type %s",
1504 seqform->seqmin,
1505 format_type_be(seqform->seqtypid))));
1506
1507 /* crosscheck min/max */
1508 if (seqform->seqmin >= seqform->seqmax)
1509 ereport(ERROR,
1510 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1511 errmsg("MINVALUE (%" PRId64 ") must be less than MAXVALUE (%" PRId64 ")",
1512 seqform->seqmin,
1513 seqform->seqmax)));
1514
1515 /* START WITH */
1516 if (start_value != NULL)
1517 {
1518 seqform->seqstart = defGetInt64(start_value);
1519 }
1520 else if (isInit)
1521 {
1522 if (seqform->seqincrement > 0)
1523 seqform->seqstart = seqform->seqmin; /* ascending seq */
1524 else
1525 seqform->seqstart = seqform->seqmax; /* descending seq */
1526 }
1527
1528 /* crosscheck START */
1529 if (seqform->seqstart < seqform->seqmin)
1530 ereport(ERROR,
1531 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1532 errmsg("START value (%" PRId64 ") cannot be less than MINVALUE (%" PRId64 ")",
1533 seqform->seqstart,
1534 seqform->seqmin)));
1535 if (seqform->seqstart > seqform->seqmax)
1536 ereport(ERROR,
1537 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1538 errmsg("START value (%" PRId64 ") cannot be greater than MAXVALUE (%" PRId64 ")",
1539 seqform->seqstart,
1540 seqform->seqmax)));
1541
1542 /* RESTART [WITH] */
1543 if (restart_value != NULL)
1544 {
1545 if (restart_value->arg != NULL)
1546 *last_value = defGetInt64(restart_value);
1547 else
1548 *last_value = seqform->seqstart;
1549 *is_called = false;
1550 *reset_state = true;
1551 }
1552 else if (isInit)
1553 {
1554 *last_value = seqform->seqstart;
1555 *is_called = false;
1556 }
1557
1558 /* crosscheck RESTART (or current value, if changing MIN/MAX) */
1559 if (*last_value < seqform->seqmin)
1560 ereport(ERROR,
1561 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1562 errmsg("RESTART value (%" PRId64 ") cannot be less than MINVALUE (%" PRId64 ")",
1563 *last_value,
1564 seqform->seqmin)));
1565 if (*last_value > seqform->seqmax)
1566 ereport(ERROR,
1567 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1568 errmsg("RESTART value (%" PRId64 ") cannot be greater than MAXVALUE (%" PRId64 ")",
1569 *last_value,
1570 seqform->seqmax)));
1571
1572 /* CACHE */
1573 if (cache_value != NULL)
1574 {
1575 seqform->seqcache = defGetInt64(cache_value);
1576 if (seqform->seqcache <= 0)
1577 ereport(ERROR,
1578 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1579 errmsg("CACHE (%" PRId64 ") must be greater than zero",
1580 seqform->seqcache)));
1581 *reset_state = true;
1582 }
1583 else if (isInit)
1584 {
1585 seqform->seqcache = 1;
1586 }
1587}
#define PG_INT32_MAX
Definition: c.h:597
#define BoolIsValid(boolean)
Definition: c.h:765
#define PG_INT16_MIN
Definition: c.h:593
#define PG_INT64_MAX
Definition: c.h:600
#define PG_INT64_MIN
Definition: c.h:599
#define PG_INT32_MIN
Definition: c.h:596
#define PG_INT16_MAX
Definition: c.h:594
TypeName * defGetTypeName(DefElem *def)
Definition: define.c:271
List * defGetQualifiedName(DefElem *def)
Definition: define.c:239
int64 defGetInt64(DefElem *def)
Definition: define.c:173
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:371
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:291
#define lfirst(lc)
Definition: pg_list.h:172
char * defname
Definition: parsenodes.h:843
ParseLoc location
Definition: parsenodes.h:847
Node * arg
Definition: parsenodes.h:844
#define boolVal(v)
Definition: value.h:81

References DefElem::arg, Assert(), BoolIsValid, boolVal, defGetInt64(), defGetQualifiedName(), defGetTypeName(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, errorConflictingDefElem(), format_type_be(), lfirst, DefElem::location, NIL, parser_errposition(), PG_INT16_MAX, PG_INT16_MIN, PG_INT32_MAX, PG_INT32_MIN, PG_INT64_MAX, PG_INT64_MIN, and typenameTypeId().

Referenced by AlterSequence(), and DefineSequence().

◆ init_sequence()

static void init_sequence ( Oid  relid,
SeqTable p_elm,
Relation p_rel 
)
static

Definition at line 1130 of file sequence.c.

1131{
1132 SeqTable elm;
1133 Relation seqrel;
1134 bool found;
1135
1136 /* Find or create a hash table entry for this sequence */
1137 if (seqhashtab == NULL)
1139
1140 elm = (SeqTable) hash_search(seqhashtab, &relid, HASH_ENTER, &found);
1141
1142 /*
1143 * Initialize the new hash table entry if it did not exist already.
1144 *
1145 * NOTE: seqhashtab entries are stored for the life of a backend (unless
1146 * explicitly discarded with DISCARD). If the sequence itself is deleted
1147 * then the entry becomes wasted memory, but it's small enough that this
1148 * should not matter.
1149 */
1150 if (!found)
1151 {
1152 /* relid already filled in */
1155 elm->last_valid = false;
1156 elm->last = elm->cached = 0;
1157 }
1158
1159 /*
1160 * Open the sequence relation.
1161 */
1162 seqrel = lock_and_open_sequence(elm);
1163
1164 /*
1165 * If the sequence has been transactionally replaced since we last saw it,
1166 * discard any cached-but-unissued values. We do not touch the currval()
1167 * state, however.
1168 */
1169 if (seqrel->rd_rel->relfilenode != elm->filenumber)
1170 {
1171 elm->filenumber = seqrel->rd_rel->relfilenode;
1172 elm->cached = elm->last;
1173 }
1174
1175 /* Return results */
1176 *p_elm = elm;
1177 *p_rel = seqrel;
1178}
SeqTableData * SeqTable
Definition: sequence.c:79
static void create_seq_hashtable(void)
Definition: sequence.c:1114
static Relation lock_and_open_sequence(SeqTable seq)
Definition: sequence.c:1086
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:952
@ HASH_ENTER
Definition: hsearch.h:114
#define InvalidLocalTransactionId
Definition: lock.h:67
#define InvalidRelFileNumber
Definition: relpath.h:26
RelFileNumber filenumber
Definition: sequence.c:69
LocalTransactionId lxid
Definition: sequence.c:70

References SeqTableData::cached, create_seq_hashtable(), SeqTableData::filenumber, HASH_ENTER, hash_search(), InvalidLocalTransactionId, InvalidRelFileNumber, SeqTableData::last, SeqTableData::last_valid, lock_and_open_sequence(), SeqTableData::lxid, RelationData::rd_rel, and seqhashtab.

Referenced by AlterSequence(), currval_oid(), nextval_internal(), pg_get_sequence_data(), pg_sequence_last_value(), ResetSequence(), SequenceChangePersistence(), and SetSequence().

◆ lastval()

Datum lastval ( PG_FUNCTION_ARGS  )

Definition at line 898 of file sequence.c.

899{
900 Relation seqrel;
901 int64 result;
902
903 if (last_used_seq == NULL)
905 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
906 errmsg("lastval is not yet defined in this session")));
907
908 /* Someone may have dropped the sequence since the last nextval() */
911 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
912 errmsg("lastval is not yet defined in this session")));
913
915
916 /* nextval() must have already been called for this sequence */
918
922 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
923 errmsg("permission denied for sequence %s",
924 RelationGetRelationName(seqrel))));
925
926 result = last_used_seq->last;
927 sequence_close(seqrel, NoLock);
928
929 PG_RETURN_INT64(result);
930}
static SeqTableData * last_used_seq
Definition: sequence.c:87
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:100

References ACL_SELECT, ACL_USAGE, ACLCHECK_OK, Assert(), ereport, errcode(), errmsg(), ERROR, GetUserId(), SeqTableData::last, last_used_seq, SeqTableData::last_valid, lock_and_open_sequence(), NoLock, ObjectIdGetDatum(), pg_class_aclcheck(), PG_RETURN_INT64, RelationGetRelationName, SeqTableData::relid, SearchSysCacheExists1, and sequence_close().

◆ lock_and_open_sequence()

static Relation lock_and_open_sequence ( SeqTable  seq)
static

Definition at line 1086 of file sequence.c.

1087{
1088 LocalTransactionId thislxid = MyProc->vxid.lxid;
1089
1090 /* Get the lock if not already held in this xact */
1091 if (seq->lxid != thislxid)
1092 {
1093 ResourceOwner currentOwner;
1094
1095 currentOwner = CurrentResourceOwner;
1097
1099
1100 CurrentResourceOwner = currentOwner;
1101
1102 /* Flag that we have a lock in the current xact */
1103 seq->lxid = thislxid;
1104 }
1105
1106 /* We now know we have the lock, and can safely open the rel */
1107 return sequence_open(seq->relid, NoLock);
1108}
uint32 LocalTransactionId
Definition: c.h:662
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:107
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:175
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
PGPROC * MyProc
Definition: proc.c:67
LocalTransactionId lxid
Definition: proc.h:217
struct PGPROC::@130 vxid

References CurrentResourceOwner, LockRelationOid(), SeqTableData::lxid, PGPROC::lxid, MyProc, NoLock, SeqTableData::relid, RowExclusiveLock, sequence_open(), TopTransactionResourceOwner, and PGPROC::vxid.

Referenced by init_sequence(), and lastval().

◆ nextval()

Datum nextval ( PG_FUNCTION_ARGS  )

Definition at line 594 of file sequence.c.

595{
596 text *seqin = PG_GETARG_TEXT_PP(0);
597 RangeVar *sequence;
598 Oid relid;
599
601
602 /*
603 * XXX: This is not safe in the presence of concurrent DDL, but acquiring
604 * a lock here is more expensive than letting nextval_internal do it,
605 * since the latter maintains a cache that keeps us from hitting the lock
606 * manager more than once per transaction. It's not clear whether the
607 * performance penalty is material in practice, but for now, we do it this
608 * way.
609 */
610 relid = RangeVarGetRelid(sequence, NoLock, false);
611
612 PG_RETURN_INT64(nextval_internal(relid, true));
613}
int64 nextval_internal(Oid relid, bool check_permissions)
Definition: sequence.c:624
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
RangeVar * makeRangeVarFromNameList(const List *names)
Definition: namespace.c:3624
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:98
Definition: c.h:695
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:2697

References makeRangeVarFromNameList(), nextval_internal(), NoLock, PG_GETARG_TEXT_PP, PG_RETURN_INT64, RangeVarGetRelid, and textToQualifiedNameList().

Referenced by autoinc().

◆ nextval_internal()

int64 nextval_internal ( Oid  relid,
bool  check_permissions 
)

Definition at line 624 of file sequence.c.

625{
626 SeqTable elm;
627 Relation seqrel;
628 Buffer buf;
629 Page page;
630 HeapTuple pgstuple;
631 Form_pg_sequence pgsform;
632 HeapTupleData seqdatatuple;
634 int64 incby,
635 maxv,
636 minv,
637 cache,
638 log,
639 fetch,
640 last;
641 int64 result,
642 next,
643 rescnt = 0;
644 bool cycle;
645 bool logit = false;
646
647 /* open and lock sequence */
648 init_sequence(relid, &elm, &seqrel);
649
650 if (check_permissions &&
654 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
655 errmsg("permission denied for sequence %s",
656 RelationGetRelationName(seqrel))));
657
658 /* read-only transactions may only modify temp sequences */
659 if (!seqrel->rd_islocaltemp)
660 PreventCommandIfReadOnly("nextval()");
661
662 /*
663 * Forbid this during parallel operation because, to make it work, the
664 * cooperating backends would need to share the backend-local cached
665 * sequence information. Currently, we don't support that.
666 */
667 PreventCommandIfParallelMode("nextval()");
668
669 if (elm->last != elm->cached) /* some numbers were cached */
670 {
671 Assert(elm->last_valid);
672 Assert(elm->increment != 0);
673 elm->last += elm->increment;
674 sequence_close(seqrel, NoLock);
675 last_used_seq = elm;
676 return elm->last;
677 }
678
679 pgstuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(relid));
680 if (!HeapTupleIsValid(pgstuple))
681 elog(ERROR, "cache lookup failed for sequence %u", relid);
682 pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
683 incby = pgsform->seqincrement;
684 maxv = pgsform->seqmax;
685 minv = pgsform->seqmin;
686 cache = pgsform->seqcache;
687 cycle = pgsform->seqcycle;
688 ReleaseSysCache(pgstuple);
689
690 /* lock page buffer and read tuple */
691 seq = read_seq_tuple(seqrel, &buf, &seqdatatuple);
692 page = BufferGetPage(buf);
693
694 last = next = result = seq->last_value;
695 fetch = cache;
696 log = seq->log_cnt;
697
698 if (!seq->is_called)
699 {
700 rescnt++; /* return last_value if not is_called */
701 fetch--;
702 }
703
704 /*
705 * Decide whether we should emit a WAL log record. If so, force up the
706 * fetch count to grab SEQ_LOG_VALS more values than we actually need to
707 * cache. (These will then be usable without logging.)
708 *
709 * If this is the first nextval after a checkpoint, we must force a new
710 * WAL record to be written anyway, else replay starting from the
711 * checkpoint would fail to advance the sequence past the logged values.
712 * In this case we may as well fetch extra values.
713 */
714 if (log < fetch || !seq->is_called)
715 {
716 /* forced log to satisfy local demand for values */
717 fetch = log = fetch + SEQ_LOG_VALS;
718 logit = true;
719 }
720 else
721 {
722 XLogRecPtr redoptr = GetRedoRecPtr();
723
724 if (PageGetLSN(page) <= redoptr)
725 {
726 /* last update of seq was before checkpoint */
727 fetch = log = fetch + SEQ_LOG_VALS;
728 logit = true;
729 }
730 }
731
732 while (fetch) /* try to fetch cache [+ log ] numbers */
733 {
734 /*
735 * Check MAXVALUE for ascending sequences and MINVALUE for descending
736 * sequences
737 */
738 if (incby > 0)
739 {
740 /* ascending sequence */
741 if ((maxv >= 0 && next > maxv - incby) ||
742 (maxv < 0 && next + incby > maxv))
743 {
744 if (rescnt > 0)
745 break; /* stop fetching */
746 if (!cycle)
748 (errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
749 errmsg("nextval: reached maximum value of sequence \"%s\" (%" PRId64 ")",
751 maxv)));
752 next = minv;
753 }
754 else
755 next += incby;
756 }
757 else
758 {
759 /* descending sequence */
760 if ((minv < 0 && next < minv - incby) ||
761 (minv >= 0 && next + incby < minv))
762 {
763 if (rescnt > 0)
764 break; /* stop fetching */
765 if (!cycle)
767 (errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
768 errmsg("nextval: reached minimum value of sequence \"%s\" (%" PRId64 ")",
770 minv)));
771 next = maxv;
772 }
773 else
774 next += incby;
775 }
776 fetch--;
777 if (rescnt < cache)
778 {
779 log--;
780 rescnt++;
781 last = next;
782 if (rescnt == 1) /* if it's first result - */
783 result = next; /* it's what to return */
784 }
785 }
786
787 log -= fetch; /* adjust for any unfetched numbers */
788 Assert(log >= 0);
789
790 /* save info in local cache */
791 elm->increment = incby;
792 elm->last = result; /* last returned number */
793 elm->cached = last; /* last fetched number */
794 elm->last_valid = true;
795
796 last_used_seq = elm;
797
798 /*
799 * If something needs to be WAL logged, acquire an xid, so this
800 * transaction's commit will trigger a WAL flush and wait for syncrep.
801 * It's sufficient to ensure the toplevel transaction has an xid, no need
802 * to assign xids subxacts, that'll already trigger an appropriate wait.
803 * (Have to do that here, so we're outside the critical section)
804 */
805 if (logit && RelationNeedsWAL(seqrel))
807
808 /* ready to change the on-disk (or really, in-buffer) tuple */
810
811 /*
812 * We must mark the buffer dirty before doing XLogInsert(); see notes in
813 * SyncOneBuffer(). However, we don't apply the desired changes just yet.
814 * This looks like a violation of the buffer update protocol, but it is in
815 * fact safe because we hold exclusive lock on the buffer. Any other
816 * process, including a checkpoint, that tries to examine the buffer
817 * contents will block until we release the lock, and then will see the
818 * final state that we install below.
819 */
821
822 /* XLOG stuff */
823 if (logit && RelationNeedsWAL(seqrel))
824 {
825 xl_seq_rec xlrec;
826 XLogRecPtr recptr;
827
828 /*
829 * We don't log the current state of the tuple, but rather the state
830 * as it would appear after "log" more fetches. This lets us skip
831 * that many future WAL records, at the cost that we lose those
832 * sequence values if we crash.
833 */
836
837 /* set values that will be saved in xlog */
838 seq->last_value = next;
839 seq->is_called = true;
840 seq->log_cnt = 0;
841
842 xlrec.locator = seqrel->rd_locator;
843
844 XLogRegisterData(&xlrec, sizeof(xl_seq_rec));
845 XLogRegisterData(seqdatatuple.t_data, seqdatatuple.t_len);
846
847 recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG);
848
849 PageSetLSN(page, recptr);
850 }
851
852 /* Now update sequence tuple to the intended final state */
853 seq->last_value = last; /* last fetched number */
854 seq->is_called = true;
855 seq->log_cnt = log; /* how much is logged */
856
858
860
861 sequence_close(seqrel, NoLock);
862
863 return result;
864}
static int32 next
Definition: blutils.c:224
static XLogRecPtr PageGetLSN(const PageData *page)
Definition: bufpage.h:385
#define SEQ_LOG_VALS
Definition: sequence.c:58
#define ACL_UPDATE
Definition: parsenodes.h:78
bool rd_islocaltemp
Definition: rel.h:61
int64 increment
Definition: sequence.c:75
void PreventCommandIfReadOnly(const char *cmdname)
Definition: utility.c:406
void PreventCommandIfParallelMode(const char *cmdname)
Definition: utility.c:424
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6509

References ACL_UPDATE, ACL_USAGE, ACLCHECK_OK, Assert(), buf, BufferGetPage(), SeqTableData::cached, elog, END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, GetRedoRecPtr(), GETSTRUCT(), GetTopTransactionId(), GetUserId(), HeapTupleIsValid, SeqTableData::increment, init_sequence(), FormData_pg_sequence_data::is_called, SeqTableData::last, last_used_seq, SeqTableData::last_valid, FormData_pg_sequence_data::last_value, xl_seq_rec::locator, FormData_pg_sequence_data::log_cnt, MarkBufferDirty(), next, NoLock, ObjectIdGetDatum(), PageGetLSN(), PageSetLSN(), pg_class_aclcheck(), PreventCommandIfParallelMode(), PreventCommandIfReadOnly(), RelationData::rd_islocaltemp, RelationData::rd_locator, read_seq_tuple(), REGBUF_WILL_INIT, RelationGetRelationName, RelationNeedsWAL, ReleaseSysCache(), SeqTableData::relid, SearchSysCache1(), SEQ_LOG_VALS, sequence_close(), START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleData::t_len, UnlockReleaseBuffer(), XLOG_SEQ_LOG, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by ExecEvalNextValueExpr(), nextval(), and nextval_oid().

◆ nextval_oid()

Datum nextval_oid ( PG_FUNCTION_ARGS  )

Definition at line 616 of file sequence.c.

617{
618 Oid relid = PG_GETARG_OID(0);
619
620 PG_RETURN_INT64(nextval_internal(relid, true));
621}

References nextval_internal(), PG_GETARG_OID, and PG_RETURN_INT64.

◆ pg_get_sequence_data()

Datum pg_get_sequence_data ( PG_FUNCTION_ARGS  )

Definition at line 1793 of file sequence.c.

1794{
1795#define PG_GET_SEQUENCE_DATA_COLS 3
1796 Oid relid = PG_GETARG_OID(0);
1797 SeqTable elm;
1798 Relation seqrel;
1800 bool isnull[PG_GET_SEQUENCE_DATA_COLS] = {0};
1801 TupleDesc resultTupleDesc;
1802 HeapTuple resultHeapTuple;
1803 Datum result;
1804
1806 TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "last_value",
1807 INT8OID, -1, 0);
1808 TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "is_called",
1809 BOOLOID, -1, 0);
1810 TupleDescInitEntry(resultTupleDesc, (AttrNumber) 3, "page_lsn",
1811 LSNOID, -1, 0);
1812 resultTupleDesc = BlessTupleDesc(resultTupleDesc);
1813
1814 init_sequence(relid, &elm, &seqrel);
1815
1816 /*
1817 * Return all NULLs for sequences for which we lack privileges, other
1818 * sessions' temporary sequences, and unlogged sequences on standbys.
1819 */
1821 !RELATION_IS_OTHER_TEMP(seqrel) &&
1823 {
1824 Buffer buf;
1825 HeapTupleData seqtuple;
1827 Page page;
1828
1829 seq = read_seq_tuple(seqrel, &buf, &seqtuple);
1830 page = BufferGetPage(buf);
1831
1832 values[0] = Int64GetDatum(seq->last_value);
1833 values[1] = BoolGetDatum(seq->is_called);
1834 values[2] = LSNGetDatum(PageGetLSN(page));
1835
1837 }
1838 else
1839 memset(isnull, true, sizeof(isnull));
1840
1841 sequence_close(seqrel, NoLock);
1842
1843 resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
1844 result = HeapTupleGetDatum(resultHeapTuple);
1845 PG_RETURN_DATUM(result);
1846#undef PG_GET_SEQUENCE_DATA_COLS
1847}
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define PG_GET_SEQUENCE_DATA_COLS
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2260
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:31
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:668
#define RelationIsPermanent(relation)
Definition: rel.h:627
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:182
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:842
bool RecoveryInProgress(void)
Definition: xlog.c:6406

References ACL_SELECT, ACLCHECK_OK, BlessTupleDesc(), BoolGetDatum(), buf, BufferGetPage(), CreateTemplateTupleDesc(), GetUserId(), heap_form_tuple(), HeapTupleGetDatum(), init_sequence(), Int64GetDatum(), FormData_pg_sequence_data::is_called, FormData_pg_sequence_data::last_value, LSNGetDatum(), NoLock, PageGetLSN(), pg_class_aclcheck(), PG_GET_SEQUENCE_DATA_COLS, PG_GETARG_OID, PG_RETURN_DATUM, read_seq_tuple(), RecoveryInProgress(), RELATION_IS_OTHER_TEMP, RelationIsPermanent, sequence_close(), TupleDescInitEntry(), UnlockReleaseBuffer(), and values.

◆ pg_sequence_last_value()

Datum pg_sequence_last_value ( PG_FUNCTION_ARGS  )

Definition at line 1856 of file sequence.c.

1857{
1858 Oid relid = PG_GETARG_OID(0);
1859 SeqTable elm;
1860 Relation seqrel;
1861 bool is_called = false;
1862 int64 result = 0;
1863
1864 /* open and lock sequence */
1865 init_sequence(relid, &elm, &seqrel);
1866
1867 /*
1868 * We return NULL for other sessions' temporary sequences. The
1869 * pg_sequences system view already filters those out, but this offers a
1870 * defense against ERRORs in case someone invokes this function directly.
1871 *
1872 * Also, for the benefit of the pg_sequences view, we return NULL for
1873 * unlogged sequences on standbys and for sequences for which the current
1874 * user lacks privileges instead of throwing an error.
1875 */
1877 !RELATION_IS_OTHER_TEMP(seqrel) &&
1879 {
1880 Buffer buf;
1881 HeapTupleData seqtuple;
1883
1884 seq = read_seq_tuple(seqrel, &buf, &seqtuple);
1885
1886 is_called = seq->is_called;
1887 result = seq->last_value;
1888
1890 }
1891 sequence_close(seqrel, NoLock);
1892
1893 if (is_called)
1894 PG_RETURN_INT64(result);
1895 else
1897}
#define PG_RETURN_NULL()
Definition: fmgr.h:345

References ACL_SELECT, ACL_USAGE, ACLCHECK_OK, buf, GetUserId(), init_sequence(), FormData_pg_sequence_data::is_called, FormData_pg_sequence_data::last_value, NoLock, pg_class_aclcheck(), PG_GETARG_OID, PG_RETURN_INT64, PG_RETURN_NULL, read_seq_tuple(), RecoveryInProgress(), RELATION_IS_OTHER_TEMP, RelationIsPermanent, sequence_close(), and UnlockReleaseBuffer().

◆ pg_sequence_parameters()

Datum pg_sequence_parameters ( PG_FUNCTION_ARGS  )

Definition at line 1746 of file sequence.c.

1747{
1748 Oid relid = PG_GETARG_OID(0);
1749 TupleDesc tupdesc;
1750 Datum values[7];
1751 bool isnull[7];
1752 HeapTuple pgstuple;
1753 Form_pg_sequence pgsform;
1754
1756 ereport(ERROR,
1757 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1758 errmsg("permission denied for sequence %s",
1759 get_rel_name(relid))));
1760
1761 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1762 elog(ERROR, "return type must be a row type");
1763
1764 memset(isnull, 0, sizeof(isnull));
1765
1766 pgstuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(relid));
1767 if (!HeapTupleIsValid(pgstuple))
1768 elog(ERROR, "cache lookup failed for sequence %u", relid);
1769 pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
1770
1771 values[0] = Int64GetDatum(pgsform->seqstart);
1772 values[1] = Int64GetDatum(pgsform->seqmin);
1773 values[2] = Int64GetDatum(pgsform->seqmax);
1774 values[3] = Int64GetDatum(pgsform->seqincrement);
1775 values[4] = BoolGetDatum(pgsform->seqcycle);
1776 values[5] = Int64GetDatum(pgsform->seqcache);
1777 values[6] = ObjectIdGetDatum(pgsform->seqtypid);
1778
1779 ReleaseSysCache(pgstuple);
1780
1781 return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
1782}
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2095

References ACL_SELECT, ACL_UPDATE, ACL_USAGE, ACLCHECK_OK, BoolGetDatum(), elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), get_rel_name(), GETSTRUCT(), GetUserId(), heap_form_tuple(), HeapTupleGetDatum(), HeapTupleIsValid, Int64GetDatum(), ObjectIdGetDatum(), pg_class_aclcheck(), PG_GETARG_OID, ReleaseSysCache(), SearchSysCache1(), TYPEFUNC_COMPOSITE, and values.

◆ process_owned_by()

static void process_owned_by ( Relation  seqrel,
List owned_by,
bool  for_identity 
)
static

Definition at line 1598 of file sequence.c.

1599{
1600 DependencyType deptype;
1601 int nnames;
1602 Relation tablerel;
1604
1605 deptype = for_identity ? DEPENDENCY_INTERNAL : DEPENDENCY_AUTO;
1606
1607 nnames = list_length(owned_by);
1608 Assert(nnames > 0);
1609 if (nnames == 1)
1610 {
1611 /* Must be OWNED BY NONE */
1612 if (strcmp(strVal(linitial(owned_by)), "none") != 0)
1613 ereport(ERROR,
1614 (errcode(ERRCODE_SYNTAX_ERROR),
1615 errmsg("invalid OWNED BY option"),
1616 errhint("Specify OWNED BY table.column or OWNED BY NONE.")));
1617 tablerel = NULL;
1618 attnum = 0;
1619 }
1620 else
1621 {
1622 List *relname;
1623 char *attrname;
1624 RangeVar *rel;
1625
1626 /* Separate relname and attr name */
1627 relname = list_copy_head(owned_by, nnames - 1);
1628 attrname = strVal(llast(owned_by));
1629
1630 /* Open and lock rel to ensure it won't go away meanwhile */
1632 tablerel = relation_openrv(rel, AccessShareLock);
1633
1634 /* Must be a regular or foreign table */
1635 if (!(tablerel->rd_rel->relkind == RELKIND_RELATION ||
1636 tablerel->rd_rel->relkind == RELKIND_FOREIGN_TABLE ||
1637 tablerel->rd_rel->relkind == RELKIND_VIEW ||
1638 tablerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE))
1639 ereport(ERROR,
1640 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1641 errmsg("sequence cannot be owned by relation \"%s\"",
1642 RelationGetRelationName(tablerel)),
1643 errdetail_relkind_not_supported(tablerel->rd_rel->relkind)));
1644
1645 /* We insist on same owner and schema */
1646 if (seqrel->rd_rel->relowner != tablerel->rd_rel->relowner)
1647 ereport(ERROR,
1648 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1649 errmsg("sequence must have same owner as table it is linked to")));
1650 if (RelationGetNamespace(seqrel) != RelationGetNamespace(tablerel))
1651 ereport(ERROR,
1652 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1653 errmsg("sequence must be in same schema as table it is linked to")));
1654
1655 /* Now, fetch the attribute number from the system cache */
1656 attnum = get_attnum(RelationGetRelid(tablerel), attrname);
1658 ereport(ERROR,
1659 (errcode(ERRCODE_UNDEFINED_COLUMN),
1660 errmsg("column \"%s\" of relation \"%s\" does not exist",
1661 attrname, RelationGetRelationName(tablerel))));
1662 }
1663
1664 /*
1665 * Catch user explicitly running OWNED BY on identity sequence.
1666 */
1667 if (deptype == DEPENDENCY_AUTO)
1668 {
1669 Oid tableId;
1670 int32 colId;
1671
1672 if (sequenceIsOwned(RelationGetRelid(seqrel), DEPENDENCY_INTERNAL, &tableId, &colId))
1673 ereport(ERROR,
1674 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1675 errmsg("cannot change ownership of identity sequence"),
1676 errdetail("Sequence \"%s\" is linked to table \"%s\".",
1678 get_rel_name(tableId))));
1679 }
1680
1681 /*
1682 * OK, we are ready to update pg_depend. First remove any existing
1683 * dependencies for the sequence, then optionally add a new one.
1684 */
1685 deleteDependencyRecordsForClass(RelationRelationId, RelationGetRelid(seqrel),
1686 RelationRelationId, deptype);
1687
1688 if (tablerel)
1689 {
1690 ObjectAddress refobject,
1691 depobject;
1692
1693 refobject.classId = RelationRelationId;
1694 refobject.objectId = RelationGetRelid(tablerel);
1695 refobject.objectSubId = attnum;
1696 depobject.classId = RelationRelationId;
1697 depobject.objectId = RelationGetRelid(seqrel);
1698 depobject.objectSubId = 0;
1699 recordDependencyOn(&depobject, &refobject, deptype);
1700 }
1701
1702 /* Done, but hold lock until commit */
1703 if (tablerel)
1704 relation_close(tablerel, NoLock);
1705}
#define InvalidAttrNumber
Definition: attnum.h:23
int32_t int32
Definition: c.h:537
DependencyType
Definition: dependency.h:32
@ DEPENDENCY_AUTO
Definition: dependency.h:34
@ DEPENDENCY_INTERNAL
Definition: dependency.h:35
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errhint(const char *fmt,...)
Definition: elog.c:1330
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1593
#define AccessShareLock
Definition: lockdefs.h:36
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:951
int16 attnum
Definition: pg_attribute.h:74
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
NameData relname
Definition: pg_class.h:38
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:45
long deleteDependencyRecordsForClass(Oid classId, Oid objectId, Oid refclassId, char deptype)
Definition: pg_depend.c:351
bool sequenceIsOwned(Oid seqId, char deptype, Oid *tableId, int32 *colId)
Definition: pg_depend.c:828
#define llast(l)
Definition: pg_list.h:198
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define RelationGetRelid(relation)
Definition: rel.h:515
#define RelationGetNamespace(relation)
Definition: rel.h:556
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:137
#define strVal(v)
Definition: value.h:82

References AccessShareLock, Assert(), attnum, ObjectAddress::classId, deleteDependencyRecordsForClass(), DEPENDENCY_AUTO, DEPENDENCY_INTERNAL, ereport, errcode(), errdetail(), errdetail_relkind_not_supported(), errhint(), errmsg(), ERROR, get_attnum(), get_rel_name(), InvalidAttrNumber, linitial, list_copy_head(), list_length(), llast, makeRangeVarFromNameList(), NoLock, ObjectAddress::objectId, ObjectAddress::objectSubId, RelationData::rd_rel, recordDependencyOn(), relation_close(), relation_openrv(), RelationGetNamespace, RelationGetRelationName, RelationGetRelid, relname, sequenceIsOwned(), and strVal.

Referenced by AlterSequence(), and DefineSequence().

◆ read_seq_tuple()

static Form_pg_sequence_data read_seq_tuple ( Relation  rel,
Buffer buf,
HeapTuple  seqdatatuple 
)
static

Definition at line 1191 of file sequence.c.

1192{
1193 Page page;
1194 ItemId lp;
1195 sequence_magic *sm;
1197
1198 *buf = ReadBuffer(rel, 0);
1200
1201 page = BufferGetPage(*buf);
1203
1204 if (sm->magic != SEQ_MAGIC)
1205 elog(ERROR, "bad magic number in sequence \"%s\": %08X",
1206 RelationGetRelationName(rel), sm->magic);
1207
1208 lp = PageGetItemId(page, FirstOffsetNumber);
1210
1211 /* Note we currently only bother to set these two fields of *seqdatatuple */
1212 seqdatatuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
1213 seqdatatuple->t_len = ItemIdGetLength(lp);
1214
1215 /*
1216 * Previous releases of Postgres neglected to prevent SELECT FOR UPDATE on
1217 * a sequence, which would leave a non-frozen XID in the sequence tuple's
1218 * xmax, which eventually leads to clog access failures or worse. If we
1219 * see this has happened, clean up after it. We treat this like a hint
1220 * bit update, ie, don't bother to WAL-log it, since we can certainly do
1221 * this again if the update gets lost.
1222 */
1223 Assert(!(seqdatatuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI));
1225 {
1227 seqdatatuple->t_data->t_infomask &= ~HEAP_XMAX_COMMITTED;
1228 seqdatatuple->t_data->t_infomask |= HEAP_XMAX_INVALID;
1229 MarkBufferDirtyHint(*buf, true);
1230 }
1231
1232 seq = (Form_pg_sequence_data) GETSTRUCT(seqdatatuple);
1233
1234 return seq;
1235}
void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition: bufmgr.c:5604
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:5430
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:745
@ BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:207
static void * PageGetItem(const PageData *page, const ItemIdData *itemId)
Definition: bufpage.h:353
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:243
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
static TransactionId HeapTupleHeaderGetRawXmax(const HeapTupleHeaderData *tup)
Definition: htup_details.h:377
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:209
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99

References Assert(), buf, BUFFER_LOCK_EXCLUSIVE, BufferGetPage(), elog, ERROR, FirstOffsetNumber, GETSTRUCT(), HEAP_XMAX_INVALID, HEAP_XMAX_IS_MULTI, HeapTupleHeaderGetRawXmax(), HeapTupleHeaderSetXmax(), InvalidTransactionId, ItemIdGetLength, ItemIdIsNormal, LockBuffer(), sequence_magic::magic, MarkBufferDirtyHint(), PageGetItem(), PageGetItemId(), PageGetSpecialPointer, ReadBuffer(), RelationGetRelationName, SEQ_MAGIC, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, and HeapTupleData::t_len.

Referenced by AlterSequence(), nextval_internal(), pg_get_sequence_data(), pg_sequence_last_value(), ResetSequence(), SequenceChangePersistence(), and SetSequence().

◆ ResetSequence()

void ResetSequence ( Oid  seq_relid)

Definition at line 255 of file sequence.c.

256{
257 Relation seq_rel;
258 SeqTable elm;
260 Buffer buf;
261 HeapTupleData seqdatatuple;
262 HeapTuple tuple;
263 HeapTuple pgstuple;
264 Form_pg_sequence pgsform;
265 int64 startv;
266
267 /*
268 * Read the old sequence. This does a bit more work than really
269 * necessary, but it's simple, and we do want to double-check that it's
270 * indeed a sequence.
271 */
272 init_sequence(seq_relid, &elm, &seq_rel);
273 (void) read_seq_tuple(seq_rel, &buf, &seqdatatuple);
274
275 pgstuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(seq_relid));
276 if (!HeapTupleIsValid(pgstuple))
277 elog(ERROR, "cache lookup failed for sequence %u", seq_relid);
278 pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
279 startv = pgsform->seqstart;
280 ReleaseSysCache(pgstuple);
281
282 /*
283 * Copy the existing sequence tuple.
284 */
285 tuple = heap_copytuple(&seqdatatuple);
286
287 /* Now we're done with the old page */
289
290 /*
291 * Modify the copied tuple to execute the restart (compare the RESTART
292 * action in AlterSequence)
293 */
294 seq = (Form_pg_sequence_data) GETSTRUCT(tuple);
295 seq->last_value = startv;
296 seq->is_called = false;
297 seq->log_cnt = 0;
298
299 /*
300 * Create a new storage file for the sequence.
301 */
302 RelationSetNewRelfilenumber(seq_rel, seq_rel->rd_rel->relpersistence);
303
304 /*
305 * Ensure sequence's relfrozenxid is at 0, since it won't contain any
306 * unfrozen XIDs. Same with relminmxid, since a sequence will never
307 * contain multixacts.
308 */
309 Assert(seq_rel->rd_rel->relfrozenxid == InvalidTransactionId);
310 Assert(seq_rel->rd_rel->relminmxid == InvalidMultiXactId);
311
312 /*
313 * Insert the modified tuple into the new storage file.
314 */
315 fill_seq_with_data(seq_rel, tuple);
316
317 /* Clear local cache so that we don't think we have cached numbers */
318 /* Note that we do not change the currval() state */
319 elm->cached = elm->last;
320
321 sequence_close(seq_rel, NoLock);
322}

References Assert(), buf, SeqTableData::cached, elog, ERROR, fill_seq_with_data(), GETSTRUCT(), heap_copytuple(), HeapTupleIsValid, init_sequence(), InvalidMultiXactId, InvalidTransactionId, FormData_pg_sequence_data::is_called, SeqTableData::last, FormData_pg_sequence_data::last_value, FormData_pg_sequence_data::log_cnt, NoLock, ObjectIdGetDatum(), RelationData::rd_rel, read_seq_tuple(), RelationSetNewRelfilenumber(), ReleaseSysCache(), SearchSysCache1(), sequence_close(), and UnlockReleaseBuffer().

Referenced by ExecuteTruncateGuts().

◆ ResetSequenceCaches()

void ResetSequenceCaches ( void  )

Definition at line 1903 of file sequence.c.

1904{
1905 if (seqhashtab)
1906 {
1908 seqhashtab = NULL;
1909 }
1910
1911 last_used_seq = NULL;
1912}
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:865

References hash_destroy(), last_used_seq, and seqhashtab.

Referenced by DiscardAll(), and DiscardCommand().

◆ sequence_options()

List * sequence_options ( Oid  relid)

Definition at line 1712 of file sequence.c.

1713{
1714 HeapTuple pgstuple;
1715 Form_pg_sequence pgsform;
1716 List *options = NIL;
1717
1718 pgstuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(relid));
1719 if (!HeapTupleIsValid(pgstuple))
1720 elog(ERROR, "cache lookup failed for sequence %u", relid);
1721 pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
1722
1723 /* Use makeFloat() for 64-bit integers, like gram.y does. */
1725 makeDefElem("cache", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqcache)), -1));
1727 makeDefElem("cycle", (Node *) makeBoolean(pgsform->seqcycle), -1));
1729 makeDefElem("increment", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqincrement)), -1));
1731 makeDefElem("maxvalue", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqmax)), -1));
1733 makeDefElem("minvalue", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqmin)), -1));
1735 makeDefElem("start", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqstart)), -1));
1736
1737 ReleaseSysCache(pgstuple);
1738
1739 return options;
1740}
#define INT64_FORMAT
Definition: c.h:559
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition: makefuncs.c:637
static char ** options
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
Definition: nodes.h:135
Float * makeFloat(char *numericStr)
Definition: value.c:37
Boolean * makeBoolean(bool val)
Definition: value.c:49

References elog, ERROR, GETSTRUCT(), HeapTupleIsValid, INT64_FORMAT, lappend(), makeBoolean(), makeDefElem(), makeFloat(), NIL, ObjectIdGetDatum(), options, psprintf(), ReleaseSysCache(), and SearchSysCache1().

Referenced by transformTableLikeClause().

◆ SequenceChangePersistence()

void SequenceChangePersistence ( Oid  relid,
char  newrelpersistence 
)

Definition at line 542 of file sequence.c.

543{
544 SeqTable elm;
545 Relation seqrel;
546 Buffer buf;
547 HeapTupleData seqdatatuple;
548
549 /*
550 * ALTER SEQUENCE acquires this lock earlier. If we're processing an
551 * owned sequence for ALTER TABLE, lock now. Without the lock, we'd
552 * discard increments from nextval() calls (in other sessions) between
553 * this function's buffer unlock and this transaction's commit.
554 */
556 init_sequence(relid, &elm, &seqrel);
557
558 /* check the comment above nextval_internal()'s equivalent call. */
559 if (RelationNeedsWAL(seqrel))
561
562 (void) read_seq_tuple(seqrel, &buf, &seqdatatuple);
563 RelationSetNewRelfilenumber(seqrel, newrelpersistence);
564 fill_seq_with_data(seqrel, &seqdatatuple);
566
567 sequence_close(seqrel, NoLock);
568}

References AccessExclusiveLock, buf, fill_seq_with_data(), GetTopTransactionId(), init_sequence(), LockRelationOid(), NoLock, read_seq_tuple(), RelationNeedsWAL, RelationSetNewRelfilenumber(), sequence_close(), and UnlockReleaseBuffer().

Referenced by ATRewriteTables().

◆ SetSequence()

void SetSequence ( Oid  relid,
int64  next,
bool  iscalled 
)

Definition at line 946 of file sequence.c.

947{
948 SeqTable elm;
949 Relation seqrel;
950 Buffer buf;
951 HeapTupleData seqdatatuple;
953 HeapTuple pgstuple;
954 Form_pg_sequence pgsform;
955 int64 maxv,
956 minv;
957
958 /* open and lock sequence */
959 init_sequence(relid, &elm, &seqrel);
960
963 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
964 errmsg("permission denied for sequence %s",
965 RelationGetRelationName(seqrel))));
966
967 pgstuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(relid));
968 if (!HeapTupleIsValid(pgstuple))
969 elog(ERROR, "cache lookup failed for sequence %u", relid);
970 pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
971 maxv = pgsform->seqmax;
972 minv = pgsform->seqmin;
973 ReleaseSysCache(pgstuple);
974
975 /* read-only transactions may only modify temp sequences */
976 if (!seqrel->rd_islocaltemp)
977 PreventCommandIfReadOnly("setval()");
978
979 /*
980 * Forbid this during parallel operation because, to make it work, the
981 * cooperating backends would need to share the backend-local cached
982 * sequence information. Currently, we don't support that.
983 */
985
986 /* lock page buffer and read tuple */
987 seq = read_seq_tuple(seqrel, &buf, &seqdatatuple);
988
989 if ((next < minv) || (next > maxv))
991 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
992 errmsg("setval: value %" PRId64 " is out of bounds for sequence \"%s\" (%" PRId64 "..%" PRId64 ")",
994 minv, maxv)));
995
996 /* Set the currval() state only if iscalled = true */
997 if (iscalled)
998 {
999 elm->last = next; /* last returned number */
1000 elm->last_valid = true;
1001 }
1002
1003 /* In any case, forget any future cached numbers */
1004 elm->cached = elm->last;
1005
1006 /* check the comment above nextval_internal()'s equivalent call. */
1007 if (RelationNeedsWAL(seqrel))
1009
1010 /* ready to change the on-disk (or really, in-buffer) tuple */
1012
1013 seq->last_value = next; /* last fetched number */
1014 seq->is_called = iscalled;
1015 seq->log_cnt = 0;
1016
1018
1019 /* XLOG stuff */
1020 if (RelationNeedsWAL(seqrel))
1021 {
1022 xl_seq_rec xlrec;
1023 XLogRecPtr recptr;
1024 Page page = BufferGetPage(buf);
1025
1028
1029 xlrec.locator = seqrel->rd_locator;
1030 XLogRegisterData(&xlrec, sizeof(xl_seq_rec));
1031 XLogRegisterData(seqdatatuple.t_data, seqdatatuple.t_len);
1032
1033 recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG);
1034
1035 PageSetLSN(page, recptr);
1036 }
1037
1039
1041
1042 sequence_close(seqrel, NoLock);
1043}

References ACL_UPDATE, ACLCHECK_OK, buf, BufferGetPage(), SeqTableData::cached, elog, END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, GETSTRUCT(), GetTopTransactionId(), GetUserId(), HeapTupleIsValid, init_sequence(), FormData_pg_sequence_data::is_called, SeqTableData::last, SeqTableData::last_valid, FormData_pg_sequence_data::last_value, xl_seq_rec::locator, FormData_pg_sequence_data::log_cnt, MarkBufferDirty(), next, NoLock, ObjectIdGetDatum(), PageSetLSN(), pg_class_aclcheck(), PreventCommandIfParallelMode(), PreventCommandIfReadOnly(), RelationData::rd_islocaltemp, RelationData::rd_locator, read_seq_tuple(), REGBUF_WILL_INIT, RelationGetRelationName, RelationNeedsWAL, ReleaseSysCache(), SeqTableData::relid, SearchSysCache1(), sequence_close(), START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleData::t_len, UnlockReleaseBuffer(), XLOG_SEQ_LOG, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by copy_sequence(), setval3_oid(), and setval_oid().

◆ setval3_oid()

Datum setval3_oid ( PG_FUNCTION_ARGS  )

Definition at line 1065 of file sequence.c.

1066{
1067 Oid relid = PG_GETARG_OID(0);
1069 bool iscalled = PG_GETARG_BOOL(2);
1070
1071 SetSequence(relid, next, iscalled);
1072
1074}
void SetSequence(Oid relid, int64 next, bool iscalled)
Definition: sequence.c:946
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274

References next, PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_INT64, and SetSequence().

◆ setval_oid()

Datum setval_oid ( PG_FUNCTION_ARGS  )

Definition at line 1050 of file sequence.c.

1051{
1052 Oid relid = PG_GETARG_OID(0);
1054
1055 SetSequence(relid, next, true);
1056
1058}

References next, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_INT64, and SetSequence().

Variable Documentation

◆ last_used_seq

SeqTableData* last_used_seq = NULL
static

Definition at line 87 of file sequence.c.

Referenced by lastval(), nextval_internal(), and ResetSequenceCaches().

◆ seqhashtab

HTAB* seqhashtab = NULL
static

Definition at line 81 of file sequence.c.

Referenced by create_seq_hashtable(), init_sequence(), and ResetSequenceCaches().