Skip to content

Add article: merges that fail with MEMORY_LIMIT_EXCEEDED in a loop - #196

Open
BorisTyshkevich wants to merge 5 commits into
mainfrom
kb/merges-memory-limit-loop
Open

BorisTyshkevich wants to merge 5 commits into
mainfrom
kb/merges-memory-limit-loop

Conversation

@BorisTyshkevich

Copy link
Copy Markdown
Contributor

New article: altinity-kb-setup-and-maintenance/altinity-kb-merges-memory-limit-loop.md. It covers a replicated merge that fails with MEMORY_LIMIT_EXCEEDED again and again: how to detect it, how to stop the loop, and why merges of wide rows use so much memory.

Background

We saw this on a production cluster on 25.8: an OpenTelemetry traces table with a Map attributes column and a 12.6 GiB pod.

  • Five logged MERGE_PARTS entries, each about 30 GB, failed with MEMORY_LIMIT_EXCEEDED for about 2 weeks.
  • There were about 430 failed attempts a day per replica, and they kept CPU at about 100% while queries were idle.
  • All merges were vertical. Peaks were 15–24 GiB even when the pods had 27 GiB.

Sources for the claims (25.8)

  • Logged merges cannot change, and retries wait at most 60 s. See ReplicatedMergeTreeQueue::shouldExecuteLogEntry and max_postpone_time_for_failed_replicated_merges_ms (60000 in system.merge_tree_settings). Upstream FR #135911 describes the same retry storm.
  • max_bytes_to_merge_at_max_space_in_pool does not stop a logged merge.
    • In shouldExecuteLogEntry, ignore_max_size = (max_source_parts_size == max_bytes_to_merge_at_max_space_in_pool).
    • getMaxSourcePartsSizeForMerge (Compaction/CompactionStatistics.cpp) returns that maximum while free slots ≥ number_of_free_entries_in_pool_to_lower_max_size_of_merge.
    • Otherwise it interpolates between max_bytes_to_merge_at_min_space_in_pool and the maximum.
  • The threshold workaround was used on the production cluster (25.8.16).
    • We set it to 31 out of 32 slots, then ran STOP/START MERGES.
    • The running merges ended with error 236.
    • Three big merges restarted at once on each replica, not 1–2, which is why the article mentions the race.
  • STOP MERGES cancels running merges. getActionLock(PartsMerge) calls merges_blocker.cancel(), and MergeTask checks it before every block (checkOperationIsNotCanceled).
  • Vertical merge granularity. In vertical merges, result granules follow the blocks of the key-column stage; see ClickHouse#46324 for the design. Insert granularity uses the average row size of the block: computeIndexGranularity, and the docs' "data storage" section.

Tests (local ClickHouse 25.8.16.34)

The test table has a sort key (svc, k), 10 small String columns and an attrs Map(String, String) column. Every 33rd row has svc = 'heavy' and about 25 KB of attributes. There are 6 inserts of 300,000 rows with min_bytes_for_wide_part = 0, followed by OPTIMIZE FINAL 1–3 times. All merges were vertical. Peak memory comes from system.part_log:

Settings Rows per granule after merge First merge peak Later merges peak
defaults 8072 1.49 GiB 844 MiB
merge_max_block_size_bytes = 1048576 8072 1.49 GiB 844 MiB
merge_max_block_size = 1024 at CREATE 1023 1.49 GiB 120 MiB
merge_max_block_size = 1024 via ALTER on the defaults table 1042 on average – 844 MiB
index_granularity = 1024 1023 397 MiB 120 MiB
index_granularity_bytes = 1048576 1199 450 MiB 132 MiB

The result part was 13 MiB compressed in every case.

ALTER ... MODIFY SETTING index_granularity and index_granularity_bytes fail with READONLY_SETTING (472) on 25.8.

Recipes tested

  • The part_log query: on local 25.8.16, and as a variant on the production 25.8.16 cluster.
  • The replication_queue query: on local 25.8.16 and on the production cluster.
  • MODIFY SETTING / RESET SETTING number_of_free_entries_in_pool_to_lower_max_size_of_merge: on local 25.8.16. MODIFY also ran on production.
  • SYSTEM STOP/START MERGES: on production.

Not verified

  • Behavior on versions other than 25.8.
  • Why the ALTER case keeps the old memory although average rows per granule went down to 1042. My guess is that the merge copies whole source granules where source parts do not overlap in key order. The article states only what the test observed.

Draft: please review before merge.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Describe the pool threshold as a size-based mitigation, not a concurrency cap. Clarify retry backoff, cancellation, rollback and data deletion risks. Preserve the tested diagnostic SQL, summarize benchmark findings and remove unsupported generalizations. Revised recovery sequence still needs runtime validation.
@BorisTyshkevich
BorisTyshkevich marked this pull request as ready for review December 1, 2026 15:50
BorisTyshkevich and others added 3 commits December 1, 2026 17:00
Distinguish source-verified behavior from tests performed on 25.8. Document the 25.4 introduction of configurable merge retry backoff and qualify immutable granularity settings for ReplicatedMergeTree.
… partition

Explain the replicated DROP_RANGE mechanism, empty detached-part precondition, completeness and duplicate-data risks, and the need to reduce subsequent merge sizes. Mark the approach as source-verified but not runtime-tested for this failure scenario; do not add an untested executable recovery recipe.
@BorisTyshkevich

Copy link
Copy Markdown
Contributor Author

Changes in the latest commit, all checked or tested:

  • Pre-25.4 retries: getPostponeTimeMsForEntry is missing from ReplicatedMergeTreeQueue.cpp in both 24.8 and 25.3. A failed merge there has no backoff.
  • Fetch instead of merge: in MergeFromLogEntryTask.cpp (25.8, L209-L223), a replica fetches the merged part only when the entry is older than prefer_fetch_merged_part_time_threshold and the source parts take at least prefer_fetch_merged_part_size_threshold on disk.
  • DETACH/ATTACH: tested on local 25.8.16.34 with two ReplicatedMergeTree replicas, embedded Keeper and a 1 GiB max_server_memory_usage. A MERGE_PARTS entry was looping with 241, and num_tries grew 13 → 15 in 30 s.
    • DETACH PARTITION on r1 cleared the replication queue on both replicas and stopped the merges. Both replicas listed the parts in system.detached_parts.
    • ATTACH PARTITION on r1 only restored 750000 rows on both replicas, with 750000 distinct keys. r2 logged NewPart and no DownloadPart, so it attached its own detached copies. No detached leftovers remained, and the parts got new block numbers.
    • Within about 60 s, the attached parts were selected for a new merge, which failed with 241 again. max_bytes_to_merge_at_max_space_in_pool = 100 MiB did not prevent it, because the parts were about 5 MiB compressed while the merge needed more than 1 GiB.
    • MODIFY SETTING without ON CLUSTER changed only the replica where it ran; the other replica kept its old setting.
  • Test numbers: the article now shows the later-merge peaks (844 → 120 / 132 MiB).

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant