FULL AI ANALYSIS

PostgreSQL standby stops applying WAL with 'invalid record length' after pg_upgrade and manual archive restore

Generated Oct 17, 2026, 9:26:11 AM

Analysis summary

Errors point to corrupted or partially written WAL segments on the archive path. Possible causes include manual copying during maintenance leading to overlapping/partial files, NFS/samba storing stale/zero-length files, or mismatched WAL filenames from multiple archives (mixing WALs from different timelines or clusters). Because fetching a clean WAL from primary allowed replay to continue, the archive copy is the primary suspect. Safe recovery often requires ensuring the archive contains an uncorrupted, continuous WAL sequence or rebuilding standby with a fresh base backup if archive continuity cannot be guaranteed.

Possible causes

Corrupted/partially-written WAL segments in the archive (NFS/sync issues or interrupted archive_command), causing invalid records on restore

90%

Archive contains mixed WALs from different clusters/timelines or manual copies placed in archive with same names, leading to mismatched contents

60%

restore_command or transfer tool (rsync/scp) truncating or zeroing files due to disk space or permission issues on archive/standby

45%

WAL checksum mismatch because primary was running with checksums off and standby expects checksums (rare misconfiguration after upgrade)

15%

Suggested fixes

1

Diagnostic step 1

Do not overwrite or delete existing primary pg_wal files. On standby, identify the first corrupt WAL LSN from logs and copy the corresponding WAL segment directly from primary's pg_wal (if available) to the standby's restore path; test if replay continues.

2

Diagnostic step 2

Verify archive integrity: run sha256sum on archived WAL files and compare to copies on primary (if primary retains them) or re-archive cleanly from primary for suspect segments. Avoid public sharing of checksums with private data.

3

Diagnostic step 3

Search archive for duplicate WAL filenames and verify file sizes/mtime; if duplicates exist, move suspect files to a quarantine location (do not delete) and replace with verified copies from primary.

4

Diagnostic step 4

If archive continuity cannot be restored (missing or corrupted segments), prepare to rebuild standby from a fresh base backup (pg_basebackup) taken from current primary. Rebuilding is reversible and avoids data divergence.

5

Diagnostic step 5

Once stable, consider enabling WAL checksums (requires downtime + reinitdb) or implement a more robust archival transfer (atomic upload + verification) and S3-style object store with strong consistency to prevent future silent corruption. Escalate to storage/NFS admin if archive server shows filesystem errors or transient network issues.