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.
views:
168answers:
2
+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
2010-04-07 23:53:02
Incidentally, if someone has any information on how to alias the big, repeated expression, I'd be glad to hear it.
aib
2010-04-08 00:03:29
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
2010-04-08 11:08:28
very nice and closer to relational algebra. thanks.
aib
2010-04-08 19:08:10
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
2010-04-10 00:31:39