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

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

Wiki / SQLSTATE

42P05 — 重复预备语句

当前会话已有同名的服务端预备语句。

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

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

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

版本定义 PG 18

42P05 — 重复预备语句

速览 {#at-a-glance}

42P05(duplicate_prepared_statement)表示当前后端/会话已有同名服务器端预备语句。连接池可能让后续请求复用这段状态,而另一条连接可以拥有同名的独立语句。

含义 {#meaning}

StorePreparedStatement 把命名语句放在当前后端的 prepared_queries 哈希表中;HASH_ENTER 发现名称已存在时,在保存新计划前报告 ERROR prepared statement "%s" already exists。DEALLOCATE name 删除当前后端的一项,DEALLOCATE ALL 删除当前后端的全部项;它们不会删除另一条连接拥有的语句。

诊断 {#diagnosis}

检查物理连接/后端身份、pg_prepared_statements、连接池签出历史和 PREPARE 路径,区分命名服务器语句、未命名协议语句和驱动本地缓存键。若多个请求复用了同一连接,先核对准确名称和参数类型再修改 SQL。

处理 {#response}

只有 SQL 与参数类型匹配时才复用现有语句;否则由该连接的所有者协调 DEALLOCATE name、使用确定性的唯一名称,或按连接池生命周期策略重置会话。不要在另一条连接执行 DEALLOCATE 后假定冲突已清除。此 ERROR 若发生在显式事务中,应回滚保存点或回滚事务;重连只是受控重置,不是通用修复。

消息变体 {#messages}

确认的 18.6 变体是 ERROR:prepared statement "%s" already exists;%s 是动态的服务器端名称。

版本 {#versions}

目录范围从 7.4 到 18.6、19beta3,不是精确的功能引入判断。选定的 18.6 源码覆盖当前后端的插入与释放路径,未运行数据库。

相关 {#related}

来源 {#sources}

  • src.errcodes.42P05.18.6 — src/backend/utils/errcodes.txt line 381,固定于 724edf9bde9d356724ad384a2e196edc3c9f80f7;SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba(源码)。

  • src.call.42P05.b490336894ec11628c1766f2 — src/backend/commands/prepare.c lines 412-415,固定于 724edf9bde9d356724ad384a2e196edc3c9f80f7;SHA-256 e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956(源码)。

  • src.call.42P05.lifecycle — src/backend/commands/prepare.c lines 401-423、504-511,固定于 724edf9bde9d356724ad384a2e196edc3c9f80f7;SHA-256 e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956(源码)。

  • src.calls.REL_18_6.42P05 — 固定本地核心调用扫描;SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf。

  • 作者证据 — 记录源码断言、消息角色和运行边界。

比较版本

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.42P05.18.6"
  ],
  "statement": "42P05 is the duplicate_prepared_statement 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 complete resolved REL_18_6 report groups and message roles.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.call.42P05.b490336894ec11628c1766f2",
    "src.call.42P05.lifecycle"
  ],
  "statement": "StorePreparedStatement inserts named statements into the current backend's prepared_queries hash and reports ERROR `prepared statement \"%s\" already exists` when HASH_ENTER finds the name. DEALLOCATE removes entries from that backend's hash; another connection has independent state."
}
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 snapshots.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.errcodes.42P05.18.6"
  ],
  "statement": "From 7.4 through 18.6 and 19beta3; this catalogue range is not a precise introduction claim. The selected 18.6 source covers insertion and current-backend deallocation, and runtime was not run."
}
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": "prepared statement \"%s\" already exists",
    "roles": [
      {
        "role": "errmsg",
        "template": "prepared statement \"%s\" already exists"
      }
    ],
    "severity_source": "explicit ERROR",
    "sources": [
      "src.call.42P05.b490336894ec11628c1766f2"
    ],
    "sqlstate": "42P05"
  },
  "severity": "explicit ERROR",
  "sources": [
    "src.call.42P05.b490336894ec11628c1766f2"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "prepared statement \" \" already exists",
      "position": 0,
      "role": "",
      "template": "prepared statement \"%s\" already exists"
    }
  ]
}
sources · src.errcodes.42P05.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "line 381",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.42P05.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L381"
}
sources · src.call.42P05.b490336894ec11628c1766f2
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "lines 412-415",
  "path": "src/backend/commands/prepare.c",
  "position": 1,
  "sha256": "e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956",
  "source_id": "src.call.42P05.b490336894ec11628c1766f2",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/prepare.c#L412-L415"
}
sources · src.call.42P05.lifecycle
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "lines 401-423 and 504-511",
  "path": "src/backend/commands/prepare.c",
  "position": 2,
  "sha256": "e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956",
  "source_id": "src.call.42P05.lifecycle",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/prepare.c#L401-L423"
}
sources · src.calls.REL_18_6.42P05
{
  "commit": "",
  "docs_url": "",
  "kind": "local_artifact",
  "location": "",
  "path": "raw/calls/REL_18_6.jsonl",
  "position": 3,
  "sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
  "source_id": "src.calls.REL_18_6.42P05",
  "tag": "",
  "url": ""
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:e37cbe48dbc0578a88d3141e5290771b240b35383619148dc71ec82c7cbebd85

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