Repository navigation
Fix a memory leak of min/max states built for _minmax_count_projection - #132557
Algunenano merged 2 commits into
Conversation
`MergeTreeData::getMinMaxCountProjectionBlock` built each min/max/count state in a temporary place (arena alloc + `create` + `add`) and then called `ColumnAggregateFunction::insertFrom(place)`. That overload does not take ownership: it creates a new column-owned state and merges `place` into it, so `place` was never destroyed. For a `min`/`max` state that owns heap memory (`SingleValueDataGeneric` holding a `Tuple`, `Array` or long `FixedString` field of the first primary key column or of a min-max partition column) every part read leaked one state, which LeakSanitizer reported in a stress run (STID 1830-4545). Build the state inside the column instead (`insertDefault` and fill `getData().back()`, as `FunctionsConversion.cpp` does), so the column owns it from creation and destroys it on every path, including the early returns and an exception from `add`. Results are unchanged: merging into a default state yields the same state. The new test runs the query in a short-lived `clickhouse-local` and counts LeakSanitizer reports in its own log; the count is 0 on builds without a leak sanitizer, and an EXPLAIN line asserts the projection was used. 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; 0 findings), plus my own cold review (1 nit, no code change).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261118-011200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261118-002500 |
|
Workflow [PR], commit [2af1cae] Summary: ✅
AI ReviewSummaryThe change fixes leaked Missing context / blind spots
Final VerdictStatus: ✅ Approve |
CI finish ledger - 0bc213cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Both Session id: cron:our-pr-ci-monitor:20261118-053020 |
Algunenano
left a comment
There was a problem hiding this comment.
LGTM. I checked other places using arena.alignedAlloc and ColumnAggregateFunction::insertFrom(place) and insertMergeFrom(place) and they are safe, but src/Processors/QueryPlan/Optimizations/optimizeUseAggregateProjection.cpp:1224 doesn't leak today but pure luck (the states don't use heap memory).
Could you address it in this PR too please?
`makeBlockFromStatistics` had the same shape as the `_minmax_count_projection` builder fixed in the previous commit: it allocated each state in the column's arena, filled it, and passed it to `ColumnAggregateFunction::insertFrom(place)`, which merges `place` into a new column-owned state and never destroys `place`. It does not leak today only because the states it builds have no heap memory: `count`, `min`/`max` of a type whose value is a number (`canStatisticsTrackMinMax`), and `sum`/`avg`/`sumCount` of an integer or `Decimal`. Use `insertDefault` and fill `getData().back()` here too, so the column owns the state from creation and destroys it on every path, including an exception from `add`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Done in 2af1caea3ab7: I did not add a test, because those states hold no heap memory and nothing leaks there today. The existing aggregation-from-statistics tests ( |
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, 76 s compile time in total, 8 of them have a recent master baseline. |
Backport #132557 to 26.10: Fix a memory leak of `min`/`max` states built for `_minmax_count_projection`
Backport #132557 to 26.9: Fix a memory leak of `min`/`max` states built for `_minmax_count_projection`
Cherry pick #132557 to 26.3: Fix a memory leak of `min`/`max` states built for `_minmax_count_projection`
…lt for `_minmax_count_projection`
Cherry pick #132557 to 26.8: Fix a memory leak of `min`/`max` states built for `_minmax_count_projection`
…lt for `_minmax_count_projection`
Backport #132557 to 26.8: Fix a memory leak of `min`/`max` states built for `_minmax_count_projection`
Backport #132557 to 26.3: Fix a memory leak of `min`/`max` states built for `_minmax_count_projection`
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a memory leak in queries answered by
_minmax_count_projectionthat computeminormaxof aTuple,ArrayorFixedStringprimary key or partition column: every part read leaked one aggregate state.Description
Related: Stress test (arm_asan_ubsan, s3),
Sanitizer (STID: 1830-4545)(LeakSanitizer,SingleValueDataGeneric::set<-MergeTreeData::getMinMaxCountProjectionBlock). No related open issue found.On 26.3.63.123, 2000 runs of
SELECT max(k)over a two-partMergeTreetable ordered byk Tuple(UInt64, String)(64 KiB strings) raiseMemoryTrackingby 280 MB, versus 1.4 MB withoptimize_use_implicit_projections = 0. The memory is never freed.Root cause:
getMinMaxCountProjectionBlockbuilt each state in a temporary place (create+add) and passed it toColumnAggregateFunction::insertFrom(place). That overload creates its own state and mergesplaceinto it, and nothing destroyedplace.SingleValueDataGenerickeeps a heap-owningFieldforTuple,Arrayor long string values, so each such state leaked.Change: create the state in the column (
insertDefault, then fillgetData().back()), so the column owns it on every path, including the early returns and an exception fromadd. Results are unchanged. The block builder for aggregation from column statistics (makeBlockFromStatisticsinoptimizeUseAggregateProjection.cpp) had the same pattern and gets the same change. It does not leak today only because the states it builds (count, numericmin/max, integer orDecimalsum/avg) own no heap memory.Validation: the new test counts LeakSanitizer reports of a short-lived
clickhouse-local, and it fails on the unfixed ASan master build (0->1). The fixed ASan build reports no leak forTuple,Array,FixedStringandNullable(FixedString)keys or aFixedStringpartition column, the existing minmax_count projection tests pass on debug and ASan, the aggregation-from-statistics tests pass on debug, and the new test passes 50/50 in repeated runs.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132557]
Version info
26.11.1.2767-master(included in26.11and later)26.10.7.13,26.9.33.11,26.8.41.16,26.3.67.26