pg数据库查看表大小
在PG数据库中,查看表大小可以通过以下两种方式实现:
1. 单表大小查询:可以使用pg_size_pretty和pg_relation_size两个函数来查询单个表的大小。具体语句如下:
```
SELECT pg_size_pretty(pg_relation_size('表名'));
```
这个查询结果不包括索引大小,如果要查询索引大小,可以通过查询information_schema.tables来获取。
2. 所有数据库表大小批量查询:如果要查询所有表的大小,包括索引,可以直接查询information_schema.tables表。具体语句如下:
```
SELECT table_name, pg_size_pretty(table_size) as table_size, pg_size_pretty(indexes_size) as indexes_size, pg_size_pretty(total_size) as total_size FROM (SELECT table_name, pg_table_size(table_name) as table_size, pg_indexes_size(table_name) as indexes_size, pg_total_relation_size(table_name) as total_size FROM (SELECT '"' || table_schema || '."' || table_name || '"' AS table_name FROM information_schema.tables) AS all_tables ORDER BY total_size DESC) AS pretty_sizes;
```
这个查询结果包括表的数据文件、索引文件、fsm文件、vm文件、init文件和toast表的总大小。