Sub topic 11
Upgrading streaming replicas
Why standbys need their own upgrade step
pg_upgrade only touches one cluster. It reads the old data directory and builds a new one that the new major version can open. It knows nothing about the standby servers that follow the primary.
A streaming standby is a byte-level copy of the primary. It replays the primary's WAL (write-ahead log), and WAL records only make sense to the version that wrote them. That leaves two problems after the primary is upgraded:
- The upgrade does not generate WAL that old-version standbys could replay. It rewrites catalogs and creates a new cluster with a new system identifier.
- The new cluster is a different cluster from the standby's point of view. The old standbys cannot be reattached to it.
So every standby must be replaced with a copy of the upgraded primary, running the new binaries. This chapter assumes you have already run the upgrade steps from the earlier chapters on the primary and understand --link mode. It covers the two ways to replace a standby: the fast rsync method and a rebuild with pg_basebackup.
Choosing a method
| Method | Needs `--link` on the primary | Data sent over the network | Speed on a large database | Main risk |
|---|---|---|---|---|
| `rsync` with hard links | Yes | Mostly catalog files and metadata | Minutes | A mistake in preparation silently corrupts the standby |
| `pg_basebackup` rebuild | No | The whole cluster | Hours for terabytes | Long window without a standby |
| Drop the standby and re-provision | No | Whole cluster | Depends on tooling | Long window without a standby |
The rsync method works because of how link mode lays out files. After pg_upgrade --link, the primary's new relation files are hard links to the old ones. The standby's old data directory contains the same files as the primary's old directory, as long as the standby was fully caught up. rsync --hard-links can therefore recreate the same link structure on the standby, without transferring the user data. Only files that differ (mostly the new catalogs) travel over the network.
Both methods leave the standby unusable as an old-version replica once it is started on the new version.
Preparing the standbys before the primary upgrade
Preparation happens before pg_upgrade runs, while the old cluster is still the primary. Four things must be true.
- The new binaries are installed on every standby host, in the same paths you use on the primary.
- The old standbys are fully caught up with the old primary.
- The old primary's
wal_leveland the new cluster'swal_levelare notminimal. A standby cannot stream from a primary that usesminimal. - You can reach each standby from the primary host with
ssh, as the user that owns the data directory.
Proving the standbys are caught up
The rsync method assumes the standby's old files match the primary's old files. To check this, shut down the primary first, then the standbys. A clean primary shutdown writes a final checkpoint, and a standby that has received all WAL replays it.
# On the primary
/opt/pg16/bin/pg_ctl -D /data/pg16 stop -m fast
# On each standby, after it has replayed the shutdown record
/opt/pg16/bin/pg_ctl -D /data/pg16 stop -m fastThen compare the checkpoint locations.
# On the primary host
/opt/pg16/bin/pg_controldata -D /data/pg16 | grep -E "cluster state|Latest checkpoint location"
# On each standby host
/opt/pg16/bin/pg_controldata -D /data/pg16 | grep -E "cluster state|Latest checkpoint location"The primary should report shut down and each standby shut down in recovery. The Latest checkpoint location values must be identical. A mismatch has two common causes:
- The standby was stopped before the primary, so it never saw the final checkpoint.
- The standby is still running.
Running the upgrade on the primary
With both servers stopped and the checkpoint locations matching, run pg_upgrade on the primary host in link mode, as in the earlier chapter. --link is what makes the next step possible.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--link \
--jobs=4Do not start the new primary yet. Starting it modifies the hard-linked files, and then the standbys can no longer be synchronized with rsync.
Synchronizing the standby with rsync
Run rsync from the primary host, from a directory that is the common parent of the old and new data directories. In this tutorial that parent is /data. The same parent directory must exist on the standby, and it must contain the standby's old data directory pg16.
cd /data
rsync --archive --delete --hard-links --size-only --no-inc-recursive \
pg16 pg17 standby1:/dataPay attention to the details of this command.
- The source arguments have no trailing slash. This makes
rsynccopy the directories themselves, sopg16andpg17land under/dataon the standby. A trailing slash would copy only their contents into/dataand make a mess. --hard-linksrecreates the primary's link structure on the standby. For files already present in the standby'spg16, it creates thepg17entries as links instead of transferring the data.--size-onlyskips content comparison. This is what makes it fast and also what makes the catch-up check mandatory.--no-inc-recursivemakesrsyncscan the full file list first. Without it, hard links between files in different parts of the tree can be missed.--deleteremoves files on the standby that no longer exist on the primary, such as old relation files.
If the cluster uses tablespaces, repeat the synchronization for each tablespace directory. Each tablespace has a version-specific subdirectory (for example PG_16_202307071 and PG_17_202406281). Run rsync from the tablespace parent directory, naming the old and new subdirectories, with the standby's matching parent as the destination.
Configuring the standby to follow the new primary
After rsync, the standby's new directory is a copy of the new primary. It is not yet a standby. Make it one:
# On the standby host, as the postgres user
touch /data/pg17/standby.signal
cat >> /data/pg17/postgresql.auto.conf <<'EOF'
primary_conninfo = 'host=primary1 port=5432 user=replicator application_name=standby1'
primary_slot_name = 'standby1'
EOFPhysical replication slots are not carried over by pg_upgrade. Create the slot on the new primary after you start it, and do this before starting the standby.
SELECT pg_create_physical_replication_slot('standby1');Then start the new primary and the new standby, in that order.
# On the primary host
/opt/pg17/bin/pg_ctl -D /data/pg17 -l /data/pg17/server.log start
# On the standby host, after creating the slot
/opt/pg17/bin/pg_ctl -D /data/pg17 -l /data/pg17/server.log startCheck that streaming works from the primary:
SELECT application_name, state, sent_lsn, replay_lsn
FROM pg_stat_replication;You want state to be streaming. If a standby does not appear, read its server log. The usual causes are a missing standby.signal, a wrong primary_conninfo, or a pg_hba.conf that lacks a replication entry. Remember that pg_hba.conf on the new primary is the one created by initdb plus whatever you restored from your backup copy.
Statistics and cleanup on replicas
Run the post-upgrade statistics rebuild once, on the primary, after the standbys are attached.
/opt/pg17/bin/vacuumdb --all --analyze-in-stagesThe new statistics are ordinary catalog writes, so they replicate to the standbys through WAL. Running vacuumdb on a standby is not possible, because it needs to write.
Delete the old cluster (the delete_old_cluster.sh script that pg_upgrade generates, or your own cleanup) on each host only after you are satisfied with the new setup. With link mode, remember that the old directory on the primary is already unusable once the new primary started. On a standby that you synchronized with rsync, the same applies once you start the new standby.
Rebuilding a standby with pg_basebackup
If a standby failed the checkpoint check, if you did not use --link, or if you simply prefer a cleaner result, discard the old standby and take a fresh base backup from the running new primary.
# On the standby host, with the new cluster directory empty
/opt/pg17/bin/pg_basebackup \
--host=primary1 --username=replicator \
--pgdata=/data/pg17 \
--wal-method=stream \
--write-recovery-conf \
--create-slot --slot=standby1 \
--progress
/opt/pg17/bin/pg_ctl -D /data/pg17 -l /data/pg17/server.log start--write-recovery-conf creates standby.signal and fills in primary_conninfo. --create-slot creates the physical slot on the primary, which saves the manual step above. The cost is that the whole database crosses the network, so the standby is missing until the copy finishes.
Failure modes and rollback consequences
Know what each choice does to your options if the upgrade goes wrong.
- Before you run
rsync, the old standbys are intact, and they are your best fallback. Because they were stopped at the same checkpoint as the old primary, one of them can be started and promoted on the old version with no data loss, as long as the primary was not started in the new version in link mode and its data directory was not modified. - Once the new primary has started in link mode, the old primary directory is no longer valid. The old standbys remain valid only until they are started on the new version or
rsynchas replaced their files. - If you delay your decision, the fallback is a restore from backup, which is why the backup step at the start of the process matters.
- A standby that falls behind during the maintenance window must be rebuilt rather than patched. Never try to "catch up" an old-version standby against the new primary.
Summary of the procedure
- Install new binaries on all hosts.
- Stop the primary, then the standbys, and confirm identical checkpoint locations.
- Run
pg_upgrade --linkon the primary, without starting the new server. - Run
rsyncfrom the common parent directory to each standby, then repeat it for tablespaces. - Add
standby.signaland replication settings on each standby. - Start the new primary, create the physical slots, and start the standbys.
- Run
vacuumdb --all --analyze-in-stageson the primary and verifypg_stat_replication.
Exercise
A standby was stopped a few seconds before the primary. pg_controldata shows different "Latest checkpoint location" values on the two hosts. Can you use the rsync method for this standby, and what should you do instead?
Show answer
No. The standby did not replay the primary's shutdown checkpoint, so its old files are not guaranteed to match the primary's. rsync --size-only would not detect differing contents and could leave a corrupted standby. Rebuild the standby from the new primary with pg_basebackup --write-recovery-conf (optionally with --create-slot).
Exercise
After pg_upgrade --link you start the new primary to test it, and only then try to run rsync for the standby. Why is this a mistake, and how do you fix it?
Show answer
Starting the new primary modifies the hard-linked files, so the old directory is no longer identical to what the standby holds and the link structure that rsync relies on is gone. Fix it by rebuilding the standby with pg_basebackup from the running new primary.
Exercise
Why must you recreate physical replication slots after the upgrade, and where do you do it?
Show answer
pg_upgrade does not copy physical replication slots to the new cluster. Run SELECT pg_create_physical_replication_slot('standby1'); on the new primary before starting the standby, or let pg_basebackup --create-slot create it.