Learn SQL
-- tables.sequences_identity_keys

Sequences, IDENTITY & Serial Keys in SQL

Gap-free is a myth β€” order, not contiguity

--concept

Surrogate keys ride sequences: IDENTITY (standard, GENERATED ALWAYS AS IDENTITY) or SERIAL (PG legacy) or explicit CREATE SEQUENCE. NEXTVAL hands ordered, non-transactional numbers β€” rollbacks burn gaps, replicas cache blocks. Gap-free numbering needs locked counters (slow) β€” order without contiguity is the production contract. UUIDv7 ordered variants bridge distributed needs.

--how it works
1

IDENTITY default

BIGINT GENERATED ALWAYS AS IDENTITY PK β€” ordered, atomic, session-safe.

2

Sequences explicit

CREATE SEQUENCE for app-controlled blocks (order numbers).

3

Gaps by design

Rollback/cache/concurrency burn values β€” order, not contiguity.

4

RETURNING not max+1

Fetch drawn ids in-statement; max(id)+1 races across sessions.

--visualization
idfate
1001committed
10031002 burned by rollback
Stage 1 / 1
Order, not contiguity. RETURNING fetches ids β€” never max(id)+1 across sessions.
--example
query.sql
1CREATE TABLE orders (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY);
2INSERT INTO orders DEFAULT VALUES RETURNING id;
Result
id
1
2

Each insert draws the next sequence value atomically with RETURNING β€” no max+1 race. A rolled-back insert burns its number; the next commit skips it by design.

--variants

Draw + return atomically

No cross-session race.

query.sql
1INSERT INTO orders DEFAULT VALUES RETURNING id;
Result
id
1001
1002
1Sequence hands next value inside the insert txn.
2RETURNING reports the drawn id.
3Rolled-back draws burn β€” next skips by design.

Gaps are healthy

Contiguity demands locks; order does not.

query.sql
1-- gaps from: ROLLBACK, cache blocks, concurrent takes
Result
idfate
1001committed
10031002 burned by rollback
1Gap-free needs table locks β€” throughput dies.
2Audit by order, not contiguity.
3TRUNCATE … RESTART IDENTITY resets only on purpose.
--key facts
Gaps normalrollback/cache/concurrency burn values
Never max+1races β€” use sequences + RETURNING
RESTART IDENTITYTRUNCATE resets, DELETE does not
--use cases
Order/ticket numbering without races
Shard-block allocation via sequences
--under the hood
NEXTVAL is non-transactional by design β€” visible immediately to avoid blocking; CACHE sizes tune contention.
UUIDv7 ordered prefixes append mostly-sorted for distributed gap-tolerance.

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.