Sub topic 10
When Rows Fail: Atomic Loads and `ON_ERROR`
Assume you know basic SQL, PostgreSQL data types, CSV files, and the psql client. This chapter focuses on what happens when a row cannot be loaded and how to choose between an all-or-nothing import and a load that skips bad rows.
The default: stop on the first error
By default, COPY FROM uses ON_ERROR stop. PostgreSQL reads rows, converts each field to the target column type, and inserts valid rows. If one row contains invalid input, the COPY command fails.
For example, this table requires an integer identifier and a numeric price:
CREATE TABLE products (
id integer PRIMARY KEY,
name text NOT NULL,
price numeric(10,2),
released date,
notes text
);Suppose a CSV file contains this row:
12,Keyboard,not-a-number,2026-01-10,The value not-a-number cannot be converted to numeric(10,2). PostgreSQL stops the command and reports an error similar to this:
ERROR: invalid input syntax for type numeric: "not-a-number"
CONTEXT: COPY products, line 4, column price: "not-a-number"The COPY command is one statement. PostgreSQL does not leave the rows successfully processed earlier in that command as committed table data after the error. This all-or-nothing behavior protects the table from a partially loaded file.
Transactions and savepoints
A COPY command participates in the current transaction. If you run it inside an explicit transaction and it fails, the transaction enters an aborted state. You must roll back the transaction or roll back to a savepoint before running another command.
BEGIN;
SAVEPOINT before_import;
COPY products FROM '/var/lib/postgresql/import/products.csv'
WITH (FORMAT csv, HEADER true);
RELEASE SAVEPOINT before_import;
COMMIT;If COPY fails, use this pattern instead of trying to continue immediately:
ROLLBACK TO SAVEPOINT before_import;
-- Inspect the problem or run another statement here.
COMMIT;A savepoint is a named position inside a transaction. Rolling back to it removes changes made after that position while preserving earlier transaction work.
This is useful when an import is one step in a larger transaction. It also lets an application catch a load error without discarding unrelated changes.
Skipping conversion errors with `ON_ERROR ignore`
PostgreSQL 17 and later support ON_ERROR ignore for COPY FROM. This option skips rows that fail while PostgreSQL converts their fields to the target column types.
COPY products FROM '/var/lib/postgresql/import/products.csv'
WITH (
FORMAT csv,
HEADER true,
ON_ERROR ignore
);With this setting, the row containing not-a-number is skipped, while convertible rows continue loading. The command can complete successfully even though the file contains invalid records.
ON_ERROR ignore is appropriate when:
- The source contains occasional bad records.
- You can process rejected records separately.
- Loading valid data is more useful than stopping the entire batch.
- You have a way to measure and investigate skipped rows.
It is risky when every source row is required. A successful command does not prove that every record was imported. Count the input, loaded rows, and rejected rows when completeness matters.
ON_ERROR ignore does not replace validation. It mainly handles errors found while converting input to the destination column types. Business rules, such as “price must be positive,” still need constraints or a validation query.
Separate loading from validation
For stricter control, load the file into a staging table. A staging table is a temporary or separate table used before data reaches the production table.
CREATE TEMP TABLE products_stage (LIKE products);
COPY products_stage FROM '/var/lib/postgresql/import/products.csv'
WITH (FORMAT csv, HEADER true);With the default error behavior, this first step still stops on a bad typed value. You can instead make staging columns broad, often using text, and validate each field with queries before inserting into products.
After validation, insert only acceptable rows:
INSERT INTO products (id, name, price, released, notes)
SELECT id, name, price, released, notes
FROM products_stage
WHERE price >= 0;Staging gives you a place to record rejected rows, transform values, detect duplicate identifiers, and compare counts before changing the real table.
Choosing an error strategy
| Situation | Suitable approach |
|---|---|
| Every row must be valid | Default `ON_ERROR stop` |
| A few malformed rows may be skipped | `ON_ERROR ignore` on PostgreSQL 17+ |
| Data needs inspection or transformation | Staging table |
| Import is part of larger work | Transaction with a savepoint |
Exercise
What happens to rows already read by a default COPY FROM when a later row contains an invalid numeric value?
Show answer
The COPY command fails, and its inserted rows are not committed as a partial successful import. The surrounding transaction must be rolled back or returned to a savepoint before continuing.