Repository navigation
Description
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_commandbe 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
- Inactive replication slot:
restart_lsnstops advancing and the primary retains WAL without bound. Withmax_slot_wal_keep_sizethe slot is eventually invalidated (see [Bug]: Invalidated (wal_status = lost) HA replication slot is never recreated, replica stays on archive recovery forever #11646). hot_standby_feedbackis not delivered while not streaming. With a slot, the last xmin is retained and can hold back vacuum on the primary.- With synchronous replication the standby cannot be a sync or quorum member. With
dataDurability: requiredthis can block commits. Withpreferredit silently reduces durability. - The replica is a poorer failover candidate, and
cnpg_pg_replication_lagreads 0 because it is computed on the replica ([Bug]: Replica permanently stuck in wal-restore loop after failover: walreceiver never reconnects, operator reports cluster healthy, burns CPU indefinitely #10419).
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.signalpresent in PGDATA. Also requirerecovery.signalto 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 (reuseshouldUseEndOfWALStreamFlag): in-cluster replicas, and the designated primary of a replica cluster only when its external cluster hasconnectionParameters. 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 (likestopDelayandfailoverDelay), where0disables it and an unset value keeps the 30 second default. - Naming: follow the semantics of the upstream
streaming_replication_retry_intervalpatch, 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
streamingstate, notcatchup. To be verified insyncrep.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-restoreis 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, missingstandby.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
- Changing how recovery, PITR or bootstrap restore WAL.
- Changing CNPG-I.
- Fixing the PostgreSQL behaviour itself.
- Health detection and remediation of a replica without a walreceiver (see [Bug]: Replica permanently stuck in wal-restore loop after failover: walreceiver never reconnects, operator reports cluster healthy, burns CPU indefinitely #10419).
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_commandfrom 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
maxParallelor 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_commandresumed, 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
feat(instance-manager): let standbys on the WAL archive retry streaming
feat(instance-manager): let standbys on the WAL archive retry streaming
feat(instance-manager): let standbys on the WAL archive retry streaming
feat(instance-manager): let standbys on the WAL archive retry streaming
feat(instance-manager): let standbys on the WAL archive retry streaming
feat(instance-manager): let standbys on the WAL archive retry streaming
feat(instance-manager): let standbys on the WAL archive retry streami…
feat(instance-manager): let standbys on the WAL archive retry streami…
feat(instance-manager): let standbys on the WAL archive retry streami…