Commit b44e46cf authored by Tom Lane's avatar Tom Lane

Remove error check that disallowed setval() on a sequence with cache

value greater than one.  The behavior this sought to disallow doesn't
seem any less confusing than the other behaviors of cached sequences.
Improve wording of some error messages, too.
Update documentation accordingly.  Also add an explanation that
aborted transactions do not roll back their nextval() calls; this
seems to be a FAQ, so it ought to be mentioned here...
parent ce174840
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v 1.14 2000/10/05 19:48:17 momjian Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v 1.15 2000/12/08 20:06:58 tgl Exp $
Postgres documentation Postgres documentation
--> -->
...@@ -77,9 +77,9 @@ CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable> [ INCREMENT ...@@ -77,9 +77,9 @@ CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable> [ INCREMENT
<term><replaceable class="parameter">maxvalue</replaceable></term> <term><replaceable class="parameter">maxvalue</replaceable></term>
<listitem> <listitem>
<para> <para>
Use the optional clause <option>MAXVALUE The optional clause <option>MAXVALUE
<replaceable class="parameter">maxvalue</replaceable></option> to <replaceable class="parameter">maxvalue</replaceable></option>
determine the maximum determines the maximum
value for the sequence. The defaults are 2147483647 and -1 for value for the sequence. The defaults are 2147483647 and -1 for
ascending and descending sequences, respectively. ascending and descending sequences, respectively.
</para> </para>
...@@ -120,15 +120,15 @@ CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable> [ INCREMENT ...@@ -120,15 +120,15 @@ CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable> [ INCREMENT
<listitem> <listitem>
<para> <para>
The optional CYCLE keyword may be used to enable the sequence The optional CYCLE keyword may be used to enable the sequence
to continue when the to wrap around when the
<replaceable class="parameter">maxvalue</replaceable> or <replaceable class="parameter">maxvalue</replaceable> or
<replaceable class="parameter">minvalue</replaceable> has been <replaceable class="parameter">minvalue</replaceable> has been
reached by reached by
an ascending or descending sequence respectively. If the limit is an ascending or descending sequence respectively. If the limit is
reached, the next number generated will be whatever the reached, the next number generated will be the
<replaceable class="parameter">minvalue</replaceable> or <replaceable class="parameter">minvalue</replaceable> or
<replaceable class="parameter">maxvalue</replaceable> is, <replaceable class="parameter">maxvalue</replaceable>,
as appropriate. respectively.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -192,7 +192,7 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl ...@@ -192,7 +192,7 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl
</computeroutput></term> </computeroutput></term>
<listitem> <listitem>
<para> <para>
If the minimum and maximum values are inconsistant. If the minimum and maximum values are inconsistent.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -213,24 +213,24 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl ...@@ -213,24 +213,24 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl
into the current data base. This involves creating and initializing a into the current data base. This involves creating and initializing a
new single-row new single-row
table with the name <replaceable class="parameter">seqname</replaceable>. table with the name <replaceable class="parameter">seqname</replaceable>.
The generator will be "owned" by the user issuing the command. The generator will be owned by the user issuing the command.
</para> </para>
<para> <para>
After a sequence is created, you may use the function After a sequence is created, you may use the function
<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function> <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
to get a new number from the sequence. to get a new number from the sequence.
The function The function
<function>currval('<replaceable class="parameter">seqname</replaceable>')</function> <function>currval('<replaceable class="parameter">seqname</replaceable>')</function>
may be used to determine the number returned by the last call to may be used to determine the number returned by the last call to
<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function> <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
for the specified sequence in the current session. for the specified sequence in the current session.
The function The function
<function>setval('<replaceable class="parameter">seqname</replaceable>', <function>setval('<replaceable class="parameter">seqname</replaceable>',
<replaceable class="parameter">newvalue</replaceable>)</function> <replaceable class="parameter">newvalue</replaceable>)</function>
may be used to set the current value of the specified sequence. may be used to set the current value of the specified sequence.
The next call to The next call to
<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function> <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
will return the given value plus the sequence increment. will return the given value plus the sequence increment.
</para> </para>
...@@ -241,7 +241,7 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl ...@@ -241,7 +241,7 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl
SELECT * FROM <replaceable>seqname</replaceable>; SELECT * FROM <replaceable>seqname</replaceable>;
</programlisting> </programlisting>
to get the parameters of a sequence. to examine the parameters of a sequence.
As an alternative to fetching the As an alternative to fetching the
parameters from the original definition as above, you can use parameters from the original definition as above, you can use
...@@ -254,8 +254,13 @@ SELECT last_value FROM <replaceable>seqname</replaceable>; ...@@ -254,8 +254,13 @@ SELECT last_value FROM <replaceable>seqname</replaceable>;
</para> </para>
<para> <para>
Low-level locking is used to enable multiple simultaneous To avoid blocking of concurrent transactions
calls to a generator. that obtain numbers from the same sequence, a nextval operation
is never rolled back; that is, once a value has been fetched it is
considered used, even if the transaction that did the nextval later
aborts. This means that aborted transactions may leave unused "holes"
in the sequence of assigned values. setval operations are never
rolled back, either.
</para> </para>
<caution> <caution>
...@@ -279,6 +284,9 @@ SELECT last_value FROM <replaceable>seqname</replaceable>; ...@@ -279,6 +284,9 @@ SELECT last_value FROM <replaceable>seqname</replaceable>;
are all distinct, not that they are generated purely sequentially. are all distinct, not that they are generated purely sequentially.
Also, last_value will reflect the latest value reserved by any backend, Also, last_value will reflect the latest value reserved by any backend,
whether or not it has yet been returned by nextval. whether or not it has yet been returned by nextval.
Another consideration is that a setval executed on such a sequence
will not be noticed by other backends until they have used up any
preallocated values they have cached.
</para> </para>
</caution> </caution>
...@@ -293,7 +301,7 @@ SELECT last_value FROM <replaceable>seqname</replaceable>; ...@@ -293,7 +301,7 @@ SELECT last_value FROM <replaceable>seqname</replaceable>;
Use <command>DROP SEQUENCE</command> to remove a sequence. Use <command>DROP SEQUENCE</command> to remove a sequence.
</para> </para>
<para> <para>
Each backend uses its own cache to store allocated numbers. Each backend uses its own cache to store preallocated numbers.
Numbers that are cached but not used in the current session will be Numbers that are cached but not used in the current session will be
lost, resulting in "holes" in the sequence. lost, resulting in "holes" in the sequence.
</para> </para>
......
...@@ -257,7 +257,7 @@ nextval(PG_FUNCTION_ARGS) ...@@ -257,7 +257,7 @@ nextval(PG_FUNCTION_ARGS)
if (rescnt > 0) if (rescnt > 0)
break; /* stop fetching */ break; /* stop fetching */
if (seq->is_cycled != 't') if (seq->is_cycled != 't')
elog(ERROR, "%s.nextval: got MAXVALUE (%d)", elog(ERROR, "%s.nextval: reached MAXVALUE (%d)",
elm->name, maxv); elm->name, maxv);
next = minv; next = minv;
} }
...@@ -273,7 +273,7 @@ nextval(PG_FUNCTION_ARGS) ...@@ -273,7 +273,7 @@ nextval(PG_FUNCTION_ARGS)
if (rescnt > 0) if (rescnt > 0)
break; /* stop fetching */ break; /* stop fetching */
if (seq->is_cycled != 't') if (seq->is_cycled != 't')
elog(ERROR, "%s.nextval: got MINVALUE (%d)", elog(ERROR, "%s.nextval: reached MINVALUE (%d)",
elm->name, minv); elm->name, minv);
next = maxv; next = maxv;
} }
...@@ -371,21 +371,13 @@ do_setval(char *seqname, int32 next, bool iscalled) ...@@ -371,21 +371,13 @@ do_setval(char *seqname, int32 next, bool iscalled)
seq = read_info("setval", elm, &buf); /* lock page' buffer and seq = read_info("setval", elm, &buf); /* lock page' buffer and
* read tuple */ * 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)) if ((next < seq->min_value) || (next > seq->max_value))
{ elog(ERROR, "%s.setval: value %d is out of bounds (%d,%d)",
elog(ERROR, "%s.setval: value %d is of of bounds (%d,%d)",
seqname, next, seq->min_value, seq->max_value); seqname, next, seq->min_value, seq->max_value);
}
/* save info in local cache */ /* save info in local cache */
elm->last = next; /* last returned number */ elm->last = next; /* last returned number */
elm->cached = next; /* last cached number */ elm->cached = next; /* last cached number (forget cached values) */
/* save info in sequence relation */ /* save info in sequence relation */
START_CRIT_CODE; START_CRIT_CODE;
...@@ -540,7 +532,7 @@ init_sequence(char *caller, char *name) ...@@ -540,7 +532,7 @@ init_sequence(char *caller, char *name)
/* Else open and check it */ /* Else open and check it */
seqrel = heap_openr(name, AccessShareLock); seqrel = heap_openr(name, AccessShareLock);
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE) if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
elog(ERROR, "%s.%s: %s is not sequence !", name, caller, name); elog(ERROR, "%s.%s: %s is not a sequence", name, caller, name);
if (elm != (SeqTable) NULL) if (elm != (SeqTable) NULL)
{ {
...@@ -704,7 +696,7 @@ get_param(DefElem *def) ...@@ -704,7 +696,7 @@ get_param(DefElem *def)
if (nodeTag(def->arg) == T_Integer) if (nodeTag(def->arg) == T_Integer)
return intVal(def->arg); return intVal(def->arg);
elog(ERROR, "DefineSequence: \"%s\" is to be integer", def->defname); elog(ERROR, "DefineSequence: \"%s\" value must be integer", def->defname);
return -1; return -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment