Skip to content

Revert "Fit the insert pipeline and merges into a small server's memory" - #128706

Merged
alexey-milovidov merged 1 commit into
masterfrom
revert-116083
Nov 3, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
revert-116083

Conversation

@fm4v

@fm4v fm4v commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

cc. @alexey-milovidov Revert #116083. It is not in any release branch yet (merged to master on 2026-10-25), so reverting now keeps it out of 26.11.

Reasons:

  • The PR was merged without an approving review. It rewrites about 1000 lines of merge selection (MergeSelectorApplier, the merge predicates, SimpleMergeSelector, TTLMergeSelector, PartProperties) and adds a cap on insert squashing in the same change. Changes of this size in merge selection need a review by the MergeTree owners.
  • There is no setting to disable the insert squashing cap. If it misbehaves, the only workaround is to lower min_insert_block_size_bytes.
  • The cap depends on max_server_memory_usage (including the dynamic hard limit) and on the number of materialized views and insert streams. Because of that, a retried insert can be split into a different number of blocks than the first attempt. From reading the code (not reproduced yet), this can lead to:
    • data loss in a materialized view target on retry, when the cap goes up between attempts and the view block that was not committed is now merged into one that is dropped as a duplicate;
    • duplicate rows in a materialized view target on retry, when the cap goes down between attempts;
    • wrong deduplication of the root table with use_strict_insert_block_limits = 1, which hashes blocks after squashing.
  • The cap applies on larger servers than the description says. For the root table it works below about 3.6 GiB of memory without views and 7.1 GiB with views (it was 2.2 GiB before). For views it is divided by 2 * views * streams, so with 10 views it applies below about 71 GiB, and more with parallel_view_processing = 1. This means more and smaller parts, more non-atomic inserts for clients that send many chunks, and more pressure towards TOO_MANY_PARTS on mid-size servers.
  • merge_memory_estimate_per_source_part_column narrows horizontal merges of wide tables on small servers, and the width also moves with the dynamic hard limit, so the part count stays higher.

The changes can be re-submitted in smaller pieces (the merge width estimate separately from the insert cap, each behind a setting) and reviewed.

The documentation pages under docs/reference/settings/ that mention merge_memory_estimate_per_source_part_column are autogenerated and will be updated by the documentation autogeneration workflow.

Related: #116083
Related: #100389

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Version info

  • Merged into: 26.11.1.1229-master (included in 26.11 and later)

…emory"

This reverts commit ae6fcab, reversing changes made to 2f0acc8.

The PR was merged without an approving review. It changes about 1000 lines of merge selection code (`MergeSelectorApplier`, merge predicates, `SimpleMergeSelector`, `TTLMergeSelector`) together with a new cap on insert squashing that depends on the server memory limit and the number of materialized views, and there is no setting to disable the insert cap. The insert cap makes the block boundaries of a retried insert depend on the memory limit and the set of materialized views at the time of the attempt, which can break insert deduplication on retry.

Related: #116083
@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f74b3d2]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 2, 2026
@fm4v
fm4v enabled auto-merge November 2, 2026 16:39
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 2, 2026
@fm4v
fm4v added this pull request to the merge queue Nov 2, 2026
@alexey-milovidov
alexey-milovidov removed this pull request from the merge queue due to a manual request Nov 2, 2026

@alexey-milovidov alexey-milovidov 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.

What was wrong in the logic of this PR? I checked every single item, and everything was correct.

@alexey-milovidov

Copy link
Copy Markdown
Member

The PR was merged without an approving review.

False. The PR went through one of the most thorough reviews you could imagine.

@alexey-milovidov

Copy link
Copy Markdown
Member

Anyway, if you revert the PR, you are going to introduce a regression: ClickHouse does not work at all on t3a.small machines.

