Skip to content

[Bug]: 1.27.1 stats query bug #9106

Closed

Description

@aniravi24

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

1.27 (latest patch)

What version of Kubernetes are you using?

1.33

What is your Kubernetes environment?

Cloud: Amazon EKS

How did you install the operator?

Helm

What happened?

I upgraded from 1.27.0 to 1.27.1 on a cluster and I'm seeing the output below in a postgres cluster.

Cluster resource

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  annotations:
    cnpg.io/skipEmptyWalArchiveCheck: enabled
    pulumi.com/waitFor: condition=Ready
    signoz.io/path: /metrics
    signoz.io/port: '9187'
    signoz.io/scrape: 'true'
  creationTimestamp: '2025-09-26T05:14:02Z'
  generation: 4
  name: postgres-cluster-demo
  namespace: pg-demo
  resourceVersion: 'REDACTED'
  uid: REDACTED-UID
spec:
  affinity:
    nodeSelector:
      demo.io/node-type: postgres-demo
    podAntiAffinityType: preferred
    tolerations:
      - effect: NoSchedule
        key: demo.io/node-type
        operator: Equal
        value: postgres-demo
  bootstrap:
    recovery:
      database: app
      owner: app
      source: postgres-cluster-demo
  enablePDB: true
  enableSuperuserAccess: true
  externalClusters:
    - name: postgres-cluster-demo
      plugin:
        enabled: true
        isWALArchiver: false
        name: barman-cloud.cloudnative-pg.io
        parameters:
          barmanObjectName: postgres-cluster-demo-object-store
          serverName: postgres-cluster-demo
  failoverDelay: 0
  imageName: ghcr.io/cloudnative-pg/postgresql:18.0-standard-trixie@sha256:6f0f41dffed078d700963eb6e0ca918815d603c97ef1875fe1db01db70d18ed5
  instances: 2
  logLevel: info
  maxSyncReplicas: 0
  minSyncReplicas: 0
  monitoring:
    customQueriesConfigMap:
      - key: queries
        name: cnpg-default-monitoring
    disableDefaultQueries: false
    enablePodMonitor: false
  plugins:
    - enabled: true
      isWALArchiver: true
      name: barman-cloud.cloudnative-pg.io
      parameters:
        barmanObjectName: postgres-cluster-demo-object-store
        serverName: postgres-cluster-demo
  postgresGID: 26
  postgresUID: 26
  postgresql:
    parameters:
      archive_mode: 'on'
      archive_timeout: 5min
      checkpoint_completion_target: '0.9'
      default_statistics_target: '100'
      dynamic_shared_memory_type: posix
      effective_cache_size: 3GB
      effective_io_concurrency: '200'
      full_page_writes: 'on'
      huge_pages: 'off'
      log_destination: csvlog
      log_directory: /controller/log
      log_filename: postgres
      log_lock_waits: 'on'
      log_min_duration_statement: '30000'
      log_rotation_age: '0'
      log_rotation_size: '0'
      log_truncate_on_rotation: 'false'
      logging_collector: 'on'
      maintenance_work_mem: 256MB
      max_connections: '200'
      max_parallel_workers: '32'
      max_replication_slots: '32'
      max_wal_size: 4GB
      max_worker_processes: '32'
      min_wal_size: 1GB
      pg_stat_statements.max: '10000'
      pg_stat_statements.track: all
      random_page_cost: '1.1'
      shared_buffers: 1GB
      shared_memory_type: mmap
      shared_preload_libraries: ''
      ssl_max_protocol_version: TLSv1.3
      ssl_min_protocol_version: TLSv1.3
      wal_buffers: 16MB
      wal_keep_size: 512MB
      wal_level: logical
      wal_log_hints: 'on'
      wal_receiver_timeout: 5s
      wal_sender_timeout: 5s
      work_mem: 5041kB
    syncReplicaElectionConstraint:
      enabled: false
    synchronous:
      dataDurability: required
      method: any
      number: 1
  primaryUpdateMethod: switchover
  primaryUpdateStrategy: unsupervised
  probes:
    liveness:
      isolationCheck:
        connectionTimeout: 1000
        enabled: true
        requestTimeout: 1000
  replicationSlots:
    highAvailability:
      enabled: true
      slotPrefix: _cnpg_
    synchronizeReplicas:
      enabled: true
    updateInterval: 30
  resources: {}
  smartShutdownTimeout: 180
  startDelay: 3600
  stopDelay: 1800
  storage:
    pvcTemplate:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 50Gi
      storageClassName: gp3
      volumeMode: Filesystem
    resizeInUseVolumes: true
  switchoverDelay: 3600
status:
  availableArchitectures:
    - goArch: amd64
      hash: a2ec96b19543b25c66838e0250631fba8b7b474fdd94231cfc0f3361dac257a8
    - goArch: arm64
      hash: 80017fba324cd54525ecd9a3377c359c5ee3ae590d79b57aa2244fc524f49000
  certificates:
    clientCASecret: postgres-cluster-demo-ca
    expirations:
      postgres-cluster-demo-ca: 2025-12-25 05:09:02 +0000 UTC
      postgres-cluster-demo-replication: 2025-12-25 05:09:02 +0000 UTC
      postgres-cluster-demo-server: 2025-12-25 05:09:02 +0000 UTC
    replicationTLSSecret: postgres-cluster-demo-replication
    serverAltDNSNames:
      - postgres-cluster-demo-rw
      - postgres-cluster-demo-rw.pg-demo
      - postgres-cluster-demo-rw.pg-demo.svc
      - postgres-cluster-demo-rw.pg-demo.svc.cluster.local
      - postgres-cluster-demo-r
      - postgres-cluster-demo-r.pg-demo
      - postgres-cluster-demo-r.pg-demo.svc
      - postgres-cluster-demo-r.pg-demo.svc.cluster.local
      - postgres-cluster-demo-ro
      - postgres-cluster-demo-ro.pg-demo
      - postgres-cluster-demo-ro.pg-demo.svc
      - postgres-cluster-demo-ro.pg-demo.svc.cluster.local
    serverCASecret: postgres-cluster-demo-ca
    serverTLSSecret: postgres-cluster-demo-server
  cloudNativePGCommitHash: 9daa68132
  cloudNativePGOperatorHash: a2ec96b19543b25c66838e0250631fba8b7b474fdd94231cfc0f3361dac257a8
  conditions:
    - lastTransitionTime: '2025-09-27T11:50:30Z'
      message: A single, unique system ID was found across reporting instances.
      reason: Unique
      status: 'True'
      type: ConsistentSystemID
    - lastTransitionTime: '2025-11-10T18:38:58Z'
      message: Cluster is Ready
      reason: ClusterIsReady
      status: 'True'
      type: Ready
    - lastTransitionTime: '2025-11-08T16:48:28Z'
      message: Continuous archiving is working
      reason: ContinuousArchivingSuccess
      status: 'True'
      type: ContinuousArchiving
    - lastTransitionTime: '2025-11-10T01:00:21Z'
      message: Backup was successful
      reason: LastBackupSucceeded
      status: 'True'
      type: LastBackupSucceeded
  configMapResourceVersion:
    metrics:
      cnpg-default-monitoring: '94669024'
  currentPrimary: postgres-cluster-demo-3
  currentPrimaryTimestamp: '2025-11-08T16:48:24.940335Z'
  healthyPVC:
    - postgres-cluster-demo-1
    - postgres-cluster-demo-3
  image: ghcr.io/cloudnative-pg/postgresql:18.0-standard-trixie@sha256:6f0f41dffed078d700963eb6e0ca918815d603c97ef1875fe1db01db70d18ed5
  instanceNames:
    - postgres-cluster-demo-1
    - postgres-cluster-demo-3
  instances: 2
  instancesReportedState:
    postgres-cluster-demo-1:
      ip: 10.0.98.37
      isPrimary: false
      timeLineID: 6
    postgres-cluster-demo-3:
      ip: 10.0.101.207
      isPrimary: true
      timeLineID: 6
  instancesStatus:
    healthy:
      - postgres-cluster-demo-1
      - postgres-cluster-demo-3
  latestGeneratedNode: 3
  managedRolesStatus: {}
  pgDataImageInfo:
    image: ghcr.io/cloudnative-pg/postgresql:18.0-standard-trixie@sha256:6f0f41dffed078d700963eb6e0ca918815d603c97ef1875fe1db01db70d18ed5
    majorVersion: 18
  phase: Cluster in healthy state
  pluginStatus:
    - capabilities:
        - TYPE_RECONCILER_HOOKS
        - TYPE_LIFECYCLE_SERVICE
      name: barman-cloud.cloudnative-pg.io
      version: 0.9.0
  poolerIntegrations:
    pgBouncerIntegration: {}
  pvcCount: 2
  readService: postgres-cluster-demo-r
  readyInstances: 2
  secretsResourceVersion:
    applicationSecretVersion: 'REDACTED'
    clientCaSecretVersion: 'REDACTED'
    replicationSecretVersion: 'REDACTED'
    serverCaSecretVersion: 'REDACTED'
    serverSecretVersion: 'REDACTED'
    superuserSecretVersion: 'REDACTED'
  switchReplicaClusterStatus: {}
  systemID: 'REDACTED-SYSTEMID'
  targetPrimary: postgres-cluster-demo-3
  targetPrimaryTimestamp: '2025-11-08T11:47:49.610065-05:00'
  timelineID: 6
  topology:
    instances:
      postgres-cluster-demo-1: {}
      postgres-cluster-demo-3: {}
    nodesUsed: 2
    successfullyExtracted: true
  writeService: postgres-cluster-demo-rw

Relevant log output

{"level":"error","ts":"2025-11-08T16:41:01.349452822Z","msg":"while collecting pg_stat_wal","logging_pod":"postgres-cluster-application-3","error":"sql: Scan error on column index 4, name \"stats_reset\": converting NULL to string is unsupported","stacktrace":"github.com/cloudnative-pg/machinery/pkg/log.(*logger).Error\n\tpkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:125\ngithub.com/cloudnative-pg/machinery/pkg/log.Error\n\tpkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:163\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/metricserver.(*Exporter).collectPgMetrics\n\tpkg/management/postgres/webserver/metricserver/pg_collector.go:454\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/metricserver.(*Exporter).Collect\n\tpkg/management/postgres/webserver/metricserver/pg_collector.go:321\ngithub.com/prometheus/client_golang/prometheus.(*Registry).Gather.func1\n\tpkg/mod/github.com/prometheus/[email protected]/prometheus/registry.go:456"}

Code of Conduct

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

Activity

  1. added
    triage
    on Nov 8, 2025
  2. gbartolini commented on Nov 10, 2025

    @gbartolini
    Contributor

    Can you please provide the info to reproduce this issue?

  3. aniravi24 commented on Nov 10, 2025

    @aniravi24
    ContributorAuthor

    @gbartolini what would be helpful here? I added the cluster resource definition to the main issue description now. The only thing I did was install the latest helm chart to upgrade from 1.27.0 to 1.27.1 and am now seeing this error on both pod instances.

  4. bmironov commented on Nov 18, 2025

    @bmironov
    Contributor

    Hi @gbartolini ,

    My guess here is that manifest attempts to "restore" this cluster in .spec.bootstrap.recovery from external DB cluster and specifies own name as name of that cluster. Perhaps, intention was to restore from own backup.

    Condition when pg_stat_wal.stats_reset is NULL might arise when initdb wasn't executed for new PostgreSQL cluster as initialization step.

  5. aniravi24 commented on Jan 6, 2026

    @aniravi24
    ContributorAuthor

    This is a new cluster that hasn't been restored from a backup (as far as I'm aware). I'm still seeing it on my latest 1.28. It seems to happen when I upgrade a cluster?

    {"level":"error","ts":"2026-01-06T16:07:06.860291843Z","msg":"while collecting pg_stat_wal","logging_pod":"postgres-cluster-demo-1","error":"sql: Scan error on column index 4, name stats_reset: converting NULL to string is unsupported","stacktrace":"github.com/cloudnative-pg/machinery/pkg/log.(*logger).Errorpkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:125github.com/cloudnative-pg/machinery/pkg/log.Errorpkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:163github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/metricserver.(*Exporter).updateInstanceMetricspkg/management/postgres/webserver/metricserver/pg_collector.go:482github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/metricserver.(*Exporter).Collectpkg/management/postgres/webserver/metricserver/pg_collector.go:322github.com/prometheus/client_golang/prometheus.(*Registry).Gather.func1pkg/mod/github.com/prometheus/[email protected]/prometheus/registry.go:456"}
    
  6. redbaron commented on Jan 22, 2026

    @redbaron
    Contributor

    We see it in one cluster as well. Interestingly it tries to run that query on a replica, but pg_stat_wal doesn't seem to be very meaningful on a replica.

  7. dosubot commented on Apr 24, 2026

    @dosubot

    Hi, @aniravi24. I'm Dosu, and I'm helping the cloudnative-pg team manage their backlog and am marking this issue as stale.

    Issue Summary:

    • You reported a "sql: Scan error" related to NULL values in pg_stat_wal.stats_reset after upgrading CloudNativePG from 1.27.0 to 1.27.1 on Amazon EKS.
    • The error persists in version 1.28 and occurs only when upgrading existing clusters, not new or restored ones.
    • Contributors suggested the issue might be due to initdb not being run or the query running on replicas where pg_stat_wal is less meaningful.
    • Maintainers have requested reproduction details to investigate further but have not yet received them.

    Next Steps:

    • Please confirm if this issue is still relevant with the latest version of CloudNativePG and provide any additional reproduction details if possible.
    • If we do not hear back within 7 days, this issue will be automatically closed.

    Thank you for your understanding and contribution!

  8. aniravi24 commented on Apr 24, 2026

    @aniravi24
    ContributorAuthor

    @dosubot I still see this all the time, it's always in my logs. Have done several upgrades of CNPG now

    pg-sample postgres-cluster-sample-4 postgres {"level":"error","ts":"2026-04-24T00:13:08.90211938Z","msg":"while collecting pg_stat_wal","logging_pod":"postgres-cluster-sample-4","error":"sql: Scan error on column index 4, name \"stats_reset\": converting NULL to string is unsupported","stacktrace":"github.com/cloudnative-pg/machinery/pkg/log.(*logger).Error\n\tpkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:125\ngithub.com/cloudnative-pg/machinery/pkg/log.Error\n\tpkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:163\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/metricserver.(*Exporter).updateInstanceMetrics\n\tpkg/management/postgres/webserver/metricserver/pg_collector.go:482\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/metricserver.(*Exporter).Collect\n\tpkg/management/postgres/webserver/metricserver/pg_collector.go:322\ngithub.com/prometheus/client_golang/prometheus.(*Registry).Gather.func1\n\tpkg/mod/github.com/prometheus/[email protected]/prometheus/registry.go:456"}
    
  9. dosubot commented on Apr 24, 2026

    @dosubot

    This is a confirmed bug that is still present in the codebase. The root cause is that the StatsReset field in the PgStatWal struct is defined as a plain (non-nullable) Go string [1], but PostgreSQL's pg_stat_wal.stats_reset column can legitimately be NULL — particularly on replica instances that have never generated WAL statistics. When the pgx driver tries to scan a NULL into a non-nullable string, it fails with the error you're seeing.

    The SQL queries used to collect these metrics don't include any COALESCE or NULL protection either [2].

    No fix has been merged yet. There are two viable approaches:

    1. Type-safe fix — Change StatsReset from string to sql.NullString (or *string) and handle the NULL case when emitting Prometheus labels. The codebase already uses sql.NullString elsewhere for similar situations [3].

    2. Architectural fix — Add a predicate query to only collect pg_stat_wal metrics on primary instances, similar to what was done for pg_stat_archiver in PR #9411. This avoids the problem entirely since the data isn't meaningful on replicas anyway (as @redbaron noted above).

    The error doesn't break your cluster — it just means pg_stat_wal metrics are not being collected, and the error line is logged repeatedly. But it's definitely noise that should be fixed. This issue could benefit from a PR if anyone wants to pick it up.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about cloudnative-pg with Dosu Add Dosu to your team

  10. honi-at-simspace commented on May 5, 2026

    @honi-at-simspace

    @dosu
    I put a PR to solve this issue using your approach 1.

  11. honi-at-simspace commented on May 8, 2026

    @honi-at-simspace
  12. added
    bug 🐛
    and removed
    triage
    on Jul 15, 2026
  13. added theissue type on Jul 15, 2026
  14. HoniSanders commented on Jul 15, 2026

    @HoniSanders
    Contributor

    I opened a fix for this: #11207. Since PG18, pg_stat_wal.stats_reset can be NULL, but the exporter scanned it into a non-nullable string. The PR scans into sql.NullString at the scan site (per @mnencia's note in #9788), with tests for the NULL case.

  15. added a commit that references this issue on Jul 30, 2026
    90d74a8
  16. added a commit that references this issue on Jul 30, 2026
    953536e
  17. added 3 commits that reference this issue on Jul 30, 2026
    d3eeb71
    d47eef3
    f7e81bd
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

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions