Repository navigation
Description
Problem
SqlClient.withTransaction starts every top-level transaction with the client's fixed beginTransaction (BEGIN by default). A transaction that needs its own isolation level or access mode has to send a second statement after it, and each statement is a round trip. Behind a remote pooler that adds up.
beginTransaction is client-wide, and PgClient doesn't expose it. Neither would help anyway: one app mixes serializable and read-committed transactions. A session default (default_transaction_isolation) isn't safe behind a transaction-mode pooler such as Cloudflare Hyperdrive, which shares server connections between clients.
#7482 was the same limit from the SQLite side (client-wide BEGIN IMMEDIATE). This asks for it per call.
Repro
sql.withTransaction(
Effect.andThen(sql`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE`, sql`SELECT 1`)
)On the wire:
BEGINSET TRANSACTION ISOLATION LEVEL SERIALIZABLESELECT 1COMMIT
Postgres takes the mode in BEGIN itself (BEGIN ISOLATION LEVEL SERIALIZABLE), which makes it 3. Drizzle's Effect driver (drizzle-orm/effect-postgres) hits this on every db.transaction(…, { isolationLevel }).
Proposal
sql.withTransaction(body, { begin: "BEGIN ISOLATION LEVEL SERIALIZABLE" })- Replaces the statement that starts a top-level transaction, for that call only.
- A nested transaction ignores it and keeps its savepoint.
- Runs unprepared, since it isn't a fixed statement like the client's own.
- Drivers whose
BEGINisn't a SQL statement (libsql, mssql, sqlite-do) ignore it. makeWithTransactionpasses it to the driver'sbegin(conn, statement).
One catch: Effect.fn passes the function's arguments to each pipeline step, so Effect.fn(function*(id) { … }, sql.withTransaction) would hand id to the new parameter, and that usually stops compiling. Three steps inside effect do this, and they'd become (effect) => sql.withTransaction(effect). An overloaded data-last form, withTransaction({ begin })(effect), avoids that but breaks inference for point-free pipe(sql.withTransaction). A Context.Reference read at BEGIN would avoid touching the signature, if you'd rather go that way.
Cost
Measured on a Cloudflare Worker (placement aws:us-east-2) → Hyperdrive (caching off) → Neon Postgres, with @effect/sql-pg 4.0.5 under Drizzle's Effect driver and prepare: false.
Statements per transaction, read from the frontend messages on the wire:
| Transaction | BEGIN + SET TRANSACTION |
Level in BEGIN |
|---|---|---|
| serializable, one query | 4 | 3 |
read committed, select … for update + one query |
5 | 4 |
Each request ran two transactions (one serializable: select, update, select; one read committed led by select … for update, plus an update) and one count(*). That's 200 sequential requests per path, warm, in four alternating blocks of 50, with no failures. The ranges are across the blocks:
| Path | server p50 | server p95 | client p50 | client p95 |
|---|---|---|---|---|
BEGIN + SET TRANSACTION |
204–214 ms | 250–262 ms | 269–292 ms | 363–421 ms |
level in BEGIN (patched withTransaction) |
169–177 ms | 195–209 ms | 243–258 ms | 294–334 ms |
node-postgres, which already sends one BEGIN … |
177–182 ms | 200–264 ms | 249–269 ms | 319–554 ms |
That's 32–37 ms per request at server p50 across two transactions, so about 16–18 ms per transaction: one round trip each.
We carry this as a three-line patch against effect 4.0.5 for now. A PR with this shape follows; happy to switch it to the Context.Reference form if you'd rather.
Activity
feat(sql): add SqlClient.BeginTransaction for per-transaction BEGIN s…