Sub topic 1
Why Major Version Upgrades Are Different
A PostgreSQL major version upgrade moves a cluster from one major release to another, such as PostgreSQL 16 to PostgreSQL 17. A major release can change the database server's behavior, system catalogs, on-disk data structures, and supported features. Because of these changes, PostgreSQL does not normally let the new server start on the old cluster's data directory.
This chapter assumes that you understand PostgreSQL clusters, data directories, pg_ctl, basic Linux commands, backups, extensions, and streaming replication.
Prerequisites
You should be able to:
- Identify a PostgreSQL cluster and its data directory.
- Stop and start a server with
pg_ctl. - Create backups with
pg_dumporpg_basebackup. - Read PostgreSQL configuration files.
- Explain the basic purpose of extensions and replicas.
Major and minor releases
PostgreSQL version numbers use a major release and, depending on the release series, a minor release number. A change from PostgreSQL 16 to PostgreSQL 17 is a major upgrade. A change from PostgreSQL 16.2 to PostgreSQL 16.3 is a minor update.
A minor update primarily fixes bugs and security problems. It is designed to keep the existing data format compatible. You normally stop the server, replace the PostgreSQL binaries, and start the same cluster again.
A major upgrade can change:
- System catalog definitions.
- Internal page and index formats.
- SQL behavior and reserved words.
- Write-ahead log behavior.
- Extension interfaces.
- Planner and executor behavior.
- Configuration parameters and defaults.
The system catalogs are tables that describe databases, tables, columns, indexes, roles, and other database objects. PostgreSQL server code expects these catalogs and stored data to have the format used by its own major release.
Why the old data directory cannot be reused
A data directory contains more than table rows. It includes system catalogs, relation files, indexes, transaction status data, control information, configuration files, and write-ahead log files. Some of these files are tied to the major version that created them.
For example, a PostgreSQL 17 server might interpret a catalog column or an index page differently from PostgreSQL 16. If PostgreSQL 17 accepted the old directory without conversion, it could read values incorrectly or write files that PostgreSQL 16 could not understand. The result could be immediate startup failure or silent corruption.
PostgreSQL therefore records the cluster's catalog version in its control information. A server checks this information during startup. A mismatch causes the server to reject the data directory rather than risk using an incompatible format.
/opt/pg17/bin/pg_controldata /data/pg16 | grep -E 'Database system identifier|Database cluster state|Catalog version'The exact output varies by PostgreSQL release. The important point is that the directory carries metadata that the server uses to decide whether it can safely open the cluster.
What an upgrade must preserve
A major upgrade should preserve the logical contents of the cluster:
- Databases and schemas.
- Tables and table data.
- Indexes and constraints.
- Roles and permissions.
- Tablespaces.
- Extension objects, when compatible versions are installed.
It does not preserve every internal detail. PostgreSQL may rebuild indexes, regenerate planner statistics, or require extensions to be updated. Query plans also cannot be assumed to remain the same because the new server may use a different planner.
The upgrade process must also account for objects outside ordinary table storage. Tablespaces can use separate filesystem paths. Large objects, prepared transactions, replication settings, and extension files may require separate checks or migration steps.
Why a special upgrade tool is needed
A logical dump and restore can create the objects again through SQL, so it does not depend on the old physical layout. However, it can take a long time for a large database because every row is exported and inserted again.
pg_upgrade takes a different path. It creates a new cluster with the new server binaries, checks whether the old cluster is compatible, and transfers or reuses data files where possible. It then performs the catalog changes required by the new release. This avoids the full cost of replaying every row through SQL, but it requires careful preparation and a maintenance window.
Exercise
Exercise
Why can a PostgreSQL minor update usually reuse the existing data directory, while a major upgrade cannot?
Show answer
Minor releases preserve the data format within a release series, but a major release can change catalogs and on-disk structures. The new major server therefore needs a conversion or migration process before it can use the cluster safely.