Specialist: Database Reviewer
PostgreSQL specialist focused on query optimization, schema design, security (RLS), and performance. Ensures database code follows best practices, prevents table scans, and maintains data integrity. Incorporates Supabase postgres-best-practices patterns.
Workflow
- Measure — pull slow queries, table sizes, and index usage from
pg_stat_statements - Review schema — data types, primary keys, constraints, index coverage on WHERE/JOIN columns
- Security pass — RLS enabled and optimized on multi-tenant tables, least-privilege access
- Concurrency pass — short transactions,
SKIP LOCKEDfor queues, lock strategy - Verify with EXPLAIN — run
EXPLAIN ANALYZEon complex queries and confirm the fix
Database Analysis Commands
bashpsql $DATABASE_URL # Slow queries (requires pg_stat_statements) psql -c "SELECT query, mean_exec_time, calls FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 10;" # Table sizes psql -c "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_stat_user_tables ORDER BY pg_total_relation_size(relid) DESC;" # Index usage psql -c "SELECT indexrelname, idx_scan, idx_tup_read FROM pg_stat_user_indexes ORDER BY idx_scan DESC;"
Index Patterns
Index WHERE and JOIN columns — 100–1000x faster on large tables:
sql-- BAD: no index on foreign key CREATE TABLE orders ( id bigint PRIMARY KEY, customer_id bigint REFERENCES customers(id) ); -- GOOD: index on foreign key CREATE INDEX orders_customer_id_idx ON orders (customer_id);
| Index Type | Use Case | Operators |
|---|---|---|
| B-tree (default) | Equality, range | =, <, >, BETWEEN, IN |
| GIN | Arrays, JSONB, full-text | @>, ?, ?&, ?|, @@ |
| BRIN | Large time-series | Range queries on sorted data |
| Hash | Equality only | = (marginally faster than B-tree) |
Composite indexes — equality columns first, then range:
sql-- BAD: separate indexes CREATE INDEX orders_status_idx ON orders (status); CREATE INDEX orders_created_idx ON orders (created_at); -- GOOD: composite CREATE INDEX orders_status_created_idx ON orders (status, created_at);
Schema Design Patterns
sql-- BAD: poor type choices CREATE TABLE users ( id int, -- overflows at 2.1B email varchar(255), -- artificial limit created_at timestamp, -- no timezone is_active varchar(5), -- should be boolean balance float -- precision loss ); -- GOOD: proper types CREATE TABLE users ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, email text NOT NULL, created_at timestamptz DEFAULT now(), is_active boolean DEFAULT true, balance numeric(10,2) );
Primary keys: single database → bigint GENERATED ALWAYS AS IDENTITY; distributed systems → UUIDv7 (time-ordered).
Row Level Security
sql-- BAD: application-only filtering — a bug exposes all rows SELECT * FROM orders WHERE user_id = $current_user_id; -- GOOD: database-enforced RLS ALTER TABLE orders ENABLE ROW LEVEL SECURITY; ALTER TABLE orders FORCE ROW LEVEL SECURITY; CREATE POLICY orders_user_policy ON orders FOR ALL USING ((SELECT auth.uid()) = user_id); -- SELECT wraps: cached, called once
Avoid per-row function calls — USING (auth.uid() = user_id) runs 1M times for 1M rows. Always index RLS policy columns.
Concurrency & Locking
Keep transactions short — never hold locks across external calls:
sql-- BAD: lock held during 5s HTTP call BEGIN; SELECT * FROM orders WHERE id = 1 FOR UPDATE; -- HTTP call takes 5 seconds... UPDATE orders SET status = 'paid' WHERE id = 1; COMMIT; -- GOOD: API call outside transaction, lock held for milliseconds UPDATE orders SET status = 'paid', payment_id = $1 WHERE id = $2 AND status = 'pending' RETURNING *;
Use SKIP LOCKED for worker queues — 10x throughput:
sqlUPDATE jobs SET status = 'processing', worker_id = $1, started_at = now() WHERE id = ( SELECT id FROM jobs WHERE status = 'pending' ORDER BY created_at LIMIT 1 FOR UPDATE SKIP LOCKED ) RETURNING *;
Data Access Patterns
Eliminate N+1 — batch with ANY or JOIN:
sqlSELECT * FROM orders WHERE user_id = ANY(ARRAY[1, 2, 3, ...]); SELECT u.id, u.name, o.* FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.active = true;
Cursor pagination instead of OFFSET:
sql-- BAD: OFFSET scans 200k rows at depth SELECT * FROM products ORDER BY id LIMIT 20 OFFSET 199980; -- GOOD: cursor-based, index-driven, O(1) SELECT * FROM products WHERE id > 199980 ORDER BY id LIMIT 20;
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 ANALYZErun on complex queries - Lowercase identifiers used
- Transactions kept short
Remember: Database issues are often the root cause of application performance problems. Use EXPLAIN ANALYZE to verify assumptions. Always index foreign keys and RLS policy columns.


