Skip to content

[Bug]: pg_rewind's single-shot restore_command abort + full reconcile restart can stall crash-failover rejoin for hours #11200

Description

@mnencia

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

  1. added
    bug 🐛
    triage
    and removed
    triage
    on Jul 15, 2026
  2. added theissue type on Jul 15, 2026
  3. self-assigned this
    on Jul 15, 2026
  4. added 2 commits that reference this issue on Jul 21, 2026
    f5f1b8a
    0ceaed0
  5. added a commit that references this issue on Jul 21, 2026
    ad1d52c
  6. added 2 commits that reference this issue on Jul 21, 2026
    16c4d64
    176f19c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions