Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 11 min read How-to mediumAdept

Navigating PostgreSQL Locks with Neon and Atlas

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.

Related posts

  • Incident Report: October 28th, 2025

    Railway mediumAdept

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

  • Safely Adding NOT NULL Columns to Your Database Tables

    Shopify Engineering mediumAdept

    Adding a NOT NULL column through LHM's shadow-table migration (new table, replication triggers, batched copy, rename) can break writes or silently lose data depending on the DEFAULT clause, an added UNIQUE INDEX and the MySQL SQL mode. Shopify's experiment matrix shows that without a DEFAULT, strict mode makes trigger-propagated INSERT/UPDATE/DELETE fail, while non-strict mode fills rows with implicit defaults such as the empty string. Adding a UNIQUE INDEX in the same migration loses rows on INSERT and UPDATE whenever duplicates exist. Always specify a DEFAULT for new NOT NULL columns, and check for duplicate values before adding a unique index.

  • LLM Rules for Postgres

    Neon mediumAdept

    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.

  • I don't trust ON DELETE CASCADE

    PlanetScale mediumAdept

    Postgres ON DELETE CASCADE turns a single-row delete into an unbounded transaction that can cause outages at scale. The cascade holds RowExclusiveLock on every table in the tree, so a migration requesting AccessExclusiveLock queues behind it and blocks all later writes. It can also generate hundreds of gigabytes of WAL that stall synchronous replication, and it falls back to sequential scans on unindexed foreign keys. With logical replication it can leave orphaned rows on subscribers, which skip cascade triggers, and it fails outright on published tables lacking a replica identity. The recommended alternative is soft deletes via a deleted_at column, followed by batched background reaping and vacuum, keeping transactions to hundreds or low thousands of rows.

  • PostgreSQL 15: Three features you can try with Neon

    Neon mediumAdept

    Neon now supports PostgreSQL 15, and three of its features can be tried from Neon's SQL Editor with runnable examples. MERGE performs insert, update or delete in a single statement, though unlike INSERT ON CONFLICT it can fail with a unique violation under concurrent inserts. CREATE privilege on the public schema is no longer granted to all users, so non-owners must be granted it explicitly. UNIQUE NULLS NOT DISTINCT lets a unique constraint permit only one NULL value.

The quest for software engineering knowledge.

Look:
Layout: