A Postgres cutover can go cleanly on the replica side and still break the application an hour later. The replica caught up, the promote worked, the health check is green. Four things that a green replica does not tell you are worth checking before you call it done.
1. Sequences do not come along with logical replication
Physical (streaming) replication copies everything, sequences included. Logical replication, which is what most major-version upgrades and low-downtime migrations use, copies row changes only. The sequence on the new primary stays where it was when the subscription was created.
The result shows up on the first insert after cutover: duplicate key value violates unique constraint, because nextval() hands out an id that already exists.
Check every serial or identity column before you open the writes:
SELECT schemaname, sequencename, last_value FROM pg_sequences; SELECT max(id) FROM orders; If last_value is lower than max(id), fix it with a margin, because the old primary may still be handing out values until it is fenced:
SELECT setval(pg_get_serial_sequence('orders', 'id'), (SELECT max(id) FROM orders) + 1000); A gap in ids is harmless. A duplicate is an outage.
2. Fence the old primary, do not just stop sending it traffic
Clients with a stale DNS answer, a connection pool that keeps its server connections, or a cron job with a hardcoded host will keep writing to the old primary after the promote. Those writes are lost, and nobody gets an error.
What works, in order of strength:
- A
rejectline inpg_hba.conffor the app network, then a reload, then terminate the existing sessions:SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'app' AND pid <> pg_backend_pid();. A new rule only affects new connections, which is why the terminate step is needed. -
REVOKE CONNECT ON DATABASE app FROM PUBLIC;on the old primary also works in the logical replication case, because grants stay on that node. Revoking from the app role alone does nothing while PUBLIC still holds the privilege. With a streaming standby the revoke would replicate to the standby you are about to promote, so usepg_hba.confthere. -
default_transaction_read_only = onis a useful seatbelt, but a session can override it withSET, so it is not a fence on its own.
Do the fence before the promote, not after, so there is no window where both nodes accept writes.
3. Poolers keep talking to the old host
PgBouncer does not drop existing server connections when you change the host in its config and reload. Idle server connections stay open to the old primary until they are closed. After changing the target, run RECONNECT on the admin console, or PAUSE, change the config, RELOAD, RESUME. Then look at SHOW SERVERS; and confirm every row points at the new address.
4. Roles, and what the old primary is still holding
Roles and their passwords are global objects. Logical replication does not carry them, so a role that exists on the old primary may be missing on the new one. Compare with pg_dumpall --globals-only before the cutover, not during it.
Afterwards, look at pg_replication_slots on the old primary. An inactive slot keeps WAL on disk forever, and a forgotten slot from a decommissioned subscriber fills a volume weeks later. Drop the ones you no longer need with pg_drop_replication_slot().
A short checklist
- Sequences:
last_valueagainstmax(id)for every table, thensetvalwith a margin. - Fence the old primary (
pg_hba.confreject or revoke, then terminate sessions) before promoting. - Pooler: reconnect, then check that every server connection points at the new host.
- Roles compared, extensions installed, privileges checked.
- One real write and one real read through the application path, not through psql.
- Old primary: slots dropped, or the node shut down.
The replica being healthy is the easy part. The other four are where the Friday evening goes.
