Fix DEFAULT marker handling in COPY FROM ... HEADER MATCH
Hello,
I noticed an issue when combining the HEADER MATCH option (added in v15)
with the DEFAULT option in COPY FROM (added in v16).
If any column name in the header line matches the configured DEFAULT marker,
two problematic behaviors occur depending on whether the column has a
default:
-When the column has a default value, the input value of the first data row
is discarded and replaced with the column's default expression. All
subsequent data rows are loaded normally:
CREATE TABLE sales (id int, "default" int DEFAULT 0, note text);
COPY sales FROM STDIN WITH (FORMAT csv, HEADER MATCH, DEFAULT 'default');
id,default,note
1,5,first
2,7,second
\.
SELECT * FROM sales ORDER BY id;
id | default | note
----+---------+--------
1 | 0 | first -- Expected 5 here ?
2 | 7 | second
(2 rows)
-When the column does not have a default value, COPY aborts during header
parsing, but not for the correct reason i guess:
CREATE TABLE t (d int, b text);
COPY t FROM STDIN WITH (FORMAT csv, HEADER MATCH, DEFAULT 'd');
d,b
5,x
\.
ERROR: unexpected default marker in COPY data
DETAIL: Column "d" has no default value.
CONTEXT: COPY t, line 1: "d,b"
This also happens with foreign tables via file_fdw when used with
OPTIONS (header 'match', default '...').
Because header parsing and the first row of data are parsed within the same
first call to NextCopyFrom(), and cstate->defaults is initialized to false
only at the top of NextCopyFrom(), the flag set while reading the header
line survived into the first data row. CopyFromTextLikeOneRow() then sees
cstate->defaults[m] set to true and evaluate the default expression
instead of
the supplied field.
The attached fix temporarily disables default marker recognition while
parsing
the header line when HEADER MATCH. I also added two tests for this.
This affects REL_16_STABLE through master.
Thoughts?
Regards,
Ayoub Kazar
On Nov 4, 2026, at 08:21, Ayoub Kazar <ayoub.kazar@data-bene.io> wrote:
Hello,
I noticed an issue when combining the HEADER MATCH option (added in v15) with the DEFAULT option in COPY FROM (added in v16).
If any column name in the header line matches the configured DEFAULT marker,
two problematic behaviors occur depending on whether the column has a default:-When the column has a default value, the input value of the first data row
is discarded and replaced with the column's default expression. All
subsequent data rows are loaded normally:CREATE TABLE sales (id int, "default" int DEFAULT 0, note text);
COPY sales FROM STDIN WITH (FORMAT csv, HEADER MATCH, DEFAULT 'default');
id,default,note
1,5,first
2,7,second
\.SELECT * FROM sales ORDER BY id;
id | default | note
----+---------+--------
1 | 0 | first -- Expected 5 here ?
2 | 7 | second
(2 rows)-When the column does not have a default value, COPY aborts during header
parsing, but not for the correct reason i guess:CREATE TABLE t (d int, b text);
COPY t FROM STDIN WITH (FORMAT csv, HEADER MATCH, DEFAULT 'd');
d,b
5,x
\.
ERROR: unexpected default marker in COPY data
DETAIL: Column "d" has no default value.
CONTEXT: COPY t, line 1: "d,b"This also happens with foreign tables via file_fdw when used with
OPTIONS (header 'match', default '...').Because header parsing and the first row of data are parsed within the same
first call to NextCopyFrom(), and cstate->defaults is initialized to false
only at the top of NextCopyFrom(), the flag set while reading the header
line survived into the first data row. CopyFromTextLikeOneRow() then sees
cstate->defaults[m] set to true and evaluate the default expression instead of
the supplied field.The attached fix temporarily disables default marker recognition while parsing
the header line when HEADER MATCH. I also added two tests for this.This affects REL_16_STABLE through master.
Thoughts?
Regards,
Ayoub Kazar<0001-Fix-DEFAULT-marker-handling-in-COPY-FROM-.-HEADER-MATCH.patch>
Nice catch. I reproduced the problem and confirmed that the v1 fix works.
The root cause is that CopyReadAttributesCSV() and CopyReadAttributesText() use the same logic to parse header lines and data lines, so a column name may be incorrectly interpreted as a DEFAULT marker. From this perspective, I think adding an is_header parameter to these two functions to distinguish header parsing from data parsing would be a more general solution.
With your finding, I realized that NULL markers have the same problem:
```
evantest=# create table t (id int, a int);
CREATE TABLE
evantest=# copy t from stdin with (format csv, header match, null 'a');
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
id,a
1,2
\.
ERROR: column name mismatch in header line field 2: got null value ("a"), expected "a"
CONTEXT: COPY t, line 1: "id,a"
```
Here, a is a valid column name in the header line and should not be rejected simply because it matches the NULL marker.
Adding an is_header parameter allows us to disable both NULL and DEFAULT marker recognition when parsing header lines, addressing both issues with the same approach. It also avoids temporarily modifying the COPY options.
So, I’m proposing v2 with this approach. I kept the regression tests you added in v1 and added tests for NULL markers and the text format. Would you please review v2?
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Hi,
Thanks for the review!
On 04/11/2026 07:54, Chao Li wrote:
ERROR: column name mismatch in header line field 2: got null value ("a"), expected "a"
CONTEXT: COPY t, line 1: "id,a"
```Here, a is a valid column name in the header line and should not be rejected simply because it matches the NULL marker.
Adding an is_header parameter allows us to disable both NULL and DEFAULT marker recognition when parsing header lines, addressing both issues with the same approach. It also avoids temporarily modifying the COPY options.
So, I’m proposing v2 with this approach. I kept the regression tests you added in v1 and added tests for NULL markers and the text format. Would you please review v2?
I have mixed feelings about this:
CREATE TABLE t (id int, "N" text);
If user inputs this in COPY:
id \N
1 \N
same is in diffed existing regression test for it:
|-ERROR: column name mismatch in header line field 3: got null value
("\N"), expected "c" +ERROR: column name mismatch in header line field
3: got "N", expected "c"|
IMO, i prefer having this error out for NULL marker reason in a column
name, rather than reading '\N' as 'N' in header, and this might be the
intention of who wrote it... maybe.
I agree this should be data rows only, but the documentation doesn't
specify that either, at least not as expected as the case of DEFAULT
marker. So i'm not sure...
CSV case:
In CSV, CopyReadAttributesCSV() already checks `!saw_quote`. If a user has a
column named "a" and sets `null 'a'`, quoting the column name in the
header (`id,"a"`) already prevents it from being treated as NULL.
I have an extra worry about performance regressions, previously from
[1]: /messages/by-id/CAOzEurSW8cNr6TPKsjrstnPfhf4QyQqB4tnPXGGe8N4e_v7Jig@mail.gmail.com
branching (without going into much details), therefore having 1-3%
performance regressions or improvements that appear just like that (this
applies everywhere in Postgres really...), CopyReadAttributesCSV() is
very sensitive, it runs millions of times very easily and its a high
registers-pressured code.
A not too serious benchmark on v2 already shows 5% regressions.
Regards,
Ayoub Kazar
[1]: /messages/by-id/CAOzEurSW8cNr6TPKsjrstnPfhf4QyQqB4tnPXGGe8N4e_v7Jig@mail.gmail.com
/messages/by-id/CAOzEurSW8cNr6TPKsjrstnPfhf4QyQqB4tnPXGGe8N4e_v7Jig@mail.gmail.com
On Nov 4, 2026, at 16:57, Ayoub Kazar <ayoub.kazar@data-bene.io> wrote:
Hi,
Thanks for the review!
On 04/11/2026 07:54, Chao Li wrote:ERROR: column name mismatch in header line field 2: got null value ("a"), expected "a"
CONTEXT: COPY t, line 1: "id,a"
```Here, a is a valid column name in the header line and should not be rejected simply because it matches the NULL marker.
Adding an is_header parameter allows us to disable both NULL and DEFAULT marker recognition when parsing header lines, addressing both issues with the same approach. It also avoids temporarily modifying the COPY options.
So, I’m proposing v2 with this approach. I kept the regression tests you added in v1 and added tests for NULL markers and the text format. Would you please review v2?
I have mixed feelings about this:
CREATE TABLE t (id int, "N" text);
If user inputs this in COPY:
id \N
1 \N
Good point. I hadn’t considered this case. This would incorrectly allow COPY to succeed, even though \N in the header line should cause it to fail.
I believe this can be resolved if we pursue v2’s approach, but some refactoring will likely be needed.
same is in diffed existing regression test for it: -ERROR: column name mismatch in header line field 3: got null value ("\N"), expected "c" +ERROR: column name mismatch in header line field 3: got "N", expected "c" IMO, i prefer having this error out for NULL marker reason in a column name, rather than reading '\N' as 'N' in header, and this might be the intention of who wrote it... maybe. I agree this should be data rows only, but the documentation doesn't specify that either, at least not as expected as the case of DEFAULT marker. So i'm not sure...CSV case:
In CSV, CopyReadAttributesCSV() already checks `!saw_quote`. If a user has a
column named "a" and sets `null 'a'`, quoting the column name in the header (`id,"a"`) already prevents it from being treated as NULL.
That’s true, but I think v2 would remove this restriction, so users would no longer have to quote column names in the header line just because they match the NULL marker. I consider that an improvement.
I have an extra worry about performance regressions, previously from [1], i saw that very minimal changes effect the binary layout and branching (without going into much details), therefore having 1-3% performance regressions or improvements that appear just like that (this applies everywhere in Postgres really...), CopyReadAttributesCSV() is very sensitive, it runs millions of times very easily and its a high registers-pressured code.
A not too serious benchmark on v2 already shows 5% regressions.
Could you share how you ran the benchmark and measured the 5% regression?
I ran a brief benchmark with assertions disabled and -O2. The test copies 1 million rows into a table with 32 text columns. The results are:
```
| Version | Format | Runs | Average time | Change vs master |
|---|---|---:|---:|---:|
| Master (`590788ad0bf`) | CSV | 6 | 625.382 ms | — |
| Master | Text | 5 | 627.275 ms | — |
| Master | Binary | 5 | 795.974 ms | — |
| v2 | CSV | 5 | 641.186 ms | +2.53% |
| v2 | Text | 5 | 607.293 ms | −3.19% |
| v2 | Binary | 6 | 785.810 ms | −1.28% |
```
These results don’t show a clear performance regression to me, though this was only a brief benchmark.
Overall, I agree that point 1 has to be addressed, and doing so would require more work beyond the scope of this patch. Let’s drop v2 for now; I’ll propose it in a separate thread.
The attached v3 restores the v1 fix. I only adjusted the commit message formatting and added author/reviewer annotations.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
On 05/11/2026 06:27, Chao Li wrote:
I have an extra worry about performance regressions, previously from [1], i saw that very minimal changes effect the binary layout and branching (without going into much details), therefore having 1-3% performance regressions or improvements that appear just like that (this applies everywhere in Postgres really...), CopyReadAttributesCSV() is very sensitive, it runs millions of times very easily and its a high registers-pressured code.
A not too serious benchmark on v2 already shows 5% regressions.
Could you share how you ran the benchmark and measured the 5% regression?
I ran a brief benchmark with assertions disabled and -O2. The test copies 1 million rows into a table with 32 text columns. The results are:
```
| Version | Format | Runs | Average time | Change vs master |
|---|---|---:|---:|---:|
| Master (`590788ad0bf`) | CSV | 6 | 625.382 ms | — |
| Master | Text | 5 | 627.275 ms | — |
| Master | Binary | 5 | 795.974 ms | — |
| v2 | CSV | 5 | 641.186 ms | +2.53% |
| v2 | Text | 5 | 607.293 ms | −3.19% |
| v2 | Binary | 6 | 785.810 ms | −1.28% |
```
Although not needed anymore, attached is the simple benchmark COPY FROM
on 4 millions rows for info:
I run these too: (On my laptop with an Intel I7-1255U CPU)
sudo cpupower frequency-set --governor=performance
sudo cpupower idle-set -D 0
echo "1" | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo
pinned backend to a performance core
non-assert, -O2 build
Latest time i ran this i got 2.5%-3% regression.
The attached v3 restores the v1 fix. I only adjusted the commit message formatting and added author/reviewer annotations.
Looks good, thanks.
Regards,
Ayoub Kazar
Attachments:
Hi Ayoub,
Thanks for the report and the patch, and thanks to Chao for
the review round that shaped v3. I reproduced the bug, read
the fix, and tested v3 (the version Chao posted on Nov 5);
this review is of v3 as posted.
I agree with the mechanism. The header line and the first
data row are read inside the same NextCopyFrom() call, and
cstate->defaults is only cleared at the top of that
function, so a header field that matches the DEFAULT
marker sets the flag during header parsing and it leaks
into row 1. The fix sits at the right layer: it disables
marker recognition only while the header line is parsed
under HEADER MATCH, by clearing opts.default_print around
the CopyReadAttributesCSV()/Text() call and restoring it
before any data row is read.
One scope note, following the thread: I reviewed the
DEFAULT marker fix only. The NULL marker question from
Chao's v2 is parked for a separate thread, as agreed, and
I checked only that v3 does not change the existing
NULL/header behaviour (see below).
What I verified, on master c07545dfe1a, with v3
cherry-picked on top (it applies cleanly; the patch was
written against fc1900915b1):
Reproduced on unpatched master, in both CSV and text
formats:
With a default on the column: header "id,default,note"
with DEFAULT 'default' loads row 1 with the column
default (0) instead of the supplied value (5). Silent
data corruption; row 2 loads normally.
Without a default: COPY aborts with ERROR "unexpected
default marker in COPY data", "Column "d" has no
default value", pointing at line 1, the header.
The same failure through file_fdw (a foreign table with
header 'match' and a default marker): the scan errors
on line 1 the same way.
With v3 applied:
Both cases load correctly in CSV and in text: row 1
keeps its supplied value, and the no-default case loads
cleanly. file_fdw loads correctly too.
A genuine DEFAULT marker in the data rows still works,
including in the first data row for the very column
whose name matched in the header, so the fix exempts
only the header line and does not weaken marker
handling in data.
A header field of \N errors exactly as on master
("column name mismatch ... got "\N""), so the existing
NULL/header behaviour is untouched, as is HEADER TRUE
with a data marker.
Scope check: I grepped the tree for default_print. It is
consulted for marker recognition only in
CopyReadAttributesText() and CopyReadAttributesCSV(),
which are exactly the two readers the patched call site
wraps; the remaining uses are option parsing/validation
and building default expressions. HEADER TRUE never
parses header fields and binary format has no header
parsing, so the HEADER MATCH call site is the complete
affected surface.
Tests: the two new copy2 cases pin both failure modes,
and the second one also proves a data-row marker still
fires after a matching header. As a discrimination check
I ran v3's test files against unpatched master: copy2
fails, and it is the only failure in the core suite
(1 of 239). With the patch, the full core regression
suite passes (239/239). installcheck-world against a
running server also passes: 54 suites, 658 tests, all
passed. I then reconfigured the same tree with TAP
tests enabled and ran check-world, which also passes
end to end, TAP suites included: 320 test files,
26,839 subtests, no failures. 32 TAP files skip for
test-configuration reasons (suites gated behind
PG_TEST_EXTRA, Windows-only tests, and one test that
needs readline, which this build omits), and the
kerberos and ldap suites are not part of this build
configuration.
One small suggestion, not a blocker: the new regression
tests cover CSV only. Both readers are reached from the
one patched call site, and I verified the text format by
hand in both failure modes, but a text-format case in
copy2 would round out the coverage.
For the committer: the bug is present from v16 (which
added the DEFAULT option) together with HEADER MATCH
(v15), so REL_16_STABLE through master are affected;
how far to backpatch I will leave to you. The commit
message already carries the Author, Reported-by,
Reviewed-by and Discussion lines.
the builds,the probe battery, and the test-suite runs
were done with AI assistance, and I reviewed the results
and the patch itself as described above.
Verdict: I am marking this Ready for Committer.
Regards,
Prashant Chinnam