Re: BUG #9578: Undocumented behaviour for temp tables created inside query language (SQL) functions

From: Haribabu Kommi <kommi(dot)haribabu(at)gmail(dot)com>
To: jack(at)douglastechnology(dot)co(dot)uk
Cc: "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #9578: Undocumented behaviour for temp tables created inside query language (SQL) functions
Date: 2014-03-17 02:27:13
Message-ID: CAJrrPGcgfBPvJwGfvChVKYJKvChMxs62eb_bnMPcyzCR8TAmCg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

On Sun, Mar 16, 2014 at 3:50 AM, <jack(at)douglastechnology(dot)co(dot)uk> wrote:
> A temp table created inside an SQL function does not override existing
> permanent tables with the same name as the documentation here indicates it
> should:
>
> http://www.postgresql.org/docs/9.3/static/sql-createtable.html#AEN72676
>
> I've reproduced this on the major versions back to 8.4.
>
> More details, test case and investigation here:
>
> http://dba.stackexchange.com/q/60997/1396

I checked the test case which you given in the above link.
As you are seeing the difference in behavior of accessing a temp table
inside an SQL function
and PLPGSQL function.

Table:

create table foo(id) as values (1);
select * from foo;

SQL-function:

create or replace function f() returns setof integer language sql as $$
create temporary table foo(id) as values (2);
select id from foo;
$$;
select * from f();

PLPGSQL function:

create or replace function f() returns setof integer language plpgsql as $$
begin
create temporary table foo(id) as values (2);
return query select id from foo;
end;
$$;
select * from f();

This is because while executing the SQL function the entire function
body is parsed and executed.
But with the PLPGSQL function statement by statement is parsed and
executed. Because of this
reason the SQL function not able to see the temp table which is
created during the function execution.
That is the reason the result is different.

I don't think it is a bug.

Regards,
Hari Babu
Fujitsu Australia

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Alvaro Herrera 2014-03-17 02:30:25 Re: [BUGS] BUG #9223: plperlu result memory leak
Previous Message Michael Paquier 2014-03-16 01:40:28 Re: BUG #9118: WAL Sender does not disconnect replication clients during shutdown

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2014-03-17 02:30:25 Re: [BUGS] BUG #9223: plperlu result memory leak
Previous Message Peter Eisentraut 2014-03-17 02:25:37 Re: Minimum supported version of Python?