BUG #5274: [PL/PgSQL] EXECUTE ... USING variable expansion - Mailing list pgsql-bugs

From Vincenzo Romano
Subject BUG #5274: [PL/PgSQL] EXECUTE ... USING variable expansion
Date
Msg-id 201001131910.o0DJArAo086208@wwwmaster.postgresql.org
Whole thread Raw
Responses Re: BUG #5274: [PL/PgSQL] EXECUTE ... USING variable expansion  (Pavel Stehule <pavel.stehule@gmail.com>)
List pgsql-bugs
The following bug has been logged online:

Bug reference:      5274
Logged by:          Vincenzo Romano
Email address:      vincenzo.romano@notorand.it
PostgreSQL version: 8.4.2
Operating system:   Linux
Description:        [PL/PgSQL] EXECUTE ... USING variable expansion
Details:

My system says:
~ lsb_release -a
LSB Version:
:core-3.1-amd64:core-3.1-noarch:core-3.2-amd64:core-3.2-noarch:desktop-3.1-a
md64:desktop-3.1-noarch:desktop-3.2-amd64:desktop-3.2-noarch
Distributor ID: Fedora
Description:    Fedora release 12 (Constantine)
Release:        12
Codename:       Constantine

If you try the following:

CREATE TABLE test ( i INT );

CREATE OR REPLACE FUNCTION func()
 RETURNS void
 LANGUAGE plpgsql
AS $function$
DECLARE
  e TEXT;
  t TEXT;
  i INT;
BEGIN
  i := 42;
  t := 'answer';
  EXECUTE 'SELECT $1' INTO e USING t;
  RAISE INFO  '%',e;
  EXECUTE 'ALTER TABLE test ALTER COLUMN i SET DEFAULT $1' USING i;
END;
$function$;

SELECT func();

The first EXECUTE...USING replaces the variable $1 with the value of the
variable "t". The first output line reads:

INFO:  answer

The second EXECUTE...USING doesn't do the replacement and triggers an
error:

ERROR:  there is no parameter $1
CONTEXT:  SQL statement "ALTER TABLE test ALTER COLUMN i SET DEFAULT $1"
PL/pgSQL function "func" line 10 at EXECUTE statement

pgsql-bugs by date:

Previous
From: "David E. Wheeler"
Date:
Subject: Termination When Switching between PL/Perl and PL/PerlU
Next
From: "James Bellinger"
Date:
Subject: BUG #5275: validate_exec in port/exec.c only reads u/g/o, not ACLs