Repository navigation
Conversation
Closes: ClickHouse#127561 Closes: ClickHouse#127334 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [58f099c] Summary: ❌
AI ReviewSummaryThe PR adds explicit memory-limit checks after reading and copying aggregate states whose allocations can bypass enforcement. The checks reject the reproduced large-state copies near the limit, but their per-state cost substantially slows small-state reads and copies. The ownership-copy regression also accepts the unchecked implementation because a later allocation raises the expected exception. PR Metadata
Findings❌ Blockers
💡 Nits
Missing context / blind spots
Tests
Final VerdictStatus: ❌ Block. Amortize the checks, demonstrate acceptable small-state read/copy performance, and make the regression reject delayed memory-limit enforcement. |
| if (check_memory_limit) | ||
| checkMemoryLimitForAggregateFunctionState(); |
There was a problem hiding this comment.
❌ [P1] Amortize the full memory-tracker check for small states
This runs CurrentMemoryTracker::check once per non-trivially destructible state, even when the state fits inline and performs no heap allocation. The full tracker walk then dominates ordinary reads and copies. Using this PR's aarch64 Fast-test binary, with 1,000,000 rows, max_threads = 1, alternating trials and six measured samples per variant:
| Operation | Helper returns immediately | Current code |
|---|---|---|
Native read of anyLastState('x') |
32.0 ms | 121.5 ms |
Shared-state copy of anyLastState('x') |
61.0 ms | 153.5 ms |
Shared-state copy over FixedString(32) |
181.0 ms | 279.0 ms |
The control is the same binary, with only the new helper's body replaced by an immediate return; caller overhead remains in both variants. This isolates the check's cost rather than claiming a separately compiled master comparison. The copy query is:
SELECT finalizeAggregation(materialize(initializeAggregation('anyLastState', 'x')))
FROM numbers(1000000)
SETTINGS max_threads = 1, max_memory_usage = '1G' FORMAT Null;Please amortize the expensive tracker walk while preserving prompt enforcement for large states, and add a small-state read/copy benchmark. The same per-state cost also occurs in ColumnAggregateFunction::ensureOwnership and the other new call sites; checking only once per block would reintroduce the memory-limit problem.
| SELECT finalizeAggregation(materialize(initializeAggregation('anyLastState', toFixedString('\0', 1048577)))) FROM numbers(4000) | ||
| SETTINGS max_memory_usage = '100M', max_block_size = 65536 FORMAT Null; -- { serverError MEMORY_LIMIT_EXCEEDED } |
There was a problem hiding this comment.
Checking only for MEMORY_LIMIT_EXCEEDED does not distinguish the fix from the original unchecked copy. ColumnAggregateFunction::convertToValues calls ensureOwnership and then reserves the finalized FixedString result, whose allocation can raise the same exception after all states have already been copied.
I disabled only the new check helper in the PR's Fast-test binary and ran this query with 400 rows instead of 4000 to bound the experiment: it still returned the expected exception, reporting 829.73 MiB of prospective usage, including a refused 400 MiB allocation. The unchanged PR binary rejected it at 98.21 MiB, under the same 95.37 MiB limit. Thus the exception assertion accepts the unchecked implementation too, and would also accept a check deferred until after the copy loop.
Record these queries with a distinct log_comment and assert from system.query_log that the reported refusal usage remains close to the configured limit (for example below 150 MB). This makes the test protect the bounded-memory behavior rather than an eventual allocation failure.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units333 translation units recompiled, 2529 s compile time in total, 333 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…rter of the states No existing query number or text changes: 2 queries are disabled and 2 copies appended as 5 and 6. The copies are queries 0 and 2 with `WHERE k < 1250000`: the first quarter of the one part of uniq_tiny_states (153 of 611 granules), the same mix of 2- and 9-element states; each of the 1000 GROUP BY sets ends two size degrees smaller at the same fill. Locally (official amd builds, perf.py, two repetitions on different NUMA nodes, L3 capped to 1 way and to 3 ways): per-row time of the copies 0.94-1.06 of the originals', per-row user time too; the GROUP BY copy keeps 0.86-0.995 of the effect of ClickHouse#111493 (9b9c059 -> d1a7da6, CI x1.14 amd / x1.20 arm on query 2), the finalizeAggregation copy keeps 0.94 of the effect of the ClickHouse#129553 CI build (CI x1.31 / x1.39 on query 0). The pair passes ran 15 runs per query and server. The copy's 80 MB of sets sits between arm's 36 MB and amd's 105 MB shared L3, so the regime and the effect were gated at both cache widths. Saving: about 27 s of query time per master perf job on amd, 25 s on arm. Related: ClickHouse#129258 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Check memory limit after reading or copying aggregate function states
Closes: #127561
Closes: #127334
Workflow [PR]
Sync PR [sync-upstream/pr/129553]