Thread: 'missing parameter $1' for sql or pl/pgsql COPY

'missing parameter $1' for sql or pl/pgsql COPY

From
Merlin Moncure
Date:
The following functions fail:

create or replace function docopy(int[]) returns int as
$$
  copy (select unnest($1)) to stdout;
  select 0;
$$ language sql;
ERROR:  there is no parameter $1

create or replace function docopy(int[]) returns int as
$$
begin
      copy (select unnest($1)) to stdout;
end;
$$ language plpgsql;
ERROR:  SPI_execute_plan failed executing query "copy (select unnest(
$1 )) to stdout": SPI_ERROR_COPY

this error is expected i guess, plpgsql doesn't support copy to stdout.

create or replace function docopy(_stuff int[]) returns int as
$$
begin
      copy (select unnest(_stuff)) to '/tmp/scratch';
end;
$$ language plpgsql;
ERROR:  there is no parameter $1

Also, copy does not allow variable substitution for the destination
file.  Dynamic sql is one work around, but EXECUTE USING does not
work:

create or replace function docopy(_stuff int[]) returns int as
$$
begin
      execute 'copy (select unnest($1)) to ''/tmp/scratch''' using _stuff;
end;
$$ language plpgsql;
ERROR:  there is no parameter $1

merlin