问题描述
Debian Linux 服务器中的 Postgres 13 集群包含 30 个数据库。数据库包含多个模式。 如何找到占用磁盘空间最多的最大文件? 我试过了
select
relname::char(25),pg_size_pretty(pg_total_relation_size(c.oid))::char(10) as totalsize,n.nspname::char(12),case
when c.relkind='i' then 'index'
when c.relkind='t' then 'toast'
when c.relkind='r' then 'table'
when c.relkind='v' then 'view'
when c.relkind='c' then 'composite type'
when c.relkind='S' then 'sequence'
else c.relkind::text
end ::char(14) as "type"
from
pg_class c
left join pg_namespace n on n.oid = c.relnamespace
left join pg_tablespace t on t.oid = c.reltablespace
where
(pg_total_relation_size(c.oid)>>21)>0 order by
pg_total_relation_size(c.oid) desc
但它只返回当前数据库的大小。如何在整个集群中运行?可以为此使用一些 plpgsql 脚本。 输出应包括数据库名称列。
客户端应用程序使用 psqlODBC 来获取数据,因此最好避免使用 psql 或 shell 脚本。
解决方法
您不能这样做,因为您只能查询您所连接的数据库。需要依次连接各个数据库。