Databases

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

Environment

Environment

Primary: PostgreSQL 13.7 on Debian 11, pg_upgrade performed from 12→13 three days ago; archive_command writes WAL segments to NFS-mounted archive server (NFSv4). Standby: physical standby on another host using restore_command that fetches from archive (rsync over SSH). Standby configured with replication_slot; standby was promoted back and forth during maintenance once.

Symptoms

Symptoms

Standby process logs repeated errors and stops applying WAL: 'invalid record length at 0/XX: invalid record length -1/0' and standby tails WAL but WAL receiver exits. Replica lag grows and replication_slot accumulates WAL files in pg_wal. Primary continues to operate normally.

Observations

Observations

Primary pg_waldump shows valid WAL segments around reported LSN. standby log (postgresql.log) shows corruption error when reading restored WAL file. sha256sum of WAL files on archive server differ from copies fetched to standby. NFS server reported transient connectivity errors during pg_upgrade window. restore_command uses scp/rsync to pull files; some archived WALs have been manually copied during maintenance. File sizes of a couple of archived WAL segments on archive share are zero or smaller than expected.

Already attempted

Already attempted

Verified WAL checksums (if enabled) on primary for recent segments; ran pg_verifybackup on base backup (no issues). Re-fetched a reported-corrupt WAL segment from primary's pg_wal directory (where it still exists) and replaced the standby copy — standby resumed applying up to that point. Searched archive for duplicate-named segments and found older segments present with same name in different directories. Attempted pg_wal replay with wal_e not used. Did not perform pg_rewind or rebase.

Question

Question

What are the likely root causes for 'invalid record length' after an upgrade and archive restore, and what safe steps can I take to ensure the standby can catch up without data loss? When should I rebuild standby with base backup instead?

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

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%

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.

Suggested Fixes

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.
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.
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.

Continue troubleshooting

Matched from the title, description and tags