如果您想查看所有数据库的大小,可以使用以下查询:
SELECT d.datname AS database_name, pg_size_pretty ( pg_database_size ( d.datname ) ) AS SIZE FROM pg_database d ORDER BY pg_database_size ( d.datname ) DESC
如果您还想查看数据库中各个表的大小,可以使用以下查询:
SELECT nspname AS SCHEMA_NAME, relname AS TABLE_NAME, pg_size_pretty ( pg_relation_size ( quote_ident( nspname ) || '.' || quote_ident( relname ) ) ) AS SIZE FROM pg_class JOIN pg_namespace ON pg_namespace.OID = pg_class.relnamespace WHERE pg_class.relkind = 'r' AND pg_namespace.nspname = 'public' ORDER BY pg_relation_size ( quote_ident( nspname ) || '.' || quote_ident( relname ) ) DESC;
以上查询在 PostgreSQL 15 版本中测试通过。