Re: Add partial :-variable expansion to psql \copy

Fabien Coelho <fabien.coelho@minesparis.psl.eu>

From: Fabien Coelho <fabien.coelho@minesparis.psl.eu>
To: Corey Huinker <corey.huinker@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2025-04-01T09:30:55Z
Lists: pgsql-hackers
Hello Corey,

> If we could do this:
>
>     COPY :"myschema".:"mytable" FROM STDIN \g < :"myfilename"
>
> that would fit our patterns most cleanly, but we would probably create 
> a parsing hassle for ourselves if we ever wanted to mix pipe-to with 
> pipe-from. It would also require checking on every command, when 
> uploaded \copy commands make up a very small percentage of commands 
> issued. So I don't think there's a good way around the asymmetry of 
> COPY TO being a regular \g-able command, whereas COPY FROM will always 
> require some other send-command.
>
> Perhaps we create a new command \copyfrom:
>
>     COPY :"myschema".:"mytable" :options FROM STDIN \copyfrom 
> :"myfilename"
>
>     COPY :"myschema".:"mytable" :options FROM STDIN \copyfrom 
> :"my_complex_command" |
>
> If we had something like that we might be able to replace all existing 
> uses of \copy.
>
Indeed, I like the idea of extending psql handling of COPY rather than 
trying to salvage \copy. I do not like that it is probably more work for 
significantly larger patch.

There are 4 cases to address: input/output cross join file/program, and 
as you pointed out the output ones are already handled.

I'm hesitating about the right syntax, though, for an input backslash 
command which in effect would really only apply to COPY? ISTM that \g* 
is used for "go", i.e. a semi-colon replacement which executes the SQL, 
and we should want the same thing, which suggests:

COPY "foo" FROM STDIN \gi filename

COPY "foo" FROM STDIN \gi command...|

Another drawback is that it creates an error path:

COPY "foo" FROM 'server-side-file' \gi 'client-side-file'

-- 

Fabien.

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix psql's "\g target" meta-command to work with COPY TO STDOUT.