Re: [Proposal] Global temporary tables

From: Prabhat Sahu <prabhat(dot)sahu(at)enterprisedb(dot)com>
To: 曾文旌 <wenjing(dot)zwj(at)alibaba-inc(dot)com>
Cc: tushar <tushar(dot)ahuja(at)enterprisedb(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>, Konstantin Knizhnik <k(dot)knizhnik(at)postgrespro(dot)ru>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, 蔡松露(子嘉) <zijia(at)taobao(dot)com>, "Cai, Le" <le(dot)cai(at)alibaba-inc(dot)com>, 萧少聪(铁庵) <shaocong(dot)xsc(at)alibaba-inc(dot)com>
Subject: Re: [Proposal] Global temporary tables
Date: 2020-04-02 08:45:44
Message-ID: CANEvxPrSOM26cGnTerCz-pJnNewroqJnw2BF97=BDmz6c+Rn8Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi All,

I have noted down few behavioral difference in our GTT implementation in PG
as compared to Oracle DB:
As per my understanding, the behavior of DROP TABLE in case of "Normal
table and GTT" in Oracle DB are as below:

1. Any tables(Normal table / GTT) without having data in a session, we
will be able to DROP from another session.
2. For a completed transaction on a normal table having data, we will be
able to DROP from another session. If the transaction is not yet complete,
and we are trying to drop the table from another session, then we will get
an error. (working as expected)
3. For a completed transaction on GTT with(on commit delete rows) (i.e.
no data in GTT) in a session, we will be able to DROP from another session.
4. For a completed transaction on GTT with(on commit preserve rows) with
data in a session, we will not be able to DROP from any session(not even
from the session in which GTT is created), we need to truncate the table
data first from all the session(session1, session2) which is having data.

*1. Any tables(Normal table / GTT) without having data in a session, we
will be able to DROP from another session.*
*Session1:*
create table t1 (c1 integer);
create global temporary table gtt1 (c1 integer) on commit delete rows;
create global temporary table gtt2 (c1 integer) on commit preserve rows;

*Session2:*
drop table t1;
drop table gtt1;
drop table gtt2;

-- *Issue 1:* But we are able to drop a simple table and failed to drop GTT
as below.

postgres=# drop table t1;
DROP TABLE
postgres=# drop table gtt1;
ERROR: can not drop relation gtt1 when other backend attached this global
temp table
postgres=# drop table gtt2;
ERROR: can not drop relation gtt2 when other backend attached this global
temp table

*3. For a completed transaction on GTT with(on commit delete rows) (i.e. no
data in GTT) in a session, we will be able to DROP from another session.*

*Session1:*create global temporary table gtt1 (c1 integer) on commit delete
rows;

*Session2:*
drop table gtt1;

-- *Issue 2:* But we are getting error for GTT with(on_commit_delete_rows)
without data.

postgres=# drop table gtt1;
ERROR: can not drop relation gtt1 when other backend attached this global
temp table

*4. For a completed transaction on GTT with(on commit preserve rows) with
data in any session, we will not be able to DROP from any session(not even
from the session in which GTT is created)*

*Case1:*
create global temporary table gtt2 (c1 integer) on commit preserve rows;
insert into gtt2 values(100);
drop table gtt2;

SQL> drop table gtt2;
drop table gtt2
*
ERROR at line 1:
ORA-14452: attempt to create, alter or drop an index on temporary table
already in use

-- *Issue 3:* But, we are able to drop the GTT(having data) which we have
created in the same session.

postgres=# drop table gtt2;
DROP TABLE

*Case2: GTT with(on commit preserve rows) having data in both session1 and
session2Session1:*create global temporary table gtt2 (c1 integer) on commit
preserve rows;
insert into gtt2 values(100);

*Session2:*insert into gtt2 values(200);

-- If we try to drop the table from any session we should get an error, it
is working fine.
drop table gtt2;

SQL> drop table gtt2;
drop table gtt2
*
ERROR at line 1:
ORA-14452: attempt to create, alter or drop an index on temporary table
already in use

postgres=# drop table gtt2 ;
ERROR: can not drop relation gtt2 when other backend attached this global
temp table

-- To drop the table gtt2 from any session1/session2, we need to truncate
the table data first from all the session(session1, session2) which is
having data.
*Session1:*
truncate table gtt2;
-- Session2:
truncate table gtt2;

*Session 2:*
SQL> drop table gtt2;

Table dropped.

-- *Issue 4:* But we are not able to drop the GTT, even after TRUNCATE the
table in all the sessions.
-- truncate from all sessions where GTT have data.
postgres=# truncate gtt2 ;
TRUNCATE TABLE

-- *try to DROP GTT still, we are getting error.*

postgres=# drop table gtt2 ;
ERROR: can not drop relation gtt2 when other backend attached this global
temp table

To drop the GTT from any session, we need to exit from all other sessions.
postgres=# drop table gtt2 ;
DROP TABLE

Kindly let me know if I am missing something.

On Wed, Apr 1, 2020 at 6:26 PM Prabhat Sahu <prabhat(dot)sahu(at)enterprisedb(dot)com>
wrote:

> Hi Wenjing,
> I hope we need to change the below error message.
>
> postgres=# create global temporary table gtt(c1 int) on commit preserve
> rows;
> CREATE TABLE
>
> postgres=# create materialized view mvw as select * from gtt;
> ERROR: materialized views must not use global temporary tables* or views*
>
> Anyways we are not allowed to create a "global temporary view",
> so the above ERROR message should change(i.e. *" or view"* need to be
> removed from the error message) something like:
> *"ERROR: materialized views must not use global temporary tables"*
>
> --
>
> With Regards,
> Prabhat Kumar Sahu
> EnterpriseDB: http://www.enterprisedb.com
>

--

With Regards,
Prabhat Kumar Sahu
EnterpriseDB: http://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2020-04-02 08:52:01 Re: [Proposal] Global temporary tables
Previous Message Alvaro Herrera 2020-04-02 08:43:28 Re: Add A Glossary