在特定会话中创建的临时表的大小

Size of temp tables created in a particular session

我使用以下查询

创建了一个临时文件 table
Drop table if exists tmp_a;
Create temp table tmp_a
(
    id int
);

Insert into tmp_a select generate_series(1,10000);

当我查询 pg_stat_activity 时,它在上述会话的 current_query 列中显示为 "IDLE"。

我将使用此 query 从 pg_class table 获取所有临时 table 的大小。 但我想要为特定会话创建的临时 tables 列表以及这些临时 tables 的大小,即如果我从两个不同的会话创建两个临时 tables 那么结果应该如下图

procpid | temp table name | size | username    
12345   | tmp_a           | 20   | gpadmin    
12346   | tmp_b           | 30   | gpadmin 

如果有人有查询,请分享查询

其实比你想象的要简单--
临时模式命名空间与会话 ID 相同 --

所以...

SELECT
  a.procpid as ProcessID,
  a.sess_id as SessionID,
    n.nspname as SchemaName,
    c.relname as RelationName,
    CASE c.relkind
    WHEN 'r' THEN 'table'
    WHEN 'v' THEN 'view'
    WHEN 'i' THEN 'index'
    WHEN 'S' THEN 'sequence'
    WHEN 's' THEN 'special'
    END as RelationType,
    pg_catalog.pg_get_userbyid(c.relowner) as RelationOwner,
    pg_size_pretty(pg_relation_size(n.nspname ||'.'|| c.relname)) as RelationSize
FROM 
  pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_catalog.pg_stat_activity a ON 'pg_temp_' || a.sess_id::varchar = n.nspname
WHERE  c.relkind IN ('r','s') 
AND  (n.nspname !~ '^pg_toast' and nspname like 'pg_temp%')
ORDER BY pg_relation_size(n.nspname ||'.'|| c.relname) DESC;

然后你得到 --

 processid | sessionid | schemaname | relationname | relationtype | relationowner | relationsize
-----------+-----------+------------+--------------+--------------+---------------+--------------
      5006 |         9 | pg_temp_9  | tmp_a        | table        | gpadmin       | 384 kB
      5006 |         9 | pg_temp_9  | tmp_b        | table        | gpadmin       | 384 kB
(2 rows)

让我们让该进程休眠——然后启动另一个进程....

gpadmin=#
[1]+  Stopped                 psql

[gpadmin@gpdb-sandbox ~]$ psql
psql (8.2.15)
Type "help" for help.

gpadmin=# SELECT nspname
FROM   pg_namespace
WHERE  oid = pg_my_temp_schema();

 nspname
---------
(0 rows)

gpadmin=# Create temp table tmp_a( id int );
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE

gpadmin=# SELECT nspname
FROM   pg_namespace
WHERE  oid = pg_my_temp_schema();

 nspname
---------
 pg_temp_10
(1 row)

... run the same query ...

 processid | sessionid | schemaname | relationname | relationtype | relationowner | relationsize
-----------+-----------+------------+--------------+--------------+---------------+--------------
      5006 |         9 | pg_temp_9  | tmp_a        | table        | gpadmin       | 384 kB
      5006 |         9 | pg_temp_9  | tmp_b        | table        | gpadmin       | 384 kB
     27365 |        10 | pg_temp_10 | tmp_a        | table        | gpadmin       | 384 kB
(3 rows)