Skip to content
Web Development·2 min read·November 22, 2026

Online Database Schema Migrations Using the Expand and Contract Pattern

Preventing PostgreSQL metadata table locks and deployment outages during rolling releases by splitting column renames and constraints into safe backwards-compatible phases.

A
By Anjali
·November 22, 2026·2 min read
Architecture Note
Online Database Schema Migrations Using the Expand and Contract Pattern

## Why ACCESS EXCLUSIVE Locks Stall Production

Running ALTER TABLE accounts RENAME COLUMN phone TO mobile_number; or adding a validated NOT NULL constraint directly in a busy PostgreSQL cluster acquires an ACCESS EXCLUSIVE lock. Even if the DDL command takes milliseconds, it queues behind any active analytics query and blocks every subsequent SELECT and INSERT behind it until all web worker pools time out.

During rolling container updates, old application pods and new pods run concurrently against the same database. If deployment A removes a column before old pods stop querying it, HTTP 500 spikes hit live users immediately.

Four-Stage Expand, Dual-Write, Backfill & Contract

As part of our Cloud & DevOps Architecture and Custom Web Platforms, every destructive schema evolution follows four sequential deployments:

  1. 1.Expand: Add the nullable target column (mobile_number) alongside the legacy column without blocking readers.
  2. 2.Dual-Write & Read Fallback: Deploy application code that writes both columns inside one transaction and reads COALESCE(mobile_number, phone).
  3. 3.Throttled Backfill: Run keyset-paginated batch updates (LIMIT 1000) with brief pauses so autovacuum and write IOPS remain healthy.
  4. 4.Contract: Deploy code reading exclusively from mobile_number, then drop the deprecated column once metrics stay flat for a full release window.

Validating Constraints Online in PostgreSQL

Never ask PostgreSQL to scan an entire live table while holding an exclusive lock. Create constraints using NOT VALID first, which takes only a brief lock to record metadata, then run VALIDATE CONSTRAINT under a lighter lock that permits concurrent reads and writes per the PostgreSQL ALTER TABLE Docs.

SET lock_timeout = '2s';
ALTER TABLE orders ADD CONSTRAINT fk_orders_tenant

-- Executed in a subsequent step with concurrent reads/writes allowed ALTER TABLE orders VALIDATE CONSTRAINT fk_orders_tenant; `

Always set a strict lock_timeout in migration runners as recommended in PostgreSQL Lock Management so stalled migrations abort cleanly instead of queueing production traffic.

Senior Engineering Practice

Need architectural guidance on Web Development?

We partner with founders and engineering leaders to design, build, and optimize scalable web platforms, custom e-commerce engines, and Salesforce CRM ecosystems with 100% intellectual property ownership.

A

Written by Anjali

Senior Technical Architect & SEO Systems Engineer

Anjali is a Senior Technical Architect & SEO Systems Engineer focusing on technical SEO architecture, WCAG accessibility, Shopify OS 2.0 storefronts, and web security hardening.

Contextual Field Notes

Related Web Development & Architecture Notes

View all engineering notes
Questions & Details

Key Takeaways & FAQs

Preventing PostgreSQL metadata table locks and deployment outages during rolling releases by splitting column renames and constraints into safe backwards-compatible phases.

Direct Engineering Engagement

Have a project in mind? Send us the details and we’ll reply within a day.

We engineer scalable cloud architecture, custom web platforms, and Salesforce CRM systems for growing companies and global enterprises.

1-Day Reply SLADirect senior architect review
Strict Mutual NDA100% IP confidentiality
Full Code OwnershipNo vendor lock-in
Fixed Sprint BillingPredictable milestones