Skip to content

fix(metrics): prevent pgrst_db_pool_available drifting below zero#4630

Open
nothankyouzzz wants to merge 2 commits intoPostgREST:mainfrom
nothankyouzzz:fix/pgrst-db-pool-available
Open

fix(metrics): prevent pgrst_db_pool_available drifting below zero#4630
nothankyouzzz wants to merge 2 commits intoPostgREST:mainfrom
nothankyouzzz:fix/pgrst-db-pool-available

Conversation

@nothankyouzzz
Copy link

Summary

pgrst_db_pool_available could drift below zero under connection churn or unstable networks. This happens because the metric was updated using pure inc/dec deltas without tracking per-connection state, so events like Connecting→Terminated or Ready→InUse→Terminated can double-decrement or decrement without a prior increment.

Fix

Track availability per connection id and set the gauge from the absolute state:

  • Maintain a small state map (conn id -> available flag) and a ready counter.
  • Update the counter on state transitions and set the gauge to the computed value.
  • Clamp the gauge to 0 and pool max to avoid out-of-bounds values.
  • Add unit tests for lifecycle transitions and bounds.

Issue

Testing

  • postgrest-lint
  • postgrest-style
  • postgrest-check

@develop7
Copy link
Collaborator

develop7 commented Feb 4, 2026

@nothankyouzzz no merge commits please, rebase your branch against main instead

Track pool availability by connection id and set gauge from absolute state.

Add unit coverage for lifecycle transitions and bounds.
@nothankyouzzz nothankyouzzz force-pushed the fix/pgrst-db-pool-available branch from 2a4b017 to 71525aa Compare February 4, 2026 14:48
@nothankyouzzz
Copy link
Author

@develop7 done.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants