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/[email protected]
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
v1-0001-Fix-stale-column-context-after-COPY-FROM-conversi.patch
Description: Binary data
