Skip to content

fix: handle load_balance_hosts null value - #6810

Merged
gbartolini merged 1 commit into
cloudnative-pg:mainfrom
usernamedt:fix-metric-collection-cnpg
Feb 12, 2025
Merged

gbartolini merged 1 commit into
cloudnative-pg:mainfrom
usernamedt:fix-metric-collection-cnpg

Conversation

@usernamedt

@usernamedt usernamedt commented Feb 10, 2025 •

Copy link
Copy Markdown
Contributor

Hi, everyone! After testing the PgBouncer 1.24 and CNPG 1.25 with this fix applied I see the following errors in the pooler logs:

{
  "level": "error",
  "ts": "2025-02-10T13:01:21.430417823Z",
  "msg": "Error while executing SHOW POOLS",
  "logger": "pgbouncer-manager",
  "error": "sql: Scan error on column index 16, name \"load_balance_hosts\": converting NULL to int is unsupported",
  "stacktrace": "github.com/cloudnative-pg/machinery/pkg/log.(*logger).Error\n\t/root/go/pkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:125\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/pgbouncer/metricsserver.(*Exporter).collectShowPools\n\tpkg/management/pgbouncer/metricsserver/pools.go:301\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/pgbouncer/metricsserver.(*Exporter).collectPgBouncerMetrics\n\tpkg/management/pgbouncer/metricsserver/pgbouncer_collector.go:143\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/pgbouncer/metricsserver.(*Exporter).Collect\n\tpkg/management/pgbouncer/metricsserver/pgbouncer_collector.go:119\ngithub.com/prometheus/client_golang/prometheus.(*Registry).Gather.func1\n\t/root/go/pkg/mod/github.com/prometheus/[email protected]/prometheus/registry.go:456"
}
pgbouncer=# show pools;
    database     |         user          | cl_active | cl_waiting | cl_active_cancel_req | cl_waiting_cancel_req | sv_active | sv_active_cancel | sv_being_canceled | sv_idle | sv_used | sv_tested | sv_login | maxwait | maxwait_us |  pool_mode  | load_balance_hosts
-----------------+-----------------------+-----------+------------+----------------------+-----------------------+-----------+------------------+-------------------+---------+---------+-----------+----------+---------+------------+-------------+--------------------
 test_cluster    | test_user             |        19 |          0 |                    0 |                     0 |         0 |                0 |                 0 |       3 |       5 |         0 |        0 |       0 |          0 | transaction |
 pgbouncer       | pgbouncer             |         1 |          0 |                    0 |                     0 |         0 |                0 |                 0 |       0 |       0 |         0 |        0 |       0 |          0 | statement   |
 postgres        | cnpg_pooler_pgbouncer |         0 |          0 |                    0 |                     0 |         0 |                0 |                 0 |       2 |       2 |         0 |        0 |       0 |          0 | transaction |
(3 rows)

Given the fact that I am using more or less standard setup, I propose to ignore the null values here instead of error.

@usernamedt
usernamedt requested a review from a team February 10, 2025 16:40
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.22 labels Feb 10, 2025
@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

@usernamedt
usernamedt force-pushed the fix-metric-collection-cnpg branch from ee710e1 to b0fb19e Compare February 10, 2025 16:43
@armru
armru self-requested a review February 10, 2025 18:57
@armru

armru commented Feb 10, 2025 •

Copy link
Copy Markdown
Member

Hello @usernamedt, thanks for the contribution, I will take a look ASAP.

@armru armru changed the title Fix load_balance_hosts metric collection for pgBouncer 1.24 fix: load_balance_hosts metric collection for pgBouncer 1.24 Feb 10, 2025
@armru armru changed the title fix: load_balance_hosts metric collection for pgBouncer 1.24 fix: handle load_balance_hosts null value Feb 10, 2025
@armru armru added this to the 1.25.1 milestone Feb 10, 2025
@sxd

sxd commented Feb 11, 2025

Copy link
Copy Markdown
Contributor

/test depth=push

@github-actions

Copy link
Copy Markdown
Contributor

@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Feb 11, 2025
@sxd
sxd force-pushed the fix-metric-collection-cnpg branch from b0fb19e to 7176c52 Compare February 12, 2025 16:21
@gbartolini
gbartolini merged commit d0e8f5f into cloudnative-pg:main Feb 12, 2025
cnpg-bot pushed a commit that referenced this pull request Feb 17, 2025
When using the `SHOW STATS` command to handle metrics for pgBouncer,
expect NULL as a result and ensure it is handled properly.
This issue was introduced in PR #6630.

