eugene 0.8.3

Careful with That Lock, Eugene
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use itertools::Itertools;
use pg_query::protobuf::ConstrType;

use crate::hint_data::{hint_url, HintId, StaticHintData};
use crate::lints::ast::{AlterTableAction, Constraint};
use crate::lints::{LintContext, StatementSummary};
use crate::output::output_format::Hint;

pub struct LintRule {
    meta: &'static StaticHintData,
    check: fn(LintContext) -> Option<String>,
}

impl HintId for LintRule {
    fn id(&self) -> &str {
        self.meta.id
    }
}

impl LintRule {
    pub fn id(&self) -> &'static str {
        self.meta.id
    }
    pub fn name(&self) -> &'static str {
        self.meta.name
    }
    pub fn workaround(&self) -> &'static str {
        self.meta.workaround
    }
    pub fn effect(&self) -> &'static str {
        self.meta.effect
    }
    pub fn condition(&self) -> &'static str {
        self.meta.condition
    }
    pub fn check(&self, stmt: LintContext) -> Option<Hint> {
        (self.check)(stmt).map(|help| Hint {
            id: self.id().to_string(),
            name: self.name().to_string(),
            effect: self.effect().to_string(),
            workaround: self.workaround().to_string(),
            condition: self.condition().to_string(),
            help,
            url: hint_url(self.id()),
        })
    }
}

/// Emit a warning if a statement takes a lock that is visible to other transactions without a timeout
pub fn locktimeout_warning(stmt: LintContext) -> Option<String> {
    let target = stmt
        .locks_visible_outside_tx()
        .into_iter()
        .find(|(schema, name)| stmt.takes_lock(schema, name));
    match target {
        Some((schema, name)) if !stmt.has_lock_timeout() => Some(format!(
            "Statement takes lock on `{}.{}`, but does not set a lock timeout",
            if schema.is_empty() { "public" } else { schema },
            name
        )),
        _ => None,
    }
}

pub const LOCKTIMEOUT_WARNING: LintRule = LintRule {
    meta: &crate::hint_data::TOOK_DANGEROUS_LOCK_WITHOUT_TIMEOUT,
    check: locktimeout_warning,
};

fn create_index_nonconcurrently(stmt: LintContext) -> Option<String> {
    match stmt.statement {
        StatementSummary::CreateIndex {
            schema,
            idxname,
            target,
            concurrently: false,
            ..
        } if stmt.is_visible(schema, target) => {
            let schema = if schema.is_empty() { "public" } else { schema };
            Some(format!(
                "Statement takes `ShareLock` on `{schema}.{target}`, blocking \
             writes while creating index `{schema}.{idxname}`"
            ))
        }
        _ => None,
    }
}

/// `CREATE INDEX` without `CONCURRENTLY`
pub const CREATE_INDEX_NONCONCURRENTLY: LintRule = LintRule {
    meta: &crate::hint_data::NEW_INDEX_ON_EXISTING_TABLE_IS_NONCONCURRENT,
    check: create_index_nonconcurrently,
};

fn adding_valid_constraint(stmt: LintContext) -> Option<String> {
    fn is_valid_constraint(alter_table_cmd: &AlterTableAction) -> bool {
        alter_table_cmd.adds_constraint_like(|cons| {
            matches!(
                cons,
                Constraint {
                    valid: true,
                    contype: ConstrType::ConstrCheck
                        | ConstrType::ConstrNotnull
                        | ConstrType::ConstrForeign
                }
            )
        })
    }
    match stmt.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
            ..
        } if stmt.is_visible(schema, name) => {
            let schema = if schema.is_empty() { "public" } else { schema };
            let new_constraint = actions.iter().find(|cmd| is_valid_constraint(cmd));
            let table = name;
            match new_constraint {
                Some(AlterTableAction::AddConstraint {
                    name,
                    constraint_type: _,
                    ..
                }) => {
                    let name = if name.is_empty() {
                        String::new()
                    } else {
                        format!("`{name}` ")
                    };

                    Some(format!(
                        "Statement takes `AccessExclusiveLock` on `{schema}.{table}`, \
                blocking reads until constraint {name}is validated"
                    ))
                }
                Some(AlterTableAction::AddColumn { column, .. }) =>
                    Some(format!("Statement takes `AccessExclusiveLock` on `{schema}.{table}` until new constraint on column `{column}` is validated.")),
                _ => None,
            }
        }
        _ => None,
    }
}
/// Adding a constraint without using `NOT VALID`
pub const ADDING_VALID_CONSTRAINT: LintRule = LintRule {
    meta: &crate::hint_data::VALIDATE_CONSTRAINT_WITH_LOCK,
    check: adding_valid_constraint,
};

fn adding_exclusion_constraint(stmt: LintContext) -> Option<String> {
    match stmt.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
            ..
        } if stmt.is_visible(schema, name) => {
            let new_constraint = actions.iter().find(|cmd| {
                cmd.adds_constraint_like(|cons| cons.contype == ConstrType::ConstrExclusion)
            });
            let table = name;
            let schema = if schema.is_empty() { "public" } else { schema };
            match new_constraint {
                Some(AlterTableAction::AddConstraint {
                         name,
                         constraint_type: _,
                         ..
                     }) => {
                    Some(format!("Statement takes `AccessExclusiveLock` on `{schema}.{table}`, blocking reads and writes until constraint `{name}` is validated and has created index"))
                }
                Some(AlterTableAction::AddColumn { column, .. }) => {
                    Some(format!("Statement takes `AccessExclusiveLock` on `{schema}.{table}`, blocking reads and writes until exclusion constraint on column `{column}` is validated and has created index"))
                }
                _ => None,
            }
        }
        _ => None,
    }
}

/// Adding a new exclusion constraint
pub const ADDING_EXCLUSION_CONSTRAINT: LintRule = LintRule {
    meta: &crate::hint_data::NEW_EXCLUSION_CONSTRAINT_FOUND,
    check: adding_exclusion_constraint,
};

fn add_new_unique_constraint_without_using_index(stmt: LintContext) -> Option<String> {
    match stmt.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
            ..
        } if stmt.is_visible(schema, name) => {
            let schema = if schema.is_empty() { "public" } else { schema };
            let table = name;

            if let Some(AlterTableAction::AddConstraint {
                name,
                use_index: false,
                ..
            }) = actions.iter().find(|cmd| {
                matches!(
                    cmd,
                    AlterTableAction::AddConstraint {
                        constraint_type: ConstrType::ConstrUnique | ConstrType::ConstrPrimary,
                        ..
                    }
                )
            }) {
                Some(format!(
                    "New constraint {name} creates implicit index on `{schema}.{table}`, \
                blocking writes until index is created and validated"
                ))
            } else {
                None
            }
        }
        _ => None,
    }
}

/// Letting `add constraint ... unique` create an index using a `ShareLock`
pub const ADD_NEW_UNIQUE_CONSTRAINT_WITHOUT_USING_INDEX: LintRule = LintRule {
    meta: &crate::hint_data::NEW_UNIQUE_CONSTRAINT_CREATED_INDEX,
    check: add_new_unique_constraint_without_using_index,
};

fn run_more_statements_after_taking_access_exclusive(stmt: LintContext) -> Option<String> {
    if stmt.holding_access_exclusive() {
        Some("Running more statements after taking `AccessExclusiveLock`".to_string())
    } else {
        None
    }
}

pub const RUNNING_STATEMENT_WHILE_HOLDING_ACCESS_EXCLUSIVE: LintRule = LintRule {
    meta: &crate::hint_data::RUNNING_STATEMENT_WHILE_HOLDING_ACCESS_EXCLUSIVE,
    check: run_more_statements_after_taking_access_exclusive,
};

fn sets_column_to_not_null(stmt: LintContext) -> Option<String> {
    match stmt.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
            ..
        } if stmt.is_visible(schema, name) => {
            let schema = if schema.is_empty() { "public" } else { schema };
            let table = name;
            actions
                .iter()
                .filter_map(|cmd| match cmd {
                    AlterTableAction::SetNotNull { column } => Some(column),
                    _ => None,
                })
                .map(|col_name| {
                    format!(
                        "Statement takes `AccessExclusiveLock` on `{schema}.{table}` by setting \
                     `{col_name}` to `NOT NULL` blocking reads until all rows are validated"
                    )
                })
                .next()
        }
        _ => None,
    }
}

pub const MAKE_COLUMN_NOT_NULLABLE_WITH_LOCK: LintRule = LintRule {
    meta: &crate::hint_data::MAKE_COLUMN_NOT_NULLABLE_WITH_LOCK,
    check: sets_column_to_not_null,
};

fn sets_column_type_to_json(stmt: LintContext) -> Option<String> {
    match stmt.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
        } => {
            let added_json = actions
                .iter()
                .filter_map(|cmd| match cmd {
                    AlterTableAction::SetType { type_name, column }
                    | AlterTableAction::AddColumn {
                        type_name, column, ..
                    } if type_name == "json" || type_name == "pg_catalog.json" => Some(column),
                    _ => None,
                })
                .next();
            added_json.map(|column| format!(
                    "Set type of column `{column}` to `json` in `{schema}.{name}`. \
                    The `json` type does not support equality and should not be used, use `jsonb` instead"))
        }
        StatementSummary::CreateTable { columns, .. } => {
            let added_json = columns
                .iter()
                .filter_map(|column| {
                    if column.type_name == "json" || column.type_name == "pg_catalog.json" {
                        Some(&column.name)
                    } else {
                        None
                    }
                })
                .next();
            added_json.map(|column| format!(
                    "Created column `{column}` with type `json`. \
                    The `json` type does not support equality and should not be used, use `jsonb` instead"))
        }
        _ => None,
    }
}

pub const SET_COLUMN_TYPE_TO_JSON: LintRule = LintRule {
    meta: &crate::hint_data::ADD_JSON_COLUMN,
    check: sets_column_type_to_json,
};

fn changes_type_of_column_in_visible_object(stmt: LintContext) -> Option<String> {
    match stmt.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
        } if stmt.is_visible(schema, name) => {
            let changed_column = actions
                .iter()
                .filter_map(|cmd| match cmd {
                    AlterTableAction::SetType { column, type_name } => Some((column, type_name)),
                    _ => None,
                })
                .next();
            changed_column.map(|(column, type_name)| {
                format!(
                    "Changed type of column `{column}` to `{type_name}` in `{schema}.{name}`. \
                    This operation requires a full table rewrite with `AccessExclusiveLock` if `{type_name}` is not binary compatible with \
                    the previous type of `{column}`. Prefer adding a new column with the new type, then dropping/renaming."
                )
            })
        }
        _ => None,
    }
}

pub const CHANGE_COLUMN_TYPE: LintRule = LintRule {
    meta: &crate::hint_data::TYPE_CHANGE_REQUIRES_TABLE_REWRITE,
    check: changes_type_of_column_in_visible_object,
};

pub fn added_serial_column(stmt: LintContext) -> Option<String> {
    let serials = ["bigserial", "serial"];
    match stmt.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
        } if stmt.is_visible(schema, name) => {
            let added_serial = actions
                .iter()
                .filter_map(|cmd| match cmd {
                    AlterTableAction::AddColumn {
                        type_name,
                        column,
                        stored_generated: generated_always,
                        ..
                    } if *generated_always || serials.contains(&type_name.as_str()) => Some(column),
                    _ => None,
                })
                .next();
            added_serial.map(|column| {
                format!(
                    "Added column `{column}` with type that will force table rewrite  in `{schema}.{name}`. \
                    `serial` types and `GENERATED ALWAYS as ... STORED` columns require a full table rewrite with `AccessExclusiveLock`"
                )
            })
        }
        _ => None,
    }
}

