惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
雷峰网
雷峰网
罗磊的独立博客
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
博客园 - 司徒正美
Last Week in AI
Last Week in AI
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
宝玉的分享
宝玉的分享

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Expand-Contract Pattern vs Blue-Green Deployment for Post...
SoftwareDevs · 2026-05-01 · via DEV Community

SoftwareDevs mvpfactory.io

---
title: "Expand-Contract vs Blue-Green: PostgreSQL Zero-Downtime Migrations"
published: true
description: "Compare expand-contract pattern and blue-green deployment for PostgreSQL schema migrations. Learn when to use each strategy for zero-downtime database changes."
tags: postgresql, architecture, devops, cloud
canonical_url: https://blog.mvpfactory.co/expand-contract-vs-blue-green-postgresql-zero-downtime-migrations
---

## What You Will Learn

By the end of this tutorial, you will understand two battle-tested strategies for zero-downtime PostgreSQL schema migrations — expand-contract and blue-green — and know exactly when to reach for each one. I will walk you through working SQL examples you can apply to your own tables today.

## Prerequisites

- A running PostgreSQL 11+ instance (critical for non-blocking `ADD COLUMN` behavior)
- Familiarity with `ALTER TABLE`, indexing, and basic replication concepts
- A deployment pipeline that supports rolling releases

## Step 1: Understand the Real Problem

Most teams get this wrong: they treat zero-downtime migrations as a deployment problem when it is actually a **schema compatibility problem**. Your application code and database schema must remain compatible across at least two versions simultaneously — the version being deployed and the version being replaced.

Here is the gotcha that will save you hours: an `ALTER TABLE ... ADD COLUMN` with a default value acquires an `ACCESS EXCLUSIVE` lock on versions before PostgreSQL 11. Even brief lock contention on a table with millions of rows can cascade into connection pool exhaustion within seconds.

## Step 2: Expand-Contract — Three Safe Phases

Let me show you a pattern I use in every project. Expand-contract splits one breaking change into three non-breaking phases:

**Phase 1 — Expand:** Add the new structure alongside the old.

Enter fullscreen mode Exit fullscreen mode


sql
-- Migration 1: Add new column (non-blocking in PG 11+)
ALTER TABLE users ADD COLUMN email_normalized VARCHAR(255);

-- Backfill in batches to avoid long-running transactions
UPDATE users SET email_normalized = LOWER(email)
WHERE id BETWEEN 1 AND 10000;


**Phase 2 — Migrate:** Deploy code that writes to both columns, reads from the new one. Create your index without blocking writes:

Enter fullscreen mode Exit fullscreen mode


sql
-- Non-blocking index creation (cannot run inside a transaction block)
CREATE INDEX CONCURRENTLY idx_users_email_normalized
ON users (email_normalized);


**Phase 3 — Contract:** Once all code reads exclusively from the new column, drop the old one.

Enter fullscreen mode Exit fullscreen mode


sql
-- Migration 3: Remove old column (after full rollout)
ALTER TABLE users DROP COLUMN email;


Each phase is independently deployable and reversible. If Phase 2 reveals a bug, roll back the application code — the old column still exists with valid data.

## Step 3: Blue-Green — Atomic Cutover

Blue-green maintains two identical environments. Green gets the new schema while blue continues serving traffic. Once validated, a load balancer switch routes traffic atomically.

Enter fullscreen mode Exit fullscreen mode


plaintext
Blue (active): users table → [id, email]
Green (standby): users table → [id, email_normalized]

Cutover: Route traffic blue → green
Rollback: Route traffic green → blue


The hard part is state synchronization. Databases accumulate writes, so you need replication between blue and green schemas. PostgreSQL's logical replication can synchronize data, but it does not replicate DDL changes, and schema differences require careful column mapping.

## Step 4: Choose Your Strategy

| Criteria | Expand-Contract | Blue-Green |
|---|---|---|
| Infrastructure cost | Single database | Two full environments |
| Rollback speed | Instant (old column exists) | Instant (switch routing) |
| Data synchronization | Not required | Required (replication lag risk) |
| Best table size | Any (batched backfills) | Small-medium tables |
| Team coordination | Low (phased rollout) | High (synchronized cutover) |

**Choose expand-contract** when you have large tables, run a single PostgreSQL instance, deploy continuously, or need to rename columns and change types.

**Choose blue-green** when migrating across database versions (e.g., PostgreSQL 14 to 16), coupling schema changes with major application rewrites, or when tables are small enough for a full data copy.

## Step 5: Protect Your Backfills

Here is the minimal setup to get concurrent backfills working safely across multiple instances:

Enter fullscreen mode Exit fullscreen mode


sql
SELECT pg_advisory_lock(hashtext('backfill_users_email'));
-- batch update here
SELECT pg_advisory_unlock(hashtext('backfill_users_email'));


Advisory locks prevent overlapping backfill jobs in multi-instance deployments.

## Gotchas

- **Missing `CONCURRENTLY` keyword:** A regular `CREATE INDEX` locks writes. I have seen teams spend weeks debating strategies when the real risk was this one missing keyword.
- **PostgreSQL version matters:** `ADD COLUMN` with a non-volatile default is only non-blocking in PostgreSQL 11+. Before that, it rewrites the entire table. Always check your version.
- **Transaction blocks:** `CREATE INDEX CONCURRENTLY` cannot run inside a transaction block. Plan your migration tooling accordingly — most frameworks wrap migrations in transactions by default.
- **Logical replication limitations:** If you pursue blue-green at the database level, remember that logical replication does not replicate DDL changes.

## Conclusion

Default to expand-contract for column-level PostgreSQL changes. It requires no extra infrastructure, handles tables of any size through batched operations, and each phase is independently reversible. Reserve blue-green for infrastructure-level migrations — major version upgrades, engine switches, or coordinated rewrites that justify parallel environments.

Invest in migration tooling that enforces safety. Tools like `pg-osc`, `pgroll`, or custom frameworks that check for lock-acquiring statements, enforce concurrent index creation, and batch backfills will prevent more outages than any deployment strategy alone.

The docs do not mention this, but the combination of advisory locks, concurrent indexing, and phased deploys will handle 90% of what production PostgreSQL throws at you.

Enter fullscreen mode Exit fullscreen mode