一. 表查询

1. 单表大小查询

  如果要查询单个表的大小,可以使用常用的函数,参考语句如下:

select pg_size_pretty(pg_relation_size('表名'));

  注意:这个查询结果不包括索引大小,如果要查询索引大小,可以通过查询information_schema.tables来获取。

2. 所有数据库表大小批量查询

  如果要查询所有表的大小,包括索引,那么最方便的就是直接查询information_schema.tables表了,可以参考如下查询语句: 

 1 select
 2     table_name,
 3     pg_size_pretty(table_size) as table_size,
 4     pg_size_pretty(indexes_size) as indexes_size,
 5     pg_size_pretty(total_size) as total_size
 6 from
 7     (
 8     select
 9         table_name,
10         pg_table_size(table_name) as table_size,
11         pg_indexes_size(table_name) as indexes_size,
12         pg_total_relation_size(table_name) as total_size
13     from
14         (
15         select
16             ('"' || table_schema || '"."' || table_name || '"') as table_name
17         from
18             information_schema.tables
19 ) as all_tables
20     order by
21         total_size desc
22 ) as pretty_sizes;

3. 未完待续.... ... (后续不断补充)