Skip to content

[Bug]: the primary isolation check does not stop an isolated primary from accepting writes #11280

Closed

Description

@mnencia

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.

Version

1.30.0

What version of Kubernetes are you using?

1.34

What is your Kubernetes environment?

Self-managed: k3s

How did you install the operator?

YAML manifest

What happened?

The version and environment fields above describe the third-party reproduction this report is based on, not a deployment of mine. This comes from reading the code after that reproduction was published, and I am filing it because the mechanism checks out against main and against every supported release.

The primary isolation check is documented as fencing an isolated primary and stopping it from accepting writes. It does not do that. It only returns HTTP 500 from the liveness probe (pkg/management/postgres/webserver/probes/liveness.go). The kubelet then signals the container, and the instance manager handles that signal by calling TryShuttingDownSmartFast (internal/cmd/manager/instance/run/lifecycle/lifecycle.go), which issues pg_ctl stop -m smart -w -t 180 first (pkg/management/postgres/instance.go). A PostgreSQL smart shutdown refuses new connections but lets every session that is already open run to completion, so sessions established before the partition keep committing on the isolated primary for up to smartShutdownTimeout, which defaults to 180 seconds.

This is not an argument for changing the shutdown mode generally. Smart shutdown is the right behaviour for the other paths that reach this code, a rolling update, a pod deletion, a node drain, where not killing user sessions is the entire point. The problem is that the isolation path, whose purpose is to stop writes as quickly as possible, inherits a shutdown routine designed for planned termination.

The documentation does not describe this. docs/src/failover.md says "The isolation check stops an isolated primary from continuing to accept writes", and docs/src/instance_manager.md says the isolation check "fences" a primary and that the effect is to "shut it down" when the liveness probe fails. On an asynchronous cluster none of that holds for the sessions that are already connected: they go on committing, and go on being acknowledged, for the length of the smart shutdown.

Evidence

Coroot published a measured reproduction at https://coroot.com/blog/reproducing-split-brain-on-cloudnativepg/, on 1.30.0 with asynchronous replication, and their numbers match the mechanism. They applied a network partition at 18:11:20, the isolation check fired 34 seconds in ("18:11:54 Killing Container postgres failed liveness probe"), and the isolated primary acknowledged its last write at 18:14:55, t+215s. 215 minus 34 is 181, against the 180 second smartShutdownTimeout. Their replacement primary took its first write at 18:13:17, so 99 seconds passed with both primaries acknowledging commits. Of the writes acknowledged after the partition they report 291 rows that pg_rewind later deleted and 562 for which, in their words, "that id now belongs to another client".

On synchronous replication

Synchronous replication closes this, and it is the project's standing advice for not losing commits: it is what the sample cluster and the quickstart configure on main, and #11148 warns when a multi-instance cluster has none. With it configured the isolated primary blocks on COMMIT and never acknowledges, and the blocked transactions are not visible to any other session either, since the transaction stays in the procarray for the whole wait.

This report is therefore about asynchronous clusters, which is what was measured above and what every released version still defaults to.

Suggested direction

Have the isolation check request a fast shutdown rather than only failing the probe, so that stopping the primary does not depend on the kubelet's timing or on a shutdown routine shared with planned termination.

Most of the machinery for that already exists: Instance.RequestFastImmediateShutdown() in pkg/management/postgres/instance.go puts the request on the instance command channel. Note that it is not unused, reconcileOldPrimary already demotes a former primary through it, so the isolation path needs a budget of its own rather than the one that path relies on. Its interaction with the lifecycle loop needs attention too, since an instance stopped for isolation must not be restarted in place.

This only collapses the second of two windows. From the partition until the liveness probe gives up, roughly 30 seconds with the default probe settings, the primary is fully open. After that, and this is the part the fix addresses, only the sessions already established on the isolated side keep writing, for up to smartShutdownTimeout.

Scope

The isolation check landed in 1.27.0, so this affects every supported release as well as main. The primary lease added in 1.30.0 neither helps nor is meant to: an isolated instance manager cannot reach the API server, so it classifies its lease read as unverifiable and keeps running, which docs/src/failover.md already documents.

Until this is fixed, spec.smartShutdownTimeout: 0 skips straight to a fast shutdown and collapses the second window to the probe detection latency.

Cluster resource

$ kubectl get cluster postgres-splitbrain -o json | jq '.spec | {failoverDelay, smartShutdownTimeout, probes}'
{
  "failoverDelay": 0,
  "smartShutdownTimeout": 180,
  "probes": {
    "liveness": {
      "isolationCheck": {"connectionTimeout": 1000, "enabled": true, "requestTimeout": 1000}
    }
  }
}

(Coroot's cluster as they published it; the rest was default.)

Relevant log output

# partition applied at 18:11:20
18:11:54  Killing         Container postgres failed liveness probe
18:11:57  FailingOver     Current primary isn't healthy, initiating a failover
18:11:58  FailoverTarget  Failing over from postgres-splitbrain-1 to postgres-splitbrain-2

# new primary takes its first write at 18:13:17
# isolated primary acknowledges its last write at 18:14:55, t+215s

Code of Conduct

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

Activity

  1. added
    bug 🐛
    triage
    and removed
    triage
    on Jul 30, 2026
  2. def commented on Jul 30, 2026

    @def

    Hi team, I'm the author of the post mentioned above. If there's anything I can help with, just let me know.

  3. ardentperf commented on Jul 31, 2026

    @ardentperf
    Contributor

    wrt. sync replication, i think it's worth just posting a link to #10783 too ~ discussion around moving toward sync-replication-by-default

  4. ardentperf commented on Aug 15, 2026

    @ardentperf
    Contributor

    @mnencia should it be an immediate shutdown?

  5. mnencia commented on Aug 15, 2026

    @mnencia
    MemberAuthor

    @ardentperf I don't see any advantage of an immediate shutdown over a fast one. In both cases all transactions are immediately canceled. An immediate would only add an unnecessary and unneeded crash recovery.

  6. ardentperf commented on Aug 15, 2026

    @ardentperf
    Contributor

    If rewind is happening then do we need the checkpoint that fast shutdown waits for? Won’t hot-standby startup need to replay WAL at the rewind point either way?

    I think the main concern is cases where isolation check kicks in, but cnpg does not choose a new primary, and the restarted primary eventually takes writes again as a primary.

    This seems like the less common case and I was wondering if it makes more sense to optimize for the more common case (about to rewind). Immediate would be significantly faster on systems with large buffer caches - I have seen checkpoints spike to double-digit minutes under demanding workloads. Immediate might enable the system to come back as a replica more quickly.

    Admittedly this stuff is tricky to reason through though, a test with a large dirty buffer cache would be worthwhile just to confirm my thinking about Postgres behavior; I might have it wrong

  7. gbartolini commented on Aug 16, 2026

    @gbartolini
    Contributor

    Given the extraordinary nature of this decision to make (emergency), I am inclined to automatically trigger an immediate shutdown. In my opinion, the most common scenario aligns with what @ardentperf described. However, we could implement a setting that lets users choose the shutdown type (smart, fast, or immediate), with immediate as the default. What are your thoughts on this?

  8. mnencia commented on Aug 16, 2026

    @mnencia
    MemberAuthor

    @ardentperf if my memory doesn't trick me, pg_rewind requires a clean shutdown to work correctly, so if we do an immediate shutdown, when the node restarts, pg_rewind will need to start the instance and shutdown it cleanly before rewinding the node. This is handled automatically by pg_rewind, but is additional work to do (crash recovery, fast shutdown, rewindil).

  9. 15 remaining items

  10. modified the milestones: 1.30.1, 1.31.0 on Sep 24, 2026
  11. PeterZaitsev commented on Oct 14, 2026

    @PeterZaitsev

    Independent reproduction on 1.30.1 (the release that carries #11518, #11347 and #11356), following the method from the Coroot post.

    Setup: k3s v1.35.9, 1 tainted control-plane node + 3 workers (2 vCPU / 4 GB), local-path storage. cluster-example (3 instances, PG 18.6), async replication, defaults: smartShutdownTimeout: 180, failoverDelay: 0, isolation check enabled. Three writers on the control-plane node hold persistent connections through -rw and insert a row every ~100 ms, logging each acknowledged id and commit timestamp.

    Fault: iptables rules inside the primary pod's network namespace drop everything except the client pods' subnet and the node's own pod CIDR, which keeps the kubelet's probes working. That cuts the pod off from the replicas, the operator, the barman-cloud plugin and the API server (10.43.0.0/16). The partition lasted 300 s.

    Result with defaults (1.30.1):

    Event Time after partition
    New primary ready (database system is ready to accept connections) +77.4 s
    Old primary's last acknowledged write +206.7 s
    Dual-primary window 129.3 s, with 3,680 writes acknowledged by the old primary during it
    Writes acknowledged by the isolated old primary after the partition 5,905, all absent from the final table after rejoin

    So the window described here is still present in 1.30.1.

    Same fault with the mitigations (one run each):

    Configuration Old primary's last ack New primary ready Overlap Acked writes lost
    smartShutdownTimeout: 0 +23.3 s +73.8 s none 660 (async lag before fencing)
    smartShutdownTimeout: 0, failoverDelay: 30 +25.7 s +107.6 s none 740
    synchronous: {method: any, number: 1, dataDurability: required, failoverQuorum: true} no acks after partition +76.4 s none 0

    With quorum sync, the writers' sessions stayed blocked on the old primary until the smart shutdown expired (+207 s), so they could not reach the new primary for about 130 s after it was ready.

    Two related observations on 1.30.1:

    I can share the scripts if that is useful.

  12. ardentperf commented on Oct 20, 2026

    @ardentperf
    Contributor

    @PeterZaitsev thanks for testing against 1.30+ which includes the new primary lease feature. we probably need to see how that is interacting with this, since it may change the failure modes and how things play out

  13. gbartolini commented on Oct 23, 2026

    @gbartolini
    Contributor

    18. @PeterZaitsev thanks for testing against 1.30+ which includes the new primary lease feature. we probably need to see how that is interacting with this, since it may change the failure modes and how things play out

    I agree with you, thanks @PeterZaitsev. We are implementing several changes to enhance asynchronous replication scenarios and are shifting our focus towards data durability rather than self-healing for these situations. By the time we reach epic #11721, we expect to be in a much better position, and we would appreciate it if the tests could be re-run at that point (using the development snapshots).

  14. mnencia commented on Oct 29, 2026

    @mnencia
    MemberAuthor

    Fixed on main. #11357 moved the isolation check to the primary lease and #11358 turned the liveness probe into a watchdog. An isolated primary now gets an immediate shutdown 10-13s after its last lease renewal. If PostgreSQL is still running at 13s, the instance manager sends SIGQUIT and exits. Both happen before a replica can claim the lease at 15s. The smart shutdown window is gone, and the docs match the code.

    1.30 keeps the old probe-based check, so the window is still there. We did not backport the fix. The first attempt (#11504) fired on ordinary connectivity blips, and the lease rework is too big to backport. #11518 documents the window on 1.30. Until 1.31, smartShutdownTimeout: 0 closes it.

    Some loss remains on async clusters. Writes acknowledged before the stop (up to about 13s) are lost, and only synchronous replication prevents that. Further hardening is tracked in #11721.

    @PeterZaitsev, @def: it would be great to re-run your tests on a 1.31 development snapshot.

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