Re: Using Temporary Tables in postgres functions

From: Mario Splivalo <mario(dot)splivalo(at)mobart(dot)hr>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Using Temporary Tables in postgres functions
Date: 2007-01-26 10:46:37
Message-ID: 1169808397.3045.13.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 2007-01-25 at 08:03 -0800, Stephan Szabo wrote:
> On Thu, 25 Jan 2007, Mario Splivalo wrote:
>
> > When I try to use TEMPORARY TABLE within postgres functions (using 'sql'
> > as a function language), I can't because postgres can't find that
> > temporary table. Consider this example:
> >
> > CREATE FUNCTION func1() RETURNS SETOF v_messages_full AS $BODY$
> > CREATE TEMPORARY TABLE tmpTbl
> > AS
> > SELECT
> > message_id
> > FROM
> > cached_messages
> > WHERE
> > billing_status = 2;
> >
> >
> > UPDATE cached_messages SET billing_status = 1 WHERE message_id IN
> > (SELECT message_id FROM tmpTbl);
> >
> > SELECT
> > *
> > FROM
> > v_messages_full
> > WHERE
> > message_id IN (SELECT message_id FROM tmpTbl);
> > $BODY$ LANGUAGE 'sql' VOLATILE SECURITY DEFINER;
>
> It seems like the sql function checker is unhappy with the above. Does it
> actually work if you turn off the check_function_bodies configuration
> variable, create the function and then call it?

Yes, when I do 'set check_function_bodies to false;' then I can create
the function but I'm unable to execute it:

testdb1=# select * from func1();
ERROR: relation "tmptbl" does not exist
CONTEXT: SQL function "func1" during startup

Bummer. In the end I wrote the function using plpgsql. Now, is there any
performance impact on using plpgsql instead of sql in simple scenarios
as in func1() example? I guess there should be some, as minimas as it
can be, but have no way of actualy knowing that.

Mike

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mario Splivalo 2007-01-26 10:47:05 Re: Using Temporary Tables in postgres functions
Previous Message Achilleas Mantzios 2007-01-26 10:02:24 Re: Intersection of two date interval