Skip to content

Use merge_use_batch_sorting_queue for lightweight delete, TTL, patch part and DEDUPLICATE merges - #133262

Merged
Algunenano merged 3 commits into
ClickHouse:masterfrom
groeneai:merge-batch-sorting-queue-row-changing-merges
Nov 23, 2026
Merged

Algunenano merged 3 commits into
ClickHouse:masterfrom
groeneai:merge-batch-sorting-queue-row-changing-merges

Conversation

@groeneai

@groeneai groeneai commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #132085
Related: #108468

Changelog category (leave one):

  • Improvement

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

With merge_use_batch_sorting_queue enabled, merges that drop or rewrite rows (lightweight deletes, TTL deletes, patch parts of lightweight updates and OPTIMIZE ... DEDUPLICATE) now also use the batch sorting queue. With the setting enabled, such merged parts can be split into blocks differently than on previous versions. The data is the same, but in a ReplicatedMergeTree cluster with replicas on different versions this can show up as a harmless "not byte-identical" checksum mismatch, after which the replica downloads the merged part from another replica.

Description

Asked for in #132085 (comment). The !merge_may_reduce_rows term came with the projection term from #108468 for missing coverage (#108468 (comment)), not for a defect. Now merge_use_batch_sorting_queue alone chooses the queue of an Ordinary merge. Only MergingSortedTransform receives it; the other merging algorithms already use their own batch queue, which the setting description now says instead of "keep using the default queue". The other uses of merge_may_reduce_rows are unchanged.

In horizontal merges the merge sees plain sorted streams: lightweight-deleted rows and patch parts are handled at read, expired TTL rows are dropped by TTLStep after the merge, and DEDUPLICATE runs a DistinctStep after it. Vertical lightweight delete and TTL delete merges now run the batch queue's row-mask code (MergingSortedAlgorithm::insertRows, bfb72a13736d) for the first time.

The new tests compare the merged part under both queues in horizontal and vertical merges (lightweight delete with patch parts, and DEDUPLICATE, in one; TTL delete in the other): row counts and a content hash with _part_offset, _block_number and _block_offset.

Release build, OPTIMIZE ... DRY RUN of 10 overlapping parts of 1M rows, setting on / off on one binary (off = the default queue, as on master), medians of 12 runs:

parts whole merge MergingSortedMilliseconds
runs of 100 rows, no delete 0.909 0.520
runs of 100 rows, lightweight delete of 5%, horizontal 0.918 0.498
same, vertical 0.976 0.784
runs of 100 rows, TTL delete of 5%, vertical 0.980 0.762
keys interleaved row by row, lightweight delete of 5% 1.043 1.127

Vertical merges gain less because masked rows are still inserted one by one. With the setting forced on, none of 881 existing lightweight delete, TTL, patch part and deduplication tests fails only with this change.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.309-master (included in 26.12 and later)

The sorting queue of an `Ordinary` merge now depends only on
`merge_use_batch_sorting_queue`. The removed `!merge_may_reduce_rows` term
kept lightweight delete, TTL delete, patch part and `OPTIMIZE ... DEDUPLICATE`
merges on the default queue. It came from the review of ClickHouse#108468
(ClickHouse#108468 (comment))
for missing coverage, not for a defect, like the projection term removed in ClickHouse#132085.

Only `MergingSortedTransform` receives the strategy; the other merging
algorithms already use a batch queue of their own, so the setting
description now says the setting has no effect on them instead of
"keep using the default queue". The other uses of `merge_may_reduce_rows`
(projections, text indexes, statistics, minmax index rebuild) are unchanged.

In horizontal merges the merge itself sees plain sorted streams: lightweight
deleted rows are filtered at read, patch parts are applied at read, expired
TTL rows are dropped by `TTLStep` after the merge, and `DEDUPLICATE` runs a
`DistinctStep` after the merge. Vertical lightweight delete and TTL delete
merges now run the batch queue's row-mask code in
`MergingSortedAlgorithm::insertRows` for the first time.

The new test compares the merged parts under both queues for each case
(row counts and a hash of the content with `_part_offset`, `_block_number`
and `_block_offset`).

Requested in ClickHouse#132085 (comment)

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

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

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (2 nits, no action).

# Sev Finding Verdict Evidence / action
1 ⚠️ The new test still passes if the removed guard is restored, since it compares the two queues' results and does not assert which queue ran; add a queue-strategy profile event DISAGREE The test is the result comparison asked for in #132085. No existing event tells the queues apart, and I did not add one only for a test. Checked locally instead: a throw at the top of mergeBatchImpl fails exactly the 7 batch-queue merges of the test with this change and none without it; inverting the mask check in insertRows reddens the vertical cases.

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

Session id: cron:clickhouse-review-slot-10:20261120-154100

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Not a defect, a missed speed-up. The observable is the queue these merges take: with a throw at the top of mergeBatchImpl, exactly the 7 _batch merges of the new test fail with this change and none on master; release CPU stacks show mergeBatchImpl with the setting on and mergeImpl with it off.
b Root cause explained? createMergedStream required !merge_may_reduce_rows for the batch queue, so every lightweight delete, TTL delete, patch part and DEDUPLICATE merge stayed on the default queue. #108468 added the term for missing coverage (review thread r3616009549), not for a defect.
c Fix matches root cause? The term is deleted; the queue now depends only on the setting. The other uses of merge_may_reduce_rows are unchanged.
d Test intent preserved / new tests added? New stateless test comparing the merged part under both queues: lightweight delete, TTL delete and patch parts in horizontal and vertical merges, DEDUPLICATE; row counts, a content hash with _part_offset, _block_number, _block_offset, and the merge algorithm from part_log. No performance test (as asked).
e Both directions demonstrated? The throw-in-mergeBatchImpl mutant fails the 7 batch merges with the change and none without it. Inverting the mask test in insertRows reddens the vertical cases; an extra deleted row or swapped duplicate order reddens the matching line. 50/50 randomized runs pass.
f Fix is general across code paths? One condition serves every Ordinary merge (base tables and normal projection parts); the other merging algorithms already use their own batch queue; Replicated and Shared MergeTree use the same MergeTask.
g Fix generalizes across inputs (params/datatypes/wrappers)? Both mask sources (_row_exists, the TTL row filter), patch parts in both algorithms, DEDUPLICATE tie order, a whole removed run of rows. 881 existing lightweight delete, TTL, patch part and deduplication tests with the setting forced on: none fails only with this change.
h Backward compatible? (maintainer-approved exception only) Opt-in setting, no default, format or settings-history change. Vertical merged parts can get different granule boundaries, which a mixed-version ReplicatedMergeTree can report as a harmless "not byte-identical" checksum mismatch; stated in the changelog entry.
i Invariants and contracts preserved? Both mask branches write one rows_sources entry per input row (the row-count check in MergeTask holds in every vertical case); merged rows, their order, tie order and gathered columns equal the default queue's (the new test).

Session id: cron:clickhouse-impl-slot-5:20261120-135500

@clickhouse-gh clickhouse-gh Bot added the can be tested Allows running workflows for external contributors label Nov 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ee52f67]

