Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

PostgreSQL Streaming Replication

Direct answer

PostgreSQL streaming replication sends WAL from the primary to standbys over a replication connection; standbys replay it and can serve read-only queries.

When to use this

Use it to set up or troubleshoot PostgreSQL standbys.

First 30 seconds

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.

Operational verification

primary WAL → walsender → walreceiver → replay on standby

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it to set up or troubleshoot PostgreSQL standbys.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Use replication slots carefully (they retain WAL). Monitor lag in bytes and time. Synchronous standbys block commits if they are down.

Key checks
  • SELECT client_addr,state,sent_lsn,replay_lsn FROM pg_stat_replication; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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;
ColumnMeaning
state = streamingNormal; catchup = still behind after connecting
sync_stateasync, sync, potential, quorum
write_lag / flush_lag / replay_lagTime until WAL was written, flushed, applied on the standby
bytes behindHow 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_commitCommit waits until the standby has…
remote_writeReceived the WAL (in OS memory)
onFlushed it to disk
remote_applyReplayed 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
postgresql streaming replicationpostgresql streaming replicationhot standby
Confirmation

Confirm action

Continue?