Sub topic 10
Planning for Rollback
This chapter assumes you know PostgreSQL clusters, data directories, pg_ctl, backups, pg_dump or pg_basebackup, extensions, and streaming replication. The goal is to decide how you will return to the old major version if the upgraded cluster fails.
A rollback is different from fixing a failed command. During an upgrade, you can usually correct an error while both clusters are stopped. After the new cluster accepts production writes, returning to the old cluster can require data recovery or logical replication.
Identify the point of no return
Before the maintenance window, define these states:
- Before
pg_upgraderuns, the old cluster is the production system and the new cluster is unused. - After a successful upgrade but before application writes, both clusters can be inspected, but only one should be started for normal service.
- After the new cluster receives writes, the old cluster no longer contains those changes. Switching back can lose data unless you separately capture and replay the changes.
- After you remove the old cluster or its backup, a simple rollback is no longer possible.
The safest rollback plan keeps the old data directory intact, preserves a tested backup, and delays cleanup until the new cluster has passed its validation period.
Choose a transfer mode
pg_upgrade can copy, clone, or hard-link the relation files. A relation file stores table or index data on disk. The transfer mode affects both upgrade speed and rollback options.
| Mode | Upgrade speed | Disk usage | Simple rollback |
|---|---|---|---|
| Copy | Lower | Higher | Best |
| Clone | High | Low initially | Usually good, if supported and tested |
| Link | Highest | Lowest | Poor after the new cluster starts |
With the default copy mode, the old cluster's files remain separate. If validation fails before application writes, stop the new cluster and start the old one. This uses more storage and takes longer, but it gives the clearest recovery path.
Clone mode uses filesystem copy-on-write support. The new and old clusters initially share physical blocks, while later writes create separate blocks. It can provide fast upgrades without immediately duplicating all data. Verify that the filesystem supports the required clone operation and test its behavior before the maintenance window. Do not treat clone mode as a substitute for a backup.
Link mode creates hard links instead of copying files. The old and new clusters then refer to the same physical files. Once the new cluster starts and changes those files, the old cluster must not be started. A link-mode rollback is therefore limited to failures found before the new cluster changes shared files. Use link mode only when you have a separate recovery plan and can accept this restriction.
> callout warning > Never delete the old cluster, its backup, or its rollback evidence immediately after a successful upgrade. Keep them until the new version has operated through an agreed observation period.
Design the rollback procedure
Write the procedure before the upgrade and assign one person to approve the decision. A basic pre-write rollback looks like this:
- Stop application traffic or put the application in maintenance mode.
- Stop the new cluster and verify that no PostgreSQL process still uses its data directory.
- Confirm whether the transfer mode permits the old cluster to start safely.
- Start the old cluster with its original configuration.
- Point the application back to the old port, socket, or service name.
- Run read and write health checks.
- Record the failure before attempting another upgrade.
For a copy-mode upgrade, the commands might look like this:
/opt/pg17/bin/pg_ctl -D /data/pg17 stop -m fast
/opt/pg16/bin/pg_ctl -D /data/pg16 -l /data/pg16/server.log start
/opt/pg16/bin/pg_isready -D /data/pg16Do not run these commands blindly. Confirm the correct cluster, service manager, ports, and ownership first. If the new cluster has already accepted writes, starting the old cluster does not restore those writes. Instead, preserve the new cluster, determine which changes must be retained, and choose a recovery method such as logical replication, application-level reconciliation, or restoring from a backup.
Include backups and replicas
Take a backup that you can restore independently of the upgrade process. A base backup is useful for recovering the complete cluster, while logical dumps can provide a portable copy of selected databases or objects. Test restoration before the maintenance window.
Streaming replicas need their own plan. A physical standby from the old major version cannot normally continue streaming from the upgraded primary. Keep an old-version standby available for emergency rollback, or rebuild the standby from the upgraded primary after validation. Do not promote an old standby after new-version writes unless you understand and accept the resulting data loss.
Exercise
You used copy mode, the new cluster fails validation before any application writes, and the old data directory is intact. What is the safest rollback action?
Show answer
Stop the new cluster, verify that no new PostgreSQL process is using the old data directory, start the old cluster, redirect the application to it, and investigate the new-cluster failure.