Repository navigation
Spill the adaptive aggregator's learning-phase tables under memory pressure - #115038
Conversation
…essure Aggregator::executeOnBlock ends in a shared baseline tail that converts the table to two-level, checks limits, and spills past max_bytes_before_external_group_by. The adaptive block returns before that tail: the thaw arm stands down first and the frozen arm runs its drain valve under the same trigger, but the learning arm returned with no memory response of any kind. A producer leaves the learning phase only by freezing at adaptive_aggregator_freeze_threshold keys or by giving up after adaptive_freeze_give_up_row_multiple times that many rows. Raise the threshold and neither bound fires inside one query, so every block took the early return and the local table grew in RAM with no byte bound: the phase is bounded only in keys, and the single limit the arm did check, max_rows_to_group_by, has to be 0 for the adaptive path to be admitted at all. A GROUP BY that spills with the feature off then dies with MEMORY_LIMIT_EXCEEDED with it on. A learning table is an ordinary table. It has no frozen twin to pair with, no staged records, and no shared session, so unlike a frozen one it can stand down for good and spill through the branch that already exists. The stood-down state is the one the give-up rule already produces, so the merge path needs no change. The transition is gated on the pressure trigger rather than unconditional. The baseline tail can convert the table to two-level, and isConvertibleToTwoLevel() is false for an already-two-level table, so the freeze guard would never pass again and an unconditional fall-through would trade this bug for a silently foreclosed feature. Copying the frozen arm's valve instead would be a no-op: it drains a backlog that is only ever populated from the frozen path, through a session that freezeAdaptive has not initialized yet. Reachability needs a raised adaptive_aggregator_freeze_threshold. At the default of 16384 the spill still works, so this is not a default-on regression. Closes: ClickHouse#114987
Every other adaptive control transition has a ProfileEvent (freeze, give-up, thaw, probe bypass, the frozen arm's pressure sweep); the pressure stand-down added by the previous commit had none, so the one path that repairs the learning-phase spill hole was the only one an operator could not observe. It also makes the regression test pin the branch directly. Before this, 04650 asserted only that a spill happened with the feature enabled, which implies the new branch only through a three-step inference over two private constants (adaptive_aggregator_freeze_threshold, so freeze cannot fire, and adaptive_thaw_min_staged_records, which pure learning never reaches). A later change to either bound would have quietly turned that cell into a plain baseline-aggregation assertion that still passed. The isLearning() test keeps the counter honest rather than gating the transition: a producer that already stood down on the give-up rule earlier in the same block would otherwise be counted as a pressure stand-down too. The standDown call itself stays unconditional, because it is idempotent and takes the same reason either way. Measured on the give-up regime (50 keys, 400k rows, max_bytes_before_external_group_by = 1): without the test the counter reports 3 stand-downs that never happened; with it, 0, while the genuine pressure regime still reports 4. The counter is deliberately not added to 04654, which enumerates one row per control path: none of its four queries raises the freeze threshold, so a row there would assert a counter its own queries cannot advance. Negative direction covered by a third clickhouse-local block, in its own process because system.events is cumulative within one: with both external thresholds at 0 the trigger cannot fire and the counter must stay at 0.
Internal second-model review (click to expand)Two review rounds ran on this change: a cold code review plus an independent second-model gate on Round 2 (this revision, Round 1 (
Noted, not blocking:
The guard on the new counter was verified with a mutation build rather than by inspection: with Session id: cron:clickhouse-review-slot-11:20260816-193800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260816-182200 |
|
Workflow [PR], commit [687d548] Summary: ✅
AI ReviewSummaryThis PR fixes a real gap in adaptive PR Metadata
With Exact replacement: ### Changelog category (leave one):
- Bug Fix (user-visible misbehavior in an official stable release)
### Changelog entry (a [user-readable short description](https://github.com/ClickHouse/ClickHouse/blob/master/docs/changelog_entry_guidelines.md) of the changes that goes into CHANGELOG.md):
Fixes adaptive `GROUP BY` spilling when `adaptive_aggregator_freeze_threshold` is raised, so queries in the learning phase can still honor `max_bytes_before_external_group_by` instead of failing with `MEMORY_LIMIT_EXCEEDED`.Final Verdict✅ Code looks correct. Update the changelog category and entry before merge. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 11/14 (78.57%) · Uncovered code |
|
cc @nickitat @KochetovNicolai, could you review this? The adaptive aggregator's learning arm returned from executeOnBlock before the baseline tail, so max_bytes_before_external_group_by was a no-op for the whole learning window; it now stands down under the same pressure trigger the frozen arm uses and reaches the existing two-level conversion and writeToTemporaryFile. Gated on the trigger rather than falling through unconditionally, because a converted table can never satisfy isConvertibleToTwoLevel() again and would foreclose freezing. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units8 translation units recompiled, 49 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - 687d548CI is fully finished on this head and it is clean: 177 check-runs, 157 success / 20 skipped,
Those two CIDB rows are historical, not open. Both predate the label being applied; the same All five Session id: cron:our-pr-ci-monitor:20260817-020000 |
cb012d3
Closes: #114987
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Requested by @ nihalzp in #114987 (comment).
The problem.
Aggregator::executeOnBlockends in a baseline tail that converts the table totwo-level, checks limits and spills past
max_bytes_before_external_group_by. The adaptive blockreturns before that tail. Its thaw arm stands down first and its frozen arm runs a drain valve, but
the learning arm returned with no memory response at all. A producer leaves learning only by
freezing at
adaptive_aggregator_freeze_thresholdkeys or by giving up after 16x that many rows, sowith the threshold raised neither bound fires inside one query and the table grows in RAM with no
byte bound.
The change. A learning table has no frozen twin to pair with and nothing staged, so under
pressure it can stand down for good and spill through the branch that already exists below. Gating
it on the pressure trigger matters: the tail can convert the table to two-level, after which
isConvertibleToTwoLevel()is false and the producer could never freeze again, so an unconditionalfall-through would disable the feature rather than fix it.
Reachability is narrower than the report implies. At the default
adaptive_aggregator_freeze_threshold = 16384the spill still works, so this needs a raisedthreshold, not stock settings. The reporter agreed on the issue.
Validation. On the report's query with the threshold raised,
ExternalAggregationWritePartgoesfrom 0 to 46, matching the feature-off arm, and under
max_memory_usage = 700000000the querychanges from
MEMORY_LIMIT_EXCEEDEDto completing. Each new defect cell fails on the unfixedbinary, the freeze counters are byte-identical on the fast path, and the two files passed 100
randomized runs.
Left alone deliberately: the stand-down reuses
Reason::TooFewDistinctKeysrather than adding aMemoryPressureenumerator, since that field has no readers today; and a frozen local table staysunbounded in bytes for growing states such as
groupArray, which the issue raises separately.Version info
26.8.1.1521(included in26.8and later)