Skip to content

[Feature]: Bound continuous archive recovery on standbys so they can resume streaming #11808

Description

@gbartolini

Is there an existing issue already for this feature request/idea?

  • I have searched for an existing issue, and could not find anything. I believe this is a new feature request to be evaluated.

What problem is this feature going to solve? Why should it be added?

A standby that has both primary_conninfo and a restore_command only tries to stream when restore_command fails. On a busy primary that keeps archiving, the archive never runs dry, so the standby can stay on archive recovery indefinitely: no walreceiver, no walsender, an inactive replication slot, and no synchronous or quorum membership.

CloudNativePG configures a restore_command on every replica whenever a WAL archive exists, so every replica in such a cluster is exposed.

PostgreSQL behaviour

In standby mode the startup process cycles through WAL sources in a fixed order: archive (restore_command), then pg_wal, then streaming. It moves to streaming only when the archive read fails, and after a streaming failure it goes back to the archive first. See standby server operation.

Two regimes keep a standby on the archive:

  • Backlog: per-segment restore latency exceeds the time the primary takes to fill a segment, so the standby never reaches the end of the archive.
  • Segment-boundary re-read: with WAL prefetching, a record straddling a segment boundary makes restore_command be called again for the previous segment after the next one misses. By then the next segment has been archived, so the retry succeeds and the miss never reaches the source state machine. Segment names become non-monotonic. Reported upstream; the mechanism is still to be confirmed against the PostgreSQL source.

Consequences

Describe the solution you'd like

A bounded mitigation in the instance manager's wal-restore command. After a configurable window of continuous successful restores while the instance is in standby mode, return "WAL not found" once, so that PostgreSQL falls through to its streaming source. The check runs before the CNPG-I plugin call, so every plugin implementation inherits it and no CNPG-I API change is needed.

Guards (all must hold)

  • Not rewindMode.
  • Standby mode, as defined in the PostgreSQL documentation on hot standby administration: standby.signal present in PGDATA. Also require recovery.signal to be absent, so that bootstrap recovery and PITR are never affected.
  • Hot standby is enabled (hot_standby = on, the default), so the instance is in hot standby mode as described in the same documentation page.
  • isStreamingAvailable(cluster, podName) is true (reuse shouldUseEndOfWALStreamFlag): in-cluster replicas, and the designated primary of a replica cluster only when its external cluster has connectionParameters. Archive-only replica clusters are excluded.

The recovery guard matters because, outside standby mode, a failed restore_command ends recovery. A forced failure during a full recovery or PITR would stop at the wrong LSN and promote. The guard must not rely on cluster status alone.

Backoff

