All Products
Search
Document Center

PolarDB:Sequence manipulation functions

Last Updated:Jul 31, 2024

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

nextval(regclass) →bigint

Advances the sequence object to its next value and returns that value. This is done atomically: even if multiple sessions execute nextval concurrently, each will safely receive a distinct sequence value. If the sequence object has been created with default parameters, successive nextval calls will return successive values beginning with 1. Other behaviors can be obtained by using appropriate parameters in the CREATE SEQUENCE command.

This function requires USAGE or UPDATE privilege on the sequence.

setval(regclass,bigint[,boolean] ) →bigint

Sets the sequence object's current value, and optionally its is_called flag. The two-parameter form sets the sequence's last_value field to the specified value and sets its is_called field to true, meaning that the next nextval will advance the sequence before returning a value. The value that will be reported by currval is also set to the specified value. In the three-parameter form,is_called can be set to either true or false.true has the same effect as the two-parameter form. If it is set to false, the next nextval will return exactly the specified value, and sequence advancement commences with the following nextval. Furthermore, the value reported by currval is not changed in this case. For example,

SELECT setval('myseq', 42); Next nextval will return 43
SELECT setval('myseq', 42, true); Same as above
SELECT setval('myseq', 42, false); Next nextval will return 42

The result returned by setval is just the value of its second argument.

This function requires UPDATE privilege on the sequence.

currval(regclass) →bigint

Returns the value most recently obtained by nextval for this sequence in the current session. (An error is reported if nextval has never been called for this sequence in this session.) Because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executed nextval since the current session did.

This function requires USAGE or SELECT privilege on the sequence.

lastval() →bigint

Returns the value most recently returned by nextval in the current session. This function is identical to currval, except that instead of taking the sequence name as an argument it refers to whichever sequence nextval was most recently applied to in the current session. It is an error to call lastval if nextval has not yet been called in the current session.

This function requires USAGE or SELECT privilege on the last used sequence.

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 Foo

Sequence 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 foo

Prior 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 runtime

Note 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.