百科 / 错误代码 / Class 42 语法错误或访问规则冲突
42P19 invalid_recursion
无效递归
ERROR 源码确认 参考 未实测
- 条件名
invalid_recursion- 宏名称
ERRCODE_INVALID_RECURSION- 启用版本
- 8.4
- 状态
- 活跃
版本覆盖
速览
42P19(invalid_recursion)递归项运行前会先检查递归查询结构。
含义
递归查询校验会拒绝无法安全求值的递归项。18.6 核心检查递归项中使用聚合、非递归项 UNION [ALL] 递归项这一必要结构,以及递归引用的上下文和次数。选定的聚合 guard 针对的是聚合(p_hasAggs);不能据此说所有窗口函数都被禁止。
诊断
检查查询结构,而不只是 CTE 名称。分开锚点项与递归项,统计递归查询的引用次数,并检查引用是否出现在子查询或外连接的可空侧:LEFT JOIN 是右侧,RIGHT JOIN 是左侧,FULL JOIN 是两侧;单侧外连接的保留侧不受该 guard 限制。还要检查 INTERSECT/EXCEPT 上下文。找出递归项中的实际聚合,不要把所有窗口表达式都归入此错误。这能把结构性递归错误与 42P18 类型推断失败区分开。
处理
将 CTE 改写为一个非递归锚点项,后接一个 UNION 或 UNION ALL 递归项,只在允许的上下文中保留一次递归引用;需要聚合时移到递归步骤之外。重试前验证终止条件和结果基数。如果 ERROR 发生在显式事务中,发送重写后的查询前先 ROLLBACK 或回滚到错误前的保存点。
消息
固定源码中的代表性消息包括:message: aggregate functions are not allowed in a recursive query's recursive term;message: recursive query "%s" does not have the form non-recursive-term UNION [ALL] recursive-term;message: recursive reference to query "%s" must not appear more than once。占位符由实际对象、列或参数填充。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
aggregate functions are not allowed in a recursive query's recursive term
来源:src/backend/parser/parse_agg.c(lines 1329-1333) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
recursive query "%s" does not have the form non-recursive-term UNION [ALL] recursive-term
来源:src/backend/parser/parse_cte.c(lines 940-944) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
recursive reference to query "%s" must not appear more than once
来源:src/backend/parser/parse_cte.c(lines 1072-1077) @ 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_cte.c 第 1072–1077 行
- 上游源码 src/backend/parser/parse_agg.c 第 1329–1333 行
- 上游源码 src/backend/parser/parse_cte.c 第 940–944 行
- 上游源码 src/backend/parser/parse_cte.c 第 1056–1069 行
- 上游源码 src/backend/utils/errcodes.txt 第 345 行
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42P19 is the invalid_recursion condition in Class 42.
-
Core 18.6 checks aggregate use in the recursive term, the required non-recursive UNION [ALL] recursive form, and recursive-reference context and multiplicity. The selected aggregate guard is p_hasAggs; it does not establish that every window function is forbidden. Outer-join context applies to the nullable side (LEFT right, RIGHT left, FULL both), while the preserved side of a one-sided join is not covered by that guard.
-
Locked catalogue snapshots show this condition by PostgreSQL 8.4.0; source behavior here is fixed at PostgreSQL 18.6.
同类错误代码
Class 42 语法错误或访问规则冲突 下的其他成员。
42000syntax_error_or_access_rule_violation42501insufficient_privilege42601syntax_error42602invalid_name42611invalid_column_definition42622name_too_long42701duplicate_column42702ambiguous_column42703undefined_column42704undefined_object42710duplicate_object42712duplicate_alias42723duplicate_function42725ambiguous_function42803grouping_error42804datatype_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_datatype42P20windowing_error42P21collation_mismatch42P22indeterminate_collation