Skip to content

Commit b0a7161

Browse files
committed
Doc: improve documentation about nextval()/setval().
Clarify that the results of nextval and setval are not guaranteed persistent until the calling transaction commits. Some people seem to have drawn the opposite conclusion from the statement that these functions are never rolled back, so re-word to avoid saying it quite that way. Discussion: https://postgr.es/m/CAKU4AWohO=NfM-4KiZWvdc+z3c1C9FrUBR6xnReFJ6sfy0i=Lw@mail.gmail.com
1 parent a00bd7a commit b0a7161

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

doc/src/sgml/func.sgml

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12397,24 +12397,6 @@ nextval('foo'::text) <lineannotation><literal>foo</literal> is looked up at
1239712397
see its command reference page for more information.
1239812398
</para>
1239912399

12400-
<important>
12401-
<para>
12402-
To avoid blocking concurrent transactions that obtain numbers from
12403-
the same sequence, a <function>nextval</function> operation is never
12404-
rolled back; that is, once a value has been fetched it is considered
12405-
used and will not be returned again. This is true even if the
12406-
surrounding transaction later aborts, or if the calling query ends
12407-
up not using the value. For example an <command>INSERT</command> with
12408-
an <literal>ON CONFLICT</literal> clause will compute the to-be-inserted
12409-
tuple, including doing any required <function>nextval</function>
12410-
calls, before detecting any conflict that would cause it to follow
12411-
the <literal>ON CONFLICT</literal> rule instead. Such cases will leave
12412-
unused <quote>holes</quote> in the sequence of assigned values.
12413-
Thus, <productname>PostgreSQL</productname> sequence objects <emphasis>cannot
12414-
be used to obtain <quote>gapless</quote> sequences</emphasis>.
12415-
</para>
12416-
</important>
12417-
1241812400
<para>
1241912401
This function requires <literal>USAGE</literal>
1242012402
or <literal>UPDATE</literal> privilege on the sequence.
@@ -12491,13 +12473,6 @@ SELECT setval('foo', 42, false); <lineannotation>Next <function>nextval</func
1249112473
The result returned by <function>setval</function> is just the value of its
1249212474
second argument.
1249312475
</para>
12494-
<important>
12495-
<para>
12496-
Because sequences are non-transactional, changes made by
12497-
<function>setval</function> are not undone if the transaction rolls
12498-
back.
12499-
</para>
12500-
</important>
1250112476

1250212477
<para>
1250312478
This function requires <literal>UPDATE</literal> privilege on the
@@ -12508,6 +12483,42 @@ SELECT setval('foo', 42, false); <lineannotation>Next <function>nextval</func
1250812483
</variablelist>
1250912484
</para>
1251012485

12486+
<caution>
12487+
<para>
12488+
To avoid blocking concurrent transactions that obtain numbers from
12489+
the same sequence, the value obtained by <function>nextval</function>
12490+
is not reclaimed for re-use if the calling transaction later aborts.
12491+
This means that transaction aborts or database crashes can result in
12492+
gaps in the sequence of assigned values. That can happen without a
12493+
transaction abort, too. For example an <command>INSERT</command> with
12494+
an <literal>ON CONFLICT</literal> clause will compute the to-be-inserted
12495+
tuple, including doing any required <function>nextval</function>
12496+
calls, before detecting any conflict that would cause it to follow
12497+
the <literal>ON CONFLICT</literal> rule instead.
12498+
Thus, <productname>PostgreSQL</productname> sequence
12499+
objects <emphasis>cannot be used to obtain <quote>gapless</quote>
12500+
sequences</emphasis>.
12501+
</para>
12502+
12503+
<para>
12504+
Likewise, sequence state changes made by <function>setval</function>
12505+
are immediately visible to other transactions, and are not undone if
12506+
the calling transaction rolls back.
12507+
</para>
12508+
12509+
<para>
12510+
If the database cluster crashes before committing a transaction
12511+
containing a <function>nextval</function>
12512+
or <function>setval</function> call, the sequence state change might
12513+
not have made its way to persistent storage, so that it is uncertain
12514+
whether the sequence will have its original or updated state after the
12515+
cluster restarts. This is harmless for usage of the sequence within
12516+
the database, since other effects of uncommitted transactions will not
12517+
be visible either. However, if you wish to use a sequence value for
12518+
persistent outside-the-database purposes, make sure that the
12519+
<function>nextval</function> call has been committed before doing so.
12520+
</para>
12521+
</caution>
1251112522
</sect1>
1251212523

1251312524

0 commit comments

Comments
 (0)