Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b44e46c

Browse files
committed
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...
1 parent ce17484 commit b44e46c

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

doc/src/sgml/ref/create_sequence.sgml

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v 1.14 2000/10/05 19:48:17 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v 1.15 2000/12/08 20:06:58 tgl Exp $
33
Postgres documentation
44
-->
55

@@ -77,9 +77,9 @@ CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable> [ INCREMENT
7777
<term><replaceable class="parameter">maxvalue</replaceable></term>
7878
<listitem>
7979
<para>
80-
Use the optional clause <option>MAXVALUE
81-
<replaceable class="parameter">maxvalue</replaceable></option> to
82-
determine the maximum
80+
The optional clause <option>MAXVALUE
81+
<replaceable class="parameter">maxvalue</replaceable></option>
82+
determines the maximum
8383
value for the sequence. The defaults are 2147483647 and -1 for
8484
ascending and descending sequences, respectively.
8585
</para>
@@ -120,15 +120,15 @@ CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable> [ INCREMENT
120120
<listitem>
121121
<para>
122122
The optional CYCLE keyword may be used to enable the sequence
123-
to continue when the
123+
to wrap around when the
124124
<replaceable class="parameter">maxvalue</replaceable> or
125125
<replaceable class="parameter">minvalue</replaceable> has been
126126
reached by
127127
an ascending or descending sequence respectively. If the limit is
128-
reached, the next number generated will be whatever the
128+
reached, the next number generated will be the
129129
<replaceable class="parameter">minvalue</replaceable> or
130-
<replaceable class="parameter">maxvalue</replaceable> is,
131-
as appropriate.
130+
<replaceable class="parameter">maxvalue</replaceable>,
131+
respectively.
132132
</para>
133133
</listitem>
134134
</varlistentry>
@@ -192,7 +192,7 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl
192192
</computeroutput></term>
193193
<listitem>
194194
<para>
195-
If the minimum and maximum values are inconsistant.
195+
If the minimum and maximum values are inconsistent.
196196
</para>
197197
</listitem>
198198
</varlistentry>
@@ -213,24 +213,24 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl
213213
into the current data base. This involves creating and initializing a
214214
new single-row
215215
table with the name <replaceable class="parameter">seqname</replaceable>.
216-
The generator will be "owned" by the user issuing the command.
216+
The generator will be owned by the user issuing the command.
217217
</para>
218218

219219
<para>
220220
After a sequence is created, you may use the function
221-
<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
221+
<function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
222222
to get a new number from the sequence.
223223
The function
224224
<function>currval('<replaceable class="parameter">seqname</replaceable>')</function>
225225
may be used to determine the number returned by the last call to
226-
<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
226+
<function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
227227
for the specified sequence in the current session.
228228
The function
229229
<function>setval('<replaceable class="parameter">seqname</replaceable>',
230230
<replaceable class="parameter">newvalue</replaceable>)</function>
231231
may be used to set the current value of the specified sequence.
232232
The next call to
233-
<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
233+
<function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
234234
will return the given value plus the sequence increment.
235235
</para>
236236

@@ -241,7 +241,7 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl
241241
SELECT * FROM <replaceable>seqname</replaceable>;
242242
</programlisting>
243243

244-
to get the parameters of a sequence.
244+
to examine the parameters of a sequence.
245245

246246
As an alternative to fetching the
247247
parameters from the original definition as above, you can use
@@ -254,8 +254,13 @@ SELECT last_value FROM <replaceable>seqname</replaceable>;
254254
</para>
255255

256256
<para>
257-
Low-level locking is used to enable multiple simultaneous
258-
calls to a generator.
257+
To avoid blocking of concurrent transactions
258+
that obtain numbers from the same sequence, a nextval operation
259+
is never rolled back; that is, once a value has been fetched it is
260+
considered used, even if the transaction that did the nextval later
261+
aborts. This means that aborted transactions may leave unused "holes"
262+
in the sequence of assigned values. setval operations are never
263+
rolled back, either.
259264
</para>
260265

261266
<caution>
@@ -279,6 +284,9 @@ SELECT last_value FROM <replaceable>seqname</replaceable>;
279284
are all distinct, not that they are generated purely sequentially.
280285
Also, last_value will reflect the latest value reserved by any backend,
281286
whether or not it has yet been returned by nextval.
287+
Another consideration is that a setval executed on such a sequence
288+
will not be noticed by other backends until they have used up any
289+
preallocated values they have cached.
282290
</para>
283291
</caution>
284292

@@ -293,7 +301,7 @@ SELECT last_value FROM <replaceable>seqname</replaceable>;
293301
Use <command>DROP SEQUENCE</command> to remove a sequence.
294302
</para>
295303
<para>
296-
Each backend uses its own cache to store allocated numbers.
304+
Each backend uses its own cache to store preallocated numbers.
297305
Numbers that are cached but not used in the current session will be
298306
lost, resulting in "holes" in the sequence.
299307
</para>

src/backend/commands/sequence.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ nextval(PG_FUNCTION_ARGS)
257257
if (rescnt > 0)
258258
break; /* stop fetching */
259259
if (seq->is_cycled != 't')
260-
elog(ERROR, "%s.nextval: got MAXVALUE (%d)",
260+
elog(ERROR, "%s.nextval: reached MAXVALUE (%d)",
261261
elm->name, maxv);
262262
next = minv;
263263
}
@@ -273,7 +273,7 @@ nextval(PG_FUNCTION_ARGS)
273273
if (rescnt > 0)
274274
break; /* stop fetching */
275275
if (seq->is_cycled != 't')
276-
elog(ERROR, "%s.nextval: got MINVALUE (%d)",
276+
elog(ERROR, "%s.nextval: reached MINVALUE (%d)",
277277
elm->name, minv);
278278
next = maxv;
279279
}
@@ -371,21 +371,13 @@ do_setval(char *seqname, int32 next, bool iscalled)
371371
seq = read_info("setval", elm, &buf); /* lock page' buffer and
372372
* read tuple */
373373

374-
if (seq->cache_value != 1)
375-
{
376-
elog(ERROR, "%s.setval: can't set value of sequence %s, cache != 1",
377-
seqname, seqname);
378-
}
379-
380374
if ((next < seq->min_value) || (next > seq->max_value))
381-
{
382-
elog(ERROR, "%s.setval: value %d is of of bounds (%d,%d)",
375+
elog(ERROR, "%s.setval: value %d is out of bounds (%d,%d)",
383376
seqname, next, seq->min_value, seq->max_value);
384-
}
385377

386378
/* save info in local cache */
387379
elm->last = next; /* last returned number */
388-
elm->cached = next; /* last cached number */
380+
elm->cached = next; /* last cached number (forget cached values) */
389381

390382
/* save info in sequence relation */
391383
START_CRIT_CODE;
@@ -540,7 +532,7 @@ init_sequence(char *caller, char *name)
540532
/* Else open and check it */
541533
seqrel = heap_openr(name, AccessShareLock);
542534
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
543-
elog(ERROR, "%s.%s: %s is not sequence !", name, caller, name);
535+
elog(ERROR, "%s.%s: %s is not a sequence", name, caller, name);
544536

545537
if (elm != (SeqTable) NULL)
546538
{
@@ -704,7 +696,7 @@ get_param(DefElem *def)
704696
if (nodeTag(def->arg) == T_Integer)
705697
return intVal(def->arg);
706698

707-
elog(ERROR, "DefineSequence: \"%s\" is to be integer", def->defname);
699+
elog(ERROR, "DefineSequence: \"%s\" value must be integer", def->defname);
708700
return -1;
709701
}
710702

0 commit comments

Comments
 (0)