Skip to content

[Bug]: quorum failover missing logic around synchronous_standby_names changes with preferred dataDurability #8790

Todo

Description

@ardentperf

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

[email protected]

Version

trunk (main)

What version of Kubernetes are you using?

1.31

What is your Kubernetes environment?

Self-managed: kind (evaluation)

How did you install the operator?

YAML manifest

What happened?

When testing preferred dataDurability and quorum failover with jepsen on 2-node clusters, jepsen detected data loss in 47% of runs. i think this might be due to some missing logic around handing state changes with preferred dataDurability.

[full test results]

the logs from the test run show that synchronous_standby_names is set before pg_stat_replication shows any streaming replication connections.

the original design included a process for checking pg_stat_replication after updating synchronous_standby_names, but I may have forgotten it when we worked on the initial implementation.

should we continually check pg_stat_replication.state? probably we only need to check this after changes to synchronous_standby_names or after promoting a new writer. postgres should block writes if they aren't on enough replicas". i think the key question (and patroni had to solve this too) is "how do you know the moment when the new synchronous_standby_names is applied. i think patroni looks at pg_stat_replication.state for this; that seems like reasonable logic to me. continuing to check pg_stat_replication should not be needed once synchronous_standby_names is confirmed applied. possibly we only zero it out when first changing cluster config, and stop checking once we're 100% sync repl config is applied.


patroni implements similar logic - but also adds an additional check on state changes to ensure LSN movement and catchup of replicas:

patroni/patroni@3161f31

When synchronous_standby_names GUC is changed PostgreSQL nearly
immediately starts reporting corresponding walsenders as synchronous,
while in fact they maybe didn't reach this state yet. To mitigate this
problem we memorize current flush lsn on the primary right after change
of synchronous_standby_names got visible and use it as an additional
check for walsenders. The walsender will be counted as truly "sync"
only when write/flush/replay_lsn on it reached memorized LSN and the
application_name is known to be a part of synchronous_standby_names.

The _handle_synchronous_standby_names_change() function also has:

self._postgresql.query('SELECT pg_catalog.txid_current()') # Ensure
some WAL traffic to move replication


i'm actually not sure that even the patroni logic is fully correct. after spending some time in the postgres code itself, i saw that SyncRepWaitForLSN() doesn't begin blocking COMMITs until the sync_standbys_defined variable is set - and this is set by the checkpointer process some time after synchronous_standby_names is updated. so i'm actually not sure if postgres still has a short window after synchronous_standby_names is changed to a non-blank value before it begins to block COMMITs (which is what guarantees we can make a promotion/failover decision without losing any transactions that were acknowledged as durable to the client). i've raised a question on hackers but didn't hear back yet.

https://www.postgresql.org/message-id/20251006225933.1dde33c7%40ardentperf.com

Cluster resource

CNPG playground the the below diff:


diff --git a/demo/yaml/eu/pg-eu-legacy.yaml b/demo/yaml/eu/pg-eu-legacy.yaml
index 75ff0e7..e7706e0 100644
--- a/demo/yaml/eu/pg-eu-legacy.yaml
+++ b/demo/yaml/eu/pg-eu-legacy.yaml
@@ -2,8 +2,10 @@ apiVersion: postgresql.cnpg.io/v1
 kind: Cluster
 metadata:
   name: pg-eu
+  annotations:
+    alpha.cnpg.io/failoverQuorum: "true"
 spec:
-  instances: 3
+  instances: 2
   imageName: ghcr.io/cloudnative-pg/postgresql:18-system-trixie

   storage:
@@ -32,6 +34,10 @@ spec:

   postgresql:
     # See https://cloudnative-pg.io/documentation/current/postgresql_conf/#the-postgresql-section
+    synchronous:
+      method: any
+      number: 1
+      dataDurability: preferred
     parameters:
       # Some very basic Postgres settings
       max_connections: '100'
diff --git a/lab/exercise-3-jepsen/jepsen-job.yaml b/lab/exercise-3-jepsen/jepsen-job.yaml
index 1966d49..360a10e 100644
--- a/lab/exercise-3-jepsen/jepsen-job.yaml
+++ b/lab/exercise-3-jepsen/jepsen-job.yaml
@@ -44,7 +44,7 @@ spec:
                   --max-txn-length 1 \
                   --key-dist uniform \
                   --concurrency 50 \
