Re: Problems with PL/PGSQL - functions

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Unnikrishnan Menon" <unnikrishnan(dot)menon(at)chennai(dot)transys(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Problems with PL/PGSQL - functions
Date: 2002-01-13 16:59:33
Message-ID: 26109.1010941173@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Unnikrishnan Menon" <unnikrishnan(dot)menon(at)chennai(dot)transys(dot)net> writes:
> b. query1 :=3D '' Select xyz as variable from S_''|| nextval(sequence)|=
> |'' where abc =3D (some counter value) '' ;
> Execute query1;

You can't use EXECUTE for a SELECT (no way to retrieve the result) nor
for a SELECT INTO (annoying implementation restriction that should be
removed someday). The only way to get a result from a
dynamically-constructed select statement is to use FOR ... EXECUTE.

You do need to use EXECUTEs with any query involving a dynamically
generated table name, so you'll just have to grin and bear it.

But --- does the table actually have to be handled that way? If you
need exactly one such temp table per session, you could use "CREATE TEMP
TABLE" and give the table a fixed name. Then you could skip all the BS
with EXECUTE. Note however that it has to be per session; dropping and
recreating the table mid-session won't work since already-cached query
plans in your plpgsql functions will still point to the old temp table.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Al Dev 2002-01-13 18:08:57 Commercial: New Book!! PostgreSQL book is released into the market
Previous Message Ross J. Reedstrom 2002-01-13 16:19:31 Re: Problems with PL/PGSQL - functions