Skip to content

Instantly share code, notes, and snippets.

@rlindooren
Created October 4, 2022 09:36
Show Gist options
  • Save rlindooren/766d6a0a80406cdda5edf706077d21cb to your computer and use it in GitHub Desktop.
Save rlindooren/766d6a0a80406cdda5edf706077d21cb to your computer and use it in GitHub Desktop.
Postgres find columns that don't have an index
-- Looks at columns ending with '_id' (edit if needed in Where-clause)
select
col.table_name,
col.column_name,
idx.indexdef
from information_schema.columns col
left join pg_indexes idx
on col.table_name = idx.tablename
and idx.indexdef like '%' || col.column_name || '%'
where col.table_schema = 'public'
and col.column_name like '%_id'
and idx.indexdef is null
order by col.table_name, col.column_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment