Goal Find composite indexes that do not support any constraint but are on more than three columns.
Notes The query does not count any included columns, which are merely stored and do not participate in the index semantics. According to the PostgreSQL documentation: "Multicolumn indexes should be used sparingly. In most situations, an index on a single column is sufficient and saves space and time. Indexes with more than three columns are unlikely to be helpful unless the usage of the table is extremely stylized."
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability Low (Many false-positive results)
License MIT License
Data Source system catalog only
SQL Query
SELECT pg_get_indexdef(indexrelid) AS indexdef, indnkeyatts AS number_of_columns, n.nspname AS table_schema, c.relname AS table_name
FROM pg_index AS i INNER JOIN pg_class AS c ON i.indrelid=c.oid
INNER JOIN pg_namespace AS n ON n.oid=c.relnamespace
INNER JOIN pg_authid AS a ON n.nspowner=a.oid
WHERE (n.nspname='public' OR a.rolname<>'postgres')
AND indnkeyatts >3
AND NOT EXISTS (SELECT *
FROM pg_constraint
WHERE i.indexrelid=pg_constraint.conindid)
ORDER BY indnkeyatts DESC, table_schema, table_name;

Collections

This query belongs to the following collections:

NameDescription
Find problems automaticallyQueries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
Categories

This query is classified under the following categories:

NameDescription
PerformanceQueries of this category provide information about indexes in a database.

Further reading and related materials:

Reference
https://www.postgresql.org/docs/current/indexes-multicolumn.html