# 42P20 — 窗口函数错误

> PostgreSQL SQLSTATE 42P20：来源与诊断参考。
---

# 42P20 — 窗口函数错误

## 速览 {#at-a-glance}

`42P20`（**windowing_error**）窗口定义同时约束函数调用和框架边界。

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| 字段 | 值 |
| --- | --- |
| SQLSTATE | `42P20` |
| 条件名 | `windowing_error` |
| 状态 | `有效` |
| 已知存在于 | `8.4.0` |
| 锁定快照 | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| 宏 | `ERRCODE_WINDOWING_ERROR` |
| 别名 | `—` |

<!-- source facts: data/errcodes/42P20.json -->
<!-- END SQLSTATE FACTS -->

## 含义 {#meaning}

固定核心在窗口函数放置或嵌套错误，以及窗口定义结构无效时使用 `42P20`。解析器语法拒绝不可能的框架起止组合；解析分析拒绝在不能包含窗口函数的子句中使用窗口函数，或把一个窗口函数放入另一个窗口函数；命名窗口转换则拒绝冲突的继承方式。合法框架对某一行仍可能为空，所以结果为空本身不是 `42P20` 信号。

## 诊断 {#diagnosis}

先按 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`。

## 处理 {#response}

把嵌套计算移到外层查询，或把窗口表达式移出消息指出的命名子句。只修复对应的框架语法，为 `RANGE`/`GROUPS` 补上所需排序，并定义一个没有冲突覆盖的命名窗口。不要因为某一当前行的框架没有行就改动本来合法的框架。如果该 `ERROR` 发生在显式事务中，重试前先执行 `ROLLBACK`，或回滚到语句前建立的保存点；自动提交模式下，失败语句返回空闲后即可提交修正查询。

## 消息 {#messages}

固定源码中的代表性消息包括：

- **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`

占位符由实际对象、列或参数填充。

## 版本 {#versions}

锁定目录显示该条件最早见于 PostgreSQL 8.4.0；行为说明固定在 PostgreSQL 18.6 源码，目录存在范围不等于每条消息或功能都从该版本开始。

## 相关 {#related}

- [`42P19`](../42p19/)
- [`42803`](../42803/)

## 来源 {#sources}

源码消息、行号和证据边界见 [作者证据](../../data/evidence/42p20.json)。

- `src/backend/executor/execExpr.c:1155-1157` ([fixed source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/executor/execExpr.c#L1155))
- `src/backend/parser/gram.y:16499-16544` ([fixed source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/gram.y#L16499))
- `src/backend/parser/parse_agg.c:1041-1046` ([fixed source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_agg.c#L1041))
- `src/backend/parser/parse_clause.c:2790-2947` ([fixed source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_clause.c#L2790))
- `src/backend/executor/nodeWindowAgg.c:2139-2210` ([fixed source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/executor/nodeWindowAgg.c#L2139))
