Re: relpersistence and temp table

Lists: pgsql-hackers
From: Amit Khandekar <amit(dot)khandekar(at)enterprisedb(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: relpersistence and temp table
Date: 2011-07-01 12:06:20
Message-ID: BANLkTimOZRwJdPV538GgePAGp=+cD9Y60g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

In 9.1, if a table is created using an explicit pg_temp qualification,
the pg_class.relpersistence is marked 'p', not 't'.

postgres=# CREATE TABLE pg_temp.temptable (i int4);
CREATE TABLE

postgres=# select relpersistence from pg_class where relname = 'temptable';
relpersistence
----------------
p
(1 row)

BUt the table does go away if I exit the session:

postgres=# \q
edb-pg ~/git-pg1 $ psql
psql (9.0.1, server 9.2devel)
WARNING: psql version 9.0, server version 9.2.
Some psql features might not work.
Type "help" for help.

postgres=# select relpersistence from pg_class where relname = 'temptable';
relpersistence
----------------
(0 rows)

So in that sense, it does work as a temp table, but the relpersistence
is not 't'. So, is the "temp"ness no longer identified by
pg_class.relpersistence now?

In RelationBuildLocalRelation(), previously, namespace was used to
determine if the table should be marked temporary:

/* it is temporary if and only if it is in my temp-table namespace */
rel->rd_istemp = isTempOrToastNamespace(relnamespace);

But in Master branch, now if I look at RelationBuildLocalRelation(),
there is no such logic to mark relpersistence.

Was this intentional or is it a bug?

Regards
-Amit Khandekar


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Amit Khandekar <amit(dot)khandekar(at)enterprisedb(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: relpersistence and temp table
Date: 2011-07-01 14:32:08
Message-ID: BANLkTin9iekQkZU0vqs3UaMzer85+2jtMg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 1, 2011 at 8:06 AM, Amit Khandekar
<amit(dot)khandekar(at)enterprisedb(dot)com> wrote:
> In 9.1, if a table is created using an explicit pg_temp qualification,
> the pg_class.relpersistence is marked 'p', not 't'.
>
> postgres=# CREATE TABLE pg_temp.temptable (i int4);
> CREATE TABLE
>
> postgres=# select relpersistence from pg_class where relname = 'temptable';
>  relpersistence
> ----------------
>  p
> (1 row)
>
>
> BUt the table does go away if I exit the session:
>
> postgres=# \q
> edb-pg ~/git-pg1 $ psql
> psql (9.0.1, server 9.2devel)
> WARNING: psql version 9.0, server version 9.2.
>         Some psql features might not work.
> Type "help" for help.
>
> postgres=# select relpersistence from pg_class where relname = 'temptable';
>  relpersistence
> ----------------
> (0 rows)
>
> So in that sense, it does work as a temp table, but the relpersistence
> is not 't'. So, is the "temp"ness no longer identified by
> pg_class.relpersistence now?
>
>
> In RelationBuildLocalRelation(), previously, namespace was used to
> determine if the table should be marked temporary:
>
>        /* it is temporary if and only if it is in my temp-table namespace */
>        rel->rd_istemp = isTempOrToastNamespace(relnamespace);
>
> But in Master branch, now if I look at RelationBuildLocalRelation(),
> there is no such logic to mark relpersistence.
>
> Was this intentional or is it a bug?

That's a bug. Thanks for the report.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Amit Khandekar <amit(dot)khandekar(at)enterprisedb(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: relpersistence and temp table
Date: 2011-07-01 17:59:48
Message-ID: BANLkTimDZHJJ-Ha_uUYmWV21_azxh8Cgfw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 1, 2011 at 10:32 AM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Fri, Jul 1, 2011 at 8:06 AM, Amit Khandekar
> <amit(dot)khandekar(at)enterprisedb(dot)com> wrote:
>> In 9.1, if a table is created using an explicit pg_temp qualification,
>> the pg_class.relpersistence is marked 'p', not 't'.
>
> That's a bug.  Thanks for the report.

OK, so I think the problem here is that, in 9.0, it was possible to
figure out what value relistemp should take at a very late date,
because it was entirely a function of the schema name. A temporary
schema implies relistemp = true, while a non-temporary schema implies
relistemp = false. However, in 9.1, that clearly won't do, since
unlogged and permanent tables can share the same schema. Moreover, by
the time we get as far as RelationBuildLocalRelation(), we've already
made lots of other decisions based on relpersistence, so it seems that
we need to make this correct as early as possible. It's not feasible
to do that in the parser, because the creation namespace could also
come from search_path:

SET search_path = pg_temp;
CREATE TABLE foo (a int);

So it seems we can't fix this any earlier than
RangeVarGetCreationNamespace(). In the attached patch, I took
basically that approach, but created a new function
RangeVarAdjustRelationPersistence() that does the actual adjusting
(since de-constifying RangeVarGetCreationNamespace() didn't seem
smart), plus adds a bunch of additional sanity-checking that I
previously overlooked. Namely, it forbids:

- creating unlogged tables in temporary schemas
- creating relations in temporary schemas of other sessions

On the other hand, it does allow CREATE TEMP TABLE pg_temp.foo(a int),
which was somewhat pointlessly forbidden by previous releases. In
short, the code now checks directly what it used to check by
inference: that you're not creating a temporary table in a permanent
schema, or the other way around.

I also rearranged a few other bits of code to make sure that the
appropriate fixups happen BEFORE we enforce the condition that
temporary tables mustn't be created in security-restricted contexts.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

Attachment Content-Type Size
fix-relpersistence-early.patch application/octet-stream 11.0 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Amit Khandekar <amit(dot)khandekar(at)enterprisedb(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: relpersistence and temp table
Date: 2011-07-03 21:47:45
Message-ID: CA+TgmoYtTt1tT2X7jZ_O9_7-RaiRxpywG4TDRQ9SJYmFfOj53A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 1, 2011 at 1:59 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Fri, Jul 1, 2011 at 10:32 AM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
>> On Fri, Jul 1, 2011 at 8:06 AM, Amit Khandekar
>> <amit(dot)khandekar(at)enterprisedb(dot)com> wrote:
>>> In 9.1, if a table is created using an explicit pg_temp qualification,
>>> the pg_class.relpersistence is marked 'p', not 't'.
>>
>> That's a bug.  Thanks for the report.
>
> OK, so I think the problem here is that, in 9.0, it was possible to
> figure out what value relistemp should take at a very late date,
> because it was entirely a function of the schema name.  A temporary
> schema implies relistemp = true, while a non-temporary schema implies
> relistemp = false.   However, in 9.1, that clearly won't do, since
> unlogged and permanent tables can share the same schema.  Moreover, by
> the time we get as far as RelationBuildLocalRelation(), we've already
> made lots of other decisions based on relpersistence, so it seems that
> we need to make this correct as early as possible.  It's not feasible
> to do that in the parser, because the creation namespace could also
> come from search_path:
>
> SET search_path = pg_temp;
> CREATE TABLE foo (a int);
>
> So it seems we can't fix this any earlier than
> RangeVarGetCreationNamespace().  In the attached patch, I took
> basically that approach, but created a new function
> RangeVarAdjustRelationPersistence() that does the actual adjusting
> (since de-constifying RangeVarGetCreationNamespace() didn't seem
> smart), plus adds a bunch of additional sanity-checking that I
> previously overlooked.  Namely, it forbids:
>
> - creating unlogged tables in temporary schemas
> - creating relations in temporary schemas of other sessions
>
> On the other hand, it does allow CREATE TEMP TABLE pg_temp.foo(a int),
> which was somewhat pointlessly forbidden by previous releases.  In
> short, the code now checks directly what it used to check by
> inference: that you're not creating a temporary table in a permanent
> schema, or the other way around.
>
> I also rearranged a few other bits of code to make sure that the
> appropriate fixups happen BEFORE we enforce the condition that
> temporary tables mustn't be created in security-restricted contexts.

Committed.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Amit Khandekar <amit(dot)khandekar(at)enterprisedb(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: relpersistence and temp table
Date: 2011-07-12 01:55:29
Message-ID: 201107120155.p6C1tT208504@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas wrote:
> On Fri, Jul 1, 2011 at 10:32 AM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> > On Fri, Jul 1, 2011 at 8:06 AM, Amit Khandekar
> > <amit(dot)khandekar(at)enterprisedb(dot)com> wrote:
> >> In 9.1, if a table is created using an explicit pg_temp qualification,
> >> the pg_class.relpersistence is marked 'p', not 't'.
> >
> > That's a bug. ?Thanks for the report.
>
> OK, so I think the problem here is that, in 9.0, it was possible to
> figure out what value relistemp should take at a very late date,
> because it was entirely a function of the schema name. A temporary
> schema implies relistemp = true, while a non-temporary schema implies
> relistemp = false. However, in 9.1, that clearly won't do, since
> unlogged and permanent tables can share the same schema. Moreover, by
> the time we get as far as RelationBuildLocalRelation(), we've already
> made lots of other decisions based on relpersistence, so it seems that
> we need to make this correct as early as possible. It's not feasible
> to do that in the parser, because the creation namespace could also
> come from search_path:
>
> SET search_path = pg_temp;
> CREATE TABLE foo (a int);
>
> So it seems we can't fix this any earlier than
> RangeVarGetCreationNamespace(). In the attached patch, I took
> basically that approach, but created a new function
> RangeVarAdjustRelationPersistence() that does the actual adjusting
> (since de-constifying RangeVarGetCreationNamespace() didn't seem
> smart), plus adds a bunch of additional sanity-checking that I
> previously overlooked. Namely, it forbids:
>
> - creating unlogged tables in temporary schemas
> - creating relations in temporary schemas of other sessions
>
> On the other hand, it does allow CREATE TEMP TABLE pg_temp.foo(a int),
> which was somewhat pointlessly forbidden by previous releases. In
> short, the code now checks directly what it used to check by
> inference: that you're not creating a temporary table in a permanent
> schema, or the other way around.
>
> I also rearranged a few other bits of code to make sure that the
> appropriate fixups happen BEFORE we enforce the condition that
> temporary tables mustn't be created in security-restricted contexts.

Does this affect tables created during 9.1 beta? I assume a server
restart fixes all this, but I am just checking.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Amit Khandekar <amit(dot)khandekar(at)enterprisedb(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: relpersistence and temp table
Date: 2011-07-12 03:43:22
Message-ID: 2EB53717-5F2A-4188-9660-360F43DB20CD@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jul 11, 2011, at 8:55 PM, Bruce Momjian <bruce(at)momjian(dot)us> wrote:
> Does this affect tables created during 9.1 beta? I assume a server
> restart fixes all this, but I am just checking.

Yes, I think a server restart will fix it, though there might be corner cases I'm not thinking of.

...Robert