Skip to content

Do not use parallel replicas when a replica would read a temporary table - #124987

Open
groeneai wants to merge 6 commits into
ClickHouse:masterfrom
groeneai:disable-parallel-replicas-temporary-table
Open

groeneai wants to merge 6 commits into
ClickHouse:masterfrom
groeneai:disable-parallel-replicas-temporary-table

Conversation

@groeneai

@groeneai groeneai commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #74229

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 queries that read a temporary table with an engine other than Memory (for example MergeTree) with parallel replicas enabled: they failed with ALL_CONNECTION_TRIES_FAILED or UNKNOWN_TABLE, or logged There is no table ... on server for every replica. Such queries now run without parallel replicas. Closes #74229.

Description

On 26.8.18.2 and 26.3.42.3, SELECT sum(a) FROM t for a temporary MergeTree table t with parallel replicas enabled (settings as in the new test) and parallel_replicas_local_plan = 0 fails with ALL_CONNECTION_TRIES_FAILED. With the local plan the result is right, but each replica logs There is no table ... on server, so 05233_constraint_graph_negated_ordered_comparison fails in the ParallelReplicas job when optimize_trivial_count_query = 0 is drawn (example, 7 PRs since 2026-10-04).

Only Memory temporary tables are sent to the replicas (RemoteQueryExecutor), yet a replica can be made to read another one: the table itself, a custom-key read, a JOIN or IN subquery the followers execute, a GLOBAL IN/GLOBAL JOIN materialization, or a temporary view unwrapped by parallel_replicas_allow_view_over_mergetree.

DisableParallelReplicasPass, which already does this for correlated subqueries, now disables parallel replicas for the whole query that reads such a table, also when only a subquery's own SETTINGS enables them. A view is no longer unwrapped onto such a table. Memory temporary tables and plan-based parallel replicas are not affected; the latter keep the read of a temporary table on the initiator since #129223. Not covered: joinGet on a temporary Join table, and the body of a view or view() that a follower executes.

Validation: each query of the new test reading a non-Memory temporary table fails on master and passes with the fix; the Memory control still plans a parallel-replicas read. 05233 with optimize_trivial_count_query = 0 goes from 20/20 FAIL to 20/20 OK in the ParallelReplicas job. Five tests blacklisted there for this bug pass again and leave parallel_replicas_blacklist.txt.

Related: #124934 (pins the 05233 query).


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

groeneai and others added 3 commits October 9, 2026 15:57
A session temporary table exists only on the server that created it, and
`RemoteQueryExecutor` sends only the `Memory` ones to the replicas as
external tables. Parallel replicas still planned reads that a replica had
to serve from such a table: the table read itself (05233 under
`optimize_trivial_count_query = 0`), the custom-key read, a JOIN or an
`IN` subquery executed by the followers, the materialization of a
`GLOBAL IN` / `GLOBAL JOIN` side, and a temporary view unwrapped by
`parallel_replicas_allow_view_over_mergetree`. With the local plan every
replica logged `There is no table ... on server` and the initiator read
alone; without it the query failed with `ALL_CONNECTION_TRIES_FAILED` or
`UNKNOWN_TABLE` (ClickHouse#74229).

`DisableParallelReplicasPass` already turns parallel replicas off for a
query shape they cannot serve (correlated subqueries). It now also does
so when the query reads a temporary table that is not `Memory` (a
temporary view only when it would be unwrapped). The rule is evaluated
for the task-based and the custom-key modes.

The five tests blacklisted for this bug in the ParallelReplicas
configuration pass again and are removed from the blacklist.

Closes ClickHouse#74229

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y SETTINGS or a view

A subquery with its own `SETTINGS` clause is planned with its own context, so
it could enable parallel replicas while the root query did not, and the
replicas were then asked to read a temporary table they never received. The
finder in `DisableParallelReplicasPass` now always runs and records whether any
query or union scope can use the task-based or custom-key mode.

With `parallel_replicas_allow_view_over_mergetree` the planner unwrapped a
view (a stored view or `view(...)`) whose body reads a temporary `MergeTree`
table, because the eligibility check had no temporary-table rule, and shipped
the view to replicas that cannot resolve its body. A temporary table is no
longer an eligible source there, so the view body is planned locally, where the
pass sees the temporary table.

05345 gains a line for each case and an `EXPLAIN` control proving a `Memory`
temporary table still gets a parallel-replicas read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the pass doc comment to one line and drop the clause of the finder's comment that restated what the code does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 9, 2026
@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; round 1: 3 findings, round 2 delta: 1 finding) plus my own cold review of both rounds.

