Databases

PostgreSQL WAL backlog and standby lag after nightly bulk UPDATE with synchronous rsync archive_command

Environment

Environment

PostgreSQL 13.4 primary and one streaming standby on Debian 11 VMs. Primary configured with archive_mode = on, archive_command runs a shell wrapper that rsyncs WAL files to an S3 gateway appliance. Nightly bulk UPDATE job touching ~200M rows starts at 02:00 and lasts ~3–4 hours.

Symptoms

Symptoms

Standby falls behind during the bulk update. WAL files accumulate on primary (pg_wal grows), wal_sender processes show high CPU and some WAL segments remain needed by the replication slot for multiple hours; disk usage on primary creeping toward full.

Observations

Observations

pg_stat_replication shows standby state frequently 'streaming' but replay_lag increases; archive_command logs show intermittent timeouts/exit code non-zero when rsync to S3 gateway times out under high write churn. Manually running the archive_command for queued WAL files sometimes succeeds. No long-running user transactions on standby. The WAL sender CPU spikes correlate with archive_command failures.

Already attempted

Already attempted

Paused the bulk load job briefly and retried archive_command; increased checkpoints temporarily; ran manual rsync to the S3 gateway which succeeded for some WAL files; restarted WAL sender process — backlog persisted until network to S3 gateway stabilized. Did not disable archiving because archival retention policy must be preserved.

Question

Question

What is the most likely cause of the WAL accumulation and standby lag during the bulk update, and what safe, reversible mitigations let replication catch up without data loss or violating retention policy?

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 (rsync to S3 gateway) is timing out or slow under high WAL churn, preventing WAL archival and causing primary to retain WAL; replication backlog follows. 75%
High WAL generation during the bulk update plus local disk I/O saturation causes slow fsync/archiving operations and delays WAL recycling. 40%
Replication slot/standby has an issue (long-running query or narrow replay throughput) causing wal_sender to wait and WAL to accumulate. 35%

Summary

The archive_command is synchronous and appears to be the bottleneck: under high WAL generation the rsync->S3 gateway step is slow/intermittent, meaning WAL files cannot be removed and the primary must retain them, causing WAL_dir growth and standby lag. Less likely but possible contributors are long-running transactions or replication slot issues on the standby, or local disk IO saturation. Because manual rsync works sometimes, transient network/storage slowness at the S3 gateway is the likeliest root cause.

Suggested Fixes

Diagnostic step 1
Inspect archive_command logs and capture exit codes for failed rsync attempts. Test rsync to S3 gateway under simulated bulk load (from another host) to confirm latency/timeouts.
Diagnostic step 2
Temporarily modify archive_command to copy WALs to a local staging directory on the primary (fast local disk) rather than remote S3; keep the uploader as an asynchronous background job that uploads from staging to S3. This is reversible: preserve original archive_command and revert after tests.
Diagnostic step 3
Check pg_stat_activity for long-running transactions on primary and standby; on standby ensure replay is not blocked by queries using REPEATABLE READ/SERIALIZABLE snapshots.

Continue troubleshooting

Matched from the title, description and tags