JTCunning pushed a commit to JTCunning/ClickHouse that referenced this pull request Nov 3, 2026
`Stateless tests (amd_binary, targeted)` and `Integration tests (amd_binary, targeted)` in `MergeQueueCI` stopped at 20 minutes from their start. The integration job hit the limit on broad selections and silently ran only a fraction of them (e.g. 370 of 1618 selected test items for the merge-queue entry of ClickHouse#128706). The merge-queue flaky check keeps its 20-minute limit.

Related: ClickHouse#125571
@fm4v

fm4v commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

I withdraw the review point and the use_strict_insert_block_limits claim (both modes hash after squashing, nothing strict-specific). The rest, more precisely. All of this is from reading the code at a8380670201, not reproduced yet.

The cap

capInsertBlockSizeBytesToMemoryLimit (src/Interpreters/capInsertBlockSizeBytesToMemoryLimit.cpp:28):

cap = min(min_insert_block_size_bytes, 0.9 * L * 0.4 / (5 * n)) = min(256 MiB, 0.072 * L / n), with L = total_memory_tracker.getHardLimit().

n (src/Interpreters/InsertDependenciesBuilder.h:140-150): root 1 without views, 2 with views (binds below 3.47 / 6.94 GiB). Each view: 2 * V * S (V views, S sink streams, +1 with materialized_views_squash_parallel_inserts), so it binds whenever L < 6.94 GiB * V * S.

L V S view block cap
28 GiB 1 1 256 MiB (no-op)
28 GiB 10 1 103 MiB
28 GiB 1 8 129 MiB
110 GiB 10 4 101 MiB

The previous cap bound only below ~2.27 GiB, so on servers with tens of GiB this is new behavior, not "no-op with several GiB".

1. Retry can lose or duplicate rows in MV targets

After the view query, the output goes through SquashingTransform with this cap and then UpdateDeduplicationInfoWithViewIDTransform (InsertDependenciesBuilder.cpp:722-736), which numbers the squashed blocks and appends view-block-K to the dedup id (DeduplicationTokenTransforms.cpp:52-57). The id is derived from the source block, the view id, and K, not from the view block content. If a retry cuts the view output at different boundaries:

  • loss: attempt 1 (small cap) commits view block 0 holding a prefix and fails; attempt 2 (larger cap) emits one block 0 with all rows, which is dropped as a duplicate. The suffix never reaches the target.
  • duplicates: attempt 1 commits one big block 0; attempt 2 emits blocks 0, 1, 2; 0 is dropped, 1 and 2 are inserted again.

The cap can differ between attempts because the hard limit is dynamic by default (memory_worker_dynamic_hard_limit = true, re-applied on >1% change), because the retry lands on a replica of a different size (e.g. during vertical scaling), or because the retry runs with a different stream count. Dedup in dependent views is on by default and clients (Kafka connectors, ClickPipes, HTTP clients) retry routinely. The result is silent: MV aggregates disagree with the source table.

2. More and smaller parts in MV targets

10 views at 28 GiB gives 2.5x more parts in every target, more with larger S. Each part means object storage writes and Keeper writes, then merges; TOO_MANY_PARTS and insert delays get closer. Inserts bigger than the cap become several parts and are no longer atomic.

3. No way to turn it off

The constants are hardcoded and the result is min(setting, formula), so raising min_insert_block_size_bytes does not help. Only removing the hard limit entirely disables it. If 1 or 2 shows up in production, there is no mitigation other than a rollback.

4. Merges of wide tables

merge_memory_estimate_per_source_part_column limits merge width roughly linearly in L / columns and re-reads the dynamic limit every selection round (MergeSelectorApplier.cpp:200-219): below 100 parts at ~1,100 columns with a 7 GiB limit, ~4,600 at 28 GiB. TTL delete and recompression merges also get this cap (they had none). buildPartProperties adds O(columns) work per part per selection round, which the 0 value of the setting does not disable.

5. Async insert with dedup

DeduplicationInfo::setViewBlockNumber throws NOT_IMPLEMENTED for async inserts with dedup when a view produces more than one block (InsertDeduplication.cpp:927-945). A smaller view cap makes that more likely. Needs async_insert_deduplicate = 1, so low exposure.

I'm merging the revert to keep this out of 26.11. Happy to help re-land it in pieces: the merge width estimate on its own, and the insert cap behind a setting, with the view cap not depending on the stream count and a fix for the view block numbering on retry.

@fm4v
fm4v added this pull request to the merge queue Nov 3, 2026
@alexey-milovidov
alexey-milovidov removed this pull request from the merge queue due to a manual request Nov 3, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Ok, at least item 3 makes sense...

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 3, 2026
Merged via the queue into master with commit a666472 Nov 3, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the revert-116083 branch November 3, 2026 16:52
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 3, 2026
PedroTadim added a commit that referenced this pull request Nov 4, 2026
…ate_adaptive_write_buffer

`max_memory_ratio_to_activate_adaptive_write_buffer` was reverted on master
(#128706 reverts #116083 "clickbench-small-memory"), so it no longer exists in
the binary CI builds. The randomizer still injected
`--max_memory_ratio_to_activate_adaptive_write_buffer` into every test's client
args, so every test that drew it failed with return code 40
`Code: 552 ... Unrecognized option ... (UNRECOGNIZED_ARGUMENTS)` regardless of
content - a systemic storm across the targeted and all flaky-check jobs ("Too
many test failures"). Comment the entry out (keep it for re-enable if the
setting ever returns). `min_columns_to_activate_adaptive_write_buffer` and
`merge_memory_estimate_per_source_part_column` are unaffected (the latter is
not randomized).

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=106734&sha=9b5082447401231d07326cb0c3b15e2152dfbdd8&name_0=PR
PR: #106734

Co-Authored-By: Claude Opus 4.8 <[email protected]>
PedroTadim added a commit that referenced this pull request Nov 4, 2026
Randomize many more server and MergeTree settings in clickhouse-test, and pin
or narrow the tests whose observable plan/EXPLAIN/spill/cache/stats output the
extra randomization legitimately changes (results unchanged). Output-changing
settings that make poor randomization candidates are commented out of the
randomizer (kept, so they can be re-enabled); one-off non-bug test failures are
pinned to the setting's default in the test itself. Each pin carries a comment
explaining which randomized setting it guards against.

Rebased onto current master and squashed. The reverted MergeTree setting
`max_memory_ratio_to_activate_adaptive_write_buffer` (#128706 reverts #116083)
is no longer randomized.

PR: #106734

Co-Authored-By: Claude Opus 4.8 <[email protected]>
subkanthi pushed a commit to subkanthi/ClickHouse that referenced this pull request Nov 10, 2026
The Parquet reader keeps a whole pipeline of `input_format_parquet_prefer_block_bytes`
blocks in flight: prefetched, decoded and ready to deliver. Measured on the 105-column
ClickBench `hits` table, the default 16 MiB preferred block size costs about 290 MiB of
live reader memory, so a cap of a 64th of the budget did not help a small server. With
the default setting the new cap binds only below about 4.4 GiB of memory limit.

This is a part of ClickHouse#116083 reverted in
ClickHouse#128706, re-submitted separately.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
subkanthi pushed a commit to subkanthi/ClickHouse that referenced this pull request Nov 10, 2026
…by default

The squashing stages of an `INSERT` cap `min_insert_block_size_bytes` by the memory
limit. The cap was hardcoded as 0.9 / 8 of the limit, could not be disabled, and was
too high for a small server: a squashing stage holds several block-sized copies at once.

The new setting `insert_squashing_max_memory_ratio` (default 1/16) replaces the
hardcoded fraction. The cap depends only on the memory limit and the setting, not on
the number of materialized views or insert streams. `0` disables the cap, and `0.1125`
gives the previous behavior.

This is a part of ClickHouse#116083 reverted in
ClickHouse#128706, re-submitted in a reduced form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-merges Merges of data parts: choosing what to merge (merge selectors, merge predicates, part collectors)... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-merges Merges of data parts: choosing what to merge (merge selectors, merge predicates, part collectors)... pr-not-for-changelog This PR should not be mentioned in the changelog 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.

3 participants