1. 查看数据库大小:
SELECT pg_size_pretty(pg_database_size(current_database())) AS database_size;
2. 查看表的大小:
SELECT table_name, pg_size_pretty(pg_total_relation_size(table_name)) AS table_size
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY pg_total_relation_size(table_name) DESC;
3. 查看索引的大小:
SELECT indexrelname AS index_name, pg_size_pretty(pg_total_relation_size(indexrelname)) AS index_size
FROM pg_stat_all_indexes
WHERE schemaname = 'public'
ORDER BY pg_total_relation_size(indexrelname) DESC;
4. 查看所有表和索引的总大小:
SELECT
table_name,
pg_size_pretty(pg_total_relation_size(table_name)) AS table_size,
pg_size_pretty(pg_total_relation_size(indexrelname)) AS index_size
FROM information_schema.tables
LEFT JOIN pg_stat_all_indexes ON indexrelname = table_name
WHERE table_schema = 'public'
ORDER BY pg_total_relation_size(table_name) DESC;
这些查询将返回以人类可读的格式显示的数据库、表和索引的大小。请注意,这些查询提供的是估算值,实际使用的磁盘空间可能会有一些差异。
转载请注明出处:http://www.zyzy.cn/article/detail/8446/PostgreSQL