Skip to content

[4.2-dev] fix(sql): reclaim materialized CTE reader batches - #30796

Merged
XuPeng-SH merged 3 commits into
matrixorigin:4.2-devfrom
LeftHandCold:fix/4.2-materialized-cte-ownership-30790
Oct 22, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:4.2-devfrom
LeftHandCold:fix/4.2-materialized-cte-ownership-30790

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

Fixes #30790

What this PR does / why we need it:

Adapt the materialized-reader ownership fix from main #26936
(690fed8e1f03fa716494b5f2dbc2304d040c0b98) to 4.2-dev, without backporting
its unrelated foreign-key changes.

materialized.Source.Next returns an independent caller-owned batch. Ordinary
4.2 Merge readers did not retain this copy for cleanup. A pass-through Projection
borrows its vectors, so neither downstream teardown nor Source cleanup reclaims
the returned copy. Q15 can therefore succeed while leaking off-heap memory.
Native profile deltas and a real operator reproduction both confirmed
4,000,256 bytes / 52 allocations left for the observed two-consumer input.

Changes and release-branch compatibility

  • Retain one current materialized batch and reclaim it before the next ordinary
    reader Call, as well as on Prepare, Reset and Free.
  • Make Free release the source reader when Reset has not run; preserve
    idempotent teardown.
  • Preserve the 4.2-only MaterializedSingleBatch contract: scalar vector-query
    TVFs may still borrow their first batch after EOF, until Reset/Free. Keep the
    existing rejection of a second scalar batch. This is why an unmodified
    cherry-pick of main's unconditional per-Call cleanup is inappropriate.
  • Keep non-materialized Merge, planner decisions, allocation algorithms,
    transport and persistent formats unchanged. No additional batch copies,
    goroutines, locks, or I/O are introduced.

Validation

Base: 26d3a200cb13f4be3b5f64d489bac75d8dcc5814.
Local validation used the repository CGo wrapper on macOS arm64 / Go 1.26.4.

  • Before the production change, the new borrowing and terminal-cleanup tests
    fail on the unmodified base with unclaimed batches.
  • Real Source -> Merge -> Projection diagnostic: the 100k-group input changes
    from 4,000,256 outstanding bytes / 52 allocations to zero. This larger
    diagnostic is not added to CI; permanent tests use only tiny batches.
  • Full owning merge package passes, including scalar EOF lifetime, rejection
    of multiple scalar batches, concurrent readers and MergeOrder spill.
  • materialized, projection, and mergeorder package tests pass.
  • Full merge package passes under -race; the borrowing, terminal cleanup,
    concurrent-reader and scalar-lifetime tests each pass 100 focused race runs.
  • Existing frontend integration test
    TestIssue26131Q15SharedCTEExecutesBothConsumers passes (16.18 s), including
    the two-Sink-Scan plan assertion and expected SQL result. No new large BVT or
    embedded cluster fixture is added.
  • Changed-code golangci-lint reports 0 issues; git diff --check passes.

The incident reproduction and local checks do not replace release CI or a
post-upgrade stability observation. No production cluster was modified.

