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 行记录)
看到在事务结束后,表中的数据也都没有了,在别的会话中也是无法看到这个临时表的。
不管什么类型的表,在会话结束后,表的定义就会消失。
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 行记录)
看到在事务结束后,表中的数据也都没有了,在别的会话中也是无法看到这个临时表的。
相关文章推荐
- 算法 - 排序 - 插入排序
- 读什么,才能让你的心沉静下来
- var_dump()函数输出不完整,有省略号?解决办法
- elasticsearch 操作
- IE执行ajax时,返回JSON出现下载时怎么解决,需要配置
- 《Android开发艺术探索》——View的事件体系
- java 初识String
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(五) 补充:历史记录 和 消息提醒
- 集群和分布式
- xml常用四种解析方式优缺点的分析×××××
- 移动硬盘只读解决办法
- Android安卓应用程序窗口化
- Typescript Mixins(混合)
- 街机模拟器 MAME 现在是自由软件了
- .tar.bz2文件解压命令
- 百度地图图片叠加层切片工具:图片在不同级别间定位不一致(横向或纵向漂移)问题的解决
- 求数组长度
- Java 导出Excel方法三
- 抽象类与接口
- android开发之应用Crash自动抓取Log_自动保存崩溃日志到本地