Relates #6566

Signed-off-by: Daniil Zakhlystov <[email protected]>
Co-authored-by: Daniil Zakhlystov <[email protected]>
(cherry picked from commit d0e8f5f)
cnpg-bot pushed a commit that referenced this pull request Feb 17, 2025
When using the `SHOW STATS` command to handle metrics for pgBouncer,
expect NULL as a result and ensure it is handled properly.
This issue was introduced in PR #6630.

Relates #6566

Signed-off-by: Daniil Zakhlystov <[email protected]>
Co-authored-by: Daniil Zakhlystov <[email protected]>
(cherry picked from commit d0e8f5f)
cnpg-bot pushed a commit that referenced this pull request Feb 17, 2025
When using the `SHOW STATS` command to handle metrics for pgBouncer,
expect NULL as a result and ensure it is handled properly.
This issue was introduced in PR #6630.

Relates #6566

Signed-off-by: Daniil Zakhlystov <[email protected]>
Co-authored-by: Daniil Zakhlystov <[email protected]>
(cherry picked from commit d0e8f5f)
mnencia pushed a commit that referenced this pull request Sep 14, 2026
`collectShowPools` picks its scan layout from the number of columns
`SHOW POOLS` returns. Every layout PgBouncer reports today is covered,
but the next release that adds a column falls into the 13 destination
fallback, and the scan then fails on every row. It would not read as
missing data either: the error branch falls through to the gauge writes,
so each pool metric is published as a genuine zero under empty `database`
and `user` labels.

Bind each column to its gauge by name instead, from one table that
`Describe`, `Reset`, `Collect` and the scan derive from, and skip the row
when the scan fails. #11366 does the same for `SHOW STATS`, where that
switch already broke on 1.24.

`load_balance_hosts` comes along. PgBouncer reports it as text holding
`disable` or `round-robin`, and null for a single-host pool, while the
scan expected an integer; the generated `[databases]` section never
configures more than one host, so nothing surfaces today. Encode it the
way `pool_mode` already is, correct its help text, which described a
count of hosts, and drop the scan error fixed in #6810 from the docs
sample.

Signed-off-by: Armando Ruocco <[email protected]>
mnencia pushed a commit that referenced this pull request Sep 14, 2026
`collectShowPools` picks its scan layout from the number of columns
`SHOW POOLS` returns. Every layout PgBouncer reports today is covered,
but the next release that adds a column falls into the 13 destination
fallback, and the scan then fails on every row. It would not read as
missing data either: the error branch falls through to the gauge writes,
so each pool metric is published as a genuine zero under empty `database`
and `user` labels.

Bind each column to its gauge by name instead, from one table that
`Describe`, `Reset`, `Collect` and the scan derive from, and skip the row
when the scan fails. #11366 does the same for `SHOW STATS`, where that
switch already broke on 1.24.

`load_balance_hosts` comes along. PgBouncer reports it as text holding
`disable` or `round-robin`, and null for a single-host pool, while the
scan expected an integer; the generated `[databases]` section never
configures more than one host, so nothing surfaces today. Encode it the
way `pool_mode` already is, correct its help text, which described a
count of hosts, and drop the scan error fixed in #6810 from the docs
sample.

Signed-off-by: Armando Ruocco <[email protected]>
mnencia added a commit that referenced this pull request Sep 14, 2026
`collectShowPools` picks its scan layout from the number of columns
`SHOW POOLS` returns. Every layout PgBouncer reports today is covered,
but the next release that adds a column falls into the 13-destination
fallback, and the scan fails on every row. The error branch falls
through to the gauge writes anyway, so each pool metric ends up
published as zero, under empty `database` and `user` labels.

Bind each column to its gauge by name instead. One table now drives
`Describe`, `Reset`, `Collect`, and the scan, and a row is skipped when
its scan fails. #11366 does the same for `SHOW STATS`, where this
already broke on 1.24. This PR doesn't depend on it.

`load_balance_hosts` also needed a fix. PgBouncer reports it as text:
`disable` or `round-robin`, and null for a single-host pool. The scan
expected an integer instead. CNPG's generated `[databases]` section
never configures more than one host, so this never showed up in
practice. It's now encoded the same way `pool_mode` already is. The help
text no longer describes a host count, and the docs sample drops the
scan error that #6810 fixed.

