文档 / SQL 状态码 / Class 22 数据异常
22P04 bad_copy_file_format
ERROR 源码确认 参考 未实测
- 条件名
bad_copy_file_format- 宏名称
ERRCODE_BAD_COPY_FILE_FORMAT- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
22P04 是 COPY 文件格式边界。固定解析器在二进制签名和头部、文本或 CSV 帧、头部/行字段数以及二进制字段长度错误时使用它。它表示 COPY 结构错误,不是所有值转换失败的统称。
含义
二进制输入会校验 PGCOPY 签名、flags、扩展长度、行字段数、字段长度和结束标记。代表性主消息包括 COPY file signature not recognized、invalid COPY file header (missing flags)、invalid COPY file header (wrong length)、row field count is %d, expected %d、invalid field size 和 unexpected EOF in COPY data。
文本和 CSV 有独立的帧检查。头部匹配可能报告 wrong number of fields in header line: got %d, expected %d 或列名不匹配;普通行可能报告 extra data after last expected column 或 missing data for column "%s"。CSV 引号和换行可能报告 unterminated CSV quoted field、unquoted carriage return found in data 或 unquoted newline found in data;文本模式使用对应的 literal ... found in data 消息和提示。帧已正确解析但值无法转换时通常属于 22P02;二进制类型接收函数留下未消费字节时可能是 22P03。
报文
固定源码中的代表性主消息均为 ERROR:
- 二进制头部/字段:
COPY file signature not recognized;invalid COPY file header (missing flags);unrecognized critical flags in COPY file header;invalid COPY file header (missing length);invalid COPY file header (wrong length);invalid field size;unexpected EOF in COPY data。 - 头部和行:
wrong number of fields in header line: got %d, expected %d;column name mismatch in header line field %d: got "%s", expected "%s";extra data after last expected column;missing data for column "%s";row field count is %d, expected %d。 - CSV 和行帧:
unterminated CSV quoted field;literal carriage return found in data;unquoted carriage return found in data;literal newline found in data;unquoted newline found in data;end-of-copy marker is not alone on its line。回车/换行变体还会携带使用\r、\n或带引号 CSV 字段的源码提示。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
COPY file signature not recognized
invalid COPY file header (missing flags)
unrecognized critical flags in COPY file header
invalid COPY file header (missing length)
invalid COPY file header (wrong length)
wrong number of fields in header line: got %d, expected %d
column name mismatch in header line field %d: got "%s", expected "%s"
extra data after last expected column
missing data for column "%s"
row field count is %d, expected %d
invalid field size
unexpected EOF in COPY data
unterminated CSV quoted field
literal carriage return found in data
Use "\r" to represent carriage return.
unquoted carriage return found in data
Use quoted CSV field to represent carriage return.
literal newline found in data
Use "\n" to represent newline.
unquoted newline found in data
Use quoted CSV field to represent newline.
end-of-copy marker is not alone on its line
诊断
先确定来源是文本、CSV、二进制 COPY 还是前端 COPY-in。保留完整主消息,因为它能定位解析阶段。依次检查二进制签名/标志位/长度、头部和目标列顺序、行字段数、CSV 引号/转义与换行规则,以及文本 COPY 的数据结束标记是否单独占行,之后再检查目标类型的输入转换。
ON_ERROR IGNORE 不是通用的坏行跳过开关。固定文本/CSV 路径会围绕安全的类型输入转换处理软错误,并可发出通知后跳过数据类型不兼容的行;头部、字段数、行帧、CSV 引号和二进制结构错误仍以 ERROR 抛出,不能都靠这个选项跳过。
处理
按声明的文本/CSV/二进制格式、目标列顺序,重新生成带正确头部、长度、引号和行格式的流。前端 COPY-in 出错时,按当前 COPY 子协议状态结束输入,适当时使用 CopyFail。如果 COPY 由扩展协议发起且后端发送 ErrorResponse,客户端应发送 Sync 并等待 ReadyForQuery;如果由 simple Query 发起,剩余查询消息会被丢弃,随后直接发送 ReadyForQuery;客户端不需要发送 Sync,消费该 ReadyForQuery 后再发送下一条查询。不要在 COPY-in 期间发送普通 SQL。显式事务中的 ERROR 要在协议边界恢复后执行 ROLLBACK 或 ROLLBACK TO SAVEPOINT;ReadyForQuery 只报告状态,不能替代事务恢复。ON_ERROR IGNORE 只可能适用于文档所说的安全类型输入失败,不能修复坏头或损坏的 CSV/二进制帧。普通 COPY ERROR 本身不要求重置连接。
版本
锁定目录从 PostgreSQL 7.4 记录此条件。引用的解析器及 ON_ERROR 边界来自 PostgreSQL 18.6 REL_18_6;本次没有运行自然 COPY 文件案例。
来源
- 上游源码 doc/src/sgml/protocol.sgml 第 1287–1318 行
- 上游源码 src/backend/utils/errcodes.txt 第 208 行
- 上游源码 src/backend/commands/copyfromparse.c 第 2012 行
- 上游源码 src/backend/commands/copyfromparse.c 第 190 行
- 上游源码 src/backend/commands/copyfromparse.c 第 1084 行
- 上游源码 src/backend/commands/copyfromparse.c 第 1465 行
- 上游源码 src/backend/commands/copyfromparse.c 第 1818 行
- 上游源码 src/backend/commands/copyfromparse.c 第 1388 行
- 上游源码 src/backend/commands/copyfromparse.c 第 1026 行
- 上游源码 src/backend/tcop/postgres.c 第 416–445 行
- 上游源码 src/backend/commands/copyfromparse.c 第 779 行
- 上游源码 src/backend/commands/copyfromparse.c 第 937 行
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
22P04 is bad_copy_file_format in SQLSTATE Class 22.
-
The fixed COPY parser uses 22P04 for binary signature/flags/extension length, text or CSV header field counts and names, extra or missing fields, and binary row/field framing.
-
Text and CSV line framing errors, end-of-copy marker placement, and unterminated quoted fields use 22P04; CSV quoting and text escape rules choose different primary/hint variants.
-
ON_ERROR IGNORE is applied around safe text/CSV type-input conversion failures and emits a skip notice; it does not turn COPY header, row framing, CSV quoting, or binary field structure errors into skippable rows.
-
The locked catalogue records 22P04 from 7.4; that boundary does not prove exact implementation introduction.
-
Frontend COPY-in errors follow the protocol that started COPY: extended-query COPY discards frontend messages until Sync and then sends ReadyForQuery, while simple Query COPY discards the rest of the query and sends ReadyForQuery without Sync. The frontend loop marks extended messages for skip-until-Sync recovery, and ReadyForQuery reports status rather than replacing transaction recovery.
同类 SQL 状态码
| 状态码 | 条件名 | 宏名称 | 严重等级 | 版本 |
|---|---|---|---|---|
| 22000 | data_exception | ERRCODE_DATA_EXCEPTION |
ERROR | 7.4 |
| 数据异常的通用类别,具体原因见成员码。 | 活跃 | |||
| 22001 | string_data_right_truncation | ERRCODE_STRING_DATA_RIGHT_TRUNCATION |
ERROR | 7.4 |
| 值超过目标类型长度而被右截断。 | 活跃 | |||
| 22002 | null_value_no_indicator_parameter | ERRCODE_NULL_VALUE_NO_INDICATOR_PARAMETER |
ERROR | 7.4 |
| 返回 NULL 但没有提供指示变量,见于 ECPG。 | 活跃 | |||
| 22003 | numeric_value_out_of_range | ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE |
ERROR | 7.4 |
| 数值超出目标类型范围,如整数溢出。 | 活跃 | |||
| 22004 | null_value_not_allowed | ERRCODE_NULL_VALUE_NOT_ALLOWED |
ERROR | 7.4 |
| 参数或值不允许为 NULL。 | 活跃 | |||
| 22005 | error_in_assignment | ERRCODE_ERROR_IN_ASSIGNMENT |
ERROR | 7.4 |
| 赋值违反目标变量契约,如变量声明为 CONSTANT。 | 活跃 | |||
| 22007 | invalid_datetime_format | ERRCODE_INVALID_DATETIME_FORMAT |
ERROR | 7.4 |
| 日期时间格式串无效,如 interval 格式错误。 | 活跃 | |||
| 22008 | datetime_field_overflow | ERRCODE_DATETIME_FIELD_OVERFLOW |
ERROR | 7.4 |
| 日期或时间字段取值越界。 | 活跃 | |||
| 22009 | invalid_time_zone_displacement_value | ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE |
ERROR | 7.4 |
| 时区偏移量超出支持范围。 | 活跃 | |||
| 2200B | escape_character_conflict | ERRCODE_ESCAPE_CHARACTER_CONFLICT |
ERROR | 7.4 |
| 转义字符与其他字符冲突。 | 活跃 | |||
| 2200C | invalid_use_of_escape_character | ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER |
ERROR | 7.4 |
| 转义字符用法无效,如 SIMILAR TO 模式错误。 | 活跃 | |||
| 2200D | invalid_escape_octet | ERRCODE_INVALID_ESCAPE_OCTET |
ERROR | 7.4 |
| 转义八位组无效的数据异常条件。 | 活跃 | |||
| 2200F | zero_length_character_string | ERRCODE_ZERO_LENGTH_CHARACTER_STRING |
ERROR | 7.4 |
| 操作拒绝了零长度字符串。 | 活跃 | |||
| 2200G | most_specific_type_mismatch | ERRCODE_MOST_SPECIFIC_TYPE_MISMATCH |
ERROR | 7.4 |
| 无法确定值的最具体类型,类型不匹配。 | 活跃 | |||
| 2200H | sequence_generator_limit_exceeded | ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED |
ERROR | 10 |
| nextval 触及序列的最大值或最小值。 | 活跃 | |||
| 2200L | not_an_xml_document | ERRCODE_NOT_AN_XML_DOCUMENT |
ERROR | 8.3 |
| 输入不满足 XML DOCUMENT 形式要求。 | 活跃 | |||
| 2200M | invalid_xml_document | ERRCODE_INVALID_XML_DOCUMENT |
ERROR | 8.3 |
| XML 文档解析失败,文档无效。 | 活跃 | |||
| 2200N | invalid_xml_content | ERRCODE_INVALID_XML_CONTENT |
ERROR | 8.3 |
| XML 内容无效,未通过 CONTENT 模式解析。 | 活跃 | |||
| 2200S | invalid_xml_comment | ERRCODE_INVALID_XML_COMMENT |
ERROR | 8.3 |
| XML 注释无效,含 -- 或以 - 结尾。 | 活跃 | |||
| 2200T | invalid_xml_processing_instruction | ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION |
ERROR | 8.3 |
| XML 处理指令无效,目标名或内容非法。 | 活跃 | |||
| 22010 | invalid_indicator_parameter_value | ERRCODE_INVALID_INDICATOR_PARAMETER_VALUE |
ERROR | 7.4 |
| 指示参数取值不符合规则。 | 活跃 | |||
| 22011 | substring_error | ERRCODE_SUBSTRING_ERROR |
ERROR | 7.4 |
| substring 长度参数为负等取子串错误。 | 活跃 | |||
| 22012 | division_by_zero | ERRCODE_DIVISION_BY_ZERO |
ERROR | 7.4 |
| 除法或取模运算遇到零除数。 | 活跃 | |||
| 22013 | invalid_preceding_or_following_size | ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE |
ERROR | 11 |
| 窗口帧的 PRECEDING/FOLLOWING 偏移为负。 | 活跃 | |||
| 22014 | invalid_argument_for_ntile_function | ERRCODE_INVALID_ARGUMENT_FOR_NTILE |
ERROR | 8.4 |
| ntile 的桶数量必须大于零。 | 活跃 | |||
| 22015 | interval_field_overflow | ERRCODE_INTERVAL_FIELD_OVERFLOW |
ERROR | 7.4 |
| interval 输入字段超出可接受范围。 | 活跃 | |||
| 22016 | invalid_argument_for_nth_value_function | ERRCODE_INVALID_ARGUMENT_FOR_NTH_VALUE |
ERROR | 8.4 |
| nth_value 的序号必须大于零。 | 活跃 | |||
| 22018 | invalid_character_value_for_cast | ERRCODE_INVALID_CHARACTER_VALUE_FOR_CAST |
ERROR | 7.4 |
| 字符值无法通过所要求的类型转换。 | 活跃 | |||
| 22019 | invalid_escape_character | ERRCODE_INVALID_ESCAPE_CHARACTER |
ERROR | 7.4 |
| 操作拒绝了给定的转义字符。 | 活跃 | |||
| 2201B | invalid_regular_expression | ERRCODE_INVALID_REGULAR_EXPRESSION |
ERROR | 7.4 |
| 正则表达式无效,编译或执行阶段被拒绝。 | 活跃 | |||
| 2201E | invalid_argument_for_logarithm | ERRCODE_INVALID_ARGUMENT_FOR_LOG |
ERROR | 8.0 |
| 对数函数参数为零或负数。 | 活跃 | |||
| 2201F | invalid_argument_for_power_function | ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION |
ERROR | 8.0 |
| 幂运算参数使实数结果无定义。 | 活跃 | |||
| 2201G | invalid_argument_for_width_bucket_function | ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION |
ERROR | 8.0 |
| width_bucket 参数无效,如桶数非正。 | 活跃 | |||
| 2201W | invalid_row_count_in_limit_clause | ERRCODE_INVALID_ROW_COUNT_IN_LIMIT_CLAUSE |
ERROR | 8.4 |
| LIMIT 行数无效,不能为负。 | 活跃 | |||
| 2201X | invalid_row_count_in_result_offset_clause | ERRCODE_INVALID_ROW_COUNT_IN_RESULT_OFFSET_CLAUSE |
ERROR | 8.4 |
| OFFSET 行数无效,不能为负。 | 活跃 | |||
| 22021 | character_not_in_repertoire | ERRCODE_CHARACTER_NOT_IN_REPERTOIRE |
ERROR | 7.4 |
| 字节序列对目标编码无效。 | 活跃 | |||
| 22022 | indicator_overflow | ERRCODE_INDICATOR_OVERFLOW |
ERROR | 7.4 |
| 指示变量容纳不下接口返回的结果。 | 活跃 | |||
| 22023 | invalid_parameter_value | ERRCODE_INVALID_PARAMETER_VALUE |
ERROR | 7.4 |
| 参数或选项取值超出允许范围。 | 活跃 | |||
| 22024 | unterminated_c_string | ERRCODE_UNTERMINATED_C_STRING |
ERROR | 7.4 |
| C 风格字符串缺少终止符。 | 活跃 | |||
| 22025 | invalid_escape_sequence | ERRCODE_INVALID_ESCAPE_SEQUENCE |
ERROR | 7.4 |
| 转义序列无效,如 ESCAPE 参数非单字符。 | 活跃 | |||
| 22026 | string_data_length_mismatch | ERRCODE_STRING_DATA_LENGTH_MISMATCH |
ERROR | 7.4 |
| 位串长度与声明的类型长度不一致。 | 活跃 | |||
| 22027 | trim_error | ERRCODE_TRIM_ERROR |
ERROR | 7.4 |
| trim 操作的参数或裁剪规格无效。 | 活跃 | |||
| 2202E | array_subscript_error | ERRCODE_ARRAY_SUBSCRIPT_ERROR |
ERROR | 7.4 |
| 数组下标或维度错误,如拼接维度不兼容。 | 活跃 | |||
| 2202G | invalid_tablesample_repeat | ERRCODE_INVALID_TABLESAMPLE_REPEAT |
ERROR | 9.5 |
| TABLESAMPLE 的 REPEATABLE 种子不能为空。 | 活跃 | |||
| 2202H | invalid_tablesample_argument | ERRCODE_INVALID_TABLESAMPLE_ARGUMENT |
ERROR | 9.5 |
| TABLESAMPLE 方法收到越界的采样参数。 | 活跃 | |||
| 22030 | duplicate_json_object_key_value | ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE |
ERROR | 12 |
| 构造 JSON 对象时出现重复键。 | 活跃 | |||
| 22031 | invalid_argument_for_sql_json_datetime_function | ERRCODE_INVALID_ARGUMENT_FOR_SQL_JSON_DATETIME_FUNCTION |
ERROR | 13 |
| SQL/JSON datetime 方法的参数或格式无效。 | 活跃 | |||
| 22032 | invalid_json_text | ERRCODE_INVALID_JSON_TEXT |
ERROR | 12 |
| JSON 文本无效,无法被消费方解析。 | 活跃 | |||
| 22033 | invalid_sql_json_subscript | ERRCODE_INVALID_SQL_JSON_SUBSCRIPT |
ERROR | 12 |
| SQL/JSON 数组下标无效,如越界或非数值。 | 活跃 | |||
| 22034 | more_than_one_sql_json_item | ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM |
ERROR | 12 |
| SQL/JSON 结果多于一项,需要单项。 | 活跃 | |||
| 22035 | no_sql_json_item | ERRCODE_NO_SQL_JSON_ITEM |
ERROR | 12 |
| 指定 SQL/JSON path 没有找到任何项。 | 活跃 | |||
| 22036 | non_numeric_sql_json_item | ERRCODE_NON_NUMERIC_SQL_JSON_ITEM |
ERROR | 12 |
| JSON 路径项方法收到非数值的项。 | 活跃 | |||
| 22037 | non_unique_keys_in_a_json_object | ERRCODE_NON_UNIQUE_KEYS_IN_A_JSON_OBJECT |
ERROR | 12 |
| JSON 对象中的键不唯一。 | 活跃 | |||
| 22038 | singleton_sql_json_item_required | ERRCODE_SINGLETON_SQL_JSON_ITEM_REQUIRED |
ERROR | 12 |
| SQL/JSON 路径要求单一指定类型的结果。 | 活跃 | |||
| 22039 | sql_json_array_not_found | ERRCODE_SQL_JSON_ARRAY_NOT_FOUND |
ERROR | 12 |
| JSON 路径访问器要求数组却收到非数组项。 | 活跃 | |||
| 2203A | sql_json_member_not_found | ERRCODE_SQL_JSON_MEMBER_NOT_FOUND |
ERROR | 12 |
| JSON 对象成员不存在或当前项不是对象。 | 活跃 | |||
| 2203B | sql_json_number_not_found | ERRCODE_SQL_JSON_NUMBER_NOT_FOUND |
ERROR | 12 |
| jsonpath 一元运算收到非数值的项。 | 活跃 | |||
| 2203C | sql_json_object_not_found | ERRCODE_SQL_JSON_OBJECT_NOT_FOUND |
ERROR | 12 |
| JSON 路径访问器要求对象却收到非对象项。 | 活跃 | |||
| 2203D | too_many_json_array_elements | ERRCODE_TOO_MANY_JSON_ARRAY_ELEMENTS |
ERROR | 12 |
| JSON 数组元素数量过多。 | 活跃 | |||
| 2203E | too_many_json_object_members | ERRCODE_TOO_MANY_JSON_OBJECT_MEMBERS |
ERROR | 12 |
| JSON 对象成员数量过多。 | 活跃 | |||
| 2203F | sql_json_scalar_required | ERRCODE_SQL_JSON_SCALAR_REQUIRED |
ERROR | 12 |
| JSON_VALUE 要求标量,收到的项不是标量。 | 活跃 | |||
| 2203G | sql_json_item_cannot_be_cast_to_target_type | ERRCODE_SQL_JSON_ITEM_CANNOT_BE_CAST_TO_TARGET_TYPE |
ERROR | 15 |
| SQL/JSON 项无法转换为目标类型。 | 活跃 | |||
| 22P01 | floating_point_exception | ERRCODE_FLOATING_POINT_EXCEPTION |
ERROR | 7.4 |
| 浮点异常,进程收到 SIGFPE 信号。 | 活跃 | |||
| 22P02 | invalid_text_representation | ERRCODE_INVALID_TEXT_REPRESENTATION |
ERROR | 7.4 |
| 文本无法解析为目标类型,如整数输入非法。 | 活跃 | |||
| 22P03 | invalid_binary_representation | ERRCODE_INVALID_BINARY_REPRESENTATION |
ERROR | 7.4 |
| 二进制数据格式无效,如 Bind 参数或二进制 COPY。 | 活跃 | |||
| 22P04 | bad_copy_file_format | ERRCODE_BAD_COPY_FILE_FORMAT |
ERROR | 7.4 |
| COPY 文件结构错误,如字段数或头部不符。 | 活跃 | |||
| 22P05 | untranslatable_character | ERRCODE_UNTRANSLATABLE_CHARACTER |
ERROR | 7.4 |
| 字符在目标编码中没有对应表示。 | 活跃 | |||
| 22P06 | nonstandard_use_of_escape_character | ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER |
ERROR | 8.1 |
| 字符串字面量中反斜杠转义用法非标准。 | 活跃 | |||