Sub topic 13
Cascading Standbys in PostgreSQL
Prerequisites and the basic model
This chapter assumes you understand PostgreSQL streaming replication, pg_basebackup, standby.signal, primary_conninfo, and basic Linux networking.
A cascading standby is a standby that also sends WAL to another standby. Instead of every standby connecting directly to the primary, the servers form a chain:
The first standby receives WAL from the primary with a walreceiver process. It replays that WAL locally and also runs a walsender process for the downstream standby. The downstream server therefore connects to the first standby, not to the primary.
Cascading replication is useful when a primary has limited network bandwidth, many remote sites, or standbys located behind separate network boundaries.
Configure the upstream standby
The primary must allow the first standby to connect, as in ordinary physical replication. The first standby must also be able to accept replication connections from its downstream standby.
On standby 1, configure a replication role or use a suitable role already created for replication. Its postgresql.conf needs a replication sender capacity and a network address:
# On standby 1
listen_addresses = '*'
max_wal_senders = 5
hot_standby = onThe downstream standby's address must be allowed in standby 1's pg_hba.conf:
# On standby 1
host replication replicator 10.0.1.0/24 scram-sha-256A standby can serve read-only queries while it forwards WAL. Forwarding does not require the standby to be promoted or made read-write.
Configure the downstream standby
Create standby 2 with a base backup from the server that will be its upstream. The -h value points to standby 1:
pg_basebackup -h standby1.example.com -U replicator \
-D /var/lib/postgresql/data -X stream -R -POn standby 2, pg_basebackup -R writes connection settings and creates standby.signal. You can inspect or set the upstream connection explicitly:
# On standby 2
primary_conninfo = 'host=standby1.example.com user=replicator password=change-me application_name=standby2'
hot_standby = onStandby 1 must have enough retained WAL for standby 2 to catch up after a temporary disconnection. A physical replication slot on standby 1 can provide that guarantee:
-- Run on standby 1
SELECT pg_create_physical_replication_slot('standby2');Then set the slot name on standby 2:
primary_slot_name = 'standby2'A slot prevents standby 1 from recycling WAL that standby 2 has not received. However, an inactive slot can cause unbounded disk growth. Monitor slots and remove unused ones only after confirming that no standby needs them.
Choosing a topology
| Topology | Main advantage | Main risk |
|---|---|---|
| Direct primary to every standby | Fewer dependencies | More primary connections and network traffic |
| Primary to standby to standby | Reduces primary traffic and spans networks | Upstream failure delays downstream replication |
| Cascading with WAL archive fallback | Supports remote recovery | More configuration and monitoring |
Exercise
Exercise
If standby 2 must receive WAL from standby 1, which setting identifies standby 1 as its source?
Show answer
standby 2 uses primaryconninfo with standby 1's host, and may use primaryslot_name for a slot created on standby 1.