Re: calling function

From: James Robinson <jlrobins(at)socialserve(dot)com>
To: Bhushan Bhangale <bbhangale(at)Lastminute(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: calling function
Date: 2004-03-03 16:32:35
Message-ID: 60BB31AC-6D30-11D8-971F-000A9566A412@socialserve.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Here's a before and after version of a simplified version of what
you're trying to do in your script. The 'after' version uses 'execute'
and 'for row in exectue ... loop' to not cache plans. Also notice the
use of a temporary table, visible only to this backend, in case more
than one backend calls the function simultaneously.

Any other discussion should be done off of this list, as this is not
JDBC related at all.

James

-- simplified version of inital plpgsql function. Gets bitten by cached
query plan.
create or replace function f() returns setof record as '
DECLARE
row RECORD;

BEGIN
create table foo
(
i int
);

insert into foo values(1);
insert into foo values(2);

FOR row in select * from foo LOOP
RETURN NEXT row;
END LOOP;

drop table foo;
return;
END;
' LANGUAGE 'plpgsql';

-- works
select * from f() as f_results(id int);

-- fails on any subsequent call in this session
select * from f() as f_results(id int);

-- now a version that will work more than once per session
-- note the use of execute and for ... in execute
create or replace function f() returns setof record as '
DECLARE
row RECORD;

BEGIN
create temporary table foo
(
i int
);

execute ''insert into foo values(1)'';
execute ''insert into foo values(2)'';

FOR row in execute ''select * from foo'' LOOP
RETURN NEXT row;
END LOOP;

drop table foo;
return;
END;
' LANGUAGE 'plpgsql';

-- now can call many times per session
select * from f() as f_results(id int);
select * from f() as f_results(id int);
select * from f() as f_results(id int);
select * from f() as f_results(id int);

----
James Robinson
Socialserve.com

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message kenr 2004-03-04 00:53:15 Problems with JDBC client and LATIN1 chars
Previous Message Dave Cramer 2004-03-03 16:10:21 Re: Same old story :( "Cannot load JDBC driver class 'null'"