Last active
May 16, 2024 18:41
-
-
Save jfrost/6382059 to your computer and use it in GitHub Desktop.
PostgreSQL Duplicate indexes check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;