Skip to content

fix: allow batching of more than one query inside interactive transactions#25571

Merged
jacek-prisma merged 7 commits intoprisma:mainfrom
LucianBuzzo:lucianbuzzo/25570-auto-batch-itx
Feb 18, 2026
Merged

fix: allow batching of more than one query inside interactive transactions#25571
jacek-prisma merged 7 commits intoprisma:mainfrom
LucianBuzzo:lucianbuzzo/25570-auto-batch-itx

Conversation

@LucianBuzzo
Copy link
Copy Markdown
Contributor

@LucianBuzzo LucianBuzzo commented Nov 4, 2024

Fixes: #25570

This change fixes an issue where running different queries inside an interactive transaction in the same tick would not be batched together automatically by the engine.
By accounting for the protocolQuery when batching interactive transactions, the engine can optimize the queries correctly, as the queries will be batched by both the transaction ID and the query.

Summary by CodeRabbit

  • Improvements

    • Batching for interactive transactions now groups requests by transaction and protocol batch identifier when available for more precise execution.
  • Database

    • Added a Comment model and a comments relation on User for test scenarios.
  • Tests

    • Expanded interactive-transaction batching tests with detailed engine/batch metrics and PostgreSQL-only scenarios.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Nov 4, 2024
@jkomyno
Copy link
Copy Markdown
Contributor

jkomyno commented Nov 4, 2024

Fixes: #25570

This change fixes an issue where running different queries inside an interactive transaction in the same tick would not be batched together automatically by the engine. By accounting for the protocolQuery when batching interactive transactions, the engine can optimize the queries correctly, as the queries will be batched by both the transaction ID and the query.

Hi @LucianBuzzo, thanks for this PR! Do you think you can maybe add a few tests for this improvement in https://github.com/prisma/prisma/tree/main/packages/client/tests/functional/batching? You can run tests locally by cd'ing to ./packages/client and running:

pnpm run test:functional --provider=${DB_PROVIDER} --client-runtime node --engine-type library --runInBand extensions

You can set DB_PROVIDER to e.g. sqlite, to make things easier.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Nov 4, 2024
@LucianBuzzo
Copy link
Copy Markdown
Contributor Author

@jkomyno No problem, I've added tests to check the base case of batching, and batching with multiple models queried.

@LucianBuzzo LucianBuzzo force-pushed the lucianbuzzo/25570-auto-batch-itx branch from 4322bb9 to d963e26 Compare November 4, 2024 16:33
@LucianBuzzo LucianBuzzo force-pushed the lucianbuzzo/25570-auto-batch-itx branch from 587d609 to 87c83a1 Compare November 26, 2024 16:11
@SAGV
Copy link
Copy Markdown

SAGV commented Jun 16, 2025

@LucianBuzzo could you suggest the easiest way to monkeypatch prisma while this fix is on the way? We're facing a very similar issue with our own graphql+rls server unfortunately…

@LucianBuzzo
Copy link
Copy Markdown
Contributor Author

@SAGV You can see the patching operation here https://github.com/cerebruminc/yates/blob/v3.6.3/src/index.ts#L245
Getting this to function "natively" also required copying in some utility functions from the prisma client source: getBatchId() and buildKeysString().

@SAGV
Copy link
Copy Markdown

SAGV commented Jun 17, 2025

@LucianBuzzo wow, huge thanks! 👍🏻 👍🏻 👍🏻

I've tried so many things yesterday trying to figure out why the batching is broken inside transactions… The patch has just made our graphql requests 10x faster!

CleanShot 2025-06-17 at 17 15 38@2x

@aqrln even if there are 2 queries, it's still insanely faster…

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Sep 24, 2025

CLA assistant check
All committers have signed the CLA.

…tions

Fixes: prisma#25570

This change fixes an issue where running different queries inside an
interactive transaction in the same tick would not be batched together
automatically by the engine.
By accounting for the protocolQuery when batching interactive
transactions, the engine can optimize the queries correctly, as the
queries will be batched by both the transaction ID and the query.
@LucianBuzzo LucianBuzzo force-pushed the lucianbuzzo/25570-auto-batch-itx branch from c2fd912 to 4e9c491 Compare February 9, 2026 16:48
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 9, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

RequestHandler.batchBy now groups interactive-transaction (itx) requests using a batch identifier derived from protocolQuery; test schema adds a Comment model and tests are extended to instrument and assert engine batching behavior for interactive transactions (PostgreSQL-only).

Changes

Cohort / File(s) Summary
RequestHandler batching
packages/client/src/runtime/RequestHandler.ts
Internal change to batchBy: when request.transaction.kind === 'itx', compute batchId from protocolQuery and return itx-${transaction.id}-${batchId} if present, otherwise itx-${transaction.id}. Non-itx behavior unchanged. No public signature changes.
Test schema
packages/client/tests/functional/batching/prisma/_schema.ts
Added new Comment model and a comments Comment[] relation field on User for test coverage.
Functional batching tests
packages/client/tests/functional/batching/tests.ts
Changed setupTestSuite callback to ({ provider }) => { ... }. Added engine- and query-level counters, reset helpers, instrumentation of engine.request/engine.requestBatch, refined query-event classification, and PostgreSQL-gated interactive-transaction tests asserting batching and batch sizes.
🚥 Pre-merge checks | ✅ 4 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Merge Conflict Detection ⚠️ Warning ❌ Merge conflicts detected (41 files):

