Skip to content

Instantly share code, notes, and snippets.

@hayesr
Created January 23, 2019 03:06
Show Gist options
  • Save hayesr/ba437bc0ee44910dc616a375ae5a8fb1 to your computer and use it in GitHub Desktop.
Save hayesr/ba437bc0ee44910dc616a375ae5a8fb1 to your computer and use it in GitHub Desktop.
A tip for finding tables that need indexes

Finding which tables need extra indexes

pg_stat_user_tables is a fantastic system view that can show us invaluable information about the tables in our databases and can be used in diagnosing performance issues. Vacuum statistics like last_autovacuum and n_dead_tup will show if there are tables that aren’t being vacuumed efficiently and n_tup_hot_upd if you aren't performing HOT updates.

One of the things to look for first, though, is if there are tables that need new indexes as this can have a significant impact on your database's overall performance:

SELECT relname,
    seq_scan,
    seq_tup_read,
    idx_scan
FROM pg_stat_user_tables
WHERE seq_scan > 0
ORDER BY 3 DESC LIMIT 10;

This simple query will help highlight tables that are heavy on sequential scans and potential candidates for adding an index.

By looking at the seq_scan results, you can see precisely how many sequential scans have happened on a table. Then looking at seq_tup_read you can see how many tuples are being read during those scans. If those numbers are significantly higher on certain tables, some further indexes may be badly needed.

Taken from Postgres Weekly Tip of the Week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment