1997-04-02 05:51:23 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* sequence.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* PostgreSQL sequences support code.
|
1997-04-02 05:51:23 +02:00
|
|
|
*
|
2011-01-01 19:18:15 +01:00
|
|
|
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
|
2000-12-08 21:10:19 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/commands/sequence.c
|
2000-12-08 21:10:19 +01:00
|
|
|
*
|
1997-04-02 05:51:23 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-12-08 21:10:19 +01:00
|
|
|
#include "postgres.h"
|
1997-04-02 05:51:23 +02:00
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "access/heapam.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/transam.h"
|
|
|
|
#include "access/xact.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "access/xlogutils.h"
|
2006-08-21 02:57:26 +02:00
|
|
|
#include "catalog/dependency.h"
|
2002-03-30 02:02:42 +01:00
|
|
|
#include "catalog/namespace.h"
|
2002-03-29 20:06:29 +01:00
|
|
|
#include "catalog/pg_type.h"
|
2002-05-22 23:40:55 +02:00
|
|
|
#include "commands/defrem.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "commands/sequence.h"
|
2006-07-11 19:26:59 +02:00
|
|
|
#include "commands/tablecmds.h"
|
2011-01-02 14:08:08 +01:00
|
|
|
#include "funcapi.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "miscadmin.h"
|
2006-03-14 23:48:25 +01:00
|
|
|
#include "nodes/makefuncs.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "storage/bufmgr.h"
|
|
|
|
#include "storage/lmgr.h"
|
2007-09-05 20:10:48 +02:00
|
|
|
#include "storage/proc.h"
|
2010-02-09 22:43:30 +01:00
|
|
|
#include "storage/smgr.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "utils/acl.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/builtins.h"
|
2006-08-21 02:57:26 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2004-09-16 18:58:44 +02:00
|
|
|
#include "utils/resowner.h"
|
2005-06-07 09:08:35 +02:00
|
|
|
#include "utils/syscache.h"
|
2004-09-16 18:58:44 +02:00
|
|
|
|
2001-06-07 00:03:48 +02:00
|
|
|
|
2000-11-30 02:47:33 +01:00
|
|
|
/*
|
2002-01-11 19:16:04 +01:00
|
|
|
* We don't want to log each fetching of a value from a sequence,
|
2000-11-30 02:47:33 +01:00
|
|
|
* so we pre-log a few fetches in advance. In the event of
|
|
|
|
* crash we can lose as much as we pre-logged.
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
#define SEQ_LOG_VALS 32
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
/*
|
|
|
|
* The "special area" of a sequence's buffer page looks like this.
|
|
|
|
*/
|
|
|
|
#define SEQ_MAGIC 0x1717
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct sequence_magic
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
uint32 magic;
|
1997-09-08 23:56:23 +02:00
|
|
|
} sequence_magic;
|
1997-04-02 05:51:23 +02:00
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
/*
|
|
|
|
* We store a SeqTable item for every sequence we have touched in the current
|
|
|
|
* session. This is needed to hold onto nextval/currval state. (We can't
|
|
|
|
* rely on the relcache, since it's only, well, a cache, and may decide to
|
|
|
|
* discard entries.)
|
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* XXX We use linear search to find pre-existing SeqTable entries. This is
|
2002-05-22 23:40:55 +02:00
|
|
|
* good when only a small number of sequences are touched in a session, but
|
|
|
|
* would suck with many different sequences. Perhaps use a hashtable someday.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct SeqTableData
|
|
|
|
{
|
2002-05-22 23:40:55 +02:00
|
|
|
struct SeqTableData *next; /* link to next SeqTable object */
|
|
|
|
Oid relid; /* pg_class OID of this sequence */
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
Oid filenode; /* last seen relfilenode of this sequence */
|
2007-09-05 20:10:48 +02:00
|
|
|
LocalTransactionId lxid; /* xact in which we last did a seq op */
|
2007-10-25 20:54:03 +02:00
|
|
|
bool last_valid; /* do we have a valid "last" value? */
|
2002-05-22 23:40:55 +02:00
|
|
|
int64 last; /* value last returned by nextval */
|
|
|
|
int64 cached; /* last value already cached for nextval */
|
|
|
|
/* if last != cached, we have not used up all the cached values */
|
|
|
|
int64 increment; /* copy of sequence's increment field */
|
2007-10-25 20:54:03 +02:00
|
|
|
/* note that increment is zero until we first do read_info() */
|
1997-09-08 23:56:23 +02:00
|
|
|
} SeqTableData;
|
1997-04-02 05:51:23 +02:00
|
|
|
|
|
|
|
typedef SeqTableData *SeqTable;
|
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
static SeqTable seqtab = NULL; /* Head of list of SeqTable items */
|
1997-04-02 05:51:23 +02:00
|
|
|
|
2005-06-07 09:08:35 +02:00
|
|
|
/*
|
|
|
|
* last_used_seq is updated by nextval() to point to the last used
|
|
|
|
* sequence.
|
|
|
|
*/
|
|
|
|
static SeqTableData *last_used_seq = NULL;
|
2002-05-22 23:40:55 +02:00
|
|
|
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
static void fill_seq_with_data(Relation rel, HeapTuple tuple);
|
2005-10-03 01:50:16 +02:00
|
|
|
static int64 nextval_internal(Oid relid);
|
2006-07-31 22:09:10 +02:00
|
|
|
static Relation open_share_lock(SeqTable seq);
|
2005-10-03 01:50:16 +02:00
|
|
|
static void init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel);
|
2003-07-20 23:56:35 +02:00
|
|
|
static Form_pg_sequence read_info(SeqTable elm, Relation rel, Buffer *buf);
|
2006-08-21 02:57:26 +02:00
|
|
|
static void init_params(List *options, bool isInit,
|
2008-05-17 03:20:39 +02:00
|
|
|
Form_pg_sequence new, List **owned_by);
|
2005-10-03 01:50:16 +02:00
|
|
|
static void do_setval(Oid relid, int64 next, bool iscalled);
|
2006-08-21 02:57:26 +02:00
|
|
|
static void process_owned_by(Relation seqrel, List *owned_by);
|
|
|
|
|
1997-04-02 05:51:23 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* DefineSequence
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates a new sequence relation
|
1997-04-02 05:51:23 +02:00
|
|
|
*/
|
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
DefineSequence(CreateSeqStmt *seq)
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
FormData_pg_sequence new;
|
2006-08-21 02:57:26 +02:00
|
|
|
List *owned_by;
|
1997-09-08 04:41:22 +02:00
|
|
|
CreateStmt *stmt = makeNode(CreateStmt);
|
2002-03-22 03:56:37 +01:00
|
|
|
Oid seqoid;
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation rel;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupDesc;
|
|
|
|
Datum value[SEQ_COL_LASTCOL];
|
2008-11-02 02:45:28 +01:00
|
|
|
bool null[SEQ_COL_LASTCOL];
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1998-03-30 19:15:26 +02:00
|
|
|
NameData name;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2011-02-22 20:42:45 +01:00
|
|
|
/* Unlogged sequences are not implemented -- not clear if useful. */
|
|
|
|
if (seq->sequence->relpersistence == RELPERSISTENCE_UNLOGGED)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("unlogged sequences are not supported")));
|
|
|
|
|
2003-11-24 17:54:07 +01:00
|
|
|
/* Check and set all option values */
|
2008-05-17 03:20:39 +02:00
|
|
|
init_params(seq->options, true, &new, &owned_by);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2008-05-17 01:36:05 +02:00
|
|
|
* Create relation (and fill value[] and null[] for the tuple)
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
stmt->tableElts = NIL;
|
|
|
|
for (i = SEQ_COL_FIRSTCOL; i <= SEQ_COL_LASTCOL; i++)
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
2006-03-14 23:48:25 +01:00
|
|
|
ColumnDef *coldef = makeNode(ColumnDef);
|
2002-03-29 20:06:29 +01:00
|
|
|
|
2002-09-22 21:42:52 +02:00
|
|
|
coldef->inhcount = 0;
|
|
|
|
coldef->is_local = true;
|
2002-07-17 00:12:20 +02:00
|
|
|
coldef->is_not_null = true;
|
2009-10-13 02:53:08 +02:00
|
|
|
coldef->storage = 0;
|
1999-10-04 01:55:40 +02:00
|
|
|
coldef->raw_default = NULL;
|
|
|
|
coldef->cooked_default = NULL;
|
2002-07-17 00:12:20 +02:00
|
|
|
coldef->constraints = NIL;
|
|
|
|
|
2008-11-02 02:45:28 +01:00
|
|
|
null[i - 1] = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (i)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case SEQ_COL_NAME:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(NAMEOID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "sequence_name";
|
2002-03-21 17:02:16 +01:00
|
|
|
namestrcpy(&name, seq->sequence->relname);
|
1998-03-30 19:15:26 +02:00
|
|
|
value[i - 1] = NameGetDatum(&name);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case SEQ_COL_LASTVAL:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(INT8OID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "last_value";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = Int64GetDatumFast(new.last_value);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
2008-05-17 01:36:05 +02:00
|
|
|
case SEQ_COL_STARTVAL:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(INT8OID, -1, InvalidOid);
|
2008-05-17 01:36:05 +02:00
|
|
|
coldef->colname = "start_value";
|
|
|
|
value[i - 1] = Int64GetDatumFast(new.start_value);
|
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
case SEQ_COL_INCBY:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(INT8OID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "increment_by";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = Int64GetDatumFast(new.increment_by);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case SEQ_COL_MAXVALUE:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(INT8OID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "max_value";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = Int64GetDatumFast(new.max_value);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case SEQ_COL_MINVALUE:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(INT8OID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "min_value";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = Int64GetDatumFast(new.min_value);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case SEQ_COL_CACHE:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(INT8OID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "cache_value";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = Int64GetDatumFast(new.cache_value);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
2000-11-30 02:47:33 +01:00
|
|
|
case SEQ_COL_LOG:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(INT8OID, -1, InvalidOid);
|
2000-11-30 02:47:33 +01:00
|
|
|
coldef->colname = "log_cnt";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = Int64GetDatum((int64) 1);
|
2000-11-30 02:47:33 +01:00
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
case SEQ_COL_CYCLE:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(BOOLOID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "is_cycled";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = BoolGetDatum(new.is_cycled);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case SEQ_COL_CALLED:
|
2011-02-08 22:04:18 +01:00
|
|
|
coldef->typeName = makeTypeNameFromOid(BOOLOID, -1, InvalidOid);
|
1997-09-08 04:41:22 +02:00
|
|
|
coldef->colname = "is_called";
|
2001-08-16 22:38:56 +02:00
|
|
|
value[i - 1] = BoolGetDatum(false);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
stmt->tableElts = lappend(stmt->tableElts, coldef);
|
|
|
|
}
|
|
|
|
|
2002-03-21 17:02:16 +01:00
|
|
|
stmt->relation = seq->sequence;
|
|
|
|
stmt->inhRelations = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
stmt->constraints = NIL;
|
2009-04-04 23:12:31 +02:00
|
|
|
stmt->options = list_make1(defWithOids(false));
|
2002-11-11 23:19:25 +01:00
|
|
|
stmt->oncommit = ONCOMMIT_NOOP;
|
2004-07-12 07:38:11 +02:00
|
|
|
stmt->tablespacename = NULL;
|
2010-07-26 01:21:22 +02:00
|
|
|
stmt->if_not_exists = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2010-08-18 20:35:21 +02:00
|
|
|
seqoid = DefineRelation(stmt, RELKIND_SEQUENCE, seq->ownerId);
|
2010-07-26 01:21:22 +02:00
|
|
|
Assert(seqoid != InvalidOid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-03-22 03:56:37 +01:00
|
|
|
rel = heap_open(seqoid, AccessExclusiveLock);
|
1998-09-01 05:29:17 +02:00
|
|
|
tupDesc = RelationGetDescr(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
/* now initialize the sequence's data */
|
|
|
|
tuple = heap_form_tuple(tupDesc, value, null);
|
|
|
|
fill_seq_with_data(rel, tuple);
|
|
|
|
|
|
|
|
/* process OWNED BY if given */
|
|
|
|
if (owned_by)
|
|
|
|
process_owned_by(rel, owned_by);
|
|
|
|
|
|
|
|
heap_close(rel, NoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset a sequence to its initial value.
|
|
|
|
*
|
|
|
|
* The change is made transactionally, so that on failure of the current
|
|
|
|
* transaction, the sequence will be restored to its previous state.
|
|
|
|
* We do that by creating a whole new relfilenode for the sequence; so this
|
|
|
|
* works much like the rewriting forms of ALTER TABLE.
|
|
|
|
*
|
|
|
|
* Caller is assumed to have acquired AccessExclusiveLock on the sequence,
|
|
|
|
* which must not be released until end of transaction. Caller is also
|
|
|
|
* responsible for permissions checking.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ResetSequence(Oid seq_relid)
|
|
|
|
{
|
|
|
|
Relation seq_rel;
|
|
|
|
SeqTable elm;
|
|
|
|
Form_pg_sequence seq;
|
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
HeapTuple tuple;
|
|
|
|
HeapTupleData tupledata;
|
|
|
|
ItemId lp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the old sequence. This does a bit more work than really
|
|
|
|
* necessary, but it's simple, and we do want to double-check that it's
|
|
|
|
* indeed a sequence.
|
|
|
|
*/
|
|
|
|
init_sequence(seq_relid, &elm, &seq_rel);
|
|
|
|
seq = read_info(elm, seq_rel, &buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the existing sequence tuple.
|
|
|
|
*/
|
|
|
|
page = BufferGetPage(buf);
|
|
|
|
lp = PageGetItemId(page, FirstOffsetNumber);
|
|
|
|
Assert(ItemIdIsNormal(lp));
|
|
|
|
|
|
|
|
tupledata.t_data = (HeapTupleHeader) PageGetItem(page, lp);
|
|
|
|
tupledata.t_len = ItemIdGetLength(lp);
|
|
|
|
tuple = heap_copytuple(&tupledata);
|
|
|
|
|
|
|
|
/* Now we're done with the old page */
|
|
|
|
UnlockReleaseBuffer(buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modify the copied tuple to execute the restart (compare the RESTART
|
|
|
|
* action in AlterSequence)
|
|
|
|
*/
|
|
|
|
seq = (Form_pg_sequence) GETSTRUCT(tuple);
|
|
|
|
seq->last_value = seq->start_value;
|
|
|
|
seq->is_called = false;
|
|
|
|
seq->log_cnt = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new storage file for the sequence. We want to keep the
|
|
|
|
* sequence's relfrozenxid at 0, since it won't contain any unfrozen XIDs.
|
|
|
|
*/
|
|
|
|
RelationSetNewRelfilenode(seq_rel, InvalidTransactionId);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert the modified tuple into the new storage file.
|
|
|
|
*/
|
|
|
|
fill_seq_with_data(seq_rel, tuple);
|
|
|
|
|
|
|
|
/* Clear local cache so that we don't think we have cached numbers */
|
|
|
|
/* Note that we do not change the currval() state */
|
|
|
|
elm->cached = elm->last;
|
|
|
|
|
|
|
|
relation_close(seq_rel, NoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a sequence's relation with the specified tuple as content
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fill_seq_with_data(Relation rel, HeapTuple tuple)
|
|
|
|
{
|
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
sequence_magic *sm;
|
|
|
|
|
2001-06-29 23:08:25 +02:00
|
|
|
/* Initialize first page of relation with special magic number */
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
buf = ReadBuffer(rel, P_NEW);
|
2001-06-29 23:08:25 +02:00
|
|
|
Assert(BufferGetBlockNumber(buf) == 0);
|
|
|
|
|
2008-07-13 22:45:47 +02:00
|
|
|
page = BufferGetPage(buf);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2008-07-13 22:45:47 +02:00
|
|
|
PageInit(page, BufferGetPageSize(buf), sizeof(sequence_magic));
|
1997-09-07 07:04:48 +02:00
|
|
|
sm = (sequence_magic *) PageGetSpecialPointer(page);
|
|
|
|
sm->magic = SEQ_MAGIC;
|
|
|
|
|
2001-06-29 23:08:25 +02:00
|
|
|
/* hack: ensure heap_insert will insert on the just-created page */
|
2010-02-09 22:43:30 +01:00
|
|
|
RelationSetTargetBlock(rel, 0);
|
2001-06-29 23:08:25 +02:00
|
|
|
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
/* Now insert sequence tuple */
|
2006-07-04 00:45:41 +02:00
|
|
|
simple_heap_insert(rel, tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-01-11 19:16:04 +01:00
|
|
|
Assert(ItemPointerGetOffsetNumber(&(tuple->t_self)) == FirstOffsetNumber);
|
|
|
|
|
2001-04-03 23:58:00 +02:00
|
|
|
/*
|
2002-01-11 19:16:04 +01:00
|
|
|
* Two special hacks here:
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* 1. Since VACUUM does not process sequences, we have to force the tuple
|
|
|
|
* to have xmin = FrozenTransactionId now. Otherwise it would become
|
2002-09-04 22:31:48 +02:00
|
|
|
* invisible to SELECTs after 2G transactions. It is okay to do this
|
2002-01-11 19:16:04 +01:00
|
|
|
* because if the current transaction aborts, no other xact will ever
|
|
|
|
* examine the sequence tuple anyway.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* 2. Even though heap_insert emitted a WAL log record, we have to emit an
|
|
|
|
* XLOG_SEQ_LOG record too, since (a) the heap_insert record will not have
|
|
|
|
* the right xmin, and (b) REDO of the heap_insert record would re-init
|
|
|
|
* page and sequence magic number would be lost. This means two log
|
|
|
|
* records instead of one :-(
|
2001-04-03 23:58:00 +02:00
|
|
|
*/
|
2001-04-04 17:43:25 +02:00
|
|
|
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-04-03 23:58:00 +02:00
|
|
|
START_CRIT_SECTION();
|
2002-01-11 19:16:04 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Note that the "tuple" structure is still just a local tuple record
|
2008-11-02 02:45:28 +01:00
|
|
|
* created by heap_form_tuple; its t_data pointer doesn't point at the
|
2005-10-15 04:49:52 +02:00
|
|
|
* disk buffer. To scribble on the disk buffer we need to fetch the
|
|
|
|
* item pointer. But do the same to the local tuple, since that will
|
|
|
|
* be the source for the WAL log record, below.
|
2002-01-11 19:16:04 +01:00
|
|
|
*/
|
|
|
|
ItemId itemId;
|
|
|
|
Item item;
|
|
|
|
|
|
|
|
itemId = PageGetItemId((Page) page, FirstOffsetNumber);
|
|
|
|
item = PageGetItem((Page) page, itemId);
|
|
|
|
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetXmin((HeapTupleHeader) item, FrozenTransactionId);
|
2002-01-11 19:16:04 +01:00
|
|
|
((HeapTupleHeader) item)->t_infomask |= HEAP_XMIN_COMMITTED;
|
|
|
|
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetXmin(tuple->t_data, FrozenTransactionId);
|
2002-01-11 19:16:04 +01:00
|
|
|
tuple->t_data->t_infomask |= HEAP_XMIN_COMMITTED;
|
|
|
|
}
|
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buf);
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* XLOG stuff */
|
2010-12-13 18:34:26 +01:00
|
|
|
if (RelationNeedsWAL(rel))
|
2001-04-03 23:58:00 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
xl_seq_rec xlrec;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
XLogRecData rdata[2];
|
|
|
|
Form_pg_sequence newseq = (Form_pg_sequence) GETSTRUCT(tuple);
|
2001-04-03 23:58:00 +02:00
|
|
|
|
|
|
|
/* We do not log first nextval call, so "advance" sequence here */
|
2002-01-11 19:16:04 +01:00
|
|
|
/* Note we are scribbling on local tuple, not the disk buffer */
|
2001-08-16 22:38:56 +02:00
|
|
|
newseq->is_called = true;
|
2001-04-03 23:58:00 +02:00
|
|
|
newseq->log_cnt = 0;
|
|
|
|
|
|
|
|
xlrec.node = rel->rd_node;
|
|
|
|
rdata[0].data = (char *) &xlrec;
|
|
|
|
rdata[0].len = sizeof(xl_seq_rec);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2001-04-03 23:58:00 +02:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
2001-08-16 22:38:56 +02:00
|
|
|
rdata[1].data = (char *) tuple->t_data;
|
2001-04-03 23:58:00 +02:00
|
|
|
rdata[1].len = tuple->t_len;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = InvalidBuffer;
|
2001-04-03 23:58:00 +02:00
|
|
|
rdata[1].next = NULL;
|
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG, rdata);
|
2001-04-03 23:58:00 +02:00
|
|
|
|
|
|
|
PageSetLSN(page, recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2001-04-03 23:58:00 +02:00
|
|
|
}
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-04-03 23:58:00 +02:00
|
|
|
END_CRIT_SECTION();
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
1997-04-02 05:51:23 +02:00
|
|
|
}
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
/*
|
|
|
|
* AlterSequence
|
|
|
|
*
|
2003-11-24 17:54:07 +01:00
|
|
|
* Modify the definition of a sequence relation
|
2003-03-20 08:02:11 +01:00
|
|
|
*/
|
|
|
|
void
|
2003-08-08 23:42:59 +02:00
|
|
|
AlterSequence(AlterSeqStmt *stmt)
|
2003-03-20 08:02:11 +01:00
|
|
|
{
|
2005-10-03 01:50:16 +02:00
|
|
|
Oid relid;
|
2003-03-20 08:02:11 +01:00
|
|
|
SeqTable elm;
|
|
|
|
Relation seqrel;
|
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
Form_pg_sequence seq;
|
|
|
|
FormData_pg_sequence new;
|
2006-08-21 02:57:26 +02:00
|
|
|
List *owned_by;
|
2003-03-20 08:02:11 +01:00
|
|
|
|
|
|
|
/* open and AccessShareLock sequence */
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
relid = RangeVarGetRelid(stmt->sequence, false);
|
2005-10-03 01:50:16 +02:00
|
|
|
init_sequence(relid, &elm, &seqrel);
|
2003-03-20 08:02:11 +01:00
|
|
|
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
/* allow ALTER to sequence owner only */
|
|
|
|
if (!pg_class_ownercheck(relid, GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
|
|
|
|
stmt->sequence->relname);
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
/* lock page' buffer and read tuple into new sequence structure */
|
2003-07-20 23:56:35 +02:00
|
|
|
seq = read_info(elm, seqrel, &buf);
|
2003-03-20 08:02:11 +01:00
|
|
|
page = BufferGetPage(buf);
|
|
|
|
|
2008-05-17 03:20:39 +02:00
|
|
|
/* Copy old values of options into workspace */
|
|
|
|
memcpy(&new, seq, sizeof(FormData_pg_sequence));
|
|
|
|
|
|
|
|
/* Check and set new values */
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
init_params(stmt->options, false, &new, &owned_by);
|
2003-03-20 08:02:11 +01:00
|
|
|
|
2007-10-25 20:54:03 +02:00
|
|
|
/* Clear local cache so that we don't think we have cached numbers */
|
|
|
|
/* Note that we do not change the currval() state */
|
|
|
|
elm->cached = elm->last;
|
|
|
|
|
2003-11-24 17:54:07 +01:00
|
|
|
/* Now okay to update the on-disk tuple */
|
2004-04-06 18:39:30 +02:00
|
|
|
memcpy(seq, &new, sizeof(FormData_pg_sequence));
|
2003-03-20 08:02:11 +01:00
|
|
|
|
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buf);
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
/* XLOG stuff */
|
2010-12-13 18:34:26 +01:00
|
|
|
if (RelationNeedsWAL(seqrel))
|
2003-03-20 08:02:11 +01:00
|
|
|
{
|
|
|
|
xl_seq_rec xlrec;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
XLogRecData rdata[2];
|
|
|
|
|
|
|
|
xlrec.node = seqrel->rd_node;
|
|
|
|
rdata[0].data = (char *) &xlrec;
|
|
|
|
rdata[0].len = sizeof(xl_seq_rec);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2003-03-20 08:02:11 +01:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
|
|
|
rdata[1].data = (char *) page + ((PageHeader) page)->pd_upper;
|
|
|
|
rdata[1].len = ((PageHeader) page)->pd_special -
|
|
|
|
((PageHeader) page)->pd_upper;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = InvalidBuffer;
|
2003-03-20 08:02:11 +01:00
|
|
|
rdata[1].next = NULL;
|
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG, rdata);
|
2003-03-20 08:02:11 +01:00
|
|
|
|
|
|
|
PageSetLSN(page, recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2003-03-20 08:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
2003-03-20 08:02:11 +01:00
|
|
|
|
2006-08-21 02:57:26 +02:00
|
|
|
/* process OWNED BY if given */
|
|
|
|
if (owned_by)
|
|
|
|
process_owned_by(seqrel, owned_by);
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
relation_close(seqrel, NoLock);
|
|
|
|
}
|
|
|
|
|
1997-04-02 05:51:23 +02:00
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
/*
|
|
|
|
* Note: nextval with a text argument is no longer exported as a pg_proc
|
|
|
|
* entry, but we keep it around to ease porting of C code that may have
|
|
|
|
* called the function directly.
|
|
|
|
*/
|
2000-06-11 22:08:01 +02:00
|
|
|
Datum
|
|
|
|
nextval(PG_FUNCTION_ARGS)
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
2000-06-11 22:08:01 +02:00
|
|
|
text *seqin = PG_GETARG_TEXT_P(0);
|
2002-03-30 02:02:42 +01:00
|
|
|
RangeVar *sequence;
|
2005-10-03 01:50:16 +02:00
|
|
|
Oid relid;
|
|
|
|
|
|
|
|
sequence = makeRangeVarFromNameList(textToQualifiedNameList(seqin));
|
|
|
|
relid = RangeVarGetRelid(sequence, false);
|
|
|
|
|
|
|
|
PG_RETURN_INT64(nextval_internal(relid));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
nextval_oid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
|
|
|
|
PG_RETURN_INT64(nextval_internal(relid));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64
|
|
|
|
nextval_internal(Oid relid)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
SeqTable elm;
|
2002-05-22 23:40:55 +02:00
|
|
|
Relation seqrel;
|
1997-09-08 04:41:22 +02:00
|
|
|
Buffer buf;
|
2002-03-15 20:20:36 +01:00
|
|
|
Page page;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_sequence seq;
|
2001-08-16 22:38:56 +02:00
|
|
|
int64 incby,
|
1997-09-08 04:41:22 +02:00
|
|
|
maxv,
|
|
|
|
minv,
|
2000-11-30 02:47:33 +01:00
|
|
|
cache,
|
|
|
|
log,
|
|
|
|
fetch,
|
|
|
|
last;
|
2001-08-16 22:38:56 +02:00
|
|
|
int64 result,
|
1997-09-08 04:41:22 +02:00
|
|
|
next,
|
|
|
|
rescnt = 0;
|
2000-11-30 02:47:33 +01:00
|
|
|
bool logit = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
/* open and AccessShareLock sequence */
|
2005-10-03 01:50:16 +02:00
|
|
|
init_sequence(relid, &elm, &seqrel);
|
2000-06-11 22:08:01 +02:00
|
|
|
|
2006-01-21 03:16:21 +01:00
|
|
|
if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_USAGE) != ACLCHECK_OK &&
|
|
|
|
pg_class_aclcheck(elm->relid, GetUserId(), ACL_UPDATE) != ACLCHECK_OK)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2003-08-01 02:15:26 +02:00
|
|
|
errmsg("permission denied for sequence %s",
|
2005-10-03 01:50:16 +02:00
|
|
|
RelationGetRelationName(seqrel))));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2010-02-20 22:24:02 +01:00
|
|
|
/* read-only transactions may only modify temp sequences */
|
2010-08-13 22:10:54 +02:00
|
|
|
if (seqrel->rd_backend != MyBackendId)
|
2010-02-20 22:24:02 +01:00
|
|
|
PreventCommandIfReadOnly("nextval()");
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (elm->last != elm->cached) /* some numbers were cached */
|
|
|
|
{
|
2007-10-25 20:54:03 +02:00
|
|
|
Assert(elm->last_valid);
|
|
|
|
Assert(elm->increment != 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
elm->last += elm->increment;
|
2002-05-22 23:40:55 +02:00
|
|
|
relation_close(seqrel, NoLock);
|
2007-10-25 20:54:03 +02:00
|
|
|
last_used_seq = elm;
|
2005-10-03 01:50:16 +02:00
|
|
|
return elm->last;
|
1997-04-02 05:51:23 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
/* lock page' buffer and read tuple */
|
2003-07-20 23:56:35 +02:00
|
|
|
seq = read_info(elm, seqrel, &buf);
|
2002-03-15 20:20:36 +01:00
|
|
|
page = BufferGetPage(buf);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-30 02:47:33 +01:00
|
|
|
last = next = result = seq->last_value;
|
1997-09-07 07:04:48 +02:00
|
|
|
incby = seq->increment_by;
|
|
|
|
maxv = seq->max_value;
|
|
|
|
minv = seq->min_value;
|
2000-11-30 02:47:33 +01:00
|
|
|
fetch = cache = seq->cache_value;
|
|
|
|
log = seq->log_cnt;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-08-16 22:38:56 +02:00
|
|
|
if (!seq->is_called)
|
2000-11-30 02:47:33 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
rescnt++; /* last_value if not called */
|
2000-11-30 02:47:33 +01:00
|
|
|
fetch--;
|
|
|
|
log--;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-03-15 20:20:36 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Decide whether we should emit a WAL log record. If so, force up the
|
|
|
|
* fetch count to grab SEQ_LOG_VALS more values than we actually need to
|
|
|
|
* cache. (These will then be usable without logging.)
|
2002-03-15 20:20:36 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* If this is the first nextval after a checkpoint, we must force a new
|
|
|
|
* WAL record to be written anyway, else replay starting from the
|
|
|
|
* checkpoint would fail to advance the sequence past the logged values.
|
|
|
|
* In this case we may as well fetch extra values.
|
2002-03-15 20:20:36 +01:00
|
|
|
*/
|
2000-11-30 02:47:33 +01:00
|
|
|
if (log < fetch)
|
|
|
|
{
|
2002-03-15 20:20:36 +01:00
|
|
|
/* forced log to satisfy local demand for values */
|
|
|
|
fetch = log = fetch + SEQ_LOG_VALS;
|
2000-11-30 02:47:33 +01:00
|
|
|
logit = true;
|
|
|
|
}
|
2002-03-15 20:20:36 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
XLogRecPtr redoptr = GetRedoRecPtr();
|
|
|
|
|
|
|
|
if (XLByteLE(PageGetLSN(page), redoptr))
|
|
|
|
{
|
|
|
|
/* last update of seq was before checkpoint */
|
|
|
|
fetch = log = fetch + SEQ_LOG_VALS;
|
|
|
|
logit = true;
|
|
|
|
}
|
|
|
|
}
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
while (fetch) /* try to fetch cache [+ log ] numbers */
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check MAXVALUE for ascending sequences and MINVALUE for descending
|
|
|
|
* sequences
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-06-11 22:08:01 +02:00
|
|
|
if (incby > 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-11 22:08:01 +02:00
|
|
|
/* ascending sequence */
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((maxv >= 0 && next > maxv - incby) ||
|
|
|
|
(maxv < 0 && next + incby > maxv))
|
|
|
|
{
|
|
|
|
if (rescnt > 0)
|
2000-11-30 02:47:33 +01:00
|
|
|
break; /* stop fetching */
|
2001-08-16 22:38:56 +02:00
|
|
|
if (!seq->is_cycled)
|
2002-09-03 20:50:54 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char buf[100];
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
snprintf(buf, sizeof(buf), INT64_FORMAT, maxv);
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("nextval: reached maximum value of sequence \"%s\" (%s)",
|
|
|
|
RelationGetRelationName(seqrel), buf)));
|
2002-09-03 20:50:54 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
next = minv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
next += incby;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-06-11 22:08:01 +02:00
|
|
|
/* descending sequence */
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((minv < 0 && next < minv - incby) ||
|
|
|
|
(minv >= 0 && next + incby < minv))
|
|
|
|
{
|
|
|
|
if (rescnt > 0)
|
2000-11-30 02:47:33 +01:00
|
|
|
break; /* stop fetching */
|
2001-08-16 22:38:56 +02:00
|
|
|
if (!seq->is_cycled)
|
2002-09-03 20:50:54 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char buf[100];
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
snprintf(buf, sizeof(buf), INT64_FORMAT, minv);
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("nextval: reached minimum value of sequence \"%s\" (%s)",
|
|
|
|
RelationGetRelationName(seqrel), buf)));
|
2002-09-03 20:50:54 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
next = maxv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
next += incby;
|
|
|
|
}
|
2000-11-30 02:47:33 +01:00
|
|
|
fetch--;
|
|
|
|
if (rescnt < cache)
|
|
|
|
{
|
|
|
|
log--;
|
|
|
|
rescnt++;
|
|
|
|
last = next;
|
2001-03-22 05:01:46 +01:00
|
|
|
if (rescnt == 1) /* if it's first result - */
|
|
|
|
result = next; /* it's what to return */
|
2000-11-30 02:47:33 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2002-03-15 20:20:36 +01:00
|
|
|
log -= fetch; /* adjust for any unfetched numbers */
|
|
|
|
Assert(log >= 0);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* save info in local cache */
|
|
|
|
elm->last = result; /* last returned number */
|
2000-11-30 02:47:33 +01:00
|
|
|
elm->cached = last; /* last fetched number */
|
2007-10-25 20:54:03 +02:00
|
|
|
elm->last_valid = true;
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2005-06-07 09:08:35 +02:00
|
|
|
last_used_seq = elm;
|
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
START_CRIT_SECTION();
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buf);
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* XLOG stuff */
|
2010-12-13 18:34:26 +01:00
|
|
|
if (logit && RelationNeedsWAL(seqrel))
|
2000-11-30 02:47:33 +01:00
|
|
|
{
|
|
|
|
xl_seq_rec xlrec;
|
|
|
|
XLogRecPtr recptr;
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecData rdata[2];
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
xlrec.node = seqrel->rd_node;
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[0].data = (char *) &xlrec;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].len = sizeof(xl_seq_rec);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* set values that will be saved in xlog */
|
2000-12-28 14:00:29 +01:00
|
|
|
seq->last_value = next;
|
2001-08-16 22:38:56 +02:00
|
|
|
seq->is_called = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
seq->log_cnt = 0;
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[1].data = (char *) page + ((PageHeader) page)->pd_upper;
|
|
|
|
rdata[1].len = ((PageHeader) page)->pd_special -
|
|
|
|
((PageHeader) page)->pd_upper;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[1].next = NULL;
|
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG, rdata);
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
PageSetLSN(page, recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2000-11-30 02:47:33 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
/* update on-disk data */
|
2000-11-30 02:47:33 +01:00
|
|
|
seq->last_value = last; /* last fetched number */
|
2001-08-16 22:38:56 +02:00
|
|
|
seq->is_called = true;
|
2000-11-30 02:47:33 +01:00
|
|
|
seq->log_cnt = log; /* how much is logged */
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
END_CRIT_SECTION();
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
relation_close(seqrel, NoLock);
|
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
return result;
|
1997-04-02 05:51:23 +02:00
|
|
|
}
|
|
|
|
|
2000-06-11 22:08:01 +02:00
|
|
|
Datum
|
2005-10-03 01:50:16 +02:00
|
|
|
currval_oid(PG_FUNCTION_ARGS)
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
2005-10-03 01:50:16 +02:00
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
int64 result;
|
1997-09-08 04:41:22 +02:00
|
|
|
SeqTable elm;
|
2002-05-22 23:40:55 +02:00
|
|
|
Relation seqrel;
|
2002-03-30 02:02:42 +01:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
/* open and AccessShareLock sequence */
|
2005-10-03 01:50:16 +02:00
|
|
|
init_sequence(relid, &elm, &seqrel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-01-21 03:16:21 +01:00
|
|
|
if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_SELECT) != ACLCHECK_OK &&
|
|
|
|
pg_class_aclcheck(elm->relid, GetUserId(), ACL_USAGE) != ACLCHECK_OK)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2003-08-01 02:15:26 +02:00
|
|
|
errmsg("permission denied for sequence %s",
|
2005-10-03 01:50:16 +02:00
|
|
|
RelationGetRelationName(seqrel))));
|
2002-03-22 00:27:25 +01:00
|
|
|
|
2007-10-25 20:54:03 +02:00
|
|
|
if (!elm->last_valid)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("currval of sequence \"%s\" is not yet defined in this session",
|
2005-10-03 01:50:16 +02:00
|
|
|
RelationGetRelationName(seqrel))));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
result = elm->last;
|
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
relation_close(seqrel, NoLock);
|
|
|
|
|
2001-08-16 22:38:56 +02:00
|
|
|
PG_RETURN_INT64(result);
|
1997-04-02 05:51:23 +02:00
|
|
|
}
|
|
|
|
|
2005-06-07 09:08:35 +02:00
|
|
|
Datum
|
|
|
|
lastval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Relation seqrel;
|
|
|
|
int64 result;
|
|
|
|
|
|
|
|
if (last_used_seq == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("lastval is not yet defined in this session")));
|
|
|
|
|
|
|
|
/* Someone may have dropped the sequence since the last nextval() */
|
2010-02-14 19:42:19 +01:00
|
|
|
if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(last_used_seq->relid)))
|
2005-06-07 09:08:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("lastval is not yet defined in this session")));
|
|
|
|
|
2006-07-31 22:09:10 +02:00
|
|
|
seqrel = open_share_lock(last_used_seq);
|
2005-06-07 09:08:35 +02:00
|
|
|
|
|
|
|
/* nextval() must have already been called for this sequence */
|
2007-10-25 20:54:03 +02:00
|
|
|
Assert(last_used_seq->last_valid);
|
2005-06-07 09:08:35 +02:00
|
|
|
|
2006-01-21 03:16:21 +01:00
|
|
|
if (pg_class_aclcheck(last_used_seq->relid, GetUserId(), ACL_SELECT) != ACLCHECK_OK &&
|
|
|
|
pg_class_aclcheck(last_used_seq->relid, GetUserId(), ACL_USAGE) != ACLCHECK_OK)
|
2005-06-07 09:08:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for sequence %s",
|
|
|
|
RelationGetRelationName(seqrel))));
|
|
|
|
|
|
|
|
result = last_used_seq->last;
|
|
|
|
relation_close(seqrel, NoLock);
|
2005-10-03 01:50:16 +02:00
|
|
|
|
2005-06-07 09:08:35 +02:00
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2001-02-13 02:57:12 +01:00
|
|
|
* Main internal procedure that handles 2 & 3 arg forms of SETVAL.
|
|
|
|
*
|
|
|
|
* Note that the 3 arg version (which sets the is_called flag) is
|
|
|
|
* only for use in pg_dump, and setting the is_called flag may not
|
2001-03-22 05:01:46 +01:00
|
|
|
* work if multiple users are attached to the database and referencing
|
2001-02-13 02:57:12 +01:00
|
|
|
* the sequence (unlikely if pg_dump is restoring it).
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* It is necessary to have the 3 arg version so that pg_dump can
|
2001-02-13 02:57:12 +01:00
|
|
|
* restore the state of a sequence exactly during data-only restores -
|
|
|
|
* it is the only way to clear the is_called flag in an existing
|
|
|
|
* sequence.
|
|
|
|
*/
|
2000-10-16 19:08:11 +02:00
|
|
|
static void
|
2005-10-03 01:50:16 +02:00
|
|
|
do_setval(Oid relid, int64 next, bool iscalled)
|
1998-08-25 23:25:46 +02:00
|
|
|
{
|
|
|
|
SeqTable elm;
|
2002-05-22 23:40:55 +02:00
|
|
|
Relation seqrel;
|
1998-09-01 06:40:42 +02:00
|
|
|
Buffer buf;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_sequence seq;
|
1998-08-25 23:25:46 +02:00
|
|
|
|
2002-03-22 00:27:25 +01:00
|
|
|
/* open and AccessShareLock sequence */
|
2005-10-03 01:50:16 +02:00
|
|
|
init_sequence(relid, &elm, &seqrel);
|
2002-03-22 00:27:25 +01:00
|
|
|
|
|
|
|
if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_UPDATE) != ACLCHECK_OK)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2003-08-01 02:15:26 +02:00
|
|
|
errmsg("permission denied for sequence %s",
|
2005-10-03 01:50:16 +02:00
|
|
|
RelationGetRelationName(seqrel))));
|
1998-08-25 23:25:46 +02:00
|
|
|
|
2010-02-20 22:24:02 +01:00
|
|
|
/* read-only transactions may only modify temp sequences */
|
2010-08-13 22:10:54 +02:00
|
|
|
if (seqrel->rd_backend != MyBackendId)
|
2010-02-20 22:24:02 +01:00
|
|
|
PreventCommandIfReadOnly("setval()");
|
|
|
|
|
2002-03-22 00:27:25 +01:00
|
|
|
/* lock page' buffer and read tuple */
|
2003-07-20 23:56:35 +02:00
|
|
|
seq = read_info(elm, seqrel, &buf);
|
1998-08-25 23:25:46 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
if ((next < seq->min_value) || (next > seq->max_value))
|
2002-09-03 20:50:54 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char bufv[100],
|
|
|
|
bufm[100],
|
|
|
|
bufx[100];
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
snprintf(bufv, sizeof(bufv), INT64_FORMAT, next);
|
|
|
|
snprintf(bufm, sizeof(bufm), INT64_FORMAT, seq->min_value);
|
|
|
|
snprintf(bufx, sizeof(bufx), INT64_FORMAT, seq->max_value);
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("setval: value %s is out of bounds for sequence \"%s\" (%s..%s)",
|
2005-10-03 01:50:16 +02:00
|
|
|
bufv, RelationGetRelationName(seqrel),
|
|
|
|
bufm, bufx)));
|
2002-09-03 20:50:54 +02:00
|
|
|
}
|
1998-08-25 23:25:46 +02:00
|
|
|
|
2007-10-25 20:54:03 +02:00
|
|
|
/* Set the currval() state only if iscalled = true */
|
|
|
|
if (iscalled)
|
|
|
|
{
|
|
|
|
elm->last = next; /* last returned number */
|
|
|
|
elm->last_valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In any case, forget any future cached numbers */
|
|
|
|
elm->cached = elm->last;
|
1998-08-25 23:25:46 +02:00
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
START_CRIT_SECTION();
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buf);
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* XLOG stuff */
|
2010-12-13 18:34:26 +01:00
|
|
|
if (RelationNeedsWAL(seqrel))
|
2000-11-30 02:47:33 +01:00
|
|
|
{
|
|
|
|
xl_seq_rec xlrec;
|
|
|
|
XLogRecPtr recptr;
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecData rdata[2];
|
2000-12-28 14:00:29 +01:00
|
|
|
Page page = BufferGetPage(buf);
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
xlrec.node = seqrel->rd_node;
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[0].data = (char *) &xlrec;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].len = sizeof(xl_seq_rec);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* set values that will be saved in xlog */
|
2000-12-28 14:00:29 +01:00
|
|
|
seq->last_value = next;
|
2001-08-16 22:38:56 +02:00
|
|
|
seq->is_called = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
seq->log_cnt = 0;
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[1].data = (char *) page + ((PageHeader) page)->pd_upper;
|
|
|
|
rdata[1].len = ((PageHeader) page)->pd_special -
|
|
|
|
((PageHeader) page)->pd_upper;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[1].next = NULL;
|
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG, rdata);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
|
|
|
PageSetLSN(page, recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2000-11-30 02:47:33 +01:00
|
|
|
}
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
/* save info in sequence relation */
|
|
|
|
seq->last_value = next; /* last fetched number */
|
2001-08-16 22:38:56 +02:00
|
|
|
seq->is_called = iscalled;
|
2000-12-28 14:00:29 +01:00
|
|
|
seq->log_cnt = (iscalled) ? 0 : 1;
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
END_CRIT_SECTION();
|
1998-08-25 23:25:46 +02:00
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
2002-05-22 23:40:55 +02:00
|
|
|
|
|
|
|
relation_close(seqrel, NoLock);
|
2000-10-11 17:31:34 +02:00
|
|
|
}
|
|
|
|
|
2001-02-13 02:57:12 +01:00
|
|
|
/*
|
|
|
|
* Implement the 2 arg setval procedure.
|
|
|
|
* See do_setval for discussion.
|
|
|
|
*/
|
2000-10-11 17:31:34 +02:00
|
|
|
Datum
|
2005-10-03 01:50:16 +02:00
|
|
|
setval_oid(PG_FUNCTION_ARGS)
|
2000-10-11 17:31:34 +02:00
|
|
|
{
|
2005-10-03 01:50:16 +02:00
|
|
|
Oid relid = PG_GETARG_OID(0);
|
2001-08-16 22:38:56 +02:00
|
|
|
int64 next = PG_GETARG_INT64(1);
|
2002-03-30 02:02:42 +01:00
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
do_setval(relid, next, true);
|
2000-10-11 17:31:34 +02:00
|
|
|
|
2001-08-16 22:38:56 +02:00
|
|
|
PG_RETURN_INT64(next);
|
2000-10-11 17:31:34 +02:00
|
|
|
}
|
|
|
|
|
2001-02-13 02:57:12 +01:00
|
|
|
/*
|
|
|
|
* Implement the 3 arg setval procedure.
|
|
|
|
* See do_setval for discussion.
|
|
|
|
*/
|
2000-10-11 17:31:34 +02:00
|
|
|
Datum
|
2005-10-03 01:50:16 +02:00
|
|
|
setval3_oid(PG_FUNCTION_ARGS)
|
2000-10-11 17:31:34 +02:00
|
|
|
{
|
2005-10-03 01:50:16 +02:00
|
|
|
Oid relid = PG_GETARG_OID(0);
|
2001-08-16 22:38:56 +02:00
|
|
|
int64 next = PG_GETARG_INT64(1);
|
2000-10-11 17:31:34 +02:00
|
|
|
bool iscalled = PG_GETARG_BOOL(2);
|
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
do_setval(relid, next, iscalled);
|
2000-06-11 22:08:01 +02:00
|
|
|
|
2002-03-30 02:02:42 +01:00
|
|
|
PG_RETURN_INT64(next);
|
1998-08-25 23:25:46 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-06-07 09:08:35 +02:00
|
|
|
/*
|
2006-07-31 22:09:10 +02:00
|
|
|
* Open the sequence and acquire AccessShareLock if needed
|
|
|
|
*
|
2005-06-07 09:08:35 +02:00
|
|
|
* If we haven't touched the sequence already in this transaction,
|
2005-10-15 04:49:52 +02:00
|
|
|
* we need to acquire AccessShareLock. We arrange for the lock to
|
2005-06-07 09:08:35 +02:00
|
|
|
* be owned by the top transaction, so that we don't need to do it
|
|
|
|
* more than once per xact.
|
|
|
|
*/
|
2006-07-31 22:09:10 +02:00
|
|
|
static Relation
|
|
|
|
open_share_lock(SeqTable seq)
|
2005-06-07 09:08:35 +02:00
|
|
|
{
|
2007-09-05 20:10:48 +02:00
|
|
|
LocalTransactionId thislxid = MyProc->lxid;
|
2005-06-07 09:08:35 +02:00
|
|
|
|
2006-07-31 22:09:10 +02:00
|
|
|
/* Get the lock if not already held in this xact */
|
2007-09-05 20:10:48 +02:00
|
|
|
if (seq->lxid != thislxid)
|
2005-06-07 09:08:35 +02:00
|
|
|
{
|
|
|
|
ResourceOwner currentOwner;
|
|
|
|
|
|
|
|
currentOwner = CurrentResourceOwner;
|
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
CurrentResourceOwner = TopTransactionResourceOwner;
|
2006-07-31 22:09:10 +02:00
|
|
|
LockRelationOid(seq->relid, AccessShareLock);
|
2005-06-07 09:08:35 +02:00
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
/* Ensure CurrentResourceOwner is restored on error */
|
|
|
|
CurrentResourceOwner = currentOwner;
|
|
|
|
PG_RE_THROW();
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
CurrentResourceOwner = currentOwner;
|
|
|
|
|
2006-07-31 22:09:10 +02:00
|
|
|
/* Flag that we have a lock in the current xact */
|
2007-09-05 20:10:48 +02:00
|
|
|
seq->lxid = thislxid;
|
2005-06-07 09:08:35 +02:00
|
|
|
}
|
2006-07-31 22:09:10 +02:00
|
|
|
|
|
|
|
/* We now know we have AccessShareLock, and can safely open the rel */
|
|
|
|
return relation_open(seq->relid, NoLock);
|
2005-06-07 09:08:35 +02:00
|
|
|
}
|
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
/*
|
2005-10-03 01:50:16 +02:00
|
|
|
* Given a relation OID, open and lock the sequence. p_elm and p_rel are
|
2002-05-22 23:40:55 +02:00
|
|
|
* output parameters.
|
|
|
|
*/
|
|
|
|
static void
|
2005-10-03 01:50:16 +02:00
|
|
|
init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel)
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
SeqTable elm;
|
1999-12-31 01:54:27 +01:00
|
|
|
Relation seqrel;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-09-16 18:58:44 +02:00
|
|
|
/* Look to see if we already have a seqtable entry for relation */
|
|
|
|
for (elm = seqtab; elm != NULL; elm = elm->next)
|
|
|
|
{
|
|
|
|
if (elm->relid == relid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-03-30 02:02:42 +01:00
|
|
|
/*
|
2002-05-22 23:40:55 +02:00
|
|
|
* Allocate new seqtable entry if we didn't find one.
|
2002-03-30 02:02:42 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* NOTE: seqtable entries remain in the list for the life of a backend. If
|
|
|
|
* the sequence itself is deleted then the entry becomes wasted memory,
|
|
|
|
* but it's small enough that this should not matter.
|
2002-09-04 22:31:48 +02:00
|
|
|
*/
|
2002-05-22 23:40:55 +02:00
|
|
|
if (elm == NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Time to make a new seqtable entry. These entries live as long as
|
|
|
|
* the backend does, so we use plain malloc for them.
|
1999-12-31 01:54:27 +01:00
|
|
|
*/
|
|
|
|
elm = (SeqTable) malloc(sizeof(SeqTableData));
|
2001-06-01 21:54:58 +02:00
|
|
|
if (elm == NULL)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2002-03-30 02:02:42 +01:00
|
|
|
elm->relid = relid;
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
elm->filenode = InvalidOid;
|
2007-09-05 20:10:48 +02:00
|
|
|
elm->lxid = InvalidLocalTransactionId;
|
2007-10-25 20:54:03 +02:00
|
|
|
elm->last_valid = false;
|
2002-05-22 23:40:55 +02:00
|
|
|
elm->last = elm->cached = elm->increment = 0;
|
|
|
|
elm->next = seqtab;
|
|
|
|
seqtab = elm;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2006-07-31 22:09:10 +02:00
|
|
|
/*
|
|
|
|
* Open the sequence relation.
|
|
|
|
*/
|
|
|
|
seqrel = open_share_lock(elm);
|
|
|
|
|
|
|
|
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a sequence",
|
|
|
|
RelationGetRelationName(seqrel))));
|
2002-05-22 23:40:55 +02:00
|
|
|
|
Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error. This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.
To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY. If the transaction aborts, we'll
automatically revert to the old storage file. This acts just like a
rewriting ALTER TABLE operation. A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.
The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent. Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.
In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired. The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error. Transactional rollback is a much better
solution to that.
Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 22:42:18 +01:00
|
|
|
/*
|
|
|
|
* If the sequence has been transactionally replaced since we last saw it,
|
|
|
|
* discard any cached-but-unissued values. We do not touch the currval()
|
|
|
|
* state, however.
|
|
|
|
*/
|
|
|
|
if (seqrel->rd_rel->relfilenode != elm->filenode)
|
|
|
|
{
|
|
|
|
elm->filenode = seqrel->rd_rel->relfilenode;
|
|
|
|
elm->cached = elm->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return results */
|
2002-05-22 23:40:55 +02:00
|
|
|
*p_elm = elm;
|
|
|
|
*p_rel = seqrel;
|
1997-04-02 05:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
/* Given an opened relation, lock the page buffer and find the tuple */
|
|
|
|
static Form_pg_sequence
|
2003-07-20 23:56:35 +02:00
|
|
|
read_info(SeqTable elm, Relation rel, Buffer *buf)
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
2008-07-13 22:45:47 +02:00
|
|
|
Page page;
|
2002-05-22 23:40:55 +02:00
|
|
|
ItemId lp;
|
|
|
|
HeapTupleData tuple;
|
|
|
|
sequence_magic *sm;
|
|
|
|
Form_pg_sequence seq;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-22 23:40:55 +02:00
|
|
|
*buf = ReadBuffer(rel, 0);
|
|
|
|
LockBuffer(*buf, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
2008-07-13 22:45:47 +02:00
|
|
|
page = BufferGetPage(*buf);
|
2002-05-22 23:40:55 +02:00
|
|
|
sm = (sequence_magic *) PageGetSpecialPointer(page);
|
|
|
|
|
|
|
|
if (sm->magic != SEQ_MAGIC)
|
2003-07-28 02:09:16 +02:00
|
|
|
elog(ERROR, "bad magic number in sequence \"%s\": %08X",
|
|
|
|
RelationGetRelationName(rel), sm->magic);
|
2002-05-22 23:40:55 +02:00
|
|
|
|
|
|
|
lp = PageGetItemId(page, FirstOffsetNumber);
|
2007-09-13 00:10:26 +02:00
|
|
|
Assert(ItemIdIsNormal(lp));
|
2008-07-13 22:45:47 +02:00
|
|
|
tuple.t_data = (HeapTupleHeader) PageGetItem(page, lp);
|
2002-05-22 23:40:55 +02:00
|
|
|
|
|
|
|
seq = (Form_pg_sequence) GETSTRUCT(&tuple);
|
|
|
|
|
2007-10-25 20:54:03 +02:00
|
|
|
/* this is a handy place to update our copy of the increment */
|
2002-05-22 23:40:55 +02:00
|
|
|
elm->increment = seq->increment_by;
|
|
|
|
|
|
|
|
return seq;
|
1997-04-02 05:51:23 +02:00
|
|
|
}
|
|
|
|
|
2003-11-24 17:54:07 +01:00
|
|
|
/*
|
|
|
|
* init_params: process the options list of CREATE or ALTER SEQUENCE,
|
2006-08-21 02:57:26 +02:00
|
|
|
* and store the values into appropriate fields of *new. Also set
|
|
|
|
* *owned_by to any OWNED BY option, or to NIL if there is none.
|
2003-11-24 17:54:07 +01:00
|
|
|
*
|
|
|
|
* If isInit is true, fill any unspecified options with default values;
|
|
|
|
* otherwise, do not change existing options that aren't explicitly overridden.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
static void
|
2006-08-21 02:57:26 +02:00
|
|
|
init_params(List *options, bool isInit,
|
2008-05-17 03:20:39 +02:00
|
|
|
Form_pg_sequence new, List **owned_by)
|
1997-04-02 05:51:23 +02:00
|
|
|
{
|
2008-05-17 03:20:39 +02:00
|
|
|
DefElem *start_value = NULL;
|
|
|
|
DefElem *restart_value = NULL;
|
1997-09-08 04:41:22 +02:00
|
|
|
DefElem *increment_by = NULL;
|
|
|
|
DefElem *max_value = NULL;
|
|
|
|
DefElem *min_value = NULL;
|
|
|
|
DefElem *cache_value = NULL;
|
2003-11-24 17:54:07 +01:00
|
|
|
DefElem *is_cycled = NULL;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *option;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-08-21 02:57:26 +02:00
|
|
|
*owned_by = NIL;
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
foreach(option, options)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
DefElem *defel = (DefElem *) lfirst(option);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
if (strcmp(defel->defname, "increment") == 0)
|
2003-02-13 06:25:24 +01:00
|
|
|
{
|
|
|
|
if (increment_by)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
1997-09-07 07:04:48 +02:00
|
|
|
increment_by = defel;
|
2003-02-13 06:25:24 +01:00
|
|
|
}
|
2008-05-17 01:36:05 +02:00
|
|
|
else if (strcmp(defel->defname, "start") == 0)
|
|
|
|
{
|
2008-05-17 03:20:39 +02:00
|
|
|
if (start_value)
|
2008-05-17 01:36:05 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2008-05-17 03:20:39 +02:00
|
|
|
start_value = defel;
|
2008-05-17 01:36:05 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(defel->defname, "restart") == 0)
|
2003-02-13 06:25:24 +01:00
|
|
|
{
|
2008-05-17 03:20:39 +02:00
|
|
|
if (restart_value)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2008-05-17 03:20:39 +02:00
|
|
|
restart_value = defel;
|
2003-02-13 06:25:24 +01:00
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
else if (strcmp(defel->defname, "maxvalue") == 0)
|
2003-02-13 06:25:24 +01:00
|
|
|
{
|
|
|
|
if (max_value)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
1997-09-07 07:04:48 +02:00
|
|
|
max_value = defel;
|
2003-02-13 06:25:24 +01:00
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
else if (strcmp(defel->defname, "minvalue") == 0)
|
2003-02-13 06:25:24 +01:00
|
|
|
{
|
|
|
|
if (min_value)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
1997-09-07 07:04:48 +02:00
|
|
|
min_value = defel;
|
2003-02-13 06:25:24 +01:00
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
else if (strcmp(defel->defname, "cache") == 0)
|
2003-02-13 06:25:24 +01:00
|
|
|
{
|
|
|
|
if (cache_value)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
1997-09-07 07:04:48 +02:00
|
|
|
cache_value = defel;
|
2003-02-13 06:25:24 +01:00
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
else if (strcmp(defel->defname, "cycle") == 0)
|
2003-02-13 06:25:24 +01:00
|
|
|
{
|
2003-11-24 17:54:07 +01:00
|
|
|
if (is_cycled)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2003-11-24 17:54:07 +01:00
|
|
|
is_cycled = defel;
|
2003-02-13 06:25:24 +01:00
|
|
|
}
|
2006-08-21 02:57:26 +02:00
|
|
|
else if (strcmp(defel->defname, "owned_by") == 0)
|
|
|
|
{
|
|
|
|
if (*owned_by)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
|
|
|
*owned_by = defGetQualifiedName(defel);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "option \"%s\" not recognized",
|
1997-09-07 07:04:48 +02:00
|
|
|
defel->defname);
|
|
|
|
}
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
/* INCREMENT BY */
|
2004-01-07 19:56:30 +01:00
|
|
|
if (increment_by != NULL)
|
2003-03-20 08:02:11 +01:00
|
|
|
{
|
|
|
|
new->increment_by = defGetInt64(increment_by);
|
2003-07-20 23:56:35 +02:00
|
|
|
if (new->increment_by == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("INCREMENT must not be zero")));
|
2003-03-20 08:02:11 +01:00
|
|
|
}
|
2003-11-24 17:54:07 +01:00
|
|
|
else if (isInit)
|
|
|
|
new->increment_by = 1;
|
|
|
|
|
|
|
|
/* CYCLE */
|
2004-01-07 19:56:30 +01:00
|
|
|
if (is_cycled != NULL)
|
2003-11-24 17:54:07 +01:00
|
|
|
{
|
|
|
|
new->is_cycled = intVal(is_cycled->arg);
|
2010-11-15 03:03:48 +01:00
|
|
|
Assert(BoolIsValid(new->is_cycled));
|
2003-11-24 17:54:07 +01:00
|
|
|
}
|
|
|
|
else if (isInit)
|
|
|
|
new->is_cycled = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-24 17:54:07 +01:00
|
|
|
/* MAXVALUE (null arg means NO MAXVALUE) */
|
2004-01-07 19:56:30 +01:00
|
|
|
if (max_value != NULL && max_value->arg)
|
2003-11-24 17:54:07 +01:00
|
|
|
new->max_value = defGetInt64(max_value);
|
2004-01-07 19:56:30 +01:00
|
|
|
else if (isInit || max_value != NULL)
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new->increment_by > 0)
|
2003-08-04 02:43:34 +02:00
|
|
|
new->max_value = SEQ_MAXVALUE; /* ascending seq */
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2004-08-29 07:07:03 +02:00
|
|
|
new->max_value = -1; /* descending seq */
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
}
|
1997-04-02 05:51:23 +02:00
|
|
|
|
2003-11-24 17:54:07 +01:00
|
|
|
/* MINVALUE (null arg means NO MINVALUE) */
|
2004-01-07 19:56:30 +01:00
|
|
|
if (min_value != NULL && min_value->arg)
|
2003-11-24 17:54:07 +01:00
|
|
|
new->min_value = defGetInt64(min_value);
|
2004-01-07 19:56:30 +01:00
|
|
|
else if (isInit || min_value != NULL)
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new->increment_by > 0)
|
2004-08-29 07:07:03 +02:00
|
|
|
new->min_value = 1; /* ascending seq */
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2003-08-04 02:43:34 +02:00
|
|
|
new->min_value = SEQ_MINVALUE; /* descending seq */
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-24 17:54:07 +01:00
|
|
|
/* crosscheck min/max */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new->min_value >= new->max_value)
|
2002-09-03 20:50:54 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char bufm[100],
|
|
|
|
bufx[100];
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->min_value);
|
|
|
|
snprintf(bufx, sizeof(bufx), INT64_FORMAT, new->max_value);
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("MINVALUE (%s) must be less than MAXVALUE (%s)",
|
|
|
|
bufm, bufx)));
|
2002-09-03 20:50:54 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2008-05-17 03:20:39 +02:00
|
|
|
/* START WITH */
|
|
|
|
if (start_value != NULL)
|
|
|
|
new->start_value = defGetInt64(start_value);
|
2003-11-24 17:54:07 +01:00
|
|
|
else if (isInit)
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new->increment_by > 0)
|
2008-05-17 01:36:05 +02:00
|
|
|
new->start_value = new->min_value; /* ascending seq */
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2008-05-17 01:36:05 +02:00
|
|
|
new->start_value = new->max_value; /* descending seq */
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2008-05-17 01:36:05 +02:00
|
|
|
/* crosscheck START */
|
|
|
|
if (new->start_value < new->min_value)
|
|
|
|
{
|
|
|
|
char bufs[100],
|
|
|
|
bufm[100];
|
|
|
|
|
|
|
|
snprintf(bufs, sizeof(bufs), INT64_FORMAT, new->start_value);
|
|
|
|
snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->min_value);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("START value (%s) cannot be less than MINVALUE (%s)",
|
|
|
|
bufs, bufm)));
|
|
|
|
}
|
|
|
|
if (new->start_value > new->max_value)
|
|
|
|
{
|
|
|
|
char bufs[100],
|
|
|
|
bufm[100];
|
|
|
|
|
|
|
|
snprintf(bufs, sizeof(bufs), INT64_FORMAT, new->start_value);
|
|
|
|
snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->max_value);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("START value (%s) cannot be greater than MAXVALUE (%s)",
|
|
|
|
bufs, bufm)));
|
|
|
|
}
|
|
|
|
|
2008-05-17 03:20:39 +02:00
|
|
|
/* RESTART [WITH] */
|
|
|
|
if (restart_value != NULL)
|
|
|
|
{
|
|
|
|
if (restart_value->arg != NULL)
|
|
|
|
new->last_value = defGetInt64(restart_value);
|
|
|
|
else
|
|
|
|
new->last_value = new->start_value;
|
|
|
|
new->is_called = false;
|
|
|
|
new->log_cnt = 1;
|
|
|
|
}
|
|
|
|
else if (isInit)
|
|
|
|
{
|
|
|
|
new->last_value = new->start_value;
|
|
|
|
new->is_called = false;
|
|
|
|
new->log_cnt = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* crosscheck RESTART (or current value, if changing MIN/MAX) */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new->last_value < new->min_value)
|
2002-09-03 20:50:54 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char bufs[100],
|
|
|
|
bufm[100];
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
snprintf(bufs, sizeof(bufs), INT64_FORMAT, new->last_value);
|
|
|
|
snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->min_value);
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2009-06-11 16:49:15 +02:00
|
|
|
errmsg("RESTART value (%s) cannot be less than MINVALUE (%s)",
|
|
|
|
bufs, bufm)));
|
2002-09-03 20:50:54 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new->last_value > new->max_value)
|
2002-09-03 20:50:54 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char bufs[100],
|
|
|
|
bufm[100];
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
snprintf(bufs, sizeof(bufs), INT64_FORMAT, new->last_value);
|
|
|
|
snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->max_value);
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2009-06-11 16:49:15 +02:00
|
|
|
errmsg("RESTART value (%s) cannot be greater than MAXVALUE (%s)",
|
|
|
|
bufs, bufm)));
|
2002-09-03 20:50:54 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
/* CACHE */
|
2004-01-07 19:56:30 +01:00
|
|
|
if (cache_value != NULL)
|
2002-09-03 20:50:54 +02:00
|
|
|
{
|
2003-11-24 17:54:07 +01:00
|
|
|
new->cache_value = defGetInt64(cache_value);
|
|
|
|
if (new->cache_value <= 0)
|
|
|
|
{
|
|
|
|
char buf[100];
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2003-11-24 17:54:07 +01:00
|
|
|
snprintf(buf, sizeof(buf), INT64_FORMAT, new->cache_value);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("CACHE (%s) must be greater than zero",
|
|
|
|
buf)));
|
|
|
|
}
|
2002-09-03 20:50:54 +02:00
|
|
|
}
|
2003-11-24 17:54:07 +01:00
|
|
|
else if (isInit)
|
|
|
|
new->cache_value = 1;
|
1997-04-02 05:51:23 +02:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:57:26 +02:00
|
|
|
/*
|
|
|
|
* Process an OWNED BY option for CREATE/ALTER SEQUENCE
|
|
|
|
*
|
|
|
|
* Ownership permissions on the sequence are already checked,
|
|
|
|
* but if we are establishing a new owned-by dependency, we must
|
|
|
|
* enforce that the referenced table has the same owner and namespace
|
|
|
|
* as the sequence.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
process_owned_by(Relation seqrel, List *owned_by)
|
|
|
|
{
|
|
|
|
int nnames;
|
|
|
|
Relation tablerel;
|
|
|
|
AttrNumber attnum;
|
|
|
|
|
|
|
|
nnames = list_length(owned_by);
|
|
|
|
Assert(nnames > 0);
|
|
|
|
if (nnames == 1)
|
|
|
|
{
|
|
|
|
/* Must be OWNED BY NONE */
|
|
|
|
if (strcmp(strVal(linitial(owned_by)), "none") != 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("invalid OWNED BY option"),
|
2006-10-04 02:30:14 +02:00
|
|
|
errhint("Specify OWNED BY table.column or OWNED BY NONE.")));
|
2006-08-21 02:57:26 +02:00
|
|
|
tablerel = NULL;
|
|
|
|
attnum = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
List *relname;
|
|
|
|
char *attrname;
|
|
|
|
RangeVar *rel;
|
|
|
|
|
|
|
|
/* Separate relname and attr name */
|
|
|
|
relname = list_truncate(list_copy(owned_by), nnames - 1);
|
|
|
|
attrname = strVal(lfirst(list_tail(owned_by)));
|
|
|
|
|
|
|
|
/* Open and lock rel to ensure it won't go away meanwhile */
|
|
|
|
rel = makeRangeVarFromNameList(relname);
|
|
|
|
tablerel = relation_openrv(rel, AccessShareLock);
|
|
|
|
|
|
|
|
/* Must be a regular table */
|
|
|
|
if (tablerel->rd_rel->relkind != RELKIND_RELATION)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("referenced relation \"%s\" is not a table",
|
|
|
|
RelationGetRelationName(tablerel))));
|
|
|
|
|
|
|
|
/* We insist on same owner and schema */
|
|
|
|
if (seqrel->rd_rel->relowner != tablerel->rd_rel->relowner)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2007-11-15 22:14:46 +01:00
|
|
|
errmsg("sequence must have same owner as table it is linked to")));
|
2006-08-21 02:57:26 +02:00
|
|
|
if (RelationGetNamespace(seqrel) != RelationGetNamespace(tablerel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2006-10-06 19:14:01 +02:00
|
|
|
errmsg("sequence must be in same schema as table it is linked to")));
|
2006-08-21 02:57:26 +02:00
|
|
|
|
|
|
|
/* Now, fetch the attribute number from the system cache */
|
|
|
|
attnum = get_attnum(RelationGetRelid(tablerel), attrname);
|
|
|
|
if (attnum == InvalidAttrNumber)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
|
|
|
errmsg("column \"%s\" of relation \"%s\" does not exist",
|
|
|
|
attrname, RelationGetRelationName(tablerel))));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* OK, we are ready to update pg_depend. First remove any existing AUTO
|
|
|
|
* dependencies for the sequence, then optionally add a new one.
|
2006-08-21 02:57:26 +02:00
|
|
|
*/
|
|
|
|
markSequenceUnowned(RelationGetRelid(seqrel));
|
|
|
|
|
|
|
|
if (tablerel)
|
|
|
|
{
|
|
|
|
ObjectAddress refobject,
|
|
|
|
depobject;
|
|
|
|
|
|
|
|
refobject.classId = RelationRelationId;
|
|
|
|
refobject.objectId = RelationGetRelid(tablerel);
|
|
|
|
refobject.objectSubId = attnum;
|
|
|
|
depobject.classId = RelationRelationId;
|
|
|
|
depobject.objectId = RelationGetRelid(seqrel);
|
|
|
|
depobject.objectSubId = 0;
|
|
|
|
recordDependencyOn(&depobject, &refobject, DEPENDENCY_AUTO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done, but hold lock until commit */
|
|
|
|
if (tablerel)
|
|
|
|
relation_close(tablerel, NoLock);
|
|
|
|
}
|
|
|
|
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2011-01-02 14:08:08 +01:00
|
|
|
/*
|
|
|
|
* Return sequence parameters, for use by information schema
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_sequence_parameters(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
Datum values[5];
|
|
|
|
bool isnull[5];
|
|
|
|
SeqTable elm;
|
|
|
|
Relation seqrel;
|
|
|
|
Buffer buf;
|
|
|
|
Form_pg_sequence seq;
|
|
|
|
|
|
|
|
/* open and AccessShareLock sequence */
|
|
|
|
init_sequence(relid, &elm, &seqrel);
|
|
|
|
|
|
|
|
if (pg_class_aclcheck(relid, GetUserId(), ACL_SELECT | ACL_UPDATE | ACL_USAGE) != ACLCHECK_OK)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for sequence %s",
|
|
|
|
RelationGetRelationName(seqrel))));
|
|
|
|
|
|
|
|
tupdesc = CreateTemplateTupleDesc(5, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "start_value", INT8OID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "minimum_value", INT8OID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "maximum_value", INT8OID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "increment", INT8OID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "cycle_option", BOOLOID, -1, 0);
|
|
|
|
|
|
|
|
BlessTupleDesc(tupdesc);
|
|
|
|
|
|
|
|
memset(isnull, 0, sizeof(isnull));
|
|
|
|
|
|
|
|
seq = read_info(elm, seqrel, &buf);
|
|
|
|
|
|
|
|
values[0] = Int64GetDatum(seq->start_value);
|
|
|
|
values[1] = Int64GetDatum(seq->min_value);
|
|
|
|
values[2] = Int64GetDatum(seq->max_value);
|
|
|
|
values[3] = Int64GetDatum(seq->increment_by);
|
|
|
|
values[4] = BoolGetDatum(seq->is_cycled);
|
|
|
|
|
|
|
|
UnlockReleaseBuffer(buf);
|
|
|
|
relation_close(seqrel, NoLock);
|
|
|
|
|
|
|
|
return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
void
|
|
|
|
seq_redo(XLogRecPtr lsn, XLogRecord *record)
|
2000-11-30 02:47:33 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
uint8 info = record->xl_info & ~XLR_INFO_MASK;
|
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
char *item;
|
|
|
|
Size itemsz;
|
|
|
|
xl_seq_rec *xlrec = (xl_seq_rec *) XLogRecGetData(record);
|
2000-12-28 14:00:29 +01:00
|
|
|
sequence_magic *sm;
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2009-01-20 19:59:37 +01:00
|
|
|
/* Backup blocks are not used in seq records */
|
|
|
|
Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
|
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
if (info != XLOG_SEQ_LOG)
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(PANIC, "seq_redo: unknown op code %u", info);
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
buffer = XLogReadBuffer(xlrec->node, 0, true);
|
2006-03-29 23:17:39 +02:00
|
|
|
Assert(BufferIsValid(buffer));
|
2000-11-30 02:47:33 +01:00
|
|
|
page = (Page) BufferGetPage(buffer);
|
|
|
|
|
2002-01-11 19:16:04 +01:00
|
|
|
/* Always reinit the page and reinstall the magic number */
|
|
|
|
/* See comments in DefineSequence */
|
2000-12-28 14:00:29 +01:00
|
|
|
PageInit((Page) page, BufferGetPageSize(buffer), sizeof(sequence_magic));
|
|
|
|
sm = (sequence_magic *) PageGetSpecialPointer(page);
|
|
|
|
sm->magic = SEQ_MAGIC;
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
item = (char *) xlrec + sizeof(xl_seq_rec);
|
2000-12-28 14:00:29 +01:00
|
|
|
itemsz = record->xl_len - sizeof(xl_seq_rec);
|
|
|
|
itemsz = MAXALIGN(itemsz);
|
2001-03-22 05:01:46 +01:00
|
|
|
if (PageAddItem(page, (Item) item, itemsz,
|
2007-09-20 19:56:33 +02:00
|
|
|
FirstOffsetNumber, false, false) == InvalidOffsetNumber)
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(PANIC, "seq_redo: failed to add item to page");
|
2000-11-30 02:47:33 +01:00
|
|
|
|
|
|
|
PageSetLSN(page, lsn);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buffer);
|
|
|
|
UnlockReleaseBuffer(buffer);
|
2000-11-30 02:47:33 +01:00
|
|
|
}
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
void
|
2006-03-24 05:32:13 +01:00
|
|
|
seq_desc(StringInfo buf, uint8 xl_info, char *rec)
|
2000-11-30 02:47:33 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
uint8 info = xl_info & ~XLR_INFO_MASK;
|
|
|
|
xl_seq_rec *xlrec = (xl_seq_rec *) rec;
|
2000-11-30 02:47:33 +01:00
|
|
|
|
|
|
|
if (info == XLOG_SEQ_LOG)
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(buf, "log: ");
|
2000-11-30 02:47:33 +01:00
|
|
|
else
|
|
|
|
{
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(buf, "UNKNOWN");
|
2000-11-30 02:47:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(buf, "rel %u/%u/%u",
|
2006-10-04 02:30:14 +02:00
|
|
|
xlrec->node.spcNode, xlrec->node.dbNode, xlrec->node.relNode);
|
2000-11-30 02:47:33 +01:00
|
|
|
}
|