Repository navigation
Description
Description
Accounted aggregate states return an internal allocation-account error when a grouped query enters multi-level aggregate spill. Confirmed affected functions are APPROX_COUNT_DISTINCT, APPROX_PERCENTILE, HLL_ADD_AGG, HLL_MERGE_AGG, BITMAP_CONSTRUCT_AGG, and BITMAP_OR_AGG. The same queries succeed when kept resident, and exact/scalar aggregate controls succeed under the same spill threshold.
Environment
- Branch:
main - Commit:
07fdd4ae0f80f287b93fc4b525fd296d5617abc7 - Deployment: local standalone, 1 CN / 1 TN / 1 LogService, dedicated data directory and ports
Steps to reproduce
drop database if exists approx_hll_spill_repro;
create database approx_hll_spill_repro;
use approx_hll_spill_repro;
create table t as
select result as id, result as g
from generate_series(1, 7400) s;
set @@max_dop = 1;
set @@agg_spill_mem = 65536;
select count(*) as groups_out, sum(ad) as sum_ad
from (
select g, approx_count_distinct(id) as ad
from t
group by g
) q;Actual behavior
The supported aggregate fails before returning its 7,400 groups:
ERROR 1105 (HY000): allocation account still owns memory
The CN diagnostic summary confirms recursive spill/reload on the failing operator, including GroupSpillMaxLevel=3, GroupSpillRespills=1056, and GroupSpillReloadRows=22200.
Replacing the aggregate in the reproducer with any of the following produces the same error:
hll_cardinality(hll_add_agg(id))
hll_cardinality(hll_merge_agg(sketch))
bitmap_count(bitmap_construct_agg(bitmap_bit_position(id)))
bitmap_count(bitmap_or_agg(bmp))
approx_percentile(value, 0.5)Expected behavior
Each one-row-per-group query should return:
groups_out sum_ad
7400 7400
Lowering agg_spill_mem may change the execution path and latency, but it should not make a supported aggregate fail with an internal memory-ownership error.
Stability and controls
- Reproducer:
3/3for each ofAPPROX_COUNT_DISTINCT,APPROX_PERCENTILE, HLL add/merge, and Bitmap construct/or. - Resident controls (
agg_spill_mem=1073741824): every affected aggregate returns its expected group count and compact result invariant (7400 / 7400for one-value-per-group HLL/Bitmap cases;20000 / 1000010000for the percentile case). - Exact control (
COUNT(DISTINCT id),agg_spill_mem=65536): returns7400 / 7400. - Scalar-state controls (
COUNT,SUM,VAR_POP,MEDIAN,BIT_AND/OR/XOR) and JSON aggregates complete the same forced-spill path. - Scale control: 7,200 one-row groups succeeds; 7,400 one-row groups fails.
- Failure atomicity/data check: the service remains available; the table still contains 7,400 rows with
SUM(id)=27383700. EXPLAINwithout execution succeeds; both plain execution andEXPLAIN ANALYZEfail once this HLL spill path is exercised.
Evidence
- Failing SQL and three independent outputs were saved during validation.
- Server log records the exact statement and
allocation account still owns memoryfrom the Group operator. - Tested commit: 07fdd4a
Code analysis
pkg/sql/colexec/aggexec/approx_count2.go stores a dense HLL sketch of 16,384 registers per active group and allocates its register storage through the aggregate allocation account. Bitmap aggregation uses the same bounded opaque-state ownership mechanism, while APPROX_PERCENTILE owns a bounded quantile sketch. The failure appears after multi-level Group spill reload/reuse and during terminal account cleanup. Because these state-owning families fail while scalar states and JSON aggregation succeed, this points to retained or incorrectly transferred MPool ownership in the generic spill recovery path; the exact missing release/transfer site still needs developer confirmation.
Regression coverage
After the fix, add:
- Group spill UTs covering HLL, Bitmap, and approximate-percentile aggregate state across recursive spill/reload and terminal allocation-account cleanup.
- A big-data SQL case that forces those aggregate states to spill and compares compact result invariants against resident execution.
This is not suitable as a passing MOTR baseline before the product path is fixed.
Related
Discovered while expanding aggregate/statistical-function spill coverage; duplicate searches included the exact error, APPROX_COUNT_DISTINCT, HLL, aggregate spill, allocation account, and open/closed issues.
Activity
[-][Bug]: APPROX_COUNT_DISTINCT fails with allocation-account error during multi-level aggregate spill[/-][+][Bug]: HLL and Bitmap aggregate states fail allocation cleanup during multi-level spill[/+][-][Bug]: HLL and Bitmap aggregate states fail allocation cleanup during multi-level spill[/-][+][Bug]: HLL, Bitmap, and approximate aggregate states fail cleanup during multi-level spill[/+]Ariznawlll commentedon Sep 15, 2026
补充当前
main的APPROX_PERCENTILE规模证据:0c3a04f390adaf6281fd592a49778ea5b1155e67agg_spill_mem=1073741824max_dop=1与max_dop=8均连续 3 轮返回完整 20,000 组;每组的 p0/p25/p50/p75/p1 分别为-2/-1/0/1/2agg_spill_mem=65536max_dop=1连续 3 轮均返回ERROR 1105 (HY000): allocation account still owns memorymax_dop=8连续 3 轮均返回相同错误max_dop和agg_spill_mem已恢复默认值该结果说明问题在当前 main 仍存在,且同时覆盖串行、并行两种 recursive spill 路径;常驻路径的数值与分组结果正常。
fix: release opaque aggregate spill state on reuse (#28925)
Ariznawlll commentedon Sep 17, 2026
测试版本
main8e8e1998ef02b1f6233ddb1c2d3208f3b70d2d83原问题验证
按原始触发条件构造 7,400 个一行分组,设置
max_dop=1、agg_spill_mem=64KiB后执行approx_count_distinct、approx_percentile、HLL 和 bitmap 聚合序列。所有分组数和汇总值均匹配精确预期:7400/7400/7400/27383700.0/7400/7400,未再出现 spill 后错误。Happy Path 覆盖
覆盖原始 7,400 分组规模、64KiB spill 阈值以及四类 opaque aggregate。
Unhappy Path 覆盖
不适用:该问题是合法低阈值 spill 下的结果正确性。测试会恢复
agg_spill_mem、max_dop并删除 fixture。白盒验证
不适用:以低 spill 阈值下的全量聚合结果与 golden 比对作为端到端验收。
回归测试
已加入 big-data PR #1698:
38_issue_28853_28871_grouped_agg_spill.sql,并接入 1Y big-data workflow。本地连续执行与 golden 一致。测试证据
run.py使用该 case 执行:7,400 分组的汇总为7400/7400/7400/27383700.0/7400/7400,连续 golden 比对一致。测试结论
测试通过。
fix: release opaque aggregate spill state on reuse (matrixorigin#28925)
fix: release opaque aggregate spill state on reuse (matrixorigin#28925)