Repository navigation
Conversation
A window function whose result is an aggregate function state copies a state into every output row (`insertMergeResultInto`, `insertResultInto` of a state-typed result, `lagInFrame` of a state column). With a frame over the whole partition each copy is the whole-partition state, so the query needs rows x state size. `uniqTheta` states are `datasketches` sketches using `std::allocator` and `groupBitmap` states use `malloc`; such allocations are tracked but never refused (`min_allocation_size_to_throw_on_memory_limit` for `operator new`), and `computeReadyRows` computes all ready rows of a block in one `work()` call with no limit check, so nothing stopped the query before it passed both `max_memory_usage` and `max_server_memory_usage`. The AST fuzzer on master got the server OOM-killed this way: a `uniqThetaOrDefaultTupleState(...) OVER (GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)` over 240000 rows from `remote()` reached 14.69 GiB against a 9.31 GiB query limit. When some window function returns a type that can hold aggregate states (`AggregateFunction`, `Dynamic`, `JSON`), check the memory limit after each row whose allocations have reached the current thread's memory tracker, which happens every `max_untracked_memory`. `Aggregator::checkLimits` does the same per block for the same reason. Windows that return no states only pay one branch per row. `ColumnAggregateFunction::ensureOwnership` copies all states a column shares in one loop. `leadInFrame` over a column that repeats one state gets there at the end of a block, so one row could copy a block of states before the window's check. Check the limit in that loop the same way, reading the thread's memory tracker once before the loop. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the sentence narrating the gated check in computeReadyRows and the comment restating the last-check field names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
Internal second-model review (4 rounds): 6 findings agreed and fixed, 0 dismissed
|
Collaborator
Author
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261015-122800 |
Contributor
|
Workflow [PR], commit [6316917] Summary: ❌
|
Collaborator
Author
|
cc @Michicosun @antonio2368, could you review this? A window function that returns an aggregate state copies the state into every output row, and those copies ( |
Member
|
not now |
This was referenced Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: #111892
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed window functions that return aggregate function states (for example
uniqThetaState(x) OVER ()) ignoringmax_memory_usageandmax_server_memory_usage. Each output row gets its own copy of the state, so such a query could use many times the configured limit and get the server killed by the OOM killer.Description
On 26.8.18.2,
clickhouse localrunningSELECT uniqThetaState(number) OVER () FROM numbers(100000) FORMAT Nullis OOM-killed in a 3 GiB container althoughmax_server_memory_usageis 2G, because neither the query nor the server limit is checked while the window writes the states.Found by the AST fuzzer on master (report): a
uniqThetaOrDefaultTupleState(...) OVER (...)over 240000 rows fromremote()reached 14.69 GiB against a 9.31 GiB query limit.Root cause:
WindowTransform::computeReadyRowswrites all ready rows of a block in onework()call, and a window function whose result is a state gives every row its own copy.uniqThetastates usestd::allocatorandgroupBitmapstates usemalloc; such allocations are tracked but never refused, so nothing in that loop stopped the query.Change: when some window function returns a type that can hold an aggregate state (
AggregateFunction, or aDynamic/JSONvalue),WindowTransformchecks the memory limit after each row whose allocations have reached the current thread's memory tracker (everymax_untracked_memory, 4 MiB by default).ColumnAggregateFunction::ensureOwnership, which copies all states a column shares in one loop (reached byleadInFrameandfinalizeAggregation), checks the same way. Cost over 20M rows:countState() OVER (...)+1.0%,count() OVER (...)-0.3%,finalizeAggregationof sharedcountStatestates +1.8 to +3.3%.Validation: the new test fails on master and passes with the fix. On the fuzzer's query shape the initiator now stops at 954 to 957 MiB against a
1Glimit instead of 1.9 to 5.6 GiB, and two such queries keep a server capped at 5.59 GiB at 4.6 to 5.9 GiB peak RSS instead of 15.3 GiB.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126026]