在 PostgreSQL 中记录 table 和架构大小

Record table and schema sizes in PostgreSQL

是否有一种查询 postgres 数据库大小的简单方法? 我正在尝试做这样的事情:

select 'session_metrics',pg_size_pretty(pg_total_relation_size('schema1.session_metrics'))
union
select 'transaction_metrics',pg_size_pretty(pg_total_relation_size('schema1.transaction_metrics'))
union
select 'cookie_user_metrics',pg_size_pretty(pg_total_relation_size('schema1.cookie_user_metrics'))
union
select 'cookie_transaction_metrics',pg_size_pretty(pg_total_relation_size('schema1.cookie_transaction_metrics'));

并将这些值存储在 table 中,以便我以后可以轻松跟踪 table 的增长率。 问题是我现在有 50 多个不同的 table,我不想在每次创建新的 table.

时都添加一行查询

如果有人能指导我做类似的事情,我将不胜感激:

select table_name, schema_name, size;

您要查找的 'table names' table 是 pg_catalog.pg_namespace。 以下查询改编自 psql \d 命令:

SELECT n.nspname as "Schema", 
    c.relname as "Name",
    pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as "Size", 
    now() as "Timestamp"
FROM pg_catalog.pg_class c
 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname <> 'pg_catalog'
  AND n.nspname <> 'information_schema'
  AND n.nspname !~ '^pg_toast'

我冒昧地添加了时间戳,因为您打算随时间比较大小。