【问题标题】:How to find biggest files in cluster如何找到集群中最大的文件
【发布时间】:2021-09-30 13:32:52
【问题描述】:

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 脚本。

【问题讨论】:

    标签: database postgresql plpgsql psqlodbc


    【解决方案1】:

    您不能这样做,因为您只能查询您所连接的数据库。您需要依次连接到每个数据库。

    【讨论】:

    • 如何创建在每个数据库中运行查询并将结果合并到单个表中的 PL/pgSQL 脚本。也许有一些通用脚本可以用来运行相同的查询是所有数据库。
    • 正如我所说,你不能这样做,因为 PL/pgSQL 函数总是在单个数据库会话中运行。使用 shell 脚本或类似的东西。
    • PL/pgSQL 能否调用 postgres_fdw 从集群中的其他数据库中检索数据。
    • 当然,为什么不呢?
    • 在哪里可以找到 PL/pgSQL 示例代码,它枚举集群中的所有用户数据库并在它们上运行一些命令?
    猜你喜欢
    • 2023-01-25
    • 2012-02-26
    • 2021-12-22
    • 2015-11-21
    • 2013-09-10
    • 2020-04-08
    • 2016-02-28
    • 2021-09-24
    • 2020-08-27
    相关资源
    最近更新 更多