COPY TO: provide hint when WHERE clause is used

From: torikoshia <torikoshia(at)oss(dot)nttdata(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: COPY TO: provide hint when WHERE clause is used
Date: 2025-08-19 01:35:28
Message-ID: 3520c224c5ffac0113aef84a9179f37e@oss.nttdata.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I saw a user wanted to restrict the rows copied by COPY TO using a WHERE
clause, but ran into an error.

As you know, COPY FROM supports WHERE, but COPY TO does not:

=# copy t1 to stdout where i > 5;
ERROR: WHERE clause not allowed with COPY TO
LINE 1: copy t1 to stdout where i > 5;
^

In such cases, we can do with specifying a query as the target of COPY:

=# copy (select i from t1 where i > 5) to stdout;

However, as shown in the first example, no hint is provided in the error
message.

For views or others, COPY TO already provides a helpful hint message:

=# copy v1 to stdout;
ERROR: cannot copy from view "v1"
HINT: Try the COPY (SELECT ...) TO variant.

Considering what the user was trying to do, it might be helpful to
provide a similar hint in this case as well.
I’ve attached a patch that adds such a hint.

What do you think?

--
Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA Japan Corporation to SRA OSS K.K.

Attachment Content-Type Size
v1-0001-provide_hint_when_WHERE_is_used.patch text/x-diff 945 bytes

Browse pgsql-hackers by date

  From Date Subject
Next Message Man Zeng 2025-08-19 01:44:03 Re: When deleting the plpgsql function, release the CachedPlan of the function
Previous Message Naga Appani 2025-08-19 01:32:39 Re: [Proposal] Expose internal MultiXact member count function for efficient monitoring