← Back to PostgreSQL, page 5

postgresql-0095 · Hard senior

Plan a major-version upgrade of a 2 TB PostgreSQL database with minimal downtime.

pg_upgrade with --link upgrades in place in minutes regardless of size, but needs a short full outage and careful extension compatibility checks. Logical replication to a new-version cluster allows near-zero downtime: replicate, verify, then switch traffic, at the cost of more setup and caveats such as sequences and DDL not being replicated. Either way, rehearse on a copy, run ANALYZE afterwards, and keep a rollback path.

Full answer

Option 1, pg_upgrade --link:

  • Install the new version and all extensions in compatible versions; run pg_upgrade --check.
  • Stop the old cluster, run the upgrade (hard links make it fast), start the new one, then run vacuumdb --analyze-in-stages, because planner statistics are not carried over (PostgreSQL 18 begins to preserve them).
  • Replicas are rebuilt or upgraded with rsync procedures. Rollback after starting the new cluster in link mode is not possible, so take a backup first.

Option 2, logical replication:

  • Create the schema on the new cluster, publish all tables on the old one and subscribe from the new one; wait for the initial copy and catch-up.
  • Logical replication does not copy sequence values or DDL: freeze schema changes during the window and set sequences after the switch.
  • Tables need primary keys or replica identity for updates and deletes.
  • Switch by stopping writes, waiting for zero lag, moving sequences, and repointing applications or the pooler. The old cluster stays as a fallback.

Common to both: rehearse with production-sized data, read the release notes for behaviour changes, test the application against the new version in CI, and watch query plans and pg_stat_statements closely for the first days. Nothing was run here.

Key points

  • pg_upgrade --link is fast but needs a short outage
  • Logical replication gives near-zero downtime
  • Sequences and DDL do not replicate logically
  • ANALYZE after upgrading
  • Rehearse and keep a rollback path

Common mistakes

  • Skipping ANALYZE after pg_upgrade
  • Forgetting sequences in a logical cutover

Follow-ups

  • How do extensions such as PostGIS affect upgrades?
  • What does pg_createsubscriber in PostgreSQL 17 offer?

Tips

  • Compare the two methods