Skip to content

Lesson 9 of 9 · 28m

Low-Downtime Schema Changes and Major Upgrades

How to evolve a busy schema without locking production, when CREATE INDEX CONCURRENTLY helps and hurts, and how to choose between in-place, logical, and blue-green major upgrades.

The schema migration that looks harmless in review is often the one that takes ACCESS EXCLUSIVE on the table every request touches. The migration itself may finish in 80 milliseconds, but if it queues behind a long analytical query, every web request lines up behind it. Low-downtime operations are about lock acquisition order, queue control, batching, and compatibility.

Control lock acquisition first

SET lock_timeout = '2s'; SET statement_timeout = '30s';

Run migrations in a session that fails fast instead of waiting indefinitely. If the lock cannot be acquired, retry during a calmer moment. Verify lock behavior on production-scale data; "it worked in staging" means little when staging had no concurrent traffic.

Expand, migrate, contract

Do not couple old-schema removal with new-schema rollout. Use three stages:

  1. Expand: add nullable columns, new tables, and compatible indexes.
  2. Migrate: deploy code that writes both shapes or backfills gradually.
  3. Contract: after old code is gone, enforce constraints and drop obsolete columns.
ALTER TABLE orders ADD COLUMN status_v2 text;
UPDATE orders SET status_v2 = legacy_status
WHERE id BETWEEN 1000000 AND 1099999 AND status_v2 IS NULL;

Backfill in batches and commit each batch. Measure replication lag and vacuum pressure. A single giant UPDATE is a bloat generator wearing a hard hat.

Indexes are maintenance operations too

CREATE INDEX CONCURRENTLY idx_orders_account_created ON orders (account_id, created_at);

CREATE INDEX CONCURRENTLY avoids blocking ordinary reads and writes, but it is slower, cannot run inside a transaction block, and can fail leaving an invalid index. Migration frameworks that wrap everything in a transaction need deliberate exceptions. Partitioning is often the longer-term answer for very large append-only tables because retention becomes partition drops rather than massive deletes.

SELECT indexrelid::regclass, indisvalid, indisready FROM pg_index WHERE NOT indisvalid;

Major version upgrades are architecture choices

In-place with pg_upgrade is usually fastest and preserves the cluster, but requires both versions installed, extension compatibility, rehearsal, and a clear rollback boundary. Logical replication cutover builds a new cluster on the target version, replicates tables logically, then cuts over. It can approach zero downtime but requires explicit handling of sequences, DDL changes, large objects, replication gaps, and application rollback. Dump/restore or blue-green rebuild is simple but often too slow for large databases. Choose based on downtime tolerance, size, extensions, and rollback requirements — not habit.

Rehearse the cutover, not just the upgrade

1. Freeze schema changes; use read-only mode; verify replication or upgrade completion and validate.
2. Switch endpoint or pooler target; keep the old cluster untouched through rollback; watch statistics and query plans.

Run ANALYZE after upgrade and expect some plans to move even when data is identical.

Field notes

  • ADD COLUMN with a volatile default or rewrite-triggering type change can still be expensive; verify behavior on your exact version.
  • Foreign keys can be added NOT VALID and validated later with less blocking.
  • Batch deletes create dead tuples; coordinate retention cleanup with vacuum.
  • Test migrations with production-scale data and concurrent traffic.
  • Schema rollback usually means compatibility, not reverting the database.

Was this lesson helpful?

Sign in to rate