A column PgBouncer renames instead of adds still disappears silently
under the by-name bind. Log it as a warning, here and in the
already-merged `SHOW STATS` collector from #11366, so a rename shows up
in the logs instead of just vanishing. A new test also checks that one
pool's row failing to scan doesn't blank out the pools that scan fine
right after it.

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
cnpg-bot pushed a commit that referenced this pull request Sep 14, 2026
`collectShowPools` picks its scan layout from the number of columns
`SHOW POOLS` returns. Every layout PgBouncer reports today is covered,
but the next release that adds a column falls into the 13-destination
fallback, and the scan fails on every row. The error branch falls
through to the gauge writes anyway, so each pool metric ends up
published as zero, under empty `database` and `user` labels.

Bind each column to its gauge by name instead. One table now drives
`Describe`, `Reset`, `Collect`, and the scan, and a row is skipped when
its scan fails. #11366 does the same for `SHOW STATS`, where this
already broke on 1.24. This PR doesn't depend on it.

`load_balance_hosts` also needed a fix. PgBouncer reports it as text:
`disable` or `round-robin`, and null for a single-host pool. The scan
expected an integer instead. CNPG's generated `[databases]` section
never configures more than one host, so this never showed up in
practice. It's now encoded the same way `pool_mode` already is. The help
text no longer describes a host count, and the docs sample drops the
scan error that #6810 fixed.

A column PgBouncer renames instead of adds still disappears silently
under the by-name bind. Log it as a warning, here and in the
already-merged `SHOW STATS` collector from #11366, so a rename shows up
in the logs instead of just vanishing. A new test also checks that one
pool's row failing to scan doesn't blank out the pools that scan fine
right after it.

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
(cherry picked from commit d2c5ccd)
cnpg-bot pushed a commit that referenced this pull request Sep 14, 2026
`collectShowPools` picks its scan layout from the number of columns
`SHOW POOLS` returns. Every layout PgBouncer reports today is covered,
but the next release that adds a column falls into the 13-destination
fallback, and the scan fails on every row. The error branch falls
through to the gauge writes anyway, so each pool metric ends up
published as zero, under empty `database` and `user` labels.

Bind each column to its gauge by name instead. One table now drives
`Describe`, `Reset`, `Collect`, and the scan, and a row is skipped when
its scan fails. #11366 does the same for `SHOW STATS`, where this
already broke on 1.24. This PR doesn't depend on it.

`load_balance_hosts` also needed a fix. PgBouncer reports it as text:
`disable` or `round-robin`, and null for a single-host pool. The scan
expected an integer instead. CNPG's generated `[databases]` section
never configures more than one host, so this never showed up in
practice. It's now encoded the same way `pool_mode` already is. The help
text no longer describes a host count, and the docs sample drops the
scan error that #6810 fixed.

A column PgBouncer renames instead of adds still disappears silently
under the by-name bind. Log it as a warning, here and in the
already-merged `SHOW STATS` collector from #11366, so a rename shows up
in the logs instead of just vanishing. A new test also checks that one
pool's row failing to scan doesn't blank out the pools that scan fine
right after it.

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
(cherry picked from commit d2c5ccd)
cnpg-bot pushed a commit that referenced this pull request Sep 14, 2026
`collectShowPools` picks its scan layout from the number of columns
`SHOW POOLS` returns. Every layout PgBouncer reports today is covered,
but the next release that adds a column falls into the 13-destination
fallback, and the scan fails on every row. The error branch falls
through to the gauge writes anyway, so each pool metric ends up
published as zero, under empty `database` and `user` labels.

Bind each column to its gauge by name instead. One table now drives
`Describe`, `Reset`, `Collect`, and the scan, and a row is skipped when
its scan fails. #11366 does the same for `SHOW STATS`, where this
already broke on 1.24. This PR doesn't depend on it.

`load_balance_hosts` also needed a fix. PgBouncer reports it as text:
`disable` or `round-robin`, and null for a single-host pool. The scan
expected an integer instead. CNPG's generated `[databases]` section
never configures more than one host, so this never showed up in
practice. It's now encoded the same way `pool_mode` already is. The help
text no longer describes a host count, and the docs sample drops the
scan error that #6810 fixed.

A column PgBouncer renames instead of adds still disappears silently
under the by-name bind. Log it as a warning, here and in the
already-merged `SHOW STATS` collector from #11366, so a rename shows up
in the logs instead of just vanishing. A new test also checks that one
pool's row failing to scan doesn't blank out the pools that scan fine
right after it.

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
(cherry picked from commit d2c5ccd)
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 ok to merge 👌 This PR can be merged

Development

Successfully merging this pull request may close these issues.

6 participants