Use it to set up or troubleshoot PostgreSQL standbys.
PostgreSQL Streaming Replication
PostgreSQL streaming replication sends WAL from the primary to standbys over a replication connection; standbys replay it and can serve read-only queries.
1. Primary: SELECT client_addr,state,sent_lsn,replay_lsn FROM pg_stat_replication;.
2. Standby: SELECT pg_is_in_recovery(), now()-pg_last_xact_replay_timestamp();.
3. pg_hba allows replication user.
4. Slots active?
5. Logs on both sides.
primary WAL → walsender → walreceiver → replay on standby
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Check state is streaming and lag is small. Test promotion on a non-production pair.
Step 2 Setup and checks Verify boundary
pg_basebackup -h primary -U replicator -D /var/lib/postgresql/17/main -R -X stream -S standby1 -C
-- on primary
SELECT client_addr, state, sync_state, replay_lag FROM pg_stat_replication;
-- on standby
SELECT pg_is_in_recovery(), pg_last_wal_replay_lsn(), now() - pg_last_xact_replay_timestamp() AS lag;
Documented example, not captured output. -R writes the connection settings and standby.signal. Use replication slots so WAL is kept for the standby, and synchronous_standby_names for zero data loss.
Step 3 Common failures Narrow the fault
Without a slot or enough wal_keep_size, a lagging standby can fall behind permanently. Synchronous replication with an unavailable standby blocks commits. Long queries on the standby are cancelled by replay conflicts.
Use replication slots carefully (they retain WAL). Monitor lag in bytes and time. Synchronous standbys block commits if they are down.
SELECT client_addr,state,sent_lsn,replay_lsn FROM pg_stat_replication;— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
The standby's WAL receiver connects to the primary's WAL sender and writes WAL locally; the startup process replays it. A replication slot can make the primary keep WAL the standby still needs.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Reading replication status on the primary
SELECT application_name, client_addr, state, sync_state,
pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_bytes_behind,
write_lag, flush_lag, replay_lag
FROM pg_stat_replication;
| Column | Meaning |
|---|---|
state = streaming | Normal; catchup = still behind after connecting |
sync_state | async, sync, potential, quorum |
write_lag / flush_lag / replay_lag | Time until WAL was written, flushed, applied on the standby |
| bytes behind | How much WAL the standby still has to replay |
On the standby: SELECT pg_is_in_recovery(), pg_last_wal_receive_lsn(), pg_last_wal_replay_lsn(), now() - pg_last_xact_replay_timestamp();
Slots vs wal_keep_size
A physical replication slot makes the primary keep WAL until the standby has received it — the standby can never fall too far behind, but a dead standby fills the primary's disk. Cap it with max_slot_wal_keep_size and watch wal_status (reserved, extended, unreserved, lost) in pg_replication_slots.
Synchronous replication levels
synchronous_commit | Commit waits until the standby has… |
|---|---|
remote_write | Received the WAL (in OS memory) |
on | Flushed it to disk |
remote_apply | Replayed it — reads on the standby see the commit |
synchronous_standby_names = 'ANY 1 (s1, s2)' lets either of two standbys acknowledge, so one standby failure does not stop all commits.
Query conflicts on hot standbys
Long queries on a standby can be cancelled (canceling statement due to conflict with recovery) when replayed vacuum removes rows they need. max_standby_streaming_delay delays replay instead; hot_standby_feedback = on avoids cancels but can cause bloat on the primary.
Common mistakes
- Replication slots left behind, filling disk.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.