Video in progress

You need to upgrade PostgreSQL from 14 to 17, and the allowed downtime is a few minutes. What plan do you make?

Two main paths: pg_upgrade --link (fast, but a few minutes of downtime and rolling back is hard) or syncing to a new cluster with logical replication and then doing a switchover (downtime of seconds, but a lot of preparation). In both cases I do a full rehearsal on a copy first.

📖 Answer

pg_upgrade --link doesn't copy the data files, it makes hard links. Even a multi-TB database upgrades in minutes. But:

  • once the new cluster has started, you can't go back to the old one (the files are shared); the way back is a backup or an old replica;
  • after the upgrade there are no statistics, and you need vacuumdb --analyze-in-stages; during that time queries run with bad plans (PG 18 started preserving statistics, but 17 doesn't);
  • replicas have to be rebuilt, or you use a special rsync procedure.

Through logical replication:

  1. A new cluster on PG 17, and I move the schema with pg_dump --schema-only.
  2. A PUBLICATION on the old database and a SUBSCRIPTION on the new one: an initial copy, then the stream.
  3. When the lag drops to zero: put the application in read-only or stop it briefly, copy the sequences by hand (logical replication doesn't carry over sequence values!), and switch the endpoint.
  4. Set up replication in the opposite direction to keep a way back.

The limitations: tables without a PK don't replicate UPDATE/DELETE; DDL isn't replicated, so migrations are frozen; large objects don't come across.

In both cases: a full rehearsal on a prod copy, measuring the time, application tests on the new version (some queries may slow down because of planner changes), and compatible versions of the extensions (PostGIS, pg_partman). For a small database I choose pg_upgrade, and for a big system with a strict SLA the logical path.