简体   繁体   English

在postgres中计算表格大小的正确方法是什么?

[英]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.

相关问题 postgres 表中元数据的大小是多少? - What is the size of metadata in a postgres table? 在Postgres中将表的一部分行从一个数据库复制到另一个数据库的最佳方法是什么? - What's the best way to copy a subset of a table's rows from one database to another in Postgres? 在postgres中,使用连接表进行AND的最佳方法是什么? - In postgres, what is the best way to do an AND with a join table? 基于关联计算表中行之间相似度的最佳方法是什么? - What's the best way to calculate similarity between rows in a table based on association? SQLite什么是使一个表容纳多个表的数据的正确方法? - Sqlite what's the right way to make a table holding data for several tables? 在 Rails 中进行计数的正确方法是什么? - What's the right way to do counts in Rails? 用SQL编写此查询的正确方法是什么? - What's the right way to write this query in SQL? 从 Postgres 中的表函数返回多行的最简单方法是什么? - What is the simplest way to return multiple rows from table function in Postgres? 批量插入 Postgres 的最快方法是什么? - What's the fastest way to do a bulk insert into Postgres? 如果我只有用户 ID,那么计算留存率的最佳方法是什么? - What's the best way to calculate retention if all I have is userIds?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM