Databases

PostgreSQL replica accumulates WAL and stalls after archive_command ownership changed during maintenance

Environment

Environment

Primary and standby on Ubuntu 20.04. Primary PostgreSQL 12.8 with streaming replication and archive_command writing WAL files to /var/lib/pgsql/archive owned by postgres:postgres. Admin ran maintenance and temporarily changed owner of archive directory to root; archive_command now runs via postgres user but fails to write.

Symptoms

Symptoms

Standby shows increasing 'replay' lag; primary's pg_wal directory grows until disk nearly full. Replication slot on primary grows in size and WAL segments are not removed. New connections to standby succeed but replication apply stalls with no clear error.

Observations

Observations

Primary log shows repeated 'archive command failed' errors with 'could not open file: Permission denied' for WAL segments. archive_mode=on and archive_timeout set. archive_command returns non-zero; WAL segments accumulate. Standby reports 'waiting for WAL to be archived' in pg_stat_replication state.

Already attempted

Already attempted

Restored ownership to postgres:postgres but left restrictive permissions (700) on archive parent directories; restarted postgres on primary; archive_command still intermittently fails for some WAL files. Ran pg_archivecleanup tests manually as postgres user; some files write fine, others fail.

Question

Question

How to safely recover replication to streaming remove accumulated WAL, ensure archive_command reliably archives WAL again, and reclaim disk without data loss?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

archive_command failing due to filesystem permission/ownership preventing postgres from writing WAL copies 90%
AppArmor/SELinux profile blocking postgres archive writes after ownership change 50%
Archive disk full or mount options (noexec/nosuid) interfering with archive_command scripts 30%

Summary

Primary logs indicate archive_command permission errors preventing WAL archival; streaming replication keeps WAL segments due to retention and replication slot. Changing ownership back to postgres likely fixed some cases but strict directory permissions or SELinux/AppArmor could still block writes. The immediate goal is to restore successful archival so WAL can be archived/removed, then replay or remove excess WAL as appropriate. Caution required to avoid deleting WAL needed by lagging standby.

Suggested Fixes

Diagnostic step 1
Confirm ownership and permissions: chown -R postgres:postgres /var/lib/pgsql/archive and set directories to 750 or 755; ensure postgres can create files as the postgres user.
Diagnostic step 2
As postgres user, run the exact archive_command on a WAL file (su - postgres; /bin/bash -c "<archive_command> path/to/WAL") and observe return code and errors.
Diagnostic step 3
Check AppArmor/SELinux logs (journalctl, /var/log/audit/audit.log) and temporarily put profile in complain/permissive mode while testing.

Continue troubleshooting

Matched from the title, description and tags