Sub topic 14
Migrating a `serial` column to identity
Assume you know basic CREATE TABLE, INSERT, and ALTER TABLE syntax. You should also understand primary keys and know that existing rows must keep their current IDs during the migration.
A serial column is older PostgreSQL shorthand. PostgreSQL creates a sequence, sets a column default to nextval(...), and uses that sequence to generate numbers. An identity column records the generation rule as part of the column definition.
| Feature | `serial` | Identity column |
|---|---|---|
| Definition | A default plus a separate sequence | A column property backed by a sequence |
| Standard SQL syntax | No | Yes |
| Existing IDs preserved during conversion | Yes | Yes |
| Explicit values controlled by generation mode | No | Yes |
Inspect the old serial definition
Before changing the table, find the sequence used by the serial column:
SELECT pg_get_serial_sequence('legacy', 'id');This returns the sequence name, usually something like public.legacy_id_seq. Do not assume the name if the table or column was renamed.
The old default must be removed before the old sequence is dropped. Otherwise, the column default still depends on that sequence.
Convert the column
The following example assumes that legacy.id is an integer primary key, the table already contains rows through ID 1000, and the column is already NOT NULL.
ALTER TABLE legacy ALTER COLUMN id DROP DEFAULT;
DROP SEQUENCE legacy_id_seq;
ALTER TABLE legacy
ALTER COLUMN id
ADD GENERATED BY DEFAULT AS IDENTITY (START WITH 1001);The existing values remain unchanged. PostgreSQL creates a new sequence for the identity property, starting at 1001. Future inserts can omit id and receive generated values.
INSERT INTO legacy (note)
VALUES ('new row')
RETURNING id;BY DEFAULT permits an insert to provide an explicit ID. This is useful when importing historical data or synchronizing records between systems. Use GENERATED ALWAYS instead when applications should normally let PostgreSQL control the column.
Migration safety
Run the change during a maintenance window or otherwise prevent writes that depend on the old default. ALTER TABLE takes a table lock, and concurrent application activity can make a multi-step migration harder to reason about.
If the table has foreign keys, indexes, triggers, or application code that refers to the sequence by name, inspect those dependencies before dropping it. The identity column replaces the default-generation mechanism, but it does not automatically repair external code that calls the old sequence directly.
If the old serial sequence is ahead of the largest stored ID, starting the new identity sequence at max(id) + 1 is still safe. If rows were loaded with larger explicit IDs, choose a start value above the actual maximum instead:
SELECT COALESCE(max(id), 0) + 1
FROM legacy;For a production migration, record that result and use it in the START WITH clause. Check for conflicting IDs before enabling writes again.
Exercise
Why must the old default be dropped before legacy_id_seq is dropped?
Show answer
The default calls nextval on that sequence. Dropping the default removes the dependency so the old sequence can be removed safely.