Skip to content

[Bug]: Invalidated (wal_status = lost) HA replication slot is never recreated, replica stays on archive recovery forever #11646

Open

Description

@chobostar

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

tag me in github

Version

1.30 (latest patch)

What version of Kubernetes are you using?

1.35

What is your Kubernetes environment?

Other

How did you install the operator?

Helm

What happened?

A node running one of our replicas was down for about two days. The primary wrote more WAL than max_slot_wal_keep_size (6GB) during that time, so PostgreSQL invalidated the replica's HA slot:

 slot_name          | active | wal_status | invalidation_reason
--------------------+--------+------------+---------------------
 _cnpg_cluster_example_2 | f      | lost       | wal_removed

When the node came back, the replica could not start streaming and kept falling back to restoring WAL from the archive:

FATAL: could not start WAL streaming: ERROR: can no longer access replication slot "_cnpg_cluster_example_2"
DETAIL: This replication slot has been invalidated due to "wal_removed".

It caught up from the archive, but it never went back to streaming. The cluster reported Cluster in healthy state with all instances ready, and stayed like that until we fixed it by hand.

The other 15 replicas on the same node reconnected fine because their slots had not been invalidated.

Looking at the code, reconcilePrimaryHAReplicationSlots only creates a slot when no slot with that name exists. infrastructure.List does not read wal_status, so a lost slot counts as present and is left alone. An invalidated slot cannot be reused, it has to be dropped and created again.

There is a second, smaller problem. After we dropped the slot manually, the operator did not recreate it. On the primary, HA slots seem to be reconciled only when the Cluster object changes (instance_controller.go), so in a quiet, healthy cluster that can take a long time. The replica kept logging replication slot "_cnpg_cluster_example_2" does not exist. We had to create the slot ourselves:

SELECT pg_drop_replication_slot('_cnpg_cluster_example_2');
SELECT pg_create_physical_replication_slot('_cnpg_cluster_example_2');

After that the replica switched to streaming within a few seconds.

Expected: if a HA slot is inactive and wal_status = 'lost', the operator drops and recreates it. If the replica can no longer catch up, it should at least report the instance as not healthy instead of showing the cluster as healthy.

To reproduce:

  1. Create a 3-instance cluster with max_slot_wal_keep_size set (e.g. 1GB).
  2. Stop one replica (e.g. scale down its node or cordon it and delete the pod).
  3. Write more than max_slot_wal_keep_size of WAL on the primary.
  4. Bring the replica back. Its slot is lost, the replica only restores from the archive, and the cluster is reported as healthy.

Possibly related, but not the same: #8726, #10419.

Cluster resource

spec:
  instances: 3
  imageCatalogRef:        # PostgreSQL 18.4
    apiGroup: postgresql.cnpg.io
    kind: ClusterImageCatalog
    name: <custom catalog>
    major: 18
  postgresql:
    parameters:
      max_slot_wal_keep_size: "6GB"
      wal_keep_size: "512MB"
      archive_timeout: "5min"
  replicationSlots:
    highAvailability:
      enabled: true
      slotPrefix: _cnpg_
    synchronizeReplicas:
      enabled: true
    updateInterval: 30
  plugins:
    - name: pgbackrest.cnpg.opera.com
      isWALArchiver: true

Relevant log output

{"logger":"postgres","msg":"record","logging_pod":"cluster-example-2","record":{"error_severity":"FATAL","message":"could not start WAL streaming: ERROR:  can no longer access replication slot \"_cnpg_cluster_example_2\"\nDETAIL:  This replication slot has been invalidated due to \"wal_removed\"."}}
{"logger":"wal-restore","msg":"Refusing to restore future timeline history file"}
{"logger":"postgres","msg":"record","logging_pod":"cluster-example-2","record":{"error_severity":"LOG","message":"waiting for WAL to become available at 3/9000018"}}

Code of Conduct

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

Activity

  1. added
    bug 🐛
    triage
    on Oct 12, 2026
  2. changed the title [-][Bug]:[/-] [+][Bug]: Invalidated (`wal_status = lost`) HA replication slot is never recreated, replica stays on archive recovery forever[/+] on Oct 12, 2026
  3. LeVadim commented on Nov 3, 2026

    @LeVadim

    We hit the same thing on CloudNativePG 1.30.1 with PostgreSQL 18.6, using plugin-barman-cloud v0.15.1 as the WAL archiver, so it is not specific to the pgBackRest plugin. This was on kind, with a 3-instance cluster and HA replication slots at their defaults. The only relevant settings were max_slot_wal_keep_size: 256MB and archive_timeout: 60s.

    Steps

    1. Fence one replica: kubectl cnpg fencing on <cluster> <cluster>-2.
    2. On the primary, write more than 256 MB of WAL (pgbench -i -s 70), then run CHECKPOINT, SELECT pg_switch_wal() and CHECKPOINT again.
    3. The replica's slot on the primary is now wal_status = lost, active = f.
    4. Unfence the replica: kubectl cnpg fencing off <cluster> <cluster>-2.

    What we saw 240 s after unfencing

    • On the primary, pg_stat_replication lists only <cluster>-3 (streaming, quorum).
    • pg_replication_slots still shows _cnpg_<cluster>_2 | lost | f.
    • The Cluster reports 3 ready instances and Cluster in healthy state.
    • The replica's log holds 52 restored log file, 28 could not start WAL streaming: ERROR: can no longer access replication slot, and 27 waiting for WAL to become available lines.

    So the replica keeps up from the archive, but it does not stream again on its own and stays out of the synchronous quorum. Nothing in the Cluster status shows it.

    The second problem you describe reproduces too

    • After SELECT pg_drop_replication_slot('_cnpg_<cluster>_2') on the primary, the slot had not been recreated 120 s later. The replica was still not streaming.
    • Deleting the replica's pod (kubectl delete pod <cluster>-2) got things moving. Within 150 s the slot was back as reserved and the replica was streaming/quorum, with no manual pg_create_physical_replication_slot.
    • So "drop the lost slot, then delete the replica's pod" is a way out that stays inside the operator. It may help others until this is fixed.

    The same symptom showed on a second cluster in the same environment. It looks related to #11762 and #11808.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions