PostgreSQL standby stops applying WAL with 'invalid record length' after pg_upgrade and manual archive restore
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
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
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
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
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?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases37% match
PostgreSQL standby reports 'invalid checksum' and stops applying WAL after incremental base backup restore
Open0 answers - Databases36% match
PostgreSQL standby stops applying WAL during long-running transactions on primary after pg_basebackup restore
Open0 answers - Databases35% match
Standby PostgreSQL fails replay with 'invalid record length' after restoring base backup from different wal_segment_size
Solved1 answer - Databases33% match
PostgreSQL standby stops applying WAL after pg_basebackup restore — replication slot grows and disk fills
Open0 answers - Databases30% match
PostgreSQL WAL archiving stops when archive_command intermittently exits 75 (permission denied) — WAL accumulates on primary
Open0 answers