Repository navigation
Add small-parts batching settings to SimpleMergeSelector - #100436
Conversation
Add three new MergeTree settings to reduce write amplification under rapid small part insertion: - `merge_selector_small_parts_threshold` (default 10 MB): max size for a part to be considered "small" - `merge_selector_small_parts_min_count` (default 0 = disabled): minimum number of parts required before merging small fresh parts - `merge_selector_small_parts_max_age` (default 600s): age safety valve — once the youngest part in the range exceeds this age, the restriction is lifted so old small parts can still eventually merge When all three conditions are met (all parts small, all parts fresh, too few parts), the merge is rejected, forcing the system to accumulate more parts before merging. This reduces the total number of merge operations. Simulation test shows ~38% fewer merges (42 → 26) with negligible WA change (3.31 → 3.325) for 200 × 1 MB parts inserted every 1 second.
|
Workflow [PR], commit [f7b73b6] Summary: ✅
AI ReviewSummaryThis PR adds three opt-in Final Verdict✅ Approve |
The new merge selector small-parts settings were originally added to the `26.4` block in `SettingsChangesHistory.cpp`. After merging master, `26.5` is the new current version, so move them there. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
@groeneai, provide a fix for stress test failures: |
|
@alexey-milovidov — STID 2508-3132 (and the sibling stack-frame variants 2508-34fb, 2508-3307, 2508-3cd3) are the chronic Your PR #98779 ("Fix assertion failure in view dependencies on DROP+CREATE MV") merged 2026-05-08 21:44 UTC, but the family is still firing on master post-fix — CIDB shows 9 master hits across 5 distinct master commits in the 24h after the merge, all on commits that include
The hit on this PR (version PR #98779 evidently doesn't cover all |
… limit Switch the `merge_selector_small_parts_max_age` safeguard to compare against the **oldest** part in a candidate range (`max_age`) instead of the **youngest** part (`min_age`). The intent — documented both in the header and in the PR description — is that the batching restriction should apply only when *all* parts in the range are still fresh. The previous condition `min_age < small_parts_max_age` is the opposite: it is satisfied whenever *any* part is fresh, so a single newly inserted small part can keep blocking merges with older small parts indefinitely, even when the rest of the range has long since aged past the limit. Using `max_age` correctly expresses "all parts fresh" and makes the threshold a proper safety valve: as soon as any part ages past the limit, the restriction is lifted and the backlog of stale small parts can merge. Also align the setting description in `MergeTreeSettings` and the comment in `SimpleMergeSelector.h` with the corrected semantics. Addresses review on ClickHouse#100436. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
@groeneai, investigate the AST fuzzer failure: Received signal 6 (STID: 3452-4527) and provide a fix in a separate PR. If the fix is already in progress, link it here. Crash is in correlated-subquery planner ( The other failure ( |
|
@alexey-milovidov — confirming both failures are tracked and unrelated to this PR. Status of each: STID 3452-4527 (AST fuzzer, amd_debug targeted, old_compatibility)Fix PR already in flight: #103738 — "Fix vector OOB in ConvertQueryToCNFPass when WHERE is a correlated subquery (STID 3452-4527)" (groeneai, opened 2026-04-29, in response to your directive on PR #103539). Root cause matches the stack you described: PR state right now (commit 89065eb):
CIDB confirms 3452-4527 has only 3 hits in 30 days, all on unrelated PR runs (2026-04-26 PR #103539, 2026-04-27, 2026-05-18 PR #100436) — never master, never twice on the same PR. Matches "rare trunk bug exposed by fuzzer randomization" pattern, fully unrelated to amosbird's 02180_group_by_lowcardinality (Stateless tests, ParallelReplicas s3 storage)Chronic master flake tracked under task umbrella SummaryBoth failures = pre-existing chronic issues with tracking tasks. No new fix PR needed from PR #100436 — the fix for 3452-4527 is #103738, just needs reviewer attention. |
Workflow files in this branch were stale or had been deleted as a workaround for GitHub's workflow-scope check. Restoring them to match `master` so the PR diff no longer contains those unrelated changes.
The version on master is now 26.6.1.1 and `02995_new_settings_history` requires new settings to be registered under a version strictly greater than `26.5`. Move `merge_selector_small_parts_threshold`, `merge_selector_small_parts_min_count` and `merge_selector_small_parts_max_age` into a new `26.6` `merge_tree_settings_changes_history` section. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Cursor Bugbot pointed out that `merge_selector_small_parts_min_count` was enforced in `allow` before `Estimator::consider` ran the right-tail trimming heuristic. A candidate passing with `size == small_parts_min_count` could then be shortened to fewer parts and still scheduled, breaking the contract "require at least this many parts". Concrete case: 8 fresh parts, all `< merge_selector_small_parts_threshold`, sizes `[9 MiB, 7 x 64 KiB]`, `small_parts_min_count = 8`. The range passes `allow`, then trimming reduces it to 2-3 parts, so a `< 8` merge still runs. Re-check the gate inside `Estimator::consider` after trimming. Since trimming removes only small parts from the right end, the trimmed range's `max_size` and `max_age` cannot exceed the originals, so passing the originals through is conservative and correct. Added a regression test that constructs the failing scenario directly. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…gate Cursor Bugbot pointed out that the post-trim re-check added in 972ca72 was still bypassable. The gate compared pre-trim `max_size`/`max_age`, so if the right tail held the only large or only old part, the pre-trim max would see "large" or "old" and skip the gate — even when the surviving trimmed range was the all-small-fresh batch the user wanted to defer. Concrete case: 8 small parts where the rightmost is tiny and old (age > 600, size < 1% of sum). `allow` accepts the range because pre-trim `max_age` is old, so the small-parts gate is bypassed. Right-tail trimming removes the old tail, leaving < 8 parts that are all small and fresh. The post-trim re-check still sees the old pre-trim `max_age` and lets the merge through. Fix: compute `max_size` and `max_age` over the trimmed `[begin, end)` range inside `Estimator::consider` and apply the gate to those values. Added a regression test (`SmallPartsMinCountUsesTrimmedMaxAge`) that reproduces the failing scenario directly: 7 x 1 MiB fresh + 1 x 8 KiB old at the right tail. Trimming removes the tail; the trimmed range's `max_age` is fresh, so the gate fires and the candidate is rejected. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The simulation in `SmallPartsMinCountReducesWriteAmplification` is deterministic, so `EXPECT_LE` would still pass if the `merge_selector_small_parts_min_count` setting stopped affecting the simulation entirely (`with_small.num_merges == baseline.num_merges`). Use `EXPECT_LT` so the test actually guards the PR's merge-count reduction claim (42 -> 26 merges for this workload). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Continued this PR:
Reviewed the full diff ( |
… compatibility The `SettingsChangesHistory` entries listed the previous values of `merge_selector_small_parts_threshold` and `merge_selector_small_parts_max_age` as `0`. Under `compatibility < 26.6` that reverted both knobs to `0`, so a user who reads the changelog and enables batching by setting only `merge_selector_small_parts_min_count` would still get no batching: no part can satisfy `max_size < 0` / `max_age < 0`. Set the previous values to the runtime defaults (`10 * 1024 * 1024` and `600`), leaving `merge_selector_small_parts_min_count` as the sole compatibility gate. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… only The `DECLARE` docstrings of `merge_selector_small_parts_threshold`, `merge_selector_small_parts_min_count` and `merge_selector_small_parts_max_age` read as if the gate always applies whenever the `Simple` / `StochasticSimple` selector is used, but `fillSimpleSettings` zeroes `small_parts_min_count` for aggressive selection, so `OPTIMIZE` without `FINAL` ignores it. Document the exemption in all three settings descriptions - they are the durable user-facing docs - and in the `small_parts_*` comment in `SimpleMergeSelector.h`. Addresses the review thread on `MergeTreeSettings.cpp` and the AI review verdict.
|
🕵 Merged fresh The four affected translation units compile after the merge, and Public CI was fully green before this push; the only red was |
…hold # Conflicts: # src/Core/SettingsChangesHistory.cpp # src/Storages/MergeTree/Compaction/MergeSelectors/SimpleMergeSelector.cpp # src/Storages/MergeTree/Compaction/MergeSelectors/SimpleMergeSelector.h # src/Storages/MergeTree/tests/gtest_simple_merge_selector.cpp
…at_once` Address review: with `merge_selector_enable_heuristic_to_lower_max_parts_to_merge_at_once` (default on) a nearly full partition lowers the effective cap to `base`, so a user-set `min_parts_to_merge_at_once = N` above the cap could never form an `N`-part candidate: `allow` only saw narrower widths, rejected them all, and nothing merged until a force-merge setting fired. The compensation that the small-parts gate already had in `selectWithinPartsRange` now also enumerates up to `min_parts_to_merge_at_once` when the lowered cap is below it, with the same exemptions (force-merge eligible ranges, and an explicit `max_parts_to_merge_at_once` below the floor is left alone). Also mirror master's new `min_partition_age_to_force_merge` in the post-trim `min_parts_to_merge_at_once` and small-parts re-checks of `Estimator::consider`, matching the precedence `allow` gives to it. After merging master the `ForceMergeByPartitionAgeWaivesMinPartsToMergeAtOnce` gtest failed on this branch because the re-check only knew about `min_age_to_force_merge`. New gtests: `MinPartsToMergeAtOnceSurvivesLoweredMaxPartsCap`, `MinPartsToMergeAtOnceCapCompensationStopsAtTheFloor`, `MinPartsToMergeAtOnceDoesNotLiftCapForForceMergeEligibleParts`, `MinPartsToMergeAtOnceDoesNotLiftCapForPartitionAgeForceMerge`. All 19 `SimpleMergeSelector.*` gtests pass. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 Continued this PR:
|
Master bumped the version to 26.10, so the `settings_changes_history` style check now requires the records of the three new `merge_selector_small_parts_*` settings under the `26.10` block of `merge_tree_settings_changes_history`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=100436&sha=25b145228965fbea6343a8281efec79dbd83550d&name_0=PR&name_1=Style%20check PR: ClickHouse#100436 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…hold Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts: # src/Storages/MergeTree/Compaction/MergeSelectors/SimpleMergeSelector.cpp
…small_parts_min_count` doc The lowered-cap exemption and the early escape for a contradictory configuration apply to `min_partition_age_to_force_merge_seconds` as well as to `min_age_to_force_merge_seconds`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-parts gate Add `MinPartitionAgeToForceMergeOverridesSmallPartsMinCountAfterTrim`: with `small_parts_min_count` armed, `min_age_to_force_merge = 0` and `min_partition_age_to_force_merge` reached by the idle partition, the trimmed range must still be selected. The test fails if the partition-age clause is dropped from `qualifies_for_force_merge` in `Estimator::consider`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add three new MergeTree settings that let users reduce the number of merge
operations under rapid small-part insertion. When all parts in a candidate
merge range are small, fresh, and fewer than a configurable minimum count,
the merge is rejected — forcing the system to accumulate more parts before
merging.
Settings:
merge_selector_small_parts_threshold(default 10 MB) — size ceiling for "small" partsmerge_selector_small_parts_min_count(default 0 = disabled) — minimum part count requiredmerge_selector_small_parts_max_age(default 600 s) — age safety valveDisabled by default. Simulation test with 200 × 1 MB parts at 1 insert/s
shows merge count drops from 42 to 26 (−38%) with negligible WA change.
Rationale
Merging a small number of small parts is wasteful: the I/O cost is high
relative to the modest read-performance improvement. Waiting for more small
parts to accumulate produces wider merges that achieve the same part-count
reduction at lower total write amplification.
Cassandra STCS applies the same idea: it requires at least
min_threshold(default 4) similar-sized SSTables before triggering compaction, and groups
SSTables below
min_sstable_size(default 50 MB) together.The gate applies to background merge selection only. Aggressive selection
(
OPTIMIZEwithoutFINAL) is an explicit user request that ignores partnovelty, so
fillSimpleSettingsdisables the gate (small_parts_min_count = 0)when it builds the aggressive selector —
OPTIMIZE TABLEstill merges a freshbelow-minimum small range immediately.
The gate is monotone on the candidate set: it only rejects candidate ranges,
it never introduces a new one. So every range the selector emits with the gate
enabled is a range it could also emit with the gate disabled — the gate cannot
invent a merge shape, in particular it cannot steer the selector toward merging
large dissimilar-sized parts.
There is exactly one deliberate exception. The default-on fullness heuristic
(
merge_selector_enable_heuristic_to_lower_max_parts_to_merge_at_once) can lowerthe effective
max_parts_to_merge_at_oncebelowmerge_selector_small_parts_min_count, which would make the minimum unattainableand silently degrade the gate into "block all fresh small merges until
merge_selector_small_parts_max_age". To avoid that, enumeration is extendedthrough the first all-small, all-fresh candidate of exactly
merge_selector_small_parts_min_countparts. That extension is deliberatelynarrow: stale or large ranges keep the lowered cap, an explicitly configured
max_parts_to_merge_at_oncebelow the minimum keeps it (that is a contradictoryconfiguration, and it is documented as such), and ranges that already reached
min_age_to_force_mergekeep it too —allowaccepts those before thesmall-parts gate is evaluated, so the gate is not what blocks them and there is
nothing to compensate for.
merge_selector_window_sizeis a hard width limit in the same way: it is aglobal anti-snowball protection and is not widened for the gate, so
configuring it below
merge_selector_small_parts_min_countis likewisecontradictory. This is documented in the setting description.
That is deliberately a statement about the candidate set, and it is the only
claim this PR relies on. Two things it explicitly does not claim:
Estimatorkeeps one global best candidate per partition, so rejecting thetop-scoring small batch can hand that round to a different, already-eligible
range rather than produce no merge. For example, in a fresh partition shaped
[1, 1, 1, 1000, 5, 5, 5, 5, 1000, ...]MiB withmerge_selector_small_parts_min_count = 4, the three 1 MiB parts are the bestcandidate without the gate, and with the gate the four 5 MiB parts are merged
instead. The rejected batch is still merged later (or immediately once
min_age_to_force_mergeor the age safety valve fires).(
sum_size / max_size >= lowered_base) holds for every emitted range. Thepre-existing right-tail trimming heuristic can already shorten a range after
it passes
allow, independently of these settings; this PR does not changethat behavior.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add
merge_selector_small_parts_min_count,merge_selector_small_parts_threshold, andmerge_selector_small_parts_max_ageMergeTree settings to reduce the number of merge operations under rapid small-part insertion by requiring a minimum batch size before merging small fresh parts.Documentation entry for user-facing changes
Three new MergeTree settings are added. When
merge_selector_small_parts_min_countis set to a non-zero value (e.g. 8), the merge selector will not merge small parts (belowmerge_selector_small_parts_thresholdbytes) that are younger thanmerge_selector_small_parts_max_ageseconds unless at least that many parts are available. This reduces merge churn under rapid small-part insertion workloads.Workflow [PR]
Sync PR [sync-upstream/pr/100436]
Version info
26.10.1.2172-master(included in26.10and later)