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

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

文档 / SQL 状态码 / 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

同类 SQL 状态码

状态码 条件名 宏名称 严重等级 版本
Class 42 语法错误或访问规则冲突 Syntax Error or Access Rule Violation 44 个
42000 syntax_error_or_access_rule_violation ERRCODE_SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION ERROR 7.4
语法错误或访问规则违反的总类,见具体子码。 活跃
42501 insufficient_privilege ERRCODE_INSUFFICIENT_PRIVILEGE ERROR 7.4
当前角色对目标对象的权限不足。 活跃
42601 syntax_error ERRCODE_SYNTAX_ERROR ERROR 7.4
SQL 语句存在语法错误,解析器拒绝执行。 活跃
42602 invalid_name ERRCODE_INVALID_NAME ERROR 7.4
名称不合法,如 enum 标签超出长度限制。 活跃
42611 invalid_column_definition ERRCODE_INVALID_COLUMN_DEFINITION ERROR 7.4
列定义无效,如继承的父表默认值冲突。 活跃
42622 name_too_long ERRCODE_NAME_TOO_LONG ERROR 7.4
标识符或 name 值超出长度上限。 活跃
42701 duplicate_column ERRCODE_DUPLICATE_COLUMN ERROR 7.4
同一定义中重复指定了同名列。 活跃
42702 ambiguous_column ERRCODE_AMBIGUOUS_COLUMN ERROR 7.4
列引用有歧义,多个表都有该列名。 活跃
42703 undefined_column ERRCODE_UNDEFINED_COLUMN ERROR 7.4
引用的列在目标关系中不存在。 活跃
42704 undefined_object ERRCODE_UNDEFINED_OBJECT ERROR 7.4
引用的数据库对象不存在,如触发器或类型。 活跃
42710 duplicate_object ERRCODE_DUPLICATE_OBJECT ERROR 7.4
要定义的对象名已被占用,如重复 CREATE TYPE。 活跃
42712 duplicate_alias ERRCODE_DUPLICATE_ALIAS ERROR 7.4
同一查询命名空间中重复使用了表别名。 活跃
42723 duplicate_function ERRCODE_DUPLICATE_FUNCTION ERROR 7.4
同一模式下已存在同名同参数类型的函数。 活跃
42725 ambiguous_function ERRCODE_AMBIGUOUS_FUNCTION ERROR 7.4
函数调用有多个候选,无法选出最佳匹配。 活跃
42803 grouping_error ERRCODE_GROUPING_ERROR ERROR 7.4
分组查询输出了既未分组也未聚合的列。 活跃
42804 datatype_mismatch ERRCODE_DATATYPE_MISMATCH ERROR 7.4
表达式类型与目标上下文要求的类型不符。 活跃
42809 wrong_object_type ERRCODE_WRONG_OBJECT_TYPE ERROR 7.4
命令本身有效,但不能作用于该类对象。 活跃
42830 invalid_foreign_key ERRCODE_INVALID_FOREIGN_KEY ERROR 7.4
外键在被引用列上找不到合格的唯一键。 活跃
42846 cannot_coerce ERRCODE_CANNOT_COERCE ERROR 7.4
源类型到目标类型之间没有可用的 cast。 活跃
42883 undefined_function ERRCODE_UNDEFINED_FUNCTION ERROR 7.4
按名称和参数类型找不到匹配的函数或操作符。 活跃
428C9 generated_always ERRCODE_GENERATED_ALWAYS ERROR 10
向身份列或生成列赋值,这类列只接受 DEFAULT。 活跃
42939 reserved_name ERRCODE_RESERVED_NAME ERROR 7.4
对象名使用了保留名称,如模式或表空间。 活跃
42P01 undefined_table ERRCODE_UNDEFINED_TABLE ERROR 7.4
引用的表、视图或其他关系不存在。 活跃
42P02 undefined_parameter ERRCODE_UNDEFINED_PARAMETER ERROR 7.4
查询引用了解析器未知的位置参数,如 $1。 活跃
42P03 duplicate_cursor ERRCODE_DUPLICATE_CURSOR ERROR 7.4
游标或 portal 名称与已有的冲突。 活跃
42P04 duplicate_database ERRCODE_DUPLICATE_DATABASE ERROR 7.4
集群中已存在同名数据库。 活跃
42P05 duplicate_prepared_statement ERRCODE_DUPLICATE_PSTATEMENT ERROR 7.4
当前会话已有同名的服务端预备语句。 活跃
42P06 duplicate_schema ERRCODE_DUPLICATE_SCHEMA ERROR 7.4
要创建的模式名在当前数据库中已存在。 活跃
42P07 duplicate_table ERRCODE_DUPLICATE_TABLE ERROR 7.4
关系名已存在,或继承父项重复。 活跃
42P08 ambiguous_parameter ERRCODE_AMBIGUOUS_PARAMETER ERROR 7.4
参数存在但无法推导出一致而具体的类型。 活跃
42P09 ambiguous_alias ERRCODE_AMBIGUOUS_ALIAS ERROR 7.4
一个表引用对应多个关系命名空间项。 活跃
42P10 invalid_column_reference ERRCODE_INVALID_COLUMN_REFERENCE ERROR 7.4
列引用不符合该操作的契约,如 COPY 生成列。 活跃
42P11 invalid_cursor_definition ERRCODE_INVALID_CURSOR_DEFINITION ERROR 7.4
游标声明违反定义阶段规则,如 SCROLL 选项冲突。 活跃
42P12 invalid_database_definition ERRCODE_INVALID_DATABASE_DEFINITION ERROR 7.4
数据库定义无效,核心源码中未见触发路径。 活跃
42P13 invalid_function_definition ERRCODE_INVALID_FUNCTION_DEFINITION ERROR 7.4
函数、聚合或触发器声明在校验阶段失败。 活跃
42P14 invalid_prepared_statement_definition ERRCODE_INVALID_PSTATEMENT_DEFINITION ERROR 7.4
PREPARE 定义预备语句时失败。 活跃
42P15 invalid_schema_definition ERRCODE_INVALID_SCHEMA_DEFINITION ERROR 7.4
CREATE SCHEMA 的模式声明不一致被拒绝。 活跃
42P16 invalid_table_definition ERRCODE_INVALID_TABLE_DEFINITION ERROR 7.4
表定义无效,如分区键或主键规则冲突。 活跃
42P17 invalid_object_definition ERRCODE_INVALID_OBJECT_DEFINITION ERROR 7.4
对象定义无效,如继承、生成列或规则冲突。 活跃
42P18 indeterminate_datatype ERRCODE_INDETERMINATE_DATATYPE ERROR 7.4
无法推断表达式或参数的数据类型。 活跃
42P19 invalid_recursion ERRCODE_INVALID_RECURSION ERROR 8.4
递归查询结构不合法,执行前被检查拒绝。 活跃
42P20 windowing_error ERRCODE_WINDOWING_ERROR ERROR 8.4
窗口函数调用或框架边界定义不合法。 活跃
42P21 collation_mismatch ERRCODE_COLLATION_MISMATCH ERROR 9.1
参与运算的排序规则不兼容,产生冲突。 活跃
42P22 indeterminate_collation ERRCODE_INDETERMINATE_COLLATION ERROR 9.1
操作需要唯一排序规则,但无法确定。 活跃