百科 / 错误代码 / Class 22 数据异常
22P03 invalid_binary_representation
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: %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。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
incorrect binary data format
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
诊断
先确定传输来自扩展协议 Bind、二进制 COPY、快速路径、逻辑复制,还是某个类型的二进制接收函数。保留参数、参数位置、函数参数或远端列号及完整报文。比较生产端格式码和类型 OID 与接收端预期;若是接收函数内部错误,检查它是否消费了长度限定的完整缓冲区,剩余 cursor(游标位置)通常表示格式不匹配而不是普通文本值错误。
处理
修正生产端或类型契约,再从发生错误的协议边界重试。任何位于显式事务内的客户端 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 本身推断出来。逻辑复制应修正发布端/接收端的二进制编码器或类型定义,并交给复制后台进程自己的重试策略;本页没有依据要求杀掉或重置所有连接。
版本
锁定目录从 PostgreSQL 7.4 记录此条件。协议、复制、数组和 numeric 路径来自 PostgreSQL 18.6 REL_18_6;本次没有运行二进制协议案例。
来源
- 上游源码 doc/src/sgml/protocol.sgml 第 1287–1318 行
- 上游源码 doc/src/sgml/protocol.sgml 第 7821–7823 行
- 上游源码 src/backend/utils/errcodes.txt 第 207 行
- 上游源码 src/backend/utils/adt/arrayfuncs.c 第 1296 行
- 上游源码 src/backend/utils/adt/arrayfuncs.c 第 1476 行
- 上游源码 src/backend/tcop/postgres.c 第 1922 行
- 上游源码 src/backend/commands/copyfromparse.c 第 2047 行
- 上游源码 src/backend/tcop/fastpath.c 第 426 行
- 上游源码 src/backend/tcop/postgres.c 第 4843 行
- 上游源码 src/backend/replication/logical/worker.c 第 832 行
- 上游源码 src/backend/utils/adt/numeric.c 第 1093 行
- 上游源码 src/backend/tcop/postgres.c 第 416–445 行
- 上游源码 src/backend/tcop/postgres.c 第 4483 行
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
22P03 is invalid_binary_representation in SQLSTATE Class 22.
-
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.
-
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.
-
Built-in binary receive functions can emit 22P03 for invalid internal layouts, including array dimensions/flags/element framing and numeric sign/scale/digit fields.
-
The locked catalogue records 22P03 from 7.4; that boundary does not prove exact implementation introduction.
同类错误代码
Class 22 数据异常 下的其他成员。
22000data_exception22001string_data_right_truncation22002null_value_no_indicator_parameter22003numeric_value_out_of_range22004null_value_not_allowed22005error_in_assignment22007invalid_datetime_format22008datetime_field_overflow22009invalid_time_zone_displacement_value2200Bescape_character_conflict2200Cinvalid_use_of_escape_character2200Dinvalid_escape_octet2200Fzero_length_character_string2200Gmost_specific_type_mismatch2200Hsequence_generator_limit_exceeded2200Lnot_an_xml_document2200Minvalid_xml_document2200Ninvalid_xml_content2200Sinvalid_xml_comment2200Tinvalid_xml_processing_instruction22010invalid_indicator_parameter_value22011substring_error22012division_by_zero22013invalid_preceding_or_following_size22014invalid_argument_for_ntile_function22015interval_field_overflow22016invalid_argument_for_nth_value_function22018invalid_character_value_for_cast22019invalid_escape_character2201Binvalid_regular_expression2201Einvalid_argument_for_logarithm2201Finvalid_argument_for_power_function2201Ginvalid_argument_for_width_bucket_function2201Winvalid_row_count_in_limit_clause2201Xinvalid_row_count_in_result_offset_clause22021character_not_in_repertoire22022indicator_overflow22023invalid_parameter_value22024unterminated_c_string22025invalid_escape_sequence22026string_data_length_mismatch22027trim_error2202Earray_subscript_error2202Ginvalid_tablesample_repeat2202Hinvalid_tablesample_argument22030duplicate_json_object_key_value22031invalid_argument_for_sql_json_datetime_function22032invalid_json_text22033invalid_sql_json_subscript22034more_than_one_sql_json_item22035no_sql_json_item22036non_numeric_sql_json_item22037non_unique_keys_in_a_json_object22038singleton_sql_json_item_required22039sql_json_array_not_found2203Asql_json_member_not_found2203Bsql_json_number_not_found2203Csql_json_object_not_found2203Dtoo_many_json_array_elements2203Etoo_many_json_object_members2203Fsql_json_scalar_required2203Gsql_json_item_cannot_be_cast_to_target_type22P01floating_point_exception22P02invalid_text_representation22P04bad_copy_file_format22P05untranslatable_character22P06nonstandard_use_of_escape_character