Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to estimate the size of one column in a Postgres table?

Tags:

postgresql

There is a column of type text in a table in Postgres 9.1. I'd like to know the impact of just that column on the disk space needed. It doesn't need to be precise, but I'd like to get an idea if that column is responsible for 20%/30%/... of the disk space consumed by the database.

I know pg_relation_size, but it only operates at table level.

I have many databases with this same schema. I dumped a smaller one and cut out the column with grep and cut and compared the size of the plain text dumps. But this is not necessarily a good indicator of space requirements in the live db, and it's also more difficult to do that for large databases.

like image 963
Thomas Kappler Avatar asked Aug 19 '13 14:08

Thomas Kappler


People also ask

How do I find the index size in PostgreSQL?

PostgreSQL index size To get total size of all indexes attached to a table, you use the pg_indexes_size() function. The pg_indexes_size() function accepts the OID or table name as the argument and returns the total disk space used by all indexes attached of that table.

How do I select a specific column in PostgreSQL?

PostgreSQL SELECT statement syntax If you specify a list of columns, you need to place a comma ( , ) between two columns to separate them. If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names.


2 Answers

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; 
like image 135
Clodoaldo Neto Avatar answered Sep 21 '22 03:09

Clodoaldo Neto


Slight improvement on the accepted answer: pretty print the size and use pg_total_relation_size to be more accurate.

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; 
like image 31
Denis Vermylen Avatar answered Sep 22 '22 03:09

Denis Vermylen