Sub topic 1
Why Major Version Upgrades Are Different
Prerequisites
You should be comfortable with the following before you start:
- Basic PostgreSQL administration: clusters, data directories, and
pg_ctl. - The Linux command line, including file paths and running commands with options.
- Taking backups with
pg_dumporpg_basebackup. - Basic ideas about extensions and streaming replication.
Why major version upgrades are different
PostgreSQL releases come in two kinds. Minor releases, such as 16.2 to 16.3, keep the same on-disk format. You can replace the binaries and restart. Major releases, such as 16 to 17, can change the data file layout and the system catalogs. A 17 server cannot read a 16 data directory as it stands, so the data must be converted or copied into a new cluster.
Choosing an upgrade approach
Three approaches are common:
| Approach | Downtime | Disk space needed | Rollback |
|---|---|---|---|
| Dump and restore | Long, grows with data size | Enough for the dump file | Old cluster untouched |
| `pg_upgrade` | Short, mostly independent of data size | Little with `--link`, full copy otherwise | Depends on transfer mode |
| Logical replication | Very short cutover | Full second copy of the data | Old cluster kept in sync |
Use pg_upgrade when a short maintenance window is acceptable and the schema is stable.
What pg_upgrade does under the hood
pg_upgrade runs with the old and new binaries together. It starts the old server in read-only mode to export the schema, then creates matching objects in the new cluster. Next, it moves the table and index files across using the transfer mode you choose. Finally, it updates the new cluster's catalogs so the data is visible there. Both clusters must be stopped while it runs.
Copy, clone or link: choosing a transfer mode
Pick the mode based on speed and how much you need a fallback:
- Copy (the default): duplicates every file. It is the safest, because the old cluster stays intact, but it is the slowest and needs free space equal to the data size.
- Clone (
--clone): uses copy-on-write reflinks on filesystems such as Btrfs, XFS with reflink, or APFS. It is fast and uses little extra space, and the old cluster stays usable. - Link (
--link): creates hard links. It is the fastest, but once the new cluster starts, the old one must not be started again.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--linkExercise
You have a 2 TB cluster, a filesystem without reflink support, and a rule that you must be able to return to the old cluster after the upgrade. Which transfer mode do you choose, and why?
Show answer
Copy mode. Link mode would make the old cluster unusable once the new one starts, and clone mode needs reflink support, which this filesystem lacks. Copy mode keeps the old cluster intact, at the cost of time and roughly 2 TB of free space.