Sub topic 3
What pg_upgrade Does Under the Hood
This chapter assumes basic PostgreSQL administration, Linux command-line skills, and familiarity with clusters, data directories, extensions, and backups. You should also know that a PostgreSQL major version changes database internals and system catalogs. A new server cannot safely open an old major version's data directory directly.
The working model
A PostgreSQL cluster is a directory containing databases, system catalogs, relation files, and configuration-related files. A major upgrade creates a second cluster with the new PostgreSQL binaries and then makes the user data available to that cluster.
pg_upgrade uses two clusters:
- The old cluster contains the running version's data.
- The new cluster was initialized by the new version's
initdb. - The old and new binaries provide the tools and server programs for each version.
The new cluster receives new system catalogs. pg_upgrade checks whether the old cluster's database objects and stored data can work with those catalogs. It then transfers relation files where possible and generates SQL scripts for objects that must be recreated or adjusted.
pg_upgrade does not normally copy every row through SQL. That is why it can be much faster than a complete dump and restore. It preserves the physical contents of tables and indexes when the new version can use them, while the new server supplies the new catalog structure.
What happens during an upgrade
The process has several logical stages:
pg_upgradeverifies the old and new data directories, executable paths, operating-system permissions, and PostgreSQL versions.- It checks databases, extensions, tablespaces, and data types for known incompatibilities.
- It prepares the new cluster's catalog and records how objects map from the old cluster to the new one.
- It transfers or clones relation files according to the selected mode.
- It recreates global objects and writes scripts for post-upgrade work.
- It reports commands for updating configuration, analyzing tables, and removing temporary files.
The command below performs only the validation stage. Both clusters must be stopped.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--checkA successful check means that pg_upgrade found no known blocker. It does not prove that every application query, extension function, or authentication rule will behave exactly as before. Application testing remains necessary.
How data files are transferred
The transfer mode controls how relation files move from the old cluster to the new one.
| Mode | Mechanism | Main consequence |
|---|---|---|
| Copy | Reads and writes separate files | Uses more time and storage, but preserves the old cluster |
| Link | Creates hard links to the same files | Fast, but the old cluster cannot be safely used afterward |
| Clone | Uses filesystem copy-on-write cloning | Fast when supported, with more rollback flexibility than links |
Copy mode is the most conservative choice. Link mode is often the fastest choice on the same filesystem, but it changes the rollback plan. A hard link gives two directory entries for the same underlying file. If either cluster modifies that file, both directory entries refer to the changed data.
Clone mode depends on filesystem and operating-system support. The clone initially shares storage internally, but later writes create separate blocks. Confirm that the filesystem supports the required cloning operation before relying on it.
>warning >Never start the old cluster after completing a link-mode upgrade. The old and new clusters share relation files, so writes from the old server can corrupt the upgraded cluster.
Why the new cluster matters
The new cluster is not an empty directory created by copying the old one. initdb creates system catalogs in the format expected by the new server. These catalogs describe databases, roles, tables, indexes, privileges, and other objects.
pg_upgrade bridges the two catalog formats. It does not eliminate differences between major versions. Unsupported extensions, obsolete data types, incompatible collations, prepared transactions, or configuration changes can still stop the process or require manual work.
The result is therefore a coordinated migration of metadata and physical data, not a simple directory rename.
Exercise
Why can pg_upgrade transfer table files without transferring the old system catalogs?
Show answer
The new cluster needs system catalogs created by the new PostgreSQL version. pg_upgrade validates the old objects, maps them to the new catalogs, and transfers compatible relation data separately.