Skip to content

Commit 691d99d

Browse files
committed
Docs: improve warnings about nextval() not producing gapless sequences.
In the documentation for nextval(), point out explicitly that INSERT ... ON CONFLICT will call nextval() if needed for the insertion case, whether or not it ends up following the ON CONFLICT path. This seems to be a matter of some confusion, cf bug #14126, so let's be clear about it. Also mention the issue in the CREATE SEQUENCE reference page, since that is another place where people might expect such things to be covered. Minor wording improvements nearby, as well. Back-patch to 9.5 where ON CONFLICT was introduced.
1 parent 7dc1d35 commit 691d99d

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

doc/src/sgml/func.sgml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11512,13 +11512,19 @@ nextval('foo'::text) <lineannotation><literal>foo</literal> is looked up at
1151211512

1151311513
<important>
1151411514
<para>
11515-
To avoid blocking concurrent transactions that obtain numbers from the
11516-
same sequence, a <function>nextval</function> operation is never
11515+
To avoid blocking concurrent transactions that obtain numbers from
11516+
the same sequence, a <function>nextval</function> operation is never
1151711517
rolled back; that is, once a value has been fetched it is considered
11518-
used, even if the transaction that did the
11519-
<function>nextval</function> later aborts. This means that aborted
11520-
transactions might leave unused <quote>holes</quote> in the sequence
11521-
of assigned values.
11518+
used and will not be returned again. This is true even if the
11519+
surrounding transaction later aborts, or if the calling query ends
11520+
up not using the value. For example an <command>INSERT</> with
11521+
an <literal>ON CONFLICT</> clause will compute the to-be-inserted
11522+
tuple, including doing any required <function>nextval</function>
11523+
calls, before detecting any conflict that would cause it to follow
11524+
the <literal>ON CONFLICT</> rule instead. Such cases will leave
11525+
unused <quote>holes</quote> in the sequence of assigned values.
11526+
Thus, <productname>PostgreSQL</> sequence objects <emphasis>cannot
11527+
be used to obtain <quote>gapless</> sequences</emphasis>.
1152211528
</para>
1152311529
</important>
1152411530

@@ -11547,8 +11553,8 @@ nextval('foo'::text) <lineannotation><literal>foo</literal> is looked up at
1154711553
Return the value most recently returned by
1154811554
<function>nextval</> in the current session. This function is
1154911555
identical to <function>currval</function>, except that instead
11550-
of taking the sequence name as an argument it fetches the
11551-
value of the last sequence used by <function>nextval</function>
11556+
of taking the sequence name as an argument it refers to whichever
11557+
sequence <function>nextval</function> was most recently applied to
1155211558
in the current session. It is an error to call
1155311559
<function>lastval</function> if <function>nextval</function>
1155411560
has not yet been called in the current session.

doc/src/sgml/ref/create_sequence.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ SELECT * FROM <replaceable>name</replaceable>;
239239
(-9223372036854775808 to 9223372036854775807).
240240
</para>
241241

242+
<para>
243+
Because <function>nextval</> and <function>setval</> calls are never
244+
rolled back, sequence objects cannot be used if <quote>gapless</>
245+
assignment of sequence numbers is needed. It is possible to build
246+
gapless assignment by using exclusive locking of a table containing a
247+
counter; but this solution is much more expensive than sequence
248+
objects, especially if many transactions need sequence numbers
249+
concurrently.
250+
</para>
251+
242252
<para>
243253
Unexpected results might be obtained if a <replaceable
244254
class="parameter">cache</replaceable> setting greater than one is

0 commit comments

Comments
 (0)