网站首页 > 技术文章 正文
PostgreSQL支持两类临时表,一种是会话级的临时表,一种是事务级的临时表。在会话级别的临时表中,数据可以一直保存在整个会话的生命周期,而在事务级别的临时表,数据只存在这个事务的生命周期中。
在PostgreSQL中,不管是事务级的临时表还是会话级的临时表,当会话结束时,临时表就会消失,这与Oracle数据库不同。在Oracle数据库中,只是临时表中的数据消失,而临时表还存在。
如果在两个不同的session中创建一个同名的临时表,实际上创建的还是不同的两张表。
创建临时表
testdb=# create TEMPORARY table tmp_t1(id int primary key, note text);
在本session中可以看到这张表:
testdb=# \d
List of relations
Schema | Name | Type | Owner
-----------+--------+-------+----------
pg_temp_4 | tmp_t1 | table | postgres
可以看出临时表在schema pg_temp_4,其中4代表一个数字,不同的session数字不同
另开一个sql
-bash-4.2$ psql testdb
testdb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | e | table | postgres
public | t | table | postgres
public | t1 | table | postgres
在另一个session中,直接用\d命令看不到这个临时表,试着加上schema
testdb=# \d pg_temp_4.tmp_t1
Table "pg_temp_4.tmp_t1"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | not null |
note | text | | |
Indexes:
"tmp_t1_pkey" PRIMARY KEY, btree (id)
可以查到这个临时表,查询数据看看,并不能访问表中的数据
testdb=# select * from pg_temp_4.tmp_t1;
ERROR: cannot access temporary tables of other sessions
在第一个session中插入数据
testdb=# insert into tmp_t1 values(1,'1111');
INSERT 0 1
testdb=# insert into tmp_t1 values(2,'2222');
INSERT 0 1
testdb=# select * from tmp_t1;
id | note
----+------
1 | 1111
2 | 2222
在默认情况下,创建的临时表时会话级的,如果想创建出事务级的临时表,可以加"ON COMMIT DELETE ROWS"
testdb=# create TEMPORARY table tmp_t2(id int primary key, note text) on commit delete rows;
testdb=# begin;
testdb=*# insert into tmp_t2 values(1,'aaaa');
testdb=*# insert into tmp_t2 values(2,'bbbb');
testdb=*# select * from tmp_t2;
id | note
----+------
1 | aaaa
2 | bbbb
(2 rows)
testdb=*# commit;
COMMIT
testdb=# select * from tmp_t2;
id | note
----+------
(0 rows)
可以看出,事务一结束,这种临时表中的数据就消失了。
ON COMMIT子句有3种形式:
ON COMMIT PRESERVE ROWS: 不带ON COMMIT,默认情况下,会话级临时表
ON COMMIT DELETE ROWS: 数据只存在于事务周期,事务一提交,数据就消失了。
ON COMMIT DROP: 数据只存在于事务周期,事务一提交,临时表就消失。这种情况,创建临时表的语句与插入数据的语句要放到一个事务中。
创建临时表的关键字TEMPORARY可以缩写为TEMP,下面2条命令等价
create temporary table tmp_t1(id int primary key, note text)
create TEMP table tmp_t1(id int primary key, note text)
猜你喜欢
- 2025-07-17 故障分析 | MySQL 派生表优化(mysql pipe)
- 2025-07-17 《战场兄弟》全事件攻略 一般事件合同事件红装及隐藏职业攻略
- 2024-10-23 Oracle日常维护内容 oracle系统维护
- 2024-10-23 一文看懂PG的表空间物理布局设计、布局及管理
- 2024-10-23 oracle19c上安装样例数据库 oracle数据库如何安装
- 2024-10-23 超详细的oracle DB体系结构图(三)
- 2024-10-23 每天一个入坑小技巧:MySql和oracle语法区别(今日追加)
- 2024-10-23 性能优化技巧 - 内存关联计算 内存条关联
- 2024-10-23 Oracle大数据量delete清空数据后查询依旧很慢
- 2024-10-23 从零开始学习Oracle之管理表空间 oracle表空间是什么意思
你 发表评论:
欢迎- 615℃几个Oracle空值处理函数 oracle处理null值的函数
- 608℃Oracle分析函数之Lag和Lead()使用
- 595℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 592℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 587℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 580℃【数据统计分析】详解Oracle分组函数之CUBE
- 569℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 560℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
-
- PageHelper - 最方便的 MyBatis 分页插件
- 面试二:pagehelper是怎么实现分页的,
- MyBatis如何实现分页查询?(mybatis-plus分页查询)
- SpringBoot 各种分页查询方式详解(全网最全)
- 如何在Linux上运行exe文件,怎么用linux运行windows软件
- 快速了解hive(快速了解美国50个州)
- Python 中的 pyodbc 库(pydbclib)
- Linux搭建Weblogic集群(linux weblogic部署项目步骤)
- 「DM专栏」DMDSC共享集群之部署(一)——共享存储配置
- 故障分析 | MySQL 派生表优化(mysql pipe)
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- 前端获取当前时间 (50)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)