The list of all the queries

Base tables that have more than five indexes

Query goal: Find base tables that have more than five indexes. Indexes can be used to increase the speed of queries (SELECT statements). However, the amount of indexes shouldn't be too large. Otherwise it may reduce the speed of operations that are used to modify data.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: Low (Many false-positive results)
Query license: MIT License
Data source: system catalog only
SQL query: Click on query to copy it

SELECT n.nspname AS schema_name,
c2.relname AS table_name,
Count(*) AS number_of_indexes,
Count(*) FILTER (WHERE i.indisunique=true) AS number_of_unique_indexes
FROM pg_catalog.pg_class c INNER JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
INNER JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
INNER JOIN pg_catalog.pg_authid u ON u.oid = c.relowner
INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('i') AND c2.relkind='r'
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND (n.nspname = 'public' OR u.rolname <> 'postgres')
GROUP BY n.nspname, c2.relname 
HAVING Count(*)>5
ORDER BY Count(*) DESC,  n.nspname, c2.relname;

Categories where the query belongs to

Category nameCategory description
PerformanceQueries of this category provide information about indexes in a database.

The list of all the queries