Sub topic 2
How Streaming Replication Works: walsender, walreceiver, and Recovery
Assume you know how to start PostgreSQL, use psql, read postgresql.conf, and understand that a transaction becomes durable when it commits. This chapter focuses on the runtime path from a primary server to a physical standby. Configuration details come later.
The working model
PostgreSQL records changes in its write-ahead log (WAL). A WAL record describes a change that PostgreSQL can replay, such as modifying a table page. PostgreSQL writes the WAL record before it writes the changed data page. This ordering lets recovery repair or repeat changes after a crash.
A physical standby receives the primary's WAL and replays it. It does not receive SQL statements and run them again. It receives the same low-level WAL changes, so the standby must use a compatible PostgreSQL major version and compatible physical storage format.
Each position in the WAL has a log sequence number (LSN). LSNs let PostgreSQL describe progress precisely:
- The primary generates WAL.
- The
walsenderprocess reads WAL and sends it over the replication connection. - The standby's
walreceiverprocess receives the stream and writes it to the standby'spg_waldirectory. - The startup process replays the WAL and applies the changes to data files.
- The standby reports receive, flush, and replay progress back to the primary.
What recovery means on a standby
Recovery is the process of replaying WAL to make database files match the primary's state. A standby remains in recovery while it continuously receives and replays WAL. The function pg_is_in_recovery() returns true during this period.
Recovery has separate stages. A record may have been received but not yet written safely to disk. It may be flushed to disk but not replayed. It may be replayed locally while newer records are still waiting in the receive queue. These stages explain why a standby can show different receive and replay LSNs.
With hot standby enabled, PostgreSQL opens the database for read-only sessions while recovery continues. Queries see a consistent snapshot of the replayed state. They cannot change tables, create ordinary temporary objects that require writes to shared catalogs, or make the standby an independent writer.
-- Run on the primary to inspect a connected standby.
SELECT application_name,
state,
sent_lsn,
write_lsn,
flush_lsn,
replay_lsn
FROM pg_stat_replication;Asynchronous streaming
By default, streaming replication is asynchronous. The primary can report a commit as successful without waiting for the standby. This keeps commit latency low, but a failure can lose transactions that the standby has not received or replayed.
The replication connection also carries feedback. The standby reports its WAL positions and, when configured, information about the oldest query snapshot it still needs. The primary uses this information for monitoring and some cleanup decisions. A broken connection stops streaming, but it does not automatically stop the primary from accepting writes.
Common failure points
A standby can fall behind because the network is slow, the primary generates WAL faster than the standby can replay it, or standby queries delay cleanup. A missing WAL segment can prevent the standby from continuing. WAL archiving or a replication slot can help retain required segments, but each has storage and operational trade-offs.
Exercise
Why can a standby's replay LSN be behind its receive LSN?
Show answer
The walreceiver has already written WAL that the startup process has not yet replayed. The difference represents work waiting in the standby's receive or replay path.