Skip to content

Ignore parallel_replicas_min_number_of_rows_per_replica when automatic parallel replicas builds its plan - #126815

Merged
nickitat merged 5 commits into
masterfrom
probe-index-analysis
Oct 22, 2026
Merged

nickitat merged 5 commits into
masterfrom
probe-index-analysis

Conversation

@nickitat

@nickitat nickitat commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

parallel_replicas_min_number_of_rows_per_replica is ignored while automatic_parallel_replicas_mode builds its plan with parallel replicas, including in subqueries and views. Previously the row estimate for it ran a full extra index analysis on every such build, and its threshold could keep that plan from using replicas.


To decide whether to use parallel replicas, automatic_parallel_replicas_mode builds a plan with parallel replicas and costs it against the single-node plan. That build turns the automatic mode off in its own context, so with parallel_replicas_min_number_of_rows_per_replica > 0 the planner ran its row estimate there: a full primary-key and skip-index analysis of the read, repeated on every run that builds the plan. On large primary indexes this took seconds per query. The automatic mode decides with its own cost model, so the estimate added nothing, and when its threshold was not met it also disabled parallel replicas in the plan being costed (AutoParallelReplicasPlanWithoutParallelReplicas).

The build is now marked with a context field, is_automatic_parallel_replicas_probe, like join_analyze_mode. Every context copied from it inherits it, including the contexts of views analyzed later by their own interpreters, the view table function, and the context getSQLSecurityOverriddenContext builds from the global one. The planner skips the estimate, and the filter collection for it, in marked contexts. A settings override would not hold here: a subquery's or a view body's own SETTINGS clause re-applies the threshold, and those clauses cannot be removed because they are part of the tree hash that pairs nodes with the single-node plan. The main planning already skips the estimate while the automatic mode is on.

Local repro (two-table join with LIMIT and no ORDER BY, 937K marks, threshold 50M, automatic mode on): 5 → 4 index analysis rounds, plan build 184 → 5 ms, query 398 → 228 ms. These now match the same query without the threshold.

05399_autopr_plan_without_parallel_replicas used the min-rows threshold as its planner-side rejection. It now uses parallel_replicas_allow_in_with_subquery = 0 and keeps its original counter values.

Stacked on #126792 (the fix for max_partitions_to_read with read-time skip indexes); this PR targets its branch qcc-pruning and should be merged after it, then retargeted to master. Today the estimate run during this build is what fills the query condition cache with skip-index results, so queries on tables with max_partitions_to_read that rely on a skip index to stay under the limit recover after their first cold-cache run. Without the estimate, they would fail with TOO_MANY_PARTITIONS on every run unless skip indexes are applied during index analysis for such tables, which #126792 does.

Checked on the combined branches with the #126792 repro (31 partitions, max_partitions_to_read = 10, bloom-filter skip index, threshold 50M): with automatic mode 1 and 2, table-level and query-level max_partitions_to_read, every query passes from the first cold-cache run on.

Related: #126792


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

Version info

  • Merged into: 26.11.1.494-master (included in 26.11 and later)
  • Backported to: 26.10.2.74

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2c17568]

Summary: ✅


AI Review

Summary

This PR removes redundant index analysis for parallel_replicas_min_number_of_rows_per_replica while building automatic parallel-replica candidate plans, including nested queries and views. The previously reported max_partitions_to_read prerequisite is present in the current code. No unresolved findings remain; the reported before/after measurements and focused regression coverage support the change.

Missing context / blind spots
  • ⚠️ The latest-head CI run is still running, with the focused stateless checks pending. The unchanged 05407_autopr_probe_ignores_min_rows_per_replica test passed 50 repetitions each under TSan and ASan/UBSan on the preceding head. Completed latest-head checks would close this validation gap; tests were not rerun locally.
Final Verdict

