一 .数据库大小的统计
1.单个库查询
select pg_database_size('zybdb');
2.查询所有的库
方法一:
select pg_database.datname, pg_size_pretty (pg_database_size(pg_database.datname)) AS size from pg_database;
方法二:
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
二 数据库表大小查询
所有表方法一:
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 WHERE table_schema='cas_it'
ORDER BY pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') desc;
所有表方法二:
SELECT relname,pg_size_pretty(pg_relation_size(relid))
FROM pg_stat_user_tables WHERE schemaname='cas_it' ORDER BY pg_size_pretty(pg_relation_size(relid)) DESC
单个表方法一:
select pg_relation_size('cas_it.t4');
单个表方法二:
select pg_size_pretty(pg_total_relation_size('cas_it.t4'));
三 查询索引大小
select
schemaname,
relname,
indexrelname,
pg_size_pretty(pg_relation_size(indexrelid))
from pg_stat_user_indexes
where schemaname = 'cas_it'
order by pg_relation_size(indexrelid) desc;