會話級或事務級的臨時表,臨時表在會話結束或事物結束自動刪除,任何在臨時表上建立的索引也會被自動刪除。除非用模式修飾的名字引用,不然現有的同名永久表在臨時表存在期間,在本會話或事務中是不可見的。另外臨時表對其餘會話也是不可見的,可是會話級的臨時表也能夠使用臨時表所在模式修飾的名字引用。sql
建立臨時表的語法:數據庫
CREATE TEMP tbl_name()ON COMMIT{PRESERVE ROWS|DELETE ROWS|DROP};app
PRESERVE ROWS:默認值,事務提交後保留臨時表和數據post
DELETE ROWS:事務提交後刪除數據,保留臨時表性能
DROP:事務提交後刪除表測試
示例1spa
會話A:設計
建立臨時表postgresql
test=# create temp table tbl_temp(a int); CREATE TABLE
會話B:code
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是爲臨時數據設計的,寫入性能較高,可是當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 0 S postgres 2129 1 0 80 0 - 66830 poll_s 04:24 ? 00:00:00 /opt/pg9.6/bin/postgres -D /mnt/pgdata 1 S postgres 2130 2129 0 80 0 - 29645 ep_pol 04:24 ? 00:00:00 postgres: logger process 1 S postgres 2132 2129 0 80 0 - 66898 poll_s 04:24 ? 00:00:00 postgres: checkpointer process 1 S postgres 2133 2129 0 80 0 - 66830 ep_pol 04:24 ? 00:00:00 postgres: writer process 1 S postgres 2134 2129 0 80 0 - 66871 ep_pol 04:24 ? 00:00:00 postgres: wal writer process 1 S postgres 2135 2129 0 80 0 - 66954 ep_pol 04:24 ? 00:00:00 postgres: autovacuum launcher process 1 S postgres 2136 2129 0 80 0 - 29677 ep_pol 04:24 ? 00:00:00 postgres: stats collector process 0 S root 2262 2099 0 80 0 - 28768 n_tty_ 04:52 pts/1 00:00:00 /opt/pg9.6/bin/psql -d test -U postgres 1 S postgres 2264 2129 0 80 0 - 67351 ep_pol 04:52 ? 00:00:02 postgres: postgres test [local] idle 0 S root 2334 2198 0 80 0 - 25813 pipe_w 05:15 pts/2 00:00:00 grep postgres [root@MiWiFi-R1CL-srv ~]# kill -9 2129 [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 ~]# 2016-06-22 05:16:04.399 CST 2372 LOG: redirecting log output to logging collector process 2016-06-22 05:16:04.399 CST 2372 HINT: Future log output will appear in directory "/var/log/pg_log".
再次查詢unlogged表testu,發現數據已丟失
test=# select * from testu ; a --- (0 rows)