Simplify an Postgres SQL query for listing table and index sizes?

postgresql, postgresql-9.1, sql

Solution

I get comparable results (with different formatting) with this query:

select
    nspname as schema,
    relname as name,
    pg_relation_size(pg_class.oid) as size,
    pg_indexes_size(pg_class.oid) as index,
    pg_total_relation_size(pg_class.oid) as total,
    100 * case when relkind = 'i' then pg_relation_size(pg_class.oid) 
                                  else pg_indexes_size(pg_class.oid) end 
        / pg_total_relation_size(pg_class.oid) as i_ratio
from 
    pg_class
    join pg_namespace on relnamespace = pg_namespace.oid
order by 5 desc

Problem

The following Postgres SQL query will list all tables from all schemas and their sizes and index sizes. If a table is just an index table, it will show up as 100% index. ``` SELECT schema, name, pg_size_pretty(CASE WHEN is_index THEN 0 ELSE s END) AS size, pg_size_pretty(CASE WHEN is_index THEN s ELSE st - s END) AS index, CASE WHEN st = 0 THEN 0 WHEN is_index THEN 100 ELSE 100 - ((s*100) / st) END || '%' as ratio, pg_size_pretty(st) as total FROM (SELECT *, st = s AS is_index FROM (SELECT nspname as schema, relname as name, pg_relation_size(nspname || '.' || relname) as s, pg_total_relation_size(nspname || '.' || relname) as st FROM pg_class JOIN pg_namespace ON (relnamespace = pg_namespace.oid)) AS p) AS pp ORDER BY st DESC LIMIT 30; ``` It will give the following results: ``` schema | name | size | index | ratio | total ----------------+------------------------+---------+---------+-------+--------- public | conf | 4072 kB | 4360 kB | 52% | 8432 kB archive | product_param | 4048 kB | 3968 kB | 50% | 8016 kB public | conf_pkey | 0 bytes | 4320 kB | 100% | 4320 kB archive | product_value | 1568 kB | 1136 kB | 43% | 2704 kB public | param_mapping | 1472 kB | 832 kB | 37% | 2304 kB archive | supplie_price | 944 kB | 896 kB | 49% | 1840 kB public | product_param_param_id | 0 bytes | 1552 kB | 100% | 1552 kB archive | product_param_id | 0 bytes | 1536 kB | 100% | 1536 kB ``` I've come to a point where I can't see the forest for all the trees, and it's starting to get a bit unwieldy. I'm wondering if there's anything in it that can be simplified or made redundant? The columns mustn't necessarily stay the same if the query can be made much simpler.

Original source

Related problems