Server rejects all queries for minutes: the usage side of the total memory limit check freezes at the previous peak while the limit keeps updating
Company or project name
ClickHouse
Describe what's wrong
After a large query, the usage side of the server-wide memory check froze at the peak
value and stopped following actual memory, while the limit side kept being recomputed.
The server then rejected 378 consecutive queries over ~3 minutes — including
SELECT 1 — with MEMORY_LIMIT_EXCEEDED, before recovering on its own.
Throughout the window:
- the reported usage was constant at exactly
38.45 GiBin all 378 messages; - actual process RSS was ~7.4 GiB (reported in the same message);
system.metrics.MemoryTrackingread ~92 MiB when queried during the window (some queries did get through and answered this);- the
maximumin the same message varied continuously — 27.91, 32.74, 33.07 … 37.95 GiB — i.e.memory_worker_dynamic_hard_limitwas actively recomputing the limit, so the memory worker thread was running. Only the usage side was stale.
38.45 GiB is approximately the peak of the query that ran immediately before the window
(a hash join over 100M-row tables). So the value the hard-limit check consults appears to
have been pinned at that peak and never refreshed, even though RSS had dropped to 7.4 GiB
and the tracker itself reported 92 MiB.
Sample messages (verbatim, differing only in maximum and the RSS decimals):
(total) memory limit exceeded: would use 38.45 GiB (attempt to allocate chunk of 0.00 B), current RSS: 7.43 GiB, maximum: 35.31 GiB. Untracked memory across all threads: -5.40 MiB.
(total) memory limit exceeded: would use 38.45 GiB (attempt to allocate chunk of 0.00 B), current RSS: 7.40 GiB, maximum: 27.91 GiB. Untracked memory across all threads: -7.45 MiB.Note attempt to allocate chunk of 0.00 B — even zero-byte allocations were rejected.
Background merges failed with the same message
(MergePlainMergeTreeTask::executeStep(): Exception is in merge_task.).
Does it reproduce on the most recent release?
Seen on 26.9.1.1 built from the branch of
https://github.com/ClickHouse/ClickHouse/pull/112973 (8f9da505241b55f67b6c78990a2feae864e06c4d).
The PR only adds join_algorithm values and is very unlikely to be related, but I have
not verified against a plain master build.
How to reproduce
I could not reduce this to a minimal reproduction — reporting it because the symptom is severe (server effectively refuses all work) and the numbers above are specific enough to point at a code path.
Conditions when it happened:
- Server settings: default
allow_use_jemalloc_memory = 1,memory_worker_period_ms = 0,memory_worker_dynamic_hard_limit = 1,memory_worker_correct_memory_tracker = 0;max_server_memory_usage = 60 GiBconfigured,max_memory_usage = 32 GiBper query. - Host: 96-core aarch64, 185 GB RAM, heavily shared — other processes were taking and releasing tens of GB, so the dynamic hard limit was moving constantly (that is the 27.91 → 37.95 GiB range above).
- Workload: a benchmark loop issuing joins over 100M-row tables back to back, with background merges running. The query immediately before the window was a hash join that hit the per-query limit at ~38 GiB.
- Recovery: no intervention; it cleared after ~3 minutes. A restart also clears it.
What I tried afterwards, without reproducing it (all on the same binary and data):
- A single query driven past
max_server_memory_usage(20 GiB cap) — subsequentSELECT 1all succeeded. - Peaking at ~40 GiB, then lowering
max_server_memory_usageto 4 GiB viaSYSTEM RELOAD CONFIGto imitate the dynamic limit collapsing below the retained amount — RSS dropped 6.9 GiB → 0.63 GiB on reload (jemalloc pages were purged correctly) and queries kept working.
So it seems to need the combination of sustained load, concurrent merges, and a hard limit that is moving because the host's free memory is moving.
Expected behavior
The usage side of the hard-limit check should track actual memory, or at least be refreshed on the same ticks that recompute the limit. A stale peak should not be able to outlive the memory that produced it and lock out every subsequent query, and a zero-byte allocation should not fail.
Additional context
Found while running the "Trash Schema Benchmark" from https://github.com/ClickHouse/ClickHouse/issues/109426. It cost a benchmark run: every measurement taken during the window failed, which is how it was noticed.
Source: ClickHouse/ClickHouse