Skip to content

Fix parallel_view_processing setting being ignored for MV fan-out - #109170

Merged
antonio2368 merged 5 commits into
masterfrom
fix/parallel-view-processing
Jul 9, 2026
Merged

antonio2368 merged 5 commits into
masterfrom
fix/parallel-view-processing

Conversation

@antonio2368

@antonio2368 antonio2368 commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Closes: #106845

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 parallel_view_processing setting being ignored when inserting into a table with materialized views — since 25.10, views always ran in parallel regardless of the setting.

Documentation entry for user-facing changes

  • Documentation is not required

Version info

  • Merged into: 26.7.1.696 (included in 26.7 and later)
  • Backported to: 26.6.4.38, 26.3.29.8

antonio2368 and others added 2 commits June 30, 2026 11:40
Since 25.10 (#87280), `parallel_view_processing=0` had no effect — materialized
views always ran in parallel regardless of the setting. The root cause was that
`InsertDependenciesBuilder` builds a `CopyTransform` fan-out to all dependent MV
branches, and the pipeline executor thread ceiling was set to `max_threads`
unconditionally in three places:

- `InsertDependenciesBuilder::createChainWithDependencies`
- `InsertDependenciesBuilder::createChainWithDependenciesForAllStreams` (rebuilt
  squashing path)
- `InterpreterInsertQuery::buildInsertPipeline` (decisive overwrite for
  `INSERT ... VALUES` — the builder is called with `max_insert_threads=1` but
  the pipeline was widened back to `max_threads` before execution)

For `INSERT ... SELECT` the source side can raise `Pipe::max_parallel_streams`
above 1, so a chain-level fix alone is insufficient; `limitMaxThreads` is needed
after `setMaxThreads` in `addInsertToSelectPipeline`.

Fix: introduce `InsertDependenciesBuilder::getViewProcessingNumThreads()` which
returns `max_threads` when `parallel_view_processing=1` or no views are involved,
and 1 otherwise. Use it in all four call sites.

Closes: #106845

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

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [101a0c7]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_msan, 6/8) FAIL
test_storage_rabbitmq/test.py::test_rabbitmq_mv_combo FAIL cidb
Upgrade check (amd_release) FAIL
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb

AI Review

Summary

This PR caps the MV insert pipeline when parallel_view_processing=0 and rewrites the new regression test to use peak_threads_usage. I do not think it is ready to merge: the async materialized-view executor path still does not enforce sequential completion of sibling views, and the new helper broadens parallel_view_processing=0 into a single-thread throttle even when there is no view fan-out.

Missing context / blind spots
  • ⚠️ No local clickhouse binary/build directory was available in this workspace, so I could not run a direct repro. The blocker below is from code-path tracing; an explicit stateless test with use_async_executor_for_materialized_views = 1 would close that gap.
Findings

❌ Blockers

  • [src/Interpreters/InsertDependenciesBuilder.cpp:600-617, 746-757, 1337-1385, 1529-1568; src/Processors/Executors/PullingAsyncPipelineExecutor.cpp:100-143; tests/queries/0_stateless/04490_parallel_view_processing_values.sql:20-49] parallel_view_processing=0 is still ineffective when use_async_executor_for_materialized_views=1. The change only caps the outer insert pipeline to one worker, but createSelect still instantiates ExecutingInnerQueryFromViewTransform<PullingAsyncPipelineExecutor> for every child view. On the first pull(), each branch starts its own background executor thread, and onGenerate returns after the first produced chunk while keeping that inner executor alive. Since the MV query can emit many chunks from one inserted block, the single outer worker can advance to a sibling branch before the current view finishes, so sibling views still overlap in the supported async-executor mode. The new regression test never pins use_async_executor_for_materialized_views = 1, so it does not prove this path. Suggested fix: when parallel_view_processing=0, force the synchronous MV executor (or otherwise wait for full inner-query completion before another branch can start), and add an explicit async-executor variant to the test.

⚠️ Majors

  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/109170#discussion_r3519932465] [src/Interpreters/InsertDependenciesBuilder.cpp:1698-1709; src/Interpreters/InterpreterInsertQuery.cpp:534-537, 847-848; src/Interpreters/InsertDependenciesBuilder.h:164-165] The new getViewProcessingNumThreads gate is keyed off isViewsInvolved(), and that helper is true even for direct inserts into a materialized view whose only downstream object is its own inner table. So parallel_view_processing=0 now collapses INSERT INTO materialized_view ... to one thread even though there is no sibling fan-out to serialize. I still think this is a real regression because the current code broadens a fan-out fix into a general single-thread throttle for linear/single-view pipelines. Suggested fix: base the serial cap on actual view fan-out (multiple child branches / nested branching), not on isView(init_table_id) alone.
Final Verdict

Changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 2, 2026
InterpreterInsertQuery.cpp referenced Setting::parallel_view_processing
without declaring it in its local namespace Setting block, which fails
to compile since each translation unit must locally declare every
Setting:: tag it uses.
pipeline.setNumThreads(max_threads);
// max_threads is already memory-adjusted; use it for the parallel case to preserve that adjustment.
const bool serial_views = !settings[Setting::parallel_view_processing] && insert_dependencies->isViewsInvolved();
pipeline.setNumThreads(serial_views ? 1 : max_threads);

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.

parallel_view_processing = 0 is still ineffective when use_async_executor_for_materialized_views = 1. The new serial_views ? 1 : max_threads cap only throttles the outer insert pipeline, but createSelect can still build ExecutingInnerQueryFromViewTransform<PullingAsyncPipelineExecutor> for every view (InsertDependenciesBuilder.cpp:1382). On the first pull(), each branch starts its own ThreadFromGlobalPool (PullingAsyncPipelineExecutor.cpp:100-115), and ExceptionKeepingTransform leaves that processor in Generate without blocking the single outer worker. That means one outer thread can walk the other branches and start their async executors too, so the views still run concurrently despite the new cap.

clickhouse-test also randomizes use_async_executor_for_materialized_views, so the new regression test does not pin or cover this mode. To make the fix match the PR contract, serial view processing needs to force the synchronous MV executor (or otherwise serialize async-executor startup) and the test should exercise use_async_executor_for_materialized_views = 1.

auto & chain = result_chains.emplace_back(std::move(processor_list));
chain.attachResources(std::move(resources));
chain.setNumThreads(init_context->getSettingsRef()[Setting::max_threads]);
chain.setNumThreads(getViewProcessingNumThreads());

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.

Why we need this?
Why we need to limit threads on the pipeline for insertion?
parallel_view_processing=0 eliminated the multiple branches for pipeline graph.
But we could execute even single chained pipeline with several threads. I would rather remove this setting than give it more impact.

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.

I agree with you, but it seems we broke an expected behavior from this setting.
I guess we can just change it to true by default?

@CheSema

CheSema commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

But in general I'm OK with the changes.
Right now it helps to turn insert queries with MV in a really saving mode: use lesser memory and do not consume lot of cpu with cost of higher latency.

It indeed would be more clear if we now handle that setting in this way, and then do make memory accounting more adjustable without manual settings.

…processing test

The 200ms wall-clock duration threshold is unreliable under debug/ASan/MSan/TSan
builds, where interpreter overhead alone can push even the genuinely-parallel
case past the threshold (observed 5/5 failures in the flaky check for
amd_asan_ubsan, amd_debug, amd_msan, amd_tsan). `peak_threads_usage` is an exact
count of threads that attached to the query's thread group
(`ThreadGroup::linkThread`), not a sampled or timed value, so it distinguishes
serial from parallel view processing independent of build overhead.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109170&sha=dd1d1cd26c4645b68617a12718d9e11e6530177a&name_0=PR&name_1=Stateless%20tests%20(amd_asan_ubsan,%20flaky%20check)
Related: #109170
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.90% 85.80% -0.10%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

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

Full report · Diff report

@antonio2368
antonio2368 added this pull request to the merge queue Jul 9, 2026
Merged via the queue into master with commit 3178278 Jul 9, 2026
172 of 175 checks passed
@antonio2368
antonio2368 deleted the fix/parallel-view-processing branch July 9, 2026 06:25
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Fix parallel_view_processing setting being ignored for MV fan-out

ClickGap verified: reproduced it with the script below on 26.5; tested 14 version(s) — 12 affected, 2 clean (see Affects).

Reproduction

-- Reproduces on a server (needs system.query_log). Insert into a table with several
-- dependent MVs and compare peak_threads_usage for parallel_view_processing 0 vs 1.
CREATE TABLE pvp_source (n UInt64) ENGINE=MergeTree ORDER BY tuple();
CREATE TABLE pvp_target (n UInt64, mv UInt8, s UInt8) ENGINE=MergeTree ORDER BY tuple();
-- create 8 MVs that each do real per-row work so the fan-out actually spins up threads
CREATE MATERIALIZED VIEW pvp_mv1 TO pvp_target AS SELECT n, 1 AS mv, sleepEachRow(0.03) AS s FROM pvp_source;
-- ... repeat for pvp_mv2 .. pvp_mv8 ...

-- setting IGNORED == bug: both inserts report the SAME (max) peak_threads_usage
INSERT INTO pvp_source SETTINGS parallel_view_processing=0, max_threads=16, max_insert_threads=16,
    max_block_size=1, min_insert_block_size_rows=1, log_queries=1 SELECT number FROM numbers(16);
INSERT INTO pvp_source SETTINGS parallel_view_processing=1, max_threads=16, max_insert_threads=16,
    max_block_size=1, min_insert_block_size_rows=1, log_queries=1 SELECT number+100 FROM numbers(16);
SYSTEM FLUSH LOGS query_log;
SELECT Settings['parallel_view_processing'] AS pvp, peak_threads_usage
FROM system.query_log WHERE type='QueryFinish' AND query_kind='Insert'
  AND current_database=currentDatabase() ORDER BY event_time DESC LIMIT 2;
-- Observed on buggy build (<=26.5): pvp=0 -> 18 threads, pvp=1 -> 18 threads (setting ignored).
-- Expected/fixed:                    pvp=0 -> ~2 threads, pvp=1 -> 18 threads.

Bisect

Affects

  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.5, 26.4, 26.3, 26.2, 26.1, 25.12, 25.11, 25.10, 25.9, 25.8, 25.7, 25.6
  • Does NOT reproduce on: master, 25.5
  • Backport needed: 26.5, 26.4, 26.3, 26.2, 26.1, 25.12, 25.11, 25.10, 25.9, 25.8, 25.7, 25.6

Suggested next step

The fix has landed on master; the affected release branches above still need the backport.

Component: comp-materialized-view
Severity: P2

cc fix PR author: @antonio2368
cc fix PR reviewer: @CheSema


ClickGap · Finding: phase_d_pr109170

Triage metadata

  • Primary component: comp-materialized-view
  • Secondary components: comp-testing
  • Component owners (comp-materialized-view): @CheSema @al13n321

huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Jul 15, 2026
…ening the timeout

Since ClickHouse#109170 fixed parallel_view_processing being ignored, MV fan-out is
serialized by default (setting defaults to false). This genuinely slowed
down `RabbitMQ`'s internal insert-to-views pipeline for the 5 materialized
views in this test, which had been relying on the previously-buggy
always-parallel behavior. ClickHouse#109892 masked the slowdown by making the test's
no-progress deadline reset on progress instead of addressing the throughput
regression, which is reverted here.

Set `parallel_view_processing = 1` in the test's default profile, which is
the only place this setting can be applied for RabbitMQ's internal insert
context (it can't be set via `CREATE TABLE ... SETTINGS` or session `SET`,
since `StorageRabbitMQ` captures the global context at construction).
Verified with the CI msan binary: 188s (serial, default) vs 119s (parallel)
for the underlying consumption; combined with the revert, the test now
passes in ~124s against the original fixed 180s deadline.
@CurtizJ

CurtizJ commented Aug 29, 2026

Copy link
Copy Markdown
Member

@antonio2368 @CheSema

Can we backport it? parallel_view_processing breaks some use cases with materialized views with joins.

@CurtizJ CurtizJ added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 29, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 29, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 29, 2026
Backport #109170 to 26.6: Fix parallel_view_processing setting being ignored for MV fan-out
robot-clickhouse added a commit that referenced this pull request Sep 1, 2026
Cherry pick #109170 to 26.3: Fix parallel_view_processing setting being ignored for MV fan-out
robot-clickhouse added a commit that referenced this pull request Sep 1, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 1, 2026
antonio2368 added a commit that referenced this pull request Sep 2, 2026
Backport #109170 to 26.3: Fix parallel_view_processing setting being ignored for MV fan-out
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…processing test

The 200ms wall-clock duration threshold is unreliable under debug/ASan/MSan/TSan
builds, where interpreter overhead alone can push even the genuinely-parallel
case past the threshold (observed 5/5 failures in the flaky check for
amd_asan_ubsan, amd_debug, amd_msan, amd_tsan). `peak_threads_usage` is an exact
count of threads that attached to the query's thread group
(`ThreadGroup::linkThread`), not a sampled or timed value, so it distinguishes
serial from parallel view processing independent of build overhead.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109170&sha=dd1d1cd26c4645b68617a12718d9e11e6530177a&name_0=PR&name_1=Stateless%20tests%20(amd_asan_ubsan,%20flaky%20check)
Related: ClickHouse#109170
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…w-processing

Fix parallel_view_processing setting being ignored for MV fan-out
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ening the timeout

Since ClickHouse#109170 fixed parallel_view_processing being ignored, MV fan-out is
serialized by default (setting defaults to false). This genuinely slowed
down `RabbitMQ`'s internal insert-to-views pipeline for the 5 materialized
views in this test, which had been relying on the previously-buggy
always-parallel behavior. ClickHouse#109892 masked the slowdown by making the test's
no-progress deadline reset on progress instead of addressing the throughput
regression, which is reverted here.

Set `parallel_view_processing = 1` in the test's default profile, which is
the only place this setting can be applied for RabbitMQ's internal insert
context (it can't be set via `CREATE TABLE ... SETTINGS` or session `SET`,
since `StorageRabbitMQ` captures the global context at construction).
Verified with the CI msan binary: 188s (serial, default) vs 119s (parallel)
for the underlying consumption; combined with the revert, the test now
passes in ~124s against the original fixed 180s deadline.
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
OTHER: parallel_view_processing=0 ignored -> behaviour.
Effect in the production build a394e92: its upstream test(s) 04490_parallel_view_processing_values fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv).
Upstream patch unchanged, with its test(s): 04490_parallel_view_processing_values.

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

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

parallel_view_processing behaviour change after 25.5 is not reflected in docs

7 participants