选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 错误代码 / Class 42 语法错误或访问规则冲突

42803 grouping_error

分组错误

ERROR 已实测 详解 实测通过

条件名
grouping_error
宏名称
ERRCODE_GROUPING_ERROR
启用版本
7.4
状态
活跃

版本覆盖

速览

42803grouping_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
DETAIL 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 通过;这是有界观察,不能推断所有中间版本或所有源码分支。

来源

证据

断言

每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。

  • 42803 is the grouping_error condition in Class 42.

    核实方式Read fixed errcodes.txt and locked catalogue metadata.

    不覆盖Identity does not identify every backend call that can reuse this SQLSTATE.

    来源src/backend/utils/errcodes.txt

  • The selected grouping_non_grouped_column follows a resolved PostgreSQL source-call group; other branches can use different dynamic fields.

    核实方式Trace the REL_18_6 and REL_10_23 source-call records at the locked commits.

    不覆盖This is one mechanism boundary, not an exhaustive inventory of the code.

    来源src/backend/parser/parse_agg.c · src/backend/parser/parse_agg.c

  • 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.

    核实方式Combine the selected source call, registry SQL, and passed structured assertions.

    不覆盖The claim describes the selected path and does not generalize to every branch sharing this SQLSTATE.

    来源src/backend/parser/parse_agg.c · src/backend/parser/parse_agg.c · src/backend/catalog/pg_constraint.c · src/backend/catalog/pg_constraint.c · verify/cases/42803/snippets.json

  • 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.

    核实方式Read the complete fixed parse_agg.c error branch around the selected primary.

    不覆盖The selected ordinary grouped query did not exercise ordered-set direct arguments.

    来源src/backend/parser/parse_agg.c · src/backend/parser/parse_agg.c

  • The selected grouping_non_grouped_column passed on isolated PostgreSQL 18.6 and 10.21 targets.

    核实方式Run the shared SQL registry and inspect SQLSTATE, severity, state, repair, cleanup, and target stop.

    不覆盖Scope is limited to these statements, psycopg, and two versions.

    来源verify/cases/42803/cases.json · verify/cases/42803/snippets.json

  • The locked catalogue records 42803 from the 7.4 presence bound through the listed snapshots; runtime scope is 18.6 and 10.21.

    核实方式Read generated facts, fixed calls, and selected summaries.

    不覆盖Definition presence is not an exact behavioral introduction; two runtime versions do not prove every intermediate behavior.

    来源src/backend/utils/errcodes.txt · raw/calls/REL_18_6.jsonl · raw/calls/REL_10_23.jsonl

运行记录

目标服务器版本结果覆盖案例
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 语法错误或访问规则冲突 下的其他成员。