Skip to content

Enforce storage limits for reads inside a shipped plan fragment - #119958

Merged
devcrafter merged 6 commits into
masterfrom
enforce-storage-limits-for-shipped-plan
Sep 29, 2026
Merged

devcrafter merged 6 commits into
masterfrom
enforce-storage-limits-for-shipped-plan

Conversation

@devcrafter

@devcrafter devcrafter commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

A deserialized plan carries no storage limits: every step's deserialize builds a fresh SelectQueryInfo and nothing fills its storage_limits. The limits reach ReadProgressCallback only through the source that reports the progress, and ISource falls back to an empty list, so the byte limits and the read-speed and timeout checks were silently ignored for every read in a fragment shipped by plan-based parallel replicas or by make_distributed_plan.

The query-based implementation does not have this gap: the replica receives SQL and re-plans it, so its own interpreter derives the limits from the settings.

So derive them from the settings that arrived with the query in ReadFromMergeTree::deserialize, with buildStorageLimits, the same way resolveStorages already does for a deserialized ReadFromTableStep. Doing it at deserialization, before readFromParts, covers every execution path of a shipped plan (the TCP path and the make_distributed_plan tasks built by DistributedPlanExecutor), and also the subquery reads that index analysis builds IN sets from. The stage passed is not the initiator's, which keeps the minimal-speed limits to the initiator, the only node that sees the whole query.

Per-subquery SETTINGS overrides of these limits are still not carried: the limits are not part of any step's serialized state, so the query-level limits are restored. Carrying the per-node list would need a new ReadFromMergeTree step version that serializes StorageLimitsList. This is tracked in #122724.

Measured on a three-replica cluster reading a 100000-row MergeTree table:

parallel_replicas_plan_based = 0 parallel_replicas_plan_based = 1, before after
max_bytes_to_read_leaf = 1000 TOO_MANY_BYTES full result TOO_MANY_BYTES
max_bytes_to_read = 1000 TOO_MANY_BYTES full result TOO_MANY_BYTES

The row-count limits were already enforced on both paths, because MergeTree checks those while selecting parts - which is why this went unnoticed.

Closes: #119953
Related: #119862
Related: #122724

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed max_bytes_to_read, max_bytes_to_read_leaf and the read-speed and timeout limits being ignored for reads executed from a query plan shipped to another node, which happens with parallel_replicas_plan_based and with make_distributed_plan.


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

Version info

  • Merged into: 26.10.1.994-master (included in 26.10 and later)

A deserialized plan carries no storage limits: every step's `deserialize` builds a fresh
`SelectQueryInfo` and nothing fills its `storage_limits`, so the sources run unchecked. The limits
reach `ReadProgressCallback` only through the source that reports the progress, and `ISource` falls
back to an empty list, so the byte limits and the read-speed and timeout checks were silently
ignored for every read in a fragment shipped by plan-based parallel replicas or by
`make_distributed_plan`.

The query-based implementation does not have this gap: the replica receives SQL and re-plans it, so
its own interpreter derives the limits from the settings.

Derive them the same way on the receiving side, from the settings that arrived with the query, and
attach them to the sources of the deserialized plan. `getStorageLimits` is a pure function of the
settings and the stage, so it is reused rather than duplicated - passing the stage also keeps the
minimal-speed limits to the initiator, which is the only node that sees the whole query.

Measured on a three-replica cluster reading a 100000-row table with
`max_bytes_to_read_leaf = 1000`: query-based raised `TOO_MANY_BYTES` while plan-based returned the
full result; now both raise it. The row-count limits were already enforced on both paths, because
`MergeTree` checks those while selecting parts.

Closes: #119953

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [69a0c31]

Summary: ✅


AI Review

Summary

This PR moves storage_limits restoration into ReadFromMergeTree::deserialize, which is the right place to make shipped-plan MergeTree reads enforce the query-level byte, speed, and timeout limits before any eager index-analysis or IN-set reads run. The current diff and test updates cover the make_distributed_plan, plan-based parallel-replica, and shipped-subquery read paths that were previously missing; the still-open per-subquery SETTINGS limitation is now explicitly scoped out in the PR description and tracked separately in #122724, so I did not find a new current-head defect within the narrowed contract of this change.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 69a0c31.

Metric Baseline Current Δ
Lines 87.90% 87.90% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 14, 2026
Comment thread src/Interpreters/executeQuery.cpp Resolved
@@ -0,0 +1,37 @@
-- A read inside a shipped plan fragment has to honour the byte limits, as it does when the query is
-- shipped as SQL instead. Every step's `deserialize` builds a fresh `SelectQueryInfo`, so a
-- deserialized plan carries no `StorageLimitsList` and the receiving node derives one from the

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.

