选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 错误代码 / Class 22 数据异常

22P03 invalid_binary_representation

ERROR 源码确认 参考 未实测

类别
Class 22 数据异常
严重等级
ERROR
条件名
invalid_binary_representation
宏名称
ERRCODE_INVALID_BINARY_REPRESENTATION
启用版本
7.4
状态
活跃

版本覆盖

速览

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

含义

接收端被要求解码二进制字节,但字节不符合接收端格式,或接收函数没有消费完整缓冲区。二进制 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 判断。

报文

固定源码中的代表性主消息是:二进制 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: %dinvalid array flagsinsufficient data left in messageimproper binary format in array element %dinvalid sign in external "numeric" valueinvalid scale in external "numeric" valueinvalid digit in external "numeric" value;本次固定调用中的这些变体都是 ERROR,没有 DETAIL 或 HINT。

报文模板

源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。

主消息 incorrect binary data format

来源:src/backend/commands/copyfromparse.c @ REL_18_6

主消息 incorrect binary data format in bind parameter %d

来源:src/backend/tcop/postgres.c @ REL_18_6

主消息 incorrect binary data format in function argument %d

来源:src/backend/tcop/fastpath.c @ REL_18_6

主消息 incorrect binary data format in logical replication column %d

来源:src/backend/replication/logical/worker.c @ REL_18_6

主消息 invalid number of dimensions: %d

来源:src/backend/utils/adt/arrayfuncs.c @ REL_18_6

主消息 invalid array flags

来源:src/backend/utils/adt/arrayfuncs.c @ REL_18_6

主消息 insufficient data left in message

来源:src/backend/utils/adt/arrayfuncs.c @ REL_18_6

主消息 improper binary format in array element %d

来源:src/backend/utils/adt/arrayfuncs.c @ REL_18_6

主消息 invalid sign in external "numeric" value

来源:src/backend/utils/adt/numeric.c @ REL_18_6

主消息 invalid scale in external "numeric" value

来源:src/backend/utils/adt/numeric.c @ REL_18_6

主消息 invalid digit in external "numeric" value

来源:src/backend/utils/adt/numeric.c @ REL_18_6

诊断

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

处理

修正生产端或类型契约,再从发生错误的协议边界重试。任何位于显式事务内的客户端 ERROR(包括 Bind 错误)都会使事务进入中止状态;重试前执行 ROLLBACK,或执行此前已建立的 ROLLBACK TO SAVEPOINTSyncReadyForQuery 只恢复协议同步并报告状态,不会清除 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 本身推断出来。逻辑复制应修正发布端/接收端的二进制编码器或类型定义,并交给复制后台进程自己的重试策略;本页没有依据要求杀掉或重置所有连接。

版本

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

来源

证据

断言

每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。

  • 22P03 is invalid_binary_representation in SQLSTATE Class 22.

    核实方式Read fixed definition.

    来源src/backend/utils/errcodes.txt

  • 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.

    核实方式Read the complete receive-and-cursor guards.

    来源src/backend/commands/copyfromparse.c · src/backend/tcop/postgres.c · src/backend/tcop/postgres.c · src/backend/tcop/postgres.c · src/backend/tcop/postgres.c · doc/src/sgml/protocol.sgml · doc/src/sgml/protocol.sgml

  • 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.

    核实方式Read the complete binary branches and cursor reset/leftover checks.

    来源src/backend/tcop/fastpath.c · src/backend/replication/logical/worker.c

  • Built-in binary receive functions can emit 22P03 for invalid internal layouts, including array dimensions/flags/element framing and numeric sign/scale/digit fields.

    核实方式Read representative type receive guards.

    不覆盖The listed type paths are representative, not an exhaustive inventory of all receive functions.

    来源src/backend/utils/adt/arrayfuncs.c · src/backend/utils/adt/arrayfuncs.c · src/backend/utils/adt/numeric.c

  • The locked catalogue records 22P03 from 7.4; that boundary does not prove exact implementation introduction.

    核实方式Use catalogue boundary.

    来源src/backend/utils/errcodes.txt

同类错误代码

Class 22 数据异常 下的其他成员。