FULL AI ANALYSIS

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

Generated Oct 22, 2026, 9:30:52 AM

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

Possible 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%

Suggested fixes

1

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.

2

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.

3

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.

4

Diagnostic step 4

Avoid using pg_resetwal except as a last resort and only with expert guidance — it can cause data loss.

5

Diagnostic step 5

Consider using pg_rewind only if you have a consistent divergence scenario and matching control data; otherwise prefer a fresh base backup.