Nour Sofanati
Developer, SimpleBackups
November 8, 2024
When your database becomes large, you'll want to dig further and see what tables is using space and identify if how to optimize this.\
This post introduces a SQL query that provides insights into the size of tables within your PostgreSQL database, helping you make informed choices about your database structure.
The query below provides an overview of the largest tables in a PostgreSQL database, arranged from largest to smallest. It reveals the total size of each table, including all associated indexes and any additional data objects.
SELECT
schemaname AS schema_name,
relname AS table_name,
pg_size_pretty(pg_total_relation_size(relid)) AS table_size
FROM
pg_stat_user_tables
ORDER BY
pg_total_relation_size(relid) DESC;
Let's dissect the components to understand what each part contributes to the output:
pg_stat_user_tables: A system view that provides statistics specifically for user-created tables, helping us filter out system tables.
schemaname: Identifies the schema that contains each table, useful for distinguishing tables across different namespaces in your database.
relname: The name of the table.
pg_total_relation_size(): A function that calculates the total size of a table, inclusive of:
pg_size_pretty(): Converts the byte count into a human-readable format, displaying results in KB, MB, or GB as applicable.
The query produces the following columns:
Here’s a sample of what you might see when running this query:
schema_name | table_name | table_size
-------------+-------------------+-------------
public | customer_data | 2945 MB
public | orders | 892 MB
analytics | daily_metrics | 154 MB
In this example, we can immediately identify the largest table in the database, customer_data
, which occupies almost 3 GB of space. Knowing this allows us to assess whether it requires optimization or partitioning.
Running this query periodically can help keep your database performant and cost-effective. For example:
VACUUM
to free up unused space.Free 7-day trial. No credit card required.
Have a question? Need help getting started?
Get in touch via chat or at [email protected]