Interesting COPY edge case...

Lists: pgsql-hackers
From: Chris Browne <cbbrowne(at)acm(dot)org>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Interesting COPY edge case...
Date: 2005-07-26 21:56:42
Message-ID: 60ll3t1bad.fsf@dba2.int.libertyrms.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Just ran into a fascinating edge case. One of our folks was building
a stored function, and ran into an odd error when trying to COPY to
stdout.

Here's a characteristic example:

create or replace function build_table (integer) returns integer as '
begin
execute ''copy foo to stdout;'';
return 1;
end' language plpgsql;

The result:

/* cbbrowne(at)[local]/dba2 in=*/ select try_copy(1);
ERROR: unexpected error -2 in EXECUTE of query "copy dups to stdout;"
CONTEXT: PL/pgSQL function "try_copy" line 2 at execute statement

If I instead create

create or replace function build_table (integer) returns integer as '
copy foo to stdout;
return 1;
' language sql;

The latter works fine.

There is evidently Something Strange about the state of stdout when it
is referenced inside a stored procedure.

We can work around this reasonably, but the "unexpected error -2"
elicits some curiosity I'd like to satisfy. (I bounced it off one of
the others that aren't off on vacation, and his reaction was exactly
the same as mine, namely "Hmmm... I'm just not sure what to expect
from that...")

Is there a good reason/excuse to give as to why the pl/pgsql version
of the COPY *shouldn't* work?
--
(format nil "~S(at)~S" "cbbrowne" "acm.org")
http://www.ntlug.org/~cbbrowne/sap.html
Rules of the Evil Overlord #78. "I will not tell my Legions of Terror
"And he must be taken alive!" The command will be: ``And try to take
him alive if it is reasonably practical.''"
<http://www.eviloverlord.com/>


From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Chris Browne <cbbrowne(at)acm(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Interesting COPY edge case...
Date: 2005-07-27 15:26:44
Message-ID: 20050727152644.GA12993@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Jul 26, 2005 at 17:56:42 -0400,
Chris Browne <cbbrowne(at)acm(dot)org> wrote:
>
> There is evidently Something Strange about the state of stdout when it
> is referenced inside a stored procedure.

I suspect this is related to trusted PLs not being able to write files.
It does seem like a problem that COPY has access to stdout in one case
and not the other.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chris Browne <cbbrowne(at)acm(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Interesting COPY edge case...
Date: 2005-07-28 20:41:27
Message-ID: 28266.1122583287@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Chris Browne <cbbrowne(at)acm(dot)org> writes:
> create or replace function build_table (integer) returns integer as '
> begin
> execute ''copy foo to stdout;'';
> return 1;
> end' language plpgsql;

> The result:

> /* cbbrowne(at)[local]/dba2 in=*/ select try_copy(1);
> ERROR: unexpected error -2 in EXECUTE of query "copy dups to stdout;"
> CONTEXT: PL/pgSQL function "try_copy" line 2 at execute statement

In CVS tip I get

ERROR: cannot COPY to/from client in PL/pgSQL
CONTEXT: PL/pgSQL function "build_table" line 2 at execute statement

There are some protocol-level reasons for not supporting this, IIRC.
You're already inside a SELECT and the client library would probably
get terribly confused.

regards, tom lane