PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
sequence.h File Reference
#include "access/xlogreader.h"
#include "catalog/objectaddress.h"
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "nodes/parsenodes.h"
#include "parser/parse_node.h"
#include "storage/relfilenode.h"
Include dependency graph for sequence.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  FormData_pg_sequence_data
 
struct  xl_seq_rec
 

Macros

#define SEQ_COL_LASTVAL   1
 
#define SEQ_COL_LOG   2
 
#define SEQ_COL_CALLED   3
 
#define SEQ_COL_FIRSTCOL   SEQ_COL_LASTVAL
 
#define SEQ_COL_LASTCOL   SEQ_COL_CALLED
 
#define XLOG_SEQ_LOG   0x00
 

Typedefs

typedef struct
FormData_pg_sequence_data 
FormData_pg_sequence_data
 
typedef FormData_pg_sequence_dataForm_pg_sequence_data
 
typedef struct xl_seq_rec xl_seq_rec
 

Functions

int64 nextval_internal (Oid relid, bool check_permissions)
 
Datum nextval (PG_FUNCTION_ARGS)
 
Listsequence_options (Oid relid)
 
ObjectAddress DefineSequence (ParseState *pstate, CreateSeqStmt *stmt)
 
ObjectAddress AlterSequence (ParseState *pstate, AlterSeqStmt *stmt)
 
void DeleteSequenceTuple (Oid relid)
 
void ResetSequence (Oid seq_relid)
 
void ResetSequenceCaches (void)
 
void seq_redo (XLogReaderState *rptr)
 
void seq_desc (StringInfo buf, XLogReaderState *rptr)
 
const char * seq_identify (uint8 info)
 
void seq_mask (char *pagedata, BlockNumber blkno)
 

Macro Definition Documentation

#define SEQ_COL_CALLED   3

Definition at line 40 of file sequence.h.

Referenced by DefineSequence().

#define SEQ_COL_FIRSTCOL   SEQ_COL_LASTVAL

Definition at line 42 of file sequence.h.

Referenced by DefineSequence().

#define SEQ_COL_LASTCOL   SEQ_COL_CALLED

Definition at line 43 of file sequence.h.

Referenced by DefineSequence().

#define SEQ_COL_LASTVAL   1

Definition at line 38 of file sequence.h.

Referenced by DefineSequence().

#define SEQ_COL_LOG   2

Definition at line 39 of file sequence.h.

Referenced by DefineSequence().

#define XLOG_SEQ_LOG   0x00

Typedef Documentation

Function Documentation

ObjectAddress AlterSequence ( ParseState pstate,
AlterSeqStmt stmt 
)

Definition at line 416 of file sequence.c.

References buf, SeqTableData::cached, CatalogTupleUpdate(), elog, ereport, errmsg(), ERROR, fill_seq_with_data(), AlterSeqStmt::for_identity, GETSTRUCT, GetTopTransactionId(), heap_close, heap_copytuple(), heap_open(), HeapTupleIsValid, init_params(), init_sequence(), InvalidMultiXactId, InvalidObjectAddress, InvalidOid, InvalidTransactionId, InvokeObjectPostAlterHook, SeqTableData::last, AlterSeqStmt::missing_ok, NoLock, NOTICE, NULL, ObjectAddressSet, ObjectIdGetDatum, AlterSeqStmt::options, process_owned_by(), RangeVarCallbackOwnsRelation(), RangeVarGetRelidExtended(), RelationData::rd_rel, read_seq_tuple(), relation_close(), RelationNeedsWAL, RelationRelationId, RelationSetNewRelfilenode(), RangeVar::relname, RowExclusiveLock, SearchSysCacheCopy1, SEQRELID, AlterSeqStmt::sequence, SequenceRelationId, ShareRowExclusiveLock, HeapTupleData::t_self, and UnlockReleaseBuffer().

Referenced by ProcessUtilitySlow().

