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

From: Fabien Coelho <coelho(at)cri(dot)ensmp(dot)fr>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Add partial :-variable expansion to psql \copy
Date: 2025-04-01 08:58:17
Message-ID: 8abff45e-237c-43f9-9470-3fc6fafee2ce@cri.ensmp.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 31/03/2025 17:09, Tom Lane wrote:

> Fabien COELHO<coelho(at)cri(dot)ensmp(dot)fr> writes:
>> [...] The attached patch allows \copy to use variable's values in place of table and file names:
> Hm ... I'm on board with the general idea of the feature, but I find
> this implementation quite horrid.

Indeed, I just added the stuff in the already quite peculiar manual
lexer/parser for \copy. I did not think of addressing the why it is like
that issue and try to fix it :-)

> I would rather see us adjust the
> lexing rules in psqlscanslash.l so that variable expansion happens
> there when collecting \copy arguments. This would eliminate at
> least part of the distinction between OT_WHOLE_LINE and OT_NORMAL
> modes, and we'd have to have some discussion about how far to go
> there. Or maybe just change exec_command_copy to use OT_NORMAL
> not OT_WHOLE_LINE? If we modify the behavior of OT_WHOLE_LINE
> then the ability to expand variables would start to apply in the
> other commands using that, notably \!. I think that's at least
> potentially good, but perhaps the blast radius of such a change
> is too large.

I'm not sure that such \copy salvage to using lex is easy because:

(1) it seems that is the only command which is really full SQL hidden in
a backslash command

(2) on one line without requiring a final ';',

(3) the client needs to actually parse it and modify it to some degree
before sending it to the server.

so the implication for trying to maintain compatibility without adding
weirdness seem slim.

> Anyway, my feeling about it is that \copy parsing is a huge hack
> right now,
Yes.
> and I'd rather see it become less of a hack, that is
> more like other psql commands, instead of getting even hackier.

I think that I'll have a try with Corey suggestion to extend COPY rather
than change \copy.

--

Fabien.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message vignesh C 2025-04-01 09:16:21 Re: [PATCH] Fix build on MINGW on ARM64
Previous Message Bertrand Drouvot 2025-04-01 08:32:35 Re: Fix 035_standby_logical_decoding.pl race conditions