Sub topic 7
Checking compatibility with --check
This chapter assumes you can start and stop clusters with pg_ctl, you know what a data directory is, and you have already built the new empty cluster from the previous chapter. It covers what pg_upgrade --check verifies, how to read its output, and how to turn it into a repeatable rehearsal before the maintenance window.
What --check does
pg_upgrade --check runs the compatibility checks of a real upgrade and then stops. It does not change the old cluster. It does not copy, link or clone any data files, and it does not restore anything into the new cluster. Nothing is committed to the upgrade, so you can run it as often as you like.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--checkRun it as the operating system user that owns both data directories, normally postgres. Stop both clusters first. pg_upgrade can also run --check against an old cluster that is still running, which is handy for an early look at a live system. Some checks are limited in that mode, so do your final rehearsal with both servers stopped. The new cluster must always be stopped.
The command's exit status is zero when every check passes and nonzero when one fails. That makes it easy to put in a script or a pipeline step.
What the checks cover
The output starts with Performing Consistency Checks. It then prints one line per check, each ending in ok or a failure message. The exact list changes between versions, but the checks fall into a few groups.
| Group | What is verified | Typical failure |
|---|---|---|
| Cluster settings | Both clusters have the same encoding, locale settings and data checksum setting, and the new cluster is empty. | Encoding or locale mismatch, or a new cluster that already holds user data |
| Server state | No prepared transactions are pending, and no unsupported replication slots exist. | Leftover rows in `pg_prepared_xacts` |
| Schema contents | No user table uses a data type that `pg_upgrade` cannot carry over. | Columns of types such as `regproc` or `regoper` |
| Extensions and libraries | Every shared library referenced by the old cluster can be loaded by the new binaries. | An extension installed for 16 but not for 17 |
| Transfer mode | With `--link` or `--clone`, the filesystem supports the operation between the two directories. | The two data directories are on different filesystems |
| Environment | Directories exist and are accessible, and the servers can be started to run the checks. | Wrong `--old-bindir`, or a stale `postmaster.pid` |
The transfer-mode check is worth running with the same flags you plan to use in production. --check alone only proves that copy mode would work. Add --link or --clone to the check command and pg_upgrade confirms that the hard links or reflinks can actually be created.
/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=4 \
--checkReading a successful run
A passing run ends with a line like this:
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
Checking database user is the install user ok
Checking database connection settings ok
Checking for prepared transactions ok
Checking for system-defined composite types in user tables ok
Checking for reg* data types in user tables ok
Checking for presence of required libraries ok
Checking for new cluster tablespace directories ok
*Clusters are compatible**Clusters are compatible* means that none of the known blockers were found. It is not a guarantee that the upgrade will succeed. --check does not restore the schema into the new cluster, so a problem that only appears while recreating objects can still stop the real run. It also cannot see disk space problems that appear partway through a copy.
Reading a failed run
When a check fails, pg_upgrade prints a short explanation and names a report file. The report file lists the exact objects that caused the problem.
Checking for presence of required libraries fatal
Your installation references loadable libraries that are missing from the
new installation. You can add these libraries to the new installation,
or remove the functions using them from the old installation. A list of
problem libraries is in the file:
/data/pg17/pg_upgrade_output.d/20261127T231448.123/loadable_libraries.txt
Failure, exitingReport files live under pg_upgrade_output.d inside the new data directory, in a subdirectory named with a timestamp. The directory also holds the detailed logs for the run. Read the report, not just the summary line. For a missing library it names the library and the database. For a data type problem it lists each table and column.
Fix problems at the source:
- A missing library means you install the extension package for the new version on the server, then rerun.
- A bad column type means you change the column in the old cluster, for example with
ALTER TABLE ... ALTER COLUMN ... TYPE, then rerun. - A mismatch in encoding, locale or checksums means you delete the new cluster and recreate it with a matching
initdb.
A failed check can stop at the first failing step or report several at once, depending on the check. After you fix one problem, always rerun the full --check, because a later check may have been hidden by the earlier failure.
Finding problems before the check does
You do not need to wait for pg_upgrade to find problems. Two queries catch common blockers directly. Run them in every database in the old cluster.
Prepared transactions block the upgrade until you commit or roll them back:
SELECT gid, prepared, owner, database FROM pg_prepared_xacts;Columns that use some of the reg* types that pg_upgrade rejects can be listed like this. The list of types in the query is an example. The report file from --check is the authority on which types your version rejects.
SELECT n.nspname, c.relname, a.attname, a.atttypid::regtype AS type
FROM pg_attribute a
JOIN pg_class c ON c.oid = a.attrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE a.atttypid IN ('regproc'::regtype, 'regprocedure'::regtype,
'regoper'::regtype, 'regoperator'::regtype,
'regconfig'::regtype, 'regdictionary'::regtype)
AND c.relkind IN ('r', 'm')
AND NOT a.attisdropped
AND n.nspname NOT IN ('pg_catalog', 'information_schema');To compare the settings that must match, run this on both clusters and compare the output:
SELECT datname,
pg_encoding_to_char(encoding) AS encoding,
datcollate,
datctype
FROM pg_database
ORDER BY datname;Also compare the checksum setting with SHOW data_checksums;. In older releases the two clusters must agree on it.
Common causes of a failing or confusing check
| Symptom | Likely cause | What to do |
|---|---|---|
| `could not connect to source postmaster` | The old cluster is not startable. This can be a stale `postmaster.pid` or a wrong `--old-bindir`. | Confirm the paths. Start and cleanly stop the old cluster once with its own binaries. |
| `connection to database failed` with an authentication error | `pg_upgrade` starts temporary servers and connects as the install user, and `pg_hba.conf` rejects that connection. | Allow local `trust` or `peer` access for the install user during the upgrade, or provide a `.pgpass` file. |
| New cluster is not empty | You already created databases or loaded data in the new cluster. | Run `initdb` again into a fresh directory. Do not load data first. |
| Hard-link or clone check fails | The two data directories are on different filesystems, or the filesystem does not support reflinks. | Put both under the same mount, or use copy mode. |
| Library not found | The extension's packages exist only for the old major version. | Install the new-version packages, then rerun. |
| Old cluster was not shut down cleanly | A crash left the cluster needing recovery. | Start it, let recovery finish, then stop it with a normal shutdown. |
Making the check a rehearsal
A check on a development machine proves less than a check on a copy of production. For a risky upgrade, restore a recent base backup to a spare server, create a matching new cluster there, and run the full upgrade with the flags you intend to use. This tests timing, extension packages, authentication and filesystem support together.
A useful pre-window routine:
- Run
--checkon the real servers a few days ahead, using the final flags. This leaves time to fix problems. - Repeat the check inside the maintenance window, right before the real run. Schema changes or new extensions installed since the first check can introduce new failures.
- Keep the full output. If something later behaves differently, you have a record of what the tool verified.
The next chapter runs the actual upgrade.
Exercise
Your --check run prints fatal for "Checking for presence of required libraries" and names loadable_libraries.txt. List the steps you take before running the check again.
Show answer
Open the report file under pg_upgrade_output.d in the new data directory and note which library and which database each entry names. Install the matching extension packages for the new major version on the server. Alternatively, remove the functions that use the library from the old cluster if you no longer need them. Then rerun the full pg_upgrade --check, because later checks may have been hidden by this failure.
Exercise
A colleague says "--check printed *Clusters are compatible*, so the upgrade cannot fail." Give two reasons this is too strong a claim, and one way to reduce the remaining risk.
Show answer
First, --check does not restore the schema into the new cluster, so problems that appear only while recreating objects are not tested. Second, it cannot detect problems that occur during the data transfer, such as running out of disk space in copy mode. To reduce the risk, rehearse the full upgrade on a restored copy of production, using the same flags and extension packages as the real run.