Skip to content

Trace SQL connection wait and sql-pg connects - #8676

Merged
tim-smart merged 12 commits into
Effect-TS:mainfrom
rchasman:pg-pool-connect-spans
Oct 21, 2026
Merged

tim-smart merged 12 commits into
Effect-TS:mainfrom
rchasman:pg-pool-connect-spans

Conversation

@rchasman

@rchasman rchasman commented Oct 2, 2026 •

Copy link
Copy Markdown

A slow sql.execute span gives no reason for its duration when the statement spent it waiting for a connection, or waiting for a new PostgreSQL session to connect. On serverless Postgres a connect can take hundreds of milliseconds, or seconds when the compute wakes from scale to zero. This change makes both visible, so a trace shows whether the time went to the connection or to the query.

SpanPropagationEnabled already parents driver spans under sql.execute, and it gates everything below.

  • effect/sql Statement: with propagation on, sql.execute records db.client.connection.wait_time_ms, from the span's start to the connection hand-off. This covers every Statement-based driver, on the acquirer, borrower and stream paths.
  • @effect/sql-pg PgPool: the pool opens sessions on its own fiber, so a span around the connect has no statement parent. The pool records each session's connect start and end instead. On the session's first checkout, with propagation on, it reports a db.connect child span under that statement, covering the part of the connect the statement waited for. A session that was ready before the statement started reports nothing. Sessions with an unreported connect sit in a WeakMap that is checked at hand-off, so a session reports at most once and checkouts of reported sessions run the original code.
sql.execute  167ms  db.client.connection.wait_time_ms=145.2
  db.connect 144ms
sql.execute   16ms  db.client.connection.wait_time_ms=0.4

Validated:

  • Each new test fails without its change:
    • Statement.test.ts, on TestClock: acquired, borrowed and streamed statements record a 30 ms wait.
    • PgPool.in-process.test.ts, on TestClock with a held fake server: a first checkout through use, get and reserve reports one 40 ms db.connect, and none with propagation or timing off. A session ready before the statement reports none.
  • The effect sql suites and every non-integration sql-pg suite pass (288 tests), stable over 10 runs.
  • tsc -b for effect and sql-pg, and dprint check.
  • We run the same logic as a patch on five Neon-backed production deployments.

Follow-up idea: effect/Pool could expose acquire timing per item, which would move db.connect out of the driver and cover sql-mssql too.

Closes EFF-2699

@changeset-bot

changeset-bot Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e234abb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
effect Patch
@effect/sql-pg Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 39.13 KB 39.13 KB 0.00 KB (0.00%)
basic.ts 7.04 KB 7.04 KB 0.00 KB (0.00%)
batching.ts 10.91 KB 10.91 KB 0.00 KB (0.00%)
brand.ts 6.71 KB 6.71 KB 0.00 KB (0.00%)
cache.ts 11.35 KB 11.35 KB 0.00 KB (0.00%)
config.ts 22.51 KB 22.51 KB 0.00 KB (0.00%)
differ.ts 21.66 KB 21.66 KB 0.00 KB (0.00%)
http-client.ts 26.02 KB 26.02 KB 0.00 KB (0.00%)
http-router.ts 35.69 KB 35.69 KB 0.00 KB (0.00%)
logger.ts 11.70 KB 11.70 KB 0.00 KB (0.00%)
metric.ts 9.27 KB 9.27 KB 0.00 KB (0.00%)
optic.ts 6.93 KB 6.93 KB 0.00 KB (0.00%)
pubsub.ts 15.79 KB 15.79 KB 0.00 KB (0.00%)
queue.ts 12.61 KB 12.61 KB 0.00 KB (0.00%)
schedule.ts 11.60 KB 11.60 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 13.84 KB 13.84 KB 0.00 KB (0.00%)
schema-binary.ts 40.53 KB 40.53 KB 0.00 KB (0.00%)
schema-class.ts 21.36 KB 21.36 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 32.33 KB 32.33 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.52 KB 27.52 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.09 KB 15.09 KB 0.00 KB (0.00%)
schema-string.ts 12.10 KB 12.10 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.02 KB 16.02 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.66 KB 38.66 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 25.71 KB 25.71 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.59 KB 20.59 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.77 KB 20.77 KB 0.00 KB (0.00%)
schema-toFormatter.ts 20.88 KB 20.88 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 25.52 KB 25.52 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 20.88 KB 20.88 KB 0.00 KB (0.00%)
schema.ts 20.55 KB 20.55 KB 0.00 KB (0.00%)
stm.ts 13.89 KB 13.89 KB 0.00 KB (0.00%)
stream.ts 10.14 KB 10.14 KB 0.00 KB (0.00%)

@rchasman rchasman changed the title Trace sql-pg session checkout wait and connects Trace SQL connection wait and sql-pg connects Oct 2, 2026
rchasman and others added 2 commits October 7, 2026 15:04
# Conflicts:
#	packages/effect/src/sql/Statement.ts
Main now names statement spans after the database target, so `sql.execute` is only the fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GjpNuHbLUDNrwZAtpmvbMS
# Conflicts:
#	packages/effect/test/sql/Statement.test.ts
#	packages/sql/pg/src/PgPool.ts
#	packages/sql/pg/test/PgPool.in-process.test.ts
Track pending connects in a WeakMap keyed by session instead of a
counter, so sessions closed before their first checkout no longer keep
every later checkout on the reporting path. Apply hand-off reporting
once around main's checkout, drop the pool.name attribute, and skip the
wait-time wrapper when span timing is disabled.
Without timing the statement span starts at 0, so a connect span would
mix real timestamps with an untimed parent and report sessions that were
ready before the statement.
@tim-smart
tim-smart merged commit 3da7c0a into Effect-TS:main Oct 21, 2026
9 of 10 checks passed

This branch had an error being deployed

1 failed deployment
fork — 58ce3471 Deployed Oct 21, 2026 by effect-bot via approval-gate #22138
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.0 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants