Skip to content

Largest tables

Total size including indexes and TOAST. The number to watch over weeks, not once.

PostgreSQL · SQL
SELECT n.nspname || '.' || c.relname AS table_name,       pg_size_pretty(pg_total_relation_size(c.oid)) AS total_size,       pg_total_relation_size(c.oid) AS total_bytes,       pg_size_pretty(pg_relation_size(c.oid)) AS table_size,       pg_size_pretty(pg_indexes_size(c.oid)) AS indexes_size,       c.reltuples::bigint AS estimated_rowsFROM pg_class cJOIN pg_namespace n ON n.oid = c.relnamespaceWHERE c.relkind IN ('r', 'p', 'm')  AND n.nspname NOT IN ('pg_catalog', 'information_schema')ORDER BY pg_total_relation_size(c.oid) DESCLIMIT 20

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What this query shows

Knowing your Postgres table size tells you where the disk goes, which tables to archive or partition and which backups will take the longest. It is also the first number to check when storage grows faster than expected.

The query reads pg_class and lists the 20 largest ordinary tables, partitioned tables and materialized views, outside pg_catalog and information_schema. Sizes come from the size functions of PostgreSQL, so they match what is on disk.

How to read each column

  • table_name: schema and table.
  • total_size and total_bytes: pg_total_relation_size, the table plus its indexes plus TOAST. This is the one the result is sorted by.
  • table_size: pg_relation_size, the main table only. It excludes indexes and TOAST data.
  • indexes_size: pg_indexes_size, every index on the table together.
  • estimated_rows: reltuples, an estimate refreshed by VACUUM, ANALYZE and CREATE INDEX. It is not a count.

Compare indexes_size with table_size. Indexes larger than the table itself are a hint to review which ones you need. A total_size far above table_size plus indexes_size means a lot of TOAST, which holds large values like long text or JSON.

Healthy and worrying numbers

There is no right size, only the one that matches your data. What worries is movement: a table that doubles in a month, or one far bigger than its row count suggests. That second case often means bloat from dead tuples.

Divide total_bytes by estimated_rows for a rough size per row. If it is many times what a row should weigh, check the dead tuples query. Track the same top tables week after week, because the trend matters more than a single reading.

What to do about it

For bloat, a plain VACUUM makes space reusable. To give disk back to the operating system you need a rewrite, which VACUUM FULL does with an exclusive lock.

VACUUM (VERBOSE, ANALYZE) public.events;

For tables that grow forever, such as logs and events, partition them by date and drop old partitions, or archive old rows. For a quick view of the whole database, use:

SELECT pg_size_pretty(pg_database_size(current_database()));

And to see one table in detail, call the size functions directly.

SELECT pg_size_pretty(pg_total_relation_size('public.events'));

Limits of this query

  • estimated_rows is -1 on PostgreSQL 14 and newer for a table that was never vacuumed or analyzed, and can be stale on others.
  • For a partitioned table (relkind p) the parent has no storage, so its size is near zero. Its partitions appear as their own rows.
  • Only the connected database is covered, and tables in the schemas pg_catalog and information_schema are left out.
  • Sizes are the on-disk size of the relation, not the size of the data after compression or a dump.

Run it on a schedule in DataRunner

Connect the database to DataRunner, paste this query or start from the template, approve the SQL, then pick a schedule and a channel. Weekly, always at the same hour, gives you a series to watch growth by. Each run executes the query on your database and delivers the result as CSV, Excel, PDF or inline HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.

Every run is kept in the run history, so you can set today's result next to last week's. A schedule carries its own time zone, so a Monday at 8:00 is your Monday.

If the database sits in a private network, the bridge agent opens the connection from the inside out, so you do not need to open an inbound port.

Frequently asked questions

How do I check table size in Postgres?
Use pg_total_relation_size for the table with its indexes and TOAST, or pg_relation_size for the table alone, and wrap it in pg_size_pretty for a readable value. This template lists the 20 largest tables with all of them side by side.
How do I check database size in PostgreSQL?
Run SELECT pg_size_pretty(pg_database_size(current_database())). It returns the size of the connected database on disk. The query on this page then tells you which tables make up that total.
What is the difference between pg_relation_size and pg_total_relation_size?
pg_relation_size returns the size of the main data of the table. pg_total_relation_size adds its indexes and TOAST. For capacity planning the total is the number you want.
Why is my table bigger than its data?
Usually dead tuples that VACUUM has not reclaimed, or space freed inside the file that was not returned to the operating system. Large values stored in TOAST and indexes also count toward the total. Check the dead tuples query to tell which one it is.