天天看点

PostgreSQL 如何知道临时表是当前会话还是其他会话创建的

标签

PostgreSQL , 临时表 , 会话 , pg_table_is_visible

https://github.com/digoal/blog/blob/master/201812/20181224_01.md#%E8%83%8C%E6%99%AF 背景

PostgreSQL 不同的会话,可以创建同名的临时表,但是这个临时表是当前会话还是其他会话创建的呢?

create table stage.abc(id int);  
  
create temp table abc (like stage.abc);  
           
postgres=# select relname,relpersistence from pg_class where relname='abc';  
 relname | relpersistence   
---------+----------------  
 abc     | p  
 abc     | t  
(2 rows)  
           

通过pg_table_is_visible函数可以区分,这个临时表是当前会话,还是其他会话创建的

postgres=# select relname,relpersistence from pg_class where relpersistence='t' and relname='abc' and pg_table_is_visible(oid);    
 relname | relpersistence   
---------+----------------  
(0 rows)  
           

https://github.com/digoal/blog/blob/master/201812/20181224_01.md#%E5%8F%82%E8%80%83 参考

《PostgreSQL Oracle 兼容性之 - 全局临时表 global temp table》 https://www.postgresql.org/docs/11/functions-info.html

继续阅读