临时表

hubble支持会话范围的临时表(也称为临时表)。与持久表不同,临时表只能从创建它们的会话中访问,并且在会话结束时被删除。

要创建临时表,请将TEMP/TEMPORARY添加到CREATE TABLE或者CREATE TABLE AS语句。

hubble还支持临时视图和临时序列。

说明

  • 临时表会在会话结束时自动删除。
  • 只能从创建临时表的会话中访问临时表。
  • 临时表在同一会话中的事务中持续存在。
  • 临时表可以引用持久表,但持久表不能引用临时表。
  • 临时表不能转换为持久表。
  • 对于PostgreSQL兼容性,hubble只支持会话范围的临时表,不支持PostgreSQL中用于定义事务范围临时表的子句ON COMMIT DELETE ROWSON COMMIT DROP

默认情况下,每30分钟hubble会清理所有未绑定到活动会话的临时对象。您可以使用sql.temp_object_cleaner.cleanup_interval更改清理作业的运行频率。

临时模式

临时表不是public架构的一部分。相反,当您为会话创建第一个临时表时,hubble会为数据库当前会话中的所有临时表、临时视图和临时序列生成一个临时模式。hubble会为数据库当前会话中的所有临时表默认pg_temp_<id>

因为SHOW TABLES语句默认为public模式,所以SHOW TABLES在不指定模式的情况下使用将不会返回任何临时表。

示例

要使用临时表,需要先设置变量experimental_enable_temp_tables=on

set experimental_enable_temp_tables=on;

创建临时表

create temp table users (
        cust_id UUID,
        city STRING,
        cust_name STRING,
        cust_address STRING,
        CONSTRAINT "primary" PRIMARY KEY (cust_id ASC)
);

可以使用SHOW CREATE查看临时表:

show create table users;
 table_name |                         create_statement
-------------+-------------------------------------------------------------------
  users      | CREATE TEMP TABLE pg_temp_1661498695578411398_8589934593.users (
             |     cust_id UUID NOT NULL,
             |     city STRING NULL,
             |     cust_name STRING NULL,
             |     cust_address STRING NULL,
             |     CONSTRAINT "primary" PRIMARY KEY (cust_id ASC),
             |     FAMILY "primary" (cust_id, city, cust_name, cust_address)
             | )

创建一个引用另一个临时表的临时表

要创建另一个引用的临时表users

create temp table trans (
        id UUID NOT NULL,
        city STRING NOT NULL,
        type STRING,
        owner_id UUID,
        creation_time TIMESTAMP,
        CONSTRAINT "primary" PRIMARY KEY ( id ASC),
        CONSTRAINT fk_city_ref_users FOREIGN KEY ( owner_id) REFERENCES users( cust_id)
);

显示会话中的所有临时表

要显示会话临时模式中的所有临时表,请使用SHOW TABLES FROM pg_temp

show tables from  pg_temp;
               schema_name               | table_name | type  | owner | estimated_row_count | locality
-----------------------------------------+------------+-------+-------+---------------------+-----------
  pg_temp_1661498695578411398_8589934593 | trans      | table | root  |                   0 | NULL
  pg_temp_1661498695578411398_8589934593 | users      | table | root  |                   0 | NULL

还可以在SHOW语句中使用临时模式的全名(例如,show tables from pg_temp_1661498695578411398_8589934593)。

显示临时表information_schema

虽然临时表不包含在public架构中,但临时表的元数据包含在information_schemapg_catalog架构中。

例如,该information_schema.tables表包含有关所有数据库中所有模式中的所有表的信息,包括临时表:

select * from information_schema.tables where table_schema='pg_temp_1661498695578411398_8589934593';
  table_catalog |              table_schema              | table_name |   table_type    | is_insertable_into | version
----------------+----------------------------------------+------------+-----------------+--------------------+----------
  hdb           | pg_temp_1661498695578411398_8589934593 | users      | LOCAL TEMPORARY | YES                |       2
  hdb           | pg_temp_1661498695578411398_8589934593 | trans      | LOCAL TEMPORARY | YES                |       2

取消会话

如果您结束会话,所有临时表都将丢失。

show session_id;
             session_id
------------------------------------
  170ed442b8b9e1860000000200000001
cancel session '170ed442b8b9e1860000000200000001';
ERROR: driver: bad connection
warning: connection lost!
opening new connection: all session settings will be lost
show create table users;
ERROR: relation "trans" does not exist
SQLSTATE: 42P01