Repository navigation
Conversation
A state whose data lives outside the arena (anyLast/argMax over FixedString, groupFormat, datasketches, ...) allocates it with operator new, which the memory tracker counts but never refuses. Reading such states did not check the limit, so one remote block of 1 MiB anyLast states read gigabytes past max_memory_usage and the server was OOM-killed. createAndDeserializeBatch now takes the query's memory tracker (nullptr for trivially destructible states, which cannot own memory outside the arena) and checks the limit after each state that moved it, before the column owns the state. The query's tracker is watched rather than the thread's, because a remote read runs in a fiber that another thread of the query can resume. The readers of one state at a time (row formats, text formats and CAST from String, Protobuf) cannot use a window opened at their entry: their callers, sometimes several layers up, read the state's bytes into a String first, and that read is where the untracked memory is flushed. They compare the tracker with the last passing check of the same tracker in this thread. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the doc line of checkMemoryLimitIfChanged, which restates its body. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds, 3 findings, 0 in the second round) plus my own review of the diff.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261023-072900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261023-071200 |
|
cc @azat @antonio2368, could you review this and add the |
|
Workflow [PR], commit [1e331a5] Summary: ✅
AI ReviewSummaryAdds memory-limit checks while deserializing non-trivial Missing context / blind spots
Final VerdictStatus: ✅ Approve based on source review and the supplied regression evidence, subject to the remaining CI checks. |
|
@groeneai Do you mind explaining what allocations exactly aren't being tracked and why we can't switch those sources to be tracked and allowed to throw? |
|
They are tracked, just never refused. Here it is the Why I didn't switch the sources:
Creating states from serialized data is the one point all of them pass through, and the check runs before the column owns the state, so a refusal is clean. If you would rather convert |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes151 object files changed (+1.41 MiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units335 translation units recompiled, 2694 s compile time in total, 335 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.08 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
I measured #129553 (applied to master 39b9f2d, debug build) against this PR's test 05445: the So this PR can be closed once #129553 is merged. 05493 does not cover the single-state readers ( |
I didn't ask, but if you want to help, you can check the performance |
|
It is slower wherever many small states with a non-trivial destructor are read or merged. Local A/B: master 39b9f2d with and without the #129553 diff, RelWithDebInfo,
This matches the PR's Performance Comparison ( "Cheaper check" keeps your call sites and only changes the body, so a thread checks after its own tracked memory changed: void checkMemoryLimitForAggregateFunctionState()
{
/// The amount of the thread tracker changes only when the thread flushes its untracked memory.
static thread_local const MemoryTracker * checked_tracker = nullptr;
static thread_local Int64 checked_amount = 0;
const MemoryTracker * tracker = CurrentThread::getMemoryTracker();
const Int64 amount = tracker ? tracker->get() : 0;
if (tracker == checked_tracker && amount == checked_amount)
return;
CurrentMemoryTracker::check();
checked_tracker = tracker;
checked_amount = amount;
}With it, the Native and RowBinary reads of 3000 1 MiB |
Related: #117916
Related: #127334
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
max_memory_usageandmax_server_memory_usagenot being enforced while readingAggregateFunctionstates that hold large values (for exampleanyLastover a largeFixedString) from distributed queries and fromNative, row, text andProtobufformats. One block of such states could exceed the limit by gigabytes and get the server killed by the OOM killer.Description
AST fuzzer (amd_debug, targeted)on #127334 ended with the server OOM-killed at 43.3 GiB (log) while the initiator receivedanyLastState(FixedString(1048577))rows from six shards undermax_memory_usage = '100M'. This is pre-existing: on 26.8.18.2, aNativeread of 4000 such 1 MiB states withmax_server_memory_usage = 2Gin a 3 GiB container is OOM-killed instead of failing withMEMORY_LIMIT_EXCEEDED.Root cause:
SerializationAggregateFunction::deserializeBinaryBulkfills a block of states with onecreateAndDeserializeBatchcall that never checks the limit. A state whose data lives outside the arena allocates it withoperator new, which the memory tracker counts but does not refuse (#117916). The single-state readers (row formats, text formats andCASTfromString,Protobuf) check nothing either.Change: for functions with non-trivially destructible states,
createAndDeserializeBatchcallsCurrentMemoryTracker::check()after each state that moved the query's memory tracker, before the column owns the state. The single-state readers compare the tracker with the last passing check of the same tracker in the thread, because their callers read the state's bytes into aStringfirst.Validation: the new test fails on master and passes 50/50 with randomized settings. The fuzzer's query now stops at ~96 MiB. RelWithDebInfo read time: bulk within 1.05x,
RowBinaryuniqstates 1.083x,TSV1.031x.Copies of states in
ColumnAggregateFunctionare handled in #127334, which adds a helper of the same shape there; whichever of the two merges second will switch to the other's.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127561]