CREATE SEQUENCE statement creates a new sequence in a database. Use a sequence to auto-increment integers in a table.
The CREATE SEQUENCE statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.
Considerations
- Using a sequence is slower than auto-generating unique IDs with the
gen_random_uuid(),uuid_v4(), orunique_rowid()built-in functions and is likely to cause performance problems due to hotspots. Incrementing a sequence requires a write to persistent storage. In CockroachDB, all writes are replicated and must reach a write quorum. In multi-region deployments, replicated writes can add cross-region latency (for example, in a region survival configuration) and become a throughput bottleneck for write-heavy workloads. Cached sequences can reduce the frequency of these writes, though gaps in sequence values may occur if cached values are lost. Auto-generating a unique ID does not require a replicated write. Therefore, use auto-generated unique IDs unless an incremental sequence is preferred or required. For more information, refer to Unique ID best practices. - A column that uses a sequence can have a gap in the sequence values if a transaction advances the sequence and is then rolled back. Sequence updates are committed immediately and aren’t rolled back along with their containing transaction. This is done to avoid blocking concurrent transactions that use the same sequence.
- We discourage indexing on sequential keys. If a table must be indexed on sequential keys, use hash-sharded indexes. Hash-sharded indexes distribute sequential traffic uniformly across ranges, eliminating single-range hot spots and improving write performance on sequentially-keyed indexes at a small cost to read performance.
- By default, you cannot create sequences that are owned by columns in tables in other databases. You can enable such sequence creation by setting the
sql.cross_db_sequence_owners.enabledcluster setting totrue.
Required privileges
The user must have theCREATE privilege on the parent database.
Synopsis
Parameters
Sequence functions
CockroachDB supports the following SQL sequence functions:nextval('seq_name')currval('seq_name')lastval()setval('seq_name', value, is_called)
Temporary sequences
CockroachDB supports session-scoped temporary sequences. Unlike persistent sequences, temporary sequences can only be accessed from the session in which they were created, and they are dropped at the end of the session. You can create temporary sequences on both persistent tables and temporary tables.Temporary tables must be enabled in order to use temporary sequences. By default, temporary tables are disabled in CockroachDB. To enable temporary tables, set the
experimental_enable_temp_tables session variable to on.Details
- Temporary sequences are automatically dropped at the end of the session.
- A temporary sequence can only be accessed from the session in which it was created.
- Temporary sequences persist across transactions in the same session.
- Temporary sequences cannot be converted to persistent sequences.
Like temporary tables, temporary sequences are not in the
public schema. Instead, when you create the first temporary table, view, or sequence for a session, CockroachDB generates a single temporary schema (pg_temp_<id) for all of the temporary objects in the current session for a database.Usage
To create a temporary sequence, addTEMP/TEMPORARY to a CREATE SEQUENCE statement.
For example:
Examples
Create a sequence with default settings
In this example, we create a sequence with default settings.Use a sequence when creating a table
In this example, we create a table, using thenextval() function for a default value, with the customer_seq sequence as its input:
INSERT statement that relies on default values will call nextval, which increments the sequence.
View the current value of a sequence
To view the current value without incrementing the sequence, use:The
log_cnt and is_called columns are returned only for PostgreSQL compatibility; they are not stored in the database.currval('seq_name') function to get that most recently obtained value:
Set the next value of a sequence
In this example, we’re going to change the next value ofcustomer_seq using the setval() function. Currently, the next value will be 3 (i.e., 2 + INCREMENT 1). We will change the next value to 5.
You cannot set a value outside the
MAXVALUE or MINVALUE of the sequence.The
setval('seq_name', value, is_called) function in CockroachDB SQL mimics the setval() function in PostgreSQL, but it does not store the is_called flag. Instead, it sets the value to val - increment for false or val for true.Create a sequence with user-defined settings
In this example, we create a sequence that starts at -1 and descends in increments of 2.List all sequences
Cache sequence values in memory
For improved performance, use theCACHE keyword to cache sequence values in memory.
For example, to cache 10 sequence values in memory:

