gpt4 book ai didi

postgresql 数据库总大小与单个表大小的总和不匹配

转载 作者:行者123 更新时间:2023-11-29 11:17:35 24 4
gpt4 key购买 nike

我的 postgres 数据库(版本 8.2.3)显示大小为 24 GB。

为了得到这个数字,我执行了这个查询:

SELECT
oid, datname, pg_database_size(datname) as actualsize,
pg_size_pretty(pg_database_size(datname)) as size
FROM pg_database
ORDER BY datname

但是,当我执行此查询时,同一数据库中各个表的大小并没有增加 24 GB:

SELECT 
schemaname, tablename, pg_size_pretty(size) AS size_pretty,
pg_size_pretty(total_size) AS total_size_pretty
FROM
(SELECT *, pg_relation_size(schemaname||'.'||tablename) AS size,
pg_total_relation_size(schemaname||'.'||tablename) AS total_size
FROM pg_tables where schemaname = 'public') AS TABLES
ORDER BY total_size DESC;

我已经用 pretty size 和 total_size 总结了各个表的大小,但值不匹配:

我得到了 3.5 GB 的大小

我得到 5.2 GB 的 total_size_pretty。

我在哪里可以找到剩余空间的用途?

最佳答案

通常我使用以下两个查询来获取数据库对象的大小。希望这会有所帮助。

SELECT pg_size_pretty(sum(pg_relation_size(pg_class.oid))::bigint), nspname,
CASE pg_class.relkind WHEN 'r' THEN 'table' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 'v' THEN 'view' WHEN 't' THEN 'toast' ELSE pg_class.relkind::text END
FROM pg_class
LEFT OUTER JOIN pg_namespace ON (pg_namespace.oid = pg_class.relnamespace)
GROUP BY pg_class.relkind, nspname
ORDER BY sum(pg_relation_size(pg_class.oid)) DESC;

-

SELECT pg_size_pretty(pg_relation_size(pg_class.oid)), pg_class.relname, pg_namespace.nspname,
CASE pg_class.relkind WHEN 'r' THEN 'table' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 'v' THEN 'view' WHEN 't' THEN 'TOAST' ELSE pg_class.relkind::text END
FROM pg_class
LEFT OUTER JOIN pg_namespace ON (pg_namespace.oid = pg_class.relnamespace)
ORDER BY pg_relation_size(pg_class.oid) DESC;

关于postgresql 数据库总大小与单个表大小的总和不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5087339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com