Migration
Logical replication for migrations
For anyone planning a cutover who has been told logical replication makes it painless.
It moves a database between majors with seconds of downtime, and it leaves out sequences, large objects and DDL. What it carries, and what you carry.
Reference page, revised in place. Last updated .
Why it gets chosen, and what that choice costs
pg_upgrade is fast but it upgrades a cluster in place, which means the downtime is however long the upgrade takes and the rollback is a restore. Logical replication is the alternative: stand up the new major alongside the old one, replicate into it while the old one keeps serving, and cut over when the lag is small. Downtime becomes the length of the cutover rather than the length of the upgrade, and the old cluster is still there afterwards if the cutover goes badly.
The cost is that logical replication replicates rows, not a cluster. Everything that is not a row in a published table is yours to move, and the list is longer than people expect.
The things it does not carry
Sequences. The subscriber’s sequences are not advanced by replication. Cut over without setting them and the first insert on the new primary collides with a row already there, which surfaces as a primary key violation seconds after the application reconnects. Sequence values have to be read from the publisher after writes stop and set on the subscriber before writes resume.
DDL. Schema changes are not replicated at all. A migration that adds a column on the publisher during the sync leaves the subscriber without it, and replication of that table breaks at the next change. Either freeze DDL for the duration or apply every change to both sides by hand.
Large objects. Anything in pg_largeobject is not replicated.
TRUNCATE before version 11, and by default some row filters aside, only tables. Views, materialized views, foreign tables and indexes are not replicated; the subscriber needs its own.
None of these produce an error during the sync. They produce a working-looking replica that is wrong in a specific way at cutover.
Watching the sync without guessing
The publication side tells you what is being published and whether anything was left out:
SELECT p.pubname,
p.puballtables,
p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate,
count(pt.prrelid) AS explicit_tables
FROM pg_publication p
LEFT JOIN pg_publication_rel pt ON pt.prpubid = p.oid
GROUP BY p.pubname, p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate
ORDER BY p.pubname;
A publication with puballtables false and an explicit_tables count lower than the number of tables you meant to move is the most common silent omission, and it is usually a table added to the schema after the publication was created.
On the subscriber, the per-table state is what tells you whether the initial copy has finished. The srsubstate column reads i for initialize, d for data copy, s for synchronized and r for ready, and a table stuck in d on a large table is normal for hours and abnormal for days:
SELECT s.subname,
c.relname,
sr.srsubstate AS state
FROM pg_subscription_rel sr
JOIN pg_subscription s ON s.oid = sr.srsubid
JOIN pg_class c ON c.oid = sr.srrelid
ORDER BY sr.srsubstate, c.relname;
The lag itself is read on the publisher, from the slot the subscription holds. A replication slot that stops advancing holds WAL on the publisher indefinitely, which is the failure that takes the old database down during a migration meant to be safe:
SELECT slot_name,
slot_type,
active,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal
FROM pg_replication_slots
WHERE slot_type = 'logical'
ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC;
Watch retained_wal rather than active. A slot can be active and still falling behind, and the disk fills either way. The same mechanism and the same query appear on replication lag and slot health, because it is the same failure wearing a migration hat.
Tables without a replica identity
Updates and deletes need a way to identify the row on the subscriber. A table with a primary key has one. A table without one has REPLICA IDENTITY NOTHING in effect, and updates to it fail on the publisher with a message about replica identity rather than failing quietly, which is at least honest. REPLICA IDENTITY FULL works and makes the subscriber match on every column, which is slow on wide tables and very slow without an index.
This is worth finding before the cutover rather than during it, and it is the single most common reason a migration that tested fine on a subset breaks on the real schema.
The cutover itself
The order matters and it is short. Stop writes on the publisher. Wait for the subscriber to catch up, which you confirm from the slot rather than from a dashboard. Advance every sequence. Point the application at the new cluster. Then, and only then, drop the subscription, because dropping it first discards the slot and with it any chance of confirming where you were.
Extensions deserve a last look before any of that, for the reasons on pg_upgrade and extensions: the subscriber needs them installed and at a version the new major accepts, and logical replication will not tell you one is missing until a row needs it.