]> begriffs open source - ai-pg/blob - full-docs/txt/diskusage.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / diskusage.txt
1
2 27.6. Monitoring Disk Usage #
3
4    27.6.1. Determining Disk Usage
5    27.6.2. Disk Full Failure
6
7    This section discusses how to monitor the disk usage of a PostgreSQL
8    database system.
9
10 27.6.1. Determining Disk Usage #
11
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.
21
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.
27
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
31 er';
32
33  pg_relation_filepath | relpages
34 ----------------------+----------
35  base/16384/16806     |       60
36 (1 row)
37
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
41    file directly.
42
43    To show the space used by TOAST tables, use a query like the following:
44 SELECT relname, relpages
45 FROM pg_class,
46      (SELECT reltoastrelid
47       FROM pg_class
48       WHERE relname = 'customer') AS ss
49 WHERE oid = ss.reltoastrelid OR
50       oid = (SELECT indexrelid
51              FROM pg_index
52              WHERE indrelid = ss.reltoastrelid)
53 ORDER BY relname;
54
55        relname        | relpages
56 ----------------------+----------
57  pg_toast_16806       |        0
58  pg_toast_16806_index |        1
59
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
65       c2.oid = i.indexrelid
66 ORDER BY c2.relname;
67
68       relname      | relpages
69 -------------------+----------
70  customer_id_index |       26
71
72    It is easy to find your largest tables and indexes using this
73    information:
74 SELECT relname, relpages
75 FROM pg_class
76 ORDER BY relpages DESC;
77
78        relname        | relpages
79 ----------------------+----------
80  bigtable             |     3290
81  customer             |     3144
82
83 27.6.2. Disk Full Failure #
84
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.
90
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
94    about that.
95
96 Tip
97
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.
100
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.