Skip to main content
The CREATE SEQUENCE creates a new sequence in a database. Use a sequence to auto-increment integers in a table.
The “ statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .

Considerations

  • Using a sequence is slower than and is likely to cause performance problems due to . Incrementing a sequence requires a write to persistent storage. In CockroachDB, all writes are . In , replicated writes can add cross-region latency (for example, in a 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 .
  • 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 . If a table must be indexed on sequential keys, use . Hash-sharded indexes distribute sequential traffic uniformly across ranges, eliminating single-range and improving write performance on sequentially-keyed indexes at a small cost to read performance.
  • By default, you cannot create sequences that are columns in tables in other databases. You can enable such sequence creation by setting the sql.cross_db_sequence_owners.enabled to true.

Required privileges

The user must have the CREATE on the parent database.

Synopsis

create_sequence syntax diagram

Parameters

ParameterDescription
seq_nameThe name of the sequence to be created, which must be unique within its database and follow the . When the parent database is not set as the default, the name must be formatted as database.seq_name.
INCREMENTThe value by which the sequence is incremented. A negative number creates a descending sequence. A positive number creates an ascending sequence.

Default: 1
MINVALUEThe minimum value of the sequence. Default values apply if not specified or if you enter NO MINVALUE.

Default for ascending: 1

Default for descending: MININT
MAXVALUEThe maximum value of the sequence. Default values apply if not specified or if you enter NO MAXVALUE.

Default for ascending: MAXINT

Default for descending: -1
START [WITH]The first value of the sequence.

Default for ascending: 1

Default for descending: -1
RESTART [WITH]Sets nextval to the specified number, or back to the original START value.
NO CYCLEAll sequences are set to NO CYCLE and the sequence will not wrap.
CACHEThe number of sequence values to cache in memory at the level. All sessions on the node share the same cache, which can be concurrently accessed, and which reduces the chance of creating large gaps between generated IDs. A cache size of 1 means that there is no cache, and cache sizes of less than 1 are not valid.

Default: 1 (sequences are not cached by default). While this parameter caches sequences per node, to cache sequence values per session, use PER SESSION CACHE explicitly.
PER NODE CACHEThe number of sequence values to cache in memory at the node level. All sessions on the node share the same cache, which can be concurrently accessed, and which reduces the chance of creating large gaps between generated IDs. A cache size of 1 means that there is no cache, and cache sizes of less than 1 are not valid. This is the default caching strategy, and is equivalent to specifying CACHE.
PER SESSION CACHEThe number of sequence values to cache in memory for reuse within a single . A cache size of 1 means that there is no cache, and cache sizes of less than 1 are not valid.
OWNED BY column_nameAssociates the sequence to a particular column. If that column or its parent table is dropped, the sequence will also be dropped.
Specifying an owner column with OWNED BY replaces any existing owner column on the sequence. To remove existing column ownership on the sequence and make the column free-standing, specify OWNED BY NONE.

Default: NONE
opt_tempDefines the sequence as a session-scoped temporary sequence. For more information, see Temporary sequences.

Sequence functions

CockroachDB supports the following :
  • 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 .
This feature is in and subject to change. To share feedback and/or issues, contact Support.
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 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 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, add 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 , using the for a , with the customer_seq sequence as its input:
Inserting into this table with an 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:
If a value has been obtained from the sequence in the current session, you can also use the 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 of customer_seq using the . 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.
Let’s add another record to the table to check that the new record adheres to the new next value.

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 (per node)

For improved performance, use the CACHE keyword to cache sequence values in memory at the node level. By default, all sessions on a node share the same cache. If you want sequence values to be cached per session, see Cache sequence values per session. For example, to cache 10 sequence values in memory per node:
You can also use the PER NODE CACHE clause to explicitly cache sequence values in memory at the node level. Since this is the default strategy, it is equivalent to using CACHE. For example, to cache 10 sequence values per node:

Cache sequence values per session

To cache sequence values within a single session, use the PER SESSION CACHE clause. For example, to cache 10 sequence values per session:

See also