Skip to content

[Bug]: cnpg_collector_sync_replicas{min,max} report stale/incorrect values when using the new synchronous replication API #11309

Description

@gbartolini

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

Cluster resource

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-example
spec:
  instances: 3

  postgresql:
    synchronous:
      method: any
      number: 1

  storage:
    size: 1Gi

Relevant log output

Code of Conduct

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

Activity

  1. added this to the 1.30.1 milestone on Aug 11, 2026
  2. self-assigned this
    on Aug 11, 2026
  3. added
    bug 🐛
    on Aug 11, 2026
  4. dosubot commented on Aug 11, 2026

    @dosubot

    The root cause is clear from the code. In the reconcileMetrics function, the min and max labels are hardcoded to read directly from the legacy fields:

    exporter.Metrics.SyncReplicas.WithLabelValues("min").Set(float64(cluster.Spec.MinSyncReplicas))
    exporter.Metrics.SyncReplicas.WithLabelValues("max").Set(float64(cluster.Spec.MaxSyncReplicas))

    These are both 0 when using the new spec.postgresql.synchronous API, since those deprecated fields are never populated.

    Meanwhile, the expected label correctly goes through replication.GetExpectedSyncReplicasNumber(), which branches on whether the new API is in use:

    func GetExpectedSyncReplicasNumber(ctx context.Context, cluster *apiv1.Cluster) int {
        if cluster.Spec.PostgresConfiguration.Synchronous != nil {
            return cluster.Spec.PostgresConfiguration.Synchronous.Number
        }
        syncReplicas, _ := getSyncReplicasData(ctx, cluster)
        return syncReplicas
    }

    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.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

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

  5. Utkarsh-sharma47 commented on Aug 12, 2026

    @Utkarsh-sharma47
    Contributor

    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.

  6. gbartolini commented on Aug 12, 2026

    @gbartolini
    ContributorAuthor

    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.

  7. Utkarsh-sharma47 commented on Aug 12, 2026

    @Utkarsh-sharma47
    Contributor

    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

  8. gbartolini commented on Aug 12, 2026

    @gbartolini
    ContributorAuthor

    I think we need something that shows:

    1. if synchronous replication is needed (instances > 1)
    2. if synchronous replication is configured (verify synchronous commit is not local or off, and synchronous standby names is not empty)
    3. if synchronous replication is required (enabled and data durability is required)
    4. 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?

  9. Utkarsh-sharma47 commented on Aug 12, 2026

    @Utkarsh-sharma47
    Contributor

    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.

    Would be great to hear everyone's thoughts.

  10. modified the milestones: 1.30.1, 1.31.0 on Sep 23, 2026
  11. modified the milestones: 1.31.0, 1.32.0 on Nov 10, 2026
  12. 4 remaining items

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

Metadata

Metadata

Assignees

Labels

bug 🐛

Type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions