PostgreSQL, Prisma & Drizzle AI Agent Rules & Cursorrules
Eliminate N+1 database bottlenecks and unsafe table locks in AI-generated code.
Prevent AI agents from writing disastrous database migrations and slow unindexed queries. Enforce compound indexing, non-blocking lock timeouts, and safe ORM joins.
Configuration Workbench
Select your agent format below to preview, copy, or download the configuration file.
# PostgreSQL & ORM Cursor Rules
You are a Principal Database Administrator and Performance Engineer.
## Schema & Query Invariants
- PostgreSQL: 16+.
- Every foreign key must have an accompanying index.
- Never write N+1 query loops. Always use batching or eager loading with explicit joins.
- Use UUIDv7 or ULID for primary keys to maintain sequential B-tree ordering.
- For large migrations, use 'CREATE INDEX CONCURRENTLY' with strict statement timeouts.
- Never store raw passwords or unhashed tokens. Use Argon2id or bcrypt.Pro-tip: Place this file directly in the root directory of your PostgreSQL, Prisma & Drizzle ORM project. AI agents like Cursor Composer, Claude Code, and GitHub Copilot will read it automatically on initialization.
Architectural Principles Enforced
When this configuration file is loaded into Cursor or Claude Code, the agent strictly enforces the following design invariants:
Always add compound database indexes to foreign keys and filtered columns.
Never generate N+1 query patterns; use relational joins or batching (`include`, `with`).
Enforce lock timeouts (`SET lock_timeout = '2s'`) on production schema migrations.
Use `CONCURRENTLY` when building indexes on existing PostgreSQL tables with production traffic.
Use ULID or UUIDv7 for distributed identifiers to preserve B-tree index insertion locality.
Installation & Repository Setup
Place in project root as `.cursorrules` or `.cursor/rules/database.mdc`.
Verified Agent Behavior Examples
Observe how the AI agent responds when prompted under these active rules.
Frequently Asked Questions
Why are UUIDv7 primary keys recommended over UUIDv4?
UUIDv4 is purely random, which causes severe B-tree page fragmentation and slow index insertions on large tables. UUIDv7 embeds a timestamp prefix, ensuring chronological insertion order while retaining global uniqueness.