8000 fix(tool/postgres-list-database-stats): compute rollback_ratio_percent without the +1 bias by beskvladimir-create · Pull Request #4577 · googleapis/mcp-toolbox · GitHub
[go: up one dir, main page]

Skip to content

fix(tool/postgres-list-database-stats): compute rollback_ratio_percent without the +1 bias - #4577

Open
beskvladimir-create wants to merge 2 commits into
googleapis:mainfrom
beskvladimir-create:fix/postgres-list-database-stats-rollback-ratio
Open

beskvladimir-create wants to merge 2 commits into
googleapis:mainfrom
beskvladimir-create:fix/postgres-list-database-stats-rollback-ratio

Conversation

@beskvladimir-create
Copy link
Copy Markdown
Contributor

Description

postgres-list-database-stats computed rollback_ratio_percent as xact_rollback * 100.0 / (xact_commit + xact_rollback + 1). The + 1 guards against division by zero but is also applied to every non-zero denominator, so the reported share is always too low, and badly so for small counters: 1 commit and 1 rollback reports 33.33 instead of 50.00, 0 commits and 5 rollbacks 83.33 instead of 100.00 (details in #4576).

This change uses the same pattern as cache_hit_ratio_percent in the same statement: 0 when there are no completed transactions, otherwise round((xact_rollback * 100.0) / (xact_commit + xact_rollback), 2). No other column changes.

Testing: I ran the full statement with this change on PostgreSQL 16.15 (with and without include_templates, including template0 with 0/0) and it returns the exact percentage. I also added a subtest to RunPostgresListDatabaseStatsTest (used by the postgres, alloydbpg, alloydbomni and cloudsqlpg integration tests): it connects to the fresh test database, rolls back one transaction, waits until pg_stat_database shows it, invokes list_database_stats for that database and checks rollback_ratio_percent against the xact_commit / xact_rollback values returned in the same row. With the small counters of a fresh database the old formula would not pass this check (in a psql run of the same steps the database had 1 commit and 1 rollback: 33.33 vs 50.00). I could not run the Go tests locally (my environment cannot download the Go 1.27 toolchain), so the Go test relies on CI.

Security: no new input path; all parameters are bound exactly as before and the change only affects how two catalog counters are combined.

PR Checklist

  • Make sure you reviewed CONTRIBUTING.md
  • Make sure to open an issue as a bug/issue before writing your code
  • Ensure you have manually reviewed the entire diff before requesting a review
  • Ensure the tests and linter pass (left to CI, see Testing above)
  • Code coverage does not decrease (integration test added)
  • Appropriate docs were updated (no docs change needed)
  • Make sure to add ! if this involve a breaking change (not breaking)

🛠️ Fixes #4576

@gemini-code-assist gemini-code-assist Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the calculation of rollback_ratio_percent in the Postgres database statistics tool to avoid division-by-zero errors and remove the biased denominator (+ 1). It also adds a comprehensive integration test to verify that the rollback ratio is computed correctly. There are no review comments, and I have no feedback to provide.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

postgres-list-database-stats: rollback_ratio_percent is understated because of a +1 in the denominator

2 participants

0