Skip to content

Add aggregation memory tracker for two-level conversion - #107490

Merged
m-selmi merged 10 commits into
ClickHouse:masterfrom
m-selmi:fix-aggregator-memory-usage-accounting
Jul 1, 2026
Merged

m-selmi merged 10 commits into
ClickHouse:masterfrom
m-selmi:fix-aggregator-memory-usage-accounting

Conversation

@m-selmi

@m-selmi m-selmi commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

The Aggregator used current_memory_usage - memory_usage_before_aggregation to decide whether to switch to two-level aggregation or not. Since memory_usage_before_aggregation was recorded at construction time it reflected the memory usage during pipeline build, so the difference to current_memory_usage could include more than the aggregation memory (e.g. memory held by a join hash table lower in the pipeline). This leads to converting small aggregations to two-level.

This PR attempts to attach a memory tracker in the Aggregator to account only for the aggregation state.

image image

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Avoid converting single-level aggregations with small memory usage to two-level aggregations by attaching a memory tracker for the aggregation state instead of using whole query memory.

Version info

  • Merged into: 26.7.1.337 (included in 26.7 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a4bec74]

Summary: ✅


AI Review

Summary

This PR scopes the two-level aggregation conversion decision to memory attributed to non-merge aggregation work, keeps the merge path on query-memory delta accounting where input aggregate states may already own memory, and adds a profile event plus focused tests for the false-positive conversion case. I did not find any remaining blocker or major issue in the current diff; the previously open fallback-baseline bot thread is fixed in the current code and has been resolved.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 15, 2026
@m-selmi m-selmi added the can be tested Allows running workflows for external contributors label Jun 15, 2026
Comment thread src/Interpreters/Aggregator.cpp Resolved
Comment thread src/Interpreters/Aggregator.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 3 query(s) improved out of 38 analysed

This PR changes the runtime decision for converting aggregation hash tables to two-level so it is driven by the aggregation's own state memory rather than total query memory. ClickBench Q4, Q15, and Q28 are all aggregation/GROUP BY queries and improved by -16%, -19.6%, and -6% respectively, each consistent across both tests with low measurement noise. These improvements map directly onto the path the PR modifies: avoiding unnecessary two-level conversion for aggregations whose own state is small, so the verdicts are well-supported by the change.

clickbench

🟢 3 improved

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 4 improvement 262 220 -16.0% <0.0001 aggregation: Aggregation query; PR's two-level conversion fix avoids needless conversion. -16% confirmed by both tests.
🟢 15 improvement 245 197 -19.6% <0.0001 aggregation: GROUP BY aggregation; same two-level decision fix avoids unnecessary conversion overhead. -19.6% clear shift.
🟢 28 improvement 6671 6270 -6.0% <0.0001 aggregation: Heavy GROUP BY; smaller -6% gain, consistent with reduced two-level conversion overhead.

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 6e21e452-fd64-401c-91d6-dd8f30157b4d
  • MIRAI run: fea3b729-303d-4855-a632-416fa437430f
  • PR check IDs:
    • clickbench_485969_1782818453
    • clickbench_485976_1782818453
    • clickbench_485986_1782818453
    • tpch_adapted_1_official_485993_1782818453
    • tpch_adapted_1_official_486019_1782818453

@m-selmi
m-selmi marked this pull request as ready for review June 17, 2026 15:53
@m-selmi
m-selmi requested review from nickitat and nihalzp and removed request for nihalzp June 17, 2026 15:54
@nickitat nickitat self-assigned this Jun 18, 2026

@nickitat nickitat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually, IMO, it is right for the aggregation to track only its own memory, because the description of the group_by_two_level_threshold_bytes implies that quite unambiguously.
I'd explicitly ensured that the conversion on the merge stage is still working properly. Let's add a test that ensures all pre-aggregation tables remain single-level, are converted only during merge, and that it still happens.

Comment thread src/Interpreters/Aggregator.cpp Resolved
Comment thread src/Interpreters/Aggregator.cpp Resolved
Comment thread src/Interpreters/Aggregator.cpp Resolved
Comment thread src/Interpreters/Aggregator.cpp Resolved
Comment thread src/Interpreters/Aggregator.cpp Resolved
@m-selmi

m-selmi commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

I'd explicitly ensured that the conversion on the merge stage is still working properly. Let's add a test that ensures all pre-aggregation tables remain single-level, are converted only during merge, and that it still happens.

Seems like conversion during merge only triggers if at least one of the tables was converted during execution, so this case shouldn't happen, is that what you meant?

@nickitat nickitat added the memory When memory usage is higher than expected label Jun 26, 2026
@nickitat

Copy link
Copy Markdown
Member

I'd explicitly ensured that the conversion on the merge stage is still working properly. Let's add a test that ensures all pre-aggregation tables remain single-level, are converted only during merge, and that it still happens.

Seems like conversion during merge only triggers if at least one of the tables was converted during execution, so this case shouldn't happen, is that what you meant?

Yeah, I confused myself here.

Comment thread src/Interpreters/Aggregator.cpp Resolved

@nickitat nickitat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM apart from one comment

@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.70% 92.60% -0.10%
Branches 77.60% 77.70% +0.10%

Changed lines: Changed C/C++ lines covered: 37/39 (94.87%) · Uncovered code

Full report · Diff report

@m-selmi
m-selmi added this pull request to the merge queue Jul 1, 2026
Merged via the queue into ClickHouse:master with commit 8fa9200 Jul 1, 2026
174 checks passed
@m-selmi
m-selmi deleted the fix-aggregator-memory-usage-accounting branch July 1, 2026 08:02
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 1, 2026
alexey-milovidov pushed a commit to rorylshanks/ClickHouse that referenced this pull request Aug 15, 2026
…reVariantsToMerge

When some per-thread aggregation states converted themselves to two-level
mid-scan (after the accurate aggregation memory accounting from ClickHouse#107490
made `group_by_two_level_threshold_bytes` engage for queries with few
groups but heavy states, e.g. uniqExact), the merge preparation converted
all the remaining single-level variants sequentially on one thread.
Initializing a two-level variant allocates and zeroes 256 bucket tables,
and there is one variant per query thread, so on machines with many cores
this serial loop dominates short aggregation queries: ClickBench Q10/Q11
on c7a.metal-48xl (192 cores) regressed 2-3x. Convert the variants in
parallel on the aggregator thread pool instead.

Bisected and validated on c7a.metal-48xl via ClickBench manual runs:
ClickHouse/ClickBench#1360

Closes: ClickHouse#114640
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ry-usage-accounting

Add aggregation memory tracker for two-level conversion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors memory When memory usage is higher than expected pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants