Skip to content

[Bug]: EXPLAIN ANALYZE omits materialized CTE spill bytes #27107

Closed

Description

@Ariznawlll

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

  1. Create a table with 800,000 rows and 19 BIGINT payload columns.
  2. Patch table statistics to 100,000 rows so the reusable CTE is materialized and actual runtime output crosses the in-memory boundary.
  3. Execute a two-consumer materialized CTE limited to 500,000 source rows.
  4. Around the query, read HashBuildBudgetBytesCounter{component="spill_disk", action="reserve", scope="query"}.
  5. Run the same query as EXPLAIN (ANALYZE TRUE) and inspect all plan nodes for a positive SpillSize.

Actual behavior

In three consecutive rounds:

  • runtime spill reservation increased by 13,162,036 bytes 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 Scan consumers;
  • 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 TestIssue26408JoinSpillSQLRegression asserts a positive SpillRows and SpillSize for non-CTE HashJoin spill, so EXPLAIN ANALYZE reporting works on the ordinary operator path.
  • Duplicate search: GitHub searches for SpillSize, EXPLAIN ANALYZE, materialized CTE and 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

Activity

  1. added this to the MO-202608 milestone on Aug 13, 2026
  2. added theissue type on Aug 13, 2026
  3. added
    kind/bug
    on Aug 13, 2026
  4. assigned and unassigned on Aug 13, 2026
  5. Ariznawlll commented on Aug 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:3af8be1e400d289fe2e538c485a9291f01f7e988
    • 部署方式:本地隔离构建;1 Log / 1 TN / 2 CN;按物化 CTE spill 测试所需配置执行
    • 测试日期:2026-08-15

    原问题验证

    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 覆盖

    1. TestMaterializedDispatchAttributesSourceSpill 连续 3 轮通过,物化 CTE source 的 spill 属性可被 dispatch 路径读取。
    2. TestSharedMaterializedSourceSpillBudgetExactBoundaryAndCleanup 连续 3 轮通过,覆盖 spill budget 边界及清理。
    3. TestSharedMaterializedSourceAllowsDependentReaders 连续 3 轮通过,覆盖多个 reader 依赖同一物化 source。
    4. Test_explainResourceOverview 连续 3 轮通过,并观察到正的 SpillSize。

    Unhappy Path 覆盖

    1. 本轮未使用新的非法 SQL;测试重点是原问题的负向观测(物化 CTE 实际发生 spill 时分析计划不应再报告零 spill)。在修复后的定向测试中未复现该错误。

    白盒验证

    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
    • spill source/reader 定向测试:3/3;-race:通过
    • 修复提交:223b659e2

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions