How to Get Sizes of Database Objects in PostgreSQL
Summary: in this tutorial, you will learn how to get the sizes of database objects including databases, tables, indexes, tablespaces, and values.
Getting PostgreSQL table sizes
To get the size of a specific table, you use the pg_relation_size()
function. For example, you can get the size of the actor
table in the dvdrental
sample database as follows:
The pg_relation_size()
function returns the size of a specific table in bytes:
To make the result more human-readable, you use the pg_size_pretty()
function.
The pg_size_pretty()
function formats a number using bytes, kB, MB, GB, or TB appropriately. For example:
The following is the output in kB
Note that the pg_relation_size()
function returns the size of the table only, not including indexes or additional objects.
To get the total size of a table, you use the pg_total_relation_size()
function. For example, the following statement uses the pg_total_relation_size()
to retrieve the total size of the actor
table:
The following shows the output:
You can use the pg_total_relation_size()
function to find the size of the biggest tables including indexes.
For example, the following query returns the top 5 biggest tables in the dvdrental
database:
Here is the output:
Getting PostgreSQL database sizes
To get the size of the whole database, you use the pg_database_size()
function. For example, the following statement returns the size of the dvdrental
database:
The statement returns the following result:
To get the size of each database in the current database server, you use the following statement:
Output:
Getting PostgreSQL index sizes
To get the total size of all indexes attached to a table, you use the pg_indexes_size()
function.
The pg_indexes_size()
function accepts the OID or table name as the argument and returns the total disk space used by all indexes attached to that table.
For example, to get the total size of all indexes attached to the film
table, you use the following statement:
Here is the output:
Getting PostgreSQL tablespace sizes
To get the size of a tablespace, you use the pg_tablespace_size()
function.
The pg_tablespace_size()
function accepts a tablespace name and returns the size in bytes. For example, the following statement returns the size of the pg_default
tablespace:
Output:
Getting PostgreSQL value sizes
To find how much space is needed to store a specific value, you use the pg_column_size() function, for example:
Output:
Summary
- Use the
pg_size_pretty()
function to format the size. - Use the
pg_relation_size()
function to get the size of a table. - Use the
pg_total_relation_size()
function to get the total size of a table. - Use the
pg_database_size()
function to get the size of a database. - Use the
pg_indexes_size()
function to get the size of an index. - Use the
pg_total_index_size()
function to get the size of all indexes on a table. - Use the
pg_tablespace_size()
function to get the size of a tablespace. - Use the
pg_column_size()
function to obtain the size of a column of a specific type.