Sub topic 10
Planning for rollback
This chapter assumes you can start and stop a cluster with pg_ctl, you have taken backups with pg_dump or pg_basebackup, and you know the three transfer modes from earlier in this tutorial: copy, clone and link. It also assumes you have run pg_upgrade --check and understand the upgrade sequence.
A rollback plan answers one question before the maintenance window opens: if the new cluster is wrong, how do we get back to a working database, and how much data do we lose doing it? The answer depends on the transfer mode you picked and on one point in time, the moment the new cluster first accepts writes.
Two kinds of rollback
"Rollback" covers two different situations.
- Rollback before cutover. The upgrade failed, or post-upgrade checks failed, and no application has written to the new cluster. Nothing is lost. You return to the old cluster as it was.
- Rollback after cutover. Applications have written to the new cluster, and then you find a problem such as a performance regression, an incompatible extension, or an application bug on the new version. Returning to the old cluster now discards, or requires you to carry back, every transaction committed since cutover.
The first kind is a matter of mode and procedure. The second is a matter of design, and pg_upgrade gives you no tool for it. PostgreSQL does not support downgrading a data directory in place. Writes made on the new version cannot be replayed into an old-version cluster through WAL, because the physical formats differ.
What each transfer mode lets you do
The old data directory is your main safety net, and each mode treats it differently.
| Mode | Old cluster after a successful upgrade | Extra disk needed | Rollback before cutover |
|---|---|---|---|
| Copy (default) | Untouched and still startable | Full size of the data | Start the old cluster |
| Clone (`--clone`) | Untouched and still startable | Almost none, until files diverge | Start the old cluster |
| Link (`--link`) | Shares files with the new cluster, so it is unsafe once the new cluster starts | Almost none | Only in a narrow window, otherwise restore a backup |
In copy mode, pg_upgrade reads the old files and writes new ones. The old cluster is never modified, so you can start it with its own binaries and it behaves as if the upgrade never happened.
Clone mode asks the filesystem for reflinks (copy-on-write file sharing). This needs a filesystem that supports it, such as Btrfs, or XFS created with reflink enabled. The new cluster shares blocks with the old one, but when either side writes, the filesystem copies the changed blocks. The old cluster stays valid after the new one starts. This is the mode to choose when you want link-like speed and copy-like safety.
Link mode creates hard links, so both directory trees point at the same files on disk. Once the new cluster starts and writes to those files, the old cluster no longer matches its own control data and WAL. Starting it then risks corruption.
The link-mode safety window
To stop you from starting the old cluster by accident, pg_upgrade --link renames the old cluster's global/pg_control to global/pg_control.old when it finishes. The old server then refuses to start.
If the upgrade finished and you have not yet started the new cluster, you can undo this. Rename the file back and start the old server:
mv /data/pg16/global/pg_control.old /data/pg16/global/pg_control
/opt/pg16/bin/pg_ctl -D /data/pg16 -l /data/pg16/server.log startThis only works if the new cluster has never been started after the links were made. After the first start of the new cluster, treat the old directory as lost.
If pg_upgrade fails partway through linking, the shared files may already have been touched. Do not try to be clever. Restore the old cluster from your backup or snapshot, delete the failed new data directory, and start over.
Build the safety net before you start
A rollback you have never tested is a hope, not a plan. Pick at least one of these before the window opens, and rehearse it.
Filesystem snapshot
If your data directory is on ZFS, LVM or a cloud volume with snapshots, take a snapshot of the stopped old cluster. A stopped cluster gives a clean, consistent image.
/opt/pg16/bin/pg_ctl -D /data/pg16 stop -m fast
zfs snapshot tank/pgdata@pre-pg17-upgradeIf the upgrade goes wrong, stop the new cluster, then restore:
zfs rollback tank/pgdata@pre-pg17-upgrade
/opt/pg16/bin/pg_ctl -D /data/pg16 -l /data/pg16/server.log startA snapshot restores in seconds regardless of database size, which makes it the best choice for link mode. The names tank/pgdata and the paths above are placeholders. Substitute your own dataset or volume.
Base backup plus archived WAL
A pg_basebackup taken just before the upgrade, together with a WAL archive, lets you restore the old cluster and replay to the moment before shutdown. Restoring is slow for large databases, so this is a fallback and not the fast path. Verify that the backup restores and that the server starts. A backup that has never been restored has not been proven to work.
A stopped old-version standby
If you run streaming replication, you can leave one standby on the old version, stopped and in sync at shutdown time. Upgrade the primary, and keep that standby's directory untouched. If you need to roll back, promote the standby with the old binaries. This costs hardware but gives a fast, full-fidelity rollback with no restore step. The details of upgrading the other standbys are covered in the replica chapter.
Logical replication back to the old version
For the strongest after-cutover rollback, set up logical replication from the new cluster to the old one after the upgrade. Writes made on the new version flow back, so the old cluster stays current and you can switch applications back without losing data. This is more work. You must create publications and subscriptions, handle sequences (logical replication does not carry sequence values), and make sure every replicated table has a replica identity. Teams usually do this only for upgrades where a failed cutover is very expensive.
The rollback window
Choose and write down how long you will keep the old cluster available. The old directory is only useful while it is still a faithful copy of the data, so the window has a natural meaning for each approach.
- Before cutover, the window is open. Rollback costs nothing.
- After cutover with no reverse replication, every minute of new writes is data you would lose or have to re-apply by hand.
- After you run
delete_old_cluster.sh, the old cluster is gone, and only backups remain.
The following diagram shows the decisions as a flow.
Handling a failed pg_upgrade run
When pg_upgrade itself stops with an error, the recovery depends on where it stopped.
- Failure during checks, before any data moved. Neither cluster was changed. Fix the reported problem and run
pg_upgrade --checkagain. The old cluster can be started as normal. - Failure in copy or clone mode, during transfer. The old cluster was never modified. Delete the new data directory, run
initdbagain with the same options, and retry. Starting from a fresh new directory matters, because a partly populated one is not a valid target. - Failure in link mode, during or after linking. Assume the old cluster is damaged. Restore from snapshot or backup, then recreate the new directory.
In every case, read the log files before retrying. pg_upgrade writes its logs under a pg_upgrade_output.d directory inside the new data directory, and it keeps them after a failure. The logs name the failing command and the database where it happened. If the output directory was removed, rerunning reproduces the logs.
If you stopped the old cluster by hand for a snapshot, remember to restart it before a retry that needs it, or rerun pg_upgrade, which starts and stops both clusters itself.
Rolling back the surroundings
The data directory is only part of the system. Plan for these as well.
- Configuration.
pg_upgradedoes not carrypostgresql.conforpg_hba.confacross. You wrote them for the new cluster by hand. Keep the old files unchanged so the old cluster starts with its old settings. - Ports and addresses. If you ran the new cluster on a temporary port and then swapped ports, write down the swap and its reverse. A rollback that fails because the old server cannot bind to its usual port is a poor use of the window.
- Connection strings and poolers. Applications and tools such as PgBouncer point at a host and port. Make the switch a single config change that you can revert in one step.
- Service units and cron jobs. A systemd unit that starts
/opt/pg17/bin/postgresmust be switched back to the 16 binaries, and backup jobs that call version-specific tools need the same care. - Extensions. Extension shared libraries are per version. Rolling back means the old extension binaries must still be installed, so do not uninstall the old packages until the window closes.
Define success criteria before cutover
A rollback decision made under pressure is easier when the thresholds are written in advance. Before you let applications back in, run a short verification list against the new cluster:
SELECT version();
SELECT count(*) FROM pg_stat_activity;
SELECT extname, extversion FROM pg_extension ORDER BY extname;Then compare row counts or checksums for a few critical tables against values you recorded on the old cluster, run a representative set of queries and compare their plans and timings, and confirm that the application's smoke tests pass. Agree in advance on the failure conditions that trigger rollback, such as any failed smoke test, or query latency worse than a stated percentage. Doing this while the new cluster is still closed to users keeps you in the zero-loss rollback case.
Cleanup comes last
pg_upgrade generates a delete_old_cluster.sh script (or, on some platforms, a similar script) that removes the old data directory. Do not run it on the day of the upgrade. Keep the old directory, or its snapshot, until the window you chose has passed and you have taken a good backup of the new cluster. Only then remove the old cluster, and remove the old binaries and packages after that.
Exercise
You upgrade with pg_upgrade --link and the run completes successfully. Before starting the new cluster, a colleague reports that the wrong parameter file was used for the new server, and you decide to go back to the old cluster. What do you do, and what would change if you had already started the new cluster and let applications write to it for an hour?
Show answer
Before the new cluster ever started, rename global/pg_control.old in the old data directory back to global/pg_control and start the old cluster with the old binaries. No data is lost. Once the new cluster has started in link mode, the two clusters share files that the new server has written to, so the old directory is unsafe. You must restore the old cluster from a snapshot or backup, and the hour of writes on the new cluster is lost unless you had reverse logical replication or some other way to carry them back.
Exercise
Your database is 2 TB, you have only 500 GB of free disk space, and the filesystem is XFS with reflink enabled. You want fast upgrade and a safe rollback before cutover without a snapshot. Which transfer mode do you choose, and why not the other two?
Show answer
Use clone mode (--clone). It shares blocks through reflinks, so it needs little extra space and is fast, and the old cluster stays startable after the upgrade. Copy mode would need about 2 TB of extra space, which you do not have. Link mode would fit in the space, but it makes the old cluster unsafe once the new one starts, so it gives you no rollback without a separate snapshot or backup.