Skip to content

Collect WAL stats metric from primary only. - #9788

Closed
redbaron wants to merge 1 commit into
cloudnative-pg:mainfrom
redbaron:no-wal-metric-on-replicas
Closed

redbaron wants to merge 1 commit into
cloudnative-pg:mainfrom
redbaron:no-wal-metric-on-replicas

Conversation

@redbaron

Copy link
Copy Markdown
Contributor

pg_stat_wal view shows WAL generation stats and only primary generates WAL.

This resolves problem where pg_stat_wal can be empty on a replica which was never a primary before, which causes metric collection to fail on a NULL conversion.

fixes #9106

@redbaron
redbaron requested a review from a team January 22, 2026 11:42
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jan 22, 2026
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.25 release-1.28 labels Jan 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@dosubot dosubot Bot added bug 🐛 Something isn't working ok to merge 👌 This PR can be merged labels Jan 22, 2026
@redbaron
redbaron force-pushed the no-wal-metric-on-replicas branch from c1056c5 to 1987fab Compare January 22, 2026 11:42
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jan 27, 2026
@redbaron

Copy link
Copy Markdown
Contributor Author

There was 1.29 release without this fix. Could you please consider merging it for 1.29.1 or 1.30 at least?

@redbaron

Copy link
Copy Markdown
Contributor Author

@gbartolini , @jsilvela , what would it take to merge this in?

@jsilvela

Copy link
Copy Markdown
Contributor

a second approval, @redbaron. You already have mine

@honi-at-simspace

Copy link
Copy Markdown

we are having this problem too. Can this be merged?

@honi-at-simspace

Copy link
Copy Markdown

I also put another PR to fix the underlying issue (https://github.com/cloudnative-pg/cloudnative-pg/pull/10624) which allows for NULL values for pg_stat_wal.stats_reset, which would resolve this issue as well as other times that it can be NULL on a primary as well.

pg_stat_wal view shows WAL generation stats and only primary generates
WAL.

This resolves problem where pg_stat_wal can be empty on a replica which
was never a primary before, which causes metric collection to fail on a
NULL conversion.

Signed-off-by: Maxim Ivanov <[email protected]>
@mnencia
mnencia force-pushed the no-wal-metric-on-replicas branch from 1987fab to 9a3c093 Compare July 15, 2026 07:58
@mnencia

mnencia commented Jul 15, 2026

Copy link
Copy Markdown
Member

Thanks for digging into this, and sorry it sat for a while.

Looking closer, I don't think gating on isPrimary is the right fix here. IsPrimary() checks for the absence of standby.signal on disk, but designated primaries in a replica-cluster topology run with standby.signal present (they're PG standbys) and archive_mode = always, so isPrimary() returns false for them permanently. Gating this collector on it would silently stop collecting pg_stat_wal on those nodes for the life of the cluster, not just fix the reported crash.

The actual defect is that PgStatWal.StatsReset (probes.go) scans pg_stat_wal.stats_reset into a non-nullable Go string, but that column is nullable in Postgres. The fix belongs there, at the scan site, whether that's a COALESCE in the query or a nullable scan target, rather than trying to predict which instances will and won't have a populated value.

I'm going to close this in favor of a fix at the scan site. Appreciate you tracking down the original crash, that part of the diagnosis was right.

@mnencia mnencia closed this Jul 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@redbaron

Copy link
Copy Markdown
Contributor Author

designated primaries in a replica-cluster topology run with standby.signal present (they're PG standbys) and archive_mode = always, so isPrimary() returns false for them permanently.

If they are standby they don't generate WAL on their own and querying pg_stat_wal there should return empty values there.

Are you saying there can be a situation where IsPrimary() == false , but pod still generates own WAL?

@HoniSanders

Copy link
Copy Markdown
Contributor

@mnencia I put a new PR here that makes the Null string bug directly: #11207.
Can you review that?

Thank you!

@HoniSanders HoniSanders mentioned this pull request Jul 15, 2026
4 tasks done
@mnencia

mnencia commented Jul 15, 2026

Copy link
Copy Markdown
Member

designated primaries in a replica-cluster topology run with standby.signal present (they're PG standbys) and archive_mode = always, so isPrimary() returns false for them permanently.

If they are standby they don't generate WAL on their own and querying pg_stat_wal there should return empty values there.

Are you saying there can be a situation where IsPrimary() == false , but pod still generates own WAL?

Good point, you're right.

archive_mode = always only re-archives WAL the standby already received. It does not make the standby create new WAL. A designated primary stays in recovery mode as long as it follows the replica cluster, so it does not generate WAL either. pg_stat_wal would be empty there too, just like on any other standby. My archive_mode argument was wrong, I'm dropping it.

One smaller concern remains. IsPrimary() just checks if standby.signal is missing. Right after promotion, there may be a short moment where this is already true, but Postgres has not fully started as primary yet. I have not confirmed this happens, so it's only a guess for now.

Either way, checking primary vs replica is just a proxy for the real condition (is stats_reset set). Proxies like this often cause problems later. #11207 fixes the real problem directly, so I prefer that approach.

@redbaron
redbaron deleted the no-wal-metric-on-replicas branch July 15, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-requested ◀️ This pull request should be backported to all supported releases bug 🐛 Something isn't working lgtm This PR has been approved by a maintainer ok to merge 👌 This PR can be merged release-1.28 release-1.30 size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 1.27.1 stats query bug

6 participants