1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::analysis::mutations::Mutation;
use crate::analysis::state::{AnalysisState, CascadeResult, MutationResult};
use crate::engine::config::Config;
use crate::model::relation::Persistence;
use crate::report::violations::{ObjectKind, OperationKind, Violation, ViolationTier};
use crate::rules::Rule;
pub struct ConcurrentIndexRule;
impl Rule for ConcurrentIndexRule {
fn id(&self) -> &'static str {
"require-concurrent-index"
}
fn default_tier(&self) -> ViolationTier {
ViolationTier::Tier1
}
fn recipe(&self) -> &'static str {
"Index operations block writes (or both reads and writes) when executed synchronously. Add the CONCURRENTLY keyword."
}
fn evaluate(
&self,
mutation: &Mutation,
result: &MutationResult,
pre_state: &crate::analysis::state::PreState,
state: &AnalysisState,
config: &Config,
_cascade: Option<&CascadeResult>,
) -> Vec<Violation> {
if *result == MutationResult::Skipped {
// An index that is present in the pre-state still incurs the
// synchronous DROP INDEX risk even when V6 metadata is too
// incomplete to mutate it exactly (for example, eligibility for
// a backing constraint is not serialized). A truly absent,
// guarded drop remains a no-op and is correctly suppressed.
let known_drop_target = matches!(mutation, Mutation::DropIndex(drop)
if drop.ids.iter().any(|id| pre_state.indexes.iter().any(|edge| edge.dependent == *id)));
if !known_drop_target {
return vec![];
}
}
let mut violations = Vec::new();
match mutation {
Mutation::CreateIndex(create) if !create.concurrently => {
let (is_temp, is_stale, rows, tx_depth) =
match pre_state.relations.get(&create.table) {
Some(rel) => {
let stale =
rel.is_stale() && state.baseline_relations.contains(&create.table);
(
rel.persistence == Persistence::Temporary,
stale,
rel.estimated_rows.unwrap_or(config.default_rows),
rel.created_at_tx_depth,
)
}
None => (false, true, config.default_rows, 0),
};
if is_temp || (tx_depth > 0 && tx_depth <= state.local.transactions.len()) {
return violations;
}
if is_stale {
let key = format!("{}_stale_{}", self.id(), create.table);
violations.push(Violation { source_range: None,
rule_id: self.id(),
operation_kind: OperationKind::CreateIndex,
object_kind: ObjectKind::Index,
object_name: create.id.to_string(),
tier: ViolationTier::Tier2,
reason: format!("Table {} statistics are stale. Lock evaluations may be inaccurate.", create.table),
recipe: "Run ANALYZE to ensure accurate row estimates before structural changes.",
dedup_key: Some(key),
sql: None,
fk_dependency_related: false,
});
}
let tier1_threshold = config.rule_tier1_threshold(self.id());
let tier2_threshold = config.rule_tier2_threshold(self.id());
let tier = if rows >= tier1_threshold {
ViolationTier::Tier1
} else if rows >= tier2_threshold {
ViolationTier::Tier2
} else {
ViolationTier::Tier3
};
let mut reason = format!("Synchronous index creation on {}", create.table);
if is_stale {
reason.push_str(" [WARNING: Based on offline/stale statistics]");
}
violations.push(Violation {
source_range: None,
rule_id: self.id(),
operation_kind: OperationKind::CreateIndex,
object_kind: ObjectKind::Index,
object_name: create.id.to_string(),
tier,
reason,
recipe: self.recipe(),
dedup_key: None,
sql: None,
fk_dependency_related: false,
});
}
Mutation::DropIndex(drop) if !drop.concurrently => {
let rule_id = "require-concurrent-drop-index";
let tier1_threshold = config.rule_tier1_threshold(self.id());
let tier2_threshold = config.rule_tier2_threshold(self.id());
// DROP INDEX classification does not emit a stale-statistics finding.
for id in &drop.ids {
if pre_state.relations.is_empty() {
let rows = config.default_rows;
let tier = if rows >= tier1_threshold {
ViolationTier::Tier1
} else if rows >= tier2_threshold {
ViolationTier::Tier2
} else {
ViolationTier::Tier3
};
violations.push(Violation {
source_range: None,
rule_id,
operation_kind: OperationKind::DropIndex,
object_kind: ObjectKind::Index,
object_name: id.to_string(),
tier,
reason: format!("Synchronous index drop for {}", id),
recipe: self.recipe(),
dedup_key: None,
sql: None,
fk_dependency_related: false,
});
} else {
let target_relations = pre_state
.indexes
.iter()
.filter_map(|idx| {
(idx.dependent == *id)
.then(|| pre_state.relations.get(&idx.referenced))
.flatten()
})
.collect::<Vec<_>>();
if target_relations.is_empty() {
let rows = config.default_rows;
let tier = if rows >= tier1_threshold {
ViolationTier::Tier1
} else if rows >= tier2_threshold {
ViolationTier::Tier2
} else {
ViolationTier::Tier3
};
violations.push(Violation {
source_range: None,
rule_id,
operation_kind: OperationKind::DropIndex,
object_kind: ObjectKind::Index,
object_name: id.to_string(),
tier,
reason: format!("Synchronous index drop for {}", id),
recipe: self.recipe(),
dedup_key: None,
sql: None,
fk_dependency_related: false,
});
}
for rel in target_relations {
if rel.persistence == Persistence::Temporary {
continue;
}
let rows = rel.estimated_rows.unwrap_or(config.default_rows);
let tier = if rows >= tier1_threshold {
ViolationTier::Tier1
} else if rows >= tier2_threshold {
ViolationTier::Tier2
} else {
ViolationTier::Tier3
};
let reason = format!("Synchronous index drop for {} on {}", id, rel.id);
violations.push(Violation {
source_range: None,
rule_id,
operation_kind: OperationKind::DropIndex,
object_kind: ObjectKind::Index,
object_name: id.to_string(),
tier,
reason,
recipe: self.recipe(),
dedup_key: None,
sql: None,
fk_dependency_related: false,
});
}
}
}
}
_ => {}
}
violations
}
}