views:

168

answers:

2

I'm coming to Postgres from Oracle and looking for a way to find the table and index size in terms of bytes/MB/GB/etc, or even better the size for all tables. In Oracle I had a nasty long query that looked at user_lobs and user_segments to give back an answer. I assume in Postgres there's something I can use in the information_schema tables, but I'm not seeing where. Thanks in advance.

+6  A: 

Try the Database Object Size Functions. An example:

SELECT pg_size_pretty(pg_total_relation_size('"schema"."Table"'));

For all tables, something along the lines of:

SELECT
    table_schema || '.' || table_name AS table_full_name,
    pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size
FROM information_schema.tables
ORDER BY
    pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC
aib
Incidentally, if someone has any information on how to alias the big, repeated expression, I'd be glad to hear it.
aib
You can't alias it, but you can always run it in a subquery... like:SELECT table_full_name,pg_size_pretty(size) FROM ( SELECT .. AS table_full_name, .. AS size FROM ....) x ORDER BY size
Magnus Hagander
very nice and closer to relational algebra. thanks.
aib
A: 

PostgreSQL tables have three components: the table itself, any indexes on it, and potentially TOAST data. There's a couple of examples showing how to slide and dice the available information various ways at http://wiki.postgresql.org/wiki/Disk_Usage

Greg Smith