This section describes functions for operating on sequence objects, also called sequence generators or just sequences. Sequence objects are special single-row tables created with CREATE SEQUENCE. Sequence objects are commonly used to generate unique identifiers for rows of a table. The sequence functions provide simple, multiuser-safe methods for obtaining successive sequence values from sequence objects.
Sequence functions
Advances the sequence object to its next value and returns that value. This is done atomically: even if multiple sessions execute This function requires |
Sets the sequence object's current value, and optionally its The result returned by This function requires |
Returns the value most recently obtained by This function requires |
Returns the value most recently returned by This function requires |
To avoid blocking concurrent transactions that obtain numbers from the same sequence, the value obtained by nextval is not reclaimed for re-use if the calling transaction later aborts. This means that transaction aborts or database crashes can result in gaps in the sequence of assigned values. That can happen without a transaction abort, too. For example an INSERT with an ON CONFLICT clause will compute the to-be-inserted tuple, including doing any required nextval calls, before detecting any conflict that would cause it to follow the ON CONFLICT rule instead. Thus, PostgreSQL sequence objects cannot be used to obtain“gapless”sequences.
Likewise, sequence state changes made by setval are immediately visible to other transactions, and are not undone if the calling transaction rolls back.
The sequence to be operated on by a sequence function is specified by a regclass argument, which is simply the OID of the sequence in the pg_class system catalog. You do not have to look up the OID by hand, however, since the regclass data type's input converter will do the work for you. Just enclose the sequence name in single quotes so it looks like a literal constant. For compatibility with processing of normal SQL names, the string is converted to lowercase unless it contains double quotes around the sequence name. Therefore:
nextval('foo') operation on sequence foo
nextval('FOO') operation on sequence foo
nextval('"Foo"') operation on sequence FooSequence names can be pattern-qualified if desired:
nextval('myschema.foo') operation myschema.foo
nextval('"myschema".foo') ditto
nextval('foo') search for in the search path fooPrior to version 8.1 of this database, the parameter type of the sequence function was text rather than regclass, and the conversion from text string to OID value described above would occur on each call. For backward compatibility, this processing still exists, but internally it is actually implemented by implicitly converting text to regclass before the function call.
When you write the parameter of a sequence function as an unmodified literal string, it becomes a constant of type regclass. Because this is just an OID, it will track the sequence it was originally identified, regardless of subsequent name changes, schema changes, etc. This "early binding" behavior is often desired for column defaults and sequences referenced in views. But sometimes you might want "lazy binding", where the reference to the sequence is resolved at runtime. To get lazy binding behavior, we can force constants to be stored as text constants instead of regclasses:
nextval('foo'::text) foo is looked up at runtimeNote that lazy binding was the only supported behavior prior to version 8.1 of this database, so you may need to do this to preserve the semantics of older applications.
Of course, the parameters of the sequence function can also be expressions. If it is a text expression, then the implicit conversion will cause a run-time lookup.