Even from intial reviews of my previous post on expression based indexes I received a lot of questions and feedback around many different parts of indexing in Postgres. Here’s a mixed collection of valuable tips and guides around much of that.
Unused Indexes
In an earlier tweet I joked about some SQL that would generate the SQL to add an index to every column:
# SELECT 'CREATE INDEX idx_'
|| table_name || '_'
|| column_name || ' ON '
|| table_name || ' ("'
|| column_name || '");'
FROM information_schema.columns;
?column?
---------------------------------------------------------------------
CREATE INDEX idx_pg_proc_proname ON pg_proc ("proname");
CREATE INDEX idx_pg_proc_pronamespace ON pg_proc ("pronamespace");
CREATE INDEX idx_pg_proc_proowner ON pg_proc ("proowner");
The reasoning behind this is guessing whether an index will be helpful can be a bit hard within Postgres. So the easy solution is to add indexes to everything, then just observe if they’re being used. Of course you want to add it to all tables/columns because you never know if core of Postgres may be missing some needed ones