[英]What's the right way to calculate table size in postgres?
I'm working on a project where calculations are being made for certain tables in the postgres DB, and in different parts of code, there are 2 different calculations being made: 我正在开发一个项目,在这个项目中对postgres DB中的某些表进行计算,在代码的不同部分,有两个不同的计算:
select pg_total_relation_size ('TABLENAME');
and the second one: 第二个:
select (relpages*8/1024) from pg_class where relname='TABLENAME' (Size in MB)
I know that pg_total_relation_size is what I need to use, but I was wondering what does this other calculation represents, and if the second one is also some kind of size calculation, why am I getting different results for the same table? 我知道pg_total_relation_size是我需要使用的,但我想知道这个其他计算代表什么,如果第二个也是某种大小计算,为什么我在同一个表中得到不同的结果?
pg_total_relation_size
- is the right way to see full table size because it includes disk space utilized by indices and TOAST data. pg_total_relation_size
- 是查看完整表大小的正确方法,因为它包含索引和TOAST数据使用的磁盘空间。
When you do the second-one select - you get table size only ( relpages
is table size in 8Kb pages). 当您执行第二个选择时 - 您只获得表大小(
relpages
是8Kb页中的表大小)。 You can get TOAST and index sizes from pg_class as well but you need to do some more sql queries as described in here and sum all the sizes you've got. 你可以从烤面包和索引大小的pg_class很好,但你需要做一些更多的SQL查询中所描述这里 ,总结你已经得到了所有尺寸。
You can use pg_size_pretty(pg_total_relation_side(oid))
to pretty print table size: 您可以使用
pg_size_pretty(pg_total_relation_side(oid))
来打印表格大小:
select nspname
, relname
, pg_size_pretty(pg_total_relation_size(c.oid)) as "size"
from pg_class c
left join
pg_namespace n
on n.oid = c.relnamespace
where nspname not in ('pg_catalog', 'information_schema')
order by
pg_relation_size(c.oid) desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.