Sub topic 2
Starting the standby: standby.signal, primary_conninfo and hot_standby
This chapter assumes you have a running primary with a replication role and a base backup in hand. You should be comfortable editing postgresql.conf and running psql.
Telling the server it is a standby
A standby is a server that replays WAL (write-ahead log) records sent from a primary. Two settings turn an ordinary data directory into a standby.
An empty file named standby.signal in the data directory tells PostgreSQL to start in standby mode. pg_basebackup -R creates this file for you.
The primary_conninfo parameter holds the connection string the standby uses to reach the primary. primary_slot_name names the replication slot to use. hot_standby = on allows read-only queries during recovery. It is on by default in PostgreSQL 14 and later.
# postgresql.conf (or postgresql.auto.conf) on the standby
primary_conninfo = 'host=primary.example.com user=replicator password=change-me application_name=standby1'
primary_slot_name = 'standby1'
hot_standby = onAfter you start the server, confirm it is in recovery:
SELECT pg_is_in_recovery();Running read-only queries on the standby
A hot standby accepts SELECT statements. It rejects INSERT, UPDATE, DELETE and other writes. Queries see data as of the most recently replayed commit, so a read can return slightly older data than the primary holds.
Checking replication health and measuring lag
On the primary, pg_stat_replication shows one row per connected standby:
SELECT application_name, client_addr, state,
sent_lsn, replay_lsn, replay_lag
FROM pg_stat_replication;Check that state is streaming. The gap between sent_lsn and replay_lsn shows how much WAL the standby has not yet replayed. replay_lag reports that delay as an interval.
Replication slots and WAL retention
A replication slot is a server-side marker that stops the primary from deleting WAL the standby still needs. The slot named in primary_slot_name must exist on the primary. pg_basebackup -C -S standby1 creates it.
SELECT slot_name, active, restart_lsn, wal_status
FROM pg_replication_slots;An inactive slot, such as one whose standby is down, keeps WAL indefinitely and can fill the disk. Set max_slot_wal_keep_size on the primary to cap the retained WAL.
Exercise
A standby's data directory has no standby.signal file. What mode does the server start in, and what happens to the replication stream?
Show answer
It starts as a normal read-write primary and does not follow the other server's WAL.