

# Sequence manipulation functions
<a name="sequence-functions-syntax-support"></a>

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`](create-sequence-syntax-support.md). 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.

**Important**  
When using sequences, the cache value should be carefully considered. For more information, see the Important callout on the [`CREATE SEQUENCE`](create-sequence-syntax-support.md) page.  
For guidance on how best to use sequences based on workload patterns, see [Working with sequences and identity columns](sequences-identity-columns-working-with.md).


| Function | Description | 
| --- | --- | 
| nextval ( regclass ) → bigint | Advances the sequence object to its next value and returns that value. This is done atomically: even if multiple sessions run 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 increasing values beginning with 1. Other behaviors can be obtained by using appropriate parameters in the [`CREATE SEQUENCE`](create-sequence-syntax-support.md) 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\$1called flag. The two-parameter form sets the sequence's last\$1value field to the specified value and sets its is\$1called 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\$1called can be set to either true or false. true has the same effect as the two-parameter form. If it's 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 isn't changed here. For example: <pre>SELECT setval('myseq', 42);           -- Next nextval will return 43<br />SELECT setval('myseq', 42, true);     -- Same as above<br />SELECT setval('myseq', 42, false);    -- Next nextval will return 42</pre> 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 run nextval because 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 transaction. 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 transaction. It's an error to call lastval if nextval hasn't yet been called in the current transaction. This function requires USAGE or SELECT privilege on the last used sequence. | 

**Warning**  
The value obtained by `nextval` isn't 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, Aurora DSQL's sequence objects *can't be used to obtain "gapless" sequences*.  
Likewise, sequence state changes made by `setval` are immediately visible to other transactions, and aren't 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 don't have to look up the OID by hand, however, because the `regclass` data type's input converter will do the work for you. See the PostgreSQL documentation on [Object Identifier Types](https://www.postgresql.org/docs/current/datatype-oid.html) for details.