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

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

Wiki / SQLSTATE

P0003 — too_many_rows

PL/pgSQL 中查询返回的行数多于预期。

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

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

aliases
未知
class name
PL/pgSQL Error
condition name
too_many_rows
lang
en
sqlstate
P0003

版本定义 PG 18

P0003

速览 {#at-a-glance}

P0003 是 PL/pgSQL too_many_rows。固定路径给出准确的 message、可选 detail、hint 和按实际分支决定的严重级别。

含义 {#meaning}

当受检查的 PL/pgSQL 查询返回多于一行时,源码报告 query returned more than one row;开启 strict 参数打印时可附带 parameters: %s,并在一条路径提供 Make sure the query returns a single row, or use LIMIT 1. hint。普通多行 SELECT 不会因此自动成为 P0003。

诊断 {#diagnosis}

先看是否是严格目标赋值或 modifying statement,再检查完整 detail/hint。固定源码先按 plpgsql.extra_errors/plpgsql.extra_warnings 对 too_many_rows 的选择设置级别:前者优先并使用 ERROR,只有后者生效时才使用 WARNING;strict/modifying 路径无论这些设置如何都使用 ERROR,所以不要只凭条件名断言严重级别。

处置 {#response}

让查询满足单行契约,或改用能表达多行结果的 PL/pgSQL 结构;若业务允许第一行,必须显式设计排序和截取语义,而不是盲目套用 hint。修正查询契约后再执行,避免把同一多行结果自动重放成重复业务操作。

版本 {#versions}

锁定目录从 8.2.0 记录该条件,并在列出的正式快照及 19beta3 中出现;运行时未在本批次重演。

相关条件 {#related}

P0002, P0004, 02000

来源 {#sources}

可直接阅读固定的 pl_exec.c GUC 分支和报告路径;完整范围见结构化证据记录。

比较版本

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

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read the fixed definition row.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "P0003 is too_many_rows in PostgreSQL-specific Class P0."
}
claims · strict-query
{
  "claim_id": "strict-query",
  "limits": "",
  "method": "Read both report groups, their conditional detail, hint, and errlevel.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.path.plpgsql"
  ],
  "statement": "PL/pgSQL fixed paths report `query returned more than one row`; the strict path may include `parameters: %s` and the source provides the hint `Make sure the query returns a single row, or use LIMIT 1.`."
}
claims · severity-boundary
{
  "claim_id": "severity-boundary",
  "limits": "The hint is present on the SELECT INTO-style branch; the dynamic-execute path has the same primary/detail but no hint in the cited report group.",
  "method": "Read the fixed too_many_rows_level assignment, row-count branch, errlevel selection, and detail condition.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.path.plpgsql"
  ],
  "statement": "For SELECT INTO-style checked queries, plpgsql.extra_errors selecting too_many_rows takes precedence and sets the extra check to ERROR; if it is not selected, plpgsql.extra_warnings selecting too_many_rows sets WARNING. Strict or modifying-statement paths use ERROR regardless, and print_strict_params controls the optional parameters detail."
}
messages · message.too-many-rows
{
  "limits": "",
  "message_id": "message.too-many-rows",
  "path": "",
  "position": 0,
  "raw": {
    "detail_template": "parameters: %s",
    "hint_template": "Make sure the query returns a single row, or use LIMIT 1.",
    "id": "message.too-many-rows",
    "primary_template": "query returned more than one row",
    "severity_source": "ERROR or source-selected errlevel",
    "sources": [
      "src.path.plpgsql"
    ],
    "sqlstate": "P0003"
  },
  "severity": "ERROR or source-selected errlevel",
  "sources": [
    "src.path.plpgsql"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "query returned more than one row",
      "position": 0,
      "role": "",
      "template": "query returned more than one row"
    },
    {
      "kind": "detail",
      "literal": "parameters:",
      "position": 1,
      "role": "",
      "template": "parameters: %s"
    },
    {
      "kind": "hint",
      "literal": "Make sure the query returns a single row, or use LIMIT 1.",
      "position": 2,
      "role": "",
      "template": "Make sure the query returns a single row, or use LIMIT 1."
    }
  ]
}
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#L493"
}
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#L4215-L4220"
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:ffa6714eaaab0bfb91e2ef5ea7ae767021dbcf4dcc0fd2d9ba9157cae26e3814

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