What Is a Sequence Generator? Issue Numbers Safely and Understand Gaps
A sequence generator is a database object that returns the next number according to configured rules each time it is called. It is often used for keys or reference numbers, but reserving a value and storing a row are separate operations. This makes a sequence more flexible than an identity column for multiple tables or pre-insert work, while gaps can be normal after rollbacks or caching.
What Is a Sequence Generator? Issue Numbers Safely and Understand Gaps
A sequence generator is a database object that returns the next number according to configured rules each time it is called. It is often used for keys or reference numbers, but reserving a value and storing a row are separate operations. This makes a sequence more flexible than an identity column for multiple tables or pre-insert work, while gaps can be normal after rollbacks or caching.
How does a sequence differ from an identity column?
An identity column is a column definition that automatically fills a value when a row is inserted. A standalone sequence object can be called by SQL or an application whenever a value is needed; the caller decides which column receives it. One sequence can therefore serve multiple tables, or a value can be reserved before a row is created. Sharing is useful when consumers intentionally need one common value space, but it does not create consecutive numbers within each table or distinguish table-specific numbering.
Request a value and insert a row in PostgreSQL
In PostgreSQL 18, CREATE SEQUENCE sets the starting value and increment, and nextval atomically obtains the next value.
CREATE TABLE tickets (
ticket_ref bigint PRIMARY KEY,
subject text NOT NULL
);
CREATE SEQUENCE ticket_ref_seq
AS bigint
START WITH 5000
INCREMENT BY 1
NO CYCLE;
INSERT INTO tickets (ticket_ref, subject)
VALUES (nextval('ticket_ref_seq'), 'Check delivery status')
RETURNING ticket_ref;Concurrent nextval calls across sessions each receive a distinct value atomically. NO CYCLE makes the call fail after the maximum rather than wrap to the beginning. This SQL is illustrative and was not executed. Set the data type, start, and increment for the expected volume; if duplicates must be prevented, add a PRIMARY KEY or UNIQUE constraint on the target column.
In PostgreSQL, currval returns the most recent nextval result for that sequence in the current session, so another session’s call cannot change it. It errors if the current session has not called nextval for that sequence. Applications often find it clearer to use the key returned by the INSERT, which identifies the value stored in that row.
Why can numbers be missing or appear out of order?
In PostgreSQL, calls to nextval and setval are not undone when the calling transaction rolls back. A value may be consumed before an INSERT fails or an ON CONFLICT clause rejects a row. Concurrent calls receive distinct values, but their return order across sessions is not necessarily one uninterrupted global sequence.
Setting a cache larger than one can improve performance by reserving several numbers in advance. Unused reserved numbers can be lost when a session ends, leaving gaps; when sessions reserve separate ranges, the overall return order may differ from expectations. SQL Server also supports CACHE and documents that an abnormal shutdown can skip cached values. Even with caching disabled, a number can be missing if it was requested but not used or the transaction was rolled back.
A sequence is therefore a fast, concurrency-friendly number allocator, not a gap-free business-number issuer. If invoices or statutory references must have no gaps, a separate business process may need to lock a counter row or use another coordinated approach. This serializes concurrent requests and can create waits and bottlenecks, so balance throughput against applicable rules.
How do database implementations differ?
| Database | Feature | How values are requested and key difference |
|---|---|---|
| PostgreSQL 18 | Standalone sequence object | nextval('name') atomically returns the next value. Configure CACHE, CYCLE, bounds, and OWNED BY. |
| SQL Server | Standalone sequence object | Define it with CREATE SEQUENCE; call NEXT VALUE FOR even before an INSERT. sp_sequence_get_range can reserve a range. |
| MySQL 8.4 | AUTO_INCREMENT column attribute | Automatic numbering is tied to inserts into a particular integer column. Do not assume it is the same as a reusable standalone CREATE SEQUENCE object. Explicit inserts or updates can change the next number. |
Defaults for start, maximum, cycling, caching, and behavior after failures vary by implementation. Check the documentation for the actual product and version instead of assuming portable behavior.
What should you check before choosing a sequence?
If a value only needs to be assigned automatically to a new row in one table, an identity column or the DBMS’s auto-increment feature may be simpler.
A standalone sequence can help when values must be reserved before storing a row or intentionally shared across consumers. Document the sharing scope and ownership.
If values must be unique, add a `PRIMARY KEY` or `UNIQUE` constraint independently of number generation.
Decide whether to stop or cycle when the range is exhausted. `CYCLE` can reuse earlier values, which may be unsafe for keys.
Caching can improve performance but may increase the chance of gaps. Changing only the cache setting does not make numbering gap-free.
After generating values across systems or restoring data, verify that sequence state will not collide with existing values.
Sources and scope
[PostgreSQL 18: CREATE SEQUENCE](https://www.postgresql.org/docs/18/sql-createsequence.html)
[PostgreSQL 18: Sequence Manipulation Functions](https://www.postgresql.org/docs/18/functions-sequence.html)
[Microsoft Learn: CREATE SEQUENCE](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver17)
[MySQL 8.4: Using AUTO_INCREMENT](https://dev.mysql.com/doc/refman/8.4/en/example-auto-increment.html)
Documentation checked on 2026-11-23. SQL is illustrative and was not executed. Caching, concurrency, gaps, explicit-value handling, and feature support vary by DBMS and version.