Problem:
A production PostgreSQL primary exhibited sustained database slowness while the write-ahead log (WAL) directory continued growing without bound. The customer reported ongoing WAL file accumulation and increasing operational risk from exhausted disk space. They were asked to provide PostgreSQL version and topology, WAL folder size, available disk space, PostgreSQL logs, and whether replication or WAL archiving were configured. No application errors were reported, but client-observed performance degradation and continuous WAL retention were the immediate symptoms.
Process:
Step 1: Triage and data collection
Initial work focused on collecting configuration and metrics required to identify retention points. Requested items included PostgreSQL version, WAL directory usage, free disk space, recent PostgreSQL logs, and confirmation of replication slots and archive configuration. This established the scope of investigation because WAL removal depends on checkpoints, replication slot state, and archive success.
Step 2: Inspect replication slot retention
Attention turned to replication slot state by querying replication slot metadata (slot names, types, active flag, and retained WAL distance). The query showed a logical replication slot with active = false and a very large WAL retention delta, indicating WAL segments were being held for a consumer that was not consuming. This mattered because an inactive replication slot prevents PostgreSQL from removing or recycling WAL segments, directly explaining steady WAL growth.
Step 3: Verify archive status
pg_stat_archiver metrics were reviewed to detect archive failures (archived_count, failed_count, last_failed_wal). The archiver reported failed_count > 0 and a recent failed WAL file. Investigation of the archive command and archive destination revealed a permissions/configuration issue that caused archive_command to fail intermittently. This was important because failed archiving also blocks WAL removal when archive_mode is enabled.
Step 4: Check checkpoint progression and active transactions
Checkpoint activity and session state were examined via server logs and pg_stat_activity. Long-lived idle-in-transaction sessions and an absence of recent checkpoint completions were observed; stalled checkpoints prevented advancing past older WAL segments. This explained why, even after addressing retention sources, WAL files were not being recycled promptly and why load spiked from resource pressure.
Step 5: Plan and execute remediation
With root causes identified—an inactive logical replication slot, failing archive_command, and stalled checkpoints due to idle transactions—a coordinated remediation plan was implemented. Steps were sequenced to avoid data loss: (a) validate that no downstream consumer required the replication slot, (b) fix archive_command and ensure the archive destination had correct permissions and space, (c) terminate or resolve long-running idle transactions to allow checkpoints to complete, and (d) remove the stale replication slot when safe. A controlled pg_switch_wal was used after archiving succeeded to accelerate WAL turnover. This combination directly addressed the retention conditions and set the system to reclaim WAL files, leading into the implemented fix.
Solution:
The implemented change combined three actions in PostgreSQL: repaired the archive_command and archive destination so WAL segments could be archived successfully; safely dropped a stale logical replication slot that was retaining WAL; and cleared long-running idle-in-transaction sessions so checkpoints could progress. After these changes, a manual WAL switch and successful archive run allowed PostgreSQL to remove and recycle older WAL segments.
Architecturally this works because PostgreSQL only removes WAL files once a checkpoint has advanced past them, all replication slots have moved past those WAL positions, and archiving has completed (if enabled). Fixing the archive pipeline and removing the inactive slot eliminated the retention constraints, and allowing checkpoints to complete permitted WAL recycling.
Conclusion:
WAL growth stopped, WAL directory usage returned to expected levels, and available disk space increased. Database responsiveness improved after checkpoints resumed and resource pressure eased. The changes reduced the immediate risk of disk exhaustion and restored normal WAL lifecycle behavior in PostgreSQL.