postgresql----TEMPORARY TABLE和UNLOGGED TABLE
一.TEMPORARY|TEMP TABLE
会话级或事务级的临时表,临时表在会话结束或事物结束自动删除,任何在临时表上创建的索引也会被自动删除。除非用模式修饰的名字引用,否则现有的同名永久表在临时表存在期间,在本会话或事务中是不可见的。另外临时表对其他会话也是不可见的,但是会话级的临时表也可以使用临时表所在模式修饰的名字引用。
创建临时表的语法:
CREATE TEMP tbl_name()ON COMMIT{PRESERVE ROWS|DELETE ROWS|DROP};
PRESERVE ROWS:默认值,事务提交后保留临时表和数据
DELETE ROWS:事务提交后删除数据,保留临时表
DROP:事务提交后删除表
示例1
会话A:
创建临时表
test=# create temp table tbl_temp(a int);
CREATE TABLE
会话B:
1.在会话B查询临时表tbl_temp,提示表不存在
test=# select * from tbl_temp;
ERROR: relation "tbl_temp" does not exist
LINE 1: select * from tbl_temp;
2.但是在会话B查询pg_class中可以查到tbl_temp的记录
test=# select relname,relnamespace from pg_class where relname = 'tbl_temp';
relname | relnamespace
----------+--------------
tbl_temp | 16488
(1 row)
3.从上述查询结果中可以看到临时表tbl_temp属于16488的模式
test=# select nspname from pg_namespace where oid = 16488;
nspname
-----------
pg_temp_3
(1 row)
4.直接使用模式修饰的表名访问成功
test=# select * from pg_temp_3.tbl_temp ;
a
---
(0 rows)
会话A:
退出会话A
会话B:
再次查询tbl_temp时提示不存在
test=# select * from pg_temp_3.tbl_temp ;
ERROR: relation "pg_temp_3.tbl_temp" does not exist
LINE 1: select * from pg_temp_3.tbl_temp ;
^
示例2.创建ON COMMIT DELETE ROWS的临时表
test=# begin ;
BEGIN
test=# create temp table tbl_temp(a int) on commit delete rows;
CREATE TABLE
test=# insert into tbl_temp values (1);
INSERT 0 1
test=# select * from tbl_temp ;
a
---
1
(1 row) test=# commit ;
COMMIT
test=# select * from tbl_temp ;
a
---
(0 rows)
示例3.创建ON COMMIT DROP临时表
test=# begin ;
BEGIN
test=# create temp table tbl_temp(a int) on commit drop;
CREATE TABLE
test=# commit ;
COMMIT
test=# select * from tbl_temp;
ERROR: relation "tbl_temp" does not exist
LINE 1: select * from tbl_temp;
^
示例4.查询数据库中所有临时表
test=# select relname,nspname from pg_class join pg_namespace on(relnamespace=pg_namespace.oid) where pg_is_other_temp_schema(relnamespace);
relname | nspname
----------+-----------
tbl_test | pg_temp_2
(1 row)
二.UNLOGGED TABLE
unlogged table是为临时数据设计的,写入性能较高,但是当postgresql进程崩溃时会丢失数据。
创建一张普通表test和一张unlogged表test,测试性能情况
普通表:
test=# create table test(a int);
CREATE TABLE
test=# \timing
Timing is on.
test=# insert into test select generate_series(1,1000000);
INSERT 0 1000000
Time: 3603.715 ms
unlogged表
test=# create unlogged table testu(a int);
CREATE TABLE
Time: 12.920 ms
test=# insert into testu select generate_series(1,1000000);
INSERT 0 1000000
Time: 801.376 ms
比较以上两个结果,unlogged表的写性能是普通表的4.5倍。
杀死postgresql的主进程,重启DB服务
[root@MiWiFi-R1CL-srv ~]# ps -elf | grep postgres
S postgres - poll_s : ? :: /opt/pg9./bin/postgres -D /mnt/pgdata
S postgres - ep_pol : ? :: postgres: logger process
S postgres - poll_s : ? :: postgres: checkpointer process
S postgres - ep_pol : ? :: postgres: writer process
S postgres - ep_pol : ? :: postgres: wal writer process
S postgres - ep_pol : ? :: postgres: autovacuum launcher process
S postgres - ep_pol : ? :: postgres: stats collector process
S root - n_tty_ : pts/ :: /opt/pg9./bin/psql -d test -U postgres
S postgres - ep_pol : ? :: postgres: postgres test [local] idle
S root - pipe_w : pts/ :: grep postgres
[root@MiWiFi-R1CL-srv ~]# kill -
[root@MiWiFi-R1CL-srv ~]# rm -rf /mnt/pgdata/postmaster.pid
[root@MiWiFi-R1CL-srv ~]# su -l postgres -c '/opt/pg9.6/bin/pg_ctl -D /mnt/pgdata start'
server starting
[root@MiWiFi-R1CL-srv ~]# -- ::04.399 CST LOG: redirecting log output to logging collector process
-- ::04.399 CST HINT: Future log output will appear in directory "/var/log/pg_log".
再次查询unlogged表testu,发现数据已丢失
test=# select * from testu ;
a
---
(0 rows)
postgresql----TEMPORARY TABLE和UNLOGGED TABLE的更多相关文章
- Postgresql实战经验之alter table 开小差了
Postgresql实战经验之alter table 开小差了 今天需要将一张有数据的表中一个字段varchar 类型转换为timestamp类型,但是pg的alter table 语句却开小差,出现 ...
- MySQL删除大表时潜在的问题(drop table,truncate table)
来源于:https://www.cnblogs.com/CtripDBA/p/11465315.html,侵删,纯截图,避免吸引流量之嫌 case1,删除大表时,因为清理自适应hash索引占用的内容导 ...
- OpenFlow Switch学习笔记(五)——Group Table、Meter Table及Counters
本文主要详述OpenFlow Switch的另外两个主要组件——Group Table和Meter Table,它们在整个OpenFlow Swtich Processing中也起到了重要作用. 1. ...
- delete table 和 truncate table
delete table 和 truncate table 使用delete语句删除数据的一般语法格式: delete [from] {table_name.view_name} [where< ...
- 【翻译】Flink Table Api & SQL —— Table API
本文翻译自官网:Table API https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/tableApi.ht ...
- PostgreSQL中,database,schema,table之间关系
从逻辑上看,schema,table,都是位于database之下. 首先,在postgres数据库下建立表(相当于建立在public schema下): [pgsql@localhost bin]$ ...
- 对PostgreSQL中tablespace 与 database, table的理解
开始: 当前的tablesapce信息 pgsql=# select * from pg_tablespace; spcname | spcowner | spclocation | spcacl | ...
- 【SqlServer】empty table and delete table and create table
1.建表 1 IF object_id (N'表名', N'U') IS NULL CREATE TABLE 表名 ( 2 id INT IDENTITY (1, 1) PRIMARY KEY ,.. ...
- 【MySQL】 empty table and delete table.
1.MySQL生成删除满足条件的表的sql: 1 SELECT 2 CONCAT( 3 'DROP TABLE ', 4 GROUP_CONCAT(table_name), 5 ';' 6 ) AS ...
随机推荐
- Qt/C++ 构造函数与explicit
1.默认构造函数 默认构造函数是指所有参数都提供了默认值的构造函数,通常指无参的构造函数或提供默认值的构造函数.如类Test1和Test2的构造函数 class Test1 { public: Tes ...
- 关于Cocos2d-x运行项目时弹框崩溃的解决
想要运行工程的时候,跳出一个框说停止cantnot open the window,还提到什么GLVM之类的,这是显卡驱动出现问题,如果是远程连接电脑的话,很有可能就是用来远程连接的那台电脑的显卡驱动 ...
- English Sentenses【no use】
1.Sorry if I might sound arrogant or offensive. 2.Any further question? 3.How dare you! 4.Try it if ...
- 图像处理之3d算法----2d转3d算法介绍
http://www.3dov.cn/html/c/37/index.html http://news.ifeng.com/a/20151117/46275220_0.shtml 磁力矩阵 http: ...
- 剑指offer_面试题5_从尾到头打印链表(栈和递归实现)
题目:输入一个链表的头结点,从尾到头反过来打印出每一个节点的值 考察 单链表操作.栈.递归等概念. 理解:要实现单链表的输出,那么就须要遍历.遍历的顺序是从头到尾.而节点输出的顺序是从尾到头.因此,先 ...
- Spring-JDBC配置
以C3P0连接池为例:由于C3P0是第三方,我们无法使用注解将其定义为bean,因此需要在applicationContext.xml中配置: <!-- 导入配置文件 --> <co ...
- iOS10.0 & Swift 3.0 对于升级项目的建议
iOS & Swift新旧版本更替, 在Apple WWDC大会开始之际, 也迎来了iOS 10.0, Swift 3.0 测试版, 到目前为止, 已经是测试版2.0, 每次更新都带来了新的语 ...
- path与classpath的差别
1.path的作用 path是系统用来指定可运行文件的完整路径.即使不在path中设置JDK的路径也可运行JAVA文件,但必须把完整的路径写出来,如C:\Program Files\Java\ ...
- 微软ASP.NET网站部署指南(2):部署SQL Server Compact数据库
1. 综述 对于数据库訪问,Contoso University程序要求以下的软件必须随程序一起部署.由于不属于.NET Framework: SQL Server Compact (数据库引擎) A ...
- [ATL/WTL]_[中级]_[保存CBitmap到文件-保存屏幕内容到文件]
场景: 1. 在做图片处理时,比方放大后或加特效后须要保存CBitmap(HBITMAP)到文件. 2.截取屏幕内容到文件时. 3.不须要增加第3方库时. 说明: 这段代码部分来自网上.第一次学atl ...