⚔️ AGENTS.md (content)
⚔️ packages/adapter-mssql/src/connection-string.test.ts (content)
⚔️ packages/adapter-mssql/src/connection-string.ts (content)
⚔️ packages/adapter-mssql/src/mssql.ts (content)
⚔️ packages/cli/src/Init.ts (content)
⚔️ packages/cli/src/bin.ts (content)
⚔️ packages/cli/src/mcp/MCP.ts (content)
⚔️ packages/client-common/package.json (content)
⚔️ packages/client-common/src/client-config.ts (content)
⚔️ packages/client-engine-runtime/src/batch.ts (content)
⚔️ packages/client-engine-runtime/src/interpreter/query-interpreter.ts (content)
⚔️ packages/client-engine-runtime/src/query-plan.ts (content)
⚔️ packages/client-generator-js/package.json (content)
⚔️ packages/client-generator-js/src/TSClient/TSClient.ts (content)
⚔️ packages/client-generator-ts/package.json (content)
⚔️ packages/client-generator-ts/src/TSClient/file-generators/ClassFile.ts (content)
⚔️ packages/client/package.json (content)
⚔️ packages/client/src/__tests__/benchmarks/query-performance/compilation.bench.ts (content)
⚔️ packages/client/src/runtime/RequestHandler.ts (content)
⚔️ packages/client/src/runtime/core/engines/client/ClientEngine.ts (content)
⚔️ packages/client/src/runtime/core/engines/common/Engine.ts (content)
⚔️ packages/client/src/runtime/core/jsonProtocol/serializeJsonQuery.test.ts (content)
⚔️ packages/client/src/runtime/core/jsonProtocol/serializeJsonQuery.ts (content)
⚔️ packages/client/src/runtime/getPrismaClient.ts (content)
⚔️ packages/client/src/utils/getTestClient.ts (content)
⚔️ packages/client/tests/functional/_utils/types.ts (content)
⚔️ packages/client/tests/functional/batching/prisma/_schema.ts (content)
⚔️ packages/client/tests/functional/batching/tests.ts (content)
⚔️ packages/client/tests/functional/chunking-query/tests.ts (content)
⚔️ packages/client/tests/functional/tracing/tests.ts (content)
⚔️ packages/engines/package.json (content)
⚔️ packages/fetch-engine/package.json (content)
⚔️ packages/internals/package.json (content)
⚔️ packages/json-protocol/src/index.ts (content)
⚔️ packages/migrate/package.json (content)
⚔️ packages/schema-files-loader/package.json (content)
⚔️ packages/sqlcommenter-query-insights/src/parameterize/parameterize.ts (content)
⚔️ packages/sqlcommenter-query-insights/src/parameterize/tests/tagged-values.test.ts (content)
⚔️ packages/type-benchmark-tests/test.ts (content)
⚔️ pnpm-lock.yaml (content)
⚔️ tsconfig.build.bundle.json (content)

These conflicts must be resolved before merging into main.
Resolve conflicts locally and push changes to this branch.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: enabling batching of multiple queries inside interactive transactions, which is the core fix in the PR.
Linked Issues check ✅ Passed The PR implementation addresses issue #25570 by modifying RequestHandler.batchBy to account for protocolQuery when batching interactive transactions, enabling proper batching of multiple distinct queries within the same transaction tick.
Out of Scope Changes check ✅ Passed All changes are scoped to the interactive transaction batching fix: RequestHandler.ts for batching logic, schema.ts for test setup with User-Comment relation, and tests.ts for comprehensive batching verification tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

@LucianBuzzo LucianBuzzo requested a review from aqrln February 9, 2026 18:27
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Feb 12, 2026

Merging this PR will not alter performance

✅ 47 untouched benchmarks


Comparing LucianBuzzo:lucianbuzzo/25570-auto-batch-itx (6644c78) with main (9865dcb)

Open in CodSpeed

@LucianBuzzo LucianBuzzo force-pushed the lucianbuzzo/25570-auto-batch-itx branch from 4f9b6d9 to f2d25c3 Compare February 12, 2026 17:35
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Co-authored-by: jacek-prisma <malec@prisma.io>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

@jacek-prisma jacek-prisma merged commit 6587f54 into prisma:main Feb 18, 2026
244 of 245 checks passed
jacek-prisma added a commit that referenced this pull request Feb 19, 2026
…tions (#25571)

Fixes: #25570

This change fixes an issue where running different queries inside an
interactive transaction in the same tick would not be batched together
automatically by the engine.
By accounting for the protocolQuery when batching interactive
transactions, the engine can optimize the queries correctly, as the
queries will be batched by both the transaction ID and the query.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Improvements**
* Batching for interactive transactions now groups requests by
transaction and protocol batch identifier when available for more
precise execution.

* **Database**
* Added a Comment model and a comments relation on User for test
scenarios.

* **Tests**
* Expanded interactive-transaction batching tests with detailed
engine/batch metrics and PostgreSQL-only scenarios.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: jacek-prisma <malec@prisma.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatic batching doesn't work inside interactive transaction

6 participants