Repository navigation
Conversation
`any`, `anyLast`, `min`, `max`, `argMin`/`argMax` (result and key), `-ArgMin`/`-ArgMax` keys, `anyHeavy` and `singleValueOrNull` kept a `FixedString` value in a `Field`, i.e. in a `std::string` allocated by `operator new`. Such allocations are tracked but never refused by the memory limit, so one block of a `GROUP BY` could add as much unrefused state memory as its (checked) argument column before the end-of-block check in `Aggregator::checkLimits`. With large `FixedString` values the server was OOM-killed by the kernel although `max_memory_usage` and `max_server_memory_usage` were set (AST fuzzer "OOM in dmesg"). `SingleValueDataString` becomes a template on the column type and `FixedString` is dispatched to its second instantiation, so the value lives inline or in the aggregation arena, whose chunks are checked allocations. The serialized state stays what `SingleValueDataGeneric` wrote (a bool flag and the N bytes), so existing states are compatible. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The three refusal lines (`-ArgMax` key, `argMax` key, `max`: one per dispatcher) each aggregate 1024 x FixedString(65535) in one block under a 100M limit. Both binaries refuse them, so the test checks the peak: before the fix every refused query had already allocated the whole block's state (~135 MB of tracked memory) before `Aggregator::checkLimits` refused it at the end of the block; now the arena chunk that would cross the limit is refused (~84 MB). The compatibility lines pin the serialized state format, which stays the one `SingleValueDataGeneric` wrote. 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 runs on the same tree, 1 finding, 0 in the second run) plus my own review of the diff (no blocker or major).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261111-034100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261111-022500 |
|
Workflow [PR], commit [c7d7af0] Summary: ✅
AI ReviewSummaryPR #130682 moves single-value aggregate states over Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes8 object files changed (+404.24 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units15 translation units recompiled, 58 s compile time in total, 15 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.07 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Algunenano
left a comment
There was a problem hiding this comment.
🤖 This fixes the memory limit for FixedString only. Every type that still goes through SingleValueDataGeneric keeps the value in a Field allocated with operator new, and that allocation is never refused. So argMax(x, [big]), argMax(x, tuple(big)), Map, and any/anyLast with RESPECT NULLS should still be able to exceed max_memory_usage and max_server_memory_usage within one block. Instead of adding one specialized state per type, could you remove Field from single-value states altogether? Please prototype and measure these two approaches, preferring the first:
1. Arena-backed state with no Field and no IColumn. Store the value in the aggregation Arena, the same way SingleValueDataString does, so allocations are checked:
- Use
serializeValueIntoArenato store the value anddeserializeAndInsertFromArenato produce the result. - For
min/max/argMin/argMax, also store theserializeAsComparableencoding and compare withmemcmp. Check whether one encoding can serve both purposes (comparable and decodable) so each value is stored only once. - Reuse the existing allocation when the new value fits.
2. One-row column state. Route every type without a dedicated state to the existing SingleValueDataGenericWithColumn and delete SingleValueDataGeneric and canUseFieldForValueData. Column buffers come from the checked Allocator. This is the fallback if approach 1 is slower or can't keep the current semantics.
For both, please verify:
- Serialized states are byte-identical to the current ones (bool flag plus
serializeBinary) forArray,Tuple,Map,Nullable,LowCardinality,Decimal,UUID,IPv6and nested combinations. Write states with the old binary and read them with the new one, and also check across versions throughremote. min/maxordering matches the currentFieldcomparison, including NaN insideArray/Tuple,NULLinsideTuple/Array, and empty arrays. This is the most likely place for either approach to diverge fromField::operator<.- The memory limit is enforced with the large values wrapped in
Array,TupleandMap, not onlyFixedString.
Please measure both approaches against master with one thread and with the default thread count:
min/max/any/argMaxoverArray(UInt64),Tuple(UInt64, String),Array(String)andMap(String, UInt64), with small and large values, and with few and many groups.- The common case where a group's value is replaced often (for example, ascending input for
max), since that drives the reallocation cost. - Peak memory per group, not only time.
- A control showing
Stringand the numeric types are unchanged.
If approach 1 keeps the current semantics and is not slower, a separate FixedString state is no longer needed.
|
Acknowledged. I'll prototype both approaches on this PR, starting with the arena-backed state, and post the results for each point you listed here (state compatibility, |
|
Results for both prototypes against master
State compatibility. A1 writes the same bytes as master in 237 (function, type) cases ( Ordering. A1 matches master on 780 queries: 41 value sets (NaN, -0 and Memory limit. One correction: numeric Time and peak memory per group, prototype / master: 306 rows (
Where A1 loses:
The A1 diff is +1038/-209 lines in Neither prototype keeps the current semantics without being slower somewhere. Which should I push?
In every option the |
|
Need to think several times about this. I don't like neither of the approaches |
Related: #127561
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
max_memory_usageandmax_server_memory_usagenow also limit theFixedStringvalues held bymin,max,any,anyLast(withoutRESPECT NULLS),argMin,argMax,anyHeavy,singleValueOrNulland the-ArgMin/-ArgMaxcombinators. Before, aGROUP BYover largeFixedStringvalues could exceed both limits until the server was killed by the OOM killer.Description
AST fuzzer
OOM in dmesgon two unrelated PRs: #124320 (countIfArgMax(big, toFixedString('(a|b)*', 65535)) ... GROUP BY) and #130269 (sumDistinctArgMax(b, toFixedString(.., 1048575)) ... GROUP BY). In both, memory grew pastmax_memory_usageandmax_server_memory_usageuntil the kernel killed the server.On 26.10.2.103,
SELECT sum(a + b + c) FROM (SELECT number AS k, countArgMax(f) AS a, countArgMin(f) AS b, argMax(number, f) AS c FROM (SELECT number, toFixedString(toString(materialize(1)), 1048575) AS f FROM numbers(1024)) GROUP BY k) SETTINGS max_threads = 1, max_block_size = 1024is OOM-killed in a 3 GB container althoughmax_server_memory_usageis 2.5 GB.Single-value states kept a
FixedStringin aField, astd::stringfromoperator new, which the memory tracker counts but never refuses.Aggregator::checkLimitsrefuses only at the end of a block, so one block's state can add its argument column's size again.SingleValueDataStringbecomes a template on the column type, andFixedStringvalues now live inline or in the aggregation arena, whose chunks are checked allocations. The serialized state is unchanged (flag + N bytes; checked against the old binary, also across versions throughremote). This is the source switch asked about on #127561 (ananyLast(FixedString)state there); it also covers the add path, which the checks after reading or copying states (#127561, #129553) do not.any/anyLastwithRESPECT NULLShave their ownFieldstate for every type,Stringincluded, and are not changed here.Validation: the refused queries of the new test peak at 84.2 MB under the 100 MB limit (50 randomized runs; 134.6 MB on master); the query above is refused with
MEMORY_LIMIT_EXCEEDED. Local RelWithDebInfo, one thread:maxoverFixedString(16), 50M rows, 1000 groups, 0.68 s -> 0.29 s;Stringunchanged.Workflow [PR]
Sync PR [sync-upstream/pr/130682]