Skip to content

Parallelize ordered merge output materialization - #117123

Open
bobrik wants to merge 1 commit into
ClickHouse:masterfrom
bobrik:ivan/parallel-materialize
Open

bobrik wants to merge 1 commit into
ClickHouse:masterfrom
bobrik:ivan/parallel-materialize

Conversation

@bobrik

@bobrik bobrik commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Ordered reads must merge input streams serially, but constructing the output columns does not determine row order. For wide results, materialization can dominate the single merge thread.

This adds a deferred mode to MergedData that records source ranges instead of copying their columns immediately. Sequence numbers are assigned to completed plans, then they are materialized on a bounded set of workers and restored to their original order before passing the chunks downstream.

Consecutive completed chunks are released without waiting for every worker. Each worker can buffer one additional block to reduce head-of-line blocking while keeping memory usage bounded.

The new max_parallel_ordered_merge_materialization_threads setting controls the number of workers. Values of 0 (the default) and 1 disable the optimization.

In the performance test with 8 input streams and 1024-row blocks, 4 workers reduced median time from 0.475s to 0.165s, a 2.89x speedup.

On the production workload with matched settings, 8 workers reduced wall time from 178s to 69.2s, a 2.57x speedup. CPU increased from 312s to 346s and peak memory from 13.6 GiB to 15.2 GiB.

Changelog category (leave one):

  • Performance Improvement

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

Parallelize ordered merge output materialization (opt-in via max_parallel_ordered_merge_materialization_threads).


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

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Aug 30, 2026
@clickhouse-gh clickhouse-gh Bot closed this Aug 30, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Aug 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9481e21]

Summary: ✅


AI Review

Summary

This PR defers column materialization out of ordered merges and replays it on worker threads for MergingSorted / FinishSorting, sorted GatherReceive, and preliminary in-order read merges. The execution model looks coherent, but the query-plan version bump currently widens the parallel-replicas minimum version and drops older replicas during rolling upgrades, so I do not think this is ready to merge yet.

Findings

⚠️ Majors

  • [src/Core/ProtocolDefines.h:131] The new setting is correctly omitted from plan settings below serialization version 21, but parallel replicas still pre-serialize one shared remote-plan blob at DBMS_QUERY_PLAN_SERIALIZATION_VERSION and require every replica to advertise that same version. After this bump, v20 replicas are excluded from parallel-replica reads even when max_parallel_ordered_merge_materialization_threads stays at 0 and the plan itself could still be serialized compatibly for them. That is a mixed-version rollout regression for an otherwise opt-in local optimization.
    Suggested fix: keep DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_PARALLEL_REPLICAS on the last always-required layout version, or cache the parallel-replicas remote plan at the minimum serialization version advertised by the selected replicas.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: restore mixed-version parallel-replicas compatibility for replicas that only support query-plan serialization version 20 when this new setting is not in use.

LLVM Coverage Report

