diff --git a/sql/indexes_with_null_values.sql b/sql/indexes_with_null_values.sql index 6cff94c..6dabb56 100644 --- a/sql/indexes_with_null_values.sql +++ b/sql/indexes_with_null_values.sql @@ -7,19 +7,21 @@ -- Finds indexes that can contain null values. select - x.indrelid::regclass::text as table_name, - x.indexrelid::regclass::text as index_name, + i.indrelid::regclass::text as table_name, + i.indexrelid::regclass::text as index_name, string_agg(a.attname, ', ') as nullable_fields, /* in fact, there will always be only one column */ - pg_relation_size(x.indexrelid) as index_size + pg_relation_size(i.indexrelid) as index_size from - pg_catalog.pg_index x - inner join pg_catalog.pg_stat_all_indexes psai on psai.indexrelid = x.indexrelid - inner join pg_catalog.pg_attribute a on a.attrelid = x.indrelid and a.attnum = any(x.indkey) + pg_catalog.pg_index i + inner join pg_catalog.pg_class ic on ic.oid = i.indexrelid + inner join pg_catalog.pg_namespace nsp on nsp.oid = ic.relnamespace + inner join pg_catalog.pg_attribute a on a.attrelid = i.indrelid and a.attnum = any(i.indkey) where - not x.indisunique and + not i.indisunique and not a.attnotnull and - psai.schemaname = :schema_name_param::text and - array_position(x.indkey, a.attnum) = 0 and /* only for first segment */ - (x.indpred is null or (position(lower(a.attname) in lower(pg_get_expr(x.indpred, x.indrelid))) = 0)) -group by x.indrelid, x.indexrelid, x.indpred + not ic.relispartition and + nsp.nspname = :schema_name_param::text and + array_position(i.indkey, a.attnum) = 0 and /* only for first segment */ + (i.indpred is null or (position(lower(a.attname) in lower(pg_get_expr(i.indpred, i.indrelid))) = 0)) +group by i.indrelid, i.indexrelid, i.indpred order by table_name, index_name;