百科 / 错误代码 / 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.
同类错误代码
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_representation22P03invalid_binary_representation22P05untranslatable_character22P06nonstandard_use_of_escape_character