FULL AI ANALYSIS

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

Generated Oct 16, 2026, 9:43:08 AM

Analysis 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.

Possible 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%

Replication slot behavior combined with timeline switch: standby attached to older timeline and waiting for timeline history file, causing applied WAL to pause until history available.

15%

Suggested fixes

1

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.

2

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.

3

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).

4

Diagnostic step 4

If restore_command occasionally fails, implement retry logic or use pgbackrest/wal-e which handle retries and idempotent restores; consider ensuring streaming connection can supply missing segments without relying on archive_command during transient archive outages.

5

Diagnostic step 5

If logs indicate WAL corruption or invalid records, stop writes on primary (if possible), take filesystem-level backups, and consider re-creating the standby via fresh basebackup. Escalate to storage vendor or check hardware if corruption is suspected. Do not delete replication slots without understanding consumer state — that risks data loss for consumers relying on the slot. If unclear, escalate to DBA with WAL/backup expertise.