Sub topic 13
Fixing the sequence after loading data
This chapter assumes you know basic CREATE TABLE, INSERT, and SELECT, understand primary keys, know the smallint, integer, and bigint types, and have a basic understanding of transactions.
An identity column gets its values from a sequence. When PostgreSQL generates an ID, it asks the sequence for its next value. Loading a row with an explicit ID does not automatically move the sequence forward. The next generated ID can therefore collide with an existing row.
The problem after an explicit load
Suppose orders.id is an identity column. A data migration inserts IDs up to 5000:
INSERT INTO orders (id, customer)
OVERRIDING SYSTEM VALUE
SELECT id, customer
FROM imported_orders;The table may now contain IDs through 5000, while the sequence still believes its next value is 1. A later normal insert could attempt to use an existing ID and fail with a primary-key violation.
The sequence must be resynchronized after the load.
Set the sequence to the current maximum
Use pg_get_serial_sequence to find the sequence owned by the identity column:
SELECT setval(
pg_get_serial_sequence('orders', 'id'),
max(id)
)
FROM orders;The max(id) expression finds the largest ID currently stored. The default third argument to setval is effectively true, so PostgreSQL treats the supplied value as already returned. The next nextval call produces the following value according to the sequence increment.
For example, if the maximum ID is 5000 and the increment is 1, the next generated ID is 5001.
| Operation | Sequence value | Next generated ID |
|---|---|---|
| After `setval(..., 5000)` | 5000 | 5001 |
You can write the intent more explicitly:
SELECT setval(
pg_get_serial_sequence('orders', 'id'),
(SELECT max(id) FROM orders),
true
);The sequence is shared database state. Coordinate this repair with application writes. Otherwise, an insert occurring during the load or repair can produce an unexpected result.
Handling an empty table
max(id) returns NULL when the table is empty. Do not pass that value to setval without deciding what the first generated ID should be.
For a table that should restart at 1, use the identity restart operation:
ALTER TABLE orders
ALTER COLUMN id RESTART WITH 1;This expresses the desired identity behavior directly. Use the appropriate starting value if the column was configured with another start.
Verify the repair
After resynchronizing, insert a test row and inspect the returned ID:
INSERT INTO orders (customer)
VALUES ('Lin')
RETURNING id;The returned value should be greater than every existing ID. You can also check the table directly:
SELECT max(id) AS largest_id
FROM orders;The sequence can still have gaps. Rollbacks, cached sequence values, and failed inserts can consume values without leaving rows behind. The repair only prevents the sequence from falling behind the data.
Exercise
The largest existing orders.id is 2400, but the next normal insert tries to use 17. What SQL repairs the sequence?
Show answer
Run SELECT setval(pg_get_serial_sequence('orders', 'id'), (SELECT max(id) FROM orders), true); and then verify with a normal insert using RETURNING id.