Skip to content

refactor: implement connection tracking in metrics#4672

Open
mkleczek wants to merge 1 commit intoPostgREST:mainfrom
mkleczek:refactor/connection-tracking
Open

refactor: implement connection tracking in metrics#4672
mkleczek wants to merge 1 commit intoPostgREST:mainfrom
mkleczek:refactor/connection-tracking

Conversation

@mkleczek
Copy link
Copy Markdown
Collaborator

@mkleczek mkleczek commented Feb 25, 2026

DISCLAIMER:
This commit was authored entirely by a human without the assistance of LLMs.

Right now metrics observation handler does not track database connections but updates a single Gauge based on HasqlPoolObs events.

This is problematic because Hasql pool reports various connection events in various states that make it impossible to predict the state change from the received event. The connection state machine is not simple and to precisely report the number of connections in various states, it is necessary to track their lifecycles.

Fixes #4622

@steve-chavez
Copy link
Copy Markdown
Member

Fixes #4622

Fix should have an entry on the CHANGELOG

Comment on lines +101 to +109
SQL.ReadyForUseConnectionStatus -> atomically $
SH.insert identity uuid connTrackConnected *>
SH.focus Focus.delete identity uuid connTrackInUse
SQL.TerminatedConnectionStatus _ -> atomically $
SH.focus Focus.delete identity uuid connTrackConnected *>
SH.focus Focus.delete identity uuid connTrackInUse
SQL.InUseConnectionStatus -> atomically $
SH.insert identity uuid connTrackInUse
_ -> mempty
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could there be noticeable overhead now that we have the insert/focus logic?

Doesn't look like it from the mixed loadtest but want to make sure.

Comment on lines +111 to +112
connectionCounts :: ConnTrack -> IO ConnStats
connectionCounts = atomically . fmap (uncurry ConnStats) . bisequenceA . both SH.size . (connTrackConnected &&& connTrackInUse)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is purely used for testing right? If so, a comment here would be good for clarity.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is purely used for testing right? If so, a comment here would be good for clarity.

@steve-chavez It is not. The actual Metric is implementation is based on ConnTrack:
https://github.com/mkleczek/postgrest/blob/7e9df6416195b3d9c534c1f1c4aeb39865869f45/src/PostgREST/Metrics.hs#L59

Also - this function is crucial for #4643 - see: https://github.com/mkleczek/postgrest/blob/f3e06920544b88d26809e75d2652d2ed6a4ef8f3/src/PostgREST/AppState.hs#L421

@mkleczek mkleczek force-pushed the refactor/connection-tracking branch 5 times, most recently from 0f475e3 to 592e5a3 Compare March 10, 2026 14:53
@mkleczek mkleczek force-pushed the refactor/connection-tracking branch 2 times, most recently from 1f3b4bc to bce1f6e Compare March 13, 2026 06:49
@mkleczek mkleczek force-pushed the refactor/connection-tracking branch 7 times, most recently from a7f96ef to 7383b59 Compare April 2, 2026 08:59
DISCLAIMER:
This commit was authored entirely by a human without the assistance of LLMs.

Right now metrics observation handler does not track database connections but updates a single Gauge based on HasqlPoolObs events. This is problematic because Hasql pool reports various connection events in multiple phases. The connection state machine is not simple and to precisely report the number of connections in various states, it is necessary to track their lifecycles.

This change adds a ConnTrack data structure and logic to track database connections lifecycles. At the moment it supports "connected" and "inUse" connection counts precisely. The "pgrst_db_pool_available" metric is implemented on top of ConnTrack instead of a simple Gauge.
@mkleczek mkleczek force-pushed the refactor/connection-tracking branch from 7383b59 to 9b3c004 Compare April 2, 2026 14:45
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.

Race condition in pool_available metric causes negative values during network instability

2 participants