Skip to content

Cherry pick #132557 to 26.8: Fix a memory leak of min/max states built for _minmax_count_projection - #132775

Merged
robot-clickhouse merged 4 commits into
backport/26.8/132557from
cherrypick/26.8/132557
Nov 19, 2026
Merged

robot-clickhouse merged 4 commits into
backport/26.8/132557from
cherrypick/26.8/132557

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #132557

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 3 commits November 18, 2026 01:07
`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]>
`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]>
…state-leak

Fix a memory leak of `min`/`max` states built for `_minmax_count_projection`
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Nov 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

The only conflict is in optimizeUseAggregateProjection.cpp: this PR also changes makeBlockFromStatistics, which comes from #115738 and does not exist on 26.8, so I kept the 26.8 version of that file. MergeTreeData.cpp and the test merge cleanly, and the getMinMaxCountProjectionBlock change is identical to master's.

On a 26.8 ASan build, 05828_minmax_count_projection_state_leak passes 3 of 3 runs. The same build without the MergeTreeData.cpp change fails it with one leak report every time. The existing _minmax_count_projection tests (01710_minmax_count_projection, 01748_partition_id_pruning, 04068_lwd_minmax_projection_recovery and others) give the same output on both builds.

I cannot push to cherrypick/26.8/132557. The branch on my fork has this PR's head 82024f19538c as an ancestor, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git resolve-26.8-cherrypick-132557
git push origin FETCH_HEAD:cherrypick/26.8/132557

…2557/cherrypick/26.8/132557

# Conflicts:
#	src/Processors/QueryPlan/Optimizations/optimizeUseAggregateProjection.cpp
@robot-clickhouse
robot-clickhouse merged commit 1ee3d6a into backport/26.8/132557 Nov 19, 2026
1 check passed
@robot-clickhouse
robot-clickhouse deleted the cherrypick/26.8/132557 branch November 19, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants