Databases

Primary PostgreSQL WAL directory fills after interrupted base backup and failing archive_command

aliceadmin
1mo ago
Environment

Environment

PostgreSQL 14 on Ubuntu 20.04 as primary: streaming replication to two standbys, WAL archiving to S3 via external wal-e process (archive_command calls wal-e); a pg_basebackup run from 02:00 failed midway due to network outage; archive_command runs as postgres user; disk usage at 92% of /var/lib/postgresql/14/main/pg_wal.

Symptoms

Symptoms

pg_wal directory accumulates many WAL segments and does not purge. Archive logs show lines like 'archive_command failed: exit code 1', and archive_status/*.ready files exist, preventing WAL removal. Standbys are disconnected and report needing older WAL segments.

Observations

Observations

pg_stat_archiver shows repeated failed archive attempts; pg_stat_replication shows no connected replicas after the outage; wal_e logs show permission denied to S3 due to rotated credentials on the archival container. Manual deletion of WAL segments attempted but PostgreSQL refuses to start if missing files.

Already attempted

Already attempted

Restarted archive agent, restored S3 credentials temporarily (failed due to credential rotation history), attempted pg_archivecleanup on segments older than last known required LSN (operation refused because replicas may still need them), considered deleting .ready files but hesitated.

Question

Question

What is the safe procedure to reclaim WAL space without breaking replication or corrupting backups when a base backup was interrupted and archiving is failing?

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

Archive_command failing (S3 credential rotation / permissions) preventing WAL from being archived and removed 85%
Interrupted base backup / timeline switch requiring older WAL segments for standby recovery 60%
Standbys disconnected and lagging, still requesting older WAL segments 50%

Summary

WAL retention is functioning as designed: PostgreSQL refuses to remove WAL segments that might still be required by standbys or that haven't been archived successfully. The interrupted base backup likely left a timeline or gap; combined with failing archive_command (S3 credential rotation) the server retains WAL. The safest corrective actions are to restore archive_command functionality so archiving completes, or if standbys are permanently disconnected, take a fresh base backup for replicas and then safely archive/prune WAL.

Suggested Fixes

Diagnostic step 1
Do NOT delete WAL files manually while replicas may need them. Check pg_stat_replication and pg_stat_archiver to identify minimal required WAL.
Diagnostic step 2
Fix archive_command (restore correct S3 credentials / permissions) so existing WAL segments can be archived; confirm by observing successful archive entries in wal-e logs.
Diagnostic step 3
If replicas cannot be reconnected or are obsolete, take a fresh base backup of primary and reinitialize standbys; only then you can safely remove older WAL segments.

Continue troubleshooting

Matched from the title, description and tags