This regression only proves the top-level settings case. The code at src/Interpreters/executeQuery.cpp:3278-3281 recreates one outer-query StorageLimits object, but the original plan can carry different limits on different query nodes: nested subqueries keep their own SETTINGS in their QueryNode context (src/Analyzer/QueryTreeBuilder.cpp:313-325), and the planner turns each node's context into select_query_info.storage_limits (src/Planner/Planner.cpp:2612-2615). None of the read-step serializers preserve that list (for example ReadFromMergeTree::serialize writes filters/modifiers/bucket state but not query_info.storage_limits), so after the round-trip a worker fragment with a remote subquery like ... IN (SELECT ... SETTINGS max_bytes_to_read = ...) still loses the inner limit. Restoring one global list on every processor fixes the common top-level case, but not per-subquery limits.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕵 Agreed, this is not covered, and the PR description now says so. The StorageLimitsList isn't part of any step's serialized state, so after the round-trip only the query-level settings are left to derive it from. Keeping a subquery's own SETTINGS max_bytes_to_read* would mean serializing the per-node list with the read step: a new ReadFromMergeTree step version plus a plan serialization version bump, which changes the wire format. This PR closes the gap where no limits were enforced at all. It is not a regression, since before it the inner limit was lost as well. I am leaving this thread open for a maintainer to decide whether the per-node list should be serialized as a follow-up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕵 Opened #122724 for the per-subquery SETTINGS limits, and the PR description links it. This PR stays scoped to the query-level limits. The thread stays open for a maintainer to decide whether that scope is enough to merge.

@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Sep 14, 2026
@devcrafter
devcrafter requested a balanced review from Copilot September 14, 2026 13:23

Copilot AI 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.

Note

Copilot was unable to run its full agentic suite in this review.

Pull request overview

Fixes missing enforcement of storage byte/speed/timeout limits when executing reads from a query plan fragment shipped to another node (plan-based parallel replicas / make_distributed_plan), by deriving limits on the receiving side and attaching them to the deserialized plan’s sources/processors.

Changes:

  • Derive StorageLimits from settings/stage on the receiver and attach them to the deserialized pipeline processors.
  • Expose IInterpreterUnionOrSelectQuery::getStorageLimits for reuse outside the interpreter.
  • Add a stateless regression test verifying max_bytes_to_read/max_bytes_to_read_leaf errors on plan-shipped fragments.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
tests/queries/0_stateless/05216_storage_limits_for_shipped_plan.sql Adds regression test validating byte limits for plan-shipped reads.
tests/queries/0_stateless/05216_storage_limits_for_shipped_plan.reference Expected output for the new regression test.
src/Interpreters/executeQuery.cpp Derives storage limits on receiver and assigns them to processors in deserialized plan pipelines.
src/Interpreters/IInterpreterUnionOrSelectQuery.h Makes getStorageLimits publicly accessible for reuse.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Interpreters/executeQuery.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 69a0c31fd with master 0b1dbc7b4 (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

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

Job report

Comment thread src/Interpreters/executeQuery.cpp Resolved
@alexey-milovidov alexey-milovidov self-assigned this Sep 28, 2026
alexey-milovidov and others added 2 commits September 28, 2026 12:15
…lize`

The limits were attached to the processors of the built pipeline in the TCP
shipped-plan branch of `executeQuery` only, so the `make_distributed_plan` tasks
built by `DistributedPlanExecutor::doExecuteTask` still ran without them, and a
subquery read that index analysis builds an `IN` set from during
`readFromParts` ran before the loop, also without them.

Set `query_info.storage_limits` in `ReadFromMergeTree::deserialize` instead,
before `readFromParts`, with the free function `buildStorageLimits` - the same
way `resolveStorages` already does for `ReadFromTableStep`. That covers every
execution path of a shipped plan and only the storage reads, so exchange sources
and other processors are no longer given the limits, and
`IInterpreterUnionOrSelectQuery::getStorageLimits` stays protected.

The test gains `make_distributed_plan` cases and an `IN` subquery case; all five
error assertions fail without the change.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. and removed comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 72e3d00d-b312-4fc4-910c-65e4f2fd526a
  • MIRAI run: 90785d92-2dc3-4e4b-916b-141499f3cb11
  • PR check IDs:
    • clickbench_2321495_1790645052
    • clickbench_2321508_1790645052
    • clickbench_2321514_1790645052
    • tpch_adapted_1_official_2321521_1790645052
    • tpch_adapted_1_official_2321553_1790645052
    • tpch_adapted_1_official_2321568_1790645052

@devcrafter
devcrafter disabled auto-merge September 29, 2026 10:57
@devcrafter
devcrafter added this pull request to the merge queue Sep 29, 2026
Merged via the queue into master with commit d1d95d9 Sep 29, 2026
150 checks passed
@devcrafter
devcrafter deleted the enforce-storage-limits-for-shipped-plan branch September 29, 2026 13:20
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 29, 2026
alexey-milovidov pushed a commit to Onyx2406/ClickHouse that referenced this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-bugfix Pull request with bugfix, not backported by default 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.

Plan-based parallel replicas and make_distributed_plan: storage limits are not enforced for reads inside a shipped plan fragment

4 participants