Sub topic 3
After the upgrade: statistics, extensions and cleanup
This chapter assumes you have run pgupgrade, can start and stop clusters with pgctl, and can run SQL through psql. Both clusters use the same data-directory layout.
pg_upgrade does not copy planner statistics. Until you rebuild them, the planner guesses row counts and may choose slow plans. Rebuild them in stages so queries become usable early:
/opt/pg17/bin/pg_ctl -D /data/pg17 -l /data/pg17/server.log start
/opt/pg17/bin/vacuumdb --all --analyze-in-stagesExtensions need the same attention. Their libraries come from the new installation, but each database still holds the old extension version. List the ones that need updating:
SELECT name, installed_version, default_version
FROM pg_available_extensions
WHERE installed_version IS NOT NULL
AND installed_version <> default_version;For each row, connect to that database and run ALTER EXTENSION name UPDATE;.
Planning for rollback
Your rollback options depend on the transfer mode. With --link, the old and new clusters share data files, so the old cluster cannot be started again once the new one has run.
> Rollback is possible only with copy mode, or with clone mode on a filesystem that supports it. Keep the old cluster intact until you accept the new one.
If you used copy mode, stop the new cluster, then start the old one with its original binaries.
Upgrading streaming replicas
pg_upgrade changes only the primary. Each standby must be rebuilt to match the new version. The simplest reliable method is to stop the standby, use rsync to bring its data directory in line with the upgraded primary, and start it again. The PostgreSQL upgrade documentation describes the exact rsync options. Test the procedure on a non-production replica first.
Troubleshooting common failures
Most failures appear during the consistency checks. Check these first:
- Old server still running: Stop it before running pg_upgrade.
- Missing extension library: Install the matching extension build for the new version.
- Encoding or locale mismatch: Re-run initdb with the old cluster's settings.
- Non-empty new cluster: Initialize a fresh data directory.
pg_upgrade writes detailed logs to a directory it reports at the end of each run. Read them before retrying.
Exercise
You ran pg_upgrade with --link and now want to roll back. What do you do?
Show answer
You cannot start the old cluster, because its data files were shared with the new cluster and may have changed. Restore from a backup taken before the upgrade. For future upgrades, use copy mode or keep a backup if you need a rollback path.