Postgres:检查物化视图占用的磁盘空间?


14

我知道如何检查Postgres中索引和表的大小(我使用的是9.4版):

SELECT
   relname AS objectname,
   relkind AS objecttype,
   reltuples AS "#entries", pg_size_pretty(relpages::bigint*8*1024) AS size
   FROM pg_class
   WHERE relpages >= 8
   ORDER BY relpages DESC;

但这没有显示具体化的视图。我如何查看它们占用了多少磁盘空间?

Answers:


23

这是假设物化视图都relpages >= 8pg_class,这并不一定如此。它实际上可以是空的-尚未填充,由表示pg_class.relispopulated = FALSE。在这种情况下,相应的磁盘文件的大小为零

请尝试:

SELECT relname   AS objectname
     , relkind   AS objecttype
     , reltuples AS entries
     , pg_size_pretty(pg_table_size(oid)) AS size  -- depending - see below
FROM   pg_class
WHERE  relkind IN ('r', 'i', 'm')
ORDER  BY pg_table_size(oid) DESC;

可用的类型有

r=普通表,
i=索引,
S=序列,
v=视图,
m=物化视图,
c=复合类型,
t= TOAST表,
f=外部表

使用数据库对象大小函数之一,而不要构建自己的函数。请注意,可以用不同的方式定义“表的大小”。细节:


1
确实回答正确。请注意,按尺寸排序是令人误解的,因为您是以人类可读的形式打印尺寸,从而按字典顺序进行排序
Jack

@杰克:好点。我加了一个更明智的选择ORDER BY
Erwin Brandstetter,2016年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.