Skip to content

feat: Implement sqlx::Acquire for &Pool and expose the wrapped pool - #47

Open
AzHicham wants to merge 2 commits into
chmodas:trunkfrom
AzHicham:feat/raw-pool-access
Open

AzHicham wants to merge 2 commits into
chmodas:trunkfrom
AzHicham:feat/raw-pool-access

Conversation

@AzHicham

Copy link
Copy Markdown

Some SQLx APIs cannot take an instrumented Pool today:

  • sqlx::migrate::Migrator::run is generic over sqlx::Acquire, which &Pool does not implement;
  • sqlx::postgres::PgListener::connect_with accepts only a sqlx::Pool, and the wrapped pool is private.

An application that wraps its pool therefore has to keep a second handle to the raw pool next to it, just for migrations and LISTEN/NOTIFY.

Changes

  • impl sqlx::Acquire for &Pool<DB>. Both methods acquire through Pool::acquire, so wait_time, pending_requests and timeouts are recorded as on every other path.
    • acquire returns a new AcquiredConnection<DB>, which derefs to the backend connection (what Acquire::Connection requires) and records use_time when dropped.
    • begin returns a plain sqlx::Transaction. It cannot report when it releases its connection, so that lease records no use_time rather than a wrong one.
    • Queries run on that connection or transaction are not instrumented, since they no longer go through the wrapper.
  • Pool::inner() -> &sqlx::Pool<DB>, for APIs that accept nothing else. Nothing done through it is instrumented.

A separate AcquiredConnection type, rather than Deref on PoolConnection, keeps &mut *conn from silently bypassing instrumentation on the connection type users already have.

Docs, README and CHANGELOG cover both, including what is not instrumented.

Tests

  • pool_metrics.rs: Acquire::acquire (wait and use time, no spans), Acquire::begin (wait time only), Migrator::run(&pool), and inner() (no spans, no wait time).
  • postgres.rs: PgListener::connect_with(pool.inner()) receives a NOTIFY.
  • cargo test --all-features, the async-std pool_metrics run, clippy -D warnings and fmt pass locally.

sqlx/migrate is added to the dev-dependencies for the Migrator test.

🤖 Generated with Claude Code

AzHicham and others added 2 commits November 13, 2026 12:19
Some SQLx APIs take nothing an instrumented `Pool` can provide today:
`Migrator::run` is generic over `sqlx::Acquire`, and
`PgListener::connect_with` accepts only a `sqlx::Pool`.

- `&Pool<DB>` now implements `sqlx::Acquire`. Both methods acquire through
  `Pool::acquire`, so wait time, pending requests and timeouts are
  recorded. `acquire` returns the new `AcquiredConnection`, which
  dereferences to the backend connection and records `use_time` when
  dropped. `begin` returns a plain `sqlx::Transaction`, which cannot report
  its release, so that lease records no `use_time`.
- `Pool::inner()` returns the wrapped `sqlx::Pool`. Work done through it is
  not instrumented.

Queries run on the raw connection or pool are not instrumented; the docs,
README and CHANGELOG say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant