Databases

PostgreSQL standby stops applying WAL after pg_basebackup restore — replication slot grows and disk fills

Environment

Environment

Primary: PostgreSQL 14.8 on Ubuntu 20.04 (bare-metal); Standby created with pg_basebackup and archive/replication slots enabled. Primary performs continuous WAL archiving to a remote archive_command. Replication uses streaming with replication slot 'standby_slot'.

Symptoms

Symptoms

Standby initially follows primary, then stops applying WALs. The replication slot on primary grows indefinitely; 'pg_stat_replication' shows the standby connected but 'sent_lsn' advances while 'replay_lsn' on standby is stuck. WAL archive directory on primary continues to accumulate WAL files and disk usage approaches capacity.

Observations

Observations

On the standby, postgresql.conf shows restore_command configured, timeline history file present. 'SELECT pg_last_wal_receive_lsn(), pg_last_wal_replay_lsn()' shows receive > replay. Standby logs contain repeated 'could not open file

Already attempted

Already attempted

Restarted standby PostgreSQL service — after restart it resumed applying WAL temporarily then stalled again. Verified archive_command success on primary and file permissions. Ran 'pg_controldata' on replica basebackup directory — timeline and redo LSN consistent with primary at backup time. Cleared and re-copied archive_command result for a single missing WAL segment — standby immediately applied it. Enabled increased logging on standby for recovery and checked for I/O or permission errors — none obvious.

Question

Question

Why does the standby stop applying WAL despite receiving them (receive_lsn > replay_lsn) and what safe remediation prevents replication slot growth without reinitializing the standby from scratch?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Standby is receiving WAL via streaming but replay is blocked by a long-running query or inactive snapshot on the standby (hot standby conflicts). 75%
restore_command intermittently failing to fetch required WAL segments from archive, leaving gaps that streaming alone doesn't fill (e.g., file perms or transient S3/remote archive error). 60%
WAL corruption on primary or during transfer results in replay errors; standby logs would show 'invalid record length' or checksum failures. 20%

Summary

Receive_lsn > replay_lsn on standby indicates WAL files are arriving (streaming) and being saved but apply (replay) is blocked. Common causes include long-running transactions or open logical locks on standby (e.g., long-running queries that prevent replay), a read-only query holding a snapshot conflict, corrupted WAL records causing apply to stall, or restore_command-based archival restores interfering with streaming recovery (timeline mismatches or restore_command delays causing missing segments). Given that a manual copy of a missing WAL allowed replay to continue, an intermittent restore_command or file-permission issue on archive retrieval is likely; however long-running queries on standby that conflict with recovery also produce messages in logs ('could not apply WAL because of conflicting snapshot'). The replica briefly recovered after restart — suggesting a transient condition such as an application opening a long-running READ WHEN in standby or occasional permission/timeouts on archive storage.

Suggested Fixes

Diagnostic step 1
Check standby logfile around the stall for messages mentioning 'conflicting' snapshots, 'could not open file', 'invalid record', or 'requested WAL segment not found'. Those messages point to snapshot conflicts, missing WALs, or corruption respectively.
Diagnostic step 2
On the standby, run 'SELECT pid, usename, query_start, state, query FROM pg_stat_activity WHERE state <> 'idle' AND now() - query_start > interval '5 minutes';' to detect long-running queries that may block replay. If found, terminate or coordinate with application owners to modify query patterns.
Diagnostic step 3
Verify restore_command behavior by attempting to fetch a specific WAL file referenced in standby logs (simulate restore_command as the postgres user). Confirm permissions, connectivity, and timeouts to archive storage (S3, NFS, rsync target).

Continue troubleshooting

Matched from the title, description and tags