Professional Documents
Culture Documents
Disk Usage
From PostgreSQL wiki
Languages:
English
This will report size information for all tables, in both raw bytes and "pretty" Performance
form. Snippets
SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
Disk usage
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE Works with PostgreSQL
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM
>=9.0
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes Written in
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes SQL
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r' Depends on
) a
) a; Nothing
Databases to which the user cannot connect are sorted as if they were infinite size. Performance
SELECT d.datname AS Name, pg_catalog.pg_get_userbyid(d.datdba) AS Owner,
Snippets
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname Disk usage
ELSE 'No Access'
END AS SIZE Works with PostgreSQL
FROM pg_catalog.pg_database d
ORDER BY
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') >=8.2
THEN pg_catalog.pg_database_size(d.datname)
ELSE NULL Written in
https://wiki.postgresql.org/wiki/Disk_Usage 1/3
8/16/2017 Disk Usage - PostgreSQL wiki
END DESC -- nulls first SQL
LIMIT 20
Depends on
Relations are objects in the database such as tables and indexes, and this query Performance
shows the size of all the individual parts. Tables which have both regular and Snippets
TOAST (http://www.postgresql.org/docs/current/static/storage-toast.html) pieces
will be broken out into separate components; an example showing how you might Disk usage
include those into the main total is available in the documentation
(http://www.postgresql.org/docs/current/static/disk-usage.html) , and as of Works with PostgreSQL
PostgreSQL 9.0 it's possible to include it automatically by using pg_table_size
here instead of pg_relation_size: >=8.1
Note that all of the queries below this point on this page show you the sizes for Written in
only those objects which are in the database you are currently connected to.
SQL
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
Depends on
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema') Nothing
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 20;
relation | size
------------------------+------------
public.accounts | 326 MB
public.accounts_pkey | 44 MB
public.history | 592 kB
public.tellers_pkey | 16 kB
public.branches_pkey | 16 kB
public.tellers | 16 kB
public.branches | 8192 bytes
The pg_relation_size functions were introduced in PostgreSQL 8.1. In earlier versions, the following query can be
used instead, returning the size in megabytes:
SELECT
relname, (relpages * 8) / 1024 AS size_mb
FROM pg_class ORDER BY relpages DESC LIMIT 20;
You'll need to account for TOAST yourself here. Bear in mind also that relpages is only up-to-date as of the last
VACUUM or ANALYZE on the particular table.
https://wiki.postgresql.org/wiki/Disk_Usage 3/3