Repository navigation
Description
Is there an existing issue already for this bug?
- I have searched for an existing issue, and could not find anything. I believe this is a new bug.
I have read the troubleshooting guide
- I have read the troubleshooting guide and I think this is a new bug.
I am running a supported version of CloudNativePG
- I have read the troubleshooting guide and I think this is a new bug.
Contact Details
No response
Version
trunk (main)
What version of Kubernetes are you using?
1.34
What is your Kubernetes environment?
Self-managed: kind (evaluation)
How did you install the operator?
YAML manifest
What happened?
After a crash-failover, the demoted former primary rejoins via pg_rewind --restore-target-wal, which walks backward through WAL looking for the last common checkpoint and calls restore_command for each segment along the way. Instance.Rewind (pkg/management/postgres/instance.go) execs pg_rewind directly with no retry wrapper, and pg_rewind aborts its entire run on the first restore_command failure (standard upstream behavior). Because the instance manager only marks initialization done on success, any reconcile where Rewind fails restarts the whole rewind from scratch on the next pass, and that reconcile is requeued under controller-runtime's default rate limiter, exponential up to 1000s. So if restore_command fails intermittently for any reason during the backward search, the demoted node crawls forward one WAL segment per reconcile, with the gap between attempts growing toward 1000s each time, turning what should be a fast rejoin into a multi-hour stall during which the cluster has no failover target.
One plausible source of that kind of intermittent, self-resolving restore failure is wal-restore's speculative prefetch (internal/cmd/manager/walrestore/cmd.go), which only activates when wal.maxParallel is set above its default of 1. On every invocation it fetches the requested segment plus several segments forward of it, assuming a normal forward-replaying recovery. pg_rewind's search instead requests segments in decreasing order, so on a short post-failover timeline the forward prefetch can run past the timeline's real end and trip the end-of-wal-stream flag even though the segment pg_rewind actually asked for restored fine. The next invocation sees that stale flag and aborts immediately without even attempting its own (available) segment. I haven't been able to confirm this is what happened in the field case that prompted this report, so treat it as a plausible mechanism rather than a confirmed one.
Whether or not that's the trigger, the design gap stands on its own: a single restore_command failure during pg_rewind's backward search is fatal with no in-process retry, and the operator's response is to restart the entire rewind under generic exponential backoff rather than retrying the specific restore. I'd expect a transient or spurious restore failure encountered mid-search not to cost an entire reconcile-and-backoff cycle.
This overlaps with the wal-restore end-of-wal-stream/prefetch issues already tracked in #10419 and #8874, but is narrower: it's specifically about the interaction between pg_rewind's backward walk and the no-retry-per-invocation design, independent of which underlying cause makes a given restore attempt fail.
Reproduction sketch (not yet independently verified): configure a cluster with WAL archiving to an object store, force two crash-failovers in quick succession (within checkpoint_timeout of each other) so the demoted node has to walk a short-lived timeline backward through many segments via pg_rewind --restore-target-wal, and check whether pg_rewind fails repeatedly with could not restore file ... from archive, advancing by roughly one segment per operator reconcile.
Cluster resource
(code-analysis finding, not tied to a specific cluster spec)
Relevant log output
(representative pg_rewind error is error: could not restore file "<segment>" from archive, repeating with a strictly decreasing segment name and growing gaps between attempts, up to the ~1000s controller-runtime backoff cap)
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
fix(walrestore): serve pg_rewind without prefetching and flag machinery
fix(walrestore): serve pg_rewind without prefetching and flag machinery
fix(walrestore): serve pg_rewind without prefetching and flag machinery
fix(walrestore): serve pg_rewind without prefetching and flag machinery
fix(walrestore): serve pg_rewind without prefetching and flag machinery
fix(wal-restore): improve WAL restoration during pg_rewind (#11204)
fix(wal-restore): improve WAL restoration during pg_rewind (#11204)
fix(wal-restore): improve WAL restoration during pg_rewind (cloudnati…
fix(wal-restore): improve WAL restoration during pg_rewind (#11204)
fix(walrestore): serve pg_rewind without prefetching and flag machinery