Databases

PostgreSQL standby reports 'invalid checksum' and stops applying WAL after incremental base backup restore

alexnord
1mo ago
Environment

Environment

Primary PostgreSQL 14 on Ubuntu 22.04 with WAL archiving to an rsync‑backed archive directory. Standby restored from an incremental backup snapshot of the archive and configured for streaming + restore_command reading from archive dir.

Symptoms

Symptoms

Standby crashes apply process with errors like 'invalid checksum in record at X' and refuses to continue. Replication slot on primary shows WAL not streamed; standby lag stalls at last valid LSN. Applications still write to primary.

Observations

Observations

restore_command returns files successfully but pg_waldump shows malformed records at the reported LSN. The archive directory contains WAL segments with modified timestamps and some segments smaller than 16MB. A timeline file (.history) appears missing or belongs to a prior promotion.

Already attempted

Already attempted

Replayed older WALs with pg_waldump to isolate the corruption range, re‑rsynced archive directory from primary, restarted standby. Stopping/starting postgres on standby does not progress apply. Tried copying specific WAL segments from primary but history mismatch prevented apply.

Question

Question

How can I recover the standby and resume WAL apply without losing replication continuity? What checks and recovery steps are safe to run now?

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

WAL segment corruption from partial rsync/archive write (segments smaller than 16MB) causing checksum failure during apply 75%
Timeline/history mismatch (standby trying to apply WAL from a different timeline without corresponding .history), preventing proper WAL sequencing 60%
Restore_command reading from a mixed archive directory where older files were overwritten or pruned mid‑restore 45%

Summary

The standby error and small/partial WAL files indicate WAL corruption or a timeline mismatch. Corruption can come from partial rsync copies, archive rotation during a copy, or filesystem dedupe/truncate. A missing or incorrect timeline history file means WAL being applied belongs to a different timeline (eg. primary was promoted elsewhere then re‑integrated). If required WAL segments are lost or corrupted, the standby cannot continue and a new base backup is needed. If archives exist intact on primary or external backup, they should be retrieved; if not, rebuilding standby is likely.

Suggested Fixes

Diagnostic step 1
Stop the standby PostgreSQL instance to avoid further apply attempts and copy the entire pg_wal/pg_wal/archive status to a safe location for forensic investigation.
Diagnostic step 2
Run pg_waldump on the WAL segment reported bad to confirm corruption and note LSN ranges. Inspect archive dir timestamps and sizes for partial files (files < 16MB indicate truncation).
Diagnostic step 3
Check for timeline history files in archive; compare timeline IDs (pg_control, pg_controldata) between primary and standby. If history files missing, fetch them from primary or backups.

Continue troubleshooting

Matched from the title, description and tags