go-sql-review
Review Go code that accesses SQL databases through database/sql, sqlx, pgx, sqlc, or GORM for correctness, safety, transactions, resource handling, and performance, and report findings with severity a
SKILL.mdplugins/agent-kit/skills/go-sql-review/SKILL.md
GitHubGo SQL review
Report findings without modifying code unless the user asks for fixes.
1. Understand the data layer
- Identify the driver and library (
database/sqlwithlib/pqorpgx/stdlib, nativepgx, sqlx, sqlc-generated code, GORM), how the pool is created and configured (SetMaxOpenConns), and how transactions are wrapped. - Read the schema or migrations for nullable columns, unique constraints, and indexes, and the repository's error conventions (
ErrNotFound).
2. Check each query
Correctness
UPDATEandDELETEwithoutWHEREaffect every row. Flag them unless intended. GORM v2 refuses globalUpdate/DeletewithErrMissingWhereClause, unlessAllowGlobalUpdate,Where("1 = 1"), or rawExecbypasses it (GORM v1 does not refuse). Struct conditions skip zero-value fields, soWhere(&Order{TenantID: 0})silently drops the tenant filter.QueryRow(...).Scanreturnssql.ErrNoRows(pgx.ErrNoRowswith native pgx): callers must handle it witherrors.Is. Nullable columns needsql.NullString, pointers, orCOALESCE; scanning NULL intostringfails.- GORM
Updates(struct)skips zero values (false,0,""): flag updates that silently drop them and suggestmap[string]anyorSelect. GORMFirstreturnsErrRecordNotFound;Finddoes not. - Pagination needs a deterministic
ORDER BY.
Safety
- Placeholders (
$1,?) are parameterized.fmt.Sprintfor string concatenation into SQL, GORMRaw/Where/Orderwith interpolated strings, and sqlxInmisuse are injection: require placeholders or an allowlist for identifiers. - Check tenant or owner filters on every query that needs them, and that
SELECT *into response structs does not expose sensitive columns.
Resources and transactions
- Every
rowsfromQueryneedsdefer rows.Close()after the error check and arows.Err()check after the loop; otherwise connections leak and iteration errors are lost. - Transactions need
defer tx.Rollback()right afterBeginTx(harmless afterCommit), and every statement inside must usetx, not the pool. Passctx(QueryContext,BeginTx(ctx, ...), GORMWithContext). - Avoid holding a transaction open across slow external calls, and never share a
*sql.Txacross goroutines.
Performance
- Queries in loops (N+1) should become one query with
= ANY($1)(wrap the slice inpq.Arraywith lib/pq; native pgx accepts slices) orIN (?)via sqlx, a join, or GORMPreload. Use batch inserts orCOPYfor many rows. Check filtered and sorted columns are indexed.
3. Verify and report
Confirm each finding by reading the query, the schema, and its callers; run go vet ./... and the tests when available. Report findings ordered by severity (critical, high, medium, low) with file:line, the problem, a concrete scenario, and a fix. If there are no findings, say so and list what was checked.
Examples
- Use: "Review internal/store/orders.go." "Check this repository for SQL mistakes."
- Skip: "Write a migration that adds an index." "Review this TypeScript query." "Review the whole service for goroutine leaks." (agent-kit:go-review)