dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/postgresql/#11
Script #11
postgresql
Performanceread-onlyMin Version: 9.x

Find Duplicate Indexes in PostgreSQL

Lists indexes defined identically on the same table, columns and predicate. Reduces unnecessary write cost and disk usage.

#duplicate-index#pg_index#cleanup
postgresql · sql
SELECT indrelid::regclass AS table_name,
       array_agg(indexrelid::regclass) AS duplicate_indexes,
       pg_size_pretty(sum(pg_relation_size(indexrelid))) AS total_size
FROM pg_index
GROUP BY indrelid, indkey, indclass, indoption, indexprs::text, indpred::text
HAVING count(*) > 1;
Operational Notes & Remediation Guidance

Remove redundant duplicates using `DROP INDEX CONCURRENTLY`; be cautious with indexes backing foreign key or uniqueness constraints.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 9.x).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.