Sub topic 7
Checking PostgreSQL Compatibility with `pg_upgrade --check`
This chapter assumes that you understand PostgreSQL clusters, data directories, pg_ctl, Linux commands, backups, extensions, and streaming replication. You should also know the old cluster's version, data directory, port, tablespaces, and installed extensions.
The pg_upgrade utility moves a PostgreSQL cluster from one major version to another. Its --check mode performs compatibility checks without changing either cluster. Run it before the maintenance window so that you can fix problems while the old system is still serving traffic.
What `--check` verifies
A major version upgrade needs both an old cluster and a newly initialized cluster. The clusters must use compatible settings, installed extensions, and supported data layouts. pg_upgrade --check compares the clusters and examines objects that could prevent the upgrade.
Typical checks include:
- Whether the old and new server binaries are present and usable.
- Whether the old and new data directories are valid.
- Whether both clusters are shut down.
- Whether the operating-system user running the command owns or can access the required files.
- Whether database names, tablespaces, and relation files can be processed.
- Whether extensions have compatible control files and versions.
- Whether unsupported data types, objects, or obsolete settings exist.
- Whether prepared transactions or other objects require special handling.
The check does not prove that every application query will work after the upgrade. It tests the upgrade mechanism and cluster metadata. Application testing remains necessary.
Running the compatibility check
Stop both clusters before running the command. The old cluster must be cleanly shut down, and the new cluster must already exist but must not contain data from another cluster.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--checkReplace the paths with the paths on your host. The command uses the new version's pg_upgrade binary. It receives the binary and data-directory paths for both versions.
A successful check reports that the clusters are compatible. It does not perform the upgrade, copy files, or alter database contents. Save the complete terminal output and the generated log files as part of the change record.
Reading failures
A failure usually identifies a specific prerequisite. Read the first error carefully, then inspect the detailed log named by pg_upgrade. Do not ignore warnings because a warning may become an outage when an application depends on the affected object.
| Finding | What it usually means | Typical response |
|---|---|---|
| Old cluster is running | `pg_upgrade` cannot safely inspect a changing cluster | Stop it cleanly and retry |
| New cluster is not initialized | The target data directory is missing required structure | Run the new version's `initdb` |
| Extension is missing | The new installation lacks a required extension library or control file | Install a matching extension package |
| Tablespace path is unavailable | A directory or mount differs between environments | Mount or create the path with correct ownership |
| Unsupported object or setting | The old cluster contains data requiring manual action | Follow the log's remediation instructions |
Some extension problems require more than installing a package. The extension's shared library, SQL files, and control file must be available to the new server. After the upgrade, you may also need to run ALTER EXTENSION ... UPDATE for supported extensions.
A check can also expose prepared transactions or logical-replication objects that need a planned action. Resolve the reported condition according to the PostgreSQL release documentation and your application's requirements. Never delete database objects merely to make the check pass unless you have confirmed that they are no longer needed.
A repeatable check cycle
Use this sequence:
- Record the old cluster's version, settings, extensions, tablespaces, and backup status.
- Install the new PostgreSQL binaries and required extension packages.
- Initialize the new cluster with compatible encoding, locale, and data-checksum choices.
- Stop both clusters cleanly.
- Run
pg_upgrade --check. - Read all output and logs, then correct every failure.
- Run the check again until it succeeds.
- Preserve the successful output and the exact command for the maintenance run.
Exercise
Exercise
Why should you run pg_upgrade --check after installing extensions but before the maintenance window?
Show answer
The check confirms that the new installation can process the old cluster and exposes missing extension files or incompatible objects while there is still time to correct them without extending downtime.