Skip to content

Let subplans extracted by plan optimizations inherit limits and resources - #130378

Merged
alexey-milovidov merged 4 commits into
masterfrom
extract-subplan-inherit-resources
Nov 18, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
extract-subplan-inherit-resources

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Related: #102192

Split out of #102192 (part 6 of 6).

The member QueryPlan::extractSubplan(Node *) and QueryPlan::cloneSubtree already copy max_threads, concurrency_control and the resource holder into the new plan, but the static QueryPlan::extractSubplan(Node *, Nodes &) used by plan optimizations did not. A subplan extracted there may be executed after the outer plan has been consumed into a pipeline (e.g. the right side of a JOIN converted to IN is built later as a set), so it ran with the default thread fan-out, without concurrency control, and without holding the table locks, storage holders and interpreter contexts of the source plan.

A new static overload takes the source plan, which Optimization::ExtraSettings carries as source_plan, and tryConvertJoinToIn uses it. Lazy materialization keeps the overload without resources: its fragments are put back into the source plan, which already holds them, and copying them in would append the plan's resources to itself with every rewrite. Covered by a new case in gtest_query_plan_execution_limits and by 05768_lazy_materialization_join_union_resources, which checks that many UNION ALL arms rewritten by lazy materialization stay within a memory limit.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/130378]

Version info

  • Merged into: 26.11.1.2775-master (included in 26.11 and later)

alexey-milovidov and others added 2 commits November 9, 2026 18:15
…rces

Split out of #102192.

The member `QueryPlan::extractSubplan(Node *)` already copies `max_threads`,
`concurrency_control` and the resource holder into the extracted plan, but the
static `QueryPlan::extractSubplan(Node *, Nodes &)` used by plan optimizations
(`tryConvertJoinToIn`, lazy materialization) did not. A subplan extracted there
may be executed after the outer plan has been consumed into a pipeline (e.g. the
right side of a `JOIN` converted to `IN` is built later as a set), so it ran with
the default thread fan-out, without concurrency control, and without holding the
table locks, storage holders and interpreter contexts of the source plan.

The static overload now takes the source plan, which `Optimization::ExtraSettings`
carries as `source_plan`. The fragment that `applyParallelReplicas` cuts with
`cloneSubtree` also gets the thread cap and concurrency control of its plan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`QueryPlan::cloneSubtree` already copies `max_threads`, `concurrency_control`
and the resources of the source plan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Nov 9, 2026
…erit-resources

Resolve the conflict in `optimizeLazyMaterialization3.cpp`: keep the extra
`query_plan` argument of `assembleSource` and take the
`VectorWithMemoryTracking` parameter type from master.

Pass `query_plan` to the new `makeExtraSettings` call for
`lowerArrayJoinFunctions` that came from master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a77e3c8]

Summary: ✅


AI Review

Summary

This PR preserves execution limits and shared resource ownership for standalone subplans extracted by the JOIN-to-IN optimization. The previously reported resource multiplication during lazy materialization is fixed, and the new repeated-UNION ALL regression passes in Fast test at a77e3c84239c4519f07f661f64dfa1d03e8ec702. No outstanding findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 14, 2026
Address review: `assembleSource` in lazy materialization for `JOIN` extracted each
source with the overload of `QueryPlan::extractSubplan` that inherits the resources
of the outer plan, but `apply` puts the fragments back into that same plan via
`unitePlans` and `replaceNodeWithPlan`, which append resources. Every rewrite thus
appended the plan's resources to itself several times, and with many `UNION ALL`
arms their number grew exponentially until `MEMORY_LIMIT_EXCEEDED`.

Restore the plain `extractSubplan(Node *, Nodes &)` overload, which holds no
resources, for fragments that are reinserted into their owner, and keep the
inheriting overload only for standalone subplans (`tryConvertJoinToIn`).
Add a regression test with 12 `UNION ALL` arms under `max_memory_usage`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a77e3c842 with master f3e79c38e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

292 translation units recompiled, 3216 s compile time in total, 292 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 18, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 18, 2026
Merged via the queue into master with commit f6b0b9f Nov 18, 2026
193 checks passed
@alexey-milovidov
alexey-milovidov deleted the extract-subplan-inherit-resources branch November 18, 2026 21:44
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants