Sub topic 1
Why major version upgrades are different
Prerequisites
This chapter assumes you can do the following:
- Run a PostgreSQL cluster and find its data directory (
PGDATA). - Start and stop the server with
pg_ctlor your service manager. - Work comfortably on the Linux command line.
- Take backups with
pg_dumporpg_basebackup. - Understand at a basic level what an extension is and what streaming replication does.
No upgrade tooling is needed yet. This chapter explains why upgrading across major versions needs its own procedure. Later chapters cover pg_upgrade itself.
Reading a PostgreSQL version number
Since PostgreSQL 10, a version number has two parts: MAJOR.MINOR. In 16.4, the major version is 16 and the minor version is 4. Before version 10 there were three parts, and the major version was the first two, as in 9.6.24. If you manage old systems, keep this in mind. Going from 9.5 to 9.6 was a major upgrade, even though it looks like a small step.
You can read both parts from a running server:
SHOW server_version;
SELECT current_setting('server_version_num')::int / 10000 AS major,
current_setting('server_version_num')::int % 10000 AS minor;server_version_num is a single integer such as 160004. Integer division by 10000 gives the major version (16). The remainder gives the minor version (4). This works for PostgreSQL 10 and later. Scripts can use it to branch on version without parsing text.
Minor releases: swap the binaries and restart
The PostgreSQL project releases minor versions on a regular schedule, roughly every quarter, with extra releases when a serious security or data-loss bug appears. Each major version is supported for five years after its first release. During that time it only receives minor releases.
The project's rule for minor releases is that they contain bug fixes and security fixes only. They never change the format in which data is stored. Because of that rule, a minor update is simple:
- Install the new packages. This replaces the server binaries.
- Restart the server.
- Confirm the new version with
SELECT version();.
The data directory is untouched. The new binaries read the old files directly, and you can usually go back to the previous minor version the same way.
Always read the release notes before a minor update. Occasionally a release fixes a bug that damaged an index or a catalog entry. In that case the notes tell you to run REINDEX or a specific SQL statement afterwards. The update is still a binary swap, but the notes can add a follow-up step.
Major releases: the data directory is not reusable
A major release comes about once a year. It adds features, changes behavior, and changes how the server stores things internally. The server only opens a data directory created by the same major version. The first file it checks is PG_VERSION.
cat /data/pg16/PG_VERSION
/opt/pg16/bin/pg_controldata -D /data/pg16 | grep -E 'pg_control version number|Catalog version number'The first command prints 16. The second shows two numbers stored in the control file. The catalog version number identifies the exact layout of the system catalogs. It changes in almost every development cycle.
If you start a PostgreSQL 17 server on a PostgreSQL 16 data directory, it refuses to run. The error looks like this:
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 16, which is not compatible with this version 17.0.The check exists because the files really are incompatible. Letting the server try anyway could corrupt data.
What actually changes between major versions
Three areas change, and they explain why one procedure cannot cover everything.
System catalogs
The system catalogs are the tables that describe your database: pg_class, pg_attribute, pg_proc, pg_type and others. Major releases add, remove and reorder columns in them. They also add built-in functions and types, and the identifiers (OIDs) of built-in objects can change. The new server's code is written against the new catalog layout. It cannot interpret the old catalogs.
Internal formats
Several formats can change in a major release:
- The write-ahead log (WAL) record format.
- The
pg_controlfile layout. - Page layouts and internal structures of some index types, and the on-disk representation of some data types.
Physical streaming replication ships WAL, so a primary and a standby must run the same major version. You cannot use a version 17 standby to follow a version 16 primary. This matters for the replica chapter later.
Behavior and defaults
Major releases also remove features and change defaults. These changes can break applications even when the data moves over correctly. Examples from past releases:
| Release | Change |
|---|---|
| 10 | Versioning scheme changed to two parts. |
| 12 | `recovery.conf` was removed. Its settings moved into `postgresql.conf`, and the `standby.signal` and `recovery.signal` files were introduced. |
| 14 | `password_encryption` now defaults to `scram-sha-256`. Clients and `pg_hba.conf` entries relying on MD5 may need attention. |
| 15 | Ordinary users no longer have `CREATE` on the `public` schema by default. |
You should check the release notes of every major version between your current one and your target. Skipping versions is allowed, but you must read the notes for all the versions you skip.
The one thing that usually stays the same
User table data is stored in files of fixed-size pages, normally 8 kB. The page layout for ordinary tables has been very stable across major versions. This stability is why pg_upgrade can exist at all. The tool does not convert your table data. It builds the new cluster's catalogs from scratch, then reuses or copies the existing data files, adjusting the catalog entries so they point to them.
The consequence is easy to remember:
- Catalogs: always rebuilt for the new version.
- User data files: reused as they are, in the normal case.
Some exceptions exist, and the release notes list them. A data type or index type may change its storage format, in which case the notes tell you to rebuild affected indexes or re-create the data after the upgrade. Later chapters cover how pg_upgrade checks for the worst of these.
Installing versions side by side
Because the data directory cannot be reused, a major upgrade needs two sets of binaries at the same time: the old ones to read the old cluster, and the new ones to create and run the new cluster. Debian-style packages install each major version in its own directory (/usr/lib/postgresql/16/bin, /usr/lib/postgresql/17/bin). Red Hat-style packages from the PostgreSQL project use /usr/pgsql-16/bin and /usr/pgsql-17/bin. The examples in this tutorial use /opt/pg16 and /opt/pg17, which are placeholders for whatever your system uses.
Do not upgrade the package in place if your packaging replaces the old binaries. Without the old binaries you lose the ability to start the old cluster, which also removes your easiest rollback.
Extensions are part of the problem
Extensions complicate a major upgrade. An extension has two parts: SQL objects stored in the database catalogs, and a compiled shared library (a .so file) that must exist for each major PostgreSQL version. PostGIS, pg_stat_statements, pgcrypto and others each need a build for the new version, installed before you upgrade. Check what you have now:
SELECT e.extname,
e.extversion AS installed_version,
a.default_version AS available_version
FROM pg_extension e
LEFT JOIN pg_available_extensions a ON a.name = e.extname
ORDER BY e.extname;Run this on the old cluster and write down the result. Then confirm that packages for every listed extension exist for the new major version. A missing library is one of the most common reasons an upgrade stops partway through, and finding out during the maintenance window is expensive.
Why you cannot just "do it in place"
A reasonable question is why the project does not ship a simple in-place converter that rewrites the files. There are a few reasons.
- Safety. A converter that edits your only copy of the data can leave you with a half-converted cluster if it fails. Approaches that write a separate new cluster leave the original intact.
- Size. Rewriting every table and index would take as long as copying them, and for multi-terabyte databases that is hours.
- Cost of compatibility. Freezing the catalogs and internal formats to avoid conversion would block improvements. Keeping them free to change is what lets each release add features and performance work.
The project's answer is a set of approaches that each build a new cluster. They differ in how the data gets there and how long applications are offline. The next chapter compares them.
What this means for planning
Putting the pieces together, a major upgrade always involves these decisions:
- Downtime tolerance. Minutes, an hour, or close to none. This decides which approach you can use.
- Rollback. Whether you keep an intact copy of the old cluster, and for how long.
- Extensions and tooling. Whether everything that runs in, and around, the database has a build for the new version. This includes monitoring agents, connection poolers and backup tools.
- Application compatibility. Whether queries, drivers, and authentication settings work with the new version's behavior and defaults.
- Replicas. Standbys cannot follow across major versions, so they have to be rebuilt or upgraded as a separate step.
A minor update asks none of these questions. That difference is why teams treat major upgrades as projects, with testing, a rehearsal on a copy of production data, and a written runbook.
Exercise
A server reports server_version_num = 150009. What are its major and minor versions? Would moving to 15.10 require a new data directory? Would moving to 16.0 require one?
Show answer
The major version is 15 (150009 / 10000 using integer division) and the minor version is 9 (150009 % 10000). Moving to 15.10 is a minor update, so you install the new binaries and restart on the same data directory. Moving to 16.0 is a major upgrade, so it needs a new cluster initialized by the version 16 binaries, with the data moved over by dump and restore, pg_upgrade or logical replication.
Exercise
You start a PostgreSQL 17 server on a data directory created by PostgreSQL 16. What happens, which file does the server check first, and why is the check necessary?
Show answer
The server refuses to start with "database files are incompatible with server". It checks PG_VERSION in the data directory (and the control file). The check is necessary because the system catalogs and other internal formats can differ between major versions, so the new server could misread the old files and corrupt data.
Exercise
Name two reasons why a physical streaming standby on version 16 cannot follow a primary on version 17.
Show answer
Any two of the following. The WAL record format can change between major versions. The pg_control layout and the system catalog layout differ. Physical replication copies the primary's files and WAL byte for byte, so both sides must use the same major version.