-                  --rate 1000 \
+                  --rate 2000 \
                   --time-limit 300 \
                   --test-count 1

Relevant log output

# egrep '(success|unknown|crash|failure)' jepsen-test_*|sort -k2|uniq -c|grep -v ' 0 '
      3 1 crashed
     85 1 failures
     95 1 successes

sample from log:

Wed Oct  1 09:56:57 UTC 2025
              now              | pid |   state   | flush_lsn |    flush_lag    | sync_state |          reply_time           |        ssn
-------------------------------+-----+-----------+-----------+-----------------+------------+-------------------------------+-------------------
 2025-10-01 09:57:45.879312+00 | 652 | streaming | 0/78AB8D8 | 00:00:00.007004 | quorum     | 2025-10-01 09:57:45.893693+00 | ANY 1 ("pg-eu-2")
(1 row)

pod "pg-eu-1" force deleted
Wed Oct  1 09:57:46 UTC 2025
              now              | pid | state | flush_lsn | flush_lag | sync_state | reply_time |        ssn
-------------------------------+-----+-------+-----------+-----------+------------+------------+-------------------
 2025-10-01 09:58:37.523975+00 |     |       |           |           |            |            | ANY 1 ("pg-eu-1")
(1 row)

pod "pg-eu-2" force deleted
Wed Oct  1 09:58:37 UTC 2025
              now              | pid | state | flush_lsn | flush_lag | sync_state | reply_time |        ssn
-------------------------------+-----+-------+-----------+-----------+------------+------------+-------------------
 2025-10-01 09:59:27.801893+00 |     |       |           |           |            |            | ANY 1 ("pg-eu-2")
(1 row)

pod "pg-eu-1" force deleted
Wed Oct  1 09:59:28 UTC 2025
              now              | pid |   state   | flush_lsn | flush_lag | sync_state |          reply_time           |        ssn
-------------------------------+-----+-----------+-----------+-----------+------------+-------------------------------+-------------------
 2025-10-01 10:00:27.974272+00 | 604 | streaming | 0/A0FADA0 |           | quorum     | 2025-10-01 10:00:26.916736+00 | ANY 1 ("pg-eu-1")
(1 row)

pod "pg-eu-2" force deleted

Code of Conduct

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

Activity

  1. added
    triage
    on Oct 8, 2025
  2. dosubot commented on Oct 8, 2025

    @dosubot

    There isn’t an existing issue tracking this specific bug in CloudNativePG. Your analysis is correct: the current implementation does not include Patroni-style logic for confirming that changes to synchronous_standby_names are fully applied and that replicas are truly synchronized before allowing failover or promotion. The operator mainly waits for pod readiness and uniform configuration, but does not explicitly track WAL LSNs or poll pg_stat_replication to ensure safe promotion after config changes—leaving a race condition possible, especially with dataDurability: preferred and quorum failover in small clusters. This matches the risk you observed in Jepsen tests and aligns with the known limitations in the docs (failover quorum, replication).

    A recent PR (#8680) improved quorum-based failover logic for two-instance clusters, but it does not implement the deeper LSN/state validation seen in Patroni. The docs recommend using synchronous replication only in clusters with 3+ instances or when maxSyncReplicas < (instances - 1) due to these limitations.

    If you need to mitigate data loss risk, consider increasing cluster size or using stricter durability settings. Manual promotion should be a last resort, as documented. For more details on the failover quorum mechanism and its trade-offs, see here.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about cloudnative-pg Share Dosu with your team

  3. legrandlegrand commented on Oct 9, 2025

    @legrandlegrand

    Hi,
    I was wondering how a cluster with 3 instance (2 standby) was exposed,
    and found the answer here
    https://ardentperf.com/2025/10/05/testing-cloudnativepg-preferred-data-durability/

    thank you for all theses tests.
    Regards
    PAscal

  4. ardentperf commented on Oct 11, 2025

    @ardentperf
    ContributorAuthor

    FYI - similar log output in another test (3 instances, rapid cascading failures); at first glance i think it might be the same underlying issue. log output looks similar.

    ubuntu@cnpg2:~/cnpg-playground/lab/exercise-3-jepsen$ egrep '(success|unknown|crash|failure)' jepsen-test_20251011_061138.log|sort -k2|uniq -c|grep -v ' 0 '
         24 1 failures
         49 1 successes
    
    ubuntu@cnpg2:~/cnpg-playground/lab/exercise-3-jepsen$ head -45 jepsen-test_20251011_061138.log
    Starting jepsen test 'writer-kill' at Sat Oct 11 06:11:38 UTC 2025
    MIN_HEALTHY_REPLICAS_FOR_KILL: 0
    MIN_SECONDS_BETWEEN_KILLS: 15
    On branch main
    Your branch is up to date with 'origin/main'.
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   demo/yaml/eu/pg-eu-legacy.yaml
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            install_20250930_083050.log
            lab/exercise-3-jepsen/jepsen-test_20251011_061138.log
    
    no changes added to commit (use "git add" and/or "git commit -a")
    diff --git a/demo/yaml/eu/pg-eu-legacy.yaml b/demo/yaml/eu/pg-eu-legacy.yaml
    index 75ff0e7..f41ac34 100644
    --- a/demo/yaml/eu/pg-eu-legacy.yaml
    +++ b/demo/yaml/eu/pg-eu-legacy.yaml
    @@ -2,6 +2,8 @@ apiVersion: postgresql.cnpg.io/v1
     kind: Cluster
     metadata:
       name: pg-eu
    +  annotations:
    +    alpha.cnpg.io/failoverQuorum: "true"
     spec:
       instances: 3
       imageName: ghcr.io/cloudnative-pg/postgresql:18-system-trixie
    @@ -32,6 +34,9 @@ spec:
    
       postgresql:
         # See https://cloudnative-pg.io/documentation/current/postgresql_conf/#the-postgresql-section
    +    synchronous:
    +      method: any
    +      number: 1
         parameters:
           # Some very basic Postgres settings
           max_connections: '100'
    cluster.postgresql.cnpg.io "pg-us" deleted
    Switched to context "kind-k8s-eu".
    If you don't see a command prompt, try pressing enter.
    64 bytes from 172.18.0.2: seq=1 ttl=63 time=0.130 ms
    64 bytes from 172.18.0.2: seq=2 ttl=63 time=0.084 ms
    

    log excerpt (note the failover from node 2 to node 1 at a time when there's no streaming replication but s_s_n is set)

    ubuntu@cnpg2:~/cnpg-playground/lab/exercise-3-jepsen$ less jepsen-test_20251011_061138.log
    ...
    Sat Oct 11 08:09:47 UTC 2025
                  now              | pid |   state   | flush_lsn |    flush_lag    | sync_state |          reply_time           |                  ssn
    -------------------------------+-----+-----------+-----------+-----------------+------------+-------------------------------+---------------------------------------
     2025-10-11 08:10:02.709532+00 | 572 | streaming | 0/7B15E10 | 00:00:00.013377 | quorum     | 2025-10-11 08:10:02.711629+00 | ANY 1 ("pg-eu-3","pg-eu-1","pg-eu-2")
    (1 row)
    
    pod "pg-eu-2" force deleted
    Sat Oct 11 08:10:02 UTC 2025
                  now              | pid |   state   | flush_lsn |    flush_lag    | sync_state |         reply_time          |                  ssn
    -------------------------------+-----+-----------+-----------+-----------------+------------+-----------------------------+---------------------------------------
     2025-10-11 08:10:18.396467+00 | 171 | streaming | 0/7DCF630 | 00:00:00.008432 | quorum     | 2025-10-11 08:10:18.3958+00 | ANY 1 ("pg-eu-3","pg-eu-1","pg-eu-2")
    (1 row)
    
    pod "pg-eu-2" force deleted
    Sat Oct 11 08:10:18 UTC 2025
                  now              | pid | state | flush_lsn | flush_lag | sync_state | reply_time |                  ssn
    -------------------------------+-----+-------+-----------+-----------+------------+------------+---------------------------------------
     2025-10-11 08:10:34.020432+00 |     |       |           |           |            |            | ANY 1 ("pg-eu-3","pg-eu-1","pg-eu-2")
    (1 row)
    
    pod "pg-eu-2" force deleted
    Sat Oct 11 08:10:34 UTC 2025
                  now              | pid | state | flush_lsn | flush_lag | sync_state | reply_time |                  ssn
    -------------------------------+-----+-------+-----------+-----------+------------+------------+---------------------------------------
     2025-10-11 08:10:49.779175+00 |     |       |           |           |            |            | ANY 1 ("pg-eu-3","pg-eu-2","pg-eu-1")
    (1 row)
    
    pod "pg-eu-1" force deleted
    Sat Oct 11 08:10:50 UTC 2025
                  now              | pid | state | flush_lsn | flush_lag | sync_state | reply_time |                  ssn
    -------------------------------+-----+-------+-----------+-----------+------------+------------+---------------------------------------
     2025-10-11 08:11:06.686782+00 |     |       |           |           |            |            | ANY 1 ("pg-eu-3","pg-eu-2","pg-eu-1")
    (1 row)
    
    pod "pg-eu-1" force deleted
    Sat Oct 11 08:11:06 UTC 2025
                  now              | pid |   state   | flush_lsn |    flush_lag    | sync_state |          reply_time           |                  ssn
    -------------------------------+-----+-----------+-----------+-----------------+------------+-------------------------------+---------------------------------------
     2025-10-11 08:11:22.295584+00 | 100 | streaming | 0/913D7D8 | 00:00:00.013857 | quorum     | 2025-10-11 08:11:22.292741+00 | ANY 1 ("pg-eu-3","pg-eu-2","pg-eu-1")
    (1 row)
    
    pod "pg-eu-1" force deleted
    ...
    
  5. ardentperf commented on Oct 11, 2025

    @ardentperf
    ContributorAuthor

    the [email reply from Ants] on the hackers list explains the LSN thing well - especially his example at the end. that part makes sense to me now. i like the idea of calling something like pg_current_xact_id() to intentionally generate a WAL record after retrieving the current LSN on the writer, so that there's something to push to replicas. for data safety, i agree w Ants explanation why it would be good to actually ensure replicas have progressed to the effective LSN of the s_s_n change before allowing failover. this check (on replicas) would be in addition to the check on the primary that pg_stat_replication reports valid sync_state on replicas.

  6. ardentperf commented on Oct 12, 2025

    @ardentperf
    ContributorAuthor

    fyi @leonardoce - i was able to build cnpg from leonardoce@25c25be and try it out on my CNPG Lab VM where I'm running the jepsen tests

    i need to take a closer look and see what it's doing. at first glance it looks like failovers are still happening when pg_stat_replicaion.sync_state is blank or async

    (failover from 1 to 2 at 6:40:36 and failover from 2 to 1 at 6:43:39)

    ubuntu@cnpg2:~/cnpg-playground$ k logs cnpg-controller-manager-86d4b4c6cb-mdbbf -n cnpg-system | head
    {"level":"info","ts":"2025-10-12T06:18:51.796251965Z","logger":"setup","msg":"Starting CloudNativePG Operator","version":"1.27.0","build":{"Version":"1.27.0-dev122","Commit":"25c25be4a","Date":"2025-10-08"}}
    {"level":"info","ts":"2025-10-12T06:18:51.796494307Z","logger":"setup","msg":"Listening for changes on all namespaces"}
    
    Image Image Image
  7. ardentperf commented on Nov 4, 2025

    @ardentperf
    ContributorAuthor

    https://www.postgresql.org/docs/current/warm-standby.html#SYNCHRONOUS-REPLICATION-HA

    When a standby first attaches to the primary, it will not yet be properly synchronized. This is described as catchup mode. Once the lag between standby and primary reaches zero for the first time we move to real-time streaming state. The catch-up duration may be long immediately after the standby has been created. If the standby is shut down, then the catch-up period will increase according to the length of time the standby has been down. The standby is only able to become a synchronous standby once it has reached streaming state. This state can be viewed using the pg_stat_replication view.

  8. github-actions commented on Jan 4, 2026

    @github-actions
    Contributor

    This issue is stale because it has been open for 60 days with no activity.

  9. github-actions commented on Feb 7, 2026

    @github-actions
    Contributor

    This issue was closed because it has been inactive for 14 days since being marked as stale.

  10. added a commit that references this issue on Oct 15, 2026
    1ccc058
  11. added a commit that references this issue on Nov 23, 2026
    85242e8
  12. GellertBabel commented on Nov 24, 2026

    @GellertBabel
    Contributor

    Based on my testing the issue still applies on master and on many related open PRs. Can we plan a fix for it?

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

Metadata

Metadata

Labels

no-staletriage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions