// SQL snippet

Find the largest tables and indexes in PostgreSQL

Top tables by size, split into heap, index and toast. The breakdown tells you what kind of problem you have.

Main statement
PostgreSQL
Также охватывает
SQL Server

The task

A table's size is really three numbers: the heap (the rows), its indexes, and TOAST (large values stored out of line). A table that is 80 percent index has too many or bloated indexes; one that is mostly TOAST is holding large documents; one that is mostly heap with many dead rows needs vacuum attention. pg_total_relation_size minus pg_relation_size gives the index plus TOAST part, and pg_indexes_size separates the two.

The snippet

Written for PostgreSQL. Versions for SQL Server are under Variations.

PostgreSQL
SELECT schemaname, relname AS table_name,
       pg_relation_size(relid) AS heap_bytes,
       pg_indexes_size(relid) AS index_bytes,
       pg_total_relation_size(relid) - pg_relation_size(relid) - pg_indexes_size(relid) AS toast_bytes,
       pg_total_relation_size(relid) AS total_bytes,
       pg_size_pretty(pg_relation_size(relid)) AS heap,
       pg_size_pretty(pg_indexes_size(relid)) AS indexes,
       pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid) - pg_indexes_size(relid)) AS toast,
       pg_size_pretty(pg_total_relation_size(relid)) AS total,
       n_live_tup, n_dead_tup
FROM pg_stat_user_tables
ORDER BY pg_total_relation_size(relid) DESC
LIMIT 25;

Как это работает

pg_relation_size(relid) is the heap's main fork only. pg_indexes_size is all indexes on the table, and pg_total_relation_size adds those, the TOAST table with its index, and the small free-space and visibility map forks, so the toast column also carries those few pages. The raw byte columns sit beside the pretty ones because they sort and diff correctly. n_live_tup and n_dead_tup put the heap figure in context: a large dead count means part of that heap is reusable space rather than data.

Variations

SQL Server: top tables by reserved space

SQL Server
SELECT TOP 25
    OBJECT_SCHEMA_NAME(ps.object_id) AS schema_name,
    OBJECT_NAME(ps.object_id) AS table_name,
    CAST(SUM(ps.reserved_page_count) * 8 / 1024.0 AS DECIMAL(12,1)) AS reserved_mb,
    CAST(SUM(ps.used_page_count) * 8 / 1024.0 AS DECIMAL(12,1)) AS used_mb,
    CAST(SUM(CASE WHEN ps.index_id IN (0, 1) THEN ps.used_page_count ELSE 0 END) * 8 / 1024.0 AS DECIMAL(12,1)) AS data_mb,
    CAST(SUM(CASE WHEN ps.index_id > 1 THEN ps.used_page_count ELSE 0 END) * 8 / 1024.0 AS DECIMAL(12,1)) AS nonclustered_mb
FROM sys.dm_db_partition_stats ps
WHERE OBJECTPROPERTY(ps.object_id, 'IsUserTable') = 1
GROUP BY ps.object_id
ORDER BY reserved_mb DESC;

Include indexes as their own ranked list

SELECT relname AS table_name, indexrelname AS index_name,
       pg_size_pretty(pg_relation_size(indexrelid)) AS size, idx_scan
FROM pg_stat_user_indexes
ORDER BY pg_relation_size(indexrelid) DESC
LIMIT 25;

Questions that come up

My table shrank after DELETE but the size stayed the same. Why?

DELETE only marks rows dead. A plain VACUUM makes their space reusable by later inserts and updates, and gives space back to the operating system only when the empty pages are at the very end of the table. VACUUM FULL rewrites the table compactly but holds an ACCESS EXCLUSIVE lock for the whole rewrite; the pg_repack extension does the same job with only brief locks. SQL Server keeps freed pages allocated to the data file in the same way unless you shrink the file, which fragments indexes and is rarely worth it.

Toast is huge on a table I thought was small. What does that mean?

When a row would exceed about 2 KB, PostgreSQL compresses its largest variable-length values (text, jsonb, bytea) and moves them out of line into the TOAST table until the row fits. A big TOAST part means the table holds large documents. Updating other columns reuses the existing TOAST values, but every change to a large value writes a complete new copy, so a frequently edited jsonb document grows TOAST and WAL quickly. Splitting the large column into its own table, switching to lz4 compression (PostgreSQL 14 and later), or keeping large files in object storage are the usual remedies.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.