Find duplicate indexes in PostgreSQL
Two identical indexes do the same maintenance work twice on every write. This query finds the pairs nobody meant to create.
The task
Duplicate indexes usually arrive by accident: an ORM migration adds an index someone already created by hand, or a UNIQUE constraint and a plain index end up on the same column. Every write maintains both, and the planner only ever needs one of them. The catch is that two indexes on the same column are not always duplicates: a partial index (WHERE status = 'paid'), an expression index (lower(email)) or a different operator class covers different queries.
The snippet
Written for PostgreSQL.
SELECT n.nspname AS schema_name, t.relname AS table_name,
a.relname AS index_a, b.relname AS index_b,
x.indisunique AS a_unique, y.indisunique AS b_unique,
pg_get_indexdef(a.oid) AS definition_a,
pg_get_indexdef(b.oid) AS definition_b,
pg_size_pretty(pg_relation_size(a.oid) + pg_relation_size(b.oid)) AS combined_size
FROM pg_index x
JOIN pg_index y ON y.indrelid = x.indrelid
AND y.indexrelid > x.indexrelid
AND y.indnkeyatts = x.indnkeyatts
AND y.indkey::text = x.indkey::text
AND y.indclass::text = x.indclass::text
AND y.indcollation::text = x.indcollation::text
AND y.indoption::text = x.indoption::text
AND pg_get_expr(y.indexprs, y.indrelid) IS NOT DISTINCT FROM pg_get_expr(x.indexprs, x.indrelid)
AND pg_get_expr(y.indpred, y.indrelid) IS NOT DISTINCT FROM pg_get_expr(x.indpred, x.indrelid)
JOIN pg_class a ON a.oid = x.indexrelid
JOIN pg_class b ON b.oid = y.indexrelid
JOIN pg_class t ON t.oid = x.indrelid
JOIN pg_namespace n ON n.oid = t.relnamespace
ORDER BY pg_relation_size(a.oid) + pg_relation_size(b.oid) DESC;
仕組み
The query self-joins pg_index on the same table. indkey is the list of indexed column numbers (0 marks an expression), indnkeyatts separates key columns from INCLUDE columns, indclass and indcollation hold the operator class and collation per column, and indoption holds ASC/DESC and NULLS ordering. indkey and the others are vector types, so they are compared as text. Expressions and partial-index predicates are stored as node trees, so pg_get_expr decompiles them and IS NOT DISTINCT FROM treats two NULLs (no expression, no predicate) as equal. y.indexrelid > x.indexrelid lists each pair once. The unique flags are shown because a UNIQUE index and a plain index on the same column count as duplicates, but only the plain one should go. The sort is on the byte sum; sorting the pg_size_pretty text would put 9 MB above 10 GB.
Variations
Generate DROP statements, keeping the unique or constraint-backed index
WITH pairs AS (
SELECT x.indexrelid AS a, y.indexrelid AS b,
x.indisunique AS a_unique, y.indisunique AS b_unique,
EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = x.indexrelid) AS a_con,
EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = y.indexrelid) AS b_con
FROM pg_index x
JOIN pg_index y ON y.indrelid = x.indrelid
AND y.indexrelid > x.indexrelid
AND y.indnkeyatts = x.indnkeyatts
AND y.indkey::text = x.indkey::text
AND y.indclass::text = x.indclass::text
AND y.indcollation::text = x.indcollation::text
AND y.indoption::text = x.indoption::text
AND pg_get_expr(y.indexprs, y.indrelid) IS NOT DISTINCT FROM pg_get_expr(x.indexprs, x.indrelid)
AND pg_get_expr(y.indpred, y.indrelid) IS NOT DISTINCT FROM pg_get_expr(x.indpred, x.indrelid)
), pick AS (
SELECT CASE WHEN b_con OR (b_unique AND NOT a_unique) THEN a ELSE b END AS drop_oid,
CASE WHEN b_con OR (b_unique AND NOT a_unique) THEN a_con ELSE b_con END AS drop_con,
CASE WHEN b_con OR (b_unique AND NOT a_unique) THEN a_unique AND NOT b_unique
ELSE b_unique AND NOT a_unique END AS drop_only_unique
FROM pairs
)
SELECT DISTINCT format('DROP INDEX CONCURRENTLY %s;', drop_oid::regclass) AS drop_statement
FROM pick
WHERE NOT drop_con AND NOT drop_only_unique;
Questions that come up
Which index of a pair should I drop?
Keep the one that backs a PRIMARY KEY, UNIQUE or exclusion constraint, because dropping it would mean dropping the constraint (PostgreSQL refuses a plain DROP INDEX on it anyway). If one is UNIQUE and the other is not, keep the UNIQUE one: it serves the same lookups and also enforces the rule. The variant applies both rules and skips pairs where both indexes back constraints.
Two indexes list the same column but one has a WHERE clause. Are they duplicates?
No. A partial index only contains the rows matching its predicate, and an expression index such as lower(email) answers different predicates than an index on email. That is why the query compares the decompiled expressions and predicates, not only the column numbers. A partial index whose predicate is a subset of a full index on the same column can still be worth reviewing, but that is a judgment call, not a duplicate.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.