Re: COPY value TO STDOUT

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Denisa Cirstescu <denisa(dot)cirstescu(at)asentinel(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: COPY value TO STDOUT
Date: 2017-01-14 17:13:29
Message-ID: 7846.1484414009@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Denisa Cirstescu <denisa(dot)cirstescu(at)asentinel(dot)com> writes:
> I want to COPY a value to STDOUT from PL/pgSQL language.

You can't. Maybe RAISE NOTICE would serve the purpose?

> I saw that the STDOUT is not accessible from PL/pgSQL, but it is from SQL.
> This is why I am trying to create an auxiliary function declared as language SQL and call that function from my PL/pgSQL code.

Oh, that's an oversight --- this case can't work either, but the SQL
function code fails to prevent it. The COPY runs, but it completely
breaks the wire protocol, leading to weird errors on the client side,
eg

regression=# create or replace function printToStdout(abc text) returns void as $$
copy (SELECT 42) to stdout;
$$ language sql;
CREATE FUNCTION
regression=# select printToStdout('z');
42
server sent data ("D" message) without prior row description ("T" message)
regression=#

What this should produce is an error similar to the one you get in
plpgsql. COPY to stdout/from stdin can't be executed from inside an
already-running query, because the wire protocol can't support
nesting those operations.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2017-01-14 17:15:36 Re: raise notice question
Previous Message Pavel Stehule 2017-01-14 17:07:17 Re: raise notice question