Status: ✅ Approve. No code changes requested. Latest-head CI remains pending.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 19, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Oct 19, 2026
nickitat added a commit that referenced this pull request Oct 20, 2026
…plica`

In mode 1, `automatic_parallel_replicas_min_bytes_per_replica` skips small reads before the plan with parallel
replicas is built. With randomized MergeTree settings the test's reads fell under the gate in 1 of 50 runs of
the TSan flaky check, so the mode-1 queries built no such plan. Set the gate to 0, as the sibling tests do,
and count the build with `AutomaticParallelReplicasProbePlansBuilt`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126815&sha=13945e069464204e772d096ba7f8a9f4710939af&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20flaky%20check%29
PR: #126815

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nickitat
nickitat changed the base branch from master to qcc-pruning October 20, 2026 12:52
@nickitat
nickitat added this pull request to stack #126950 October 20, 2026 12:58
@nickitat
nickitat marked this pull request as ready for review October 20, 2026 14:48
@nickitat
nickitat requested a review from devcrafter October 20, 2026 15:22
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2c175684b with master 1b29cdec0 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e635548d5; 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

1323 translation units recompiled, 8530 s compile time in total, 1322 of them have a recent master baseline.

Translation units without a recent master baseline:

  • programs/keeper/Keeper.cpp (clickhouse-keeper): 7.3 s

Job report

Base automatically changed from qcc-pruning to master October 21, 2026 17:02
nickitat and others added 5 commits October 21, 2026 19:02
…matic mode's plan build

To decide, `automatic_parallel_replicas_mode` builds a plan with parallel replicas. That build turns the automatic
mode off in its own context, so with `parallel_replicas_min_number_of_rows_per_replica > 0` the planner ran the
row estimate there: a full primary-key and skip-index analysis of the read, on every run that builds the plan.
The automatic mode decides with its own cost model, so the estimate adds nothing. In a replay it cost up to 29 s
per query (p99 1.4 s).

Zero the setting in the build's context, and drop it from the query's top-level `SETTINGS` clause, which the
nested interpreter would otherwise re-apply. The main planning already skips the estimate while the automatic
mode is on.

Local repro (two-table join, 937K marks): with min rows 50M and the automatic mode, 5 -> 4 index analysis rounds,
probe build 184 -> 5 ms, query 398 -> 228 ms, now the same as without the setting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…obe's nested query contexts

Zeroing the setting only in the root context missed a subquery's own `SETTINGS` clause: the planner reads the
setting from each query node's context, where that clause is applied. With
`(SELECT * FROM sales SETTINGS parallel_replicas_min_number_of_rows_per_replica = 50000000)` as a join input,
the probe build still ran the estimate (one more index analysis round) and the threshold kept the candidate
local (`AutoParallelReplicasPlanWithoutParallelReplicas` 0 -> 1).

Walk the probe's query tree before planning and zero the setting in every query and union node's context. The
clauses stay in the tree, since they are part of the hash that pairs nodes with the single-node plan. The root
override stays too: the outer planner collects filters for the estimate as soon as it is constructed.

`05399_autopr_plan_without_parallel_replicas` used the min-rows threshold as its planner-side rejection, which
no longer happens in the probe build. Use `parallel_replicas_allow_in_with_subquery = 0` instead, which keeps
the original counter values.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…iding the setting

Overriding `parallel_replicas_min_number_of_rows_per_replica` in the query tree's contexts still missed views
analyzed later by their own interpreter (`analyzer_inline_views = 0`, `StorageView::read`) and the `view` table
function: their body's `SETTINGS` clause restores the threshold after the override. With the threshold in such
a body, the probe build ran one more index analysis round and the threshold kept the candidate local
(`AutoParallelReplicasPlanWithoutParallelReplicas` 0 -> 1).

No setting can hold against a later `SETTINGS` clause, so mark the probe build with a context field instead,
like `join_analyze_mode`: every context copied from the probe's inherits it, including the one
`getSQLSecurityOverriddenContext` builds from the global context. The planner skips the estimate, and the
filter collection for it, in marked contexts. The stored `SETTINGS` clauses are untouched, so tree hashes do
not change. This replaces the root override and the tree walk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…plica`

In mode 1, `automatic_parallel_replicas_min_bytes_per_replica` skips small reads before the plan with parallel
replicas is built. With randomized MergeTree settings the test's reads fell under the gate in 1 of 50 runs of
the TSan flaky check, so the mode-1 queries built no such plan. Set the gate to 0, as the sibling tests do,
and count the build with `AutomaticParallelReplicasProbePlansBuilt`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126815&sha=13945e069464204e772d096ba7f8a9f4710939af&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20flaky%20check%29
PR: #126815

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…robe_ignores_min_rows_per_replica`

Mode 1 builds the plan with parallel replicas on every run only while it cannot match it with the single-node plan,
which is the case today for `LIMIT` without `ORDER BY` only because `tryPushDownLimit` adds a `Limit` to each
branch of the `Union`. Once the plans match, the first run remembers the boundary and later runs of the same plan
decide without building it, so the threshold queries would stop exercising the fix. Mode 2 builds it on every run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nickitat
nickitat force-pushed the probe-index-analysis branch from 2eed64b to 2c17568 Compare October 21, 2026 17:03
@nickitat
nickitat added this pull request to the merge queue Oct 21, 2026
Merged via the queue into master with commit 61c7ebe Oct 22, 2026
179 checks passed
@nickitat
nickitat deleted the probe-index-analysis branch October 22, 2026 02:03
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 22, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126815 to 26.10: Ignore `parallel_replicas_min_number_of_rows_per_replica` when automatic parallel replicas builds its plan
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 22, 2026
… estimate

`indexHint` keeps its arguments in a DAG of its own (`FunctionIndexHint::getActions`),
so `dagContainsNonReadySet` scanning only the outer nodes missed an unbuilt `IN` set
there. Index analysis still descends into that DAG, so the estimate built a throwaway
copy of the set: `WHERE indexHint(k GLOBAL IN (SELECT n FROM input('n UInt64')))` with
`parallel_replicas_min_number_of_rows_per_replica = 1` still failed with
`INVALID_USAGE_OF_INPUT`, and `indexHint(k IN (SELECT ...))` ran its subquery twice.

Collect the filter's sets with `appendSetsFromActionsDAG`, which already recurses into
(nested) hints, and decline when any of them is not built. `dagContainsNonReadySet` is
left as is: its other callers evaluate the filter, and `indexHint` evaluates to 1
without its arguments.

The test gains an `indexHint` case. It bounds the inserted rows, because the hint passes
every row of the granules it selects, so only that bound shows the read still pruned by
the set. The automatic-mode case is dropped: since ClickHouse#126815 the automatic mode's probe
plan never runs the estimate, so the case passed with this PR's change reverted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants