Open In App

PostgreSQL – Size of a Table

Last Updated : 14 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PostgreSQL provides a variety of functions to help you query the size of your tables. We’ll focus on the ‘pg_relation_size()’ function to get the size of a table and enhance the readability of the output using the ‘pg_size_pretty()’ function. In this article, we will be using a sample database for reference which is described here and can be downloaded from here.

Using ‘pg_relation_size()’ to Get Table Size

The ‘pg_relation_size()’ function is designed to return the size of a specified table in bytes. This function is straightforward to use and provides a quick way to assess the raw size of any table in your database.

Syntax:

SELECT pg_relation_size('table_name');

Example 1: Querying the Size of the “country” Table

Here we will query for the size “country” table from the sample ‘dvdrental’ database using the below command: 

SELECT pg_relation_size('country');

Output: 

To make the result readable, one can use the pg_size_pretty() function. The ‘pg_size_pretty()’ function takes the result of another function and formats it using bytes, kB, MB, GB or TB as required. 

SELECT pg_size_pretty (pg_relation_size('country'));

Output: 
 

The output will show the size of the “country” table in bytes. While this is useful, the result might not be easily readable, especially for larger tables.

Enhancing Readability with ‘pg_size_pretty()’

To make the output more readable, especially when dealing with large tables, PostgreSQL offers the ‘pg_size_pretty()’ function. This function converts the raw byte size into a more human-readable format, such as kB, MB, GB, or TB, depending on the size.

Syntax:

SELECT pg_size_pretty(pg_relation_size('table_name'));

Example 2: Pretty-Printing the Size of the “country” Table

Here we will query for the size “customer” table from the sample dvdrental database using the below command: 

SELECT pg_size_pretty (pg_relation_size('customer'));

Output: 

The output will display the size in a format like “12 kB” or “2 MB,” making it easier to understand.

Example 3: Querying the Size of the “customer” Table

Here we will query for the size “film” table from the ‘sample dvdrental database’ using the below command: 

SELECT pg_size_pretty (pg_relation_size('film'));

Output: 

This command returns the size of the “customer” table in a human-readable format.

Example 4: Querying the Size of the “film” Table

Here we will query for the top 10 biggest tables in the dvdrental database. 

SELECT
    relname AS "tables",
    pg_size_pretty (
        pg_total_relation_size (X .oid)
    ) AS "size"
FROM
    pg_class X
LEFT JOIN pg_namespace Y ON (Y.oid = X .relnamespace)
WHERE
    nspname NOT IN (
        'pg_catalog',
        'information_schema'
    )
AND X .relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY
    pg_total_relation_size (X .oid) ASC
LIMIT 10;

Output: 

This query will list the top 10 largest tables in the dvdrental database, providing a clear view of where most of the storage space is being used.



Next Article
Practice Tags :

Similar Reads