Your ClickHouse® server runs analytical queries and background merges at the same time. When large merges or mutations consume too much memory, queries can fail with MEMORY_LIMIT_EXCEEDED errors.
In this article, we explore why this happens, how to investigate memory usage, and how to configure background memory limits without allowing merge operations to fall behind.
Why Do Merges and Mutations Consume So Much Memory?
ClickHouse uses background operations to maintain MergeTree tables.
- Inserts create new data parts.
- Background merges combine smaller parts into larger ones.
- Mutations rewrite existing parts to perform updates or deletes.
- Analytical queries consume memory for filtering, sorting, aggregation, and joins.
All these operations share the server's available memory.
When several large merges run while queries are already consuming substantial memory, the server can experience memory pressure. Query latency may increase before failures begin.
This is particularly common during heavy insert workloads or when large mutations run on busy tables.
Before reducing query concurrency or adding RAM, investigate whether background merges and mutations are contributing to the problem.
How to Confirm the Problem
1. Inspect running merges and mutations
The system.merges table provides information about active merges and mutations on MergeTree tables. Its memory_usage column reports memory usage for each running operation.
Run the following query:
SELECT database, table, elapsed, progress, num_parts, formatReadableSize(memory_usage) AS memory_used FROM system.merges ORDER BY memory_usage DESC; Review the operations consuming the most memory.
Look for multiple memory-intensive operations running simultaneously, particularly when query failures occur.
Remember that system.merges shows current operations rather than a complete history. If the query returns no rows, try again during the period when failures occur.
2. Inspect background memory settings
Check the current values of the relevant server settings:
SELECT name, value, changed, description FROM system.server_settings WHERE name IN ( 'merges_mutations_memory_usage_soft_limit', 'merges_mutations_memory_usage_to_ram_ratio' ) ORDER BY name; Review the values and whether an administrator has changed them. Available columns can vary between ClickHouse versions, so verify the query against your target release.
You can also inspect the server's overall memory limit settings:
SELECT name, value, changed FROM system.server_settings WHERE name IN ( 'max_server_memory_usage', 'max_server_memory_usage_to_ram_ratio' ) ORDER BY name; These settings govern the server's overall memory limit and are distinct from the background merge and mutation memory limit.
If background operations show little memory usage, investigate query memory limits, concurrent queries, overall server memory consumption, and operating system memory pressure.
Understanding the Two Settings
The following defaults are based on the ClickHouse master commit 9f79cc1, dated October 30, 2026. Verify the values against your target release before deployment.
| Setting | Default | Purpose |
|---|---|---|
merges_mutations_memory_usage_soft_limit | 0 | Sets the memory limit for background merges and mutations. A value of 0 selects the ratio-based limit. |
merges_mutations_memory_usage_to_ram_ratio | 0.5 | Determines the default background memory limit as a fraction of physical memory. |
Both settings apply to the server as a whole, not to individual queries.
merges_mutations_memory_usage_soft_limit
This setting controls the memory budget for background merges and mutations.
A value of 0 tells ClickHouse to calculate the limit using merges_mutations_memory_usage_to_ram_ratio.
Alternatively, you can specify an explicit limit in bytes. For example, 8589934592 represents 8 GiB.
When background operations reach the configured limit, ClickHouse stops scheduling new merges and mutations of these types. Operations that have already started continue running.
merges_mutations_memory_usage_to_ram_ratio
This setting determines the default background memory limit as a fraction of physical memory.
The supplied default is 0.5, or 50%.
For example, if the relevant physical memory amount is 64 GiB, a ratio of 0.5 produces a limit of approximately 32 GiB.
Lowering the ratio reduces the memory budget for background operations. This can leave more memory available for queries, but it can also reduce merge throughput.
Important: Both settings require a server restart according to the configuration described in this article.
How to Fix the Problem
Start with a measured change rather than setting an aggressive limit immediately.
Change 1: Lower the background memory ratio
If background operations consume too much memory, reducing the ratio is one way to give queries more headroom.
Create the configuration file:
/etc/clickhouse-server/config.d/merges_mutations_memory.xml
For example, start by setting the ratio to 0.3:
<clickhouse> <merges_mutations_memory_usage_to_ram_ratio>0.3</merges_mutations_memory_usage_to_ram_ratio> </clickhouse> This reduces the background memory budget from 50% to 30% of the relevant physical memory amount.
For a server with 64 GiB of relevant physical memory, the calculated limit would be approximately 19.2 GiB.
Treat this as an example, not a universal recommendation. The appropriate value depends on your workload, available RAM, and query memory requirements.
Restart required: Yes. Plan a controlled restart and monitor the workload afterward.
Change 2: Set an explicit background memory limit
An explicit limit can be useful when you need a fixed memory budget.
For example, to set a limit of 16 GiB:
<clickhouse> <merges_mutations_memory_usage_soft_limit>17179869184</merges_mutations_memory_usage_soft_limit> </clickhouse> The value 17179869184 represents 16 GiB.
Choose the limit based on available RAM, query memory requirements, and observed merge memory usage.
Before deployment, verify how your target ClickHouse version applies the explicit limit and the ratio-based limit. Do not assume that the explicit limit overrides the ratio in every case.
Restart required: Yes.
Which Change Should You Use?
For many production incidents, lowering the background memory ratio is a reasonable starting point because it provides a straightforward way to reduce the memory budget for background operations.
Use an explicit limit when you need a specific byte-based budget and have verified how your target version applies both settings.
Avoid changing both settings simultaneously unless you understand their interaction. Otherwise, it becomes harder to determine which change improved or worsened the workload.
After restarting, inspect the effective settings and monitor memory usage during a representative workload.
How to Verify the Fix
A successful change should reduce memory pressure without allowing active data parts to grow continuously.
1. Measure running background operations
Run the following query after applying the configuration change:
SELECT database, table, count() AS running_operations, formatReadableSize(sum(memory_usage)) AS total_memory_used, formatReadableSize(max(memory_usage)) AS largest_operation FROM system.merges GROUP BY database, table ORDER BY sum(memory_usage) DESC; Compare these results with measurements taken before the change under a similar workload.
2. Check query failures and latency
Review application logs and ClickHouse query logs for recurring memory errors.
Monitor query performance as well. The objective is to reduce memory-related failures without causing unacceptable query latency.
3. Monitor active data parts
Reducing the memory budget too much can prevent background merges from keeping up with incoming inserts.
Use this query to inspect active data parts:
SELECT database, table, count() AS active_parts FROM system.parts WHERE active GROUP BY database, table ORDER BY active_parts DESC LIMIT 20; Compare results over time. A single snapshot cannot tell you whether part counts are increasing.
If part counts remain stable and queries stop failing, the change may be working as intended.
If part counts continue to increase, merges may not be keeping up with inserts. Reassess the memory limit and merge workload before making further changes.
Risks and Trade-Offs
A background memory limit that is too low can create additional problems.
When ClickHouse cannot schedule enough background merges, new inserts may create parts faster than existing parts can be combined. This can cause active part counts to rise and query performance to deteriorate.
In some cases, the problem can lead to TOO_MANY_PARTS insert errors.
Mutations can also take longer to finish, delaying updates or deletes that depend on them.
Keep these trade-offs in mind:
- A lower ratio gives queries more memory headroom but may reduce merge throughput.
- Running merges continue after ClickHouse reaches the soft limit.
- The soft limit does not guarantee that total server memory usage stays below a fixed value.
- Large mutations may continue using substantial memory after new operations stop being scheduled.
- Active part counts and mutation progress require ongoing monitoring.
If queries continue to fail after lowering the ratio, investigate query memory limits and overall server memory usage rather than continually reducing the background memory budget.
Conclusion
Background merges and mutations share server memory with analytical queries. When they consume too much memory, queries can fail even while the server remains available.
To investigate and address the problem:
- Use
system.mergesand itsmemory_usagecolumn to identify memory-intensive operations. - Inspect the background memory settings and overall server memory limits.
- Consider lowering
merges_mutations_memory_usage_to_ram_ratiowhen background operations create excessive memory pressure. - Restart ClickHouse after changing either setting, as described in this article.
- Monitor query failures, query latency, active data parts, and mutation progress.
The goal is not simply to reduce background memory usage. It is to balance query reliability with healthy merge and mutation progress.
References
- ClickHouse server settings source:
ServerSettings.cpp. Verify setting definitions and defaults against your target version. - ClickHouse documentation: System tables.
- ClickHouse documentation:
system.merges. - ClickHouse documentation: Server configuration parameters.

