2 27.6. Monitoring Disk Usage #
4 27.6.1. Determining Disk Usage
5 27.6.2. Disk Full Failure
7 This section discusses how to monitor the disk usage of a PostgreSQL
10 27.6.1. Determining Disk Usage #
12 Each table has a primary heap disk file where most of the data is
13 stored. If the table has any columns with potentially-wide values,
14 there also might be a TOAST file associated with the table, which is
15 used to store values too wide to fit comfortably in the main table (see
16 Section 66.2). There will be one valid index on the TOAST table, if
17 present. There also might be indexes associated with the base table.
18 Each table and index is stored in a separate disk file — possibly more
19 than one file, if the file would exceed one gigabyte. Naming
20 conventions for these files are described in Section 66.1.
22 You can monitor disk space in three ways: using the SQL functions
23 listed in Table 9.102, using the oid2name module, or using manual
24 inspection of the system catalogs. The SQL functions are the easiest to
25 use and are generally recommended. The remainder of this section shows
26 how to do it by inspection of the system catalogs.
28 Using psql on a recently vacuumed or analyzed database, you can issue
29 queries to see the disk usage of any table:
30 SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'custom
33 pg_relation_filepath | relpages
34 ----------------------+----------
38 Each page is typically 8 kilobytes. (Remember, relpages is only updated
39 by VACUUM, ANALYZE, and a few DDL commands such as CREATE INDEX.) The
40 file path name is of interest if you want to examine the table's disk
43 To show the space used by TOAST tables, use a query like the following:
44 SELECT relname, relpages
48 WHERE relname = 'customer') AS ss
49 WHERE oid = ss.reltoastrelid OR
50 oid = (SELECT indexrelid
52 WHERE indrelid = ss.reltoastrelid)
56 ----------------------+----------
58 pg_toast_16806_index | 1
60 You can easily display index sizes, too:
61 SELECT c2.relname, c2.relpages
62 FROM pg_class c, pg_class c2, pg_index i
63 WHERE c.relname = 'customer' AND
64 c.oid = i.indrelid AND
69 -------------------+----------
70 customer_id_index | 26
72 It is easy to find your largest tables and indexes using this
74 SELECT relname, relpages
76 ORDER BY relpages DESC;
79 ----------------------+----------
83 27.6.2. Disk Full Failure #
85 The most important disk monitoring task of a database administrator is
86 to make sure the disk doesn't become full. A filled data disk will not
87 result in data corruption, but it might prevent useful activity from
88 occurring. If the disk holding the WAL files grows full, database
89 server panic and consequent shutdown might occur.
91 If you cannot free up additional space on the disk by deleting other
92 things, you can move some of the database files to other file systems
93 by making use of tablespaces. See Section 22.6 for more information
98 Some file systems perform badly when they are almost full, so do not
99 wait until the disk is completely full to take action.
101 If your system supports per-user disk quotas, then the database will
102 naturally be subject to whatever quota is placed on the user the server
103 runs as. Exceeding the quota will have the same bad effects as running
104 out of disk space entirely.