| From: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | Fix stale column context after COPY FROM conversion errors |
| Date: | 2026-11-05 06:43:54 |
| Message-ID: | A603A132-9AF1-43B1-9194-318B6FA9C0E4@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
While working on patch [1], I noticed that COPY FROM emits confusing error messages, for example:
```
evantest=# CREATE TABLE t (a int);
CREATE TABLE
evantest=#
evantest=# COPY t FROM STDIN WITH (FORMAT csv, ON_ERROR ignore);
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.
>> bad
>> 1,2
>> \.
ERROR: extra data after last expected column
CONTEXT: COPY t, line 2, column a: "1"
```
The context line shows column a: "1", which does not identify the actual problem where an extra field (2) was supplied.
The problem is that CopyFromTextLikeOneRow() only clears the current column information at the end of each loop iteration, but bypasses this cleanup when input conversion fails:
```
if (cstate->opts.on_error == COPY_ON_ERROR_IGNORE)
return true;
else if (cstate->opts.on_error == COPY_ON_ERROR_SET_NULL)
continue;
}
cstate->cur_attname = NULL;
cstate->cur_attval = NULL;
}
```
So the fix is simple: we can add the cleanup in the COPY_ON_ERROR_IGNORE branch and remove the COPY_ON_ERROR_SET_NULL branch, letting it fall through so that the cleanup is performed.
With the fix, the same test shows the correct context:
```
evantest=# COPY t FROM STDIN WITH (FORMAT csv, ON_ERROR ignore);
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.
>> bad
>> 1,2
>> \.
ERROR: extra data after last expected column
CONTEXT: COPY t, line 2: “1,2"
```
[1] https://postgr.es/m/2da457c8-a9a1-49b1-a89f-719190dff7ae@data-bene.io
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Fix-stale-column-context-after-COPY-FROM-conversi.patch | application/octet-stream | 4.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Peter Smith | 2026-11-05 06:41:41 | Re: [PATCH v1] Fix initial sync of zero-column published tables |