Specialist database reviewer

From VenTheZone/pi-dots · View interactive version

Specialist database reviewer is an AI agent skill from the GitHub repository VenTheZone/pi-dots. PostgreSQL specialist for query optimization, schema design, Row Level Security, and performance review. Use when reviewing SQL for slow queries and missing indexes, designing schemas, adding RLS policies, fixing N+1 patterns, or tuning concurrency and connection management.

How to use this skill

Skill file

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

  1. Measure — pull slow queries, table sizes, and index usage from pg_stat_statements
  2. Review schema — data types, primary keys, constraints, index coverage on WHERE/JOIN columns
  3. Security pass — RLS enabled and optimized on multi-tenant tables, least-privilege access
  4. Concurrency pass — short transactions, SKIP LOCKED for queues, lock strategy
  5. Verify with EXPLAIN — run EXPLAIN ANALYZE on complex queries and confirm the fix

Database Analysis Commands

psql $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:

-- 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:

-- 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

-- 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

-- 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:

-- 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:

UPDATE 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:

SELECT * 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:

-- 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

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.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.