Skip to content

Support passive monitoring of Postgres locks - #285

Open
Hawxy wants to merge 3 commits into
madelson:masterfrom
Hawxy:passivemonitoring
Open

Support passive monitoring of Postgres locks#285
Hawxy wants to merge 3 commits into
madelson:masterfrom
Hawxy:passivemonitoring

Conversation

@Hawxy

@Hawxy Hawxy commented Aug 13, 2026

Copy link
Copy Markdown

Hiya, I've used this package quite a lot over the last few years, both in my work as well as open source (I moved MartenDB over to it a while ago, and it'll be used within the v1 of Wallaby). I find the lock monitoring especially useful and it eliminated an entire class of problems for us, but the pg_sleep query causes a few issues:

  • Users tend to freak out when they see a long-running query show up in monitoring tools. We've had a number of Marten users complain about the "load" that monitoring tools are reporting despite pg_sleep not doing anything, to the point that we provided an opt-out on monitoring just to eliminate the noise. This gets flagged by automated problem finders too, like Datadog's DBM and RDS Performance Insights
  • The sleeping query skews query performance stats as your average query time goes through the roof and you have to filter it out whenever you're looking at metrics or trying to setup alarms.

This PR adds a passive approach that waits for the socket to break instead. In monitoring tools this will simply show up as an idle connection. This is codepath is enabled by default, but is unsupported when:

  • Npgsql Multiplexing is enabled (not very important as multiplexing is getting removed from npgsql in v11)
  • KeepAlive is enabled on the connection string

If either case is true, it'll fall back to using the pg_sleep path.

@Hawxy
Hawxy force-pushed the passivemonitoring branch from 30bc1b1 to d689cf1 Compare August 15, 2026 10:44
@Hawxy
Hawxy force-pushed the passivemonitoring branch from d689cf1 to 295523c Compare August 16, 2026 11:10
Comment thread docs/DistributedLock.Postgres.md Outdated
When `HandleLostToken` is used on a lock backed by a library-owned connection, the library monitors the connection passively using `NpgsqlConnection.WaitAsync`, which uses a blocking socket read that detects connection loss (e.g. a database restart or `pg_terminate_backend`) as soon as the socket breaks, without executing any query. The monitored session therefore shows as `idle` in `pg_stat_activity`.

Two things to be aware of:
- Because the monitored session is idle, server-side idle-session reapers (`idle_session_timeout`, `idle_in_transaction_session_timeout`, or aggressive gateways) can kill it. If any of these are in play, set `KeepaliveCadence`, as when monitoring is active, the keepalive query will be interleaved with the passive wait.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Does this issue exist if pg_sleep is used? Trying to understand if this is something that could become an issue for folks using the library today if they upgrade and get this behavior.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No it doesn't impact pg_sleep as that keeps the connection active. With the updated logic where we always do a keepalive per minute, it would only impact consumers that have an idle_session_timeout of <60 which is very unlikely.

Comment thread src/DistributedLock.Core/Internal/Data/ConnectionMonitor.cs Outdated
Comment thread src/DistributedLock.Core/Internal/Data/ConnectionMonitor.cs Outdated
Comment thread src/DistributedLock.Core/Internal/Data/DatabaseConnection.cs Outdated
Comment thread src/DistributedLock.Postgres/PostgresDatabaseConnection.cs Outdated
Comment thread src/DistributedLock.Postgres/PostgresDatabaseConnection.cs Outdated
Comment thread src/DistributedLock.Postgres/PostgresDatabaseConnection.cs Outdated
@madelson

Copy link
Copy Markdown
Owner

Thanks for filing @Hawxy ! I believe this change makes sense overall but I'd like to change the approach a bit to be more thread-safe and leverage the existing abstractions a bit more. Please take a look at https://github.com/madelson/DistributedLock/pull/285/changes#r3839331256 and let me know if you have questions or thoughts.

@Hawxy

Hawxy commented Aug 24, 2026

Copy link
Copy Markdown
Author

Changes have been made per comments.

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.

2 participants