统计PG数据库的库和表大小
- 2019 年 10 月 4 日
- 筆記
转载地址: https://blog.csdn.net/rocklee/article/details/51251174
统计各数据库占用磁盘大小:
SELECT d.datname AS Name, pg_catalog.pg_get_userbyid(d.datdba) AS Owner, CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname)) ELSE 'No Access' END AS SIZE FROM pg_catalog.pg_database d ORDER BY CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') THEN pg_catalog.pg_database_size(d.datname) ELSE NULL END DESC -- nulls first LIMIT 20 ;
统计数据库中各表占用磁盘大小:
SELECT table_schema || '.' || table_name AS table_full_name, pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size FROM information_schema.tables ORDER BY pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC ;
统计testdb库中各索引占用磁盘大小:
c testdb select schemaname,relname,indexrelname,pg_size_pretty( pg_relation_size(relid)) from pg_stat_user_indexes order by pg_relation_size(relid) desc limit 10 ;