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 Tutorial In this PostgreSQL tutorial youâll learn the basic data types(Boolean, char, text, time, int etc.), Querying and Filtering techniques like select, where, in, order by, etc. managing and modifying the tables in PostgreSQL. Weâll cover all the basic to advance concepts of PostgreSQL in this tutorial.
8 min read
PostgreSQL DATEDIFF Function PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ
6 min read
PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w
5 min read
PostgreSQL - Psql commands PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.Here, we highlight some of th
2 min read
Top 50 PostgreSQL Interview Questions and Answers Are you preparing for a PostgreSQL interview? PostgreSQL is a powerful open-source relational database management system (RDBMS) that is well-known for its reliability, scalability, and rich set of features. Itâs a favorite among developers and businesses alike, making it essential to master if we w
15+ min read
PostgreSQL - Create Database Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like
5 min read
How to Dump and Restore PostgreSQL Database? PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
PostgreSQL - SERIAL When working with PostgreSQL, we need to create tables with unique primary keys. PostgreSQL offers a powerful feature known as the SERIAL pseudo-type which simplifies generating auto-incrementing sequences for columns. In this article, weâll learn about the PostgreSQL SERIAL pseudo-type by explain h
5 min read
PostgreSQL - DISTINCT ON expression The DISTINCT ON clause in PostgreSQL allows us to retrieve unique rows based on specific columns by offering more flexibility than the standard DISTINCT clause. DISTINCT ON allow us to specify which row to keep for each unique value based on an ORDER BY clause. This is particularly useful for select
5 min read
PostgreSQL Connection String A connection string is an essential component that enables applications to communicate with databases or other data sources by providing the necessary configuration details. It consolidates critical information such as the server address, database name, user credentials, and additional parameters li
4 min read