# Sev Finding Verdict Evidence / action
1 ⚠️ The rule ran only when the outer query could use parallel replicas, so a subquery's own SETTINGS re-enabling them still read the temporary table (DisableParallelReplicasPass.cpp) AGREE, fixed @ 07b2b73 the finder runs on every query and fires when any scope can use parallel replicas; test line 8 fails on master and on the first revision
2 ⚠️ A view() or stored-view body unwrapped by parallel_replicas_allow_view_over_mergetree could hide a temporary MergeTree from the pass AGREE, fixed @ 07b2b73 isTableNodeEligibleForParallelReplicas rejects a temporary table, so such a view is not unwrapped; test line 9
3 ⚠️ The test did not show that the Memory exclusion keeps parallel replicas AGREE (control), DISAGREE (zero-usage oracle) Memory control line 10, proven live by deleting the exclusion (line turns 0); every non-Memory line already fails on master
4 ❌ parallel_replicas_mode = 'sampling_key' is not covered, so shards of a remote()/Distributed query still run an IN/JOIN subquery over the temporary table DISAGREE the local-or-remote choice per shard does not depend on that mode (SelectStreamFactory.cpp:198, parallel_replicas_enabled is task-based only, ClusterProxy/executeQuery.cpp:611), and a remote shard lacks the table with or without it; that is the non-GLOBAL IN semantics of distributed queries, so disabling the mode would change no result
5 💡 A stored view whose body reads a temporary view is still unwrapped under the BETA unwrap setting (StorageView.cpp:631) noted the follower then executes that view's body, which the description lists as not covered
6 💡 The finder now walks every analyzed query tree noted -1.6% QPS on a tiny IN query in a debug build, within noise
7 💡 Textual conflict with my open #115916 in the same pass noted whichever lands second keeps whole-query disabling and the custom-key gate (test line 5)

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261009-191100

@groeneai groeneai added the groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding label Oct 9, 2026
@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05233 with --client-option optimize_trivial_count_query=0 in the ParallelReplicas configuration: 20/20 FAIL on master. SELECT sum(a) FROM <temporary MergeTree table> SETTINGS enable_parallel_replicas = 1, parallel_replicas_local_plan = 0 throws ALL_CONNECTION_TRIES_FAILED every time (also on official 26.8.18.2 and 26.3.42.3).
b Root cause explained? Only Memory temporary tables are sent to the replicas (RemoteQueryExecutor), while parallel-replicas planning still makes a replica read other temporary tables, so each replica reports There is no table ... on server (forwarded to the client as a Warning) or the query fails.
c Fix matches root cause? Yes. DisableParallelReplicasPass disables parallel replicas for the whole query when it reads a non-Memory temporary table (a temporary view only when parallel_replicas_allow_view_over_mergetree unwraps it) and any scope of the query, including a subquery with its own SETTINGS, can use the task-based or custom-key mode. The planner no longer treats a temporary table as an eligible source when it unwraps a view. No test pin.
d Test intent preserved / new tests added? 05233 is unchanged. New 05345_parallel_replicas_temporary_table: 9 queries reading a non-Memory temporary table (table read with and without local plan, JOIN, IN subquery, custom key, GLOBAL IN, temporary view, subquery SETTINGS, view()), each failing on master on its own, plus a Memory control that must still plan a parallel-replicas read. Five tests blacklisted for this bug are re-enabled.
e Both directions demonstrated? Yes. 05233 (forced culprit): master 20/20 FAIL, fix 20/20 OK. 05345: master 5/5 FAIL, fix 5/5 OK; fix randomized 50/50 in the ParallelReplicas configuration and 50/50 in the default one. Removing the Memory exclusion makes the control line fail.
f Fix is general across code paths? Yes. One query-level rule covers the per-table read, the custom-key read, follower-executed JOIN/IN, GLOBAL IN/GLOBAL JOIN materialization, the unwrapped temporary view and a subquery whose own SETTINGS enables parallel replicas; the view-unwrapping eligibility check rejects a temporary source. Followers are unaffected (their temporary tables are the received Memory copies); the correlated-subquery rule is unchanged. 299 temporary-table, correlated and view-over-MergeTree tests: no new failure. Not covered: plan-based parallel replicas (experimental), joinGet on a temporary Join table, and a view body that a follower executes.
g Fix generalizes across inputs (params/datatypes/wrappers)? Engines: MergeTree, ReplacingMergeTree, Log, TinyLog, Set are counted; Memory keeps parallel replicas (ParallelReplicasUsedCount, and the test's EXPLAIN control). Both parallel_replicas_local_plan values, IN (subquery) and IN <table>, temporary view with the unwrap setting on and off, view() over a temporary table, parallel replicas enabled only inside a subquery.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change. Only queries that read a non-Memory temporary table stop using parallel replicas.
i Invariants and contracts preserved? The pass still only lowers allow_experimental_parallel_reading_from_replicas on query and union node contexts, and still returns early when the root cannot use parallel replicas and no temporary table was found. The finder is read-only and stops at the first match.

Session id: cron:clickhouse-impl-slot-5:20261009-170000

@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [67fffd0]

Summary: ✅


AI Review

Summary

This PR disables task-based and custom-key parallel replicas when an analyzed query reads a temporary table that cannot be sent to replicas, while preserving Memory temporary tables and plan-based execution. No actionable findings after reviewing the current diff, surrounding planning code, and all prior discussion. The head-commit Fast test report passes both 05345_parallel_replicas_temporary_table and 05497_pr_plan_based_temporary_table_read_stays_local.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Oct 9, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 24923e6

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05210_lightweight_update_materialized_cte_in_ignore flaky, not from this PR (90 of 90 reruns pass): Code: 60 ... Unknown table expression identifier '_materialized_cte_c_...' when a reused MATERIALIZED CTE is read through parallel replicas with a local plan. 45 PRs and 2 master runs show it in the last 3 days #123121 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, sequential) / 03254_system_prewarm_mark_cache flaky, not from this PR (107 of 107 reruns pass): a mark cache check reads 0 instead of 1 under parallel replicas. 6 PRs show it in the last 3 days #124942 (mine, open)
Mergeable Check, PR praktika rollups of the two rows above #123121, #124942 (mine, open)

Session id: cron:our-pr-ci-monitor:20261010-023009

groeneai and others added 2 commits November 24, 2026 13:56
…plicas-temporary-table

Resolve the conflict in `DisableParallelReplicasPass.cpp` with the dry run that
master added to `DisableParallelReplicasVisitor` for
`disablesParallelReplicasInWholeTree`: keep master's visitor and give it back
the initial latch from `FindTemporaryTableNotSentToReplicasVisitor`, renaming
`is_correlated` to `disable` as before, since the latch is no longer only about
a correlated subquery.

The dry run starts unlatched. `disablesParallelReplicasInWholeTree` may only
answer true when the pass turns parallel replicas off everywhere, so missing the
temporary-table case just builds the probe plan as on master. A query that
reads only a temporary table is already rejected by
`canQueryPossiblyUseParallelReplicas` through `isStorageEligibleForParallelReplicas`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since ClickHouse#129223 plan-based parallel replicas keep the read of a temporary table
on the initiator (`mergeTreeReadCanBeShipped`) and still distribute the other
subtrees of the query, e.g. an aggregation over a regular table joined with a
temporary table. Turning parallel replicas off for the whole query in
`DisableParallelReplicasPass` took that away and broke
`05497_pr_plan_based_temporary_table_read_stays_local` (10/10 runs). The
finder now fires only for task-based and custom-key parallel replicas, the
modes that send the query to the replicas as is.

`05345_parallel_replicas_temporary_table` covers the task-based mode, so it
sets `parallel_replicas_plan_based = 0` and keeps its result in the
plan-based CI configuration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master's .clang-tidy enables modernize-use-nodiscard, which failed
Build (arm_tidy, 3/4) on this const getter after the master merge.

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

clickhouse-gh Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 67fffd098 with master b83bf296e (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

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

Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +3.5 s (+12%) in total before that adjustment.

Job report

@devcrafter devcrafter self-assigned this Nov 24, 2026

This branch has not been deployed

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

Labels

can be tested Allows running workflows for external contributors comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't read from temporary table with enabled parallel replicas

2 participants