Repository navigation
fix(mergetop): handle large LIMIT safely - #27999
Conversation
dad11fb to
5d966e3
Compare
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
aptend
left a comment
There was a problem hiding this comment.
Deep review completed at exact head 1e9c5de.
I reviewed the issue reproduction, complete PR diff, current-base merge, planner/operator topology, allocation ownership, rollback, failure classification, cancellation/early-stop cleanup, and all current review history (there were no prior review threads).
The vector fix preserves the owned area until growth succeeds and returns the typed allocator-limit reason. MergeTop converts that terminal reason to a controlled resource error while its rollback and Free paths retain zero-owner closure. For large or runtime multi-scope limits, local spill-capable Top operators followed by global MergeOrder and Limit preserve top-N semantics without adding a second external algorithm to MergeTop; small literal and single-scope paths retain their intended topology.
Validation on this exact head passed:
- all focused new regressions, including allocator-limit/area preservation and compile topology
- full
./pkg/container/vector,./pkg/sql/colexec/mergetop, and./pkg/sql/compilepackage tests - focused race tests for all new regressions
go vetfor all three owning packagesgit diff --checkfor the complete PR diff
No blocking issue found.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep-reviewed exact head 1e9c5de.
The fix closes both layers of #27968. Vector growth now computes the allocator bound before replacing the owned area, so a failed grow leaves the prior allocation reachable for append rollback and eventual cleanup. MergeTop classifies the terminal allocator-limit reason as a controlled resource error without treating ownership/invariant failures as retryable pressure.
For multi-scope large or runtime LIMIT, local spill-capable Top -> global MergeOrder -> global Limit preserves global Top-N: any row excluded from a local Top-N cannot enter the global Top-N. I also traced the multi-CN grouping counterexample: an intermediate Merge may interleave producer streams, but each emitted Top batch remains an independently sorted run and MergeOrder merges all received batches as runs, so ordering is not assumed across that intermediate stream. LIMIT 0, exact threshold, threshold+1, dynamic/prepared LIMIT, MaxUint64-scale bounds, single-scope routing, cancellation, early stop, Reset/Free, spill FD/disk cleanup, and wide varlen allocation failure all terminate through existing bounded owners.
There is no new per-row logging or synchronization. Small literal plans retain the resident fast path; large plans trade the unsafe global resident copy for existing spill-accounted operators. Exact-head focused/package/race/vet evidence and substantive CI are green, and the current-main merge tree is clean. No remaining correctness, performance, panic, hang, leak, or OOM blocker found.
Merge Queue Status
This pull request spent 49 minutes 19 seconds in the queue, including 47 minutes 10 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #27968
What this PR does / why we need it:
MergeTopintentionally has no external algorithm, as specified bydocs/design/query_memory_control_and_attribution_impl.md. Unsupported resident pressure must return one controlled resource error and release its allocation generation.The reported panic had two causes:
unionBatchContiguousVarlenRangeassigned a failed area growth directly tov.area. Crossing the allocator single-allocation ceiling therefore replaced the still-owned old area with nil beforeMergeToprollback, causinginvalid vector append checkpointand losing the vector allocation owner.MergeTopcould not convert it to the required controlled resource error.This change keeps
MergeTopresident-only and adds two complementary protections:Capacity failures preserve the old vector area and are classified as
ErrAllocationAllocatorLimit, allowingMergeTopto return a controlled resource-exhausted error with terminal ownership at zero.Multi-scope large or runtime LIMIT plans reuse existing spill-capable operators:
local Top -> global MergeOrder -> global LimitSmall literal limits continue to use
Top -> MergeTop, and single-scope plans continue to use localTop. No external algorithm, spill file, or disk/FD lifecycle is added toMergeTop.Validation:
make buildgo vet -mod=readonly ./pkg/container/vector ./pkg/sql/colexec/mergetop ./pkg/sql/compile./pkg/container/vector,./pkg/sql/colexec/mergetop, and./pkg/sql/compilepackage testsgrowOwned: 92.3%unionBatchContiguousVarlenRange: 90.6%mergeTopTerminalCapacityError: 100%canUseResidentMergeTop: 100%compileTop: 100%mo-testercomparison mode twice for static and prepared dynamic large-LIMITINSERT ... SELECTcases: 19/19 passed each runtopoperators and globallimit -> merge order, with nomerge top