agentleFS
gurinderugurinderu/craft/skills/sql-review/SKILL.md

sql-review

Skill1 starsChanged 40 days agoMITView on GitHub

SQL code-review rubric for PostgreSQL — migrations (the lock each ALTER form takes, what rewrites a table since PG 11, NOT NULL without a default, CREATE INDEX without CONCURRENTLY, large backfills, rollout order between the old code and the new schema, reversibility, migration-number conflicts between branches) and queries (match with the schema's types and NULLs, joins, transactions and isolation level, an index for a new query, N+1, injection), in .sql files and in query strings embedded in Rust (sqlx, diesel, postgres) or any other code; severity by harm from review-rules. Use when reviewing a migration or a query, asking whether a migration is safe to deploy, or rating a SQL finding. Triggers: review this migration, is this migration safe, ALTER TABLE lock, table rewrite, CREATE INDEX CONCURRENTLY, NOT NULL column, backfill, migration conflict, N+1, sqlx query, isolation level. Not for Rust code beside the query (rust-review) nor for the review workflow itself (craft:review).

  • Deletes or force-pushes

13 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use security-review and code-review.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • SentrySkill

    security-review

    Finds exploitable application security vulnerabilities in code changes. Use for Warden security scans, appsec review, OWASP-style checks, authentication or authorization bugs, injection, XSS, SSRF, path traversal, secrets, unsafe crypto, webhook verification, open redirects, or sensitive data exposure.

    Also in 3 projects that use this

  • GoogleSkill

    code-review

    Adversarial systems, concurrency, memory-model, and architecture code review protocol. Use this skill whenever asked to review code, PRs, diffs, or branches in Filament.

    Also in 4 projects that use this

The file

We split this skill into the rules it gives an agent

1 rule. 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 11 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    CHECK, VALIDATE and SET NOT NULL sequence) in one migration the tool runs as one transaction — the first step's lock is held through the …

    line 68

“Common” is measured: kept by at least 6 in 10 of the 11 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

SQL Review

The rubric for reviewing SQL: schema migrations and queries, against PostgreSQL semantics unless the repository names another engine. Severity, finding classes and sweeps come from review-rules; this skill keeps what is specific to SQL. Each item below has a catalog ID in rules.md.

The review entry point is the craft:review workflow. It sends every changed .sql file to the SQL profile, whose two lenses — sql-migrations and sql-queries — always run, on the host's default agent. A Rust or other source file whose changed lines hold a query string (a sqlx::query, query!, query_as!, sql_query call, or a string literal opening with SELECT, INSERT INTO, UPDATE … SET, DELETE FROM or WITH) also gets the sql-queries lens in its own profile's review. There is no mechanical SQL gate: the verdict rests on the lenses and their verifiers.

Facts the checklist rests on

Lock levels (PostgreSQL docs, "Explicit Locking" and ALTER TABLE):

StatementLock on the tableWork done while the lock is held
most ALTER TABLE forms (ADD/DROP COLUMN, ALTER TYPE, SET/DROP DEFAULT, SET/DROP NOT NULL, ADD CHECK, ADD UNIQUE/PRIMARY KEY, RENAME)ACCESS EXCLUSIVE — blocks reads and writesany scan or rewrite the form does
ADD FOREIGN KEYSHARE ROW EXCLUSIVE on both tablesthe validating scan, unless NOT VALID
VALIDATE CONSTRAINTSHARE UPDATE EXCLUSIVE — reads and writes go onthe scan
CREATE INDEXSHARE — blocks writesthe whole build
CREATE INDEX CONCURRENTLYSHARE UPDATE EXCLUSIVEthe build; it cannot run in a transaction block, nor on a partitioned table's parent; a failed build leaves an INVALID index to drop
DROP INDEX, DROP TABLE, TRUNCATEACCESS EXCLUSIVE— (DROP INDEX CONCURRENTLY avoids it)

A table lock is held until the end of the transaction that took it, not the statement. The safe two-step forms — ADD CONSTRAINT … NOT VALID, then VALIDATE CONSTRAINT; CHECK (col IS NOT NULL) NOT VALID, then VALIDATE, then SET NOT NULL; on PG 18+, ADD CONSTRAINT … NOT NULL col NOT VALID, then VALIDATE CONSTRAINT — need the NOT VALID step to commit before VALIDATE runs. Migration tools wrap each file in one transaction by default (sqlx, diesel), so the two in one file keep the NOT VALID step's ACCESS EXCLUSIVE (SHARE ROW EXCLUSIVE on both tables for a foreign key) through the whole validating scan: the safe form is no safer than the plain one. Put the NOT VALID step in its own migration file, or opt the file out of the transaction. VALIDATE and the SET NOT NULL after it may share one transaction: the scan runs under SHARE UPDATE EXCLUSIVE, and the ACCESS EXCLUSIVE SET NOT NULL then takes is brief, its scan skipped. Likewise, every statement after a lock-taking one in the same transaction runs under that lock.

Any lock waits in the queue behind the transactions already holding a conflicting one, and every later query on the table waits behind it: a brief ACCESS EXCLUSIVE behind one long transaction stops the table. A migration on a hot table sets lock_timeout (and retries) for that reason.

What rewrites the table and its indexes:

