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

Commitd9b68c8

Browse files
committed
Code review for recent dbsize changes. Fix some thinkos, enforce coding
style and message style standards, improve documentation.
1 parent70c8fcb commitd9b68c8

File tree

3 files changed

+99
-95
lines changed

3 files changed

+99
-95
lines changed

‎contrib/dbsize/README.dbsize

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ database object:
55
int8 relation_size(text)
66

77
int8 pg_database_size(oid)
8-
int8 pg_tablespace_size(oid)
98
int8 pg_relation_size(oid)
9+
int8 pg_tablespace_size(oid)
1010

1111
text pg_size_pretty(int8)
1212

@@ -15,40 +15,37 @@ The first two functions:
1515
SELECT database_size('template1');
1616
SELECT 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

2421
SELECT pg_database_size(1); -- template1 database
25-
SELECT pg_tablespace_size(1663); -- pg_default tablespace
2622
SELECT 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

4846
This sample query utilizes the helper function pg_size_pretty(int8),
4947
which formats the number of bytes into a convenient string using KB, MB,
5048
GB. 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
5351
into any database using dbsize.sql.
54-

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp