Sub topic 11
Why gaps happen in PostgreSQL identity columns
Assume you know basic CREATE TABLE, INSERT, and SELECT statements, primary keys, integer types, and the basics of COMMIT and ROLLBACK. This chapter explains why an identity column can contain missing numbers.
An identity column gets values from a PostgreSQL sequence. A sequence is a database object that produces numbers independently of the transaction that requests them. The sequence usually supplies the next value through nextval.
A rolled-back insert still consumes a value
Consider this table:
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL
);
BEGIN;
INSERT INTO orders (customer)
VALUES ('rolled back');
ROLLBACK;
INSERT INTO orders (customer)
VALUES ('kept')
RETURNING id;The final insert might return 2, even though the table contains only one row. The first insert received 1 before the transaction was rolled back. PostgreSQL does not return that value to the sequence.
This behavior prevents concurrent transactions from blocking one another while they wait to decide whether an insert will commit. It also avoids complicated coordination between the table row and the sequence object.
Other common causes of gaps
A rollback is only one cause. Gaps can also result from these events:
- An application requests a sequence value and then does not insert a row.
- An insert fails after PostgreSQL has obtained an identity value.
- A row is deleted. The deleted value is not reused automatically.
- Two transactions insert at the same time. Their values are allocated separately from their commit order.
- A sequence uses caching. PostgreSQL reserves values in batches, and values that were reserved but not used can be lost after a server failure.
- An administrator or data load inserts explicit IDs, leaving unused values between automatic values.
The following table summarizes the important distinction:
| Event | Can the value be reused automatically? | Reason |
|---|---|---|
| Transaction rolls back | No | Sequence allocation is not rolled back |
| Row is deleted | No | Identity generation does not inspect deleted rows |
| Insert fails | No | The sequence value was already requested |
| Explicit value is loaded | No | The sequence does not automatically learn that value |
Gaps are necessary for concurrency
Suppose two transactions insert rows at nearly the same time. PostgreSQL can allocate 10 to one transaction and 11 to the other without locking the entire table. The transaction that receives 11 might commit first.
Therefore, identity values do not guarantee:
- no missing numbers,
- commit order,
- creation time,
- or a globally meaningful business sequence.
Use another column, such as a timestamp, when you need to record ordering. Use a separate business number when a domain requires a specific numbering policy.
When a gapless number is required
Legal documents, invoices, or regulated records may require a gapless series. An identity column is the wrong tool for that requirement.
A gapless design usually stores the next number in a single-row counter table and updates that row inside the same transaction as the document insert. The counter row must be locked so that concurrent transactions cannot receive the same number. This design has costs:
- concurrent inserts wait for the counter lock;
- a rollback must also roll back the counter update;
- failures and recovery require careful operational handling;
- throughput is lower than with a sequence.
In many systems, the better choice is to keep an internal identity key and store a separate business number with a clearly documented allocation policy.
Practical rule
Treat identity values as unique row keys, not as a gapless counter. Allow gaps unless the business requirement explicitly demands otherwise. If it does, design and test a transactional numbering mechanism instead of trying to repair identity values after the fact.
Exercise
An insert inside a transaction receives ID 8, but the transaction rolls back. The next successful insert receives ID 9. Is the identity column broken, and why?
Show answer
No. The sequence allocated ID 8 independently of the transaction, so the rollback did not restore it. ID 9 is valid, and the gap is expected.