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

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

Wiki / SQLSTATE

P0002 — no_data_found

PL/pgSQL 中查询未取到所需的数据行。

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

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

aliases
未知
class name
PL/pgSQL Error
condition name
no_data_found
lang
en
sqlstate
P0002

版本定义 PG 18

P0002

速览 {#at-a-glance}

P0002 是 PL/pgSQL 专用的 no_data_found。固定源码确认它既有 PL/pgSQL 查询契约中的 ERROR,也有核心 tablespace 路径中的 NOTICE;不能把它等同于所有返回零行的 SELECT。

含义 {#meaning}

在 PL/pgSQL 固定路径中,受检查的查询没有返回行时会报告 query returned no rows,若启用 strict 参数打印还会附带 parameters: %s detail。另一个固定路径在 tablespace 没有匹配 relation 时以 NOTICE 使用同一 SQLSTATE。

诊断 {#diagnosis}

先确认完整 message 和 severity,再定位操作。PL/pgSQL 路径要检查目标赋值、严格单行契约和 print_strict_params;tablespace NOTICE 则检查目标 tablespace 与匹配 relation。普通查询返回零行本身不会自动等于 P0002。

处置 {#response}

对 PL/pgSQL 严格查询,修正查询预期、目标赋值或显式处理无行分支;也可以在明确业务允许时改用不要求单行的逻辑。对 tablespace NOTICE,先核对没有匹配 relation 是否符合预期;如果这是正常结果,无需修复,否则再修正目标后继续。不要仅因零行就自动重试。

版本 {#versions}

锁定目录从 8.2.0 记录该条件,并在列出的正式快照及 19beta3 中出现;固定源码还显示它可以是 ERROR 或 NOTICE。

相关条件 {#related}

P0003, P0004, 02000

来源 {#sources}

可直接阅读固定的 pl_exec.c 与 tablecmds.c 路径;完整范围见结构化证据记录。

比较版本

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

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read the fixed definition row.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "P0002 is no_data_found in PostgreSQL-specific Class P0."
}
claims · strict-query
{
  "claim_id": "strict-query",
  "limits": "",
  "method": "Read both pl_exec report groups and their conditional errdetail.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.path.plpgsql"
  ],
  "statement": "PL/pgSQL fixed paths report P0002 with `query returned no rows` when the checked query produced no row; optional detail is `parameters: %s` when strict-parameter printing is enabled."
}
claims · notice-path
{
  "claim_id": "notice-path",
  "limits": "",
  "method": "Compare the literal NOTICE level with the PL/pgSQL ERROR paths.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.path.tablespace"
  ],
  "statement": "A core tablespace path also uses P0002 at NOTICE level for no matching relations in a tablespace."
}
claims · scope
{
  "claim_id": "scope",
  "limits": "No runtime case was executed in this batch.",
  "method": "Read the surrounding target-row and strict checks.",
  "position": 3,
  "runtime": [],
  "sources": [
    "src.path.plpgsql"
  ],
  "statement": "P0002 is not a synonym for every SELECT that happens to return zero rows; the operation and enforcing path determine whether it is raised."
}
messages · message.no-rows
{
  "limits": "",
  "message_id": "message.no-rows",
  "path": "",
  "position": 0,
  "raw": {
    "detail_template": "parameters: %s",
    "id": "message.no-rows",
    "primary_template": "query returned no rows",
    "severity_source": "ERROR",
    "sources": [
      "src.path.plpgsql"
    ],
    "sqlstate": "P0002"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.plpgsql"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "query returned no rows",
      "position": 0,
      "role": "",
      "template": "query returned no rows"
    },
    {
      "kind": "detail",
      "literal": "parameters:",
      "position": 1,
      "role": "",
      "template": "parameters: %s"
    }
  ]
}
messages · message.no-tablespace-relations
{
  "limits": "",
  "message_id": "message.no-tablespace-relations",
  "path": "",
  "position": 1,
  "raw": {
    "id": "message.no-tablespace-relations",
    "primary_template": "no matching relations in tablespace \"%s\" found",
    "severity_source": "NOTICE",
    "sources": [
      "src.path.tablespace"
    ],
    "sqlstate": "P0002"
  },
  "severity": "NOTICE",
  "sources": [
    "src.path.tablespace"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "no matching relations in tablespace \" \" found",
      "position": 0,
      "role": "",
      "template": "no matching relations in tablespace \"%s\" found"
    }
  ]
}
sources · src.errcodes.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L492"
}
sources · src.calls.REL_18_6
{
  "commit": "",
  "docs_url": "",
  "kind": "local_artifact",
  "location": "",
  "path": "raw/calls/REL_18_6.jsonl",
  "position": 1,
  "sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
  "source_id": "src.calls.REL_18_6",
  "tag": "",
  "url": ""
}
sources · src.path.plpgsql
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/pl/plpgsql/src/pl_exec.c",
  "position": 2,
  "sha256": "0df3c70a6bccf6dddf443fb99e152a16a67009827c44a820597e98e585cb8b20",
  "source_id": "src.path.plpgsql",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/pl/plpgsql/src/pl_exec.c#L4384-L4387"
}
sources · src.path.tablespace
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/commands/tablecmds.c",
  "position": 3,
  "sha256": "422dc8e833df4940755973c757e338295822ba3e4c7266c40669f49f96eb15a9",
  "source_id": "src.path.tablespace",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/tablecmds.c#L17162-L17166"
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:b3b55af9d3e0e9ccc8e3805da12d3343a97bedefe383dc704a14e19d40df2061

正文语言: zh-Hans · english-manuals:b3b55af9d3e0e9ccc8e3805da12d3343a97bedefe383dc704a14e19d40df2061