Reject unsupported COPY FROM targets before analyzing WHERE clause

Started by Chao Li5 months ago8 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

While testing “[21c69dc73] Disallow system columns in COPY FROM WHERE conditions”, I noticed an issue. This is not a correctness bug, but I think the user experience is not good.

See this repro:
```
evantest=# create sequence s;
CREATE SEQUENCE
evantest=# copy s from stdin where tableoid is null;
ERROR: system columns are not supported in COPY FROM WHERE conditions
DETAIL: Column "tableoid" is a system column.
```

Here, it reports an error about the WHERE clause. Okay, then I remove the WHERE clause:
```
evantest=# copy s from stdin;
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.

ddd
^CERROR: cannot copy to sequence "s"

```

It only fails with the root problem, “cannot copy to sequence”, after I type in the content I want to copy. I think that is too late. A better user experience would be to raise the root error before checking the WHERE clause and before waiting for any input.

Similar masking can also happen with other checks, such as generated columns in COPY FROM WHERE conditions (v19 new) and COPY FROM on tables with row-level security.

I tried a solution that splits the target relation pre-checks out of CopyFrom() and calls the pre-check earlier. With this patch, if a user copies to an unsupported target, such as a sequence, the command fails immediately without analyzing the WHERE clause or waiting for input.

See the attached patch for details.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0001-Reject-unsupported-COPY-FROM-targets-before-WHERE.patchapplication/octet-stream; name=v1-0001-Reject-unsupported-COPY-FROM-targets-before-WHERE.patch; x-unix-mode=0644Download+67-40
#2Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Chao Li (#1)
Re: Reject unsupported COPY FROM targets before analyzing WHERE clause

Hello.

At Wed, 24 Jun 2026 15:34:07 +0800, Chao Li <li.evan.chao@gmail.com> wrote in

It only fails with the root problem, “cannot copy to sequence”, after I type in the content I want to copy. I think that is too late. A better user experience would be to raise the root error before checking the WHERE clause and before waiting for any input.

I agree that it would be better to fail before waiting for input.
However, I am not sure that the unsupported-target error needs to take
precedence over errors in the WHERE clause.

For example, if we move the check in question from CopyFrom() to
BeginCopyFrom(), the error would still be masked by WHERE-clause
errors, but at least COPY FROM would no longer start reading input
before reporting this error. That would also make the placement of
this check more consistent with COPY TO. As far as I can tell, this
check has been performed at different stages in COPY TO and COPY FROM
for a long time, though I am not aware of any particular reason for
the difference.

Similar masking can also happen with other checks, such as generated columns in COPY FROM WHERE conditions (v19 new) and COPY FROM on tables with row-level security.

I tried a solution that splits the target relation pre-checks out of CopyFrom() and calls the pre-check earlier. With this patch, if a user copies to an unsupported target, such as a sequence, the command fails immediately without analyzing the WHERE clause or waiting for input.

So I think the important part here is to avoid waiting for input when
the command is doomed to fail. I am less convinced that we need to
reorder the checks so that unsupported-target errors are always
reported before WHERE-clause errors.

Regards,

--
Kyotaro Horiguchi
NTT Open Source Software Center

#3Chao Li
li.evan.chao@gmail.com
In reply to: Kyotaro Horiguchi (#2)
Re: Reject unsupported COPY FROM targets before analyzing WHERE clause

On Jun 24, 2026, at 16:41, Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote:

Hello.

At Wed, 24 Jun 2026 15:34:07 +0800, Chao Li <li.evan.chao@gmail.com> wrote in

It only fails with the root problem, “cannot copy to sequence”, after I type in the content I want to copy. I think that is too late. A better user experience would be to raise the root error before checking the WHERE clause and before waiting for any input.

I agree that it would be better to fail before waiting for input.
However, I am not sure that the unsupported-target error needs to take
precedence over errors in the WHERE clause.

For example, if we move the check in question from CopyFrom() to
BeginCopyFrom(), the error would still be masked by WHERE-clause
errors, but at least COPY FROM would no longer start reading input
before reporting this error. That would also make the placement of
this check more consistent with COPY TO. As far as I can tell, this
check has been performed at different stages in COPY TO and COPY FROM
for a long time, though I am not aware of any particular reason for
the difference.

Similar masking can also happen with other checks, such as generated columns in COPY FROM WHERE conditions (v19 new) and COPY FROM on tables with row-level security.

I tried a solution that splits the target relation pre-checks out of CopyFrom() and calls the pre-check earlier. With this patch, if a user copies to an unsupported target, such as a sequence, the command fails immediately without analyzing the WHERE clause or waiting for input.

So I think the important part here is to avoid waiting for input when
the command is doomed to fail. I am less convinced that we need to
reorder the checks so that unsupported-target errors are always
reported before WHERE-clause errors.

Regards,

--
Kyotaro Horiguchi
NTT Open Source Software Center

Hi Kyotaro-san,

Thank you very much for the comments.

I still think the unsupported-target check should happen first, because that is the main problem. If a user spends time fixing a WHERE clause error, then the command to fails again with “cannot copy to xxx”, the user may feel frustrated.

But I am convinced by your point about making this consistent with COPY TO. So let’s use this patch to fix the “not waiting for input” problem first. I will try to work on the unsupported-target-check ordering for v20, considering COPY TO and COPY FROM together.

PFA v2. The patch is much simpler now. It only moves the unsupported-target check to BeginCopyFrom(), so the check happens before waiting for input.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t248622_3
v2-0001-Report-unsupported-COPY-FROM-targets-before-readi.patchapplication/octet-stream; name=v2-0001-Report-unsupported-COPY-FROM-targets-before-readi.patch; x-unix-mode=0644Download+34-37
#4ZizhuanLiu X-MAN
44973863@qq.com
In reply to: Chao Li (#3)
Re: Reject unsupported COPY FROM targets before analyzing WHERE clause

Hi Kyotaro-san,

Thank you very much for the comments.

I still think the unsupported-target check should happen first, because that is the main problem. If a user spends time fixing a WHERE clause error, then the command to fails again with “cannot copy to xxx”, the user may feel frustrated.

But I am convinced by your point about making this consistent with COPY TO. So let’s use this patch to fix the “not waiting for input” problem first. I will try to work on the unsupported-target-check ordering for v20, considering COPY TO and COPY FROM together.

PFA v2. The patch is much simpler now. It only moves the unsupported-target check to BeginCopyFrom(), so the check happens before waiting for input.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Hi, everyone

Thanks a lot for the patch and detailed comments.

In most scenarios, after constructing the `CopyFromState` within `BeginCopyFrom()`,
we immediately pass it into `CopyFrom()` and invoke the function. By moving the relevant
validation checks from `CopyFrom()` into `BeginCopyFrom()`, we can deliver error feedback much earlier.

This way, validation will be performed in advance before waiting for input sources
({ 'filename' | PROGRAM 'command' | STDIN }) to be ready, so that errors can be
reported to the client side in a timelier manner.

Besides, `file_fdw.c` also stores the return value of `BeginCopyFrom()` inside its callback parameters,
so it will equally benefit from this earlier validation.

This patch looks good to me.

regards,
--
ZizhuanLiu (X-MAN) 
44973863@qq.com

#5solai v
solai.cdac@gmail.com
In reply to: ZizhuanLiu X-MAN (#4)
Re: Reject unsupported COPY FROM targets before analyzing WHERE clause

Hi Chao,

I tested the v2 patch on PostgreSQL 20devel.
Before applying the patch, I reproduced the reported behavior with:

DROP SEQUENCE IF EXISTS s;
CREATE SEQUENCE s;

COPY s FROM stdin;

PostgreSQL waited for input with:

Enter data to be copied...

After providing input, it reported:

ERROR: cannot copy to sequence "s"

After applying the v2 patch and rebuilding PostgreSQL, I ran the same
test again:

COPY s FROM stdin;

This time PostgreSQL immediately reported:

ERROR: cannot copy to sequence "s"

and did not wait for input.

I also tested:

COPY s FROM stdin WHERE tableoid IS NULL;

and it continued to report:

ERROR: system columns are not supported in COPY FROM WHERE conditions
DETAIL: Column "tableoid" is a system column.

So the v2 patch successfully fixes the delayed error reporting by
rejecting the unsupported target before waiting for input. The
existing WHERE-clause validation behavior remains unchanged.

Regards,
Solai

#6surya poondla
suryapoondla4@gmail.com
In reply to: Chao Li (#1)
Re: Reject unsupported COPY FROM targets before analyzing WHERE clause

Hi Chao,

I tested v2 on current master (51ae21aa313). The code change looks fine to
me.

One more argument for it, on master the file is opened, or the program is
run, before the relkind check fires. For example:

create sequence s;
copy s from program 'touch /tmp/marker';
ERROR: cannot copy to sequence "s"

On master /tmp/marker gets created; with v2 it doesn't. And copy s from
'/nonexistent/file' fails with "could not open file" on master, but "cannot
copy to sequence" with v2. Might be worth a line in the commit message.

The copy2.sql change needs to be dropped, though. Since Tom's 3045a25ba81,
which went in after v2, psql skips the in-line data when COPY FROM STDIN
fails early. With test1 and . removed, psql now eats everything up to the
next ., i.e. the CREATE FUNCTION, CREATE TRIGGER and the next COPY, and
copy2 fails. If you keep those two lines, the expected output doesn't
change.

Apart from that I checked sequences, views and matviews (error before
reading any input), plain/partitioned tables the WHERE clause error (still
reported first).

+1 with the test being removed.

Regards,
Surya Poondla

#7Chao Li
li.evan.chao@gmail.com
In reply to: surya poondla (#6)
Re: Reject unsupported COPY FROM targets before analyzing WHERE clause

On Nov 3, 2026, at 08:33, surya poondla <suryapoondla4@gmail.com> wrote:

Hi Chao,

I tested v2 on current master (51ae21aa313). The code change looks fine to me.

Thank you very much for your review.

One more argument for it, on master the file is opened, or the program is run, before the relkind check fires. For example:

create sequence s;
copy s from program 'touch /tmp/marker';
ERROR: cannot copy to sequence "s"

On master /tmp/marker gets created; with v2 it doesn't. And copy s from '/nonexistent/file' fails with "could not open file" on master, but "cannot copy to sequence" with v2. Might be worth a line in the commit message.

Updated the comment message in v3.

The copy2.sql change needs to be dropped, though. Since Tom's 3045a25ba81, which went in after v2, psql skips the in-line data when COPY FROM STDIN fails early. With test1 and . removed, psql now eats everything up to the next ., i.e. the CREATE FUNCTION, CREATE TRIGGER and the next COPY, and copy2 fails. If you keep those two lines, the expected output doesn't change.

Reverted the change in copy2.sql.

PFA v3.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t248622_7
v3-0001-Report-unsupported-COPY-FROM-targets-before-readi.patchapplication/octet-stream; name=v3-0001-Report-unsupported-COPY-FROM-targets-before-readi.patch; x-unix-mode=0644Download+34-35
#8surya poondla
suryapoondla4@gmail.com
In reply to: Chao Li (#7)
Re: Reject unsupported COPY FROM targets before analyzing WHERE clause

Hi Chao,

Thanks for v3. I applied it on current master (eaa5961efc6). The code is
the same as v2, and with copy2.sql left alone copy2's output matches
master. file_fdw passes too.

One thing I noticed, since the check now runs before ProcessCopyOptions(),
option errors are reported after the relkind error:

copy s from stdin (format bogus);
-- master: ERROR: COPY format "bogus" not recognized
-- v3: ERROR: cannot copy to sequence "s"

That's the same order COPY TO already uses, so I think it's fine, but maybe
worth a word in the commit message. Column-list errors still come first,
since DoCopy resolves the column list for the permission check.

Also, I don't think any existing test covers the early failure itself.
Something like COPY seq FROM '/nonexistent/file' would, since master
reports "could not open file" there. Not a must, just an idea.

Either way v3 looks good to me.

Regards,
Surya Poondla