Carefully removing unwanted indexes
Unwanted indexes are unused or underutilized ones that are not worth the performance cost to update and maintain, so we need to consider carefully removing them. Carefully removing? Do I mean pressing Enter gently after typing DROP INDEX
? Err, no!
The reasoning is that it takes a long time to build an index and a short time to drop it.
What we want is a way of removing an index so that if we discover that removing it was a mistake, we can put the index back again quickly.
Getting ready
The following query will list all invalid indexes, if any:
SELECT ir.relname AS indexname
, it.relname AS tablename
, n.nspname AS schemaname
FROM pg_index i
JOIN pg_class ir ON ir.oid = i.indexrelid
JOIN pg_class it ON it.oid = i.indrelid
JOIN pg_namespace n ON n.oid = it.relnamespace
WHERE NOT i.indisvalid;
Take note of these indexes so that you can tell whether a given index is invalid later because we marked it as invalid
during this recipe...