Full answer
Test data:
sql
CREATE TABLE logs (id bigint PRIMARY KEY, level text, message text);
INSERT INTO logs SELECT g, CASE WHEN g % 10 = 0 THEN 'error' ELSE 'info' END, repeat('m', 100) FROM generate_series(1, 10000) g;
CREATE INDEX logs_level ON logs (level);sql
SELECT pg_size_pretty(pg_relation_size('logs')) AS table_size, pg_size_pretty(pg_indexes_size('logs')) AS indexes, pg_size_pretty(pg_total_relation_size('logs')) AS total;Result on PostgreSQL 17:
text
table_size | indexes | total
------------+---------+---------
1456 kB | 328 kB | 1816 kB
(1 row)sql
SELECT indexrelid::regclass::text AS index_name, pg_size_pretty(pg_relation_size(indexrelid)) AS size FROM pg_index WHERE indrelid = 'logs'::regclass ORDER BY 1;Result:
text
index_name | size
------------+--------
logs_level | 88 kB
logs_pkey | 240 kB
(2 rows)Ten thousand rows with 100-character messages take 1456 kB of table data, and the two indexes add 328 kB. The level index is only 88 kB against 240 kB for the primary key, because B-tree deduplication (PostgreSQL 13+) stores each repeated value once with a list of row pointers, and level has only two values. Such a low-selectivity index rarely helps queries anyway, and pg_stat_user_indexes.idx_scan shows whether it is used at all. Sizes depend on page layout, so they are stable for the same data and version.
sql
CREATE TABLE logs (id bigint PRIMARY KEY, level text, message text);
INSERT INTO logs SELECT g, CASE WHEN g % 10 = 0 THEN 'error' ELSE 'info' END, repeat('m', 100) FROM generate_series(1, 10000) g;
CREATE INDEX logs_level ON logs (level);
SELECT pg_size_pretty(pg_relation_size('logs')) AS table_size, pg_size_pretty(pg_indexes_size('logs')) AS indexes, pg_size_pretty(pg_total_relation_size('logs')) AS total;
SELECT indexrelid::regclass::text AS index_name, pg_size_pretty(pg_relation_size(indexrelid)) AS size FROM pg_index WHERE indrelid = 'logs'::regclass ORDER BY 1;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- pg_relation_size for the table
- pg_indexes_size for indexes
- pg_total_relation_size for everything
- pg_size_pretty for display
- Check idx_scan for unused indexes
Common mistakes
- Indexing low-selectivity columns
- Ignoring index size when planning disk
Follow-ups
- How would you find the largest tables in a database?
- What is TOAST?
Tips
- Compare index and table size