PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Administration / How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

Summary: This tutorial shows you how to get PostgreSQL database, table, indexes, tablespace, and value size using various handy functions.

PostgreSQL table size

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:

1
select pg_relation_size('actor');

The pg_relation_size() function returns the size of a specific table in bytes:

1
2
3
pg_relation_size
------------------
            16384

To make the result more human readable, you use the pg_size_pretty()function. The pg_size_pretty() function takes the result of another function and format it using bytes, kB, MB, GB or TB as appropriate. For example:

1
2
SELECT
    pg_size_pretty (pg_relation_size('actor'));

The following is the output in kB

1
2
3
4
pg_size_pretty
    ----------------
     16 kB
    (1 row)

The pg_relation_size() function returns the size of the table only, not included indexes or additional objects.

To get the total size of a table, you use the pg_total_relation_size() function. For example, to get the total size of the actor table, you use the following statement:

1
2
3
4
SELECT
    pg_size_pretty (
        pg_total_relation_size ('actor')
    );

The following shows the output:

1
2
3
4
pg_size_pretty
----------------
72 kB
(1 row)

You can use the pg_total_relation_size() function to find the size of biggest tables including indexes.

For example, the following query returns top 5 biggest tables in the dvdrental database:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT
    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 5;

Here is the output:

1
2
3
4
5
6
7
8
  relation  | total_size
------------+------------
rental     | 2472 kB
payment    | 2232 kB
film       | 688 kB
film_actor | 536 kB
inventory  | 464 kB
(5 rows)

PostgreSQL database size

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:

1
2
3
4
SELECT
    pg_size_pretty (
        pg_database_size ('dvdrental')
    );

The statement returns the following result:

1
2
3
4
pg_size_pretty
----------------
15 MB
(1 row)

To get the size of each database in the current database server, you use the following statement:

1
2
3
4
SELECT
    pg_database.datname,
    pg_size_pretty(pg_database_size(pg_database.datname)) AS size
    FROM pg_database;

1
2
3
4
5
6
    datname     |  size
----------------+---------
postgres       | 7055 kB
template1      | 7055 kB
template0      | 6945 kB
dvdrental      | 15 MB

PostgreSQL indexes size

To get 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 of that table.

For example, to get the total size of all indexes attached to the film table, you use the following statement:

1
2
SELECT
    pg_size_pretty (pg_indexes_size('actor'));

Here is the output:

1
2
3
4
pg_size_pretty
----------------
32 kB
(1 row)

PostgreSQL tablespace size

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.

The following statement returns the size of the pg_default tablespace:

1
2
3
4
SELECT
    pg_size_pretty (
        pg_tablespace_size ('pg_default')
    );

The statement returns the following output:

1
2
3
4
pg_size_pretty
----------------
43 MB
(1 row)

PostgreSQL value size

To find how much space that needs to store a specific value, you use the pg_column_size() function, for examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
dvdrental=# select pg_column_size(5::smallint);
pg_column_size
----------------
              2
(1 row)
 
 
dvdrental=# select pg_column_size(5::int);
pg_column_size
----------------
              4
(1 row)
 
 
dvdrental=# select pg_column_size(5::bigint);
pg_column_size
----------------
              8
(1 row)

In this tutorial, you have learned various handy functions to get the size of a database, a table, indexes, a tablespace, and a value.

Previous Tutorial: PostgreSQL Rename Database: A Quick Guide
Next Tutorial: PostgreSQL Copy Database Made Easy

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.