Jarwis.ClusterOps

Blog

Upgrading PostgreSQL to the next major version with pg_upgrade --link, and rehearsing it first

Jarwis Team · · 6 min read · PostgreSQL, Patroni, Upgrades

PostgreSQL minor releases are a package update and a restart. Major releases are a different job. The on-disk format changes between versions, so you cannot start version 17 on a version 16 data directory. You have to convert the data, and every approach has a different price in downtime, disk and risk.

This post compares the options, walks through pg_upgrade --link step by step, covers what changes on a Patroni cluster, and explains the one thing that separates uneventful upgrades from bad nights: rehearsing the whole thing on a restored copy first.

Your options

  • Dump and restore. pg_dumpall on the old version, restore on the new one. It is the simplest and the safest, and the slowest. Downtime scales with database size, which means hours for anything large.
  • pg_upgrade (copy mode). Converts the catalogs and copies the data files into the new directory. Faster than a dump, but you need free disk for a complete second copy.
  • pg_upgrade --link. Converts the catalogs and hard-links the data files instead of copying them. Downtime is typically minutes, even for very large databases, and no second copy is needed. The cost is that the old and new clusters share files.
  • Logical replication. Build the new version alongside, replicate, and cut over. Minimal downtime, the most moving parts, and some things (DDL, sequence values, large objects) need handling by hand.

For a single cluster where a short, planned outage is acceptable, pg_upgrade --link is the usual answer. Understand what that last sentence about shared files means before you use it.

Because both clusters point at the same files, the moment the new cluster starts and writes, the old data directory is no longer a valid database. You cannot just start the old server again if the upgrade goes badly after that point. Your way back is the backup. That is not a reason to avoid --link. It is a reason to take a backup first, and to know exactly which step is the point of no return.

The manual procedure

Install the new version's packages next to the old ones, initialise an empty new cluster with matching settings (same encoding, locale and checksum setting), and stop the old server. Then run the check, which touches nothing:

/usr/lib/postgresql/17/bin/pg_upgrade \
  --old-bindir=/usr/lib/postgresql/16/bin \
  --new-bindir=/usr/lib/postgresql/17/bin \
  --old-datadir=/var/lib/postgresql/16/main \
  --new-datadir=/var/lib/postgresql/17/main \
  --link --check

Read the output carefully. The things that most often show up:

  • Not enough free space, or the old and new data directories are on different file systems, which makes hard links impossible.
  • An extension installed in the old cluster that has no package for the new major version.
  • Prepared transactions that are still open, or logical replication slots that need attention.
  • A mismatch in encoding, locale, or data checksum settings between old and new.

When the check is clean, run the same command without --check. Then start the new cluster and, before the application is allowed back in, do the post-upgrade work. pg_upgrade does not carry optimizer statistics across, so queries can be badly planned until you rebuild them:

vacuumdb --all --analyze-in-stages

Update extensions in each database that uses them with ALTER EXTENSION name UPDATE;, run your smoke tests, and only then remove the old cluster. pg_upgrade writes a delete_old_cluster.sh script for that last step. Keep the old directory until you are sure, but remember that once the new cluster has started in --link mode, the old one cannot be started again.

Patroni changes the shape of the upgrade

On a Patroni cluster you do not upgrade each node. You upgrade the leader once and rebuild the replicas from it. In outline:

  1. Pause Patroni, so it does not react to the shutdowns that follow.
  2. Stop the replicas, then stop the leader.
  3. Run pg_upgrade --link on the leader.
  4. Point the Patroni configuration at the new binaries and data directory, and bring the leader up.
  5. Re-initialise each replica from the new leader.
  6. Resume Patroni and check the cluster state.

Expect a short outage while the leader restarts. Replicas are not usable as they are, since they hold the old format. Do this during a window and make sure your connection pooler or load balancer comes back pointed at the right place.

Rehearse it on a restored backup

Every item above is something you can verify before you do the real thing, if you do the whole procedure on a copy. Restore last night's backup onto a spare host, run the real pg_upgrade --link there, and start the new version. Then check:

  • Did every extension upgrade cleanly?
  • Do your settings still work? Parameters get removed or renamed between majors, and a server that will not start because of a removed setting is a bad surprise at 02:00.
  • Do your important queries still plan well? Run EXPLAIN on the ten heaviest ones and compare with production.
  • How long did it take? That number is your outage window.

A rehearsal also proves that your backup restores, which is worth knowing independently.

How this looks in Jarwis ClusterOps

On a PostgreSQL cluster, the Updates tab is the major upgrade panel. It supports clusters installed from the PGDG repository, standalone and Patroni. It will tell you why if a cluster is not eligible, for example a non-PGDG distribution, a custom data directory, streaming replication without Patroni, or a cluster already on the newest major.

The workflow has four steps: pre-check, rehearsal, upgrade and finalize. The pre-check is read-only on every node: it runs pg_upgrade --check against a scratch cluster, then looks at free disk, file system layout for hard links, logical replication slots, prepared transactions, checksum, locale and encoding, and whether each installed extension has a package for the target major. Findings are ranked as blocker, warning or info. The rehearsal runs on a throw-away host. It restores a recent backup to a verify host (the same kind of host used for restore drills), runs the real pg_upgrade --link there and reports extensions, settings and queries that do not work on the new version, including the EXPLAIN result of the slowest queries from query analytics. Nothing is done to the live cluster. The upgrade needs a backup from the last 24 hours, a fresh clean pre-check, and you typing the cluster name; it then goes through administrator approval. It also needs a passed rehearsal for the same version pair from the last 14 days; administrators can skip the rehearsal explicitly, but its report is the best predictor of surprises. Finalize removes the old cluster and its data directory and cannot be undone. The pre-check and the in-place upgrade are available on every plan, including Lite (free up to 3 nodes). Rehearsal on a throw-away host is a Pro feature ($9.99 per node per month); Lite can skip the rehearsal.

On Patroni, Jarwis follows the sequence above: Patroni is paused, replicas are stopped, the leader is upgraded, the cluster configuration is switched to the new binaries, and replicas are rebuilt from the new leader before Patroni resumes. Afterwards it runs ALTER EXTENSION ... UPDATE, runs ANALYZE, upgrades the pgBackRest stanza and records the new version. The upgrade plan on the tab shows exactly which steps can still be undone. Until the data is taken over, a failure brings the old cluster back automatically. After that point, recovery is from the backup.

You can start on Jarwis Lite, which is free for up to three nodes. There is a read-only demo at demo.jarwis.com if you want to look at a PostgreSQL cluster first. The full procedure is in the PostgreSQL major upgrade docs, and the backup and restore side is in backups.