P50 28s Analyze →
↩ All field notes
Querk · Field notes

Zero-Downtime Postgres Migrations on an SMB Budget

Published November 26, 2026 · Querk — Postgres review pipeline

Small and midsize teams running PostgreSQL rarely have a dedicated platform team to babysit schema changes. Yet a single `ALTER TABLE` can take an exclusive lock, queue writes, and trigger pages at midnight. Zero-downtime migrations are possible without enterprise tooling, provided the team understands how Postgres acquires locks and plans each change in stages.

Planning for Add-Column Safety

Adding a column seems routine, but the method matters. In many Postgres versions, `ALTER TABLE ... ADD COLUMN` with a non-null default rewrites the entire table and holds an `AccessExclusive` lock, blocking reads and writes. The safer pattern is to add the column as nullable with no default, backfill existing rows in small batches, and only then issue a second `ALTER TABLE` to set `NOT NULL` and add the default. Because the second command only validates constraints and updates metadata, it completes quickly and does not rewrite table data.

Backfilling Without Locking the Table

Backfilling millions of rows in one transaction bloats the table and holds locks longer than necessary. Instead, update rows in bounded batches keyed on the primary key, such as `WHERE id BETWEEN start AND end`, and pause between batches to let autovacuum catch up. Filtering on `WHERE new_column IS NULL` ensures each batch only touches unfinished work. If the column will eventually be read by application code, deploy the code to read from both old and new sources during the transition, then switch fully after verification. This approach keeps replication lag manageable and avoids long-running transactions that block vacuum cleanup.

Dropping Indexes and Columns in Stages

Indexes and obsolete columns should never be dropped in the same transaction that introduces their replacements. Use `DROP INDEX CONCURRENTLY` and `CREATE INDEX CONCURRENTLY` so regular traffic is not blocked. For columns, stop writing to the old column first, verify the new column is fully populated, and remove the old column during a low-traffic window or alongside a planned maintenance period. Keeping both columns temporarily doubles storage but eliminates the risk of a failed rollback leaving the application pointing at a missing field.

Monitoring and Rollback on a Small Team

Without a platform team, observability must be lightweight but sufficient. Query `pg_stat_activity` and `pg_locks` to spot blocked sessions, and check `pg_stat_progress_*` views to see whether a backfill or index build is still running. Setting `statement_timeout` at the application or connection level prevents a rogue migration from holding the database hostage. A rollback plan should include keeping the old schema available until the new column or index has served traffic successfully for a defined period.

Teams using Querk can surface slow queries and lock contention during schema changes, making it easier to catch migration problems before they become incidents. The core principle remains the same: break the change into small, reversible steps, verify each one, and never assume that a fast local test will behave identically on production data.

Paste your slowest query — 3 free reviews, no signup. Index DDL, rewrites, write-path impact, and a verification command back in ~30 seconds. Analyze a query →