Overview
The database-reviewer agent is an expert PostgreSQL database specialist focused on query optimization, schema design, security, and performance. It incorporates patterns from Supabase’s postgres-best-practices.Agent identifier
Uses Claude Sonnet for comprehensive database review
Available tools:
Read, Write, Edit, Bash, Grep, GlobWhen to Use
Writing SQL queries or migrations
Designing database schemas
Optimizing slow queries
Implementing Row Level Security (RLS)
Troubleshooting database performance
The database-reviewer agent activates PROACTIVELY when writing SQL, creating migrations, designing schemas, or troubleshooting database performance.
Core Responsibilities
- Query Performance — Optimize queries, add proper indexes, prevent table scans
- Schema Design — Design efficient schemas with proper data types and constraints
- Security & RLS — Implement Row Level Security, least privilege access
- Connection Management — Configure pooling, timeouts, limits
- Concurrency — Prevent deadlocks, optimize locking strategies
- Monitoring — Set up query analysis and performance tracking
Diagnostic Commands
Review Workflow
1. Query Performance (CRITICAL)
- Are WHERE/JOIN columns indexed?
- Run
EXPLAIN ANALYZEon complex queries — check for Seq Scans on large tables - Watch for N+1 query patterns
- Verify composite index column order (equality first, then range)
2. Schema Design (HIGH)
- Use proper types:
bigintfor IDs,textfor strings,timestamptzfor timestamps,numericfor money,booleanfor flags - Define constraints: PK, FK with
ON DELETE,NOT NULL,CHECK - Use
lowercase_snake_caseidentifiers (no quoted mixed-case)
3. Security (CRITICAL)
- RLS enabled on multi-tenant tables with
(SELECT auth.uid())pattern - RLS policy columns indexed
- Least privilege access — no
GRANT ALLto application users - Public schema permissions revoked
Key Principles
Index Foreign Keys
Always, no exceptions
Use Partial Indexes
WHERE deleted_at IS NULL for soft deletesCovering Indexes
INCLUDE (col) to avoid table lookupsSKIP LOCKED for Queues
10x throughput for worker patterns
Cursor Pagination
WHERE id > $last instead of OFFSETBatch Inserts
Multi-row
INSERT or COPY, never loopsShort Transactions
Never hold locks during external API calls
Consistent Lock Ordering
ORDER BY id FOR UPDATE to prevent deadlocksAnti-Patterns to Flag
SELECT *in production codeintfor IDs (usebigint),varchar(255)without reason (usetext)timestampwithout timezone (usetimestamptz)- Random UUIDs as PKs (use UUIDv7 or IDENTITY)
- OFFSET pagination on large tables
- Unparameterized queries (SQL injection risk)
GRANT ALLto application users- RLS policies calling functions per-row (not wrapped in
SELECT)
Example: Proper Schema Design
Example: Query Optimization
Example: Row Level Security (RLS)
Example: Pagination
Example: Avoiding Deadlocks
Example: Worker Queue Pattern
Review Checklist
- All WHERE/JOIN columns indexed
- Composite indexes in correct column order
- Proper data types (bigint, text, timestamptz, numeric)
- RLS enabled on multi-tenant tables
- RLS policies use
(SELECT auth.uid())pattern - Foreign keys have indexes
- No N+1 query patterns
- EXPLAIN ANALYZE run on complex queries
- Transactions kept short
Usage Example
Success Criteria
All queries indexed properly
No N+1 query patterns
RLS policies performant
Schema uses proper data types
No security vulnerabilities
Transactions are short
Patterns adapted from Supabase Agent Skills under MIT license.