百科 / 错误代码 / Class 42 语法错误或访问规则冲突
42803 grouping_error
分组错误
ERROR 已实测 详解 实测通过
- 条件名
grouping_error- 宏名称
ERRCODE_GROUPING_ERROR- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
42803 是 grouping_error(分组错误):分组查询输出了既未分组也未聚合的值。本案例只按 category 分组,却选择 label。
含义
分析器必须为每个分组的每个选择表达式确定一个值。固定报文为 column "%s.%s" must appear in the GROUP BY clause or be used in an aggregate function。已检查的 check_functional_grouping 路径要求 GROUP BY 列包含该表主键的全部列,才会认可函数依赖;不能把任意看似唯一的表达式都当作例外。本案例的 VALUES 关系没有这种表约束,因此加入 label 会把结果粒度从每个 category 一行明确改成每个 (category,label) 一行,而不是语义中性的修复;自动提交错误后为 IDLE。
诊断
检查 SELECT、HAVING 及相关排序表达式中的非聚合项。依赖函数依赖前先核对关系约束,修改 GROUP BY 前先决定期望每个 category 一行,还是每个 category/label 一行。ordered-set 聚合的 direct argument 还有单独的分组列规则;下方记录其源码 DETAIL,但它不是本案例的普通分组查询。
处理
把需要保持的表达式加入 GROUP BY,或用明确规则聚合,再核对行数。案例按 category,label 修复并返回两行,因为两个 label 形成两个组;如果业务结果应每个 category 一行,就要选择聚合或明确保留哪个 label。显式事务中失败语句会使事务进入 INERROR,应先回滚或回到合适的 savepoint;本案例的自动提交路径才会回到 IDLE。
可复现案例
在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。
grouping_non_grouped_column PG 10 / 18 有 SQL
前置条件
- A runner-owned disposable target is provisioned.
触发
Select a nonaggregate label while grouping only the category column.
断言
- SQLSTATE is 42803 and the diagnostic identifies the ungrouped label
- The failed autocommit session remains IDLE
- Grouping both selected nonaggregate columns returns two deterministic rows
处置
Decide whether the label should be grouped or aggregated; adding GROUP BY blindly can change the intended cardinality.
清理
Close the runner connection; the case uses VALUES only.
实测诊断
固定 parse_agg.c 组是 ERROR。本案例主报文为 column "%s.%s" must appear in the GROUP BY clause or be used in an aggregate function。当未分组变量是 ordered-set 聚合的 direct argument 时,同一源码分支在 context->in_agg_direct_args 守卫下追加仅源码确认的 DETAIL:Direct arguments of an ordered-set aggregate must use only grouped columns.;本案例普通分组查询没有产生该 DETAIL。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
column "%s.%s" must appear in the GROUP BY clause or be used in an aggregate function
Direct arguments of an ordered-set aggregate must use only grouped columns.
来源:src/backend/parser/parse_agg.c @ REL_18_6 · src/backend/parser/parse_agg.c @ REL_10_23
代表案例
注册表用两个 (category,label) 值,只按 category 触发,再按 category,label 排序修复并断言 (1,a,1)、(1,b,1)。
SELECT category, label, count(*) FROM (VALUES (1, 'a'), (1, 'b')) AS grouping_rows(category, label) GROUP BY category;
SELECT category, label, count(*) FROM (VALUES (1, 'a'), (1, 'b')) AS grouping_rows(category, label) GROUP BY category, label ORDER BY category, label;
选定的 18.6 与 10.21 运行均通过 SQLSTATE、严重级别、状态/恢复、修复、清理和一次性实例停止断言。详见 案例 JSON 与 作者证据;私有清单和注册表哈希也记录在其中。
版本
锁定目录从 7.4 存在边界起包含该条件并列出相关快照。选定自然案例已在 PostgreSQL 18.6 与 10.21 通过;这是有界观察,不能推断所有中间版本或所有源码分支。
来源
- 上游源码 src/backend/utils/errcodes.txt
- 上游源码 src/backend/parser/parse_agg.c 第 1356–1362 行
- 上游源码 src/backend/parser/parse_agg.c 第 1552–1558 行
- 上游源码 src/backend/catalog/pg_constraint.c 第 1057–1107 行
- 上游源码 src/backend/catalog/pg_constraint.c 第 1728–1778 行
- 核验材料 verify/cases/42803/cases.json
- 核验材料 verify/cases/42803/snippets.json
- 核验材料 raw/calls/REL_10_23.jsonl
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42803 is the grouping_error condition in Class 42.
-
The selected grouping_non_grouped_column follows a resolved PostgreSQL source-call group; other branches can use different dynamic fields.
-
Grouped output must define one value per group; the inspected functional-dependency exception is proven only when the GROUP BY columns contain every column of the table primary key, while adding label in the selected unconstrained VALUES relation changes report cardinality.
-
The grouping walker adds the DETAIL Direct arguments of an ordered-set aggregate must use only grouped columns only when context->in_agg_direct_args is true; that branch is source-only for this review.
-
The selected grouping_non_grouped_column passed on isolated PostgreSQL 18.6 and 10.21 targets.
-
The locked catalogue records 42803 from the 7.4 presence bound through the listed snapshots; runtime scope is 18.6 and 10.21.
运行记录
| 目标 | 服务器版本 | 结果 | 覆盖案例 |
|---|---|---|---|
| latest | 18.6 (Homebrew) | passed | grouping_non_grouped_column |
| pg10 | 10.21 (Debian 10.21-1.pgdg90+1) | passed | grouping_non_grouped_column |
同类错误代码
Class 42 语法错误或访问规则冲突 下的其他成员。
42000syntax_error_or_access_rule_violation42501insufficient_privilege42601syntax_error42602invalid_name42611invalid_column_definition42622name_too_long42701duplicate_column42702ambiguous_column42703undefined_column42704undefined_object42710duplicate_object42712duplicate_alias42723duplicate_function42725ambiguous_function42804datatype_mismatch42809wrong_object_type42830invalid_foreign_key42846cannot_coerce42883undefined_function428C9generated_always42939reserved_name42P01undefined_table42P02undefined_parameter42P03duplicate_cursor42P04duplicate_database42P05duplicate_prepared_statement42P06duplicate_schema42P07duplicate_table42P08ambiguous_parameter42P09ambiguous_alias42P10invalid_column_reference42P11invalid_cursor_definition42P12invalid_database_definition42P13invalid_function_definition42P14invalid_prepared_statement_definition42P15invalid_schema_definition42P16invalid_table_definition42P17invalid_object_definition42P18indeterminate_datatype42P19invalid_recursion42P20windowing_error42P21collation_mismatch42P22indeterminate_collation