22P04
Read PG 18 manual ↗bad_copy_file_format
- SQLSTATE
- 22P04
- Condition name
- bad_copy_file_format
- Class
- Data Exception
- Source macro
- ERRCODE_BAD_COPY_FILE_FORMAT
- Evidence
- Source path confirmed
English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗
At a glance
22P04 is the COPY file-format boundary. The fixed parser uses it for binary signatures and headers, text or CSV framing, header/row field counts, and malformed binary field lengths. It is a structural COPY error, not a generic synonym for a value that failed type conversion.
Meaning
Binary input validates the PGCOPY signature, flags, extension length, row field count, field length, and end marker. Representative primaries include 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, and unexpected EOF in COPY data.
Text and CSV input has separate framing guards. Header matching can report wrong number of fields in header line: got %d, expected %d or a column-name mismatch; ordinary rows can report extra data after last expected column or missing data for column "%s". CSV quoting and line endings can report unterminated CSV quoted field, unquoted carriage return found in data, or unquoted newline found in data, while text mode uses the corresponding literal ... found in data messages and hints. A valid frame whose value cannot be converted belongs to the type-input boundary, commonly 22P02; a binary type receiver that leaves bytes unconsumed can be 22P03.
Messages
Representative fixed ERROR primaries include:
- Binary headers/fields:
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. - Header and rows:
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 and line framing:
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. The carriage-return/newline variants carry the source hints to use\r,\n, or a quoted CSV field.
Diagnosis
Determine whether the source is text, CSV, binary COPY, or frontend COPY-in. Preserve the exact primary because it identifies the parser stage. Check binary signature/flags/length, header and target-column order, row field count, CSV quote/escape and newline rules, and whether the text COPY end-of-data marker is alone on its line. Only after framing is valid should you investigate the target type's input conversion.
ON_ERROR IGNORE is narrower than a general malformed-row switch. The fixed text/CSV path wraps safe type-input conversion and can emit a notice while skipping a data-type-incompatible row; header, field-count, line-framing, CSV-quote, and binary-structure errors are raised as ERROR and are not all skippable through that option.
Response
Regenerate the stream in the declared text/CSV/binary format with the exact target column order and correct headers, lengths, quoting, and row framing. In frontend COPY-in, finish the COPY sub-protocol at its current boundary, using CopyFail when appropriate. If the COPY was issued through the extended protocol and the backend sends ErrorResponse, send Sync and wait for ReadyForQuery; if it was issued in a simple Query, the remaining query message is discarded and ReadyForQuery follows; no client Sync is required, so consume that ReadyForQuery before sending the next query. Do not send ordinary SQL in the middle of COPY-in. An ERROR inside an explicit transaction requires ROLLBACK or ROLLBACK TO SAVEPOINT after the protocol boundary; ReadyForQuery reports status but does not replace that transaction recovery. ON_ERROR IGNORE may be suitable for the documented safe type-input failures, but it does not repair a bad header or malformed CSV/binary framing. A normal COPY ERROR does not by itself require resetting the connection.
Versions
The locked catalogue records this condition from PostgreSQL 7.4. The cited parser and its ON_ERROR boundary are from PostgreSQL 18.6 REL_18_6; no natural COPY-file runtime was executed.
Sources
src/backend/commands/copyfromparse.c#L190-L228
src/backend/commands/copyfromparse.c#L779-L827
src/backend/commands/copyfromparse.c#L937-L977
src/backend/commands/copyfromparse.c#L1026-L1073
src/backend/commands/copyfromparse.c#L1084-L1130
src/backend/commands/copyfromparse.c#L1388-L1432
src/backend/commands/copyfromparse.c#L1818-L1922
src/backend/tcop/postgres.c#L416-L445
doc/src/sgml/protocol.sgml#L1287-L1318
The structured evidence record records representative exact primaries/hints and the structural ON_ERROR limit.
Source evidence
Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.
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.
Method: Read the complete header, row, and field guards.
src.path.binary-header src.path.text-header src.path.text-row src.path.binary-row src.path.binary-field
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.
Method: Read the complete line and CSV parsers.
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.
Method: Read InputFunctionCallSafe handling separately from structural parser errors.
Limits: This is source-backed; no ON_ERROR runtime was executed.
The locked catalogue records 22P04 from 7.4; that boundary does not prove exact implementation introduction.
Method: Use catalogue boundary.
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.
Method: Read the frontend message flags and the fixed COPY-in protocol documentation.
- src/backend/utils/errcodes.txt · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/commands/copyfromparse.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/tcop/postgres.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - doc/src/sgml/protocol.sgml · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7
Message templates
ERROR · message.signature
Primary
COPY file signature not recognized
ERROR · message.header-flags
Primary
invalid COPY file header (missing flags)
ERROR · message.header-critical
Primary
unrecognized critical flags in COPY file header
ERROR · message.header-length
Primary
invalid COPY file header (missing length)
ERROR · message.header-wrong-length
Primary
invalid COPY file header (wrong length)
ERROR · message.header-fields
Primary
wrong number of fields in header line: got %d, expected %d
ERROR · message.header-name
Primary
column name mismatch in header line field %d: got "%s", expected "%s"
ERROR · message.extra
Primary
extra data after last expected column
ERROR · message.missing
Primary
missing data for column "%s"
ERROR · message.row-count
Primary
row field count is %d, expected %d
ERROR · message.field-size
Primary
invalid field size
ERROR · message.eof
Primary
unexpected EOF in COPY data
ERROR · message.csv-unterminated
Primary
unterminated CSV quoted field
ERROR · message.literal-cr
Primary
literal carriage return found in data
Hint
Use "\r" to represent carriage return.
ERROR · message.csv-cr
Primary
unquoted carriage return found in data
Hint
Use quoted CSV field to represent carriage return.
ERROR · message.literal-newline
Primary
literal newline found in data
Hint
Use "\n" to represent newline.
ERROR · message.csv-newline
Primary
unquoted newline found in data
Hint
Use quoted CSV field to represent newline.
ERROR · message.marker-alone
Primary
end-of-copy marker is not alone on its line
Reproduction & repair cases
No reproduction case is attached to this condition.
Recorded runtime evidence
No runtime observation is attached to this entry.
Definition snapshot: english-manuals:7989838019adec092428ac90329… · English manual source