Adapt the Merge ownership cleanup from main commit 690fed8 (matrixorigin#26936), preserving the 4.2 scalar-reader batch lifetime through EOF. Add bounded-copy, terminal-cleanup and reuse regressions.

Fixes matrixorigin#30790
@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 →

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

代码 review 结论:未发现需要阻断合并的问题,代码层面认可本次修复。当前账号与 PR 作者相同,GitHub 不允许 self-approve,因此以 COMMENT 记录结论;这不是已完成的独立 GitHub approval。

评审范围:HEAD f2dfbaeb0c10f8da46e58220815f087e1ba8f832,base 4.2-dev@26d3a200cb13f4be3b5f64d489bac75d8dcc5814,merge-base 与上述 base 相同。检查了全部 3 个变更文件,以及 Source、Projection、MergeOrder、spool、编译/执行域和 pipeline cleanup 的相关调用链。当前没有待处理的 inline review 问题。

设计与回归风险

  • 释放责任放在返回 caller-owned batch 的 Merge reader 上是合适的。Source.Next 返回独立副本,Projection 的列引用只借用 vector,不能指望 Projection 或 Source 的存储清理替它回收。此次复用已有的 materializedBatch/reader-release 状态,不增加另一套所有权管理。
  • 普通 reader 在下一次 Call(包括 EOF、producer error、cancel)或 Prepare/Reset/Free 清理当前批次;保留数据的下游路径有自己的复制/同步消费边界:MergeOrder 内存路径 Dup、spill 路径先序列化,spool 也复制 vector。没有发现此次提前回收破坏这些消费者的可达路径。
  • 4.2 的 MaterializedSingleBatch 分支必须保留。 标量向量查询可能在 EOF 后继续借用首批数据,不能机械照搬 main 的无条件 next-Call 清理。当前实现仍保留到 Reset/Free,并拒绝第二批数据,兼容处理正确。
  • Reset/Free 幂等,Free 补齐 reader release;生产者 Finish、reader ReleaseReader、执行域 Close 的职责未被重建。没有额外批次复制、goroutine、新锁或 I/O;每批增加的是必要的回收操作,未发现实质性能退化路径。非 materialized Merge、planner、传输和持久化格式未改。

Unhappy-path 审计

维度 结论
Q1 清理责任 reader 持有返回副本并兜底清理;Source 单独管理原始存储。EOF、错误、取消、提前结束、重复清理及复用均有对应处理。
Q2 等待 未新增 goroutine、新锁或等待生产者结束的协议;沿用 Source 的通知/取消和 pipeline 终态清理。
Q3 增长 每个 reader 最多保留一个当前批次,不再随读取批次数累积副本;Source 既有 spill/budget 机制不变。

验证依据与边界

本轮复核了本地已有原始测试日志及当前干净工作树(/private/tmp/mo30790-source-42,HEAD 与 PR 一致),没有重复启动重型测试:

  • 新增回归测试在修复前失败;既有 100k-group operator 诊断日志显示 outstanding bytes/allocations 从 4,000,256 / 52 变为 0 / 0。
  • merge 全包普通及 race 测试通过;borrowing、terminal cleanup、并发 reader、scalar lifetime 四个 focused race 测试各 100 次通过。
  • materialized、projection、mergeorder 包通过。
  • TestIssue26131Q15SharedCTEExecutesBothConsumers 通过(16.18s);核对其真实 SQL、双 Sink Scan 计划断言和返回行断言。
  • 本轮独立执行 git diff --check 通过。

未新增 benchmark 或线上验证;上述局部证据不等于整套 release regression 已通过。提交 review 时 CI run 39719432722 的 shared build、Ubuntu UT、SCA 和 coverage 仍在运行,合并仍应遵守仓库 CI 门禁。

@XuPeng-SH

XuPeng-SH commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · failed

Commit: f2dfbaeb0c10f8da46e58220815f087e1ba8f832

fatal: unable to access 'https://github.com/matrixorigin/matrixone.git/': Recv failure: Connection reset by peer

@XuPeng-SH

XuPeng-SH commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-xupeng · done

Commit: f2dfbaeb0c10f8da46e58220815f087e1ba8f832

Review completed. Read the assessment.

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

mo-bot review · mo-bot-xupeng · review

Commit: f2dfbaeb0c10f8da46e58220815f087e1ba8f832

Assessment: approve

Reviewed the complete three-file delta and its ownership, cleanup, cancellation, reuse, and downstream consumer paths. The fix reclaims ordinary reader batches while preserving scalar batch lifetime through EOF. No actionable correctness, design, or coverage defect remains.

Model: gpt-6-astra / xhigh

Session: 01a14d34-4e51-79a3-a62d-6c9c16279dc8 · slot 0 · generation 0

Validation:

  • Isolation preflight, fresh repository-owned native build, native provenance verification, and git diff --check passed.

  • Retained logs contain 79 passing top-level tests and package PASS events for merge, materialized, projection, and mergeorder, with no test failures. The enclosing command exited 124; overall command success is not claimed.

  • Both new regression tests passed at head and failed with concrete outstanding-memory assertions when the two production files were restored to verified base blobs through a diagnostic overlay.

  • Final audit verified unchanged frozen source and matching native artifacts, and reconciled commands, receipts, log hashes, outcomes, and scope in /home/xupeng/.local/state/mo-bot/jobs/32d73f4cdc768bc51a5cc15c/attempt-1/validation-audit.json.

Limitations:

  • Focused race and Q15 frontend integration attempts, including bounded retries, timed out without test events. Adaptive race repetitions and the full-package race run were not executed.

  • Later diagnostic-clone source inventories timed out. Earlier executed tests have complete exact-head fingerprints; the final audit independently confirmed an empty tracked delta.

  • Author raw logs and the linked issue body were unavailable. Review used the explicit contract in frozen PR evidence; CI status and author assertions were not treated as independently verified execution.

  • No independent incident-scale memory profile, performance benchmark, full release-suite run, or post-upgrade observation was performed.

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.

3 participants