Skip to content

executor: add exact memory accounting for group, order, top, and fill - #27112

Merged
XuPeng-SH merged 27 commits into
matrixorigin:mainfrom
aptend:feature/25866-m4-allocation-accounting
Aug 15, 2026
Merged

XuPeng-SH merged 27 commits into
matrixorigin:mainfrom
aptend:feature/25866-m4-allocation-accounting

Conversation

@aptend

@aptend aptend commented Aug 13, 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:

issue #25866

What this PR does / why we need it:

Adds one exact physical-allocation accounting path for query execution:

  • generalizes the execution resource budget and adds fixed owner/site attribution;
  • migrates Group/MergeGroup, Order/MergeOrder, Top/MergeTop, and Fill;
  • bounds spill memory, disk, and file descriptors, with controlled pressure errors and terminal-zero cleanup;
  • leaves later operator families out of scope.

Validation summary:

  • make static-check, CGo-enabled build/vet/unit tests, owning-package race tests, and critical boundary races repeated 100 times passed;
  • resident benchmarks stayed within the 2% gate for HashBuild, Group, and every migrated operator; accounting primitives add 0 alloc/op, while operator allocation counts are unchanged or lower;
  • fault tests cover exact/one-byte-short admission, short/corrupt I/O, cancellation, Reset/reuse, and terminal memory/disk/FD zero.

@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 →

@matrix-meow matrix-meow added the size/XXL Denotes a PR that changes 2000+ lines label Aug 13, 2026
@aptend
aptend force-pushed the feature/25866-m4-allocation-accounting branch from 9a5db5f to 77b83ec Compare August 13, 2026 08:44
@mergify mergify Bot added kind/enhancement kind/documentation Improvements or additions to documentation kind/test-ci kind/refactor Code refactor labels Aug 13, 2026
@aptend
aptend force-pushed the feature/25866-m4-allocation-accounting branch from 77b83ec to 9967e52 Compare August 13, 2026 08:58
@aptend aptend changed the title executor: add exact query allocation accounting through M4 executor: add exact memory accounting for group, order, top, and fill Aug 13, 2026

@iamlinjunhong iamlinjunhong 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.

Blocking finding:

  • P1 pkg/container/vector/selection_codec.go:95: the selected-row spill codec drops dynamic binary-string provenance, so Group and aggregate results can depend on whether memory pressure triggers spill. See the inline comment for the verified write/read call chain and missing regression.

No additional P0-P3 findings were identified in the reviewed diff.

Comment thread pkg/container/vector/selection_codec.go Resolved

@aunjgr aunjgr 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.

Request changes: selected-row spill loses binary-string provenance. In pkg/container/vector/selection_codec.go:95-171, the codec writes NULL/grouping/prepare-param flags and raw bytes, but never writes GetBinaryStringMetadataAt(row). UnmarshalSelectedRowsFrom calls CleanOnlyData() and restores no binary metadata, so a TEXT/VARCHAR vector that carries dynamic per-row binary semantics is restored as ordinary text. This codec is used by Group spill (pkg/sql/colexec/group/helper.go) and aggregate-state spill (pkg/sql/colexec/aggexec/aggState.go), so results can change only when memory pressure causes spill; the resident path preserves the metadata. Please encode uniform/row-exact binary provenance (including NULL-row alignment), restore it transactionally, and add a spill/reload regression alongside the existing non-spill coverage. The exact head 34e5b50 and CI were green when reviewed.

@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.

[P1] Preserve dynamic binary-string provenance in the selected-row spill codec.

Deep review on exact head 34e5b50 confirms a pressure-dependent correctness break in pkg/container/vector/selection_codec.go. MarshalSelectedRowsTo records NULL, grouping, PrepareParamKind, and raw bytes, but never records GetBinaryStringMetadataAt(row). UnmarshalSelectedRowsFrom then calls CleanOnlyData(), which explicitly resetBinaryString(), and restores no binary metadata.

This is reachable through both changed spill closures: Group keys call appendSpillGroupByRows -> MarshalSelectedRowsTo and reload through unmarshalSpillGroupByRows; aggregate state calls writeSpillState -> MarshalSelectedFlagsTo and readSpillState -> UnmarshalSelectedRowsFrom. A T_text/T_varchar row with SetIsBinaryString(true) therefore preserves byte-string semantics while resident but becomes ordinary text after spill/reload, so query semantics can depend solely on memory pressure. The existing TestMergeGroupPreservesBinaryStringProvenance covers only the resident/partial path and does not exercise this codec.

