Database-architect agent for Claude Code rewritten with production-grade PostgreSQL rules

The updated agent studies existing schemas, detects lock risks, uses concurrent indexes, and validates migrations on a temporary database — scoring 10/10 on an 80-million-row table migration test wher

cpaua4 min5
Database-architect agent for Claude Code rewritten with production-grade PostgreSQL rules

The database-architect agent for Claude Code has been rewritten with a rule set derived from senior DBA practices for PostgreSQL 16+. The agent now reads the project's migration history and ORM schema, identifies locking hazards and breaking changes, applies lock_timeout and CREATE INDEX CONCURRENTLY, and verifies every migration against a throwaway database before handing it back.

What changed in the agent's behaviour

The new version follows a strict workflow: it first locates the migrations folder, the runner conventions, and any README notes on roles, tenancy, data volumes, and deploy style. It then pins down facts that change the design — table sizes, write rates, whether deploys are rolling, the connection role, tenant boundaries, and the Postgres version. Only after that does it design or alter the schema, test the SQL on a real database, and produce a report covering decisions, trade-offs, test output, assumptions, and deploy-step ordering.

Modelling rules enforce primary keys on every table (bigint identity by default, uuid v7 when IDs are public or merged), timestamptz over timestamp, numeric or integer minor units for money, text with CHECK length limits, and boolean NOT NULL. Constraints live in the database: CHECK for ranges, UNIQUE for natural keys, case-insensitive uniqueness for emails via citext or a lower-cased index. Foreign keys are mandatory with deliberate ON DELETE actions, and every FK column is indexed. Indexes are added only for queries that actually exist.

Multi-tenancy baked in

When rows belong to a tenant, the agent puts the tenant key on every tenant-owned table, makes cross-tenant references impossible through composite foreign keys, and enables row-level security with FORCE ROW LEVEL SECURITY so table owners cannot bypass it. Policies use current_setting('app.current_tenant_id') with NULLIF to fail closed on pooled connections. The app role must not be a superuser or hold BYPASSRLS. Uniqueness is scoped to the tenant unless the value is globally unique, and the tenant key leads composite indexes.

Changing a live database without downtime

The agent assumes a large, busy table. Every migration file that takes a lock sets lock_timeout = '5s' (and a statement_timeout for non-concurrent statements) using SET LOCAL so the setting never leaks into a reused connection. Adding a nullable column with a non-volatile default is instant on PG 11+. Making a column NOT NULL uses a NOT VALID check constraint, validates it, then flips the column — avoiding a full table scan on PG 12+. New CHECK or foreign keys are added NOT VALID and validated in a separate step that does not block reads or writes.

Indexes are built with CREATE INDEX CONCURRENTLY in their own migration file when the runner wraps files in transactions. Unique constraints follow the same pattern: concurrent unique index first, then ALTER TABLE ... ADD CONSTRAINT ... UNIQUE USING INDEX. Renames, drops, and type changes never happen in one step while old code runs; the agent emits an expand/contract sequence — add the new column, dual-write and backfill in batches, switch reads, then drop the old column in a later release — and labels which step belongs to which deploy.

Test results: 80 million rows, 10 minutes

In one benchmark the agent was given an 80-million-row table and asked to produce a migration with only ten minutes until deployment. Without the agent, Claude scored 6/10; with the agent, 10/10. Across eight tests Claude autonomously selected the database-architect agent every time. The author notes that vanilla Claude already handles schema design competently; the gap appears when migrations risk breaking a running application.

Installation

shellscript
npx claude-code-templates@latest --agent database/database-architect

The agent is part of the Claude Code templates collection and hands off to specialised agents such as postgres-pro for query tuning, neon-database-architect for Neon branching, backend-developer for ORM models, and security-auditor for PII classification.

Sources

Database Architect — Agents for Claude Code | Claude Code Templates

Share

Author

cpaua

VibeCode blog admin. Writing about vibe coding, AI and open source.

Related news

Comments

To leave a comment, log in
Loading...

Weekly digest

The best of vibe coding, AI agents and open source — once a week, no spam.