Skip to content

Reserve the size of the spilled table for external GROUP BY, not the query memory - #130144

Merged
azat merged 1 commit into
ClickHouse:masterfrom
azat:aggregation-spill-reserve-table-size
Nov 10, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
azat:aggregation-spill-reserve-table-size

Conversation

@azat

@azat azat commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Reserve the size of the spilled table for external GROUP BY, not the query memory

A thread over max_bytes_before_external_group_by reserved the memory usage of the whole query for its temporary file, so with N threads the query asked for N times its memory, and a disk with room for the data refused the spill with NOT_ENOUGH_SPACE. The reservation is only a preallocation, so the table's own memory (its tracker, or allocatedBytes for a merging aggregator) is enough, as Aggregator::spill does.

Adaptive aggregation (on by default) spills staged records on its own path and was not affected, which is why the issue did not reproduce with it.

Closes: #80631


Workflow [PR]
Sync PR [sync-upstream/pr/130144]

Version info

  • Merged into: 26.11.1.1859-master (included in 26.11 and later)

…query memory

A thread over `max_bytes_before_external_group_by` reserved the memory usage of the
whole query for its temporary file, so with N threads the query asked for N times its
memory, and a disk with room for the data refused the spill with NOT_ENOUGH_SPACE.
The reservation is only a preallocation, so the table's own memory (its tracker, or
`allocatedBytes` for a merging aggregator) is enough, as `Aggregator::spill` does.

Adaptive aggregation (on by default) spills staged records on its own path and was
not affected, which is why the issue did not reproduce with it.

Closes: ClickHouse#80631

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8dfa269]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb

AI Review

Summary

Reviewed #130144 at 8dfa269d0879c253b838d714aed5a50405ec639c. The PR sizes external GROUP BY spill reservations from the individual aggregation table instead of the whole query's memory usage. No blocker or major issue found. The new stateless regression test passed locally with the PR's Fast test binary: output matched the reference, with reservations below 128 MiB.

PR Metadata

The changelog entry is required, present, and specific about the affected behavior. No migration guidance is needed. The category correction is listed below.

Findings

💡 Nits

Final Verdict

✅ Approve — no blocking changes requested; only the changelog-category correction is suggested.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 8, 2026
@nihalzp nihalzp self-assigned this Nov 8, 2026
@azat
azat enabled auto-merge November 8, 2026 13:54
@clickhouse-gh clickhouse-gh Bot added the comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). label Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8dfa269d0 with master 7b5f2bf32 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 109 s compile time in total, 7 of them have a recent master baseline.

Job report

@azat

azat commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

@azat
azat disabled auto-merge November 10, 2026 12:47
@azat
azat added this pull request to the merge queue Nov 10, 2026
Merged via the queue into ClickHouse:master with commit 6e9466a Nov 10, 2026
189 of 191 checks passed
@azat
azat deleted the aggregation-spill-reserve-table-size branch November 10, 2026 14:05
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 10, 2026
alexey-milovidov pushed a commit to pugachevdmitry/ClickHouse that referenced this pull request Nov 12, 2026
The test asserts that every reservation of an external GROUP BY is below
`max_bytes_before_external_group_by`, but with `GROUP BY number` the
query memory is the sum of the per-thread tables. The spill check reads
the query memory once per block, and the buckets of a two-level table
grow x4 within a block or two, so a thread that gets most of the
`numbers_mt` blocks can take its table from 64 MiB to 252 MiB before the
check fires; since ClickHouse#130144 that table size is what gets reserved, and it
is above the 128 MiB threshold. With `max_threads = 1` every reservation
is 252.54 MiB.

Keep the assertion and change the workload: the set of
`NOT IN (SELECT number + 10000000 FROM numbers(5000000))` (pinned in
memory with `max_bytes_before_external_set = 0` and
`max_bytes_ratio_before_external_set = 0`) keeps the query over a 32 MiB
threshold, and the key `number % toUInt64(1000)` bounds every table to
1000 keys (~1 MiB), so every block spills a small table whatever the
scheduling. The key must be `UInt64`: a `UInt16` key is aggregated with
`key16`, a fixed hash map that is never spilled.

Measured with master CI binaries: with ClickHouse#130144 (f9e6c60) the largest
reservation over 130 runs is 1.10 MiB (plain, one CPU, 24 concurrent
copies, thread fuzzer); without it (b90a90b, the merge's first parent)
the smallest is 135 MiB and the test fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). pr-improvement Pull request with some product 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.

Aggregation spill to disk tries to reserve too much space

3 participants