PostgreSQL - Size of a Table
Last Updated :
14 Aug, 2024
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.
Similar Reads
PostgreSQL - Size of value
The size of a value in a database table refers to the space required to store that specific value. Accurately gauging the storage requirements for different data types can be crucial for database optimization and performance. In this article, we will explore the 'pg_column_size()' function in Postgr
2 min read
PostgreSQL - Show Tables
In PostgreSQL, viewing tables is an essential task for managing and organizing our database. Although PostgreSQL does not support the SHOW TABLES command like MySQL, it offers alternative commands like \dt in the psql tool, which helps users list all tables within a specific databaseIn this article,
4 min read
PostgreSQL - ALTER TABLE
In PostgreSQL, the ALTER TABLE statement is a powerful and essential tool that allows us to modify the structure of an existing table to meet evolving database needs. With PostgreSQL ALTER TABLE, we can perform various modifications on the table without disrupting the ongoing operations of our datab
6 min read
PostgreSQL - Size of a Database
Efficient database management is essential for ensuring optimal performance in PostgreSQL. One critical aspect of this is monitoring the database size to manage storage and plan for scaling. PostgreSQL offers powerful built-in functions like pg_database_size() to calculate the size of a specific dat
4 min read
PostgreSQL - Size of Indexes
In PostgreSQL, index management is essential for optimizing query performance and ensuring efficient database storage. One important function for assessing the storage requirements of table indexes is the pg_indexes_size() function. In this article, we will explain the pg_indexes_size() function, it
4 min read
PostgreSQL - CREATE TABLE
In PostgreSQL, the CREATE TABLE statement is used to define a new table within a database. It allows us to specify the table's structure, including column names, data types, and constraints, ensuring data integrity and consistency. Understanding the PostgreSQL table creation process is essential for
5 min read
PostgreSQL - CREATE TABLE AS
The CREATE TABLE AS statement in PostgreSQL is a powerful tool used to create a new table and populate it with data returned by a query. This functionality allows you to generate tables on the fly based on query results, which can be very useful for reporting, analysis, and other tasks.Let us better
3 min read
PostgreSQL - TIME Data Type
In PostgreSQL, the TIME data type is essential for applications that require precise time tracking, such as scheduling systems and event logging. This data type allows for accurate time-based entries without storing date information. PostgreSQLâs TIME data type also supports fractional seconds for u
4 min read
PostgreSQL - Select Into
In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword. The select into statement will assign the data returned by th
2 min read
PostgreSQL - SELECT INTO
The PostgreSQL SELECT INTO statement allows users to create a new table directly from the result set of a query. This command is ideal for duplicating or organizing data from an existing table into a new one for further analysis. SELECT INTO does not return data to the client but saves it in a new t
4 min read