pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
To: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: COPY TO (query) allows no function arguments
Date: Wed, 10 Mar 2021 10:17:07 -0500
Message-ID: <eda708fe-a55c-0df5-8c64-cae6c9eeb54e@joeconway.com> (raw)
In-Reply-To: <609326d4d6854085aeb909ae564d4ccb@stmuk.bayern.de>
References: <609326d4d6854085aeb909ae564d4ccb@stmuk.bayern.de>

On 3/10/21 9:48 AM, Färber, Franz-Josef (StMUK) wrote:
> it is as simple as the subject says:
> 
> The COPY TO (query) statement does not allow the passing of function arguments –
> is this by intent or a bug?

This is not a bug -- next time if you are uncertain please try the general list
first.

In any case, you cannot use the passed in argument text as part of a simple SQL
command -- you have to build the command from text and then execute it. For example:

8<------------------------
CREATE TABLE b(id int);
INSERT INTO b VALUES(1),(2),(42);

CREATE OR REPLACE FUNCTION a(myparam text)
RETURNS void LANGUAGE plpgsql AS $func$
BEGIN
  EXECUTE $$COPY (SELECT * FROM $$ || myparam || $$) TO '/tmp/a.txt'$$;
END;
$func$;

SELECT a('b');
SELECT pg_read_file('/tmp/a.txt');
 pg_read_file
--------------
 1           +
 2           +
 42          +

(1 row)
8<------------------------

HTH,

Joe

-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development





view thread (4+ messages)  latest in thread

Message-ID: <eda708fe-a55c-0df5-8c64-cae6c9eeb54e@joeconway.com>
Permalink:  ../eda708fe-a55c-0df5-8c64-cae6c9eeb54e@joeconway.com/
Also on:    postgresql.org/message-id/eda708fe-a55c-0df5-8c64-cae6c9eeb54e@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: mail@joeconway.com, Franz-Josef.Faerber@stmuk.bayern.de, pgsql-bugs@lists.postgresql.org
  Subject: Re: COPY TO (query) allows no function arguments
  In-Reply-To: <eda708fe-a55c-0df5-8c64-cae6c9eeb54e@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox