Sub topic 8
Running the PostgreSQL Major Version Upgrade
This chapter assumes that you understand PostgreSQL clusters, data directories, pg_ctl, backups, extensions, and basic Linux commands. It also assumes that you have already reviewed the release notes, installed both PostgreSQL versions, initialized the new cluster, and completed a successful pg_upgrade --check.
A major upgrade uses two clusters:
- The old cluster contains the databases and runs the current PostgreSQL version.
- The new cluster contains the upgraded data and runs the target version.
Both clusters must be stopped before the upgrade starts. Run the command as the operating-system user that owns the PostgreSQL files, usually postgres.
Confirm the maintenance state
Before running the upgrade, record the connection settings and check that the old cluster is stopped:
/opt/pg16/bin/pg_ctl -D /data/pg16 status
/opt/pg17/bin/pg_ctl -D /data/pg17 statusStop either cluster if it is running:
/opt/pg16/bin/pg_ctl -D /data/pg16 stop -m fast
/opt/pg17/bin/pg_ctl -D /data/pg17 stop -m fastA fast shutdown disconnects clients and stops active sessions without waiting indefinitely for long-running work. Confirm that no PostgreSQL process still uses either data directory. Also disable automatic service startup temporarily. Otherwise, a service manager could start the old cluster while the upgrade is working.
Run the upgrade
The basic command specifies the executable directories and data directories:
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--link \
--jobs=4The command uses the PostgreSQL 17 pg_upgrade program. --old-bindir and --new-bindir identify the binaries for each version. The data directory options identify the old and new clusters.
The --jobs=4 option allows several databases or tables to be processed at the same time. Choose a value that fits the available CPU, storage bandwidth, and maintenance window. More workers do not always make the upgrade faster, especially on slow or heavily contended storage.
The --link option creates hard links to existing relation files instead of copying them. This can reduce the upgrade time and required disk space. It also changes rollback planning: after the new cluster starts, do not start the old cluster. The two clusters would share files, and writes from either cluster could corrupt the other cluster.
Without --link, pg_upgrade copies the files. The process usually takes longer and needs more temporary storage, but the old cluster remains a safer rollback source until you remove it. Some filesystems support clone modes that avoid an immediate full copy while preserving copy-on-write behavior. Verify the filesystem and PostgreSQL version support before relying on them.
Follow the generated instructions
When the command succeeds, pg_upgrade prints the location of reports and scripts. Read the output and save it with the maintenance records:
ls -l pg_upgrade_output.d
cat pg_upgrade_output.d/pg_upgrade_server.logThe exact directory is normally created in the directory from which you ran pg_upgrade. The reports contain warnings, object counts, and commands for post-upgrade work. Do not delete them until validation is complete.
Start and validate the new cluster
Start the new cluster with its own log file:
/opt/pg17/bin/pg_ctl -D /data/pg17 \
-l /data/pg17/server.log startCheck that it accepts connections, then inspect the databases and extensions:
/opt/pg17/bin/psql -d postgres -c "SELECT version();"
/opt/pg17/bin/psql -d postgres -c "\l"Remove the accidental leading space before the second command if you copy these commands into a shell. Next, run the generated analyze script or analyze all databases in stages:
/opt/pg17/bin/vacuumdb --all --analyze-in-stagesThis rebuilds planner statistics. Until statistics exist, the query planner may choose poor execution plans even though the data is correct.
Exercise
Why should you avoid starting the old cluster after a successful upgrade performed with --link?
Show answer
The old and new clusters share hard-linked relation files. Writes from either cluster could change files used by the other cluster and corrupt data.