Skip to content

Coalesce adjacent singleton row-mask updates during part mutation - #122718

Merged
Avogar merged 5 commits into
masterfrom
codex/mutation-in-overhead-public
Oct 5, 2026
Merged

Avogar merged 5 commits into
masterfrom
codex/mutation-in-overhead-public

Conversation

@skuznetsov-clickhouse

@skuznetsov-clickhouse skuznetsov-clickhouse commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Many separately queued singleton UPDATE _row_exists = 0 WHERE ... IN (...) commands can reach one large part in a single mutation task. Each command otherwise evaluates its own IN predicate over that part. This change coalesces adjacent compatible commands for part-local execution while preserving persisted mutation records, version barriers, and original file planning.

The path is limited to Wide Full parts, supported constant predicates, and stable physical key types. It rejects aliases that could change predicate resolution and SELECT modifiers that could make a nominally singleton subquery return multiple rows. Other inputs use the existing path.

MergeTree table settings control the optimization and its resource bounds:

  • enable_row_mask_update_coalescing (default true in 26.10; false under compatibility=26.9)
  • max_row_mask_update_coalescing_keys (default 256; also respects the effective background max_rows_in_set)
  • max_row_mask_update_coalescing_key_bytes (default 64 KiB)
  • max_row_mask_update_coalescing_commands (default 4,096)
  • max_row_mask_update_coalescing_ast_bytes (default 1 MiB)

The optimization also respects effective background Set and AST limits. It skips coalescing when an explicitly configured finite max_expanded_ast_elements limit is in effect, because the raw mutation AST does not bound the analyzer's expanded query tree.

An isolated Release build of the merged source completed on the dev VM (26.10.1.1; binary SHA-256 5b4ec3e84e15c4811bee09cea14ba177c2aa9eb9bbdc125f0e4d4367ed0c5b40). The expanded-tree reproducer used ten singleton updates plus MATERIALIZE INDEX with a background max_expanded_ast_elements of 100: OFF and ON both completed all 11 mutation records, returned the same remaining row, and showed no QueryTree error or coalescing. SQL references 05256–05258 matched, covering the assignment-alias case, subquery behavior, and separate mutation records. The targeted integration module did not start because its runner could not pull the uncached zookeeper:3.6.2 image through the dev VM's restricted network. Upstream CI is pending.

On the same tested binary, a synthetic 2,000,000-row, 200-command, 0.24 GiB Wide Full part workload on 4 CPU had OFF post-START median 3.606 s (range 3.585–4.009 s) and ON median 0.813 s (range 0.803–0.950 s) across three alternating repetitions, about 4.4x on the median. Trace sampling confirmed 200 → 1 commands when enabled. On a smaller 0.07 GiB part the medians were 1.174 s OFF and 1.120 s ON with overlapping ranges, so that workload did not establish a speedup. Two bypass workloads also had overlapping ranges: 200 non-singleton Wide commands had medians 1.194 s OFF and 1.113 s ON; Compact Full parts had 1.744 s OFF and 1.838 s ON. Trace sampling confirmed no coalescing in either bypass workload. These synthetic results are not a production speedup estimate or a tight overhead bound.

The PR head bf82ae53f7b9e803edc345b59591a5596a2d6ae5 adds only a setting-description correction for the Wide and Full storage requirement to tested code commit 90c10588d04a88c81aa4ca8fb068ec3acddae505. The Release build and runtime checks above were on that code commit; the documentation-only follow-up was not rebuilt locally. The code commit includes merge fc1399de3a324ce7c735dc0a9417ab90c9600d10 from master snapshot bd202925d0d4a9e560455035284251b5f7be1cd7. All 14 PR paths, including two new integration config files, matched the separately merged dev VM source before the successful build. Upstream CI results are pending for the current head.

Changelog category (leave one):

  • Performance Improvement

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

Reduce repeated IN evaluation when many adjacent singleton row-mask updates are applied to one part.


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

Version info

  • Merged into: 26.10.1.1579-master (included in 26.10 and later)
  • Backported to: 26.9.23.15, 26.8.32.14

Repeated singleton IN predicates in a part mutation evaluate separate sets for each command. Coalesce adjacent compatible predicates for part-local execution, while preserving persisted mutation records, barriers, and original file planning. Bound group and input sizes and bypass unsupported part formats and settings.

The preceding mirror-master candidate passed matched Release SQL checks and a repeated synthetic Wide Full benchmark. This public-master port adapts to the current splitAndModifyMutationCommands signature. Public CI, SharedMergeTree integration, mixed-version validation, and a production canary remain pending.
The public repository has no SharedMergeTree engine registration or call site for its scheduling failpoint, so the imported stateless test cannot run here. Existing stateless and ReplicatedMergeTree integration tests cover the public mutation behavior. Cloud-specific validation remains a separate rollout gate.
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bf82ae5]

Summary: ✅


AI Review

Summary

This PR adds a bounded part-local optimization that coalesces adjacent singleton _row_exists = 0 updates during MergeTree mutation planning, guarded behind new MergeTree settings and covered with focused stateless and integration tests. I did not find a remaining correctness, safety, compatibility, or rollout issue in the current patch, and the PR body includes concrete before/after benchmark evidence for the claimed performance win.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit bf82ae5.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.60% 79.50% -0.10%

Changed lines: Changed C/C++ lines covered: 263/283 (92.93%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 28, 2026
@Avogar Avogar assigned Avogar and unassigned Avogar Sep 28, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bf82ae53f with master b466ea8c9 (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

4 object files changed (+91.92 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MutateTask.cpp.o 1.15 MiB 1.23 MiB +83.25 KiB (+7.08%)

719 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

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

Job report

@Avogar Avogar left a comment •

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.

Looks good overall, some minor comments only. Also we should also add a setting to be able to rurn the rewrite off, in case we will find a bug, so we can use previous behaviour.

Comment thread src/Storages/MergeTree/MutateTask.cpp Outdated
|| (settings[Setting::max_expanded_ast_elements] && settings[Setting::max_expanded_ast_elements] < 4096))
return commands;

size_t max_keys_per_command = 256;

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.

This should be a configurable MergeTree setting

Comment thread src/Storages/MergeTree/MutateTask.cpp Outdated
bool key_from_uint128_array_join = false;
};

static constexpr size_t max_combined_key_bytes = 64 * 1024;

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.

This should be a configurable MergeTree setting.

Comment thread src/Storages/MergeTree/MutateTask.cpp Resolved
Comment thread src/Storages/MergeTree/MutateTask.cpp Outdated
/// remains a grouping barrier. Keep file-rename planning based on the
/// original commands, including one `UPDATE` entry per original command.
bool can_coalesce_row_mask_updates = ctx->commands_for_part.size() > 1
&& ctx->commands_for_part.size() <= 4096

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.

One more magic constant, it's not clear why we use it.

Comment thread src/Storages/MergeTree/MutateTask.cpp Outdated
size_t input_bytes = 0;
for (const auto & command : ctx->commands_for_part)
{
if (command.ast_text.size() > (1 << 20) - input_bytes)

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.

One more magic constant 1 << 20, not clear why exactly this and why we need it at all

Many singleton IN row-mask updates repeatedly evaluate predicates over a
large Wide Full part. The original fast path also lacked table-level bounds
and an off switch, and alias or expanded-AST cases could change behavior.

Add MergeTree controls and bounded fallback, reject unsafe alias/subquery
shapes, and retain original commands under an explicitly configured finite
expanded query-tree limit. Extend stateless and integration coverage for the
new guards and settings.

Verified with a Release build, SQL references 05256-05258, an expanded-AST
limit-100 reproducer, and same-binary synthetic OFF/ON benchmarks. The
targeted integration module could not start because the isolated dev VM
could not pull the uncached ZooKeeper image; upstream CI remains pending.
@skuznetsov-clickhouse
skuznetsov-clickhouse marked this pull request as ready for review October 3, 2026 03:31
The generated MergeTree setting description said Wide even though the existing execution gate requires both Wide format and Full storage. Name the Full storage requirement so users do not expect the optimization on Packed parts.

Verification: staged diff and whitespace checks passed, and the wording was compared with the unchanged execution gate. No C++ build was run for this raw-string documentation edit; PR CI will validate the new head.
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR changes only how mutations run. It merges adjacent single-key UPDATE _row_exists = 0 commands while rewriting Wide parts (MutateTask::prepare) and adds new MergeTree settings to control that. No ClickBench or TPC-H SELECT runs that code, and neither benchmark has a performance change to report. The three flagged ClickBench queries are all small, noise-level improvements: Q33 (-5.7%) and Q34 (-5.5%) fall inside master's normal range, and Q28 (-5.3%) is borderline because the two tests disagree, with CPU time essentially flat in all three. Those within-range rulings fit the diff, and nothing in this PR justifies challenging them.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 28 not_sure 1068 1011 -5.3% <0.0001 This is a read-only SELECT, and the PR only changes how mutations run, so it does not execute the changed code. The -5.3% delta is noise: the two tests disagree, the measurements were noisy and CPU time is essentially flat.

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: dd98a70e-f323-4b0d-9c85-def0b21f3db5
  • MIRAI run: 8f4098b5-08fe-40cc-8cab-90c172c734a0
  • PR check IDs:
    • clickbench_3071824_1791040592
    • clickbench_3071830_1791040592
    • clickbench_3071842_1791040592
    • tpch_adapted_1_official_3071859_1791040592
    • tpch_adapted_1_official_3071882_1791040592
    • tpch_adapted_1_official_3071897_1791040592

@Avogar
Avogar added this pull request to the merge queue Oct 5, 2026
Merged via the queue into master with commit e3d9bbe Oct 5, 2026
149 checks passed
@Avogar
Avogar deleted the codex/mutation-in-overhead-public branch October 5, 2026 12:06
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 5, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added 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 labels Oct 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 20, 2026
Backport #122718 to 26.8: Coalesce adjacent singleton row-mask updates during part mutation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). 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.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants