Skip to content

fix(client): do not cache batch query plans that contain createMany - #30581

Open
lpbonomi wants to merge 1 commit into
prisma:v7from
lpbonomi:fix/batch-plan-cache-create-many
Open

lpbonomi wants to merge 1 commit into
prisma:v7from
lpbonomi:fix/batch-plan-cache-create-many

Conversation

@lpbonomi

@lpbonomi lpbonomi commented Oct 3, 2026 •

Copy link
Copy Markdown

The query plan cache does not store createMany and createManyAndReturn plans in the single query path. Their shape changes with the row count, so they rarely get a cache hit and they bloat the cache.

The batch path does not have this check. A batch is cached as one entry, keyed by the whole parameterized batch. So a $transaction array that contains a createMany gets a new batch entry for each row count:

await prisma.$transaction([
  prisma.email.deleteMany({ where: { contactId: { in: contactIds } } }),
  prisma.email.createMany({ data: rows }), // rows.length changes on each call
])

The plan grows with the number of rows. In our production workers, each entry was about 13 MB, and the batch cache held about 2.4–3.6 GB of heap per process, up to the 1,000-entry limit. The large heap made GC pauses long enough to block the event loop for several seconds.

This change moves the existing check into one function, isCacheableQuery, and uses it in both paths. A batch that contains a createMany or createManyAndReturn query is compiled but not cached.

Measured on 7.10.0 with the same change applied, $transaction([deleteMany, createMany]) for 1 to 200 rows:

before after
batch cache entries 208 3 (none with createMany)
heap growth 32.0 MB 1.4 MB

The results were the same. A repeated batch with the same row count now compiles each time (46 ms instead of 17 ms at 2,000 rows). That is the same cost that the two queries have today outside a batch.

Related: #29774, #29784, #29785. Those PRs limit the batch cache by plan count, which does not help when each plan is large.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Updated query-plan caching for bulk create operations. Requests that include these operations no longer use cached plans, while cacheable queries continue to use caching.

@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: prisma/orm
  • Branch: v7
  • File: .coderabbit.yml
  • Commit: 93ad369
  • Layers: Repository YAML
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 274ab0a9-a426-45e6-bd00-7832c1211e61
Commits

Reviewing files that changed from the base of the PR and between c7dc141 and c2986c1.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

ClientEngine now uses isCacheableQuery to control single-query plan-cache access. It also reads or writes a batch cache entry only when every query in the batch is cacheable. The helper excludes createMany and createManyAndReturn.

Changes: Client plan-cache eligibility

Layer / File(s) Summary
Apply cacheability checks
packages/client/src/runtime/core/engines/client/ClientEngine.ts
request uses isCacheableQuery for cache access. requestBatch accesses the batch cache only when every query is cacheable. The helper excludes createMany and createManyAndReturn.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · previous review c7dc1

The change is intended to keep row-count-dependent bulk-write plans out of the batch cache. No actionable merge-blocking risk is evident from the supplied context.

Security architecture risk: Minimal · up to c7dc1

The change reduces memory retention for bulk writes without changing which operations can run or their transaction controls. No introduced or worsened security exposure was identified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The directly affected resource is the ClientEngine instance's in-process plan cache. Database operations continue through existing execution and transaction inputs; no wider tenant, service, or environment exposure is established by the scoped change.

Trust Boundaries and Controls

  • inferred — Caller-supplied query actions now determine whether a batch plan may be retained, but do not select a new execution sink or gain transaction authority. Bypassing the cache follows the same compilation path previously used on a cache miss, rather than bypassing an authorization control.

Resilience and Maintainability Implications

  • inferred — Excluded batches publish no cache entry requiring cleanup on failure or repetition. Cache operations are synchronous, and request-local values remain separate from retained plans. The reviewed diff leaves execution failure handling and transaction recovery unchanged; interruption and concurrency conclusions are source-based, not runtime-tested.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 clearly summarizes the main change: batch query plans containing createMany are not cached.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

🧪 Generate unit tests (beta)

  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
ℹ️ Recent review info

⚙️ Run configuration

  • Configuration used: Repository: prisma/orm/.coderabbit.yml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 8e5e4400-604f-454b-a1c9-d8ad6521a191
📥 Commits

Reviewing files that changed from the base of the PR and between 9afb1a4 and c7dc141.

📒 Files selected for processing (1)

  • packages/client/src/runtime/core/engines/client/ClientEngine.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@wmadden-electric

Copy link
Copy Markdown
Contributor

Thank you for this, and sorry for the long wait. We want this change and are getting it ready to merge. One thing only you can do: the repository now requires a Developer Certificate of Origin sign-off on every commit (see CONTRIBUTING), and the commits on this branch have none. Please add it with git rebase --signoff origin/v7 (or git commit --amend --signoff for a single commit) and force-push your branch. We will take care of anything else the change still needs.

The single query path does not cache createMany and createManyAndReturn
plans, because their shape changes with the row count. The batch path
cached them: a $transaction([deleteMany, createMany]) array got one new
batch plan entry for each row count. These plans grow with the number of
rows, so the batch cache can hold gigabytes of plans that are never used
again.

Use the same check in both paths. A batch that contains a createMany or
createManyAndReturn query is compiled but not cached.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: luisopine <[email protected]>
@lpbonomi
lpbonomi force-pushed the fix/batch-plan-cache-create-many branch from c7dc141 to c2986c1 Compare November 20, 2026 16:44
@lpbonomi

Copy link
Copy Markdown
Author

Done! Thank you!

This branch has not been deployed

No deployments
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.

2 participants