PostgreSQL 查询所有数据库大小,表大小,索引大小,以及表空间大小

1. 查询数据库大小

  1.  
    -- 查询单个数据库大小
  2.  
    select pg_size_pretty(pg_database_size('postgres')) as size;
  3.  
     
  4.  
    -- 查询所有数据库大小
  5.  
    select datname, pg_size_pretty (pg_database_size(datname)) AS size from pg_database;

2. 查询表大小

  1.  
    -- 查询单个表大小
  2.  
    select pg_size_pretty(pg_relation_size('mytab')) as size;
  3.  
     
  4.  
    -- 查询所有表大小
  5.  
    select relname, pg_size_pretty(pg_relation_size(relid)) as size from pg_stat_user_tables;
  6.  
     
  7.  
    -- 查询单个表的总大小,包括该表的索引大小
  8.  
    select pg_size_pretty(pg_total_relation_size('tab')) as size;
  9.  
     
  10.  
    -- 查询所有表的总大小,包括其索引大小
  11.  
    select relname, pg_size_pretty(pg_total_relation_size(relid)) as size from pg_stat_user_tables;

3. 查询索引大小(暂时没有一次性查询所有索引大小的函数)

  1.  
    -- 查询单个索引大小
  2.  
    select pg_size_pretty(pg_relation_size('myindex')) as size;

4. 查询表空间大小

  1.  
    -- 查询单个表空间大小
  2.  
    select pg_size_pretty(pg_tablespace_size('pg_default')) as size;
  3.  
     
  4.  
    -- 查询所有表空间大小
  5.  
    select spcname, pg_size_pretty(pg_tablespace_size(spcname)) as size from pg_tablespace;
  6.  
    -- 或
  7.  
    select spcname, pg_size_pretty(pg_tablespace_size(oid)) as size from pg_tablespace;
posted @ 2020-09-10 14:02  疯子110  阅读(11496)  评论(0编辑  收藏  举报