您的位置:首页 > 其它

pg中的临时表

2016-03-07 15:08 337 查看
pg的临时表有2中类型,一种是会话级的临时表,一种是事务级的临时表,会话级的临时表一直存在于会话的生命周期中,事务级别的临时表存在于事务的周期中。

不管什么类型的表,在会话结束后,表的定义就会消失。

postgres=# create temporary table temp_t as select * from pg_class;

SELECT 321

postgres=# \d

                关联列表

 架构模式  |  名称  |  类型  |  拥有者

-----------+--------+--------+----------

 pg_temp_6 | temp_t | 数据表 | postgres

 public    | t      | 数据表 | postgres

 public    | t1     | 数据表 | postgres

 public    | test   | 数据表 | postgres

 public    | v_test | 视图   | postgres

(5 行记录)

默认创建的是会话级别的临时表,在别的会话中是看不到这个表的,要创建事务级别的表,需要加on commit delete rows

postgres=# create temporary table temp_t2(id int,note text) on commit delete row

s;

CREATE TABLE

postgres=# insert into temp_t2 values(1,'a');

INSERT 0 1

postgres=# select * from temp_t2;

 id | note

----+------

(0 行记录)

postgres=# begin;

BEGIN

postgres=# insert into temp_t2 values(1,'a');

INSERT 0 1

postgres=# insert into temp_t2 values(1,'b');

INSERT 0 1

postgres=# select * from temp_t2;

 id | note

----+------

  1 | a

  1 | b

(2 行记录)

postgres=# end;

COMMIT

postgres=# select * from temp_t2;

 id | note

----+------

(0 行记录)

看到在事务结束后,表中的数据也都没有了,在别的会话中也是无法看到这个临时表的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: