Skip to content

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

Shopify Engineering 7 min read Research mediumAdept

Safely Adding NOT NULL Columns to Your Database Tables

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.

Assumes: MySQL triggersonline schema change toolsSQL constraints and indexes

Related posts

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

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

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

  • The State of Online Schema Migrations in MySQL — PlanetScale

    PlanetScale mediumAdept

    MySQL's native INPLACE online DDL is unsuitable for non-blocking changes because it is resource-greedy, uninterruptible and blocks replicas, causing replication lag as long as the original ALTER. INSTANT DDL is ideal where supported, but it covers mostly metadata changes plus ADD/DROP COLUMN, and an instant DROP COLUMN destroys data and metadata with no built-in way back. For most ALTER TABLE changes, shadow-table tools such as Vitess, gh-ost, pt-online-schema-change and spirit remain the recommended approach: they throttle against lag, are interruptible and support almost every change type. Vitess and spirit also auto-detect INSTANT-eligible changes, and Vitess adds revertibility that preserves data.

  • Fix a top cause of slow queries in PostgreSQL (no slow query log needed)

    Render mediumAdept

    PostgreSQL automatically indexes primary keys and UNIQUE columns but not the referencing column of a foreign key, so lookups on that side and ON DELETE CASCADE / SET NULL triggers fall back to full table scans. In a simulated blog schema with 13 million comments, deleting a single user took 19 seconds, almost all of it in foreign-key triggers. Adding non-unique B-tree indexes on articles.author_id, comments.author_id and comments.article_id cut the delete to 342 ms, a 55x speedup, with the comments-by-article trigger dropping from 18 s to 26 ms. The rule of thumb is to index foreign key columns in referencing tables, and it is mandatory when ON DELETE propagation is defined.

The quest for software engineering knowledge.

Look:
Layout: