↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

Wiki / SQLSTATE

42P19 — 无效递归

递归查询结构不合法,执行前被检查拒绝。

英文手册覆盖始于 PostgreSQL 8.1. 来源历史与运行验证各自保留独立版本边界。

当前阅读 PG 18·选择有来源记录的版本

aliases
未知
class name
Syntax Error or Access Rule Violation
condition name
invalid_recursion
lang
en
sqlstate
42P19

版本定义 PG 18

42P19 — 无效递归

速览 {#at-a-glance}

42P19(invalid_recursion)递归项运行前会先检查递归查询结构。

含义 {#meaning}

递归查询校验会拒绝无法安全求值的递归项。18.6 核心检查递归项中使用聚合、非递归项 UNION [ALL] 递归项这一必要结构,以及递归引用的上下文和次数。选定的聚合 guard 针对的是聚合(p_hasAggs);不能据此说所有窗口函数都被禁止。

诊断 {#diagnosis}

检查查询结构,而不只是 CTE 名称。分开锚点项与递归项,统计递归查询的引用次数,并检查引用是否出现在子查询或外连接的可空侧:LEFT JOIN 是右侧,RIGHT JOIN 是左侧,FULL JOIN 是两侧;单侧外连接的保留侧不受该 guard 限制。还要检查 INTERSECT/EXCEPT 上下文。找出递归项中的实际聚合,不要把所有窗口表达式都归入此错误。这能把结构性递归错误与 42P18 类型推断失败区分开。

处理 {#response}

将 CTE 改写为一个非递归锚点项,后接一个 UNION 或 UNION ALL 递归项,只在允许的上下文中保留一次递归引用;需要聚合时移到递归步骤之外。重试前验证终止条件和结果基数。如果 ERROR 发生在显式事务中,发送重写后的查询前先 ROLLBACK 或回滚到错误前的保存点。

消息 {#messages}

固定源码中的代表性消息包括: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。占位符由实际对象、列或参数填充。

版本 {#versions}

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

相关 {#related}

来源 {#sources}

源码消息、行号和证据边界见 作者证据。

比较版本

SQLSTATE 证据与已记录的运行版本

claims · identity.class-and-condition
{
  "claim_id": "identity.class-and-condition",
  "limits": "Identity does not enumerate all source branches or client failures.",
  "method": "Read fixed REL_18_6 errcodes.txt and locked facts.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.42P19.18.6"
  ],
  "statement": "42P19 is the invalid_recursion condition in Class 42."
}
claims · mechanism.selected-source-path
{
  "claim_id": "mechanism.selected-source-path",
  "limits": "Representative confirmed core paths only; not exhaustive historical or extension coverage.",
  "method": "Read selected resolved REL_18_6 report groups and message roles.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.call.42P19.e59c077eba1a4a52a69a6d97",
    "src.call.42P19.e6e0586856ee8989b006f7ae",
    "src.call.42P19.3b25492c51b06d773f237993",
    "src.call.42P19.recursion-context"
  ],
  "statement": "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."
}
claims · versions.catalogue-boundary
{
  "claim_id": "versions.catalogue-boundary",
  "limits": "First observed release is a lower bound, not precise behavioral introduction.",
  "method": "Read locked catalogue facts and definition snapshot.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.errcodes.42P19.18.6"
  ],
  "statement": "Locked catalogue snapshots show this condition by PostgreSQL 8.4.0; source behavior here is fixed at PostgreSQL 18.6."
}
messages · message.variant-1
{
  "limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
  "message_id": "message.variant-1",
  "path": "",
  "position": 0,
  "raw": {
    "detail_templates": [],
    "hint_templates": [],
    "id": "message.variant-1",
    "limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
    "primary_template": "aggregate functions are not allowed in a recursive query's recursive term",
    "roles": [
      {
        "role": "errmsg",
        "template": "aggregate functions are not allowed in a recursive query's recursive term"
      }
    ],
    "severity_source": "ERROR",
    "sources": [
      "src.call.42P19.e59c077eba1a4a52a69a6d97"
    ],
    "sqlstate": "42P19"
  },
  "severity": "ERROR",
  "sources": [
    "src.call.42P19.e59c077eba1a4a52a69a6d97"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "aggregate functions are not allowed in a recursive query's recursive term",
      "position": 0,
      "role": "",
      "template": "aggregate functions are not allowed in a recursive query's recursive term"
    }
  ]
}
messages · message.variant-2
{
  "limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
  "message_id": "message.variant-2",
  "path": "",
  "position": 1,
  "raw": {
    "detail_templates": [],
    "hint_templates": [],
    "id": "message.variant-2",
    "limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
    "primary_template": "recursive query \"%s\" does not have the form non-recursive-term UNION [ALL] recursive-term",
    "roles": [
      {
        "role": "errmsg",
        "template": "recursive query \"%s\" does not have the form non-recursive-term UNION [ALL] recursive-term"
      }
    ],
    "severity_source": "ERROR",
    "sources": [
      "src.call.42P19.e6e0586856ee8989b006f7ae"
    ],
    "sqlstate": "42P19"
  },
  "severity": "ERROR",
  "sources": [
    "src.call.42P19.e6e0586856ee8989b006f7ae"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "recursive query \" \" does not have the form non-recursive-term UNION [ALL] recursive-term",
      "position": 0,
      "role": "",
      "template": "recursive query \"%s\" does not have the form non-recursive-term UNION [ALL] recursive-term"
    }
  ]
}
messages · message.variant-3
{
  "limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
  "message_id": "message.variant-3",
  "path": "",
  "position": 2,
  "raw": {
    "detail_templates": [],
    "hint_templates": [],
    "id": "message.variant-3",
    "limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
    "primary_template": "recursive reference to query \"%s\" must not appear more than once",
    "roles": [
      {
        "role": "errmsg",
        "template": "recursive reference to query \"%s\" must not appear more than once"
      }
    ],
    "severity_source": "ERROR",
    "sources": [
      "src.call.42P19.3b25492c51b06d773f237993"
    ],
    "sqlstate": "42P19"
  },
  "severity": "ERROR",
  "sources": [
    "src.call.42P19.3b25492c51b06d773f237993"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "recursive reference to query \" \" must not appear more than once",
      "position": 0,
      "role": "",
      "template": "recursive reference to query \"%s\" must not appear more than once"
    }
  ]
}
sources · src.errcodes.42P19.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "line 345",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.42P19.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L345"
}
sources · src.call.42P19.e59c077eba1a4a52a69a6d97
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "lines 1329-1333",
  "path": "src/backend/parser/parse_agg.c",
  "position": 1,
  "sha256": "6e80a805fddbfc9c7e0f3047b348bf992a20c99f16ce7fee89c7067753027f72",
  "source_id": "src.call.42P19.e59c077eba1a4a52a69a6d97",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_agg.c#L1329-L1333"
}
sources · src.call.42P19.e6e0586856ee8989b006f7ae
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "lines 940-944",
  "path": "src/backend/parser/parse_cte.c",
  "position": 2,
  "sha256": "908eef14ac26654fe0b9c176ef50e56ba710b7fcd8e1239b944d7c2152fc2d6b",
  "source_id": "src.call.42P19.e6e0586856ee8989b006f7ae",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_cte.c#L940-L944"
}
sources · src.call.42P19.3b25492c51b06d773f237993
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "lines 1072-1077",
  "path": "src/backend/parser/parse_cte.c",
  "position": 3,
  "sha256": "908eef14ac26654fe0b9c176ef50e56ba710b7fcd8e1239b944d7c2152fc2d6b",
  "source_id": "src.call.42P19.3b25492c51b06d773f237993",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_cte.c#L1072-L1077"
}
sources · src.call.42P19.recursion-context
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "lines 1056-1069",
  "path": "src/backend/parser/parse_cte.c",
  "position": 4,
  "sha256": "908eef14ac26654fe0b9c176ef50e56ba710b7fcd8e1239b944d7c2152fc2d6b",
  "source_id": "src.call.42P19.recursion-context",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_cte.c#L1056-L1069"
}
sources · src.calls.REL_18_6.42P19
{
  "commit": "",
  "docs_url": "",
  "kind": "local_artifact",
  "location": "",
  "path": "raw/calls/REL_18_6.jsonl",
  "position": 5,
  "sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
  "source_id": "src.calls.REL_18_6.42P19",
  "tag": "",
  "url": ""
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:6c2302d81cb07ddd1765aff6e9c2eba14895eb7bcac9cd7dba76190c5984f726

正文语言: zh-Hans · english-manuals:6c2302d81cb07ddd1765aff6e9c2eba14895eb7bcac9cd7dba76190c5984f726