Skip to content

[Bug]: Replicas can remain on WAL archive recovery indefinitely and never resume streaming (PostgreSQL behaviour) #11762

Description

@gbartolini

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?

other (unsupported)

What is your Kubernetes environment?

Other

How did you install the operator?

Other

What happened?

Important

I think this is a PostgreSQL bug and that it should be solved in PostgreSQL, ideally with a fix that can be backpatched to supported releases. The problem, a reproducer and measurements are in my reply on pgsql-hackers: https://www.postgresql.org/message-id/CA%2BVUV5r0XoJdXpM_NFcxw5_YXmcHoAWb909Fu9ApUsH16Sf6_w%40mail.gmail.com

In short: a standby with both primary_conninfo and restore_command prefers the WAL archive and tries streaming only when restore_command fails. If the primary writes faster than the standby restores, the next segment is already in the archive every time, so the standby does not return to streaming until it has consumed the whole archive backlog. That can take several minutes, as @ardentperf proved. In the meantime the replication slot is inactive and the primary retains WAL, and with synchronous replication the standby cannot act as a synchronous standby. The thread has the details and states what was measured and what is reasoning.

CloudNativePG replicas have exactly this shape (restore_command, primary_conninfo and, with HA replication slots, the default, primary_slot_name). This comes from reading the source and was not reproduced on a live CloudNativePG cluster. The measurements are on plain PostgreSQL.

If the PostgreSQL hackers do not agree that this is a bug (quite possible), or until a fix reaches the supported releases, CloudNativePG needs a workaround. This issue tracks that.

Expected: a replica whose primary is reachable returns to streaming within a short, bounded time, whatever the archive backlog.

Related issues:

What the PostgreSQL documentation says (current, PostgreSQL 18):

Documented, each piece on its own:

  • Source order, 26.2.2 Standby Server Operation: the standby begins "by restoring all WAL available in the archive location, calling restore_command. Once it reaches the end of WAL available there and restore_command fails, it tries to restore any WAL available in the pg_wal directory", then tries streaming, and "if the connection is later disconnected, the standby goes back to step 1 and tries to restore the file from the archive again. This loop of retries from the archive, pg_wal, and via streaming replication goes on until the server is stopped or is promoted."
  • The archive as a safety net, 26.2.5 Streaming Replication: with a WAL archive accessible from the standby, wal_keep_size or a replication slot "are not required, since the standby can always use the archive to catch up provided it retains enough segments". This is the architecture CloudNativePG follows.
  • Slot risk, 26.2.6 Replication Slots: slots protect WAL "even when the standby is disconnected", and the caution says they "can cause the server to retain so many WAL segments that they fill up the space allocated for pg_wal", pointing to max_slot_wal_keep_size.
  • Cap, max_slot_wal_keep_size: with -1 (the default) "replication slots may retain an unlimited amount of WAL files"; otherwise the standby using the slot "may no longer be able to continue replication due to removal of required WAL files".
  • hot_standby_feedback without a slot, same section 26.2.6: it "provides no protection during any time period when the standby is not connected". The parameter's own entry, hot_standby_feedback, warns it "can cause database bloat on the primary for some workloads".
  • Synchronous standby state, 26.2.8.4 Planning for High Availability: "The standby is only able to become a synchronous standby once it has reached streaming state", and "If the standby is shut down, then the catch-up period will increase according to the length of time the standby has been down." See also synchronous_standby_names ("currently connected and streaming data in real-time").
  • restore_command must return nonzero for files not in the archive, and 26.2.4 adds that it "should return immediately if the file does not exist".
  • wal_retrieve_retry_interval is described only as how long the standby waits "when WAL data is not available from any sources".

Not documented (the combination):

  • The consequence of the source order: nothing says that while archive restores keep succeeding the standby never streams, or that under write load this can last indefinitely. It follows from "once it reaches the end of WAL available there", but it is not spelled out.
  • No link between restore_command and slots. The restore_command entry and the slots section never mention each other, and the slot caution describes retention only for a disconnected standby, not one replaying from the archive, which produces the same retention.
  • Nothing connects a standby restoring from the archive to commits waiting under synchronous replication. A standby that is behind and restoring is not in streaming state, so it cannot act as a synchronous standby. The sentence exists, the consequence is not drawn.
  • The documentation says feedback is sent to the primary, and covers the no-slot case, but not that no feedback is sent while the standby is on the archive.
  • No mitigation is suggested (for example making restore_command fail fast) and nothing is advised to prevent the stall.

Each piece is documented, the dangerous combination is not. That supports treating this as a bug and adding a documentation note.

Way ahead: pursue the fix in PostgreSQL first. If that is not agreed, the workaround in CloudNativePG would be to have our WAL restore CNPG-I implementation inject a forced failure after 30 seconds on the archive. The failure appears in the PostgreSQL logs and lets PostgreSQL try to resume streaming. If streaming does not resume, PostgreSQL goes back to restore_command. The period would not be configurable in the initial implementation.

Details should be discussed in the comments or via exploratory PRs.

Cluster resource

Relevant log output

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 26, 2026
  2. self-assigned this
    on Oct 26, 2026
  3. added
    bug 🐛
    on Oct 26, 2026
  4. ardentperf commented on Oct 26, 2026

    @ardentperf
    Contributor

    I am going to work on pursuing this on the hackers list too

    One of the big difficulties here is this:

    Once it reaches the end of WAL available there and restore_command fails, it tries to restore any WAL available in the pg_wal directory", then tries streaming

    With separate DATA and WAL disks, I've found it's pretty easy for the replica to download a bunch of WAL quickly from object storage while replay lags behind.

    In this situation, core postgres is not going to attempt streaming again until it catches up with whats on disk - even after restore_command signals postgres to retry streaming. there might not be any way to mitigate this outside a core postgres change.

    if core postgres intends to support sync replication, then i think it would logically follow that this needs to be treated as a bug, since core postgres will block all commits on a primary without a streaming connection.

    at a CNPG level, i think we already have mitigations: (1) two replicas, because it's much less likely to see a network distruption break the streaming on both at the same time. (2) preferred durability with streaming readiness probes, for cases with one replica.

  5. gbartolini commented on Nov 23, 2026

    @gbartolini
    ContributorAuthor

    Closed by #11868 and included in 1.30.2

  6. modified the milestones: 1.31.0, 1.30.2 on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐛

Type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions