Skip to content

Skip rebuilding the automatic parallel replicas probe plan when its last build was undecided - #130565

Merged
nickitat merged 19 commits into
masterfrom
negative-cases
Nov 21, 2026
Merged

nickitat merged 19 commits into
masterfrom
negative-cases

Conversation

@nickitat

@nickitat nickitat commented Nov 10, 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):

Automatic parallel replicas (automatic_parallel_replicas_mode = 1) no longer builds the plan with parallel replicas on every execution of a query whose last build ended without a decision: the execution after the first such build builds it again at once, and from then on one in every automatic_parallel_replicas_undecided_retry_after_executions + 1 executions (default 10) does.


Related: #129544

Automatic parallel replicas decides by building the plan with parallel replicas (the probe) and matching it against the single-node plan. When that ends without a decision, nothing was remembered, so every later execution of the query built the probe again, index analysis included, and threw it away. The outcomes that leave no decision are:

  • the plans are not comparable (AutoParallelReplicasPlansNotComparable);
  • the probe has no parallel-replicas part (AutoParallelReplicasPlanWithoutParallelReplicas);
  • a step cannot collect statistics;
  • the collection measured nothing, for example because an inner join with an empty build side never read its probe side, which holds the coordinated read.

On short queries this cost is the result. On TPC-H sf=1 (staging, 3 replicas) automatic mode was 5.5% slower than one node, all of it from ten such queries at +4–28 ms per run. A replay of 112 join queries built the probe on every warm run: median 1.08x, up to 148 ms of build.

Approach

