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

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

Wiki / SQLSTATE

22P03 — invalid_binary_representation

二进制数据格式无效,如 Bind 参数或二进制 COPY。

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

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

aliases
未知
class name
Data Exception
condition name
invalid_binary_representation
lang
en
sqlstate
22P03

版本定义 PG 18

22P03

速览 {#at-a-glance}

22P03 是 PostgreSQL 的二进制表示无效边界。固定源码覆盖二进制 COPY 字段、扩展协议 Bind 参数、快速路径函数参数、逻辑复制列,以及若干类型接收函数内部的格式检查。

含义 {#meaning}

接收端被要求解码二进制字节,但字节不符合接收端格式,或接收函数没有消费完整缓冲区。二进制 COPY 在类型接收函数返回后发现剩余字节时报告 incorrect binary data format;Bind 报告 incorrect binary data format in bind parameter %d;快速路径报告 incorrect binary data format in function argument %d;逻辑复制报告 incorrect binary data format in logical replication column %d。数组和 numeric 等代表性类型接收函数还会因维度/标志位、数组元素帧,或 numeric 的 sign/scale/digit 字段(符号、标度/小数位数和数字位)无效而使用该状态码。

这不同于文本输入函数解析失败,也不同于 COPY 文件头或行框架错误 22P04。必须结合实际接收边界和类型 OID 判断。

报文 {#messages}

固定源码中的代表性主消息是:二进制 COPY 使用 incorrect binary data format,Bind 使用 incorrect binary data format in bind parameter %d,快速路径使用 incorrect binary data format in function argument %d,逻辑复制使用 incorrect binary data format in logical replication column %d。类型接收函数还使用 invalid number of dimensions: %d、invalid array flags、insufficient data left in message、improper binary format in array element %d、invalid sign in external "numeric" value、invalid scale in external "numeric" value 和 invalid digit in external "numeric" value;本次固定调用中的这些变体都是 ERROR,没有 DETAIL 或 HINT。

诊断 {#diagnosis}

先确定传输来自扩展协议 Bind、二进制 COPY、快速路径、逻辑复制,还是某个类型的二进制接收函数。保留参数、参数位置、函数参数或远端列号及完整报文。比较生产端格式码和类型 OID 与接收端预期;若是接收函数内部错误,检查它是否消费了长度限定的完整缓冲区,剩余 cursor(游标位置)通常表示格式不匹配而不是普通文本值错误。

处理 {#response}

修正生产端或类型契约,再从发生错误的协议边界重试。任何位于显式事务内的客户端 ERROR(包括 Bind 错误)都会使事务进入中止状态;重试前执行 ROLLBACK,或执行此前已建立的 ROLLBACK TO SAVEPOINT。Sync 和 ReadyForQuery 只恢复协议同步并报告状态,不会清除 INERROR。扩展协议 Bind 出错后,后端会跳过前端消息直到下一个 Sync;客户端应发送 Sync、消费 ReadyForQuery 后再发下一条扩展协议操作。自动提交模式下,等待这个边界后再重新提交修正后的操作。快速路径 FunctionCall 不在扩展协议的跳过标志内,正常循环可以返回 ReadyForQuery,但显式事务仍适用同一恢复规则。COPY FROM STDIN 应按 COPY 协议结束当前坏流,仍处于 COPY-in 时可在适当情况下使用 CopyFail。如果 COPY 由扩展协议发起,后端发出 ErrorResponse 后,客户端应发送 Sync 并等待 ReadyForQuery;如果由 simple Query 发起,剩余查询消息会被丢弃,随后直接发送 ReadyForQuery;客户端不需要发送 Sync,消费该 ReadyForQuery 后再发送下一条查询。不要在 COPY-in 期间发送普通 SQL。如果读取客户端消息时连协议帧本身都丢失,PostgreSQL 另有协议同步丢失的 FATAL 路径;不能把这种连接终止从二进制格式 ERROR 本身推断出来。逻辑复制应修正发布端/接收端的二进制编码器或类型定义,并交给复制后台进程自己的重试策略;本页没有依据要求杀掉或重置所有连接。

版本 {#versions}

锁定目录从 PostgreSQL 7.4 记录此条件。协议、复制、数组和 numeric 路径来自 PostgreSQL 18.6 REL_18_6;本次没有运行二进制协议案例。

相关 {#related}

22P02、22P04、08P01

来源 {#sources}

src/backend/commands/copyfromparse.c#L2047-L2055

src/backend/tcop/postgres.c#L1922-L1945

src/backend/tcop/fastpath.c#L426-L448

src/backend/replication/logical/worker.c#L832-L853

src/backend/utils/adt/arrayfuncs.c#L1476-L1510

src/backend/utils/adt/numeric.c#L1093-L1123

src/backend/tcop/postgres.c#L416-L445

src/backend/tcop/postgres.c#L4483-L4504

src/backend/tcop/postgres.c#L4843-L4875

doc/src/sgml/protocol.sgml#L1287-L1318

doc/src/sgml/protocol.sgml#L7821-L7823

结构化证据记录保存精确主消息模板以及源码/运行边界。

比较版本

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

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read fixed definition.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "22P03 is invalid_binary_representation in SQLSTATE Class 22."
}
claims · protocol-and-copy
{
  "claim_id": "protocol-and-copy",
  "limits": "",
  "method": "Read the complete receive-and-cursor guards.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.path.copy-binary",
    "src.path.bind",
    "src.path.protocol-flags",
    "src.path.protocol-recovery",
    "src.path.fastpath-dispatch",
    "doc.protocol.copy",
    "doc.protocol.status"
  ],
  "statement": "Binary COPY and extended-protocol Bind call a type receive function and reject leftover bytes after the receiver returns; COPY uses the unqualified primary and Bind names the parameter number. The frontend loop marks Bind/Parse for skip-until-Sync recovery, while FunctionCall and COPY data are not marked by that extended-query flag. The protocol documentation distinguishes extended-query COPY (ErrorResponse, then Sync and ReadyForQuery) from simple Query COPY (discard the rest of the query and send ReadyForQuery without Sync); ReadyForQuery reports status but does not itself clear an aborted transaction. A separate protocol-synchronization loss path is FATAL."
}
claims · fastpath-and-replication
{
  "claim_id": "fastpath-and-replication",
  "limits": "",
  "method": "Read the complete binary branches and cursor reset/leftover checks.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.path.fastpath",
    "src.path.logicalrep"
  ],
  "statement": "Fast-path arguments and logical-replication binary columns use the same consume-the-whole-buffer guard, with argument or remote-column context in the primary. Logical replication resets the StringInfo cursor before receive because some paths reparse the tuple."
}
claims · type-receivers
{
  "claim_id": "type-receivers",
  "limits": "The listed type paths are representative, not an exhaustive inventory of all receive functions.",
  "method": "Read representative type receive guards.",
  "position": 3,
  "runtime": [],
  "sources": [
    "src.path.array",
    "src.path.array-elements",
    "src.path.numeric"
  ],
  "statement": "Built-in binary receive functions can emit 22P03 for invalid internal layouts, including array dimensions/flags/element framing and numeric sign/scale/digit fields."
}
claims · history
{
  "claim_id": "history",
  "limits": "",
  "method": "Use catalogue boundary.",
  "position": 4,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "The locked catalogue records 22P03 from 7.4; that boundary does not prove exact implementation introduction."
}
messages · message.copy
{
  "limits": "",
  "message_id": "message.copy",
  "path": "",
  "position": 0,
  "raw": {
    "id": "message.copy",
    "primary_template": "incorrect binary data format",
    "severity_source": "ERROR",
    "sources": [
      "src.path.copy-binary"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.copy-binary"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "incorrect binary data format",
      "position": 0,
      "role": "",
      "template": "incorrect binary data format"
    }
  ]
}
messages · message.bind
{
  "limits": "",
  "message_id": "message.bind",
  "path": "",
  "position": 1,
  "raw": {
    "id": "message.bind",
    "primary_template": "incorrect binary data format in bind parameter %d",
    "severity_source": "ERROR",
    "sources": [
      "src.path.bind"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.bind"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "incorrect binary data format in bind parameter",
      "position": 0,
      "role": "",
      "template": "incorrect binary data format in bind parameter %d"
    }
  ]
}
messages · message.fastpath
{
  "limits": "",
  "message_id": "message.fastpath",
  "path": "",
  "position": 2,
  "raw": {
    "id": "message.fastpath",
    "primary_template": "incorrect binary data format in function argument %d",
    "severity_source": "ERROR",
    "sources": [
      "src.path.fastpath"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.fastpath"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "incorrect binary data format in function argument",
      "position": 0,
      "role": "",
      "template": "incorrect binary data format in function argument %d"
    }
  ]
}
messages · message.logicalrep
{
  "limits": "",
  "message_id": "message.logicalrep",
  "path": "",
  "position": 3,
  "raw": {
    "id": "message.logicalrep",
    "primary_template": "incorrect binary data format in logical replication column %d",
    "severity_source": "ERROR",
    "sources": [
      "src.path.logicalrep"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.logicalrep"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "incorrect binary data format in logical replication column",
      "position": 0,
      "role": "",
      "template": "incorrect binary data format in logical replication column %d"
    }
  ]
}
messages · message.array-dim
{
  "limits": "",
  "message_id": "message.array-dim",
  "path": "",
  "position": 4,
  "raw": {
    "id": "message.array-dim",
    "primary_template": "invalid number of dimensions: %d",
    "severity_source": "ERROR",
    "sources": [
      "src.path.array"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.array"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "invalid number of dimensions:",
      "position": 0,
      "role": "",
      "template": "invalid number of dimensions: %d"
    }
  ]
}
messages · message.array-flags
{
  "limits": "",
  "message_id": "message.array-flags",
  "path": "",
  "position": 5,
  "raw": {
    "id": "message.array-flags",
    "primary_template": "invalid array flags",
    "severity_source": "ERROR",
    "sources": [
      "src.path.array"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.array"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "invalid array flags",
      "position": 0,
      "role": "",
      "template": "invalid array flags"
    }
  ]
}
messages · message.array-data
{
  "limits": "",
  "message_id": "message.array-data",
  "path": "",
  "position": 6,
  "raw": {
    "id": "message.array-data",
    "primary_template": "insufficient data left in message",
    "severity_source": "ERROR",
    "sources": [
      "src.path.array-elements"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.array-elements"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "insufficient data left in message",
      "position": 0,
      "role": "",
      "template": "insufficient data left in message"
    }
  ]
}
messages · message.array-element
{
  "limits": "",
  "message_id": "message.array-element",
  "path": "",
  "position": 7,
  "raw": {
    "id": "message.array-element",
    "primary_template": "improper binary format in array element %d",
    "severity_source": "ERROR",
    "sources": [
      "src.path.array-elements"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.array-elements"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "improper binary format in array element",
      "position": 0,
      "role": "",
      "template": "improper binary format in array element %d"
    }
  ]
}
messages · message.numeric-sign
{
  "limits": "",
  "message_id": "message.numeric-sign",
  "path": "",
  "position": 8,
  "raw": {
    "id": "message.numeric-sign",
    "primary_template": "invalid sign in external \"numeric\" value",
    "severity_source": "ERROR",
    "sources": [
      "src.path.numeric"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.numeric"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "invalid sign in external \"numeric\" value",
      "position": 0,
      "role": "",
      "template": "invalid sign in external \"numeric\" value"
    }
  ]
}
messages · message.numeric-scale
{
  "limits": "",
  "message_id": "message.numeric-scale",
  "path": "",
  "position": 9,
  "raw": {
    "id": "message.numeric-scale",
    "primary_template": "invalid scale in external \"numeric\" value",
    "severity_source": "ERROR",
    "sources": [
      "src.path.numeric"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.numeric"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "invalid scale in external \"numeric\" value",
      "position": 0,
      "role": "",
      "template": "invalid scale in external \"numeric\" value"
    }
  ]
}
messages · message.numeric-digit
{
  "limits": "",
  "message_id": "message.numeric-digit",
  "path": "",
  "position": 10,
  "raw": {
    "id": "message.numeric-digit",
    "primary_template": "invalid digit in external \"numeric\" value",
    "severity_source": "ERROR",
    "sources": [
      "src.path.numeric"
    ],
    "sqlstate": "22P03"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.numeric"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "invalid digit in external \"numeric\" value",
      "position": 0,
      "role": "",
      "template": "invalid digit in external \"numeric\" value"
    }
  ]
}
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#L207"
}
sources · src.path.copy-binary
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/commands/copyfromparse.c",
  "position": 1,
  "sha256": "bc42754023579cd782a3cb3f144ad2c2c3d3aa46119eb7567fcdb05f2d445571",
  "source_id": "src.path.copy-binary",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/copyfromparse.c#L2047"
}
sources · src.path.bind
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/tcop/postgres.c",
  "position": 2,
  "sha256": "9fb62275b1badf94d01ab351337b60410cd9b3ab1fe63fa9f23d6d2185a21061",
  "source_id": "src.path.bind",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L1922"
}
sources · src.path.fastpath
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/tcop/fastpath.c",
  "position": 3,
  "sha256": "c42985c83293efe0bdf1c265930e0d4e13d6bb2eb23040815e638ad9244eab6c",
  "source_id": "src.path.fastpath",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/fastpath.c#L426"
}
sources · src.path.logicalrep
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/replication/logical/worker.c",
  "position": 4,
  "sha256": "096c5c46ffee6aab907ec5dad467100c5e3e8a4d398332d62dd302ab9c2ebf18",
  "source_id": "src.path.logicalrep",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/replication/logical/worker.c#L832"
}
sources · src.path.array
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/adt/arrayfuncs.c",
  "position": 5,
  "sha256": "1f1c9dab1e5f8c936bbfe6c28a3011365b433367e1854166bbec9d2e97341177",
  "source_id": "src.path.array",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/arrayfuncs.c#L1296"
}
sources · src.path.array-elements
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/adt/arrayfuncs.c",
  "position": 6,
  "sha256": "1f1c9dab1e5f8c936bbfe6c28a3011365b433367e1854166bbec9d2e97341177",
  "source_id": "src.path.array-elements",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/arrayfuncs.c#L1476"
}
sources · src.path.numeric
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/adt/numeric.c",
  "position": 7,
  "sha256": "6cde7cd60b3330b83be35d1f014045f5fb5fe737f3c5db4c8ed4080b99f2b666",
  "source_id": "src.path.numeric",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/numeric.c#L1093"
}
sources · src.path.protocol-recovery
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/tcop/postgres.c",
  "position": 8,
  "sha256": "9fb62275b1badf94d01ab351337b60410cd9b3ab1fe63fa9f23d6d2185a21061",
  "source_id": "src.path.protocol-recovery",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L4483"
}
sources · src.path.fastpath-dispatch
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/tcop/postgres.c",
  "position": 9,
  "sha256": "9fb62275b1badf94d01ab351337b60410cd9b3ab1fe63fa9f23d6d2185a21061",
  "source_id": "src.path.fastpath-dispatch",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L4843"
}
sources · src.path.protocol-flags
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/tcop/postgres.c",
  "position": 10,
  "sha256": "9fb62275b1badf94d01ab351337b60410cd9b3ab1fe63fa9f23d6d2185a21061",
  "source_id": "src.path.protocol-flags",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L416-L445"
}
sources · doc.protocol.copy
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "https://www.postgresql.org/docs/18/protocol.html",
  "kind": "upstream_source",
  "location": "",
  "path": "doc/src/sgml/protocol.sgml",
  "position": 11,
  "sha256": "745339e07ebbc2bb67d98b258e25772ada644ef26011e3cd6b3fcbb435235f89",
  "source_id": "doc.protocol.copy",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/doc/src/sgml/protocol.sgml#L1287-L1318"
}
sources · doc.protocol.status
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "https://www.postgresql.org/docs/18/protocol.html",
  "kind": "upstream_source",
  "location": "",
  "path": "doc/src/sgml/protocol.sgml",
  "position": 12,
  "sha256": "745339e07ebbc2bb67d98b258e25772ada644ef26011e3cd6b3fcbb435235f89",
  "source_id": "doc.protocol.status",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/doc/src/sgml/protocol.sgml#L7821-L7823"
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:d52f31901b33c0a6734347a6d8cffa1130429a61723ad6c3ea1b538efbac3771

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