Summary: ✅


AI Review

Summary

Reviewed #133262 at ee52f674756a22b77a77c0817b698cfed9875e1c. The change makes merge_use_batch_sorting_queue control all Ordinary merges, including lightweight deletes, TTL processing, patch parts, and OPTIMIZE ... DEDUPLICATE, and updates its documentation and focused regression coverage. No actionable findings remain. Both revised tests pass in the current head's fast-test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 20, 2026
@cwurm cwurm self-assigned this Nov 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ee52f6747 with master 8b903d107 (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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-merges Merges of data parts: choosing what to merge (merge selectors, merge predicates, part collectors)... label Nov 20, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 3a1e909

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
Fast test (arm_darwin) / 05042_crash_handler_without_global_context not from this PR (it does not touch the crash handler): clickhouse-local hangs on macOS after SIGTSTP in isCFICheckTrap, added by #132527, and the test times out. The same timeout hit this job on 23 PRs since 17:13 UTC #133270 (external, open)
PR praktika rollup of the row above #133270 (external, open)
Sync - CH Inc sync (private, not actionable by me)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261120-190022

@cwurm

cwurm commented Nov 23, 2026

Copy link
Copy Markdown
Member

@groeneai 05838_merge_batch_sorting_queue_row_changing_merges is too slow in the private sync CI: it failed 5 of 5 runs of the flaky check with ASan, s3 storage and meta in keeper, with "Test runs too long (> 180s). Make it faster." Could you make it faster, as you did for 05813 in #132085? For example, use fewer rows, fewer parts or fewer merges per case, and bound the randomized settings that make it slow. Please also merge master to pick up #133270, which fixes the unrelated 05042_crash_handler_without_global_context timeout in Fast test (arm_darwin).

groeneai and others added 2 commits November 23, 2026 12:18
The test ran over 180 s in every flaky-check run with ASan, s3 storage
and meta in keeper in the private CI. Its cost is the number of merges,
part copies and mutations, not the row count: under the thread fuzzer a
merge of 100 rows costs nearly as much as one of 400, and most of it is
waiting, not CPU.

Use 100 rows in 3 parts instead of 400 in 4, drop the `k` column, give
the lightweight delete and the patch parts one set of tables (both are
applied at read, and the vertical merge still takes the row mask path),
run the `DELETE` once on the seed before copying its parts, and set the
merge settings in `CREATE TABLE ... AS ... SETTINGS` instead of an
`ALTER ... MODIFY SETTING` per table. The TTL delete cases move to
05838_merge_batch_sorting_queue_row_changing_merges_ttl, so each file
carries about half of the remaining work.

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

Copy link
Copy Markdown
Collaborator Author

Done in ee52f67, and master is merged (it brings #133270).

  • 100 rows in 3 parts instead of 400 in 4, and no k column.
  • Lightweight delete and patch parts share one set of tables, the DELETE runs once on the seed before its parts are copied, and the TTL cases moved to 05838_merge_batch_sorting_queue_row_changing_merges_ttl. Every case still compares both queues in horizontal and vertical merges, with _part_offset, _block_number and _block_offset.

The cost was the number of merges, part copies and mutations rather than the rows: under the thread fuzzer a vertical merge of these parts took about 6 s of wall time for 0.9 s of CPU. Debug build, thread fuzzer on, 8 copies on 16 CPUs (the flaky check's shape), p50 per run: the old file 214-250 s, now 58 s and 33 s for the two files. I did not add a settings bound: over 50 randomized runs no setting moved the time much, and the tables already pin index_granularity and merge_max_block_size.

A throw at the top of mergeBatchImpl still fails exactly the 5 batch-queue merges, and inverting the mask check in insertRows still breaks both vertical cases.

@Algunenano Algunenano self-assigned this Nov 23, 2026
@Algunenano
Algunenano added this pull request to the merge queue Nov 23, 2026
Merged via the queue into ClickHouse:master with commit cd61bfb Nov 23, 2026
195 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 23, 2026
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-merges Merges of data parts: choosing what to merge (merge selectors, merge predicates, part collectors)... pr-improvement Pull request with some product improvements 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.

4 participants