Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4f51368

Browse files
committed
Add routines to dbsize to return the index size and total relation size.
Improve documentation.Ed L.
1 parentf553395 commit4f51368

File tree

2 files changed

+201
-28
lines changed

2 files changed

+201
-28
lines changed

‎contrib/dbsize/README.dbsize

Lines changed: 92 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,115 @@
1-
This module contains several functions that report the size of agiven
2-
database object:
1+
This module contains several functions that report theon-disksize of a
2+
givendatabase object in bytes:
33

44
int8 database_size(name)
55
int8 relation_size(text)
6+
int8 indexes_size(text)
7+
int8 total_relation_size(text)
68

79
int8 pg_database_size(oid)
810
int8 pg_relation_size(oid)
911
int8 pg_tablespace_size(oid)
1012

1113
text pg_size_pretty(int8)
1214

13-
The first two functions:
15+
setof record relation_size_components(text)
16+
17+
The first four functions take the name of the object (possibly
18+
schema-qualified for the latter three) and returns the size of the
19+
on-disk files in bytes.
1420

1521
SELECT database_size('template1');
1622
SELECT relation_size('pg_class');
23+
SELECT indexes_size('pg_class');
24+
SELECT total_relation_size('pg_class');
1725

18-
take the name of the object (possibly schema-qualified, for relation_size),
19-
while these functions take object OIDs:
26+
These functions take object OIDs:
2027

2128
SELECT pg_database_size(1); -- template1 database
2229
SELECT pg_relation_size(1259); -- pg_class table size
2330
SELECT pg_tablespace_size(1663); -- pg_default tablespace
2431

32+
The indexes_size() function returns the total size of the indices for a
33+
relation, including any toasted indices.
34+
35+
The total_relation_size() function returns the total size of the relation,
36+
all its indices, and any toasted data.
37+
2538
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;
45-
46-
This sample query utilizes the helper function pg_size_pretty(int8),
47-
which formats the number of bytes into a convenient string using KB, MB,
48-
GB. It is also contained in this module.
39+
the selected relation itself; any related indexes or toast tables are not
40+
counted. To obtain the total size of a table including all indices and
41+
toasted data, use total_relation_size().
42+
43+
The last function, relation_size_components(), returns a set of rows
44+
showing the sizes of the component relations constituting the input
45+
relation.
46+
47+
Examples
48+
========
49+
50+
I've loaded the following table with a little less than 3 MB of data for
51+
illustration:
52+
53+
create table fat ( id serial, data varchar );
54+
create index fat_uidx on fat (id);
55+
create index fat_idx on fat (data);
56+
57+
You can retrieve a rowset containing constituent sizes as follows:
58+
59+
# SELECT relation_size_components('fat');
60+
relation_size_components
61+
----------------------------------------------------
62+
(2088960,65536,2891776,fat,r,59383,59383)
63+
(32768,704512,737280,pg_toast_59383,t,59386,59386)
64+
(0,32768,32768,pg_toast_59383_index,i,59388,59388)
65+
(0,2039808,2039808,fat_idx,i,59389,59389)
66+
(0,49152,49152,fat_uidx,i,59911,59911)
67+
(5 rows)
68+
69+
To see a more readable output of the rowset:
70+
71+
SELECT *
72+
FROM relation_size_components('fat') AS (idxsize BIGINT,
73+
datasize BIGINT,
74+
totalsize BIGINT,
75+
relname NAME,
76+
kind "char",
77+
relid OID,
78+
node OID)
79+
ORDER BY totalsize;
80+
81+
idxsize | datasize | totalsize | relname | kind | relid | node
82+
---------+----------+-----------+----------------------+------+-------+-------
83+
0 | 32768 | 32768 | pg_toast_59383_index | i | 59388 | 59388
84+
0 | 49152 | 49152 | fat_uidx | i | 59911 | 59911
85+
32768 | 704512 | 737280 | pg_toast_59383 | t | 59386 | 59386
86+
0 | 2039808 | 2039808 | fat_idx | i | 59389 | 59389
87+
2088960 | 65536 | 2891776 | fat | r | 59383 | 59383
88+
(5 rows)
89+
90+
To see the sum total size of a relation:
91+
92+
# select total_relation_size('fat');
93+
total_relation_size
94+
-------------------------
95+
2891776
96+
(1 row)
97+
98+
To see just the size of the uncompressed relation data:
99+
100+
# select relation_size('fat');
101+
relation_size
102+
---------------
103+
65536
104+
(1 row)
105+
106+
To see the size of all related indices:
107+
108+
# select indexes_size('fat');
109+
indexes_size
110+
--------------
111+
2088960
112+
(1 row)
49113

50114
To install, just run make; make install. Then load the functions
51115
into any database using dbsize.sql.

‎contrib/dbsize/dbsize.sql.in

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,112 @@ CREATE FUNCTION pg_relation_size(oid) RETURNS bigint
2121
CREATE FUNCTION pg_size_pretty(bigint) RETURNS text
2222
AS 'MODULE_PATHNAME', 'pg_size_pretty'
2323
LANGUAGE C STRICT;
24+
25+
CREATE FUNCTION total_relation_size (text) RETURNS bigint AS '
26+
SELECT pg_relation_size(r.oid)
27+
+ COALESCE(pg_relation_size(t.oid), 0)::bigint
28+
+ COALESCE(pg_relation_size(ti.oid), 0)::bigint
29+
+ COALESCE(SUM(pg_relation_size(i.indexrelid)), 0)::bigint
30+
+ COALESCE(SUM(pg_relation_size(it.oid)), 0)::bigint
31+
+ COALESCE(SUM(pg_relation_size(iti.oid)), 0)::bigint AS bytes
32+
FROM pg_class r
33+
LEFT JOIN pg_class t ON (r.reltoastrelid = t.oid)
34+
LEFT JOIN pg_class ti ON (t.reltoastidxid = ti.oid)
35+
LEFT JOIN pg_index i ON (r.oid = i.indrelid)
36+
LEFT JOIN pg_class ir ON (ir.oid = i.indexrelid)
37+
LEFT JOIN pg_class it ON (ir.reltoastrelid = it.oid)
38+
LEFT JOIN pg_class iti ON (it.reltoastidxid = iti.oid)
39+
WHERE r.relname = \$1
40+
GROUP BY r.oid, t.oid, ti.oid
41+
' LANGUAGE SQL;
42+
43+
CREATE FUNCTION indexes_size (text) RETURNS bigint
44+
AS '
45+
SELECT COALESCE(SUM(pg_relation_size(ir.oid)), 0)::bigint
46+
+ COALESCE(SUM(pg_relation_size(it.oid)), 0)::bigint
47+
+ COALESCE(SUM(pg_relation_size(iti.oid)), 0)::bigint AS bytes
48+
FROM pg_class r
49+
LEFT JOIN pg_index i ON (r.oid = i.indrelid)
50+
LEFT JOIN pg_class ir ON (ir.oid = i.indexrelid)
51+
LEFT JOIN pg_class it ON (ir.reltoastrelid = it.oid)
52+
LEFT JOIN pg_class iti ON (it.reltoastidxid = iti.oid)
53+
WHERE r.relname = \$1
54+
' LANGUAGE SQL;
55+
56+
CREATE FUNCTION relation_size_components (text) RETURNS SETOF RECORD
57+
AS '
58+
-- relation size
59+
SELECT indexes_size(r.relname) AS indexes_size,
60+
relation_size(r.relname) AS data_size,
61+
total_relation_size(r.relname) AS total_size,
62+
r.relname, r.relkind, r.oid AS relid, r.relfilenode
63+
FROM pg_class r
64+
WHERE r.relname = \$1
65+
66+
UNION ALL
67+
68+
-- relation toast size
69+
SELECT indexes_size(toast.relname) AS indexes_size,
70+
relation_size(''pg_toast.''||toast.relname) AS data_size,
71+
total_relation_size(toast.relname) AS total_size,
72+
toast.relname, toast.relkind, toast.oid AS relid, toast.relfilenode
73+
FROM pg_class r, pg_class toast
74+
WHERE r.reltoastrelid = toast.oid
75+
AND r.relname = \$1
76+
77+
UNION ALL
78+
79+
-- relation toast index size
80+
SELECT indexes_size(toastidxr.relname) AS indexes_size,
81+
relation_size(''pg_toast.''||toastidxr.relname) AS data_size,
82+
total_relation_size(toastidxr.relname) AS total_size,
83+
toastidxr.relname, toastidxr.relkind,
84+
toastidxr.oid AS relid, toastidxr.relfilenode
85+
FROM pg_class r, pg_index toastidx, pg_class toastidxr
86+
WHERE r.relname = \$1
87+
AND r.reltoastrelid = toastidx.indrelid
88+
AND toastidx.indexrelid = toastidxr.oid
89+
90+
UNION ALL
91+
92+
-- relation indices size
93+
SELECT indexes_size(idxr.relname) AS indexes_size,
94+
relation_size(idxr.relname) AS data_size,
95+
total_relation_size(idxr.relname) AS total_size,
96+
idxr.relname, idxr.relkind, idxr.oid AS relid, idxr.relfilenode
97+
FROM pg_class r, pg_class idxr, pg_index idx
98+
WHERE r.relname = \$1
99+
AND r.oid = idx.indrelid
100+
AND idx.indexrelid = idxr.oid
101+
102+
UNION ALL
103+
104+
-- relation indices toast size
105+
SELECT indexes_size(idxtoastr.relname) AS indexes_size,
106+
relation_size(''pg_toast.''||idxtoastr.relname) AS data_size,
107+
total_relation_size(idxtoastr.relname) AS total_size,
108+
idxtoastr.relname, idxtoastr.relkind, idxtoastr.oid AS relid,
109+
idxtoastr.relfilenode
110+
FROM pg_class r, pg_class idxr, pg_index idx, pg_class idxtoastr
111+
WHERE r.relname = \$1
112+
AND r.oid = idx.indrelid
113+
AND idx.indexrelid = idxr.oid
114+
AND idxr.reltoastrelid = idxtoastr.oid
115+
116+
UNION ALL
117+
118+
-- relation indices toast index size
119+
SELECT indexes_size(idxtoastidxr.relname) AS indexes_size,
120+
relation_size(''pg_toast.''||idxtoastidxr.relname) AS data_size,
121+
total_relation_size(idxtoastidxr.relname) AS total_size,
122+
idxtoastidxr.relname, idxtoastidxr.relkind,
123+
idxtoastidxr.oid AS relid, idxtoastidxr.relfilenode
124+
FROM pg_class r, pg_class idxr, pg_index idx, pg_class idxtoast,
125+
pg_class idxtoastidxr
126+
WHERE r.relname = \$1
127+
AND r.oid = idx.indrelid
128+
AND idx.indexrelid = idxr.oid
129+
AND idxr.reltoastrelid = idxtoast.oid
130+
AND idxtoast.reltoastrelid = idxtoastidxr.oid
131+
' LANGUAGE SQL;
132+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp