Repository navigation
Conversation
A `ColumnAggregateFunction` can reference states it does not own. When it must own them it deep-copies each one (`create` + `merge`), either all at once in `ensureOwnership` or row by row in the forced-ownership loop of `insertRangeFrom`. A constant source turns one state into N copies, and for `anyLast` / `argMax` over a `FixedString` each copy is a `Field` allocated with `operator new`, which the memory tracker counts but never refuses. Nothing in either loop checked the limit, so `neighbor(<1 MiB state>, 10)` over `numbers(2147483647)` copied a whole block of states and the AST fuzzer server was OOM-killed at 45 GB RSS while the server tracker was already refusing other allocations. `neighbor(S, -10)` takes the second loop (the defaults come first and set forced ownership), as do `lagInFrame` and per-row `insertFrom`. In both loops, check the limit after each copied state whose allocations reached the current thread's memory tracker, reading the tracker once before the loop. Inside a `MemoryTrackerBlockerInThread` (e.g. a `Buffer` insert) the blocked levels do not move, so the checker watches the first level that does. `insert(Field)` and `tryInsert` (how `lagInFrame` copies its default into every row outside the frame) check once per state that can own memory outside the arena: part of what they copy, the `Field`, is allocated by the caller, so a check gated on the tracker moving during the call can miss it. The overshoot is bounded by `max_untracked_memory` plus one state. 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; 3 rounds, 4 findings, 0 in the last round) plus my own review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261022-055500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261022-052000 |
|
cc @antonio2368 @Avogar, could you review this and add the |
|
Workflow [PR], commit [e5c83c6] Summary: ❌
AI ReviewSummaryThis PR closes the unchecked-copy hole in Final VerdictStatus: ✅ Approve |
…pointer
`ColumnAggregateFunction::insertFrom(ConstAggregateDataPtr)` creates a new
state and merges the given one into it. Aggregate function state arithmetic
(`executeAggregateAddition`, one call per row) and every `-State` result
inserted through `insertMergeResultInto` (window functions, `arrayReduce`,
`initializeAggregation`, `runningAccumulate`) use it, so a shared state is
copied once per row, with `operator new` allocations that the memory tracker
counts but does not refuse. Before this change,
`SELECT materialize(S) + S FROM numbers(300)`, with `S` an `argMaxState` over
a 1 MiB `FixedString`, succeeded at `max_memory_usage = '100M'` with 545 MB
RSS, and so did `uniqThetaState(number) OVER ()` over 3800 rows at 180M.
The check runs before the copy, so a refusal leaves the column unchanged. It
compares with the tracker value seen by the previous call on the same thread,
not with the value at the start of the call: in
`materialize(argMaxState(0, tuple(''))) + argMaxState(1, tuple(<1 MB String>))`
the copies are made by `insertMergeFrom` of the right-hand state after
`insertFrom` returned, so a per-call comparison never sees them (that query
still succeeded at 542 MB RSS with one). An unconditional
`CurrentMemoryTracker::check()` per call was 2.8x slower on a cheap
`anyLastState(String)` addition in a debug build. `insertMergeFrom` itself is
not checked: the merging algorithms and `TotalsHavingTransform` merge every
row into one state, which does not multiply it.
RelWithDebInfo, 11 interleaved runs, medians: `materialize(countState) +
countState` over 50M rows +8% (0.9 ns per row), the same with
`anyLastState(String)` +11.5% (1.8 ns), `countState() OVER (ORDER BY number
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)` over 20M rows +5.4%, the same with
`anyLastState(String)` +3.5%.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline. Translation units without a recent master baseline:
|
CI finish ledger - e5c83c6Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261023-040008 |
|
Fixing PR for the |
|
I measured #129553 (applied to master 39b9f2d, debug build) against the repros of this PR. Every one that still reproduces on master is refused:
So this PR can be closed once #129553 is merged. 05493 covers the remote read and |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Queries that copy aggregate function states shared by a column (
finalizeAggregation(materialize(<state>)),materialize(<state>) + <state>, window functions returning states,neighbororlagInFrameover a state) now respectmax_memory_usageandmax_server_memory_usageand fail withMEMORY_LIMIT_EXCEEDED, instead of copying past every limit until the server is OOM-killed.Description
Related: #117916
Reported on #108090: AST fuzzer (amd_debug) OOM-killed the server at 45 GB RSS with a fuzzed
neighborover ananyLastStateof a 1 MiBFixedString.On 26.8.18.2 and 26.9.19.9,
SELECT finalizeAggregation(materialize(initializeAggregation('argMaxState', 1, toFixedString('x', 1048577)))) FROM numbers(4000)is OOM-killed in a 3 GiB container withmax_server_memory_usage = 2G.A
ColumnAggregateFunctionthat must own the states it references copies each one (inensureOwnership, the forced-ownership loop ofinsertRangeFrom, andinsertFrom(ConstAggregateDataPtr), used by state arithmetic and window functions returning states), so one shared state becomes N copies. ForanyLastorargMaxover aFixedString, each copy allocates aFieldwithoperator new, which the memory tracker counts but does not refuse; none of these paths checked the limit.Both loops now call
CurrentMemoryTracker::check()after each copied state that moved the memory tracker (overshoot:max_untracked_memoryplus one state per thread).insertFrom(ConstAggregateDataPtr)checks before each copy if the tracker moved since the previous call on the thread, which also catches the right operand's merge after it.insert(Field)(howlagInFramecopies its default) checks after every state with a non-trivial destructor. This also refuses the window function repro of #126026, without itsWindowTransformchange.Cost: 1-3 ns per state copied one per call (+8-19% on micro-benchmarks such as
materialize(countState) + countState), 0-4% when one call copies many states.The new test fails on master and passes with the fix (tagged
no-asan, no-tsan, no-msan: sanitizers do not track these copies). For theBufferpath, where onlymax_server_memory_usageapplies, anINSERTcopies 4.27 GB on master and the fix refuses it at 2.2 GB under 2G.Workflow [PR]
Sync PR [sync-upstream/pr/127334]