417 {
418  Oid relid;
419  SeqTable elm;
420  Relation seqrel;
421  Buffer buf;
422  HeapTupleData datatuple;
423  Form_pg_sequence seqform;
424  Form_pg_sequence_data newdataform;
425  bool need_seq_rewrite;
426  List *owned_by;
427  ObjectAddress address;
428  Relation rel;
429  HeapTuple seqtuple;
430  HeapTuple newdatatuple;
431 
432  /* Open and lock sequence, and check for ownership along the way. */
433  relid = RangeVarGetRelidExtended(stmt->sequence,
435  stmt->missing_ok,
436  false,
438  NULL);
439  if (relid == InvalidOid)
440  {
441  ereport(NOTICE,
442  (errmsg("relation \"%s\" does not exist, skipping",
443  stmt->sequence->relname)));
444  return InvalidObjectAddress;
445  }
446 
447  init_sequence(relid, &elm, &seqrel);
448 
450  seqtuple = SearchSysCacheCopy1(SEQRELID,
451  ObjectIdGetDatum(relid));
452  if (!HeapTupleIsValid(seqtuple))
453  elog(ERROR, "cache lookup failed for sequence %u",
454  relid);
455 
456  seqform = (Form_pg_sequence) GETSTRUCT(seqtuple);
457 
458  /* lock page's buffer and read tuple into new sequence structure */
459  (void) read_seq_tuple(seqrel, &buf, &datatuple);
460 
461  /* copy the existing sequence data tuple, so it can be modified localy */
462  newdatatuple = heap_copytuple(&datatuple);
463  newdataform = (Form_pg_sequence_data) GETSTRUCT(newdatatuple);
464 
465  UnlockReleaseBuffer(buf);
466 
467  /* Check and set new values */
468  init_params(pstate, stmt->options, stmt->for_identity, false,
469  seqform, newdataform,
470  &need_seq_rewrite, &owned_by);
471 
472  /* Clear local cache so that we don't think we have cached numbers */
473  /* Note that we do not change the currval() state */
474  elm->cached = elm->last;
475 
476  /* If needed, rewrite the sequence relation itself */
477  if (need_seq_rewrite)
478  {
479  /* check the comment above nextval_internal()'s equivalent call. */
480  if (RelationNeedsWAL(seqrel))
482 
483  /*
484  * Create a new storage file for the sequence, making the state
485  * changes transactional. We want to keep the sequence's relfrozenxid
486  * at 0, since it won't contain any unfrozen XIDs. Same with
487  * relminmxid, since a sequence will never contain multixacts.
488  */
489  RelationSetNewRelfilenode(seqrel, seqrel->rd_rel->relpersistence,
491 
492  /*
493  * Insert the modified tuple into the new storage file.
494  */
495  fill_seq_with_data(seqrel, newdatatuple);
496  }
497 
498  /* process OWNED BY if given */
499  if (owned_by)
500  process_owned_by(seqrel, owned_by, stmt->for_identity);
501 
502  /* update the pg_sequence tuple (we could skip this in some cases...) */
503  CatalogTupleUpdate(rel, &seqtuple->t_self, seqtuple);
504 
506 
507  ObjectAddressSet(address, RelationRelationId, relid);
508 
510  relation_close(seqrel, NoLock);
511 
512  return address;
513 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:608
int64 cached
Definition: sequence.c:79
void RelationSetNewRelfilenode(Relation relation, char persistence, TransactionId freezeXid, MultiXactId minmulti)
Definition: relcache.c:3416
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok, bool nowait, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:218
#define RelationRelationId
Definition: pg_class.h:29
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
#define SequenceRelationId
Definition: pg_sequence.h:6
FormData_pg_sequence * Form_pg_sequence
Definition: pg_sequence.h:20
#define heap_close(r, l)
Definition: heapam.h:97
TransactionId GetTopTransactionId(void)
Definition: xact.c:389
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
List * options
Definition: parsenodes.h:2465
char * relname
Definition: primnodes.h:68
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
void RangeVarCallbackOwnsRelation(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: tablecmds.c:12971
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:66
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidTransactionId
Definition: transam.h:31
static void process_owned_by(Relation seqrel, List *owned_by, bool for_identity)
Definition: sequence.c:1639
int64 last
Definition: sequence.c:78
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
static Form_pg_sequence_data read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
Definition: sequence.c:1168
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define InvalidMultiXactId
Definition: multixact.h:23
#define InvalidOid
Definition: postgres_ext.h:36
RangeVar * sequence
Definition: parsenodes.h:2464
#define NOTICE
Definition: elog.h:37
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define ShareRowExclusiveLock
Definition: lockdefs.h:42
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define RelationNeedsWAL(relation)
Definition: rel.h:505
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:165
static void init_params(ParseState *pstate, List *options, bool for_identity, bool isInit, Form_pg_sequence seqform, Form_pg_sequence_data seqdataform, bool *need_seq_rewrite, List **owned_by)
Definition: sequence.c:1234
static void init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel)
Definition: sequence.c:1101
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool for_identity
Definition: parsenodes.h:2466
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
static void fill_seq_with_data(Relation rel, HeapTuple tuple)
Definition: sequence.c:337
FormData_pg_sequence_data * Form_pg_sequence_data
Definition: sequence.h:32
ObjectAddress DefineSequence ( ParseState pstate,
CreateSeqStmt stmt 
)

Definition at line 116 of file sequence.c.

References AccessExclusiveLock, Anum_pg_sequence_seqcache, Anum_pg_sequence_seqcycle, Anum_pg_sequence_seqincrement, Anum_pg_sequence_seqmax, Anum_pg_sequence_seqmin, Anum_pg_sequence_seqrelid, Anum_pg_sequence_seqstart, Anum_pg_sequence_seqtypid, Assert, BoolGetDatum, BOOLOID, CatalogTupleInsert(), ColumnDef::collClause, ColumnDef::collOid, ColumnDef::colname, ColumnDef::constraints, CreateStmt::constraints, ColumnDef::cooked_default, DefineRelation(), ereport, errcode(), errmsg(), ERROR, fill_seq_with_data(), CreateSeqStmt::for_identity, FormData_pg_sequence, heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), i, CreateStmt::if_not_exists, CreateSeqStmt::if_not_exists, ColumnDef::inhcount, CreateStmt::inhRelations, init_params(), Int64GetDatum(), Int64GetDatumFast, INT8OID, InvalidObjectAddress, InvalidOid, ColumnDef::is_from_parent, ColumnDef::is_from_type, ColumnDef::is_local, ColumnDef::is_not_null, lappend(), FormData_pg_sequence_data::last_value, ColumnDef::location, makeNode, makeTypeNameFromOid(), Natts_pg_sequence, NIL, NoLock, NOTICE, NULL, ObjectAddress::objectId, ObjectIdGetDatum, OidIsValid, CreateStmt::oncommit, ONCOMMIT_NOOP, CreateStmt::options, CreateSeqStmt::options, CreateSeqStmt::ownerId, process_owned_by(), RangeVarGetAndCheckCreationNamespace(), ColumnDef::raw_default, CreateStmt::relation, RelationGetDescr, RELKIND_SEQUENCE, RangeVar::relname, RangeVar::relpersistence, RELPERSISTENCE_UNLOGGED, RowExclusiveLock, SEQ_COL_CALLED, SEQ_COL_FIRSTCOL, SEQ_COL_LASTCOL, SEQ_COL_LASTVAL, SEQ_COL_LOG, CreateSeqStmt::sequence, SequenceRelationId, ColumnDef::storage, CreateStmt::tableElts, CreateStmt::tablespacename, ColumnDef::typeName, and value.

Referenced by ProcessUtilitySlow().

117 {
118  FormData_pg_sequence seqform;
119  FormData_pg_sequence_data seqdataform;
120  bool need_seq_rewrite;
121  List *owned_by;
122  CreateStmt *stmt = makeNode(CreateStmt);
123  Oid seqoid;
124  ObjectAddress address;
125  Relation rel;
126  HeapTuple tuple;
127  TupleDesc tupDesc;
129  bool null[SEQ_COL_LASTCOL];
130  Datum pgs_values[Natts_pg_sequence];
131  bool pgs_nulls[Natts_pg_sequence];
132  int i;
133 
134  /* Unlogged sequences are not implemented -- not clear if useful. */
135  if (seq->sequence->relpersistence == RELPERSISTENCE_UNLOGGED)
136  ereport(ERROR,
137  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
138  errmsg("unlogged sequences are not supported")));
139 
140  /*
141  * If if_not_exists was given and a relation with the same name already
142  * exists, bail out. (Note: we needn't check this when not if_not_exists,
143  * because DefineRelation will complain anyway.)
144  */
145  if (seq->if_not_exists)
146  {
147  RangeVarGetAndCheckCreationNamespace(seq->sequence, NoLock, &seqoid);
148  if (OidIsValid(seqoid))
149  {
150  ereport(NOTICE,
151  (errcode(ERRCODE_DUPLICATE_TABLE),
152  errmsg("relation \"%s\" already exists, skipping",
153  seq->sequence->relname)));
154  return InvalidObjectAddress;
155  }
156  }
157 
158  /* Check and set all option values */
159  init_params(pstate, seq->options, seq->for_identity, true,
160  &seqform, &seqdataform,
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 = makeNode(ColumnDef);
170 
171  coldef->inhcount = 0;
172  coldef->is_local = true;
173  coldef->is_not_null = true;
174  coldef->is_from_type = false;
175  coldef->is_from_parent = false;
176  coldef->storage = 0;
177  coldef->raw_default = NULL;
178  coldef->cooked_default = NULL;
179  coldef->collClause = NULL;
180  coldef->collOid = InvalidOid;
181  coldef->constraints = NIL;
182  coldef->location = -1;
183 
184  null[i - 1] = false;
185 
186  switch (i)
187  {
188  case SEQ_COL_LASTVAL:
189  coldef->typeName = makeTypeNameFromOid(INT8OID, -1);
190  coldef->colname = "last_value";
191  value[i - 1] = Int64GetDatumFast(seqdataform.last_value);
192  break;
193  case SEQ_COL_LOG:
194  coldef->typeName = makeTypeNameFromOid(INT8OID, -1);
195  coldef->colname = "log_cnt";
196  value[i - 1] = Int64GetDatum((int64) 0);
197  break;
198  case SEQ_COL_CALLED:
199  coldef->typeName = makeTypeNameFromOid(BOOLOID, -1);
200  coldef->colname = "is_called";
201  value[i - 1] = BoolGetDatum(false);
202  break;
203  }
204  stmt->tableElts = lappend(stmt->tableElts, coldef);
205  }
206 
207  stmt->relation = seq->sequence;
208  stmt->inhRelations = NIL;
209  stmt->constraints = NIL;
210  stmt->options = NIL;
211  stmt->oncommit = ONCOMMIT_NOOP;
212  stmt->tablespacename = NULL;
213  stmt->if_not_exists = seq->if_not_exists;
214 
215  address = DefineRelation(stmt, RELKIND_SEQUENCE, seq->ownerId, NULL, NULL);
216  seqoid = address.objectId;
217  Assert(seqoid != InvalidOid);
218 
219  rel = heap_open(seqoid, AccessExclusiveLock);
220  tupDesc = RelationGetDescr(rel);
221 
222  /* now initialize the sequence's data */
223  tuple = heap_form_tuple(tupDesc, value, null);
224  fill_seq_with_data(rel, tuple);
225 
226  /* process OWNED BY if given */
227  if (owned_by)
228  process_owned_by(rel, owned_by, seq->for_identity);
229 
230  heap_close(rel, NoLock);
231 
232  /* fill in pg_sequence */
234  tupDesc = RelationGetDescr(rel);
235 
236  memset(pgs_nulls, 0, sizeof(pgs_nulls));
237 
238  pgs_values[Anum_pg_sequence_seqrelid - 1] = ObjectIdGetDatum(seqoid);
239  pgs_values[Anum_pg_sequence_seqtypid - 1] = ObjectIdGetDatum(seqform.seqtypid);
240  pgs_values[Anum_pg_sequence_seqstart - 1] = Int64GetDatumFast(seqform.seqstart);
241  pgs_values[Anum_pg_sequence_seqincrement - 1] = Int64GetDatumFast(seqform.seqincrement);
242  pgs_values[Anum_pg_sequence_seqmax - 1] = Int64GetDatumFast(seqform.seqmax);
243  pgs_values[Anum_pg_sequence_seqmin - 1] = Int64GetDatumFast(seqform.seqmin);
244  pgs_values[Anum_pg_sequence_seqcache - 1] = Int64GetDatumFast(seqform.seqcache);
245  pgs_values[Anum_pg_sequence_seqcycle - 1] = BoolGetDatum(seqform.seqcycle);
246 
247  tuple = heap_form_tuple(tupDesc, pgs_values, pgs_nulls);
248  CatalogTupleInsert(rel, tuple);
249 
250  heap_freetuple(tuple);
252 
253  return address;
254 }
RangeVar * relation
Definition: parsenodes.h:1987
#define NIL
Definition: pg_list.h:69
OnCommitAction oncommit
Definition: parsenodes.h:1996
List * inhRelations
Definition: parsenodes.h:1989
char storage
Definition: parsenodes.h:646
#define RELPERSISTENCE_UNLOGGED
Definition: pg_class.h:171
bool is_local
Definition: parsenodes.h:642
#define RelationGetDescr(relation)
Definition: rel.h:428
List * constraints
Definition: parsenodes.h:652
bool is_from_parent
Definition: parsenodes.h:645
#define Anum_pg_sequence_seqcache
Definition: pg_sequence.h:29
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SequenceRelationId
Definition: pg_sequence.h:6
#define SEQ_COL_FIRSTCOL
Definition: sequence.h:42
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
bool is_not_null
Definition: parsenodes.h:643
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1372
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define Anum_pg_sequence_seqtypid
Definition: pg_sequence.h:24
List * constraints
Definition: parsenodes.h:1994
bool if_not_exists
Definition: parsenodes.h:1998
Node * cooked_default
Definition: parsenodes.h:648
#define SEQ_COL_LASTVAL
Definition: sequence.h:38
FormData_pg_sequence
Definition: pg_sequence.h:18
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
#define Natts_pg_sequence
Definition: pg_sequence.h:22
static struct @121 value
#define NoLock
Definition: lockdefs.h:34
Oid collOid
Definition: parsenodes.h:651
#define RowExclusiveLock
Definition: lockdefs.h:38
List * options
Definition: parsenodes.h:1995
static void process_owned_by(Relation seqrel, List *owned_by, bool for_identity)
Definition: sequence.c:1639
int location
Definition: parsenodes.h:654
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ObjectAddress *typaddress, const char *queryString)
Definition: tablecmds.c:498
#define ereport(elevel, rest)
Definition: elog.h:122
#define SEQ_COL_LOG
Definition: sequence.h:39
Node * raw_default
Definition: parsenodes.h:647
List * lappend(List *list, void *datum)
Definition: list.c:128
char * tablespacename
Definition: parsenodes.h:1997
Oid RangeVarGetAndCheckCreationNamespace(RangeVar *relation, LOCKMODE lockmode, Oid *existing_relation_id)
Definition: namespace.c:508
uintptr_t Datum
Definition: postgres.h:372
#define Anum_pg_sequence_seqmin
Definition: pg_sequence.h:28
#define Anum_pg_sequence_seqcycle
Definition: pg_sequence.h:30
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define Int64GetDatumFast(X)
Definition: postgres.h:781
#define BoolGetDatum(X)
Definition: postgres.h:408
TypeName * makeTypeNameFromOid(Oid typeOid, int32 typmod)
Definition: makefuncs.c:469
#define InvalidOid
Definition: postgres_ext.h:36
bool is_from_type
Definition: parsenodes.h:644
#define NOTICE
Definition: elog.h:37
#define INT8OID
Definition: pg_type.h:304
List * tableElts
Definition: parsenodes.h:1988
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
TypeName * typeName
Definition: parsenodes.h:640
CollateClause * collClause
Definition: parsenodes.h:650
#define Anum_pg_sequence_seqstart
Definition: pg_sequence.h:25
#define BOOLOID
Definition: pg_type.h:288
#define SEQ_COL_LASTCOL
Definition: sequence.h:43
#define AccessExclusiveLock
Definition: lockdefs.h:45
static void init_params(ParseState *pstate, List *options, bool for_identity, bool isInit, Form_pg_sequence seqform, Form_pg_sequence_data seqdataform, bool *need_seq_rewrite, List **owned_by)
Definition: sequence.c:1234
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define SEQ_COL_CALLED
Definition: sequence.h:40
#define Anum_pg_sequence_seqrelid
Definition: pg_sequence.h:23
int i
int inhcount
Definition: parsenodes.h:641
char * colname
Definition: parsenodes.h:639
#define Anum_pg_sequence_seqincrement
Definition: pg_sequence.h:26
#define Anum_pg_sequence_seqmax
Definition: pg_sequence.h:27
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
Definition: pg_list.h:45
static void fill_seq_with_data(Relation rel, HeapTuple tuple)
Definition: sequence.c:337
void DeleteSequenceTuple ( Oid  relid)

Definition at line 516 of file sequence.c.

References CatalogTupleDelete(), elog, ERROR, heap_close, heap_open(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1, SEQRELID, SequenceRelationId, and HeapTupleData::t_self.

Referenced by doDeletion().

517 {
518  Relation rel;
519  HeapTuple tuple;
520 
522 
523  tuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(relid));
524  if (!HeapTupleIsValid(tuple))
525  elog(ERROR, "cache lookup failed for sequence %u", relid);
526 
527  CatalogTupleDelete(rel, &tuple->t_self);
528 
529  ReleaseSysCache(tuple);
531 }
#define SequenceRelationId
Definition: pg_sequence.h:6
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define elog
Definition: elog.h:219
Datum nextval ( PG_FUNCTION_ARGS  )

Definition at line 539 of file sequence.c.

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

Referenced by autoinc(), and ttdummy().

540 {
541  text *seqin = PG_GETARG_TEXT_PP(0);
542  RangeVar *sequence;
543  Oid relid;
544 
546 
547  /*
548  * XXX: This is not safe in the presence of concurrent DDL, but acquiring
549  * a lock here is more expensive than letting nextval_internal do it,
550  * since the latter maintains a cache that keeps us from hitting the lock
551  * manager more than once per transaction. It's not clear whether the
552  * performance penalty is material in practice, but for now, we do it this
553  * way.
554  */
555  relid = RangeVarGetRelid(sequence, NoLock, false);
556 
557  PG_RETURN_INT64(nextval_internal(relid, true));
558 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
int64 nextval_internal(Oid relid, bool check_permissions)
Definition: sequence.c:569
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2980
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NoLock
Definition: lockdefs.h:34
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3190
Definition: c.h:439
int64 nextval_internal ( Oid  relid,
bool  check_permissions 
)

Definition at line 569 of file sequence.c.

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(), INT64_FORMAT, FormData_pg_sequence_data::is_called, SeqTableData::last, SeqTableData::last_valid, FormData_pg_sequence_data::last_value, FormData_pg_sequence_data::log_cnt, MarkBufferDirty(), next, xl_seq_rec::node, NoLock, ObjectIdGetDatum, PageGetLSN, PageSetLSN, pg_class_aclcheck(), PreventCommandIfParallelMode(), PreventCommandIfReadOnly(), RelationData::rd_islocaltemp, RelationData::rd_node, read_seq_tuple(), REGBUF_WILL_INIT, relation_close(), RelationGetRelationName, RelationNeedsWAL, ReleaseSysCache(), SeqTableData::relid, result, SearchSysCache1, SEQ_LOG_VALS, SEQRELID, snprintf(), START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleData::t_len, UnlockReleaseBuffer(), XLOG_SEQ_LOG, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

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

570 {
571  SeqTable elm;
572  Relation seqrel;
573  Buffer buf;
574  Page page;
575  HeapTuple pgstuple;
576  Form_pg_sequence pgsform;
577  HeapTupleData seqdatatuple;
579  int64 incby,
580  maxv,
581  minv,
582  cache,
583  log,
584  fetch,
585  last;
586  int64 result,
587  next,
588  rescnt = 0;
589  bool cycle;
590  bool logit = false;
591 
592  /* open and lock sequence */
593  init_sequence(relid, &elm, &seqrel);
594 
595  if (check_permissions &&
598  ereport(ERROR,
599  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
600  errmsg("permission denied for sequence %s",
601  RelationGetRelationName(seqrel))));
602 
603  /* read-only transactions may only modify temp sequences */
604  if (!seqrel->rd_islocaltemp)
605  PreventCommandIfReadOnly("nextval()");
606 
607  /*
608  * Forbid this during parallel operation because, to make it work, the
609  * cooperating backends would need to share the backend-local cached
610  * sequence information. Currently, we don't support that.
611  */
612  PreventCommandIfParallelMode("nextval()");
613 
614  if (elm->last != elm->cached) /* some numbers were cached */
615  {
616  Assert(elm->last_valid);
617  Assert(elm->increment != 0);
618  elm->last += elm->increment;
619  relation_close(seqrel, NoLock);
620  last_used_seq = elm;
621  return elm->last;
622  }
623 
624  pgstuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(relid));
625  if (!HeapTupleIsValid(pgstuple))
626  elog(ERROR, "cache lookup failed for sequence %u", relid);
627  pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
628  incby = pgsform->seqincrement;
629  maxv = pgsform->seqmax;
630  minv = pgsform->seqmin;
631  cache = pgsform->seqcache;
632  cycle = pgsform->seqcycle;
633  ReleaseSysCache(pgstuple);
634 
635  /* lock page' buffer and read tuple */
636  seq = read_seq_tuple(seqrel, &buf, &seqdatatuple);
637  page = BufferGetPage(buf);
638 
639  elm->increment = incby;
640  last = next = result = seq->last_value;
641  fetch = cache;
642  log = seq->log_cnt;
643 
644  if (!seq->is_called)
645  {
646  rescnt++; /* return last_value if not is_called */
647  fetch--;
648  }
649 
650  /*
651  * Decide whether we should emit a WAL log record. If so, force up the
652  * fetch count to grab SEQ_LOG_VALS more values than we actually need to
653  * cache. (These will then be usable without logging.)
654  *
655  * If this is the first nextval after a checkpoint, we must force a new
656  * WAL record to be written anyway, else replay starting from the
657  * checkpoint would fail to advance the sequence past the logged values.
658  * In this case we may as well fetch extra values.
659  */
660  if (log < fetch || !seq->is_called)
661  {
662  /* forced log to satisfy local demand for values */
663  fetch = log = fetch + SEQ_LOG_VALS;
664  logit = true;
665  }
666  else
667  {
668  XLogRecPtr redoptr = GetRedoRecPtr();
669 
670  if (PageGetLSN(page) <= redoptr)
671  {
672  /* last update of seq was before checkpoint */
673  fetch = log = fetch + SEQ_LOG_VALS;
674  logit = true;
675  }
676  }
677 
678  while (fetch) /* try to fetch cache [+ log ] numbers */
679  {
680  /*
681  * Check MAXVALUE for ascending sequences and MINVALUE for descending
682  * sequences
683  */
684  if (incby > 0)
685  {
686  /* ascending sequence */
687  if ((maxv >= 0 && next > maxv - incby) ||
688  (maxv < 0 && next + incby > maxv))
689  {
690  if (rescnt > 0)
691  break; /* stop fetching */
692  if (!cycle)
693  {
694  char buf[100];
695 
696  snprintf(buf, sizeof(buf), INT64_FORMAT, maxv);
697  ereport(ERROR,
698  (errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
699  errmsg("nextval: reached maximum value of sequence \"%s\" (%s)",
700  RelationGetRelationName(seqrel), buf)));
701  }
702  next = minv;
703  }
704  else
705  next += incby;
706  }
707  else
708  {
709  /* descending sequence */
710  if ((minv < 0 && next < minv - incby) ||
711  (minv >= 0 && next + incby < minv))
712  {
713  if (rescnt > 0)
714  break; /* stop fetching */
715  if (!cycle)
716  {
717  char buf[100];
718 
719  snprintf(buf, sizeof(buf), INT64_FORMAT, minv);
720  ereport(ERROR,
721  (errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
722  errmsg("nextval: reached minimum value of sequence \"%s\" (%s)",
723  RelationGetRelationName(seqrel), buf)));
724  }
725  next = maxv;
726  }
727  else
728  next += incby;
729  }
730  fetch--;
731  if (rescnt < cache)
732  {
733  log--;
734  rescnt++;
735  last = next;
736  if (rescnt == 1) /* if it's first result - */
737  result = next; /* it's what to return */
738  }
739  }
740 
741  log -= fetch; /* adjust for any unfetched numbers */
742  Assert(log >= 0);
743 
744  /* save info in local cache */
745  elm->last = result; /* last returned number */
746  elm->cached = last; /* last fetched number */
747  elm->last_valid = true;
748 
749  last_used_seq = elm;
750 
751  /*
752  * If something needs to be WAL logged, acquire an xid, so this
753  * transaction's commit will trigger a WAL flush and wait for syncrep.
754  * It's sufficient to ensure the toplevel transaction has an xid, no need
755  * to assign xids subxacts, that'll already trigger an appropriate wait.
756  * (Have to do that here, so we're outside the critical section)
757  */
758  if (logit && RelationNeedsWAL(seqrel))
760 
761  /* ready to change the on-disk (or really, in-buffer) tuple */
763 
764  /*
765  * We must mark the buffer dirty before doing XLogInsert(); see notes in
766  * SyncOneBuffer(). However, we don't apply the desired changes just yet.
767  * This looks like a violation of the buffer update protocol, but it is in
768  * fact safe because we hold exclusive lock on the buffer. Any other
769  * process, including a checkpoint, that tries to examine the buffer
770  * contents will block until we release the lock, and then will see the
771  * final state that we install below.
772  */
773  MarkBufferDirty(buf);
774 
775  /* XLOG stuff */
776  if (logit && RelationNeedsWAL(seqrel))
777  {
778  xl_seq_rec xlrec;
779  XLogRecPtr recptr;
780 
781  /*
782  * We don't log the current state of the tuple, but rather the state
783  * as it would appear after "log" more fetches. This lets us skip
784  * that many future WAL records, at the cost that we lose those
785  * sequence values if we crash.
786  */
787  XLogBeginInsert();
789 
790  /* set values that will be saved in xlog */
791  seq->last_value = next;
792  seq->is_called = true;
793  seq->log_cnt = 0;
794 
795  xlrec.node = seqrel->rd_node;
796 
797  XLogRegisterData((char *) &xlrec, sizeof(xl_seq_rec));
798  XLogRegisterData((char *) seqdatatuple.t_data, seqdatatuple.t_len);
799 
800  recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG);
801 
802  PageSetLSN(page, recptr);
803  }
804 
805  /* Now update sequence tuple to the intended final state */
806  seq->last_value = last; /* last fetched number */
807  seq->is_called = true;
808  seq->log_cnt = log; /* how much is logged */
809 
811 
812  UnlockReleaseBuffer(buf);
813 
814  relation_close(seqrel, NoLock);
815 
816  return result;
817 }
int64 cached
Definition: sequence.c:79
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static int32 next
Definition: blutils.c:210
void PreventCommandIfParallelMode(const char *cmdname)
Definition: utility.c:254
Oid GetUserId(void)
Definition: miscinit.c:283
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
bool last_valid
Definition: sequence.c:77
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
static void check_permissions(void)
Definition: logicalfuncs.c:109
bool rd_islocaltemp
Definition: rel.h:90
#define END_CRIT_SECTION()
Definition: miscadmin.h:135
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:133
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
return result
Definition: formatting.c:1632
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
FormData_pg_sequence * Form_pg_sequence
Definition: pg_sequence.h:20
TransactionId GetTopTransactionId(void)
Definition: xact.c:389
RelFileNode node
Definition: sequence.h:50
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
HeapTupleHeader t_data
Definition: htup.h:67
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
uint32 t_len
Definition: htup.h:64
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:66
#define RelationGetRelationName(relation)
Definition: rel.h:436
int64 last
Definition: sequence.c:78
#define ACL_USAGE
Definition: parsenodes.h:80
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
static Form_pg_sequence_data read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
Definition: sequence.c:1168
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define ACL_UPDATE
Definition: parsenodes.h:74
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
RelFileNode rd_node
Definition: rel.h:85
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define SEQ_LOG_VALS
Definition: sequence.c:54
int64 increment
Definition: sequence.c:81
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8162
#define RelationNeedsWAL(relation)
Definition: rel.h:505
#define INT64_FORMAT
Definition: c.h:315
void PreventCommandIfReadOnly(const char *cmdname)
Definition: utility.c:236
static SeqTableData * last_used_seq
Definition: sequence.c:93
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4422
#define PageGetLSN(page)
Definition: bufpage.h:362
static void init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel)
Definition: sequence.c:1101
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
#define XLOG_SEQ_LOG
Definition: sequence.h:46
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
void ResetSequence ( Oid  seq_relid)

Definition at line 269 of file sequence.c.

References 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(), relation_close(), RelationSetNewRelfilenode(), ReleaseSysCache(), SearchSysCache1, SEQRELID, and UnlockReleaseBuffer().

Referenced by ExecuteTruncate().

270 {
271  Relation seq_rel;
272  SeqTable elm;
274  Buffer buf;
275  HeapTupleData seqdatatuple;
276  HeapTuple tuple;
277  HeapTuple pgstuple;
278  Form_pg_sequence pgsform;
279  int64 startv;
280 
281  /*
282  * Read the old sequence. This does a bit more work than really
283  * necessary, but it's simple, and we do want to double-check that it's
284  * indeed a sequence.
285  */
286  init_sequence(seq_relid, &elm, &seq_rel);
287  (void) read_seq_tuple(seq_rel, &buf, &seqdatatuple);
288 
289  pgstuple = SearchSysCache1(SEQRELID, ObjectIdGetDatum(seq_relid));
290  if (!HeapTupleIsValid(pgstuple))
291  elog(ERROR, "cache lookup failed for sequence %u", seq_relid);
292  pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
293  startv = pgsform->seqstart;
294  ReleaseSysCache(pgstuple);
295 
296  /*
297  * Copy the existing sequence tuple.
298  */
299  tuple = heap_copytuple(&seqdatatuple);
300 
301  /* Now we're done with the old page */
302  UnlockReleaseBuffer(buf);
303 
304  /*
305  * Modify the copied tuple to execute the restart (compare the RESTART
306  * action in AlterSequence)
307  */
308  seq = (Form_pg_sequence_data) GETSTRUCT(tuple);
309  seq->last_value = startv;
310  seq->is_called = false;
311  seq->log_cnt = 0;
312 
313  /*
314  * Create a new storage file for the sequence. We want to keep the
315  * sequence's relfrozenxid at 0, since it won't contain any unfrozen XIDs.
316  * Same with relminmxid, since a sequence will never contain multixacts.
317  */
318  RelationSetNewRelfilenode(seq_rel, seq_rel->rd_rel->relpersistence,
320 
321  /*
322  * Insert the modified tuple into the new storage file.
323  */
324  fill_seq_with_data(seq_rel, tuple);
325 
326  /* Clear local cache so that we don't think we have cached numbers */
327  /* Note that we do not change the currval() state */
328  elm->cached = elm->last;
329 
330  relation_close(seq_rel, NoLock);
331 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:608
int64 cached
Definition: sequence.c:79
void RelationSetNewRelfilenode(Relation relation, char persistence, TransactionId freezeXid, MultiXactId minmulti)
Definition: relcache.c:3416
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
FormData_pg_sequence * Form_pg_sequence
Definition: pg_sequence.h:20
Form_pg_class rd_rel
Definition: rel.h:114
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:66
#define InvalidTransactionId
Definition: transam.h:31
int64 last
Definition: sequence.c:78
static Form_pg_sequence_data read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
Definition: sequence.c:1168
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define InvalidMultiXactId
Definition: multixact.h:23
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
static void init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel)
Definition: sequence.c:1101
#define elog
Definition: elog.h:219
int Buffer
Definition: buf.h:23
static void fill_seq_with_data(Relation rel, HeapTuple tuple)
Definition: sequence.c:337
FormData_pg_sequence_data * Form_pg_sequence_data
Definition: sequence.h:32
void ResetSequenceCaches ( void  )

Definition at line 1927 of file sequence.c.

References hash_destroy(), and NULL.

Referenced by DiscardAll(), and DiscardCommand().

1928 {
1929  if (seqhashtab)
1930  {
1932  seqhashtab = NULL;
1933  }
1934 
1935  last_used_seq = NULL;
1936 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:793
static HTAB * seqhashtab
Definition: sequence.c:87
#define NULL
Definition: c.h:229
static SeqTableData * last_used_seq
Definition: sequence.c:93
void seq_desc ( StringInfo  buf,
XLogReaderState rptr 
)

Definition at line 21 of file seqdesc.c.

References appendStringInfo(), RelFileNode::dbNode, xl_seq_rec::node, RelFileNode::relNode, RelFileNode::spcNode, XLOG_SEQ_LOG, XLogRecGetData, XLogRecGetInfo, and XLR_INFO_MASK.

22 {
23  char *rec = XLogRecGetData(record);
24  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
25  xl_seq_rec *xlrec = (xl_seq_rec *) rec;
26 
27  if (info == XLOG_SEQ_LOG)
28  appendStringInfo(buf, "rel %u/%u/%u",
29  xlrec->node.spcNode, xlrec->node.dbNode,
30  xlrec->node.relNode);
31 }
unsigned char uint8
Definition: c.h:266
RelFileNode node
Definition: sequence.h:50
#define XLogRecGetData(decoder)
Definition: xlogreader.h:220
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:216
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_SEQ_LOG
Definition: sequence.h:46
const char* seq_identify ( uint8  info)

Definition at line 34 of file seqdesc.c.

References NULL, XLOG_SEQ_LOG, and XLR_INFO_MASK.

35 {
36  const char *id = NULL;
37 
38  switch (info & ~XLR_INFO_MASK)
39  {
40  case XLOG_SEQ_LOG:
41  id = "LOG";
42  break;
43  }
44 
45  return id;
46 }
#define NULL
Definition: c.h:229
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_SEQ_LOG
Definition: sequence.h:46
void seq_mask ( char *  pagedata,
BlockNumber  blkno 
)

Definition at line 1942 of file sequence.c.

References mask_page_lsn(), and mask_unused_space().

1943 {
1944  mask_page_lsn(page);
1945 
1946  mask_unused_space(page);
1947 }
void mask_unused_space(Page page)
Definition: bufmask.c:69
void mask_page_lsn(Page page)
Definition: bufmask.c:30
void seq_redo ( XLogReaderState rptr)

Definition at line 1874 of file sequence.c.

References buffer, BufferGetPage, BufferGetPageSize, elog, XLogReaderState::EndRecPtr, FirstOffsetNumber, InvalidOffsetNumber, sequence_magic::magic, MarkBufferDirty(), PageAddItem, PageGetSpecialPointer, PageInit(), PageSetLSN, palloc(), PANIC, pfree(), SEQ_MAGIC, UnlockReleaseBuffer(), XLOG_SEQ_LOG, XLogInitBufferForRedo(), XLogRecGetData, XLogRecGetDataLen, XLogRecGetInfo, and XLR_INFO_MASK.

1875 {
1876  XLogRecPtr lsn = record->EndRecPtr;
1877  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1878  Buffer buffer;
1879  Page page;
1880  Page localpage;
1881  char *item;
1882  Size itemsz;
1883  xl_seq_rec *xlrec = (xl_seq_rec *) XLogRecGetData(record);
1884  sequence_magic *sm;
1885 
1886  if (info != XLOG_SEQ_LOG)
1887  elog(PANIC, "seq_redo: unknown op code %u", info);
1888 
1889  buffer = XLogInitBufferForRedo(record, 0);
1890  page = (Page) BufferGetPage(buffer);
1891 
1892  /*
1893  * We always reinit the page. However, since this WAL record type is also
1894  * used for updating sequences, it's possible that a hot-standby backend
1895  * is examining the page concurrently; so we mustn't transiently trash the
1896  * buffer. The solution is to build the correct new page contents in
1897  * local workspace and then memcpy into the buffer. Then only bytes that
1898  * are supposed to change will change, even transiently. We must palloc
1899  * the local page for alignment reasons.
1900  */
1901  localpage = (Page) palloc(BufferGetPageSize(buffer));
1902 
1903  PageInit(localpage, BufferGetPageSize(buffer), sizeof(sequence_magic));
1904  sm = (sequence_magic *) PageGetSpecialPointer(localpage);
1905  sm->magic = SEQ_MAGIC;
1906 
1907  item = (char *) xlrec + sizeof(xl_seq_rec);
1908  itemsz = XLogRecGetDataLen(record) - sizeof(xl_seq_rec);
1909 
1910  if (PageAddItem(localpage, (Item) item, itemsz,
1911  FirstOffsetNumber, false, false) == InvalidOffsetNumber)
1912  elog(PANIC, "seq_redo: failed to add item to page");
1913 
1914  PageSetLSN(localpage, lsn);
1915 
1916  memcpy(page, localpage, BufferGetPageSize(buffer));
1917  MarkBufferDirty(buffer);
1918  UnlockReleaseBuffer(buffer);
1919 
1920  pfree(localpage);
1921 }
#define SEQ_MAGIC
Definition: sequence.c:59
struct xl_seq_rec xl_seq_rec
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
unsigned char uint8
Definition: c.h:266
Pointer Item
Definition: item.h:17
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:412
#define PANIC
Definition: elog.h:53
void pfree(void *pointer)
Definition: mcxt.c:950
#define XLogRecGetData(decoder)
Definition: xlogreader.h:220
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define XLogRecGetDataLen(decoder)
Definition: xlogreader.h:221
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:302
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:216
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:147
#define InvalidOffsetNumber
Definition: off.h:26
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
uint32 magic
Definition: sequence.c:63
size_t Size
Definition: c.h:356
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
void * palloc(Size size)
Definition: mcxt.c:849
#define elog
Definition: elog.h:219
#define XLOG_SEQ_LOG
Definition: sequence.h:46
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41
List* sequence_options ( Oid  relid)

Definition at line 1752 of file sequence.c.

References elog, ERROR, GETSTRUCT, HeapTupleIsValid, lappend(), makeDefElem(), makeInteger(), NIL, options, ReleaseSysCache(), SearchSysCache1, and SEQRELID.

Referenced by transformTableLikeClause().

1753 {
1754  HeapTuple pgstuple;
1755  Form_pg_sequence pgsform;
1756  List *options = NIL;
1757 
1758  pgstuple = SearchSysCache1(SEQRELID, relid);
1759  if (!HeapTupleIsValid(pgstuple))
1760  elog(ERROR, "cache lookup failed for sequence %u", relid);
1761  pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple);
1762 
1763  options = lappend(options, makeDefElem("cache", (Node *) makeInteger(pgsform->seqcache), -1));
1764  options = lappend(options, makeDefElem("cycle", (Node *) makeInteger(pgsform->seqcycle), -1));
1765  options = lappend(options, makeDefElem("increment", (Node *) makeInteger(pgsform->seqincrement), -1));
1766  options = lappend(options, makeDefElem("maxvalue", (Node *) makeInteger(pgsform->seqmax), -1));
1767  options = lappend(options, makeDefElem("minvalue", (Node *) makeInteger(pgsform->seqmin), -1));
1768  options = lappend(options, makeDefElem("start", (Node *) makeInteger(pgsform->seqstart), -1));
1769 
1770  ReleaseSysCache(pgstuple);
1771 
1772  return options;
1773 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Definition: nodes.h:509
FormData_pg_sequence * Form_pg_sequence
Definition: pg_sequence.h:20
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition: makefuncs.c:544
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
Value * makeInteger(long i)
Definition: value.c:23
#define ERROR
Definition: elog.h:43
List * lappend(List *list, void *datum)
Definition: list.c:128
static char ** options
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define elog
Definition: elog.h:219
Definition: pg_list.h:45