DEV Community

Benveta
Benveta

Posted on

Five database migrations that pass code review and still break production

Code review reads the diff. Outages come from what the diff doesn't show: how big the table is, which app version is still running, and who else reads the data. Here are five changes that look harmless in a pull request, and the safe way to ship each one. The examples use Postgres, but the patterns apply to most databases.

1. Adding a NOT NULL column without a default

ALTER TABLE orders ADD COLUMN region text NOT NULL;
Enter fullscreen mode Exit fullscreen mode

On an empty dev database this works. On production, where orders has rows, it fails, and your deploy stops halfway through.

The obvious fix, adding a default, is fine only when the default is a constant. Since Postgres 11, ADD COLUMN ... DEFAULT 'eu' NOT NULL with a constant just updates metadata and is instant. A volatile default such as DEFAULT gen_random_uuid() or clock_timestamp() rewrites the whole table while holding an exclusive lock.

Safe way: add the column as nullable, or with a constant default. Backfill it in batches. Add a CHECK (region IS NOT NULL) NOT VALID constraint, run VALIDATE CONSTRAINT, then set NOT NULL.

2. Renaming or dropping a column while the old code is still running

ALTER TABLE users RENAME COLUMN name TO full_name;
Enter fullscreen mode Exit fullscreen mode

The migration runs, and then the rolling deploy starts. For several minutes, pods running the previous version keep sending SELECT name FROM users, and every one of those queries fails. Code review looked at the new code, which is correct. The bug is in the code that is still running.

Safe way: expand, migrate, contract. Add full_name and write to both columns. Backfill it. Switch reads to full_name. Stop writing name. Only in a later release, drop name.

3. Creating an index without CONCURRENTLY

CREATE INDEX idx_orders_customer ON orders (customer_id);
Enter fullscreen mode Exit fullscreen mode

A plain CREATE INDEX takes a lock that blocks every INSERT, UPDATE and DELETE on the table until the index is built. On a large, busy table that can take minutes, so checkout stops working while reads carry on as normal.

Safe way: CREATE INDEX CONCURRENTLY. It can't run inside a transaction, so your migration tool needs to run it outside one. If it fails it leaves an INVALID index behind, so check for that and drop it before you retry.

4. Changing a column's type

ALTER TABLE events ALTER COLUMN payload TYPE jsonb USING payload::jsonb;
Enter fullscreen mode Exit fullscreen mode

Most type changes rewrite the whole table while holding an ACCESS EXCLUSIVE lock, which blocks reads as well as writes. A few changes don't need a rewrite, such as making a varchar longer or changing varchar to text. Many others do, including int to bigint.

There is a second trap. Even a fast ALTER has to wait for that lock. If a long report query is running, your ALTER waits behind it, and every new query on the table waits behind your ALTER. That's an outage caused by a statement that never ran.

Safe way: add a new column, backfill it, then swap reads and writes over, the same way as in #2. For any DDL, set lock_timeout (for example SET lock_timeout = '3s') so a blocked migration fails quickly and can be retried, instead of queuing up all your traffic.

5. Removing a field that someone else consumes

 message OrderPlaced {
   string order_id = 1;
-  string coupon_code = 2;
 }
Enter fullscreen mode Exit fullscreen mode

The producer's tests pass, and the reviewer approves a tidy cleanup. But the analytics consumer, the email service and a partner webhook all read coupon_code. Some of them crash, and others quietly fill reports with nulls for a week before anyone notices. The same applies to public API responses.

Safe way: treat the event or API schema as a contract. Deprecate the field first, find out who reads it, and remove it only when nothing does. In protobuf, reserve the field number so it is never reused.

The pattern

All five share the same cause: the change is safe on its own, but unsafe given the state of production at that moment. That state means the table size, the app versions still running, the locks being held, and the consumers downstream. Reviewers can't see any of that in a diff, and expecting them to remember it every time doesn't scale.

That's why these checks belong in the pipeline, next to your tests. A machine should be the one that says "this rewrites a 40 GB table" or "three services still read this field".


We built BenVeta Release Safety to do exactly this. It checks database, API and event changes in your pipeline, blocks the risky ones and explains why. The trial lasts 11 days and needs no card. We'd especially like to hear where it gets things wrong.

Top comments (0)