@@ -5,8 +5,8 @@ database object:
55int8 relation_size(text)
66
77int8 pg_database_size(oid)
8- int8 pg_tablespace_size(oid)
98int8 pg_relation_size(oid)
9+ int8 pg_tablespace_size(oid)
1010
1111text pg_size_pretty(int8)
1212
@@ -15,40 +15,37 @@ The first two functions:
1515SELECT database_size('template1');
1616SELECT relation_size('pg_class');
1717
18- take the name of the object, and support databases and tables. Please
19- note that relation_size() only reports table file usage and not the
20- space used by indexes and toast tables.
21-
22- Functions using oids are:
18+ take the name of the object (possibly schema-qualified, for relation_size),
19+ while these functions take object OIDs:
2320
2421SELECT pg_database_size(1); -- template1 database
25- SELECT pg_tablespace_size(1663); -- pg_default tablespace
2622SELECT pg_relation_size(1259); -- pg_class table size
23+ SELECT pg_tablespace_size(1663); -- pg_default tablespace
2724
28- pg_relation_size() will report the size of the table, index and toast
29- table OIDs, but they must be requested individually. To obtain the total
30- size of a table including all helper files you'd have to do something
31- like:
32-
33- XXX This query does not work, syntax error XXX
34-
35- SELECT pg_relation_size(cl.oid) AS tablesize,
36- CASE WHEN reltoastrelid=0 THEN 0
37- ELSE pg_relation_size(reltoastrelid) END AS toastsize,
38- SUM(pg_relation_size(indexrelid)) AS indexsize,
39- pg_size_pretty(pg_relation_size(cl.oid)
40- + pg_relation_size(reltoastrelid)
41- + SUM(pg_relation_size(indexrelid))::int8)
42- AS totalsize
43- FROM pg_class cl
44- JOIN pg_index ON cl.oid=indrelid
45- WHERE relname = 'pg_rewrite'
46- GROUP BY 1,2
25+ Please note that relation_size and pg_relation_size report only the size of
26+ the selected relation itself; any subsidiary indexes or toast tables are not
27+ counted. To obtain the total size of a table including all helper files
28+ you'd have to do something like:
29+
30+ SELECT *,
31+ pg_size_pretty(tablesize+indexsize+toastsize+toastindexsize) AS totalsize
32+ FROM
33+ (SELECT pg_relation_size(cl.oid) AS tablesize,
34+ COALESCE((SELECT SUM(pg_relation_size(indexrelid))::bigint
35+ FROM pg_index WHERE cl.oid=indrelid), 0) AS indexsize,
36+ CASE WHEN reltoastrelid=0 THEN 0
37+ ELSE pg_relation_size(reltoastrelid)
38+ END AS toastsize,
39+ CASE WHEN reltoastrelid=0 THEN 0
40+ ELSE pg_relation_size((SELECT reltoastidxid FROM pg_class ct
41+ WHERE ct.oid = cl.reltoastrelid))
42+ END AS toastindexsize
43+ FROM pg_class cl
44+ WHERE relname = 'foo') ss;
4745
4846This sample query utilizes the helper function pg_size_pretty(int8),
4947which formats the number of bytes into a convenient string using KB, MB,
5048GB. It is also contained in this module.
5149
52- To install, just run make; make install.Finally, load the functions
50+ To install, just run make; make install.Then load the functions
5351into any database using dbsize.sql.
54-