This repository was archived by the owner on Jul 6, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +21
-13
lines changed Expand file tree Collapse file tree 3 files changed +21
-13
lines changed Original file line number Diff line number Diff line change @@ -38,14 +38,17 @@ with fk_indexes as (
38
38
'CREATE INDEX',
39
39
'CREATE INDEX CONCURRENTLY'
40
40
) as revert_code,
41
- case when fi.index_name is not null then true else false end as supports_fk
41
+ (
42
+ select count(1)
43
+ from fk_indexes fi
44
+ where
45
+ fi.fk_table_ref = pct.relname
46
+ and fi.opclasses like (array_to_string(pidx.indclass, ', ') || '%')
47
+ ) > 0 as supports_fk
42
48
from pg_index pidx
43
49
join pg_class as pci on pci.oid = pidx.indexrelid
44
50
join pg_class as pct on pct.oid = pidx.indrelid
45
51
left join pg_namespace pn on pn.oid = pct.relnamespace
46
- left join fk_indexes fi on
47
- fi.fk_table_ref = pct.relname
48
- and fi.opclasses like (array_to_string(pidx.indclass, ', ') || '%')
49
52
where pidx.indisvalid = false
50
53
), data_total as (
51
54
select
Original file line number Diff line number Diff line change @@ -85,12 +85,14 @@ with fk_indexes as (
85
85
formated_table_name,
86
86
formated_relation_name,
87
87
i.opclasses,
88
- case when fi.index_name is not null then true else false end as supports_fk
88
+ (
89
+ select count(1)
90
+ from fk_indexes fi
91
+ where fi.fk_table_ref = i.table_name
92
+ and fi.schema_name = i.schema_name
93
+ and fi.opclasses like (i.opclasses || '%')
94
+ ) > 0 as supports_fk
89
95
from indexes i
90
- left join fk_indexes fi on
91
- fi.fk_table_ref = i.table_name
92
- and fi.schema_name = i.schema_name
93
- and fi.opclasses like (i.opclasses || '%')
94
96
join table_scans ts on ts.relid = i.indrelid
95
97
),
96
98
-- Never used indexes
Original file line number Diff line number Diff line change @@ -83,11 +83,14 @@ index_data as (
83
83
), redundant_indexes_fk as (
84
84
select
85
85
ri.*,
86
- case when fi.index_name is not null then true else false end as supports_fk
86
+ (
87
+ select count(1)
88
+ from fk_indexes fi
89
+ where
90
+ fi.fk_table_ref = ri.table_name
91
+ and fi.opclasses like (ri.opclasses || '%')
92
+ ) > 0 as supports_fk
87
93
from redundant_indexes ri
88
- left join fk_indexes fi on
89
- fi.fk_table_ref = ri.table_name
90
- and fi.opclasses like (ri.opclasses || '%')
91
94
),
92
95
-- Cut recursive links
93
96
redundant_indexes_tmp_num as (
You can’t perform that action at this time.
0 commit comments