2004-09-09 03:35:33 +08:00
|
|
|
This module contains several functions that report the size of a given
|
|
|
|
database object:
|
2002-02-23 07:05:35 +08:00
|
|
|
|
2004-09-09 03:35:33 +08:00
|
|
|
int8 database_size(name)
|
|
|
|
int8 relation_size(text)
|
2002-02-23 07:05:35 +08:00
|
|
|
|
2004-09-09 03:35:33 +08:00
|
|
|
int8 pg_database_size(oid)
|
|
|
|
int8 pg_relation_size(oid)
|
2004-09-29 03:35:43 +08:00
|
|
|
int8 pg_tablespace_size(oid)
|
2002-02-23 07:05:35 +08:00
|
|
|
|
2004-09-09 03:35:33 +08:00
|
|
|
text pg_size_pretty(int8)
|
|
|
|
|
|
|
|
The first two functions:
|
|
|
|
|
|
|
|
SELECT database_size('template1');
|
|
|
|
SELECT relation_size('pg_class');
|
|
|
|
|
2004-09-29 03:35:43 +08:00
|
|
|
take the name of the object (possibly schema-qualified, for relation_size),
|
|
|
|
while these functions take object OIDs:
|
2004-09-09 03:35:33 +08:00
|
|
|
|
|
|
|
SELECT pg_database_size(1); -- template1 database
|
|
|
|
SELECT pg_relation_size(1259); -- pg_class table size
|
2004-09-29 03:35:43 +08:00
|
|
|
SELECT pg_tablespace_size(1663); -- pg_default tablespace
|
2004-09-09 03:35:33 +08:00
|
|
|
|
2004-09-29 03:35:43 +08:00
|
|
|
Please note that relation_size and pg_relation_size report only the size of
|
|
|
|
the selected relation itself; any subsidiary indexes or toast tables are not
|
|
|
|
counted. To obtain the total size of a table including all helper files
|
|
|
|
you'd have to do something like:
|
|
|
|
|
|
|
|
SELECT *,
|
|
|
|
pg_size_pretty(tablesize+indexsize+toastsize+toastindexsize) AS totalsize
|
|
|
|
FROM
|
|
|
|
(SELECT pg_relation_size(cl.oid) AS tablesize,
|
|
|
|
COALESCE((SELECT SUM(pg_relation_size(indexrelid))::bigint
|
|
|
|
FROM pg_index WHERE cl.oid=indrelid), 0) AS indexsize,
|
|
|
|
CASE WHEN reltoastrelid=0 THEN 0
|
|
|
|
ELSE pg_relation_size(reltoastrelid)
|
|
|
|
END AS toastsize,
|
|
|
|
CASE WHEN reltoastrelid=0 THEN 0
|
|
|
|
ELSE pg_relation_size((SELECT reltoastidxid FROM pg_class ct
|
|
|
|
WHERE ct.oid = cl.reltoastrelid))
|
|
|
|
END AS toastindexsize
|
|
|
|
FROM pg_class cl
|
|
|
|
WHERE relname = 'foo') ss;
|
2004-09-09 03:35:33 +08:00
|
|
|
|
|
|
|
This sample query utilizes the helper function pg_size_pretty(int8),
|
|
|
|
which formats the number of bytes into a convenient string using KB, MB,
|
|
|
|
GB. It is also contained in this module.
|
|
|
|
|
2004-09-29 03:35:43 +08:00
|
|
|
To install, just run make; make install. Then load the functions
|
2004-09-09 03:35:33 +08:00
|
|
|
into any database using dbsize.sql.
|