Sub topic 13
Cascading standbys
A cascading standby is a standby that also acts as a replication source for other standbys. The primary sends WAL to one standby, and that standby forwards it to one or more downstream standbys. This chapter assumes you have already built a standby with pg_basebackup, set primary_conninfo, and used pg_stat_replication to watch it.
Why cascade
A primary runs one walsender process for each directly connected standby. Each walsender uses CPU and network bandwidth on the primary. With a few standbys this is negligible. Cascading helps in these cases:
- You have many read replicas, and you want to keep the primary's network and walsender load flat.
- Standbys sit in a remote data center. One standby can receive WAL across the expensive WAN link and relay it to local standbys.
- You want to take base backups for new standbys from an existing standby instead of the primary.
Cascading has a cost. Each hop adds replication delay, and a failure in the middle stops everything below it. It is a tool for scaling out reads and managing network topology. It does not add redundancy.
How it works
A downstream standby uses the same mechanism as any standby. Its walreceiver connects to a walsender and requests WAL from a given position. The only difference is that the walsender runs inside a server that is itself in recovery.
The intermediate standby sends only WAL it has already received and flushed to its own disk. A downstream standby therefore never gets ahead of the intermediate one. Replay on the intermediate standby does not affect what it sends. The downstream standby can receive WAL that the intermediate standby has not yet replayed.
The result is a chain of lags. If standby1 is 2 seconds behind the primary and standby2 is 1 second behind standby1, standby2 is about 3 seconds behind the primary.
Configuring the intermediate standby
The intermediate standby must accept replication connections, the same as a primary. You need three things on standby1.
- A nonzero
max_wal_senders. Hot standby also requires this value to be at least as high as on the primary, so copy the primary's value. - A
pg_hba.confentry that lets the downstream standby connect to the pseudo-databasereplication. - A
listen_addressesvalue that accepts connections from the downstream hosts.
# postgresql.conf on standby1 (the cascading standby)
listen_addresses = '*'
max_wal_senders = 10
hot_standby = on
# pg_hba.conf on standby1
host replication replicator 10.0.1.0/24 scram-sha-256You can reuse the replicator role. Roles are stored in the database and arrive on standby1 through replication, so you create nothing new. Check that pg_hba.conf is correct. It is not replicated, so standby1 keeps whatever file it had when you cloned it.
Reload the configuration after editing. Changing max_wal_senders requires a restart.
Building the downstream standby
Point pg_basebackup at standby1 instead of the primary. A physical replication slot can be created on a standby, so -C -S works here too.
pg_basebackup -h standby1.example.com -U replicator \
-D /var/lib/postgresql/data -X stream -R -C -S standby2 -PThe -R option writes standby.signal and a primary_conninfo that points at standby1. Check the generated postgresql.auto.conf. If you build the standby by other means, set the connection yourself:
# postgresql.conf (or postgresql.auto.conf) on standby2
primary_conninfo = 'host=standby1.example.com user=replicator password=change-me application_name=standby2'
primary_slot_name = 'standby2'
hot_standby = onDespite the name, primary_conninfo does not have to name the primary. It names the server to stream from.
Taking the backup from a standby moves the read load from the primary to standby1. The cost is that standby1 must be healthy and reasonably current. The backup contains whatever standby1 had replayed, and standby2 catches up from there.
Slots and WAL retention across hops
Each server in the chain makes its own decisions about how long to keep WAL. A slot on standby1 keeps WAL on standby1 for standby2. It does not keep anything on the primary. To keep WAL on the primary for standby1, use a slot on the primary.
Create the slot on the standby where it is needed:
-- Run on standby1
SELECT pg_create_physical_replication_slot('standby2');If you use -C -S with pg_basebackup, the slot already exists and you do not need to create it. If you do not use slots, wal_keep_size on standby1 controls how much WAL it holds for downstream standbys. A downstream standby that falls behind that window cannot resume streaming. It must be rebuilt or fed from a WAL archive (see the earlier chapter on archiving).
The disk-growth risk from the slots chapter applies here too. An inactive slot on standby1 forces standby1 to retain WAL. Check each server in the chain:
SELECT slot_name, slot_type, active, restart_lsn, wal_status
FROM pg_replication_slots;Monitoring a cascade
pg_stat_replication lists only the walsenders running on the server where you query it. On the primary you see standby1. To see standby2, query standby1:
-- On standby1
SELECT application_name, client_addr, state, sync_state,
sent_lsn, replay_lsn, replay_lag
FROM pg_stat_replication;Run the standby-side functions on each node to compare positions:
SELECT pg_is_in_recovery(),
pg_last_wal_receive_lsn(),
pg_last_wal_replay_lsn(),
pg_last_xact_replay_timestamp();On the downstream standby, pg_stat_wal_receiver shows which server it is streaming from:
-- On standby2
SELECT status, sender_host, sender_port, slot_name, received_lsn
FROM pg_stat_wal_receiver;To measure end-to-end lag, compare the primary's pg_current_wal_lsn() with pg_last_wal_replay_lsn() on the last standby. The lag reported by the primary covers only the first hop.
What differs from a standby fed by the primary
| Topic | Behavior in a cascade |
|---|---|
| Synchronous replication | Cascading replication is asynchronous. A downstream standby cannot be a synchronous standby of the primary, and the primary's commits never wait for it. |
| Lag | Lag accumulates across hops. Delay on standby1 delays everything below it. |
| `hot_standby_feedback` | A downstream standby reports its oldest needed transaction to standby1. Standby1 passes it upstream only if its own `hot_standby_feedback` is on. Without that, the primary can remove rows that standby2's queries need. |
| Query conflicts | Conflicts on standby2 are handled by standby2's own `max_standby_streaming_delay`. A long query on standby1 delays replay on standby1, which in turn delays what standby2 can replay. |
| Failure of standby1 | Standby2 loses its source. It keeps retrying the connection and keeps serving stale reads. |
Failover in a cascade
If the primary fails, promote standby1 so its downstream standbys keep their source. Run pg_promote() on standby1. It starts a new timeline. Downstream standbys connected to standby1 follow the timeline switch over the streaming connection, provided recovery_target_timeline is latest. That is the default since PostgreSQL 12. They need no reconfiguration and no rebuild.
If you promote standby2 and leave standby1 running, standby1 is not on the new timeline and cannot follow it without being repointed (and possibly rewound). Promote the standby nearest the primary unless you have a reason not to. The chapter on timelines and pg_rewind covers rejoining servers.
If standby1 fails, standby2 does not automatically find another source. You must change its primary_conninfo to point at the primary or another standby and reload the configuration. Keep that procedure written down and tested.
Common errors
FATAL: number of requested standby connections exceeds max_wal_senders. The intermediate standby is running withmax_wal_senders = 0or too small a value. Raise it and restart.no pg_hba.conf entry for replication connection. Thepg_hba.confon standby1 has noreplicationline for the downstream host. Standby1 did not inherit it from the primary.requested WAL segment has already been removed. Standby1 recycled WAL that standby2 still needed. Use a slot on standby1, raisewal_keep_sizethere, or configurerestore_commandon standby2.- Downstream standby stuck while the intermediate looks healthy. Standby1 can only send WAL it has received. If standby1's own connection to the primary is down, standby2 sees nothing new. Check
pg_stat_wal_receiveron standby1.
Exercise
You run SELECT * FROM pg_stat_replication; on the primary and see only standby1, although standby2 and standby3 are streaming from standby1 and working. Why, and where do you look to see them?
Show answer
pg_stat_replication shows only the walsender processes on the server where you run the query. The primary has one walsender, for standby1. Standby2 and standby3 are connected to walsenders on standby1, so you query pg_stat_replication on standby1. On each downstream standby, pg_stat_wal_receiver shows which server it streams from.
Exercise
The primary fails. Standby2 and standby3 stream from standby1. Which standby should you promote to leave the other two in place, and why do they not need rebuilding?
Show answer
Promote standby1. Standby2 and standby3 are connected to standby1's walsender, and with recovery_target_timeline = 'latest' (the default since PostgreSQL 12) they follow the new timeline over the existing streaming connection. Promoting standby2 would leave standby1 and standby3 on the old timeline, and they would have to be repointed and possibly rewound.