diff options
author | Marc G. Fournier <scrappy@hub.org> | 1998-08-25 21:25:46 +0000 |
---|---|---|
committer | Marc G. Fournier <scrappy@hub.org> | 1998-08-25 21:25:46 +0000 |
commit | 8e9d69d6ac76001458f0c190a35990b23abf81de (patch) | |
tree | f1b2ec4d5991f2963169b8964efcadc21d41bfd0 /src/backend/commands/sequence.c | |
parent | 88b17d9c56bfd0b6d35b824e9edbce29ae241c33 (diff) | |
download | postgresql-8e9d69d6ac76001458f0c190a35990b23abf81de.tar.gz postgresql-8e9d69d6ac76001458f0c190a35990b23abf81de.zip |
From: Massimo Dal Zotto <dz@cs.unitn.it>
> sequence.patch
>
> adds the missing setval command to sequences. Owner of sequences
> can now set the last value to any value between min and max
> without recreating the sequence. This is useful after loading
> data from external files.
Diffstat (limited to 'src/backend/commands/sequence.c')
-rw-r--r-- | src/backend/commands/sequence.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index f28ec52b009..20f9bef503a 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -9,6 +9,7 @@ #include <string.h> #include <postgres.h> +#include <miscadmin.h> #include <storage/bufmgr.h> #include <storage/bufpage.h> @@ -18,6 +19,7 @@ #include <commands/creatinh.h> #include <commands/sequence.h> #include <utils/builtins.h> +#include <utils/acl.h> #define SEQ_MAGIC 0x1717 @@ -311,6 +313,52 @@ currval(struct varlena * seqin) } +int4 +setval(struct varlena * seqin, int4 next) +{ + char *seqname = textout(seqin); + SeqTable elm; + Buffer buf; + SequenceTupleForm seq; + ItemPointerData iptr; + +#ifndef NO_SECURITY + if (pg_aclcheck(seqname, getpgusername(), ACL_WR) != ACLCHECK_OK) + elog(ERROR, "%s.setval: you don't have permissions to set sequence %s", + seqname, seqname); +#endif + + /* open and WIntentLock sequence */ + elm = init_sequence ("setval", seqname); + seq = read_info ("setval", elm, &buf); /* lock page and read tuple */ + + if ( seq->cache_value != 1 ) { + elog (ERROR, "%s.setval: can't set value of sequence %s, cache != 1", + seqname, seqname); + } + + if ((next < seq->min_value) || (next > seq->max_value)) { + elog (ERROR, "%s.setval: value %d is of of bounds (%d,%d)", + seqname, next, seq->min_value, seq->max_value); + } + + /* save info in local cache */ + elm->last = next; /* last returned number */ + elm->cached = next; /* last cached number */ + + /* save info in sequence relation */ + seq->last_value = next; /* last fetched number */ + seq->is_called = 't'; + + if ( WriteBuffer (buf) == STATUS_ERROR ) + elog (ERROR, "%s.settval: WriteBuffer failed", seqname); + + ItemPointerSet(&iptr, 0, FirstOffsetNumber); + RelationUnsetSingleWLockPage (elm->rel, &iptr); + + return (next); +} + static SequenceTupleForm read_info(char *caller, SeqTable elm, Buffer *buf) { |