Databases
VERIFIED FIXES

Standby PostgreSQL fails replay with 'invalid record length' after restoring base backup from different wal_segment_size

astridapps
1mo ago
Environment

Environment

PostgreSQL 14 primary built with default wal_segment_size during initdb on primary host; standby was provisioned via pg_basebackup from a primary which used a non-default wal_segment_size (e.g., 64MB vs 16MB). Standby is running streaming replication as a physical standby.

Symptoms

Symptoms

Standby connects to primary and receives WAL, but WAL replay aborts with errors such as 'invalid record length' or 'unexpected end of WAL' at early LSN. Replication slot on standby grows as standby cannot apply WAL. System logs show repeated replay failures at same LSN.

Observations

Observations

pg_basebackup completed without error. pg_controldata on primary and standby show different wal_segment_size values. stalling occurs immediately after starting recovery; there are no disk IO errors or kernel messages indicating hardware corruption. Re-fetching WAL segments from primary does not resolve replay errors.

Already attempted

Already attempted

Re-ran pg_basebackup once; verified recovery.conf and restore_command; attempted to copy older WAL files manually; tried pg_resetwal (abandoned due to risk). Did not perform pg_rewind. No changes to postgresql.conf besides hot_standby settings.

Question

Question

Can the standby be recovered without a full resync? What is the safe recovery path when wal_segment_size differs between clusters used for base backup and primary?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
alexnord Answer
user
Posted 1mo ago • 0 confirmations

Check pg_controldata on both primary and standby — if wal_segment_size differs, you must reinitialize the standby from a base backup taken from the primary. The WAL format depends on that size, so replaying incompatible WAL yields 'invalid record length'. Take a fresh pg_basebackup from the primary and start the standby from that backup; once replication is streaming and replaying cleanly, remove the old standby. Keep the old data directory offline for investigation but don’t run pg_resetwal unless you accept possible data loss. This restores safe replication with matching control data.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Mismatch in wal_segment_size between the base backup cluster and the target standby (initdb difference), causing incompatible WAL segmentation and replay errors. 90%
Corruption introduced during WAL transfer or file truncation on standby filesystem while replaying, producing invalid record errors. 35%
Binary incompatibility between PostgreSQL builds (minor version/compile options) leading to WAL interpretation problems. 25%

Summary

wal_segment_size is set at initdb time and embedded in the cluster's control data; WAL binary format depends on this. A base backup taken from a cluster with one wal_segment_size cannot be reliably replayed on a cluster initialized with a different wal_segment_size. The mismatch manifests as invalid WAL record lengths during replay. The safe recovery path is to obtain a fresh base backup from the running primary and initialize the standby with matching control data, or provision a new standby using the same initdb wal_segment_size as the primary.

Suggested Fixes

Diagnostic step 1
Confirm wal_segment_size on primary and the standby using pg_controldata; if they differ, plan a fresh base backup from the primary.
Diagnostic step 2
Take a new base backup from the primary with pg_basebackup and initialize the standby using that backup so control files match the primary, then start streaming replication.
Diagnostic step 3
If a full resync is not possible immediately, stop replay, archive corrupted WAL for analysis, and keep the existing standby offline to preserve evidence.

Continue troubleshooting

Matched from the title, description and tags