Please extend the codec metadata with uniform and row-exact binary provenance, preserve NULL-row alignment, restore it transactionally with the other row metadata, and add direct codec plus forced spill/reload regressions. Exact-head CI being green does not cover this missing semantic axis.

@iamlinjunhong iamlinjunhong 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.

Reviewed exact head b3d98c2 against current origin/main.

The previous P1 about spill codec binary-string provenance is fixed by b3d98c2; the author replied with the matching fix/coverage and that thread is resolved.

Remaining finding:

  • P2 pkg/container/vector/vector.go:4100: selected-batch preflight omits the binary-string provenance bitmap. Group commits the hash before publishing the preflighted key vectors, so a mixed binary/text key can hit a retryable capacity allocation only after mutation and abort instead of taking the existing spill/retry path. See the inline comment for the verified call chain and missing boundary test.

No P0/P1 findings remain. Exact-head checks are complete with no failures.

Comment thread pkg/container/vector/vector.go Resolved

@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.

[P2] Complete selected-batch allocation preflight before committing Group state.

On exact head b3d98c2, PreExtendSelectedBatch* reserves descriptors, NULL/grouping bitmaps, prepare-param metadata, and varlena area, then publishes a ready proof at pkg/container/vector/vector.go:4100. It does not call PreflightUnionBatchBinaryString; the supposedly preflighted unionBatch still performs that fallible allocation at line 6742.

I reproduced this with an accounted T_text destination and two selected source rows carrying mixed binary/text provenance. A wide-budget run used 48 bytes after preflight and 56 after publication. Re-running with the account capped at the preflight result made PreExtendSelectedBatch succeed, then UnionBatchPreflighted fail with allocation account capacity exceeded: used=48 requested=8 limit=48.

This is not only a vector protocol mismatch. Group.commitGroupByChunk commits TxnItr before appendGroupByBatch calls UnionBatchPreflighted, so this late capacity failure occurs after hash mutation and bypasses the existing pre-commit spill/retry path. A query that could recover by spilling its resident prefix instead aborts under the exact cap.

Please include binary-string provenance in selected-batch preflight, ensure publication performs no allocation, and add an exact-cap Group regression with mixed binary/text keys. The previous spill-codec provenance blocker is fixed; this is the remaining closure edge.

@CLAassistant

CLAassistant commented Aug 15, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@iamlinjunhong iamlinjunhong 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.

Reviewed exact head 46c7983 against current origin/main a1e161a (merge-base 7e03000). The previous P2 about selected-batch binary-string provenance preflight was explicitly answered by the author and fixed in this head; the exact-capacity vector regression and post-hash-commit Group regression close the reported gap. I found no remaining P0-P3 issues in the current diff.

@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 re-review of exact head 46c79833f279c2dd18c096c02253a145bd412be0: the previous selected-batch binary-string allocation blocker is closed.

The new preflight rejects unsafe varlena aliases before publishing a proof, runs PreflightUnionBatchBinaryString before that proof, and leaves UnionBatchPreflighted with no late capacity allocation. This restores the Group invariant that allocation failure can occur before hash commit and still enter spill/retry.

Focused exact-head tests passed: TestSelectedBatchBinaryStringPreflightClosesExactCapacity, TestSelectedBatchPreflightProtocol, and TestGroupSelectedBinaryPreflightAllocatesNothingAfterHashCommit.

Unhappy-path audit: Q1 ownership remains with the destination vector and retained capacity is released by its existing Free path; Q2 no new waits or dependency edges are introduced; Q3 retained memory remains bounded by the allocation account and vector lifetime. I found no remaining P0-P3 issue in the current diff.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator

Retrospective linkage for #27450.

The high-cardinality GROUP BY failure was caused by allocating a new group batch before the duplicate-only fast path returned at an exact hash-group chunk boundary. That left an empty group batch without corresponding aggregate state, and later output could address a non-existent aggregate chunk.

This was fixed by commit 193f22f18e36046529804092e29ac075ffecf539 in this merged PR, which moves the duplicate-only return before the allocation.

Current-main verification is pinned to d57f99abc0b9756e07aeb09ba642471db8c40ad7. The build and existing regression gates passed locally; full issue-shape QA is being performed separately before closure.

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/documentation Improvements or additions to documentation kind/enhancement kind/refactor Code refactor kind/test-ci size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants