Databases

PostgreSQL replica lag grows after nightly rsync base backups and manual WAL copy workflow

Environment

Environment

Primary PostgreSQL 13 on Linux writes ~2 TB/day; nightly 'rsync' used to copy data directory while DB running, followed by manual copy of recent WAL segments into archive-restore area on replica. Replica uses streaming but often falls behind and slot WAL accumulates.

Symptoms

Symptoms

Replica WAL apply lag increases over hours after each nightly job; replication slot size grows and pg_wal on primary increases until alerts. Manual replay on replica shows 'invalid record' and timeline gaps requiring rebase.

Observations

Observations

Rsync backup is not crash-consistent; primary continued running during copy. Archive command sometimes fails to find matching WAL filenames. Replica reports missing WAL segments and later reports checksum/invalid record errors. pg_basebackup wasn't used. Primary autovacuum and load unchanged.

Already attempted

Already attempted

Switched archive_command to a reliable remote-copy during the window, attempted base backup from snapshot LVM afterwards, and tried replaying missing WAL manually — some WAL segments rejected. Stopped rsync job and tested pg_basebackup on a smaller dataset successfully.

Question

Question

Why does using rsync for live base backups cause replica WAL gaps and invalid WAL records, and how should we fix replication and prevent WAL accumulation without long downtime?

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

Use of rsync on a live data directory producing an inconsistent base backup leading to mismatched WAL and data pages 95%
Archive_command timing issues leaving gaps if WAL segments rotated during copy and not atomically archived 55%
Replica replay settings/timeline confusion when base backup and WALs come from different timelines or missing checkpoint info 45%

Summary

Rsync of a live data directory produces an inconsistent base backup: files change while copying, leading to WALs that reference pages not matching the copied data. Replicas expect a consistent base backup (from pg_basebackup or filesystem snapshot with the database offline or LVM snapshot) plus continuous WAL stream. The manual copying of WALs can't reconcile page-level mismatches, resulting in invalid record/replay failures. WAL accumulation occurs because replicas wait for WAL that references a different base state and cannot apply it.

Suggested Fixes

Diagnostic step 1
Stop using rsync on the live cluster. Create a consistent base backup using pg_basebackup or take an LVM/ZFS snapshot while pausing writes or using the database's online backup facility.
Diagnostic step 2
Set up continuous WAL archiving with a robust archive_command that writes WAL segments atomically (use tempfile+rename pattern) to remote storage.
Diagnostic step 3
To repair replica: promote to recovery from a fresh base backup plus continuous WAL stream or rebuild replica using pg_basebackup from primary.

Continue troubleshooting

Matched from the title, description and tags