pub const ADD_SERIAL_COLUMN: LintRule = LintRule {
    meta: &crate::hint_data::ADDED_SERIAL_OR_STORED_GENERATED_COLUMN,
    check: added_serial_column,
};

pub fn multiple_alter_table_with_same_target(ctx: LintContext) -> Option<String> {
    match ctx.statement {
        StatementSummary::AlterTable { schema, name, .. }
            if ctx.is_visible(schema, name) && ctx.has_altered_table(schema, name) =>
        {
            let schema = if schema.is_empty() { "public" } else { schema };
            Some(format!(
                "Multiple `ALTER TABLE` statements on `{schema}.{name}`. \
                    Combine them into a single statement to avoid scanning the table multiple times."
            ))
        }
        _ => None,
    }
}

pub const MULTIPLE_ALTER_TABLES_WHERE_ONE_WILL_DO: LintRule = LintRule {
    meta: &crate::hint_data::MULTIPLE_ALTER_TABLES_WHERE_ONE_WILL_DO,
    check: multiple_alter_table_with_same_target,
};

pub fn creating_enum(ctx: LintContext) -> Option<String> {
    match ctx.statement {
        StatementSummary::CreateEnum { name, .. } => Some(format!(
            "Created enum `{name}`. \
                Enumerated types are not recommended for use in new applications. \
                Consider using a foreign key to a lookup table instead."
        )),
        _ => None,
    }
}
pub const CREATING_ENUM: LintRule = LintRule {
    meta: &crate::hint_data::CREATING_ENUM,
    check: creating_enum,
};

fn add_primary_key_constraint_using_index(ctx: LintContext) -> Option<String> {
    match ctx.statement {
        StatementSummary::AlterTable {
            schema,
            name,
            actions,
            ..
        } if ctx.is_visible(schema, name) => {
            let schema = if schema.is_empty() { "public" } else { schema };
            let table = name;
            actions.iter().filter_map(|cmd| {
                if let AlterTableAction::AddConstraint {
                    constraint_type: ConstrType::ConstrPrimary,
                    use_index: true,
                    ..
                } = cmd
                {
                    Some(format!(
                        "New primary key constraint using index on `{schema}.{table}`, \
                    may cause postgres to `SET NOT NULL` on columns in the index. \
                    This lint may be a false positive if the columns are already `NOT NULL`, ignore it \
                    by commenting the statement with `-- eugene: ignore {}`", ADD_PRIMARY_KEY_USING_INDEX.id()
                    ))
                } else {
                    None
                }
            }).next()
        }
        _ => None,
    }
}
pub const ADD_PRIMARY_KEY_USING_INDEX: LintRule = LintRule {
    meta: &crate::hint_data::ADD_PRIMARY_KEY_USING_INDEX,
    check: add_primary_key_constraint_using_index,
};
const RULES: &[LintRule] = &[
    ADDING_VALID_CONSTRAINT,
    MAKE_COLUMN_NOT_NULLABLE_WITH_LOCK,
    SET_COLUMN_TYPE_TO_JSON,
    RUNNING_STATEMENT_WHILE_HOLDING_ACCESS_EXCLUSIVE,
    CHANGE_COLUMN_TYPE,
    CREATE_INDEX_NONCONCURRENTLY,
    ADD_NEW_UNIQUE_CONSTRAINT_WITHOUT_USING_INDEX,
    ADDING_EXCLUSION_CONSTRAINT,
    LOCKTIMEOUT_WARNING,
    ADD_SERIAL_COLUMN,
    MULTIPLE_ALTER_TABLES_WHERE_ONE_WILL_DO,
    CREATING_ENUM,
    ADD_PRIMARY_KEY_USING_INDEX,
];

/// Get all available lint rules
pub fn all_rules() -> impl Iterator<Item = &'static LintRule> {
    RULES.iter().sorted_by_key(|rule| rule.id())
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_all_are_in_static_data() {
        super::all_rules().for_each(|rule| {
            assert!(crate::hint_data::ALL
                .iter()
                .any(|hint| hint.id == rule.id()));
        });
    }
}