@@ -79,22 +79,22 @@ with unused as (
79
79
join pg_stat_user_indexes as s on s .indexrelid = i2 .indexrelid
80
80
where
81
81
not i1 .indisprimary -- index 1 is not primary
82
- and not ( -- skip if index1 is primary or uniq and index2 is primary or unique
82
+ and not ( -- skip if index1 is ( primary or uniq) and is NOT ( primary and uniq)
83
83
(i1 .indisprimary or i1 .indisunique )
84
84
and (not i2 .indisprimary or not i2 .indisunique )
85
85
)
86
86
and am1 .amname = am2 .amname -- same access type
87
87
and (
88
- i2 .columns like (i1 .columns || ' %' ) -- index 2 include all columns from index 1
89
- or i1 .columns = i2 .columns -- index1 and index 2 include same columns
88
+ i2 .columns like (i1 .columns || ' %' ) -- index 2 includes all columns from index 1
89
+ or i1 .columns = i2 .columns -- index1 and index 2 includes same columns
90
90
)
91
91
and (
92
92
i2 .opclasses like (i1 .opclasses || ' %' )
93
93
or i1 .opclasses = i2 .opclasses
94
94
)
95
- -- index expressions is same
95
+ -- index expressions are same
96
96
and pg_get_expr(i1 .indexprs , i1 .indrelid ) is not distinct from pg_get_expr(i2 .indexprs , i2 .indrelid )
97
- -- index predicates is same
97
+ -- index predicates are same
98
98
and pg_get_expr(i1 .indpred , i1 .indrelid ) is not distinct from pg_get_expr(i2 .indpred , i2 .indrelid )
99
99
), together as (
100
100
select reason, table_name, index_name, index_size, index_def, null as main_index_def, indexrelid
@@ -103,12 +103,11 @@ with unused as (
103
103
select reason, table_name, index_name, index_size, index_def, main_index_def, indexrelid
104
104
from redundant
105
105
where index_usage = 0
106
- order by table_name asc , index_name
107
106
), droplines as (
108
107
select format(' DROP INDEX CONCURRENTLY %s; -- %s, %s, table %s' , max (index_name), max (index_size), string_agg(reason, ' , ' ), table_name) as line
109
108
from together t1
110
- group by table_name, indexrelid
111
- order by table_name, indexrelid
109
+ group by table_name, index_name
110
+ order by table_name, index_name
112
111
), createlines as (
113
112
select
114
113
replace(
@@ -117,8 +116,8 @@ with unused as (
117
116
' CREATE INDEX CONCURRENTLY'
118
117
)as line
119
118
from together t2
120
- group by table_name, indexrelid
121
- order by table_name, indexrelid
119
+ group by table_name, index_name
120
+ order by table_name, index_name
122
121
)
123
122
select ' -- Do migration: --' as run_in_separate_transactions
124
123
union all
0 commit comments