Repository navigation
Description
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
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.
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:
When
synchronous_standby_namesGUC 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
ofsynchronous_standby_namesgot 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_nameis known to be a part ofsynchronous_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.
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 1Relevant 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 deletedCode of Conduct
- I agree to follow this project's Code of Conduct
Activity
dosubot commentedon Oct 8, 2025
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_namesare 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 pollpg_stat_replicationto ensure safe promotion after config changes—leaving a race condition possible, especially withdataDurability: preferredand 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.
legrandlegrand commentedon Oct 9, 2025
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
ardentperf commentedon Oct 11, 2025
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.
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)
ardentperf commentedon Oct 11, 2025
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.ardentperf commentedon Oct 12, 2025
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_stateis blank or async(failover from 1 to 2 at 6:40:36 and failover from 2 to 1 at 6:43:39)
ardentperf commentedon Nov 4, 2025
https://www.postgresql.org/docs/current/warm-standby.html#SYNCHRONOUS-REPLICATION-HA
github-actions commentedon Jan 4, 2026
This issue is stale because it has been open for 60 days with no activity.
github-actions commentedon Feb 7, 2026
This issue was closed because it has been inactive for 14 days since being marked as stale.
fix(failover): don't promote replicas missing commits
fix(failover): don't promote replicas missing commits
GellertBabel commentedon Nov 24, 2026
Based on my testing the issue still applies on master and on many related open PRs. Can we plan a fix for it?