Skip to content

fix(mergetop): handle large LIMIT safely - #27999

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
ck89119:issue-27968-main
Sep 2, 2026
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
ck89119:issue-27968-main

Conversation

@ck89119

@ck89119 ck89119 commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #27968

What this PR does / why we need it:

MergeTop intentionally has no external algorithm, as specified by docs/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:

  • unionBatchContiguousVarlenRange assigned a failed area growth directly to v.area. Crossing the allocator single-allocation ceiling therefore replaced the still-owned old area with nil before MergeTop rollback, causing invalid vector append checkpoint and losing the vector allocation owner.
  • Impossible vector growth was returned as an unclassified internal error, so MergeTop could not convert it to the required controlled resource error.

This change keeps MergeTop resident-only and adds two complementary protections:

  1. Capacity failures preserve the old vector area and are classified as ErrAllocationAllocatorLimit, allowing MergeTop to return a controlled resource-exhausted error with terminal ownership at zero.

  2. Multi-scope large or runtime LIMIT plans reuse existing spill-capable operators:

    local Top -> global MergeOrder -> global Limit

Small literal limits continue to use Top -> MergeTop, and single-scope plans continue to use local Top. No external algorithm, spill file, or disk/FD lifecycle is added to MergeTop.

Validation:

  • make build
  • go vet -mod=readonly ./pkg/container/vector ./pkg/sql/colexec/mergetop ./pkg/sql/compile
  • full ./pkg/container/vector, ./pkg/sql/colexec/mergetop, and ./pkg/sql/compile package tests
  • compiler graph coverage for threshold, above-threshold, dynamic LIMIT, and single-scope plans
  • changed-function coverage:
    • growOwned: 92.3%
    • unionBatchContiguousVarlenRange: 90.6%
    • mergeTopTerminalCapacityError: 100%
    • canUseResidentMergeTop: 100%
    • compileTop: 100%
  • mo-tester comparison mode twice for static and prepared dynamic large-LIMIT INSERT ... SELECT cases: 19/19 passed each run
  • verified the AP physical plan contains local top operators and global limit -> merge order, with no merge top

@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label Sep 2, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Sep 2, 2026
@matrix-meow matrix-meow added size/M Denotes a PR that changes [100,499] lines and removed size/XL Denotes a PR that changes [1000, 1999] lines labels Sep 2, 2026
@ck89119 ck89119 changed the title fix(mergetop): spill large LIMIT payloads fix(mergetop): preserve vectors on capacity failure Sep 2, 2026
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@aptend aptend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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/compile package tests
  • focused race tests for all new regressions
  • go vet for all three owning packages
  • git diff --check for the complete PR diff

No blocking issue found.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@mergify mergify Bot added the queued label Sep 2, 2026
@mergify

mergify Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-02 15:11 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-09-02 16:00 UTC · at 17c161a31b47a22e71ca222839143f66717ac365 · squash

This pull request spent 49 minutes 19 seconds in the queue, including 47 minutes 10 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:IVFFlat 10M DML 后 INSERT SELECT ORDER BY LIMIT 触发 invalid vector append checkpoint panic

4 participants