Understanding PostgreSQL Index Performance Statistics
Check table statistics to see index usage
Note: Query helps identify whether tables are using indexes effectively or relying mostly on sequential scans.
SELECT relname, seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, CASE WHEN idx_scan > seq_scan THEN 'INDEX OPTIMIZED' WHEN idx_scan > 0 THEN 'MIXED ACCESS' ELSE 'NEEDS INDEXES' END as performance_statusFROM pg_stat_all_tablesWHERE schemaname = 'schemaname'ORDER BY seq_scan + idx_scan DESC;
| Column | Meaning |
|---|---|
relname | Table name |
seq_scan | Number of sequential (full table) scans |
seq_tup_read | Rows read by sequential scans |
idx_scan | Number of index scans |
idx_tup_fetch | Rows fetched using indexes |
performance_status | Custom status based on scan usage |
Note: A high seq_scan is not always bad. PostgreSQL may intentionally choose a sequential scan when:
Statistics indicate an index would be slower.
The table is small.
A query returns most rows.
2. Find Tables Mostly Using Sequential Scans
SELECT relname, seq_scan, idx_scan, n_live_tupFROM pg_stat_user_tablesWHERE seq_scan > idx_scanORDER BY seq_scan DESC;