# 22P03 — invalid_binary_representation

> PostgreSQL SQLSTATE 22P03 的来源与诊断参考。
---

# 22P03

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

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| 字段 | 值 |
| --- | --- |
| SQLSTATE | `22P03` |
| 条件名 | `invalid_binary_representation` |
| 状态 | `有效` |
| 已知存在于 | `7.4` |
| 锁定快照 | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| 宏 | `ERRCODE_INVALID_BINARY_REPRESENTATION` |
| 别名 | `—` |

<!-- source facts: data/errcodes/22P03.json -->
<!-- END SQLSTATE FACTS -->

## 含义 {#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`](../22p02/)、[`22P04`](../22p04/)、[`08P01`](../08p01/)

## 来源 {#sources}
[`src/backend/commands/copyfromparse.c#L2047-L2055`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/copyfromparse.c#L2047)

[`src/backend/tcop/postgres.c#L1922-L1945`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L1922)

[`src/backend/tcop/fastpath.c#L426-L448`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/fastpath.c#L426)

[`src/backend/replication/logical/worker.c#L832-L853`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/replication/logical/worker.c#L832)

[`src/backend/utils/adt/arrayfuncs.c#L1476-L1510`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/arrayfuncs.c#L1476)

[`src/backend/utils/adt/numeric.c#L1093-L1123`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/numeric.c#L1093)

[`src/backend/tcop/postgres.c#L416-L445`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L416)

[`src/backend/tcop/postgres.c#L4483-L4504`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L4483)

[`src/backend/tcop/postgres.c#L4843-L4875`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L4843)

[`doc/src/sgml/protocol.sgml#L1287-L1318`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/doc/src/sgml/protocol.sgml#L1287-L1318)

[`doc/src/sgml/protocol.sgml#L7821-L7823`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/doc/src/sgml/protocol.sgml#L7821-L7823)

结构化[证据记录](../../data/evidence/22p03.json)保存精确主消息模板以及源码/运行边界。
