Repository navigation
Description
Description
EXPLAIN (ANALYZE TRUE) does not report SpillSize for a materialized CTE even when runtime metrics prove that the CTE reserved and released spill-disk bytes.
This was found while validating #26131. The original CTE reuse/cancellation issue is fixed; this report is about missing execution observability.
Environment
- Branch:
main - Commit:
1d7b6311ce91df0968da435bb405f3d68137c595 - Deployment: embedded 1 Log + 1 TN + 2 CN
- Both CNs:
processLimitationSize=1 GiB,processLimitationSpillSize=32 MiB,max_dop=1
Steps to reproduce
- Create a table with 800,000 rows and 19
BIGINTpayload columns. - Patch table statistics to 100,000 rows so the reusable CTE is materialized and actual runtime output crosses the in-memory boundary.
- Execute a two-consumer materialized CTE limited to 500,000 source rows.
- Around the query, read
HashBuildBudgetBytesCounter{component="spill_disk", action="reserve", scope="query"}. - Run the same query as
EXPLAIN (ANALYZE TRUE)and inspect all plan nodes for a positiveSpillSize.
Actual behavior
In three consecutive rounds:
- runtime spill reservation increased by
13,162,036bytes and the same number of bytes was released; - the query returned the exact expected 500,000-row aggregate result;
- the analyzed plan contained the materialized producer and two
Sink Scanconsumers; - no plan node reported a positive
SpillSize(the field was omitted because the accumulated value was zero).
Expected behavior
EXPLAIN ANALYZE should attribute materialized-CTE spill bytes/rows to the producing CTE/Sink node, or otherwise expose them in the query-level analyzed statistics. A query that physically spills must not appear spill-free.
Stability and controls
- Reproducer: 3/3 on the latest official
main, with a positive independent spill-reservation delta in every round. - Happy control: the query result, spill release accounting and both CN health checks pass.
- Ordinary-spill control: the existing
TestIssue26408JoinSpillSQLRegressionasserts a positiveSpillRowsandSpillSizefor non-CTE HashJoin spill, soEXPLAIN ANALYZEreporting works on the ordinary operator path. - Duplicate search: GitHub searches for
SpillSize,EXPLAIN ANALYZE,materialized CTEand spill returned no matching issue other than parent [Optimizer] TPCH Q15 executes the same multi-reference CTE twice #26131.
Evidence
round 1: spill_disk reserve delta=13162036, positive SpillSize=false
round 2: spill_disk reserve delta=13162036, positive SpillSize=false
round 3: spill_disk reserve delta=13162036, positive SpillSize=false
Related validation comment: #26131 (comment)
Code analysis
The materialized source is initialized in pkg/sql/compile/compile.go with its own spill budget, while analyzed node statistics are accumulated from PhyOperator.OpStats in pkg/sql/compile/analyze_module.go. Operator-level tests prove a materialized reader/order path can record spill stats, but this CTE fanout spill is managed outside the displayed operator statistics, so its bytes are not attached to a plan node. This is an inference from the runtime metric and current aggregation topology.
Regression coverage
After the fix, add a configured SQL integration regression that proves a positive spill-budget delta and requires a positive SpillSize in EXPLAIN ANALYZE for the materialized CTE. Keep the existing ordinary HashJoin spill test as the control. The generated wide fixture and low spill cap make the configured Go issue/integration suite a better fit than default MOTR.
Related
- Parent validation: [Optimizer] TPCH Q15 executes the same multi-reference CTE twice #26131
- Existing ordinary HashJoin spill control:
pkg/tests/issues/issue_26408_join_spill_test.go
Activity
fix(colexec): report materialized CTE spill stats (#27110)
Ariznawlll commentedon Aug 15, 2026
测试版本
main3af8be1e400d289fe2e538c485a9291f01f7e988原问题验证
Issue 原始复现依赖 800,000 行宽表、统计信息 patch 和
spill_disk指标读取;Issue 中未提供可直接运行的统计 patch 脚本。本轮因此使用修复路径的可重复黑盒/白盒验证:执行EXPLAIN ANALYZE资源统计测试并检查物化 CTE sink/source spill 统计。Test_explainResourceOverview连续 3 轮输出MemoryUsage:17408B, SpillSize:17408B,不再出现物理 spill 但SpillSize=0的结果。Happy Path 覆盖
TestMaterializedDispatchAttributesSourceSpill连续 3 轮通过,物化 CTE source 的 spill 属性可被 dispatch 路径读取。TestSharedMaterializedSourceSpillBudgetExactBoundaryAndCleanup连续 3 轮通过,覆盖 spill budget 边界及清理。TestSharedMaterializedSourceAllowsDependentReaders连续 3 轮通过,覆盖多个 reader 依赖同一物化 source。Test_explainResourceOverview连续 3 轮通过,并观察到正的SpillSize。Unhappy Path 覆盖
白盒验证
go test ./pkg/sql/internal/materialized ./pkg/sql/colexec/dispatch -run 'Test(MaterializedDispatchAttributesSourceSpill|SharedMaterializedSourceSpillBudgetExactBoundaryAndCleanup|SharedMaterializedSourceAllowsDependentReaders)' -count=3 -v通过;同组 spill 测试使用-race -count=1通过。go test ./pkg/sql/compile -run 'Test_explainResourceOverview|TestMaterializedCTE' -count=3 -v通过。回归测试
该问题由主分支修复提交
223b659e2ba89774d846bf7d52ca092b98622779覆盖。由于用例依赖低 spill 上限、宽数据和统计信息 patch,回归归属为配置化 spill/集成或 big-data workflow,不放入默认 MOTR SQL;MOTR PR #112 已明确排除该配置化场景,本轮没有新增默认 MOTR 基线。测试证据
Test_explainResourceOverview:MemoryUsage:17408B, SpillSize:17408B-race:通过测试结论
测试通过。
fix(colexec): report materialized CTE spill stats (matrixorigin#27110)
fix(colexec): report materialized CTE spill stats (matrixorigin#27110)
fix(colexec): report materialized CTE spill stats (matrixorigin#27110)