9.17. Sequence Manipulation Functions
This section describes functions for operating onsequence objects, also called sequence generators or just sequences. Sequence objects are special single-row tables created withCREATE SEQUENCE. Sequence objects are commonly used to generate unique identifiers for rows of a table. The sequence functions, listed inTable 9.50, provide simple, multiuser-safe methods for obtaining successive sequence values from sequence objects.
Table 9.50. Sequence Functions
Function Description |
---|
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 SELECT setval('myseq', 42);Next 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 |
Caution
To avoid blocking concurrent transactions that obtain numbers from the same sequence, the value obtained bynextval
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 anINSERT
with anON CONFLICT
clause will compute the to-be-inserted tuple, including doing any requirednextval
calls, before detecting any conflict that would cause it to follow theON CONFLICT
rule instead. Thus,Postgres Pro sequence objectscannot be used to obtain“gapless” sequences.
Likewise, sequence state changes made bysetval
are immediately visible to other transactions, and are not undone if the calling transaction rolls back.
If the database cluster crashes before committing a transaction containing anextval
orsetval
call, the sequence state change might not have made its way to persistent storage, so that it is uncertain whether the sequence will have its original or updated state after the cluster restarts. This is harmless for usage of the sequence within the database, since other effects of uncommitted transactions will not be visible either. However, if you wish to use a sequence value for persistent outside-the-database purposes, make sure that thenextval
call has been committed before doing so.
The sequence to be operated on by a sequence function is specified by a The sequence name can be schema-qualified if necessary: SeeSection 8.19 for more information about BeforePostgreSQL 8.1, the arguments of the sequence functions were of type When you write the argument of a sequence function as an unadorned literal string, it becomes a constant of type Note that late binding was the only behavior supported inPostgreSQL releases before 8.1, so you might need to do this to preserve the semantics of old applications. Of course, the argument of a sequence function can be an expression as well as a constant. If it is a text expression then the implicit coercion will result in a run-time lookup.regclass
argument, which is simply the OID of the sequence in thepg_class
system catalog. You do not have to look up the OID by hand, however, since theregclass
data type's input converter will do the work for you. Just write the sequence name enclosed in single quotes so that it looks like a literal constant. For compatibility with the handling of ordinarySQL names, the string will be converted to lower case unless it contains double quotes around the sequence name. Thus:nextval('foo')operates on sequence
foo
nextval('FOO')operates on sequencefoo
nextval('"Foo"')operates on sequenceFoo
nextval('myschema.foo')operates on
myschema.foo
nextval('"myschema".foo')same as abovenextval('foo')searches search path forfoo
regclass
.Note
text
, notregclass
, and the above-described conversion from a text string to an OID value would happen at run time during each call. For backward compatibility, this facility still exists, but internally it is now handled as an implicit coercion fromtext
toregclass
before the function is invoked.regclass
. Since this is really just an OID, it will track the originally identified sequence despite later renaming, schema reassignment, etc. This“early binding” behavior is usually desirable for sequence references in column defaults and views. But sometimes you might want“late binding” where the sequence reference is resolved at run time. To get late-binding behavior, force the constant to be stored as atext
constant instead ofregclass
:nextval('foo'::text)
foo
is looked up at runtime