The same signals avoid flapping when streaming cannot hold (WAL recycled, slot invalidated, timelines diverged as in #10419):

  • If the next invocation after a forced failure arrives sooner than the idle threshold, streaming did not hold. Double the effective interval for the next window, up to a cap.
  • Reset to the configured interval after an idle gap.

Evaluation of a new field

A user-facing setting is deliberately not part of this issue. It should be evaluated later, based on feedback from the first releases that carry this behaviour. Points to consider then:

  • Whether any real deployment needs to disable the behaviour (for example an archive-only topology we have not anticipated) or to tune the window. There is currently no opt-out, which is the main argument for a field if such a case appears.
  • Proposed shape if we add one: .spec.postgresql.streamingRetryInterval, in seconds (like stopDelay and failoverDelay), where 0 disables it and an unset value keeps the 30 second default.
  • Naming: follow the semantics of the upstream streaming_replication_retry_interval patch, so that on a PostgreSQL version that has it the setting can be mapped to the native GUC, at which point the instance manager logic could be bypassed.
  • Validation: reject values below the greater of 5 seconds and wal_retrieve_retry_interval.
  • Whether a permanent API field is justified for what is a bridge until the upstream fix lands, compared with an operator-level setting or an experimental annotation.
  • Whether the default should depend on replicationSlots.highAvailability.enabled, since a slot guarantees the WAL is still on the primary.

Cost

If streaming cannot start, each forced failure may idle the standby for up to wal_retrieve_retry_interval (5s by default) before it returns to the archive. With a 30s window that is a worst-case slowdown of archive catch-up of roughly 14%. This is an estimate to be measured. The backoff limits repeated cost.

Interaction with other features

  • Replication slots: effective only if the primary still has the WAL, which HA slots guarantee by default. Slot invalidation ([Bug]: Invalidated (wal_status = lost) HA replication slot is never recreated, replica stays on archive recovery forever #11646) is a separate problem, but the backoff keeps the cost bounded.
  • Synchronous replication: a standby that attaches sooner can become eligible sooner. To our understanding a walsender only counts as a sync candidate in the streaming state, not catchup. To be verified in syncrep.c.
  • Parallel restore: the forced failure happens before any new download for that invocation. Spool hits for segments already downloaded are skipped for that one invocation and served on the next.

Observability

  • A clear info log line for every forced failure, with the window, the effective interval and the backoff level.
  • A counter of forced failures per instance. Since wal-restore is a separate process, this likely means persisting it in the state file and exposing it from the instance manager.

Testing

  • Unit tests for the window logic with an injectable clock: open and expiry, idle reset, backoff growth and reset, non-segment files.
  • Unit tests for each guard: rewindMode, recovery.signal, missing standby.signal, archive-only replica cluster.
  • E2E: a replica behind a high-latency archive under write load attaches to the primary within about two windows, and recovery, PITR and replica cluster bootstrap are unaffected.

Non-goals

Describe alternatives you've considered

  • Wait for the upstream fix. The archive-to-streaming switch patch is not committed, adds a GUC and cannot be backported, so supported majors would stay exposed for years.
  • Remove restore_command from replicas, or make it fail fast. This works (it is what the original reporter did) but gives up the resilience that the archive fallback exists to provide, and it must be applied before the problem occurs.
  • Implement it in each CNPG-I plugin. This duplicates the logic and gives inconsistent behaviour across plugins. The instance manager is the single place that sees every invocation.
  • Derive the window from archive_timeout. Rejected, as not related.
  • Add a cluster field for the window now. Deferred: a constant with a backoff is enough to fix the problem, and an API field is hard to remove once users depend on it. See "Evaluation of a new field".
  • Rely on the existing end-of-WAL-stream flag. Not reachable from plugins, never fires while the archive keeps supplying files, and one-shot (see above).
  • A replica-side watchdog that restarts or re-clones a replica with no walreceiver ([Bug]: Replica permanently stuck in wal-restore loop after failover: walreceiver never reconnects, operator reports cluster healthy, burns CPU indefinitely #10419). It is complementary, but it treats the symptom late, and restarts do not change the source selection order.
  • Tune maxParallel or use a faster archive. This helps the backlog regime but does not change the order in which PostgreSQL picks sources.

Additional context

  • Reproducer (backlog regime), posted to pgsql-hackers: message. On a plain master build there was no walsender or walreceiver 30s after restore_command resumed, with 104MB retained on the inactive slot.
  • Original user report (CNPG, PostgreSQL 15.7): pgsql-admin, 2025-11-24
  • Archive-to-streaming switch patch (streaming_replication_retry_interval): commitfest 3663
  • Segment-boundary re-read: commitfest 6343

Related, but not the same: #10419 (replica stuck in a wal-restore loop after failover) and #11646 (invalidated HA slot never recreated, replica stays on archive recovery).

Open questions:

  • Idle threshold: a fixed multiple of the interval, or of wal_retrieve_retry_interval?
  • Should the window count only successful restores, or all invocations?
  • What evidence would justify adding a field later (see "Evaluation of a new field").

Backport?

No

Are you willing to actively contribute to this feature?

Yes

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added this to the 1.31.0 milestone on Oct 31, 2026
  2. self-assigned this
    on Oct 31, 2026
  3. added
    enhancement 🪄
    on Oct 31, 2026
  4. added a commit that references this issue on Nov 6, 2026
    0acf666
  5. moved this to In Progress in Roadmapon Nov 10, 2026
  6. moved this to Waiting for First Review in CloudNativePG operator developmenton Nov 10, 2026
  7. added 4 commits that reference this issue on Nov 10, 2026
    14705f5
    d0be4de
    02cfbc0
    ef0e29f
  8. added a commit that references this issue on Nov 12, 2026
    e7c3cd9
  9. moved this from Waiting for First Review to Done in CloudNativePG operator developmenton Nov 12, 2026
  10. moved this from In Progress to Done in Roadmapon Nov 12, 2026
  11. added 2 commits that reference this issue on Nov 12, 2026
    bc0889a
    8310771
  12. added a commit that references this issue on Nov 16, 2026
    4a3a999
  13. modified the milestones: 1.31.0, 1.30.2 on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement 🪄

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions