本文分享自华为云社区《GaussDB(DWS)临时表系列 - 本地临时表-云社区-华为云》,作者: acydy 。
GaussDB(DWS) 从8.2.1版本后支持三种形式的临时表:本地临时表、Volatile临时表、全局临时表。本文先介绍DWS的本地临时表功能。
本地临时表特点:表定义和数据都是会话相关,其他会话看不到本会话创建的本地临时表。元数据会持久化到系统表,集群节点异常出错可以支持RETRY(SQL语句出错自动重试_数据仓库服务 GaussDB(DWS)_性能调优_SQL执行troubleshooting_华为云)。
CREATE [LOCAL] { TEMPORARY | TEMP } TABLE [ IF NOT EXISTS ] table_name
({ column_name data_type [ compress_mode ] [ COLLATE collation ] [ column_constraint [ ... ] ]
| table_constraint
| LIKE source_table [ like_option [...] ] }
[, ... ])
[ WITH ( {storage_parameter = value} [, ... ] ) ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS } ]
建表时需要指定TEMP或者TEMPORARY,表示创建本地临时表。
ON COMMIT选项决定在事务中执行创建临时表操作,当事务提交时,此临时表的后续操作。
其他部分与普通表相同。
gaussdb=# create temp table tmp1(a int,b int);
NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using round-robin as the distribution mode by default.
HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
CREATE TABLE
临时表可以与非临时表同名。如果同名,优先级临时表高于非临时表。
gaussdb=# create temp table tmp1(a int,b int);
NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using round-robin as the distribution mode by default.
HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
CREATE TABLE
gaussdb=# insert into tmp1 values(1,1);
INSERT 0 1
gaussdb=# create table tmp1(a int,b int);
NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using round-robin as the distribution mode by default.
HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
CREATE TABLE
gaussdb=# select *from tmp1;
a | b
---+---
1 | 1
(1 row)
gaussdb=# select *from public.tmp1;
a | b
---+---
(0 rows)
视图:基于临时表创建的视图是临时视图。
postgres=# create view tmp_v1 as select *from tmp1;
NOTICE: view "tmp_v1" will be a temporary view
CREATE VIEW
临时表在元数据上与普通表的区别是临时表由于在其他会话不可见,所以会建在一个只属于当前会话的schema。本会话第一次创建临时表时会同时建立这个会话的schema。每一个会话的临时schema都不同。
会话1:
gaussdb=# create temp table tmp1(a int,b int);
NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using round-robin as the distribution mode by default.
HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
CREATE TABLE
gaussdb=# d+ tmp1
Table "pg_temp_coordinator1_65_3_140257888512760.tmp1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+-----------+---------+--------------+-------------
a | integer | | plain | |
b | integer | | plain | |
Has OIDs: no
Distribute By: ROUND ROBIN
Location Nodes: ALL DATANODES
Options: orientation=row, compression=no:
会话2, 查询不到tmp1表。
gaussdb=# select * from tmp1;
ERROR: relation "tmp1" does not exist
LINE 1: select * from tmp1;
^
临时schema的命名规则:pg_temp_Coordinator名_timelineID_全局自增ID_threadID
Coordinator名:CN名称,隔离不同CN创建的schema。
timelineID:在节点重启后会增加,用于判断此schema是否已经无效。
全局自增ID:单个CN上自增ID。同一个CN不用会话自增ID不同。
元数据:本地临时表的relpersistence标识是’t’。
gaussdb=# select relname, relpersistence from pg_class where relname = 'tmp1';
relname | relpersistence
---------+----------------
tmp1 | t
CN Retry功能开启时会为临时表数据记录日志,为保证数据一致性,在使用临时表时不建议切换CN Retry开关状态,保持使用临时表的会话中CN Retry开关始终处于打开状态或者关闭状态。
在打开CN Retry时,DN节点异常重启,临时表的数据可以保证不丢失。DN重启后,仍可以访问之前的会话。
如果希望临时表不记录日志:
set max_query_retry_times = 0;
关注#华为云开发者联盟# 点击下方,第一时间了解华为云新鲜技术~
华为云博客_大数据博客_AI博客_云计算博客_开发者中心-华为云
页面更新:2024-05-18
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号