如何检查特定模式中的磁盘使用情况并总结所有模式? PG9 +

时间:2017-11-16 16:05:00

标签: postgresql

documentation about disk-usage 没有“快速而简单”的线索或信息......所以在这里检查更快。

  • 当我尝试SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'big'它工作正常...但“大”不是“public.big”,它是“othername.big”,如果还有公共,也会暧昧。

  • 当我尝试SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'othername.big'无效时。

我需要比较表的磁盘使用情况并检查(或求和)所有架构磁盘使用情况。

2 个答案:

答案 0 :(得分:1)

获取Schema大小:

SELECT schemaname, pg_size_pretty(t.taille::bigint) AS taille_table, pg_size_pretty(t.taille_totale::bigint) AS taille_totale_table
  FROM (SELECT schemaname,          
               sum(pg_relation_size(schemaname || '.' || tablename)) AS taille, 
               sum(pg_total_relation_size(schemaname || '.' || tablename)) AS taille_totale
          FROM pg_tables
          WHERE relname_exists(tablename,schemaname)   -- see note
GROUP BY schemaname) as t ORDER BY taille_totale DESC;

对于Chart by Schema,你可以这样做:

SELECT schemaname, tablename, tablespace, pg_size_pretty(taille) AS taille_table, pg_size_pretty(taille_totale) AS taille_totale_table
  FROM (SELECT *, 
               pg_relation_size(schemaname || '.' || tablename) AS taille, 
               pg_total_relation_size(schemaname || '.' || tablename) AS taille_totale
          FROM pg_tables) AS tables
          WHERE relname_exists(tablename,schemaname)   -- see note
 ORDER BY taille_totale DESC;

注意:要避免“错误:关系'x.y'不存在”,需要在使用之前保护字符串,所以

在Postgres 9.4 +中使用to_regclass(rel_name) ...请参阅https://stackoverflow.com/a/24089729

或对任何pg版本使用通用(参见“SwissKnife libraries”)函数,如下所示

CREATE or replace FUNCTION relname_exists(text,text default NULL) RETURNS boolean AS $$
  SELECT EXISTS (
     SELECT 1
     FROM   pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace,
            regexp_split_to_array($1,'\.') t(x) -- not work with quoted names
     WHERE  CASE
            WHEN COALESCE(x[2],'')>'' THEN   n.nspname = x[1]      AND c.relname = x[2]
            WHEN $2 IS NULL THEN         n.nspname = 'public'  AND c.relname = $1
            ELSE                         n.nspname = $2        AND c.relname = $1
     END
  )
$$ language SQL IMMUTABLE;

答案 1 :(得分:0)

使用pg_class并简化布局

与Hervé的查询(使用pg_tables)相同的结果......改编自https://wiki.postgresql.org/wiki/Disk_Usage

-- -- -- -- --
-- DISK-USAGE

CREATE VIEW pgvw_class_usage AS
  SELECT *, pg_size_pretty(table_bytes) AS table_size
  FROM (
    SELECT nspname , relname, total_bytes
           , total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes
    FROM (
        SELECT nspname , relname
          , pg_total_relation_size(c.oid) AS total_bytes
          , pg_indexes_size(c.oid) AS index_bytes
          , pg_total_relation_size(reltoastrelid) AS toast_bytes
        FROM pg_class c
        LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
        WHERE relkind = 'r'
    ) a
  ) t
  ORDER BY 1,2
; -- eg. SELECT * FROM pgvw_class_usage WHERE relname='foo' AND nspname='bar';

CREATE VIEW pgvw_nsclass_usage AS
  SELECT *, pg_size_pretty(table_bytes) as table_size
  FROM (
    SELECT nspname, count(*) as n_tables,
           sum(total_bytes) as total_bytes, sum(table_bytes) as table_bytes
    FROM pgvw_class_usage
    GROUP BY nspname
  ) t
; -- eg. SELECT * FROM pgvw_nsclass_usage WHERE nspname='bar';

实施例

      nspname       | n_tables | total_bytes | table_bytes | table_size 
--------------------+----------+-------------+-------------+------------
 bench1             |        8 |     4718592 |     3825664 | 3736 kB
 dataset            |        4 |     8552448 |     6225920 | 6080 kB
 information_schema |        7 |      352256 |      294912 | 288 kB
 pg_catalog         |       54 |     9003008 |     4734976 | 4624 kB
(4 rows)