Re: temporary views

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tomasz Myrta <jasiek(at)lamer(dot)pl>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: temporary views
Date: 2001-10-05 20:42:58
Message-ID: 200110052042.f95KgwZ22912@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> Bruce Momjian wrote:
> >
> > > Hi
> > > I have simple question: How to create view on a temporary table?
> > > I need this, because pl/pgsql function returns data via temporary table.
> > >
> > > View created on a temporary table is useful only to the end of session.
> > > Next time i create the same temp table i get
> > > "Table xxx with oid xxx doesn't exist"
> >
> > Just name your temporary table the same name in every session. Why
> > bother with a view.
>
> Creating a view makes my life easier. My temporary table has fields
> like id1,id2,id3,id4 and view translates it using inner joins to
> name1,name2,name3,name4. This temp table has always the same
> name and I don't want to do the translation inside pl/pgsql function.

OK, basically there is no way to create views reliably on temp tables:

creattest=> create temp table x(y int);
CREATE
test=> create view z on x as select * from x;
ERROR: parser: parse error at or near "on"
test=> create view z as select * from x;
CREATE
test=> select * from z;
y
---
(0 rows)

Of course this works, but exiting the session and restarting it gets
you:

test=> create temp table x(y int);
CREATE
test=> select * from z;
ERROR: Relation "x" with OID 16562 no longer exists

Internally, the problem is that the temp table is referenced by oid, not
table name. If you create a temp in a later session, it doesn't have
the same oid as the one in the session where you created the view.

What actually should happen is that the view should go away at the end
of the session. However, unlike indexes, we can have several tables
involved in a view so it is hard to know exactly how to handle this.

Seems like a TODO item, at least. What we could do is to create views
as TEMP if they use temp tables and drop the view as soon as the session
ends . You of course would have to recreate the view each time but
because it is a _temp_ view, it could be done reliably by multiple
backends at the same time.

Added to TODO:

* Allow views on temporary tables to behave as temporary views

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Alex Pilosov 2001-10-05 21:01:33 Re: temporary views
Previous Message Josh Berkus 2001-10-05 20:31:25 Re: SQL CONSTRAINTS - Constraining time values from two attributes on a table.