LLM Rules for Postgres
Six explicit Postgres rules, drawn from building the wispbit review bot, let an LLM code reviewer or coding agent catch common migration and query mistakes before production. The rules are to create indexes CONCURRENTLY to avoid write locks, mark columns as ignored in the ORM before dropping them, index every foreign key, require id/created_at/updated_at on every table, skip indexes already covered by a composite index, and ensure frequent WHERE conditions have matching indexes. Each comes with bad/good SQL, Prisma or SQLAlchemy examples. Teams are encouraged to add new rules after each incident.
Related posts
- Incident Report: October 28th, 2025
Railway mediumAdeptA schema migration that added an indexed column to a ~1 billion-row Postgres table without `CREATE INDEX CONCURRENTLY` took an exclusive table lock, queuing nearly all backend API queries and taking down Railway's dashboard, CLI, Public API and GitHub webhook deploys for about 40 minutes. Blocked requests piled up until PgBouncer exceeded Postgres's connection limit, exhausting even reserved administrative slots, so engineers could not connect to kill the index build. The outage ended only when the build finished on its own after roughly 30 minutes. Fixes include a CI check that requires `CONCURRENTLY` for every index creation, PgBouncer pool limits capped below Postgres capacity, and per-user connection limits that keep admin access available.
- Navigating PostgreSQL Locks with Neon and Atlas
Neon mediumAdept
Adding a UNIQUE constraint or setting a column NOT NULL in Postgres triggers a full table scan under an ACCESS EXCLUSIVE lock, which blocks all reads and writes and can take down an API on large tables. The safer sequences are CREATE UNIQUE INDEX CONCURRENTLY followed by ADD UNIQUE USING INDEX, and a NOT VALID CHECK constraint followed by VALIDATE before SET NOT NULL. To catch such migrations automatically, a GitHub Actions pipeline runs Atlas's migrate lint (check PG105) on pull requests. It then applies the migration to a Neon branch cloned from production, with a 10s statement_timeout hook, so long-locking migrations fail the build before deployment.
- Dynamic Table Partitioning in Postgres
Supabase mediumAdeptPostgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock. The walkthrough builds a simulated chat app with pl/pgSQL functions that create partitions dynamically. It also covers why large tables hurt (btree depth, vacuum, archiving), the requirement that the partition key be part of composite primary and foreign keys, and ORM support gaps such as Django's lack of composite keys.
- The silent syntax difference in foreign keys between Postgres and MySQL
Neon mediumAdept
MySQL parses but silently ignores inline column-level `REFERENCES` clauses, so a foreign key written Postgres-style (`customer_id INT REFERENCES customers(customer_id) ON DELETE CASCADE`) creates no constraint, no index and no cascade behavior in MySQL. Orphaned rows insert without error. Only a separate table-level `FOREIGN KEY (...) REFERENCES ...` clause is enforced. Use the explicit `FOREIGN KEY` form in both databases, review schemas when porting between them, and test that constraints actually reject invalid data.
- Vibe Coding With AI to Generate Synthetic Data: Part 1
Neon mediumAdept
Asking Claude to generate synthetic Postgres seed data directly as SQL INSERT statements from a schema-only dump breaks down quickly: claude-3-5-sonnet lost referential integrity somewhere between 20 and 100 rows, and claude-3-7-sonnet often failed with 'model overloaded' errors. A hybrid approach scaled much further. There the model emits JavaScript objects that use faker.js, and the script converts them into INSERTs, which passed at 3,000 rows before foreign-key errors appeared at 5,000. GPT-4.5 showed the same pattern. The pipeline runs in GitHub Actions for Neon dev/test branches, and the open question is whether better prompting costs less effort than maintaining seed scripts by hand.