PostgreSQL 临时表[通俗易懂]

PostgreSQL 临时表[通俗易懂]转载自: http://blog.163.com/digoal@126/blog/static/1638770402012101575032326/SQL标准中临时表是一次创建,以后使用的时候无须再次创建的.并且每个会话保持各自的数据.但是在PostgreSQL中,临时表的使用有所改变.1.临时表在会话结束后会自动删除(或者在事务结束后删除oncommitdrop)

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定


SQL标准中临时表是一次创建, 以后使用的时候无须再次创建的. 并且每个会话保持各自的数据.
但是在PostgreSQL中, 临时表的使用有所改变.
1. 临时表在会话结束后会自动删除(或者在事务结束后删除on commit drop). 也就是说每个会话中需要使用临时表的话需要重新创建.
    这个有好处也有坏处, 好处是不同的会话能够使用同名但是不同结构的临时表. SQL标准无法做到. 
    坏处是新建的会话如果只是要使用同名同结构的临时表也有重新创建. 
2. 临时表可以选择在事务结束后删除数据或者保留数据或者删除表.

【语法】

CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [
  { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
    | table_constraint
    | LIKE source_table [ like_option ... ] }
    [, ... ]
] )
[ INHERITS ( parent_table [, ... ] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE tablespace_name ]

Jetbrains全家桶1年46,售后保障稳定

红色部分是与临时表有关的. 其中GLOBAL和LOCAL在这个语法中是一样的, 没有分别, 但是在SQL标准中是不一样的.
ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP 
PRESERVE ROWS 
表示临时表的数据在事务结束后保留.
DELETE ROWS 表示
临时表的数据在事务结束后truncate掉.
DROP 
表示
临时表在事务结束后删除.
默认使用的是
PRESERVE ROWS.

【例子】
1. 
临时表在会话结束后会自动删除(或者在事务结束后删除on commit drop).
 
会话1 : 

pg9 . 2.0@db - 172 - 16 - 3 - 150 -> psql digoal digoal
psql ( 9.2 . 0 )
Type "help" for help .
digoal=> create temp table t(id int);
CREATE TABLE
digoal=> select relname,relnamespace,oid from pg_class where relname='t';
 relname | relnamespace |  oid  
---------+--------------+-------
 t       |        41192 | 41203
(1 row)
digoal=> select nspname from pg_namespace where oid=41192;
  nspname  
-----------
 pg_temp_2
(1 row)

退出会话1后重进, 临时表已经被删除了.

digoal=> \q
pg9.2.0@db-172-16-3-150-> psql digoal digoal
psql (9.2.0)
Type "help" for help.
digoal=> select nspname from pg_namespace where oid=41192;
  nspname  
-----------
 pg_temp_2
(1 row)
digoal=> select relname,relnamespace,oid from pg_class where relname='t';
 relname | relnamespace | oid 
---------+--------------+-----
(0 rows)


2. 
每个会话中需要使用临时表的话需要重新创建. 
好处是不同的会话能够使用同名但是不同结构的临时表.
会话1

pg9.2.0@db-172-16-3-150-> psql digoal digoal
psql (9.2.0)
Type "help" for help.
digoal => create temp table t ( id int );
CREATE TABLE

会话2

pg9.2.0@db-172-16-3-150-> psql digoal digoal
psql (9.2.0)
Type "help" for help.
digoal=> create temp table t(id text,id2 int);
CREATE TABLE
digoal=> select relname,relnamespace,oid from pg_class where relname='t';
 relname | relnamespace |  oid  
---------+--------------+-------
 t       |        11194 | 41206
 t       |        41192 | 41209
(2 rows)
digoal=> select nspname from pg_namespace where oid in (11194, 41192);
  nspname  
-----------
 pg_temp_1
 pg_temp_2
(2 rows)

会话3

pg9.2.0@db-172-16-3-150-> psql digoal digoal
psql (9.2.0)
Type "help" for help.
digoal=> create temp table t(id text,id2 int,info text);
CREATE TABLE
digoal=> select relname,relnamespace,oid from pg_class where relname='t';
 relname | relnamespace |  oid  
---------+--------------+-------
 t       |        11194 | 41206
 t       |        41192 | 41209
 t       |        41215 | 41217
(3 rows)
digoal=> select nspname from pg_namespace where oid in (11194, 41192, 41215);
  nspname  
-----------
 pg_temp_1
 pg_temp_2
 pg_temp_3
(3 rows)


3. 
临时表可以选择在事务结束后删除数据或者保留数据或者删除表.

digoal=> begin;
BEGIN
digoal=> create temp table test (id int) on commit preserve rows;
CREATE TABLE
digoal=> create temp table test1 (id int) on commit delete rows;
CREATE TABLE
digoal=> create temp table test2 (id int) on commit drop;
CREATE TABLE
digoal=> select relname,relnamespace,oid from pg_class where relname in ('test', 'test1', 'test2');
 relname | relnamespace |  oid  
---------+--------------+-------
 test    |        41215 | 41223
 test1   |        41215 | 41226
 test2   |        41215 | 41232
(3 rows)
digoal=> insert into test values (1);
INSERT 0 1
digoal=> insert into test1 values (1);
INSERT 0 1
digoal=> commit;
COMMIT

事务提交后test2已经被自动drop掉了.

digoal=> select relname,relnamespace,oid from pg_class where relname in ('test', 'test1', 'test2');
 relname | relnamespace |  oid  
---------+--------------+-------
 test    |        41215 | 41223
 test1   |        41215 | 41226
(2 rows)

test的数据事务提交后数据保留.

digoal=> select * from test;
 id 
----
  1
(1 row)

test1的数据事务提交后数据已删除.

digoal=> select * from test1;
 id 
----
(0 rows)

test2在事务提交后表已删除.

digoal=> select * from test2;
ERROR:  relation "test2" does not exist
LINE 1: select * from test2;
                      ^


4. 如果有临时表和非临时表重名了, 那么默认是使用临时表的, 如果要使用非临时表, 需要带上schema, 如schema.table.

digoal=> create table dup_table_name (id int);
CREATE TABLE
digoal=> create temp table dup_table_name (id int);
CREATE TABLE
digoal=> insert into digoal.dup_table_name values (1);
INSERT 0 1
digoal=> select * from dup_table_name ;
 id 
----
(0 rows)
digoal=> insert into dup_table_name values (2);
INSERT 0 1
digoal=> select * from dup_table_name ;
 id 
----
  2
(1 row)
digoal=> select * from digoal.dup_table_name ;
 id 
----
  1
(1 row)


5. 临时表上创建的索引也是临时的.

digoal=> create index idx_test on dup_table_name (id);
CREATE INDEX
digoal=> \d dup_table_name 
Table "pg_temp_3.dup_table_name"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | 
Indexes:
    "idx_test" btree (id)
digoal=> \di idx_test 
                   List of relations
  Schema   |   Name   | Type  | Owner  |     Table      
-----------+----------+-------+--------+----------------
 pg_temp_3 | idx_test | index | digoal | dup_table_name
(1 row)


6. 临时表无法选择性的创建在某个schema下面, 它是存在于临时schema的, 例如pg_temp_?. 对应的TOAST表也在临时的schema下, 例如(pg_toast_temp_?) . 虽然无法选择schema但是tablespace是可以指定的.

digoal=> create temp table digoal.tmp_test (id int);
ERROR:  cannot create temporary relation in non-temporary schema


7. PostgreSQL 中临时表的统计信息不会被autovacuum daemo自动收集. 所以如果有复杂查询的话, 最好再有DML后自己执行analyze.

【小结】
1. 如果有临时表和非临时表重名了, 那么默认是使用临时表的, 如果要使用非临时表, 需要带上schema, 如schema.table.
2. 临时表上创建的索引也是临时的.
3. 临时表无法选择性的创建在某个schema下面, 它是存在于临时schema的, 例如pg_temp_?. 对应的TOAST表也在临时的schema下, 例如(pg_toast_temp_?) . 虽然无法选择schema但是tablespace是可以指定的.
4. PostgreSQL 中临时表的统计信息不会被autovacuum daemo自动收集. 所以如果有索引的情况下, 最好再有DML后自己执行analyze.

【参考】

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210102.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • springmvc上传文件过程(c菜鸟教程)

    UploadControllerpackagecom.jege.spring.mvc;importjava.io.File;importjava.io.FileOutputStream;importjava.io.InputStream;importjavax.servlet.http.HttpServletRequest;importorg.apache.co

    2022年4月11日
    94
  • ES数据库操作入门总结「建议收藏」

    ES数据库操作入门总结「建议收藏」elasticsearch总的来说应该算是一个搜索引擎,公司使用一般是作为日志结果查询。json文档格式,倒排索引的快速以及分布式的特性,使得es可以在大量数据中快速查询到结果。windows安装和配置可参考官方网址。https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html倒排查询可参考这个知乎回答https://zhuanlan.zhihu.com/p/62892586可以使用浏览器的U

    2022年5月13日
    66
  • 应用程序正常初始化(0xc015002)失败解决方法

    应用程序正常初始化(0xc015002)失败解决方法

    2021年12月15日
    50
  • java static 变量存在哪_Java中的静态方法和静态变量存储在哪里?

    java static 变量存在哪_Java中的静态方法和静态变量存储在哪里?小编典典静态方法(实际上是所有方法)以及静态变量都存储在PermGen堆的部分中,因为它们是反射数据(与类相关的数据,而不与实例相关的数据)的一部分。更新说明:请注意,只有变量及其技术值(原始或引用)存储在PermGen空间中。如果你的静态变量是对对象的引用,则对象本身存储在堆的常规部分(青年/旧世代或幸存者空间)中。这些对象(除非它们是类之类的内部对象)不会存储在PermGen空间中。例:sta…

    2022年5月4日
    206
  • 关于cBridge2.0,你不能错过的关键信息(三)!

    关于cBridge2.0,你不能错过的关键信息(三)!上篇ELI5短文中我们讨论了cBridge2.0如何解决「自管」流动性模型中与状态守卫者网络(SGN)的协调问题。今天我们来继续讨论自管模型的最后一个话题:cBridge2.0的设计如何利用SGN为非托管桥接系统中的「作恶问题」提供有史以来第一个解决方案。那么什么是「作恶」呢?首先我们要知道,在cBridge2.0自管桥接模型中,对于桥接节点和用户来说,跨链交易的发生总是需要两个步骤,其顺序如下: 用户的第1步:向源链上的桥接节点进行「时间锁定」的转账,发起转账的用户…

    2022年5月4日
    54
  • inputstream和inputstreamreader_FileInputStream

    inputstream和inputstreamreader_FileInputStream使用默认的编码格式解码文件和使用文件原本的编码格式来解码文件importjava.io.*;importjava.util.HashMap;publicclassFuXi2{publicstaticvoidmain(String[]args)throwsIOException{demo01();demo02();}privatestaticvoiddemo02()throws.

    2022年9月26日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号