Rule Architecture
All pgvet rules implement theRule interface defined in rule/rule.go:
- Has a unique kebab-case name (e.g.,
select-star,not-in-subquery) - Provides a human-readable description
- Analyzes parsed PostgreSQL AST nodes via
pg_query_go - Returns zero or more diagnostics with file location and severity
Diagnostic Structure
When a rule finds an issue, it returns aDiagnostic:
Severity Levels
pgvet uses two severity levels:Warning
Indicates a code smell or best practice violation that’s likely unintentional but not always wrong:select-star- UsingSELECT *makes code fragilelimit-without-order- Non-deterministic results (exceptLIMIT 1)update-without-where- Accidentally updating all rowsdelete-without-where- Accidentally deleting all rowsinsert-without-columns- Fragile column order dependencyban-char-type-char(n)pads with spacestimestamp-without-timezone- Loses timezone contextorder-by-ordinal- Fragile positional referencesgroup-by-ordinal- Fragile positional referenceslike-starts-with-wildcard- Prevents index usageoffset-without-limit- Returns all remaining rowsfor-update-no-skip- Can cause lock contentiondistinct-on-order- Non-deterministic row selection
Error
Indicates code that is almost certainly incorrect:not-in-subquery-NOT IN (SELECT ...)broken when subquery returns NULLsnull-comparison-= NULLor<> NULLalways yields NULLmulti-statement- Multiple statements in one block lose CTE scope
Default vs Opt-In Rules
Default Rules
These rules run automatically when you runpgvet:
Opt-In Rules
These rules must be explicitly enabled because they may not apply to all codebases:All Rules (Including Opt-In)
To get all rules programmatically (both default and opt-in):--rules is specified to search the full rule pool.
Enabling and Disabling Rules
Exclude Specific Rules
Use--exclude to skip rules:
Enable Opt-In Rules
Use--rules to explicitly enable extra rules:
Run Only Specific Rules
You can run only the rules you specify:How Rules Work
pgvet uses pg_query_go, which wraps libpg_query (PostgreSQL’s own parser) to:- Parse SQL into an Abstract Syntax Tree (AST)
- Walk the AST nodes to find problematic patterns
- Extract exact line and column positions for diagnostics
Exit Codes
- 0 - No issues found
- 1 - Warnings or errors found
- 2 - Fatal error (parse failure, file not found, etc.)
Next Steps
Default Rules
Explore all 15 rules that run by default
Opt-In Rules
Learn about rules that require explicit enablement