← All Insights

PRIMESHIFT INSIGHTS · April 15, 2026

Expand-and-Contract DDL: Shipping Zero-Lock Schema Changes on 4TB Transaction Tables

Avoiding ACCESS EXCLUSIVE lock queues in PostgreSQL when renaming columns or backfilling NOT NULL constraints at scale.

Geometric event-sourced double-entry financial ledger routing schematic
WED, APR 15, 2026·7 min read·Modernization & Data·Issue #23 · Strangler Cutovers
Dispatch Issue #23 · Key Metric: 0 blocked OLTP transactions across 140+ production schema evolutions

Production Context & Why This Matters

A seemingly trivial `ALTER TABLE … ADD COLUMN … DEFAULT …` or unguarded `NOT NULL` constraint queues an `ACCESS EXCLUSIVE` lock behind long-running reporting queries—bringing all application reads and writes to an immediate halt.

Expand-and-contract separates additive nullable deployment, batched concurrent backfilling, `NOT VALID` check constraint validation, and final cleanup across four reversible releases.

Architectural Mechanics & Reference Implementation

Every migration file begins by setting explicit `lock_timeout` and `statement_timeout` safeguards so contested locks fail harmlessly instead of wedging traffic.

SET lock_timeout = '2s';
ALTER TABLE ledger_entries ADD COLUMN settlement_rail text;
ALTER TABLE ledger_entries ADD CONSTRAINT chk_rail_not_null
  CHECK (settlement_rail IS NOT NULL) NOT VALID;
ALTER TABLE ledger_entries VALIDATE CONSTRAINT chk_rail_not_null;

Verified Operational Benchmarks

Linting every pull-request SQL file with automated lock-safety analyzers prevented 100% of schema-induced outages over 12 months.

  • Primary Delta: 0 blocked OLTP transactions across 140+ production schema evolutions
  • Reliability Gate: Lock timeout capped strictly at 2,000ms inside automated CI runners
  • Ownership Boundary: 100% client-owned IaC & telemetry pipelines audited by Avtar Singh

Implementation Checklist

  • Always specify lock_timeout = 2s before issuing any DDL on active tables.
  • Build indexes strictly with CREATE INDEX CONCURRENTLY outside transaction blocks.
  • Use NOT VALID + VALIDATE CONSTRAINT to check multi-terabyte tables safely.
About the Author

Avtar Singh

Senior Systems Engineer & Head of Engineering Operations

Senior Systems Engineer overseeing day-to-day infrastructure operations, SRE incident response, Kubernetes unit-margin telemetry, and zero-downtime production cutovers across PrimeShift Digital enterprise pods.

Read All Stories by Avtar Singh →

In This Dispatch

Evaluating this architecture inside your production stack?

Our Principal Engineering Desk runs two-week topology audits directly inside your organization cloud & GitHub repos.