Sub topic 11
Upgrading Streaming Replicas
This chapter assumes you understand PostgreSQL clusters, pg_ctl, pg_basebackup, replication slots, and basic streaming replication. You should also have a tested backup and a maintenance plan for the primary database.
A streaming replica receives and replays WAL from a primary server. Physical streaming replication requires the primary and standby to use the same PostgreSQL major version. A PostgreSQL 16 standby cannot continue streaming from a PostgreSQL 17 primary. Therefore, upgrading the primary also requires a plan for every physical standby.
The safest strategy: rebuild each standby
The simplest and most reliable approach is:
- Upgrade the primary with
pg_upgrade. - Validate the upgraded primary.
- Stop or isolate the old standby.
- Create a new base backup from the upgraded primary.
- Configure the new standby and start it.
- Verify that streaming and replay work.
This approach takes time proportional to the size of the database because pg_basebackup reads the data files again. It is usually easier to reason about than trying to upgrade each standby in place.
Do not expect the old standby to catch up to the new primary. Its data directory belongs to the old major version, and its WAL stream is not compatible with the new server.
Rebuild the standby from the upgraded primary
First, confirm that the upgraded primary allows replication. Check wal_level, max_wal_senders, pg_hba.conf, and any required TLS settings. If the standby used a physical replication slot, decide whether to reuse its name or create a new slot.
Stop the old standby before replacing its data directory. Preserve it temporarily if you need a rollback reference or additional diagnostics. Do not delete it until the new primary and standby have passed validation.
On the upgraded primary, create a replication slot if the new standby needs one:
SELECT pg_create_physical_replication_slot('standby17');Run pg_basebackup on the standby host. The -R option creates the standby signal file and writes connection settings. The -C option creates the replication slot named by -S on the source server.
/opt/pg17/bin/pg_basebackup \
--host=db-primary \
--username=repl \
--pgdata=/data/pg17-standby \
--write-recovery-conf \
--wal-method=stream \
--progress \
--create-slot \
--slot=standby17The command may require a password or another authentication method. Ensure that /data/pg17-standby is empty or is a newly created directory owned by the PostgreSQL operating-system user.
Review the generated connection settings before starting the server. Confirm the primary hostname, port, replication user, slot name, and TLS options. Then start the standby with the new PostgreSQL binaries:
/opt/pg17/bin/pg_ctl \
--pgdata=/data/pg17-standby \
--log=/data/pg17-standby/server.log \
startValidate streaming and failover readiness
On the primary, inspect the connected standby:
SELECT application_name,
client_addr,
state,
sync_state,
sent_lsn,
write_lsn,
flush_lsn,
replay_lsn
FROM pg_stat_replication;A healthy standby normally has state = 'streaming'. The meaning of sync_state depends on whether the standby is asynchronous, synchronous, or part of a quorum configuration.
On the standby, check whether recovery is active and whether replay is advancing:
SELECT pg_is_in_recovery(),
pg_last_wal_receive_lsn(),
pg_last_wal_replay_lsn(),
now() - pg_last_xact_replay_timestamp() AS replay_delay;Also verify that monitoring, backups, alerting, connection pooling, and failover tooling refer to the new data directory and PostgreSQL version. A standby that streams correctly but is missing from the failover manager is not ready for production use.
Alternative: upgrade a standby in place
An in-place standby upgrade can reduce the time needed to copy a large database, but it requires careful coordination. The standby must be stopped, and the old and new clusters must have compatible layouts, binaries, extensions, and configuration. The exact procedure depends on the PostgreSQL versions and the transfer mode used by pg_upgrade.
For most environments, rebuilding with pg_basebackup is preferable unless the database is too large for the available maintenance window. If you use pg_upgrade on a standby, test the complete sequence in a separate environment and follow the version-specific PostgreSQL documentation.
Cutover and rollback considerations
Keep the old standby stopped while you validate the new one. If the primary upgrade fails before the new primary accepts writes, you may be able to return to the old cluster. Once the upgraded primary has accepted writes, treat the old primary and its standbys as stale. Returning to them would lose those new writes unless you use a deliberate recovery or data-reconciliation plan.
The standby rebuild is complete only when the new server streams from the upgraded primary, replays WAL, passes application-level checks, and is included in the tested failover procedure.
Exercise
Why can a PostgreSQL 16 physical standby not continue streaming from a PostgreSQL 17 primary, and what is the usual replacement procedure?
Show answer
Physical streaming replication requires matching major versions. After upgrading the primary, stop or retire the old standby, take a new base backup from the PostgreSQL 17 primary, configure the replacement standby, and verify streaming and replay.