Missed in PostgreSQL 19 : Add COPY (on_error set_null) option

On 3rd of March 2026, Peter Eisentraut committed patch:

Add COPY (on_error set_null) option
 
If ON_ERROR SET_NULL is specified during COPY FROM, any data type
conversion errors will result in the affected column being set to a
null value.  A column's not-null constraints are still enforced, and
attempting to set a null value in such columns will raise a constraint
violation error.  This applies to a column whose data type is a domain
with a NOT NULL constraint.
 
Author: Jian He <jian.universality@gmail.com>
Author: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@oss.nttdata.com>
Reviewed-by: Jim Jones <jim.jones@uni-muenster.de>
Reviewed-by: "David G. Johnston" <david.g.johnston@gmail.com>
Reviewed-by: Yugo NAGATA <nagata@sraoss.co.jp>
Reviewed-by: torikoshia <torikoshia@oss.nttdata.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://www.postgresql.org/message-id/flat/CAKFQuwawy1e6YR4S%3Dj%2By7pXqg_Dw1WBVrgvf%3DBP3d1_aSfe_%2BQ%40mail.gmail.com

In PostgreSQL 17 we got ON_ERROR option for COPY, with two possible values: stop (default) and ignore. Now there is a third one.

Let's see how it works. Simple table for some sensor data:

=$ create table measurements (
    id int4 not null,
    taken_at timestamptz,
    temperature numeric,
    humidity int4
);
CREATE TABLE

And let's load some, not really perfect, data:

=$ copy measurements from stdin with (format csv);
1,2026-11-20 10:00:00,21.5,40
2,2026-11-20 11:00:00,N/A,41
3,yesterday-ish,22.1,abc
4,2026-11-20 13:00:00,22.8,39
\.
ERROR:  invalid input syntax for type numeric: "N/A"
CONTEXT:  COPY measurements, line 2, column temperature: "N/A"

This is the default on_error stop. With on_error ignore, which we have since Pg 17, we get:

=$ copy measurements from stdin with (format csv, on_error ignore, log_verbosity verbose);
1,2026-11-20 10:00:00,21.5,40
2,2026-11-20 11:00:00,N/A,41
3,yesterday-ish,22.1,abc
4,2026-11-20 13:00:00,22.8,39
\.
NOTICE:  skipping row due to data type incompatibility at line 2 for column "temperature": "N/A"
NOTICE:  skipping row due to data type incompatibility at line 3 for column "taken_at": "yesterday-ish"
NOTICE:  2 rows were skipped due to data type incompatibility
COPY 2
 
=$ select * from measurements;
 id │        taken_at        │ temperature │ humidity
────┼────────────────────────┼─────────────┼──────────
  1 │ 2026-11-20 10:00:00+01 │        21.5 │       40
  4 │ 2026-11-20 13:00:00+01 │        22.8 │       39
(2 rows)

Whole rows 2 and 3 were skipped. But in row 2, only temperature was bad. Timestamp and humidity were perfectly fine. It would be nice to keep them. And this is what set_null does:

=$ truncate measurements;
TRUNCATE TABLE
 
=$ copy measurements from stdin with (format csv, on_error set_null, log_verbosity verbose);
1,2026-11-20 10:00:00,21.5,40
2,2026-11-20 11:00:00,N/A,41
3,yesterday-ish,22.1,abc
4,2026-11-20 13:00:00,22.8,39
\.
NOTICE:  setting to null due to data type incompatibility at line 2 for column "temperature": "N/A"
NOTICE:  setting to null due to data type incompatibility at line 3 for column "taken_at": "yesterday-ish"
NOTICE:  setting to null due to data type incompatibility at line 3 for column "humidity": "abc"
NOTICE:  in 2 rows, columns were set to null due to data type incompatibility
COPY 4
 
=$ select * from measurements;
 id │        taken_at        │ temperature │ humidity
────┼────────────────────────┼─────────────┼──────────
  1 │ 2026-11-20 10:00:00+01 │        21.5 │       40
  2 │ 2026-11-20 11:00:00+01 │      [null] │       41
  3 │ [null]                 │        22.1 │   [null]
  4 │ 2026-11-20 13:00:00+01 │        22.8 │       39
(4 rows)

All four rows got loaded, and only the bad values were replaced with nulls. Without log_verbosity verbose you just get the summary:

=$ truncate measurements;
TRUNCATE TABLE
 
=$ copy measurements from stdin with (format csv, on_error set_null);
1,2026-11-20 10:00:00,21.5,40
2,2026-11-20 11:00:00,N/A,41
3,yesterday-ish,22.1,abc
4,2026-11-20 13:00:00,22.8,39
\.
NOTICE:  in 2 rows, columns were set to null due to data type incompatibility
COPY 4

What if the bad value is in column that can't be null? Then the not-null constraint wins, and whole copy fails:

=$ copy measurements from stdin with (format csv, on_error set_null);
five,2026-11-20 14:00:00,23.0,38
\.
ERROR:  null value in column "id" of relation "measurements" violates not-null constraint
DETAIL:  Failing row contains (null, 2026-11-20 14:00:00+01, 23.0, 38).
CONTEXT:  COPY measurements, line 1: "five,2026-11-20 14:00:00,23.0,38"

Interesting case are domains. Since domain check failure happens while converting the input value, it is treated as data type incompatibility too. Let's see:

=$ create domain percent as int4 check (value between 0 and 100);
CREATE DOMAIN
 
=$ create domain strict_percent as int4 not null check (value between 0 and 100);
CREATE DOMAIN
 
=$ create table readings (id int4, humidity percent, humidity_strict strict_percent);
CREATE TABLE
 
=$ copy readings from stdin with (format csv, on_error set_null, log_verbosity verbose);
1,50,50
2,150,50
\.
NOTICE:  setting to null due to data type incompatibility at line 2 for column "humidity": "150"
NOTICE:  in 1 row, columns were set to null due to data type incompatibility
COPY 2

150 is a perfectly valid int4, but it fails the domain check, so it got changed to null. But if the domain itself doesn't allow nulls:

=$ copy readings from stdin with (format csv, on_error set_null, log_verbosity verbose);
3,50,abc
\.
ERROR:  domain strict_percent does not allow null values
DETAIL:  ON_ERROR SET_NULL cannot be applied because column "humidity_strict" (domain strict_percent) does not accept null values.
CONTEXT:  COPY readings, line 1, column humidity_strict: "abc"
 
=$ select * from readings;
 id │ humidity │ humidity_strict
────┼──────────┼─────────────────
  1 │       50 │              50
  2 │   [null] │              50
(2 rows)

Nice, clear error message.

Just like ignore, it doesn't work for binary format:

=$ copy measurements from stdin with (format binary, on_error set_null);
ERROR:  only ON_ERROR STOP is allowed in BINARY mode

And it's only about data type conversion errors. If the line has wrong number of columns, or there is unique violation, or check constraint on the table fails, you will still get an error.

This is really helpful when loading data from sources that you don't control, and where losing whole row is worse than losing single value. Thanks to everyone involved.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.