Skip to content

[Bug]: ConsistentSystemID stays False (NotFound) for a long time after a replica cluster demotion #11602

Closed

Description

@gabriele-wolfox

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.37

What is your Kubernetes environment?

Self-managed: kind (evaluation)

How did you install the operator?

Other

What happened?

After a role swap between two replica clusters (demote one with .spec.replica.primary, then promote the other with the demotion token), the demoted cluster reports ConsistentSystemID=False (reason NotFound) with an empty .status.systemID.
PostgreSQL is healthy: all instances are in recovery and streaming within seconds. The condition does not recover on its own.
On main it was still False after 150 seconds, in both swap directions, until something wrote to the Cluster, for example kubectl annotate cluster <name> --overwrite test/touch=1.
I expected the operator to sample the instances again once they are back.

The instance status is read before replicaclusterswitch.Reconcile runs.
In the passes that complete the demotion the instances are still fenced or restarting, so none reports a system ID and the condition becomes False/NotFound.
The last of those passes end without a requeue. Status-only updates do not retrigger the controller, and the fence is too short to flip any Pod readiness, so nothing refreshes the condition.

operator-log-cluster-one-main.log

Cluster resource

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-one
  namespace: swap-test
spec:
  instances: 3
  replica:
    primary: cluster-one
    source: cluster-two
  postgresql:
    pg_hba:
      - hostssl replication streaming-replica 0.0.0.0/0 cert
  bootstrap:
    initdb:
      database: appSrc
      owner: userSrc
      postInitApplicationSQL:
        - ALTER ROLE "userSrc" REPLICATION
  storage:
    size: 1Gi
  externalClusters:
    - name: cluster-two
      connectionParameters:
        host: cluster-two-rw
        user: streaming_replica
        sslmode: verify-full
        dbname: appSrc
      sslKey: {name: cluster-two-replication, key: tls.key}
      sslCert: {name: cluster-two-replication, key: tls.crt}
      sslRootCert: {name: cluster-two-ca, key: ca.crt}
    - name: cluster-one
      connectionParameters:
        host: cluster-one-rw
        user: streaming_replica
        sslmode: verify-full
        dbname: appSrc
      sslKey: {name: cluster-one-replication, key: tls.key}
      sslCert: {name: cluster-one-replication, key: tls.crt}
      sslRootCert: {name: cluster-one-ca, key: ca.crt}
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-two
  namespace: swap-test
spec:
  instances: 3
  replica:
    primary: cluster-one
    source: cluster-one
  postgresql:
    pg_hba:
      - hostssl replication streaming-replica 0.0.0.0/0 cert
  bootstrap:
    pg_basebackup:
      database: appSrc
      owner: userSrc
      source: cluster-one
  storage:
    size: 1Gi
  externalClusters:
    - name: cluster-one
      connectionParameters:
        host: cluster-one-rw
        user: streaming_replica
        sslmode: verify-full
        dbname: appSrc
      sslKey: {name: cluster-one-replication, key: tls.key}
      sslCert: {name: cluster-one-replication, key: tls.crt}
      sslRootCert: {name: cluster-one-ca, key: ca.crt}
    - name: cluster-two
      connectionParameters:
        host: cluster-two-rw
        user: streaming_replica
        sslmode: verify-full
        dbname: appSrc
      sslKey: {name: cluster-two-replication, key: tls.key}
      sslCert: {name: cluster-two-replication, key: tls.crt}
      sslRootCert: {name: cluster-two-ca, key: ca.crt}

Relevant log output

The debug excerpt from the `main` run, showing no pass after 09:49:35 until the annotation at 09:52:26, plus the stale status.

Code of Conduct

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

Activity

  1. added
    bug 🐛
    triage
    on Oct 2, 2026
  2. dosubot commented on Oct 2, 2026

    @dosubot

    The instance status behind ConsistentSystemID is sampled once per pass by GetStatusFromInstances and consumed right away by updateClusterStatusThatRequiresInstancesState, which sets the condition to False/NotFound whenever none of the sampled instances reported a system ID [1] [2]. That sampling happens before replicaclusterswitch.Reconcile runs later in the same pass [3]. When that call is the one completing the demotion, the instances were still fenced or restarting at sampling time, so the condition is written with stale data.

    The missing requeue also matches the code. reconcileDemotionToken returns a non-zero ctrl.Result only on specific error paths (token generation failing or a transient auth error). Once cleanupTransitionMetadata succeeds, it returns a bare &ctrl.Result{} [4]. The cleanupTransitionMetadata patch, like the demotion-token patch before it, goes through PatchWithOptimisticLock, which diffs against the live object and skips the API call when nothing is left to change [5]. So on a pass where fencing removal and condition cleanup are the only remaining work, and a prior retry already applied that work, no Update or Patch is issued, no watch event fires, and nothing triggers the controller to resample the instances. That explains why an unrelated write (your kubectl annotate) is needed to get a fresh pass.

    I didn't find an existing fix for this gap. PR #10871 fixed a similar pattern: it addressed stale primary-side status on Database, Publication and Subscription objects after a cluster demotion by re-checking status on the already-reconciled exit path and watching the Cluster for demotion. That change only touches the internal/management/controller resources, not ConsistentSystemID in the main cluster reconciler. PR #11260 and PR #11268 changed how InstancesReportedState is preserved across passes, but detectedSystemID in updateClusterStatusThatRequiresInstancesState still looks only at the freshly sampled list each pass, so they don't fix this either.

    A fix would likely need either a guaranteed requeue right after a demotion or promotion transition completes, or deferring the system ID condition update until after replicaclusterswitch.Reconcile has run in the same pass.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about cloudnative-pg with Dosu Add Dosu to your team

  3. added a commit that references this issue on Oct 2, 2026
    2082a7f
  4. added a commit that references this issue on Oct 14, 2026
    44eefd1
  5. added a commit that references this issue on Oct 14, 2026
    9e33eb2
  6. added a commit that references this issue on Oct 14, 2026
    9db8760
  7. added a commit that references this issue on Oct 15, 2026
    413d732
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