The outcome is remembered under the same key as the boundary: the single-node plan's root hash plus the boundary settings. It is a bounded negative cache, not a verdict:

  • Retry by turns. Executions take turns on the entry. One in every automatic_parallel_replicas_undecided_retry_after_executions + 1 (default 10) builds the probe again. The others skip it and count AutoParallelReplicasRetryDeferred beside the remembered outcome's event. Concurrent executions cannot all rebuild at once, and a hit never extends the wait. 0 turns it off.
  • One immediate retry. After the first undecided outcome for a key, the next execution builds the probe again at once, and from the second outcome on the regular quota applies. This covers the cold-to-warm case without looking at the statistics. A probe read that cannot be paired with a single-node read, such as one of two identical subqueries over a table, takes no row estimates from the single-node analysis (Estimate the join inputs of the automatic parallel replicas probe from the single-node analysis #130516), so its joins can be ordered differently. The first execution measures the hash tables that join reordering takes row counts from, and the build after it orders both plans alike. Statistics that change later are noticed on the next turn.
  • Collections without a result. A step that cannot collect and a collection that measured zero input and zero output bytes are both remembered as undecided. The zeros themselves are not stored as statistics: the statistics key ignores the values of string constants, so a query that differs only in such a constant and does read data would decide "one node" from them. A cancelled run that measured nothing only defers the probe, it never decides.
  • Decisions clear it. A decline or a switch decided from statistics, and a successful collection, forget the entry.
  • Independent of remembered boundaries. automatic_parallel_replicas_remember_boundary = 0 no longer makes every execution build the probe on its own: undecided plans are still skipped. To benchmark the cost of the probe, also set automatic_parallel_replicas_undecided_retry_after_executions = 0.
  • Cheap checks still run every time. The settings and query-shape gates, the size gate, and the decline from a remembered boundary are all checked on every execution, before the cache is consulted.

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

Version info

  • Merged into: 26.12.1.131-master (included in 26.12 and later)
  • Backported to: 26.11.1.2920, 26.10.10.3

…decided

When automatic parallel replicas built the plan with parallel replicas and
the outcome left no decision (`AutoParallelReplicasPlansNotComparable`,
`AutoParallelReplicasPlanWithoutParallelReplicas`, or a step that cannot
collect statistics), nothing was remembered, so every later execution
built that plan again, index analysis included, and threw it away. On
short queries this cost became the result: on TPC-H sf=1 automatic mode
was 5.5% slower than one node, all of it from ten such queries.

Remember the outcome under the same key as the boundary (the single-node
plan's root hash and the boundary settings). It is not a verdict:
- Executions take turns on the entry: one in every
  `automatic_parallel_replicas_undecided_retry_after_executions + 1`
  (default 10) builds the plan again, so concurrent executions never all
  rebuild at once. 0 turns it off.
- What join reordering read from `HashTablesStatistics<HashJoinEntry>`
  (`RuntimeHashStatisticsContext::getCachedHint`), in both plans, misses
  included, is kept with the entry. The plan is built again at once when one
  of them appears, disappears or changes by more than twice; a smaller change
  waits for the next turn, since a growing table replaces its stored size on
  every run.
- A `CollectorUnsupported` entry keeps the statistics entry it saw when its
  collection started, and no longer holds once a later collection has
  replaced it, whatever order the two finished in.

A skipped execution counts `AutoParallelReplicasRetryDeferred` beside the
event of the remembered outcome.

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

clickhouse-gh Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6ab11e5]

Summary: ✅


AI Review

Summary

The PR adds a bounded cache for automatic parallel-replica planning attempts that end without a decision, with one immediate retry followed by configurable execution-based retries. The earlier bot findings and the reported warm-cache test regression are addressed in the current code. No outstanding actionable findings. Using the CI binary for head 6ab11e5bbd6ff642081261862b603f0027b1da64, both 05617_autopr_undecided_retry and 05628_autopr_transplant_repeated_table_occurrences passed locally with exact reference matches; the latest Fast test report also passes the retry regression.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 10, 2026
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Nov 10, 2026
nickitat and others added 6 commits November 10, 2026 18:16
…tatistics

Keeping a snapshot of what join reordering read, and comparing it with a
tolerance of our own, was a lot of machinery for one case: the first
execution of a join measures the hash tables that join reordering takes row
counts from, so the build after it often orders both plans alike. Cover that
case directly: after the first undecided outcome for a key, the turn of the
next execution comes at once, and from the second outcome on the regular
quota applies. Later changes of the statistics wait for the next turn, as
changes within the tolerance did before.

This removes the reads recorded in `getCachedHint`, their plumbing through
the optimization settings and the plan with parallel replicas, the snapshot
in the entry, and `HashTablesStatistics::findEntry`.

#130565

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With `automatic_parallel_replicas_undecided_retry_after_executions` at its
largest value the period cannot have one added, and the offset that gave the
first execution after an entry's first outcome its turn wrapped into a skip:
the immediate retry never came. Admit that execution explicitly, and count
the turns from the one after it.

The descriptions of `automatic_parallel_replicas_remember_boundary` and of the
new setting both promised that turning one of them off builds the plan with
parallel replicas on every execution. The two caches are independent, so a
benchmark of that cost has to turn off both.

#130565

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An undecided entry can be recorded after another execution of the same plan
matched and collected statistics, when the two raced. Executions that decline
at the remembered boundary never read the entry, but one whose statistics
favour parallel replicas needs the replicas plan, and the stale entry made it
skip building it. Such statistics are evidence that the plans match now, so
the entry is forgotten and the plan is built.

That covers what the snapshot of the statistics in a `CollectorUnsupported`
entry was for, apart from a failed and a successful collection finishing at the
same moment, after which a drifted boundary is recollected at the next turn.
Remove the snapshot, its check, and the unit tests of that race.

#130565

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Statistics that favour parallel replicas do not prove the plan with them can
be adopted: when adopting it fails after the cost model, for example when a set
of the plan has no counterpart (`moveSetsFromLocalPlanToReplicasPlan`), the
entry is recorded with exactly those statistics in place, and the previous
commit deleted it on every execution, which built the plan every time. Remove
that override. The race it was for, an outcome recorded just after another
execution matched, only defers the switch until the next turn.

The test now covers that case, after
`05401_autopr_set_transplant_unpaired_sets`: once the immediate retry has
failed to adopt the plan as well, the next executions skip it.

#130565

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The ordinary join that keeps receiving inserts tested the tolerance of the
removed join-statistics tracking, and repeated the cycle the `LIMIT` case
already covers; the fifth execution of the adoption case repeated the fourth.
Remove both. Turning the deferral off needs a third execution to tell it from
the immediate retry, which builds the second one either way. The two unit
tests of a failed collection are one sequence: remembered, then forgotten by a
collection that succeeds.

#130565

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Nothing branches on the reason any more: a skipped execution only counts the
event its outcome was counted in. Store that event, and remove the enum and
the function that mapped it.

#130565

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

clickhouse-gh Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6ab11e5bb with master 4aa16c68a (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.

Object file sizes

52 object files changed (+80.92 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/RuntimeDataflowStatistics.cpp.o 444.36 KiB 513.18 KiB +68.83 KiB (+15.49%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

When a collection of runtime statistics measured zero input and zero output bytes, `RuntimeDataflowStatisticsBlock`
dropped it and recorded nothing else, so every later execution found no statistics and built the plan with parallel
replicas again. This happens, for example, when an inner join has an empty build side and never reads its probe side,
which holds the coordinated read (TPC-DS q46-like queries filtered to an empty dimension: a 11-21 ms build on every
run of a 22-167 ms query).

The zeros are still not stored as statistics: the statistics key ignores the values of string constants, so a query
that differs only in such a constant and does read data would decide from them. Instead the plan is marked undecided,
like a collection a step cannot take part in, so it is deferred by
`automatic_parallel_replicas_undecided_retry_after_executions` and keeps collecting on its turns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nickitat and others added 6 commits November 12, 2026 15:51
The four early returns of `decideAtBoundary` (the boundary is the read itself, no index analysis, a read served from a
projection, no selected rows) counted `AutoParallelReplicasPlansNotComparable` but remembered nothing. With
`automatic_parallel_replicas_remember_boundary = 0` they are found only after building the plan with parallel
replicas, so such a plan was built on every execution, contrary to the promised independence of the two settings.
They now record the plan as undecided when the plan with parallel replicas was built for this execution. From a
remembered boundary the same outcome is reached without building it, so nothing needs remembering there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Undecided` -> `UndecidedQuery`, `UndecidedCache` -> `UndecidedQueryCache`, `undecided_cache` -> `undecided_query_cache`,
`undecided_key` -> `undecided_query_key`. Also assert that an outcome recorded after building the plan with parallel
replicas has that plan.

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

`LIMIT` without `ORDER BY` makes the plans incomparable only because of a bug that is to be fixed, and a correlated
subquery is to be ruled out before building the plan with parallel replicas
(#129544). Either change would have broken the cases built on them. An
inner join with an empty build side, whose collection measures nothing, is deferred by design, so the turn-taking,
the disabled setting and the largest value are checked with it instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	src/Common/ProfileEvents.cpp
#	src/Processors/QueryPlan/Optimizations/considerEnablingParallelReplicas.cpp
A read that selects no rows has nothing to decide; `AutoParallelReplicasPlansNotComparable` is only the event it is
counted in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since the merge with master, a plan that builds a set is rejected while matching the two plans ("The plan builds a set,
its root must be preserved"), before statistics are collected, so the case no longer reaches the cost model and
checks nothing the other not-comparable cases do not. The random settings limits were only for its table.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nickitat
nickitat marked this pull request as ready for review November 17, 2026 19:56
nickitat and others added 2 commits November 17, 2026 20:10
With the adoption case of `05617` gone, nothing checked that statistics which favour parallel replicas do not make
an execution build the plan again after it failed to be adopted. The ambiguous reads of `05628` are declined after
the cost model favoured replicas, so two more runs of that shape cover it: the immediate retry builds the plan and
declines the same way, and the next run skips the build.

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

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I built this PR merged into current master (5ac4d89f, debug) and ran it against a local server with the 3-replica localhost cluster.

05617_autopr_undecided_retry fails on current master, 3 of 3 runs, only in the undecided_warm rows:

-undecided_warm_1	1	0	0	1	0	0
-undecided_warm_2	1	0	0	0	1	0
+undecided_warm_1	1	0	0	0	1	0
+undecided_warm_2	0	0	0	0	0	1

The first run already matches the two plans and collects statistics, and the second decides from them without building. With #130516 reverted locally on the same tree, the test passes 3 of 3. That PR merged on 2026-11-19, after this PR's CI ran on 2026-11-17: the probe now takes its join estimates from the single-node analysis, so the "no row estimates for the filtered read" premise of this case no longer holds. The immediate retry is still exercised by the undecided_empty_* and undecided_no_rows_* rows and by 05628, but this was its only cold-to-warm example. Does the immediate retry still have a case of its own after #130516? If not, UndecidedQuery::first and the execution - 1 arithmetic could go.

Minor: master is at 26.12 now (it was 26.11 at this branch's merge base), and the new setting's history row still says 26.11.

Checked, no issue found:

  • RuntimeDataflowStatisticsUndecided.FailedCollectionIsRememberedUntilOneSucceeds passes, and 05628 passes 3 of 3.
  • Turn-taking: 120 executions of the empty-build-side query with automatic_parallel_replicas_undecided_retry_after_executions = 3 built the probe 31 times one at a time (the first build, the immediate retry, then one in four) and 21 times at concurrency 8. So concurrent executions do not all rebuild.
  • EXPLAIN of a joined query builds no probe and leaves no entry: the SELECT after two EXPLAINs builds and collects like a fresh key.

nickitat and others added 3 commits November 20, 2026 13:31
… in the `26.12` history

Master is at `26.12` now.

Related: #130565 (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since #130516 the probe takes its join estimates from the single-node analysis, so the old three-table join already
matches on its first run. Identical subqueries reading the same table cannot be paired with the single-node reads,
so they still have no estimates on a cold run, and the immediate retry is what lets the second run match.

Related: #130565 (review)
Related: #130516

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Common/ProfileEvents.cpp Resolved
@stresshouse

stresshouse Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

📊 Cloud Performance Report

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

No queries show a performance change caused by this PR. The only flagged query, clickbench Q10, is +20.7% slower (111 ms against 92 ms). That is still inside master's current range of 83–115 ms, and this query's timings have been noisy on master. The PR only changes how automatic parallel replicas decides whether to rebuild its parallel-replicas probe plan after an undecided outcome. At most that skips work, so it cannot plausibly slow down a single-table GROUP BY with COUNT(DISTINCT), and the decision to suppress this row stands.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 10 not_sure 92 111 +20.7% <0.0001 aggregation: The +20.7% shift, 111 ms against 92 ms, stays inside master's current range of 83–115 ms on a query with a noisy history. This PR changes no code that would make this GROUP BY with COUNT(DISTINCT) slower.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: f9f972c2-e113-4e8e-8397-c54f8e7d3f0b
  • MIRAI run: cc78141e-edbf-4851-9b7f-c32d391b5e15
  • PR check IDs:
    • clickbench_686866_1795283705
    • clickbench_686872_1795283704
    • tpch_adapted_1_official_686910_1795283705
    • tpch_adapted_1_official_686931_1795283705

@nickitat
nickitat enabled auto-merge November 21, 2026 15:33
@nickitat
nickitat added this pull request to the merge queue Nov 21, 2026
Merged via the queue into master with commit 6886ced Nov 21, 2026
383 of 385 checks passed
@nickitat
nickitat deleted the negative-cases branch November 21, 2026 17:00
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 21, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 21, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 21, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 21, 2026
Backport #130565 to 26.11: Skip rebuilding the automatic parallel replicas probe plan when its last build was undecided
clickhouse-gh Bot added a commit that referenced this pull request Nov 21, 2026
Backport #130565 to 26.10: Skip rebuilding the automatic parallel replicas probe plan when its last build was undecided
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.

6 participants