Re: Disk space usage analyzer?

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: Yang Zhang <yanghatespam(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Disk space usage analyzer?
Date: 2011-03-28 16:21:38
Message-ID: 4D90B592.2000307@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 03/25/2011 07:58 PM, Yang Zhang wrote:
> Is there any tool for breaking down how much disk space is used by
> (could be freed by removing) various tables, indexes, selected rows,
> etc.? Thanks!
>

You can use the pg_class table and the pg_relation_size (and optionally
the pg_size_pretty) functions to get that info. This query gives table
sizes and percent of overall usage.

BUT! It is only looking at tables, not indexes. If you want to know how
much space will be freed by dropping a table, you will have to modify
this query to total up the index space used for all the indexes
associated with each table.

select
relname as table,
lpad(pg_size_pretty(pg_relation_size(oid)), 9) as size,
(100*pg_relation_size(oid)/(select sum(pg_relation_size(oid)) from
pg_class where relkind='r'))::numeric(4,1) as percent
from
pg_class
where
relkind = 'r'
order by
relpages desc
;

table | size | percent
----------------------------------+-----------+---------
foobar | 2310 MB | 19.7
foobaz | 2021 MB | 17.2
bazbar | 1642 MB | 14.0
...

Cheers,
Steve

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nicholson, Brad (Toronto, ON, CA) 2011-03-28 16:59:58 Re: Disk space usage analyzer?
Previous Message Tom Lane 2011-03-28 16:18:40 Re: A simpler time zone question