Skip to content

All articles

PostgreSQL & Performance · Updated 1 min read142 wordsby Callum Reid

Zero-Downtime PostgreSQL Schema Migrations Without Table Locks

How we roll out column changes, backfill high-volume UK ledgers, and add composite indexes in PostgreSQL without blocking live production traffic.

Share this article
LinkedInX / Twitter
Zero-Downtime PostgreSQL Schema Migrations Without Table Locks

Mid-market engineering teams rarely experience downtime from slow SELECT queries alone—they suffer outages during Friday afternoon schema migrations that accidentally acquire an ACCESS EXCLUSIVE lock on a busy table.

In PostgreSQL, a single ALTER TABLE ADD COLUMN ... NOT NULL with a volatile default or an un-scoped CREATE INDEX can queue every incoming API request behind a stalled lock wait until connection pools saturate.

Our delivery rule across every UK client ledger is expand-and-contract paired with explicit lock timeouts:

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

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_consignments_hub_status_date
  ON freight_consignments (hub_code, status, dispatched_at DESC)
  WHERE status IN ('in_transit', 'customs_hold');

Before deploying destructive drops, we add nullable columns first, deploy dual-write application code, backfill in bounded 5,000-row batches, attach NOT VALID constraints, and run VALIDATE CONSTRAINT separately so normal reads and writes never stall.

Keep reading

More engineering notes from overlapping topics.

View all articles

Planning a new web platform or legacy migration?

Speak directly with a senior UK engineer. Share your requirements and receive a pragmatic scope, architecture outline, and timeline.

Schedule Discovery Call