Skip to content

Give 00084_external_aggregation real memory headroom - #112054

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-00084-external-aggregation-headroom
Jul 28, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-00084-external-aggregation-headroom

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Fixes the recurring MEMORY_LIMIT_EXCEEDED flakiness of 00084_external_aggregation, e.g. this MasterCI run:

Code: 241. DB::Exception: Query memory limit exceeded: would use 316.72 MiB
(attempt to allocate chunk of 1.75 MiB), maximum: 286.10 MiB.
Untracked memory across all threads: -9.28 MiB: While executing AggregatingTransform.

29 failures over 2026-07-26 and 2026-07-27 across amd_tsan, arm_binary, amd_debug, amd_msan and amd_asan_ubsan, always on the second query and always with code 241. The settings randomizer is not the trigger: CI's own diagnosis reruns the same randomized settings 16-22 times and they all pass.

What is going on

The second query spills at 200 MB (max_bytes_before_external_group_by) and was capped at 300000000 (286.10 MiB) by max_memory_usage. Measured against test.hits on an idle machine, over 10 runs of the query pair on a freshly started server, the second query peaks at 258-290 MiB - 90-97% of its own limit. There is essentially no headroom, so anything the query allocates on top of the aggregation state crosses the limit.

One reproducible source of that extra memory is the aggregation hash-table size statistics. Once the statistics cache holds an entry for this aggregation, collect_hash_table_stats_during_aggregation preallocates the hash tables and the peak jumps by more than 100 MiB:

server state AggregationPreallocatedElementsInHashTables peak of the second query test at 300000000
fresh (10 runs) 0 258-278 MiB 10/10 pass
statistics cache populated (5 runs) 2712170 362-387 MiB 3/3 fail

In the second state the current test fails on every single run with exactly the CI message:

Query memory limit exceeded: would use 288.69 MiB
(attempt to allocate chunk of 4.06 MiB), maximum: 286.10 MiB

This is the same interaction between external aggregation and the hash-table statistics that #111984 describes for 04625_hash_table_sizes_stats_table_expression_modifiers.

The fix

Raise max_memory_usage for the second query to 600000000 (572.20 MiB) and record why in a comment. The number is picked so the test keeps its meaning:

  • with spilling the query needs 258-290 MiB normally and 362-387 MiB with preallocated hash tables - both pass with room to spare;
  • without spilling (max_bytes_before_external_group_by = 0) the same query needs ~855 MiB and still fails under the new limit (would use 687.22 MiB ... maximum: 572.20 MiB), so the test still verifies that the aggregation goes to disk;
  • the first query of the test is untouched - it peaks at 550-601 MiB against its 1500000000 limit.

Verified locally on an ARM release build of master (26.8.1.144) with test.hits loaded from the stateful web disk: the patched test passes and matches the reference both on a fresh server and on a server whose statistics cache makes the current version fail 3/3.

Related: #102618
Related: #111984

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

The second query of the test spills to disk at 200 MB
(`max_bytes_before_external_group_by`) and was capped at 300000000
(286.10 MiB) by `max_memory_usage`. Measured on an idle machine it peaks at
258-290 MiB, i.e. it ran at 90-97% of its own limit, so any allocation the
query makes on top of the aggregation state pushed it over and the test
failed with `MEMORY_LIMIT_EXCEEDED`.

One reproducible way to add that extra memory is the aggregation hash-table
size statistics: once the cache holds an entry for this aggregation,
`collect_hash_table_stats_during_aggregation` preallocates ~2.7 million
elements and the peak jumps from ~258 MiB to ~374 MiB. With the old limit
the test then fails on every run, with the same message as in CI:

    Query memory limit exceeded: would use 288.69 MiB
    (attempt to allocate chunk of 4.06 MiB), maximum: 286.10 MiB

Raise the limit to 600000000 (572.20 MiB). Without spilling the same query
needs ~855 MiB and still fails with `MEMORY_LIMIT_EXCEEDED` under the new
limit, so the test keeps checking that the aggregation goes to disk.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=a8c156e623333d40936998a3b0d273966835912c&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3a9ca63]

Summary: ✅


AI Review

Summary

This PR raises max_memory_usage for the second query in 00084_external_aggregation from 300000000 to 600000000 and documents why the old limit was too close to the spilling peak once hash-table statistics preallocation kicks in. I did not find a concrete defect in the diff: the new limit still sits between the reported spilling peak and the reported non-spilling peak, so the test continues to encode the intended "GROUP BY must spill to disk under this threshold" contract while leaving materially more headroom for the cached-statistics path.

Missing context / blind spots
  • ⚠️ The stateful amd_debug / sanitizer build flavors called out in the PR body were still queued during this review, so I could only validate the threshold from the code, the PR’s measurements, and the test contract, not from completed target CI runs on those exact configurations.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 27, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Alright.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

2 participants