Goal Find indexes that are not used by the DBMS. Remember that indexes are not a "free lunch" and they slow down the processes of updating data.
Notes The query excludes indexes that have been created to enforce a constraint.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability Medium (Medium number of false-positive results)
License MIT License
Fixing Suggestion Remove the unused indexes.
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
SELECT schemaname AS table_schema, relname AS table_name, indexrelname AS index_name 
FROM  pg_stat_all_indexes INNER JOIN pg_index USING (indexrelid)
WHERE idx_scan=0
AND schemaname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND indisunique=FALSE
AND indisprimary=FALSE
AND indisexclusion=FALSE
ORDER BY table_schema, table_name, index_name;

SQL statements that help generate fixes for the identified problem.

SQL Query to Generate FixDescription
SELECT format('DROP INDEX %1$I.%2$I;', schemaname, indexrelname) AS statements
FROM  pg_stat_all_indexes INNER JOIN pg_index USING (indexrelid)
WHERE idx_scan=0
AND schemaname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND indisunique=FALSE
AND indisprimary=FALSE
AND indisexclusion=FALSE
ORDER BY schemaname, relname, indexrelname;
Drop the index.
Collections

This query belongs to the following collections:

NameDescription
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Categories

This query is classified under the following categories:

NameDescription
Data at the database physical levelQueries of this category provide information about the disk usage.
PerformanceQueries of this category provide information about indexes in a database.