PostgreSQL – How to view the Size of a Table
To get the size of a specific table in PostgreSQL, you can use the pg_total_relation_size
function. This function returns the total size of a table in bytes, including the main data file, indexes, and associated objects. Below is the syntax:
SELECT pg_size_pretty(pg_total_relation_size('your_table_name'));
where 'your_table_name'
with the name of the table you want to get the size for. When you execute this query, it will return the size of the table in a human-readable format, making it easier to understand.
SELECT pg_size_pretty(pg_total_relation_size('my_table'));
This query will return the size of the “my_table” table in a human-readable format like “123 MB” or “2 GB.”
This query provides an estimate of the total size of the table, including its indexes and related objects. If you want to get more detailed information about the size of individual components (e.g., the table itself, indexes), you can use other PostgreSQL functions and queries like pg_relation_size
and pg_indexes_size
.