Thread: COPY statement cannot take binding parameters

COPY statement cannot take binding parameters

From
"Stephen R. van den Berg"
Date:
Trying to parse and bind the following:
  COPY (SELECT $1::INT) TO STDOUT

gives a correct parsing-done, but then in the parameterdescription tells
me that there are no parameters.

Is this intended?
Is this a limitation of the COPY statement that will not change?
In that case, might I suggest the server generates an appropriate error
message when someone attempts to do this?
-- 
Sincerely,          Stephen R. van den Berg.
"Having a non-smoking section in a restaurant is likehaving a non-peeing section in a pool."


Re: COPY statement cannot take binding parameters

From
Tom Lane
Date:
"Stephen R. van den Berg" <srb@cuci.nl> writes:
> Trying to parse and bind the following:
>    COPY (SELECT $1::INT) TO STDOUT
> gives a correct parsing-done, but then in the parameterdescription tells
> me that there are no parameters.

> Is this intended?

It's an artifact of the klugy way that SELECT was shoehorned into COPY :-(.
The SELECT isn't actually parsed/planned until execution time, so there
is no opportunity even to notice that it contains parameter symbols,
much less identify their types.  If you experiment you'll soon see that
any but the most basic syntactical errors in the SELECT aren't reported
at Parse-message time.

It might be possible to fix that by inverting the structure of the
statement internally, turning it into something approximating
SELECT ... INTO <copy parameters here> FROM ...

where IntoClause or some equivalent is made to carry everything from the
outer COPY syntax.  Don't hold your breath though.  (I think the parser
changes would actually be pretty trivial, but it might take some major
refactoring of the COPY code to let it be driven entirely as a tuple
receiver.)
        regards, tom lane