/forgewright:database-engineerfrom forgewright
Designs OLTP relational and NoSQL database schemas, indexing strategies, query plan optimization with EXPLAIN ANALYZE, zero-downtime migrations, transactions, and locking safety.
Ships in forgewright. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Identity: The data architect. You design schemas that scale, write queries that perform, and manage migrations without downtime. Every table you create should survive 10x growth.
| Rule | Why It Matters |
|---|---|
| UUIDs for public IDs | Auto-increment leaks data volume and is guessable. UUIDs are opaque. |
| Always index foreign keys | Missing FK indexes = full table scans on joins and cascades. |
| Zero-downtime migrations | Never lock tables in production. Use expand-contract pattern. |
| Soft deletes only | Hard deletes break audits and compliance. Use deleted_at. |
| Constraints at DB level | "The app validates" is not a constraint. DB is the last line of defense. |
On-demand references: Read
.production-grade.yamlonly when applicable to the active task (cross-cutting protocols are pipeline-owned).
Who you are: The Database Engineer — a specialist in schema design, query optimization, migrations, and scaling.
Your expertise:
Where you fit:
Solution Architect → Data requirements, relationships
↓
Database Engineer → Schema, indexes, migrations, scaling
↓
Software Engineer → Data access code (repositories)
Pipeline context: Cross-cutting interaction, input validation, and configuration come from
PIPELINE_CONTEXTand.production-grade.yamlwhen present.
| Mode | Behavior |
|---|---|
| Express | Full schema design, migrations, optimization. Report decisions. |
| Standard | Surface engine choice, normalization trade-offs, indexing. Auto-resolve rest. |
| Thorough | Full data model. Walk through normalization decisions. Show EXPLAIN. |
| Meticulous | Walk through each table. User reviews indexes, constraints. Capacity projections. |
If .forgewright/codebase-context.md exists and mode is brownfield:
snake_case, don't switch to camelCase| Category | Inputs | Behavior if Missing |
|---|---|---|
| Critical | Data entities/relationships, or existing schema to optimize | STOP — cannot design without knowing the data |
| Degraded | Query patterns, traffic volume, performance metrics | WARN — design for general-purpose performance |
| Optional | Compliance requirements, retention policies, backup strategy | Continue — use production defaults |
| Phase | Purpose |
|---|---|
| 1 | Data Requirement Analysis, engine selection |
| 2 | Schema Design, normalization, constraints |
| 3 | Indexing Strategy, query optimization |
| 4 | Migration Scripts, zero-downtime patterns |
| 5 | Scaling Architecture, capacity planning |
Goal: Understand data patterns and select database engines.
| Pattern | Characteristics | Best Engine |
|---|---|---|
| Transactional CRUD | Strong consistency, complex joins, ACID | PostgreSQL, MySQL |
| Document-oriented | Flexible schema, nested objects, no joins | MongoDB, DynamoDB |
| Key-value cache | Fast reads, TTL, simple lookups | Redis, Memcached |
| Full-text search | Search queries, facets, autocomplete | Elasticsearch, OpenSearch |
| Time-series | Append-only, time-bucketed queries | TimescaleDB, InfluxDB |
| Graph relationships | Traversals, recommendations, social | Neo4j, Neptune |
| Vector/embeddings | Similarity search, RAG, ML | pgvector, Pinecone, Weaviate |
## Recommended Multi-Database Architecture
| Database | Purpose | Data |
|----------|---------|------|
| **PostgreSQL** | Primary store | Users, orders, products |
| **Redis** | Cache + sessions | Hot data, sessions, rate limiting |
| **Elasticsearch** | Full-text search | Products, content |
| **pgvector** | Vector storage | Embeddings, recommendations |
### When to Use Each
| Use Case | Solution | Why |
|----------|----------|-----|
| User sessions | Redis | Fast TTL-based expiry |
| Product catalog search | Elasticsearch | Faceted search, autocomplete |
| ML recommendations | pgvector | Similarity search |
| Time-series analytics | TimescaleDB | Time-bucketed aggregation |
| Graph relationships | Neo4j | Friend-of-friend queries |
| Scale | Rows/Table | Storage | Considerations |
|---|---|---|---|
| Small | < 100K | < 1 GB | Any engine works. Optimize for DX. |
| Medium | 100K - 10M | 1-50 GB | Index strategy matters. Pooling needed. |
| Large | 10M - 1B | 50-500 GB | Partitioning required. Replicas needed. |
| Massive | > 1B | > 500 GB | Sharding, distributed. Specialized engines. |
Goal: Design the complete database schema.
-- UUID for distributed systems (RECOMMENDED)
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
email VARCHAR(255) NOT NULL,
-- ...
);
-- BIGSERIAL for internal-only tables
CREATE TABLE audit_logs (
id BIGSERIAL PRIMARY KEY,
action VARCHAR(50) NOT NULL,
-- ...
);
-- Every table gets these
CREATE TABLE example (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
-- Audit fields
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_by UUID REFERENCES users(id),
updated_by UUID REFERENCES users(id),
-- Soft delete (never hard delete)
deleted_at TIMESTAMPTZ NULL,
-- Tenant isolation (if multi-tenant)
tenant_id UUID NOT NULL,
-- Your columns here
name VARCHAR(255) NOT NULL
);
-- Auto-update updated_at trigger
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER example_updated_at
BEFORE UPDATE ON example
FOR EACH ROW
EXECUTE FUNCTION update_updated_at();
## Naming Standards
| Element | Convention | Example |
|---------|-----------|---------|
| Tables | plural snake_case | users, order_items |
| Columns | snake_case | first_name, created_at |
| Primary keys | id | id |
| Foreign keys | <singular>_id | user_id, order_id |
| Indexes | idx_<table>_<columns> | idx_orders_user_id_created_at |
| Unique constraints | uk_<table>_<columns> | uk_users_email |
| Check constraints | chk_<table>_<rule> | chk_orders_total_positive |
| Sequences | <table>_<column>_seq | users_id_seq |
-- NOT NULL (when required)
ALTER TABLE users ADD COLUMN email VARCHAR(255) NOT NULL;
-- UNIQUE (business identifiers)
ALTER TABLE users ADD CONSTRAINT uk_users_email UNIQUE (email);
ALTER TABLE users ADD CONSTRAINT uk_users_email_tenant UNIQUE (email, tenant_id);
-- CHECK (value validation)
ALTER TABLE orders ADD CONSTRAINT chk_orders_total_positive
CHECK (total_cents >= 0);
ALTER TABLE users ADD CONSTRAINT chk_users_age
CHECK (age >= 0 AND age < 150);
-- FOREIGN KEY (relationships)
ALTER TABLE orders ADD CONSTRAINT fk_orders_user
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE RESTRICT; -- Prevent deletion if orders exist
ALTER TABLE order_items ADD CONSTRAINT fk_order_items_order
FOREIGN KEY (order_id) REFERENCES orders(id)
ON DELETE CASCADE; -- Delete items when order deleted
-- EXCLUSION (non-overlapping ranges)
CREATE TABLE reservations (
room_id INTEGER NOT NULL,
start_time TIMESTAMPTZ NOT NULL,
end_time TIMESTAMPTZ NOT NULL,
EXCLUDE USING gist (
room_id WITH =,
tstzrange(start_time, end_time) WITH &&
)
);
| Level | When | Example |
|---|---|---|
| 3NF (default) | Most tables | users, orders, products |
| Denormalize | Read-heavy, rarely updated, complex joins slow | Materialized views, search indexes |
| Over-normalize | Never | Don't split first_name and last_name into a name_parts table |
Goal: Design indexes to support query patterns.
| Type | Use Case | PostgreSQL |
|---|---|---|
| B-tree (default) | Equality, range, sorting | CREATE INDEX |
| Hash | Exact equality only | USING hash |
| GIN | Full-text search, JSONB, arrays | USING gin |
| GiST | Geometric, range types, proximity | USING gist |
| BRIN | Large time-series tables, sequential data | USING brin |
## Index Creation Checklist
| Query Pattern | Index |
|--------------|-------|
| WHERE user_id = ? | `CREATE INDEX ON orders(user_id)` |
| WHERE tenant_id = ? AND status = ? | `CREATE INDEX ON orders(tenant_id, status)` |
| WHERE created_at > ? | `CREATE INDEX ON orders(created_at)` |
| WHERE email LIKE 'john%' | `CREATE INDEX ON users(email varchar_pattern_ops)` |
| Full-text search | `CREATE INDEX ON posts USING gin(to_tsvector('english', content))` |
| JSONB containment | `CREATE INDEX ON events USING gin(metadata jsonb_path_ops)` |
| Array contains | `CREATE INDEX ON tags USING gin(tag_ids)` |
-- Rule: Most selective column first, but also consider equality vs range
-- Good: Equality first, then range
CREATE INDEX idx_orders_tenant_status_created
ON orders(tenant_id, status, created_at DESC);
-- Good: For pagination with cursor
CREATE INDEX idx_users_tenant_id_id
ON users(tenant_id, id);
-- Bad: Range column first (can't use subsequent columns efficiently)
CREATE INDEX idx_orders_created_tenant
ON orders(created_at DESC, tenant_id); -- Can't use tenant_id efficiently
-- Include frequently-selected columns to avoid table lookups
CREATE INDEX idx_orders_tenant_id_id_email_name
ON orders(tenant_id, id)
INCLUDE (user_email, user_name);
-- Now this query is a covering index scan
SELECT id, user_email, user_name
FROM orders
WHERE tenant_id = $1
ORDER BY id
LIMIT 20;
-- Index only active rows (smaller, faster)
CREATE INDEX idx_orders_pending
ON orders(created_at)
WHERE status = 'pending';
-- Index non-deleted rows
CREATE INDEX idx_users_active
ON users(email)
WHERE deleted_at IS NULL;
-- Analyze query plan
EXPLAIN ANALYZE
SELECT *
FROM orders
WHERE tenant_id = '123'
AND status = 'pending'
ORDER BY created_at DESC
LIMIT 20;
-- Common issues to look for:
-- Seq Scan on large table → Add index
-- Nested Loop on large datasets → Check join conditions
-- Sort without index → Add index with correct column order
-- High cost estimate → Check row estimates vs actual
-- ❌ SELECT * (fetches unnecessary columns)
SELECT * FROM orders WHERE tenant_id = $1;
-- ✅ Select only needed columns
SELECT id, status, total FROM orders WHERE tenant_id = $1;
-- ❌ N+1 queries (loop executes query per item)
for order_id in order_ids:
items = db.query("SELECT * FROM items WHERE order_id = $1", order_id)
-- ✅ Batch query or JOIN
SELECT o.*, json_agg(i.*) as items
FROM orders o
LEFT JOIN items i ON i.order_id = o.id
WHERE o.id = ANY($1)
GROUP BY o.id;
-- ❌ LIKE '%search%' (can't use B-tree index)
SELECT * FROM posts WHERE content LIKE '%search%';
-- ✅ Full-text search with GIN index
SELECT * FROM posts WHERE to_tsvector('english', content) @@ to_tsquery('english', 'search');
-- ❌ ORDER BY RANDOM() (full table scan + sort)
SELECT * FROM posts ORDER BY RANDOM() LIMIT 1;
-- ✅ Keyset pagination with random offset
SELECT * FROM posts
WHERE id > (SELECT id FROM posts ORDER BY id LIMIT 1 OFFSET floor(random() * 1000))
LIMIT 1;
-- ❌ Missing LIMIT (returns entire table)
SELECT * FROM orders WHERE tenant_id = $1;
-- ✅ Always paginate
SELECT * FROM orders WHERE tenant_id = $1 LIMIT 20;
-- ❌ Function in WHERE (index can't be used)
SELECT * FROM orders WHERE EXTRACT(YEAR FROM created_at) = 2024;
-- ✅ Range query (uses index)
SELECT * FROM orders
WHERE created_at >= '2024-01-01'
AND created_at < '2025-01-01';
Goal: Generate safe, reversible database migration scripts.
schemas/
├── migrations/
│ ├── 000_initial.sql
│ ├── 001_create_users.sql
│ ├── 002_create_orders.sql
│ ├── 003_add_pending_status.up.sql
│ ├── 003_add_pending_status.down.sql
│ └── .../
└── seed/
└── seed.sql
-- Migration: 003_add_order_status
-- Created: 2024-01-15
-- Author: database-engineer
-- Description: Add status column to orders table with default 'pending'
-- UP Migration
BEGIN;
-- Add column as nullable first
ALTER TABLE orders ADD COLUMN status VARCHAR(20) DEFAULT 'pending';
-- Add NOT NULL constraint after all rows have value
ALTER TABLE orders ALTER COLUMN status SET NOT NULL;
-- Create index for common query pattern
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_status
ON orders(status)
WHERE status = 'pending';
-- Add check constraint
ALTER TABLE orders ADD CONSTRAINT chk_orders_status
CHECK (status IN ('pending', 'processing', 'shipped', 'delivered', 'cancelled'));
-- Update any existing rows (already have default)
UPDATE orders SET status = 'pending' WHERE status IS NULL;
COMMIT;
-- DOWN Migration
BEGIN;
ALTER TABLE orders DROP CONSTRAINT IF EXISTS chk_orders_status;
DROP INDEX IF EXISTS idx_orders_status;
ALTER TABLE orders DROP COLUMN IF EXISTS status;
COMMIT;
## Safe Migration Patterns
| Change | Safe Approach |
|--------|--------------|
| **Add column** | Add nullable → backfill → add NOT NULL |
| **Remove column** | Stop reading → deploy → drop column |
| **Rename column** | Add new → copy data → update code → drop old |
| **Add index** | `CREATE INDEX CONCURRENTLY` (no lock) |
| **Remove index** | Deploy with unused index → drop in next deploy |
| **Change type** | Add new column → migrate → swap → drop old |
| **Add table** | Safe — no existing data |
| **Remove table** | Verify zero reads → soft-archive → drop |
### Expand-Contract Pattern
```sql
-- EXPAND: Add new structure
ALTER TABLE orders ADD COLUMN new_status VARCHAR(20);
-- Copy data
UPDATE orders SET new_status = old_status;
-- CONTRACT: Remove old structure
ALTER TABLE orders DROP COLUMN old_status;
-- Phase 1: Add new column (nullable)
ALTER TABLE orders ADD COLUMN new_status VARCHAR(20);
-- Phase 2: Backfill (batch to avoid lock)
DO $$
DECLARE
batch_size INT := 1000;
offset_val INT := 0;
rows_updated INT;
BEGIN
LOOP
UPDATE orders
SET new_status = old_status
WHERE id IN (
SELECT id FROM orders
WHERE new_status IS NULL
LIMIT batch_size
);
GET DIAGNOSTICS rows_updated = ROW_COUNT;
EXIT WHEN rows_updated = 0;
-- Add small delay between batches
PERFORM pg_sleep(0.1);
END LOOP;
END $$;
-- Phase 3: Add constraint
ALTER TABLE orders ALTER COLUMN new_status SET NOT NULL;
-- Phase 4: Deploy code that uses new_status
-- Phase 5: Remove old column
ALTER TABLE orders DROP COLUMN old_status;
Goal: Plan database scaling strategy.
| Strategy | When | Complexity | Implementation |
|---|---|---|---|
| Vertical scaling | First resort | Low | Bigger instance |
| Connection pooling | Many service instances | Low | PgBouncer, RDS Proxy |
| Read replicas | Read-heavy (> 80% reads) | Medium | Streaming replication |
| Partitioning | Single table > 100M rows | Medium | Range, list, hash partitioning |
| Materialized views | Complex reporting queries | Medium | Pre-computed aggregations |
| Sharding | Multi-tenant at scale | High | Citus, Vitess, manual |
| CQRS | Separate read/write models | High | Event sourcing |
# PgBouncer configuration
[databases]
app = host=postgres dbname=app
[pgbouncer]
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 20
min_pool_size = 5
reserve_pool_size = 5
reserve_pool_timeout = 5
max_db_connections = 100
-- Connection pool sizing guide
-- Total connections = max_pool_size × service_instances ≤ max_connections
-- Example: 4 service pods × 20 = 80 connections
-- Keep max_connections on DB at 100 (leave headroom)
-- Range partitioning by time (for time-series data)
CREATE TABLE events (
id BIGSERIAL,
created_at TIMESTAMPTZ NOT NULL,
tenant_id UUID NOT NULL,
event_type VARCHAR(50) NOT NULL,
payload JSONB
) PARTITION BY RANGE (created_at);
-- Create partitions
CREATE TABLE events_2024_q1 PARTITION OF events
FOR VALUES FROM ('2024-01-01') TO ('2024-04-01');
CREATE TABLE events_2024_q2 PARTITION OF events
FOR VALUES FROM ('2024-04-01') TO ('2024-07-01');
-- Partition pruning is automatic
SELECT * FROM events WHERE created_at >= '2024-05-01';
-- Automatically uses events_2024_q2
-- Application-level routing
-- Write to primary, read from replica
// Example: Read replica routing
class DatabaseClient {
private primary: Pool;
private replica: Pool;
async query(sql: string, params: any[], options: { readOnly?: boolean } = {}) {
const pool = options.readOnly ? this.replica : this.primary;
return pool.query(sql, params);
}
}
// Usage
const users = await db.query(
'SELECT * FROM users WHERE tenant_id = $1',
[tenantId],
{ readOnly: true } // Route to replica
);
## Capacity Thresholds
| Metric | Current | 10x | 100x | Action at Threshold |
|--------|---------|-----|------|-------------------|
| Connections | N | 10N | 100N | Pooler at 50+ |
| Storage (GB) | X | 10X | 100X | Archival at 500GB |
| QPS | Q | 10Q | 100Q | Replicas at 5K QPS |
| Write QPS | W | 10W | 100W | Sharding at 10K writes |
| Largest table (rows) | R | 10R | 100R | Partitioning at 100M rows |
## Projection Template
| Table | Current Rows | 1 Year | 3 Years | Index Strategy |
|-------|-------------|--------|---------|---------------|
| users | 10K | 100K | 1M | Partition by tenant |
| orders | 100K | 1M | 10M | Partition by date |
| events | 1M | 10M | 100M | Partition by month |
| Mistake | Fix |
|---|---|
| No indexes on foreign keys | Index FK columns — joins and cascades need them |
| VARCHAR(255) everywhere | Use appropriate lengths or TEXT if no real limit |
| Auto-increment as public ID | Use UUIDs. Auto-increment leaks volume and is guessable. |
| Hard deletes | Soft delete with deleted_at. Customer data is compliance. |
| No connection pooling | PgBouncer from day one. 10 pods × 20 = 200 connections. |
| Migrations that lock tables | Use CONCURRENTLY for indexes, expand-contract for changes |
| Business logic in triggers | Triggers make debugging harder. Keep logic in app code. |
| No constraints at DB level | "The app validates" isn't a constraint. DB is the last defense. |
| Same pool size for all envs | Dev: 2-5, staging: 5-10, prod: 10-20 |
| No data dictionary | Future developers need to know what status INT means |
schemas/
├── erd.md # Entity-relationship diagram
├── migrations/
│ ├── 000_initial.sql
│ ├── 001_*.sql
│ └── ...
├── seed/
│ └── seed.sql # Development seed data
└── data-dictionary.md # Column-level documentation
.forgewright/database-engineer/
├── data-analysis.md # Access patterns, volume estimates
├── schema-design.md # Design decisions, normalization rationale
├── optimization-report.md # Query analysis, index recommendations
├── scaling-plan.md # Capacity projections, scaling strategy
└── migration-guide.md # Zero-downtime procedures
data-engineer), REST/GraphQL contract design (api-designer), or application business logic (software-engineer).EXPLAIN (ANALYZE, BUFFERS) on representative data; record Schema v2 evidence (VERIFY.md, plus review-2 for HARD schema/payment migrations).What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
database-design, schema-design, indexing, query-optimization, migrations, scaling
npx claudepluginhub buiphucminhtam/forgewright --plugin forgewright93 skills in this plugin
/3d-spatial-engineerUse for 3D spatial layouts, scene-graph coordinate transforms, greybox blockouts, culling volumes, static batching, and LOD optimization across 3D game engines./accessibility-engineerUse for web WCAG 2.1/2.2 AA accessibility audits, ARIA semantics, keyboard navigation, focus management, contrast verification, and automated axe-core remediation./ai-behavior-engineerUse for game AI and NPC decision systems: finite state machines, behavior trees, utility AI, GOAP, perception, navigation, tactical steering, and encounter tuning./ai-engineerUse for production AI/ML application systems, LLM API integrations, RAG retrieval pipelines, structured outputs, model serving, prompt/tool orchestration, and AI reliability checks./animation-engineerUse for game animation systems: 2D spritesheets, smear frames, pivot stability, Spine/Rive/Skeleton2D, 3D rigs/blend trees/IK/montages, procedural springs, combat frame data, and hitstop./api-designerUse for designing REST, GraphQL, gRPC, or AsyncAPI contracts, OpenAPI schemas, pagination, versioning, idempotency, error envelopes, and rate-limiting policies.Other plugins' skills for the same job