如何估计Postgres表中一列的大小?

时间:2013-08-19 14:54:49

标签: postgresql

Postgres 9.1中的表格中有一个text类型的列。我想知道该列对所需磁盘空间的影响。它不需要精确,但我想知道该列是否负责数据库消耗的磁盘空间的20%/ 30%。

我知道pg_relation_size,但它只能在表级运行。

我有许多具有相同架构的数据库。我倾倒了一个较小的一个并用grep切割了列并切割并比较了纯文本转储的大小。但这不一定是实时数据库中空间需求的一个很好的指标,而且对于大型数据库来说也是如此。

3 个答案:

答案 0 :(得分:44)

select
    sum(pg_column_size(the_text_column)) as total_size,
    avg(pg_column_size(the_text_column)) as average_size,
    sum(pg_column_size(the_text_column)) * 100.0 / pg_relation_size('t') as percentage
from t;

答案 1 :(得分:4)

对已接受的答案略有改进:漂亮地打印尺寸并使用pg_total_relation_size更加准确。

select
    pg_size_pretty(sum(pg_column_size(column_name))) as total_size,
    pg_size_pretty(avg(pg_column_size(column_name))) as average_size,
    sum(pg_column_size(column_name)) * 100.0 / pg_total_relation_size('table_name') as percentage
from table_name;

答案 2 :(得分:2)

如果要针对数据库中所有列按大小排序的报告,则采用以下方法

DROP FUNCTION IF EXISTS tc_column_size;
CREATE FUNCTION tc_column_size(table_name varchar(255), column_name varchar(255))
    RETURNS BIGINT AS
$$
    declare response BIGINT;
BEGIN
    EXECUTE 'select sum(pg_column_size(t."' || column_name || '")) from ' || table_name || ' t ' into response;
    return response;
END;
$$
    LANGUAGE plpgsql;

select z.table_name, z.column_name, pg_size_pretty(z.size) from(
select table_name, column_name, tc_column_size(table_name::varchar,
                                                               column_name::varchar) size
from information_schema.columns where table_schema='public') as z
where size is not null
order by z.size desc;