COPY TO: provide hint when WHERE clause is used

torikoshia <torikoshia@oss.nttdata.com>

From: torikoshia <torikoshia@oss.nttdata.com>
To: pgsql-hackers@lists.postgresql.org
Date: 2025-08-19T01:35:28Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Add HINT for COPY TO when WHERE clause is used.

Attachments

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.