Skip to content

Instantly share code, notes, and snippets.

@jfrost
Last active May 16, 2024 18:41
Show Gist options
  • Save jfrost/6382059 to your computer and use it in GitHub Desktop.
Save jfrost/6382059 to your computer and use it in GitHub Desktop.
PostgreSQL Duplicate indexes check
\o /tmp/duplicate-indexes.txt
-- check for exact matches
SELECT indrelid::regclass
, array_agg(indexrelid::regclass)
FROM pg_index
GROUP BY indrelid
, indkey
HAVING COUNT(*) > 1;
-- check for matches on only the first column of the index
-- requires some human eyeballing to verify
SELECT indrelid::regclass
, array_agg(indexrelid::regclass)
FROM pg_index
GROUP BY indrelid
, indkey[0]
HAVING COUNT(*) > 1;
\o
@lospejos
Copy link

The query is not correct for indices having where in their conditions. F.e.

create index if not exists on my_table_my_index_col_3_is_1_idx (col_1, col_2) where col_3 = 1;

and

create index if not exists on my_table_my_index_col_3_is_2_idx (col_1, col_2) where col_3 = 2;

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