Measured on commit 9481e21.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 392/447 (87.70%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9481e21ad with master 253f06071 (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

114 object files changed (+374.83 KiB total), 2 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Merges/Algorithms/MergedData.cpp.o 64.17 KiB 223.93 KiB +159.76 KiB (+248.97%)
src/CMakeFiles/dbms.dir/Processors/Merges/MaterializeMergedDataTransform.cpp.o new 90.87 KiB +90.87 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/ParallelMergingSortedMaterialization.cpp.o new 47.38 KiB +47.38 KiB

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

803 translation units recompiled, 6279 s compile time in total, 797 of them have a recent master baseline.

Symbol sizes
Binary Symbol Master PR Δ
programs/clickhouse ProfileEvents::global_counters_storage 12.62 MiB 12.69 MiB +64.00 KiB (+0.50%)
programs/clickhouse-keeper ProfileEvents::global_counters_storage 12.62 MiB 12.69 MiB +64.00 KiB (+0.50%)

Job report

@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 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: 872e4a5f-ce31-474c-97cb-b17e3a3b8cfd
  • MIRAI run: 884b1827-63a2-4a03-9534-f79be45ddf29
  • PR check IDs:
    • clickbench_288055_1789278829
    • clickbench_288062_1789278829
    • clickbench_288068_1789278828
    • tpch_adapted_1_official_288078_1789278828
    • tpch_adapted_1_official_288091_1789278829
    • tpch_adapted_1_official_288125_1789278829

@bobrik
bobrik force-pushed the ivan/parallel-materialize branch 3 times, most recently from 2f87de4 to 243d175 Compare August 30, 2026 21:15
@bobrik
bobrik force-pushed the ivan/parallel-materialize branch 2 times, most recently from 95f1877 to 0ca94e9 Compare August 31, 2026 15:50
@bobrik
bobrik force-pushed the ivan/parallel-materialize branch from 0ca94e9 to 76f565f Compare August 31, 2026 16:28
@bobrik
bobrik force-pushed the ivan/parallel-materialize branch 2 times, most recently from f449be4 to 7280152 Compare September 2, 2026 20:52
Comment thread src/Core/Settings.cpp Resolved
@bobrik
bobrik force-pushed the ivan/parallel-materialize branch 2 times, most recently from 08b9ef6 to 890aace Compare September 2, 2026 22:14
@bobrik
bobrik force-pushed the ivan/parallel-materialize branch 3 times, most recently from 0d8a3f0 to 4b6e319 Compare September 4, 2026 05:37
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 4, 2026
@bobrik

bobrik commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

The unrelated failed test is being fixed in #117764.

@bobrik
bobrik force-pushed the ivan/parallel-materialize branch from 4b6e319 to 451605b Compare September 5, 2026 23:38
Comment thread src/Core/Settings.cpp Resolved
@bobrik
bobrik force-pushed the ivan/parallel-materialize branch from d5a3a4f to 33e012b Compare October 1, 2026 00:11
Ordered reads must merge input streams serially, but constructing the output
columns does not determine row order. For wide results, materialization can
dominate the single merge thread.

This adds a deferred mode to `MergedData` that records source ranges instead of
copying their columns immediately. Sequence numbers are assigned to completed
plans, then they are materialized on a bounded set of workers and restored to
their original order before passing the chunks downstream.

Consecutive completed chunks are released without waiting for every worker.
Each worker can buffer one additional block to reduce head-of-line blocking
while keeping memory usage bounded.

The new `max_parallel_ordered_merge_materialization_threads` setting controls
the number of workers. Values of 0 (the default) and 1 disable the optimization.

In the performance test with 8 input streams and 1024-row blocks, 4 workers
reduced median time from 0.475s to 0.165s, a 2.89x speedup.

On the production workload with matched settings, 8 workers reduced wall time
from 178s to 69.2s, a 2.57x speedup. CPU increased from 312s to 346s and peak
memory from 13.6 GiB to 15.2 GiB.
@bobrik
bobrik force-pushed the ivan/parallel-materialize branch from 33e012b to 9481e21 Compare October 1, 2026 00:35
/// Version 21 registers the `max_parallel_ordered_merge_materialization_threads` plan setting.
/// It is written only towards peers at this version or above because older peers reject unknown
/// query-plan setting names.
static constexpr auto DBMS_QUERY_PLAN_SERIALIZATION_VERSION = 21;

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.

Version-gating max_parallel_ordered_merge_materialization_threads inside SortingStep::Settings::updatePlanSettings only helps when we can serialize per peer. Here the parallel-replicas path still caches one plan blob at DBMS_QUERY_PLAN_SERIALIZATION_VERSION, and DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_PARALLEL_REPLICAS is kept aliased to that global version.

That means this bump to 21 now rejects v20 replicas from parallel-replica reads during a rolling upgrade even when the new setting is left at 0 and the plan could have been serialized compatibly for them. In other words, a purely opt-in local optimization becomes a mixed-version parallel-replicas compatibility regression.

Please either keep the parallel-replicas minimum on the last always-required layout version, or serialize the cached remote-plan blob at the minimum version advertised by the selected replicas.

@bobrik bobrik Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can open a separate PR that would generalize this:

  1. Pick some baseline version.
  2. Bump it if a setting is non-default.

Even that is probably suboptimal. I don't think it belongs in this PR though. Multiple existing setting suffers from the same problem.

It's a non-trivial change to just add here.

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Dear @nickitat, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Dear @nickitat, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

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-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants