Skip to content

Add SqlClient.batch and send PostgreSQL batches in one Sync - #10147

Merged
tim-smart merged 22 commits into
v4/next-minorfrom
agent/nelson-effect-bot/634621d8d78f
Oct 30, 2026
Merged

tim-smart merged 22 commits into
v4/next-minorfrom
agent/nelson-effect-bot/634621d8d78f

Conversation

@effect-bot

@effect-bot effect-bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Adds SqlClient.batch, which runs statements in order on one connection and returns each statement's rows as a typed tuple. PostgreSQL and libSQL send it in one round trip. On PostgreSQL, setting transaction-local context such as row-level security via set_config(..., true) and running a query then takes one round trip instead of four (BEGIN, context, query, COMMIT). Because the context ends with the batch's implicit transaction, this is safe behind PgBouncer in transaction mode.

const [, items] = yield* sql.batch([
  sql`SELECT set_config('app.organization_id', ${org}, true)`,
  sql`SELECT * FROM item`
])

API

  • SqlClient.batch(statements) (effect): typed like the existing D1Client.batch. Each statement is compiled and transformed by the client that built it, and Statement.CurrentTransformer applies to each one. The batch runs under one client span (OpenTelemetry BATCH naming for more than one statement), on the current transaction's connection when there is one. An empty batch returns [] without acquiring a connection.
  • Connection.executeBatch (effect, optional): the driver hook for native batching. Without it, core runs the statements one at a time with execute on the same connection, which is not atomic outside withTransaction.
  • PgConnection.batch(statements) and BatchStatement (@effect/sql-pg): the protocol primitive, also on pinned connections.

PostgreSQL

  • One write: (Parse "" / Bind / Describe / Execute)+ Sync. Statements are unnamed, so batches skip the prepared statement cache and are never retried.
  • Each statement gets its own RowDescription and decoder. An interrupted batch drains without a decoder, so later rows aren't decoded with an earlier statement's types.
  • A failing statement fails the batch, the cycle drains to ReadyForQuery, and the session stays usable. Outside an explicit transaction nothing is kept; inside one, the transaction is aborted.
  • Multiplexed connections pipeline a batch without pinning. Each batch has its own Sync, so context cannot leak into neighbouring entries.

libSQL

  • executeBatch sends one SDK batch call in "write" mode, matching beginTransaction.
  • Outside withTransaction it runs as one transaction, so a failing statement keeps nothing. Inside, it runs in the open SDK Transaction, and a failure rolls back with the enclosing transaction.

Streams don't take batches, and batching BEGIN together with context in withTransaction is out of scope.

Tests

  • Core: SqlClient.test.ts covers the sequential fallback, executeBatch with per-statement transforms, and the empty batch. typetest/sql/SqlClient.tst.ts covers tuple typing.
  • pg:
    • PgConnection.in-process.test.ts: wire shape, no retry, the empty batch, and draining an interrupted heterogeneous batch.
    • PgConnection.integration.test.ts: context lifetime, error recovery, unnamed statements, explicit transactions and multiplexed isolation.
    • Client.integration.test.ts: transaction routing, per-client compile and transforms, the statement transformer and span propagation.
  • libSQL: Client.integration.test.ts checks for exactly one SDK batch call with per-statement rows, and that a failing later statement rolls back the earlier one.

Closes #10113
Closes EFF-3121

@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ba72349

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

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

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

@effect-bot
effect-bot changed the base branch from main to v4/next-minor October 29, 2026 20:45
@effect-bot
effect-bot force-pushed the agent/nelson-effect-bot/634621d8d78f branch from 1a80117 to db7ad38 Compare October 29, 2026 20:53
@github-actions

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.35 KB 39.35 KB 0.00 KB (0.00%)
basic.ts 7.18 KB 7.18 KB 0.00 KB (0.00%)
batching.ts 11.06 KB 11.06 KB 0.00 KB (0.00%)
brand.ts 6.84 KB 6.84 KB 0.00 KB (0.00%)
cache.ts 11.49 KB 11.49 KB 0.00 KB (0.00%)
config.ts 22.77 KB 22.77 KB 0.00 KB (0.00%)
differ.ts 21.92 KB 21.92 KB 0.00 KB (0.00%)
http-client.ts 26.86 KB 26.86 KB 0.00 KB (0.00%)
http-router.ts 36.85 KB 36.85 KB 0.00 KB (0.00%)
logger.ts 11.96 KB 11.96 KB 0.00 KB (0.00%)
metric.ts 9.42 KB 9.42 KB 0.00 KB (0.00%)
optic.ts 7.05 KB 7.05 KB 0.00 KB (0.00%)
pubsub.ts 15.97 KB 15.97 KB 0.00 KB (0.00%)
queue.ts 12.77 KB 12.77 KB 0.00 KB (0.00%)
schedule.ts 11.74 KB 11.74 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 14.02 KB 14.02 KB 0.00 KB (0.00%)
schema-binary.ts 41.06 KB 41.06 KB 0.00 KB (0.00%)
schema-class.ts 21.65 KB 21.65 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 29.51 KB 29.51 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.91 KB 27.91 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.27 KB 15.27 KB 0.00 KB (0.00%)
schema-string.ts 12.26 KB 12.26 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.19 KB 16.19 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.87 KB 38.87 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 26.10 KB 26.10 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.83 KB 20.83 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.97 KB 20.97 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.07 KB 21.07 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.11 KB 26.11 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.16 KB 21.16 KB 0.00 KB (0.00%)
schema.ts 20.75 KB 20.75 KB 0.00 KB (0.00%)
stm.ts 14.15 KB 14.15 KB 0.00 KB (0.00%)
stream.ts 10.33 KB 10.33 KB 0.00 KB (0.00%)

@effect-bot effect-bot changed the title Batch PostgreSQL prelude statements and a query in one Sync Add SqlClient.batch and send PostgreSQL batches in one Sync Oct 29, 2026
@effect-bot effect-bot changed the title Add SqlClient.batch and send PostgreSQL batches in one Sync Add PgClient.batch and send PostgreSQL batches in one Sync Oct 30, 2026
@effect-bot effect-bot changed the title Add PgClient.batch and send PostgreSQL batches in one Sync Add SqlClient.batch and send PostgreSQL batches in one Sync Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql-pg: send several statements in one extended-query Sync (transaction-local context without extra round trips)

2 participants