Menu

Five database migrations that pass code review and still break production
📰

Five database migrations that pass code review and still break production

DEV Community: postgres·Benveta·3 days ago
#EWG5Z7B4
#devtopostgresql#devto#dev#table#column
Long article (832 words) — no AI summary yet.
Reading 0:00
15s threshold

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.