ChangeRewrite?
ADD COLUMN with no default, or a non-volatile default (a constant, now())no, since PG 11
ADD COLUMN with a volatile default (gen_random_uuid(), clock_timestamp(), a serial or identity), or a STORED generated columnyes
ALTER COLUMN TYPE to a binary-coercible type (varchar(n) → varchar(m), m > n, or → text)no
ALTER COLUMN TYPE otherwise (int → bigint, text → int, varchar narrowed)yes
timestamp ↔ timestamptzno rewrite from PG 12 when the session time zone is UTC; a rewrite otherwise
SET NOT NULLno rewrite, but a full scan under ACCESS EXCLUSIVE — skipped (PG 12+) when a validated CHECK (col IS NOT NULL) exists; on PG 18+ a NOT NULL constraint can instead be added NOT VALID and validated under SHARE UPDATE EXCLUSIVE
DROP COLUMNno (the column is only marked dropped)

Migrations checklist

  • Lock (MIG-001): a statement that holds ACCESS EXCLUSIVE or SHARE through a scan, a rewrite or an index build on a table that has traffic — and the lock lasts to the end of its transaction, so everything after it in the same file runs under it. The safe forms: CREATE INDEX CONCURRENTLY, then ADD CONSTRAINT … USING INDEX; ADD CONSTRAINT … NOT VALID, then VALIDATE CONSTRAINT.
  • Two steps, one transaction (MIG-009): the NOT VALID step and its VALIDATE CONSTRAINT (or the CHECK, VALIDATE and SET NOT NULL sequence) in one migration the tool runs as one transaction — the first step's lock is held through the scan. Must-fix on a large table with traffic.
  • Rewrite (MIG-002): a type change or added column from the rewrite table above, on a large table.
  • NOT NULL (MIG-003): ADD COLUMN … NOT NULL without a DEFAULT fails on a table with rows; SET NOT NULL without the validated CHECK scans under ACCESS EXCLUSIVE. The safe routes: the CHECK trick (CHECK NOT VALID, VALIDATE, SET NOT NULL, drop the CHECK), or on PG 18+ ADD CONSTRAINT … NOT NULL col NOT VALID, then VALIDATE CONSTRAINT — each with its NOT VALID step committed first (MIG-009).
  • Index (MIG-004): CREATE [UNIQUE] INDEX on an existing table without CONCURRENTLY. With it: a migration tool that wraps the file in a transaction (sqlx: opt out with a -- no-transaction first line; diesel: run_in_transaction = false in the migration's metadata.toml); and, opted out, a file with any other statement beside it — several statements sent as one query string run as an implicit transaction block and fail with "cannot run inside a transaction block", so the concurrent build is the file's only statement. On a partitioned table CONCURRENTLY is refused on the parent: build the index concurrently on each partition, then CREATE INDEX … ON ONLY the parent and ALTER INDEX … ATTACH PARTITION each one.
  • Backfill (MIG-005): one UPDATE or INSERT … SELECT over a large table, or a backfill in the same transaction as DDL — long row locks, any DDL lock held to the end, WAL and replica lag. Batch it, outside the schema migration.
  • Rollout order (MIG-006): during a deploy the old code runs against the new schema (and the new code against the old when migrations run after the rollout). A dropped or renamed column or table still used on the base, a new NOT NULL column old INSERTs omit, a type old code decodes, an enum value old code cannot parse. Expand first, migrate the code, contract in a later release. Grep the repository's code in every language for the object.
  • Reversibility (MIG-007): a down migration missing where the repository keeps them, or one that cannot restore what the up removed.
  • Migration numbers (MIG-008): the base's and the trunk's migrations are listed with git ls-tree --name-only <ref> -- <migrations dir>/; a version this diff reuses, one that sorts before the latest on the trunk (refused by tools that forbid out-of-order, never applied by tools that record only the current version), or an edit to a migration the base already holds (a checksum mismatch, or a change no applied database ever sees).

A statement on a table the same branch creates has no lock or rollout harm.

Queries checklist

  • Schema match (QRY-001): read the table's migrations at HEAD; every column exists with the type the code binds and decodes; a nullable column (an outer join's side included) decoded into a non-optional type.
  • NULL and joins (QRY-002): = NULL; NOT IN (subquery) over a NULL; SUM/MAX over no rows returning NULL; a WHERE on a LEFT JOIN's right table; a join on an incomplete key multiplying rows.
  • Transactions (QRY-003): a read-then-write outside one transaction, or at READ COMMITTED (the default) without FOR UPDATE, a conditional UPDATE, a unique constraint or ON CONFLICT; a serialization failure (40001) or deadlock (40P01) never retried; a statement sent on the pool instead of the transaction (&pool for &mut *tx in sqlx); a transaction held across a slow call.
  • Index (QRY-004): a new WHERE/JOIN/ORDER BY on a growing table with no index whose leading columns serve it; a function or cast on the column, or a leading-wildcard LIKE, defeating a b-tree.
  • N+1 (QRY-005): a query per row or per loop item; one query with a JOIN or = ANY($1) instead.
  • Injection (QRY-006): query text built from a value instead of a bind parameter; an identifier not quoted with quote_ident/format('%I').

Severity

By the harm cap of review-rules (severity.md), never above it:

  • must-fix — outage: a table locked or rewritten under traffic (a two-step safe form run in one transaction included), a migration that fails on deploy, old code broken by the new schema; data-loss: data dropped, truncated or corrupted that no durable source restores; security: injection from outside input; intent-unmet: a query that does not do what the change says.
  • should-fix — a missing index or an N+1 whose traffic is not named; a non-reversible down migration where the repository keeps them (a repo-rule when its rule is written down).
  • not a finding — keyword case, formatting, alias or constraint naming, style.

Table size and traffic are premises: cite where they were seen, or say they are unknown.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.