Sub topic 2
Choosing an Upgrade Approach
Assume you know basic PostgreSQL administration, can work on Linux, and understand clusters, data directories, pg_ctl, backups, extensions, and streaming replication. This chapter compares three ways to move databases between PostgreSQL major versions:
- Dump and restore
pg_upgrade- Logical replication
A major version upgrade changes PostgreSQL's internal data formats or server behavior. A data directory created by PostgreSQL 16 cannot usually be started directly by PostgreSQL 17. A minor upgrade, such as 16.2 to 16.3, is different. Minor releases keep the same on-disk format and normally use the same data directory.
Dump and restore
Dump and restore reads database objects and rows through PostgreSQL's SQL interface. It writes portable SQL or an archive file, then loads that output into a new cluster.
A typical custom-format migration looks like this:
pg_dump -Fc -d appdb -f /backup/appdb.dump
createdb -T template0 appdb_new
pg_restore --exit-on-error --clean --if-exists \
-d appdb_new /backup/appdb.dumpYou must also preserve roles, tablespaces, and other cluster-wide objects. For example, pg_dumpall --globals-only can export roles and tablespace definitions.
Dump and restore is often the safest choice when:
- You are moving between operating systems or CPU architectures.
- You need to change database encoding, tablespaces, or other layout decisions.
- The source cluster is small enough that a long outage is acceptable.
- You want a logically clean migration that does not depend on old data files.
- The PostgreSQL versions have compatibility concerns that
pg_upgradecannot resolve.
Its main cost is time. Every table row is read, serialized, transferred, and written again. Indexes and statistics also need to be rebuilt. The process requires downtime unless you add a separate change-capture or replication design.
`pg_upgrade`
pg_upgrade creates a new cluster with the target PostgreSQL version and converts the old cluster by reusing or copying its existing data files. It checks catalog and extension compatibility rather than exporting every row through SQL.
Choose pg_upgrade when:
- The outage must be short.
- The old and new clusters run on compatible systems.
- The data directory is large.
- Required extensions provide compatible versions in both installations.
- You can provision space for the new cluster and keep a tested backup.
The upgrade still requires a maintenance window. Applications must stop writing, and both clusters must meet the tool's compatibility requirements. pg_upgrade does not solve incompatible extensions, missing libraries, unsupported data types, or application changes.
The transfer mode affects both speed and rollback. Copying files is slower but leaves the old cluster independent. Linking files is fast and space-efficient, but starting the old cluster after the new cluster has modified linked files can corrupt the migration path. Cloning may provide copy-on-write behavior when the filesystem supports it.
Logical replication
Logical replication sends changes as logical row-level operations, such as inserts, updates, and deletes. It connects a source cluster to a target cluster, which can run a different major version.
It is useful when:
- Downtime must be very short.
- The target cluster needs a different operating system or storage design.
- You can run both clusters during synchronization.
- The application can tolerate a planned cutover.
Logical replication requires more planning than pg_upgrade. Native logical replication does not automatically reproduce every schema change. You must manage tables, indexes, roles, extensions, sequences, large objects, and permissions separately. Some tables need primary keys or another suitable replica identity for updates and deletes.
A common process is:
- Create the target schema and install compatible extensions.
- Configure a publication on the source.
- Configure a subscription on the target.
- Let the initial table copy finish.
- Monitor replication lag.
- Stop writes, synchronize remaining changes, and redirect clients.
Compare the approaches
| Approach | Downtime | Migration speed | Main strength | Main risk |
|---|---|---|---|---|
| Dump and restore | Long | Slow for large data sets | Portable and conceptually simple | Extended outage |
| `pg_upgrade` | Short | Fast | Reuses existing data files | Version, extension, and platform constraints |
| Logical replication | Very short cutover | Initial copy can be long | Supports parallel old and new clusters | Schema and object synchronization |
Use dump and restore when portability and isolation matter more than downtime. Use pg_upgrade when the environment is compatible and the cluster is large. Use logical replication when the business requires a near-continuous service and can support a more complex migration.
Exercise
A 20 TB PostgreSQL cluster must move to a compatible newer PostgreSQL release, and the business allows only one hour of downtime. Which approach is the first candidate, and what must you verify before choosing it?
Show answer
Evaluate pg_upgrade first. Verify operating-system and architecture compatibility, available space, extension support, backups, transfer-mode behavior, and the measured upgrade time in a production-like rehearsal. Consider logical replication if the one-hour window is still too short.