文档 / SQL 状态码 / Class 42 语法错误或访问规则冲突
42P20 windowing_error
窗口函数错误
ERROR 源码确认 参考 未实测
- 条件名
windowing_error- 宏名称
ERRCODE_WINDOWING_ERROR- 启用版本
- 8.4
- 状态
- 活跃
版本覆盖
速览
42P20(windowing_error)窗口定义同时约束函数调用和框架边界。
含义
固定核心在窗口函数放置或嵌套错误,以及窗口定义结构无效时使用 42P20。解析器语法拒绝不可能的框架起止组合;解析分析拒绝在不能包含窗口函数的子句中使用窗口函数,或把一个窗口函数放入另一个窗口函数;命名窗口转换则拒绝冲突的继承方式。合法框架对某一行仍可能为空,所以结果为空本身不是 42P20 信号。
诊断
先按 primary 消息分类,再改 SQL。框架消息指向 UNBOUNDED 方向或起止顺序;带 offset 的 RANGE 必须有且只有一个 ORDER BY 列,GROUPS 必须有 ORDER BY。放置消息会指出 WHERE、GROUP BY、JOIN、RETURNING 或窗口定义等子句;嵌套调用消息表示一个窗口表达式出现在另一个窗口表达式内部。命名窗口消息分别表示重复定义、覆盖所复制窗口的 PARTITION BY 或 ORDER BY,以及复制已经带框架的窗口;因此 OVER foo 与 OVER (foo) 的继承行为不同。执行器会把负的 ROWS 或 GROUPS 框架 offset 报为 SQLSTATE 22013(invalid_preceding_or_following_size),把 NULL offset 报为 22004;这些不是 42P20。
处理
把嵌套计算移到外层查询,或把窗口表达式移出消息指出的命名子句。只修复对应的框架语法,为 RANGE/GROUPS 补上所需排序,并定义一个没有冲突覆盖的命名窗口。不要因为某一当前行的框架没有行就改动本来合法的框架。如果该 ERROR 发生在显式事务中,重试前先执行 ROLLBACK,或回滚到语句前建立的保存点;自动提交模式下,失败语句返回空闲后即可提交修正查询。
消息
固定源码中的代表性消息包括:
- ERROR message:
window function calls cannot be nested - ERROR message:
frame start cannot be UNBOUNDED FOLLOWING - ERROR message:
frame starting from following row cannot end with current row - ERROR message:
frame end cannot be UNBOUNDED PRECEDING - ERROR message:
frame starting from current row cannot have preceding rows - ERROR message:
frame starting from following row cannot have preceding rows - ERROR message:
window functions are not allowed in %s - ERROR message:
window "%s" is already defined - ERROR message:
cannot override PARTITION BY clause of window "%s" - ERROR message:
cannot override ORDER BY clause of window "%s" - ERROR message:
cannot copy window "%s" because it has a frame clause - ERROR message:
cannot copy window "%s" because it has a frame clause; HINT:Omit the parentheses in this OVER clause. - ERROR message:
RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column - ERROR message:
GROUPS mode requires an ORDER BY clause
占位符由实际对象、列或参数填充。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
window function calls cannot be nested
来源:src/backend/executor/execExpr.c(lines 1155-1157) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
frame start cannot be UNBOUNDED FOLLOWING
来源:src/backend/parser/gram.y(lines 16499-16502) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
frame starting from following row cannot end with current row
来源:src/backend/parser/gram.y(lines 16504-16507) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
frame end cannot be UNBOUNDED PRECEDING
来源:src/backend/parser/gram.y(lines 16528-16531) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
frame starting from current row cannot have preceding rows
来源:src/backend/parser/gram.y(lines 16534-16537) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
frame starting from following row cannot have preceding rows
来源:src/backend/parser/gram.y(lines 16541-16544) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
window functions are not allowed in %s
来源:src/backend/parser/parse_agg.c(lines 1041-1046) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
window "%s" is already defined
来源:src/backend/parser/parse_clause.c(lines 2790-2793) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
cannot override PARTITION BY clause of window "%s"
来源:src/backend/parser/parse_clause.c(lines 2851-2855) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
cannot override ORDER BY clause of window "%s"
来源:src/backend/parser/parse_clause.c(lines 2863-2867) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
cannot copy window "%s" because it has a frame clause
来源:src/backend/parser/parse_clause.c(lines 2893-2897) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
cannot copy window "%s" because it has a frame clause
Omit the parentheses in this OVER clause.
Omit the parentheses in this OVER clause.
来源:src/backend/parser/parse_clause.c(lines 2899-2904) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
来源:src/backend/parser/parse_clause.c(lines 2921-2924) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
GROUPS mode requires an ORDER BY clause
来源:src/backend/parser/parse_clause.c(lines 2944-2947) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
版本
锁定目录显示该条件最早见于 PostgreSQL 8.4.0;行为说明固定在 PostgreSQL 18.6 源码,目录存在范围不等于每条消息或功能都从该版本开始。
来源
- 上游源码 src/backend/parser/parse_clause.c 第 2851–2855 行
- 上游源码 src/backend/parser/gram.y 第 16534–16537 行
- 上游源码 src/backend/executor/execExpr.c 第 1155–1157 行
- 上游源码 src/backend/parser/gram.y 第 16541–16544 行
- 上游源码 src/backend/parser/parse_clause.c 第 2790–2793 行
- 上游源码 src/backend/parser/parse_clause.c 第 2893–2897 行
- 上游源码 src/backend/parser/gram.y 第 16499–16502 行
- 上游源码 src/backend/parser/parse_clause.c 第 2944–2947 行
- 上游源码 src/backend/parser/parse_clause.c 第 2899–2904 行
- 上游源码 src/backend/parser/parse_clause.c 第 2863–2867 行
- 上游源码 src/backend/parser/gram.y 第 16528–16531 行
- 上游源码 src/backend/parser/gram.y 第 16504–16507 行
- 上游源码 src/backend/parser/parse_clause.c 第 2921–2924 行
- 上游源码 src/backend/parser/parse_agg.c 第 1041–1046 行
- 上游源码 src/backend/executor/nodeWindowAgg.c 第 2139 行
- 上游源码 src/backend/utils/errcodes.txt 第 344 行
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42P20 is the windowing_error condition in Class 42.
-
Selected fixed 18.6 parser, parse-analysis, named-window, and executor paths reject invalid frame bounds, forbidden window placement or nesting, and illegal window inheritance. The parser also guards RANGE offset and GROUPS structural requirements.
-
The 18.6 executor assigns negative ROWS or GROUPS frame offsets to 22013 invalid_preceding_or_following_size and NULL frame offsets to 22004 null_value_not_allowed; these boundaries are separate from 42P20.
-
Locked catalogue snapshots show this condition by PostgreSQL 8.4.0; source behavior here is fixed at PostgreSQL 18.6.
同类 SQL 状态码
| 状态码 | 条件名 | 宏名称 | 严重等级 | 版本 |
|---|---|---|---|---|
| 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 |
| 操作需要唯一排序规则,但无法确定。 | 活跃 | |||