You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: cnpg_collector_sync_replicas{min,max} report stale/incorrect values when using the new synchronous replication API #11309
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.36
What is your Kubernetes environment?
Self-managed: kind (evaluation)
How did you install the operator?
YAML manifest
What happened?
Just by using the new cluster-example.yaml file, and running:
kubectl port-forward cluster-example-1 9187:9187
You can clearly see:
# HELP cnpg_collector_sync_replicas Number of requested synchronous replicas (synchronous_standby_names)
# TYPE cnpg_collector_sync_replicas gauge
cnpg_collector_sync_replicas{value="expected"} 1
cnpg_collector_sync_replicas{value="max"} 0
cnpg_collector_sync_replicas{value="min"} 0
cnpg_collector_sync_replicas{value="observed"} 1
The min and max labels are populated directly from the deprecated legacy fields, regardless of which synchronous replication API the cluster actually uses.
We should improve, also based on discussion #10783
The fix would involve applying the same branching logic for min and max. When cluster.Spec.PostgresConfiguration.Synchronous != nil, both should be derived from cluster.Spec.PostgresConfiguration.Synchronous.Number (since any N semantics collapse min/max to a single value), or the labels could be repurposed to reflect the new API's number field. The legacy MinSyncReplicas/MaxSyncReplicas path would remain correct for clusters still using the deprecated fields.
I investigated this and traced the metric generation through reconcileMetrics
and the synchronous-replication helpers.
The immediate issue is that min and max still read the deprecated MinSyncReplicas / MaxSyncReplicas fields, while expected already handles
the new postgresql.synchronous API. Since the webhook prevents mixing the
two APIs, those fields remain zero for clusters using the new API.
For the reported any + number: 1 configuration, this would therefore produce min=0,max=0,expected=1, even though the configured synchronous replica count
is 1.
While investigating the mapping, I also noticed a related semantic point: dataDurability: preferred can reduce the effective NumSync below the
configured number, so blindly mapping min=max=number for every new-API
configuration may not preserve the existing meaning of the metrics.
My current understanding is:
legacy API → min=MinSyncReplicas, max=MaxSyncReplicas
new API + required → min=max=number
new API + preferred → the effective synchronous count can be lower than number, while the minimum floor should remain 0
I also noticed that GetExpectedSyncReplicasNumber() currently returns the
configured number for the new API rather than the effective NumSync in the
preferred case.
@leonardoce, since you worked on the new synchronous API/data-durability
implementation, does this interpretation match the intended semantics of
these four metrics?
If so, I'm happy to prepare a focused fix and tests for this.
Thanks @Utkarsh-sharma47. At least, we should document this (monitoring page or replication page?), and mention that min/max are deprecated. We should probably converge just on expected and observed (which reads synchronous_standby_names), clarify their meaning and document it.
We should also document how to set alerts where instances is > 1 and observed is 0. we should also alert when expected is 0 as this is a configuration problem. I hope I explained myself.
Thanks, that makes sense. I agree that simply making min/max mirror the new API may not be the best long-term direction if those metrics are tied to the deprecated configuration.
Before I prepare a PR, would you prefer this to be split into:
documentation clarifying expected vs observed, marking min/max as deprecated and documenting the recommended alerts; and
a code change to ensure expected correctly represents the effective synchronous replica count with the new API, including dataDurability: preferred?
In particular, I noticed that GetExpectedSyncReplicasNumber() currently returns the configured number for the new API, whereas preferred can reduce the effective NumSync. I can investigate that path further and propose the smallest change before implementing it
if synchronous replication is needed (instances > 1)
if synchronous replication is configured (verify synchronous commit is not local or off, and synchronous standby names is not empty)
if synchronous replication is required (enabled and data durability is required)
if synchronous replication is working (that we actually have in stat replication the expected number of standby) -> this could actually be how we reinvent 'observed'
Let's make sure evyerone is aligned though. @ardentperf thoughts?
I traced the current metrics and sync-replication paths.
These four signals make more sense:
Needed — instances > 1, with replica clusters as an exception.
Configured — non-empty synchronous_standby_names and synchronous_commit not off/local.
Required — synchronous replication enabled with dataDurability=required.
Working — actual sync standbys from pg_stat_replication.
One important finding: current observed is only the N parsed from synchronous_standby_names; it does not tell us how many standbys are actually participating. For ANY, pg_stat_replication reports sync_state=quorum, while FIRST uses sync, so the working check needs to account for both.
I also found that expected currently uses the configured number; with preferred, the effective NumSync can be lower.
Before changing observed, we should align on whether to change its semantics or introduce a separate working metric.
Activity
dosubot commentedon Aug 11, 2026
The root cause is clear from the code. In the
reconcileMetricsfunction, theminandmaxlabels are hardcoded to read directly from the legacy fields:These are both
0when using the newspec.postgresql.synchronousAPI, since those deprecated fields are never populated.Meanwhile, the
expectedlabel correctly goes throughreplication.GetExpectedSyncReplicasNumber(), which branches on whether the new API is in use:The fix would involve applying the same branching logic for
minandmax. Whencluster.Spec.PostgresConfiguration.Synchronous != nil, both should be derived fromcluster.Spec.PostgresConfiguration.Synchronous.Number(sinceany Nsemantics collapse min/max to a single value), or the labels could be repurposed to reflect the new API'snumberfield. The legacyMinSyncReplicas/MaxSyncReplicaspath would remain correct for clusters still using the deprecated fields.To reply, just mention @dosu.
Docs are dead. Just use Dosu.
Utkarsh-sharma47 commentedon Aug 12, 2026
I investigated this and traced the metric generation through
reconcileMetricsand the synchronous-replication helpers.
The immediate issue is that
minandmaxstill read the deprecatedMinSyncReplicas/MaxSyncReplicasfields, whileexpectedalready handlesthe new
postgresql.synchronousAPI. Since the webhook prevents mixing thetwo APIs, those fields remain zero for clusters using the new API.
For the reported
any + number: 1configuration, this would therefore producemin=0,max=0,expected=1, even though the configured synchronous replica countis 1.
While investigating the mapping, I also noticed a related semantic point:
dataDurability: preferredcan reduce the effectiveNumSyncbelow theconfigured
number, so blindly mappingmin=max=numberfor every new-APIconfiguration may not preserve the existing meaning of the metrics.
My current understanding is:
min=MinSyncReplicas,max=MaxSyncReplicasmin=max=numbernumber, while the minimum floor should remain 0I also noticed that
GetExpectedSyncReplicasNumber()currently returns theconfigured
numberfor the new API rather than the effectiveNumSyncin thepreferred case.
@leonardoce, since you worked on the new synchronous API/data-durability
implementation, does this interpretation match the intended semantics of
these four metrics?
If so, I'm happy to prepare a focused fix and tests for this.
gbartolini commentedon Aug 12, 2026
Thanks @Utkarsh-sharma47. At least, we should document this (monitoring page or replication page?), and mention that min/max are deprecated. We should probably converge just on expected and observed (which reads
synchronous_standby_names), clarify their meaning and document it.We should also document how to set alerts where instances is > 1 and observed is 0. we should also alert when expected is 0 as this is a configuration problem. I hope I explained myself.
Utkarsh-sharma47 commentedon Aug 12, 2026
Thanks, that makes sense. I agree that simply making min/max mirror the new API may not be the best long-term direction if those metrics are tied to the deprecated configuration.
Before I prepare a PR, would you prefer this to be split into:
documentation clarifying expected vs observed, marking min/max as deprecated and documenting the recommended alerts; and
a code change to ensure expected correctly represents the effective synchronous replica count with the new API, including dataDurability: preferred?
In particular, I noticed that GetExpectedSyncReplicasNumber() currently returns the configured number for the new API, whereas preferred can reduce the effective NumSync. I can investigate that path further and propose the smallest change before implementing it
gbartolini commentedon Aug 12, 2026
I think we need something that shows:
Let's make sure evyerone is aligned though. @ardentperf thoughts?
Utkarsh-sharma47 commentedon Aug 12, 2026
I traced the current metrics and sync-replication paths.
These four signals make more sense:
instances > 1, with replica clusters as an exception.synchronous_standby_namesandsynchronous_commitnotoff/local.dataDurability=required.pg_stat_replication.One important finding: current
observedis only theNparsed fromsynchronous_standby_names; it does not tell us how many standbys are actually participating. ForANY,pg_stat_replicationreportssync_state=quorum, whileFIRSTusessync, so the working check needs to account for both.I also found that
expectedcurrently uses the configurednumber; withpreferred, the effectiveNumSynccan be lower.Before changing
observed, we should align on whether to change its semantics or introduce a separateworkingmetric.Would be great to hear everyone's thoughts.
4 remaining items
feat(metrics): report the attached synchronous standbys as observed
fix(metrics): report sync replicas min/max with the new synchronous API
feat(metrics): report the attached synchronous standbys as observed
fix(metrics): report sync replicas min/max with the new synchronous API
fix(metrics): report sync replicas min/max with the new synchronous API
fix(metrics): report sync replicas min/max with the new synchronous A…
fix(metrics): report sync replicas min/max with the new synchronous A…
feat(metrics): report the attached synchronous standbys as observed
feat(metrics): report the attached synchronous standbys as observed
feat(metrics): report the attached synchronous standbys as observed (#…
fix(metrics): report sync replicas min/max with the new synchronous A…