Repository navigation
Conversation
Fixes unbounded memory growth in QueryPlanCache where each batch was counted as one entry regardless of how many individual query plans it contained. With maxSize=1000, if every batch held 1000 plans, the cache could accumulate 1,000,000 query plans while appearing to respect the limit. Fixes two related issues: 1. Batch eviction now tracks total query plans (not just batch count), ensuring total cached plans never exceed maxSize. 2. Replacement entries (existing key in setBatch) now trigger the eviction loop instead of returning early, preventing the total from growing unbounded when the same batch key is repeatedly replaced with larger response batches. Tests added: - 'evicts oldest batch entry when at capacity (tracking total queries)' verifies LRU eviction based on plan count not entry count. - 'triggers eviction when replacing an existing batch exceeds max total queries' is a regression test for the early-return replacement bug. - 'does not cache a batch if its queries exceed maxSize' verifies that oversized single batches are silently skipped. Co-Authored-By: Claude <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesBatch cache capacity
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In @.status.txt:
- Around line 1-2: Remove the scratch investigation log .status.txt from the
change unless repository policy explicitly requires it; retain only source or
documentation files intended for long-term maintenance.
In `@packages/client/src/runtime/core/engines/client/query-plan-cache.ts`:
- Around line 59-64: Update the queryCount calculation in setBatch to count
every cached batch as at least one query, including multi responses with an
empty plans array. Preserve the existing plans.length count for non-empty multi
responses and the single-query count for other response types so empty entries
contribute to `#batchCacheTotalQueries` and trigger eviction.
- Line 59: Update the queryCount calculation in the query-plan cache to
distinguish both BatchResponse variants: count multi responses using
response.plans.length, and count compacted responses using
response.arguments.length. Use this execution count when evicting entries from
`#batchCache`.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 779529df-a6ed-4bd8-83da-08a744284415
📒 Files selected for processing (3)
.status.txtpackages/client/src/runtime/core/engines/client/query-plan-cache.test.tspackages/client/src/runtime/core/engines/client/query-plan-cache.ts
Address 3 CodeRabbit findings on the QueryPlanCache fix:
1. Remove .status.txt scratch file from the PR (not project docs)
2. CompactedBatchResponse now counts as arguments.length not 1.
response.type === 'compacted' entries with many argument sets previously
counted as 1 toward the eviction budget regardless of how many actual
executions they represented — another unbounded-growth path.
3. Empty multi batches count as 1 (Math.max(plans.length, 1)).
A { type:'multi', plans:[] } batch previously contributed 0 to
batchCacheTotalQueries and could never trigger eviction.
All 18 tests pass.
Co-Authored-By: Claude <[email protected]>
|
Hey @aqrln, this PR fixes a memory leak in QueryPlanCache that was causing unbounded memory growth (the root cause of issue #29774). Key changes:
All 18 unit tests pass. CI checks are running. Could you take a look when you get a chance? |
| cache.setBatch('key1', { | ||
| type: 'multi' as const, | ||
| plans: [ | ||
| { type: 'value' as const, args: 1 }, | ||
| { type: 'value' as const, args: 2 }, | ||
| ], | ||
| }) // Total: 2 | ||
| cache.setBatch('key2', { | ||
| type: 'multi' as const, | ||
| plans: [{ type: 'value' as const, args: 3 }], | ||
| }) // Total: 3 |
There was a problem hiding this comment.
why do we need as const everywhere here, can't TypeScript infer it from the argument type?
There was a problem hiding this comment.
as const on variables preserves the literal type discriminator — without it TS widens 'value' to string, breaking assignability to QueryPlanNode.
…eding max total queries
|
Hi @aqrln , @nurul3101 @sorenbs could you please take a look at this PR when you have a moment? Thanks in advance! |
|
Thank you. The batch plan cache is already limited by its number of entries; the memory comes from a few very large plans. #30581 stops caching those, which fixes the measured cause without changing what |
Summary
Fixes unbounded memory growth in
QueryPlanCachewhere each batch was counted as one entry regardless of how many individual query plans it contained. WithmaxSize=1000, if every batch held 1000 plans, the cache could accumulate 1,000,000 query plans while appearing to respect the limit.Changes
Root causes fixed
Batch eviction now tracks total query plans — a new
#batchCacheTotalQueriescounter ensures thewhile(totalQueries > maxSize)loop actually caps the number of cached query plans, not batches. Previously,#batchCache.size >= #maxSizemeant the cache could hold unbounded plans per batch entry.Replacement entries trigger eviction — old
setBatchreturned early after updating an existing entry, skipping the eviction loop entirely. Now replaced entries flow through the same insertion+eviction path as new keys. CodeRabbit caught this regression: replacing a 1-plan batch with a 4-plan batch in a 4-plan cache would leave 6 total plans in cache.Skip oversized single batches — a new guard rejects
setBatchof a single batch whose query count already exceedsmaxSize, preventing a single query from exhausting the entire cache.Files Changed
packages/client/src/runtime/core/engines/client/query-plan-cache.ts#batchCacheTotalQueries,whileloop, skip guardpackages/client/src/runtime/core/engines/client/query-plan-cache.test.tsTesting
All 16 QueryPlanCache unit tests pass:
Related Issues
Closes #29774 (memory leak in query compiler, memory grows from ~100MB to ~550MB over an hour).
Relates to PR #29784 (incoming
QueryPlanCacheupdate with same goal but missing the replacement-eviction fix).Summary by CodeRabbit