You are on page 1of 3

8/16/2017 Disk Usage - PostgreSQL wiki

Disk Usage
From PostgreSQL wiki

Languages:
English

Finding the size of various object in your database


table size, database size (http://andreas.scherbaum.la/blog/archives/282-table-size,-database-size.html)

General Table Size Information

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

Finding the largest databases in your cluster

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

Finding the size of your biggest relations Nothing

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;

Example output (from a database created with pgbench, scale=25):

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

Finding the total size of your biggest tables


This version of the query uses pg_total_relation_size (http://www.postgresql.org/docs/current/static/functions-
admin.html#FUNCTIONS-ADMIN-DBSIZE) , which sums total disk space used by the table including indexes
and toasted data rather than breaking out the individual pieces:

SELECT nspname || '.' || relname AS "relation",


pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size(C.oid) DESC
LIMIT 20;

Sizes before 8.1


https://wiki.postgresql.org/wiki/Disk_Usage 2/3
8/16/2017 Disk Usage - PostgreSQL wiki

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.

Sizes in 8.4 and later


In 8.4, pg_relation_size was changed to use the regclass type, which means that pg_relation_size(data_type_name)
no longer works.

Easy access to these queries


~/.psqlrc tricks: table sizes (https://github.com/datachomp/dotfiles/blob/master/.psqlrc#L53) shows how to make it
easy to run size related queries like this in psql.

Retrieved from "https://wiki.postgresql.org/index.php?title=Disk_Usage&oldid=27942"


Categories: Performance Snippets SQL Administration Performance

This page was last modified on 2 August 2016, at 14:59.

https://wiki.postgresql.org/wiki/Disk_Usage 3/3

You might also like