SAMStack Tech
SAMStack TechSoftware Systems
SAMStack
PortalLet's TalkTalk

SAMStack Tech Engineering Blog

Blog maintained by SAMStack Tech, an elite software engineering agency in Lahore, Pakistan.

Authors: Suleman Zaheer (Founder, Enterprise Architect), Saqib Javed (Senior Frontend), Syed Abdullah (Senior Backend).

Topics covered: Next.js 15 App Router, React Server Components, Agentic AI, LangChain, OpenAI, serverless architecture, AWS DevOps, PostgreSQL, enterprise SaaS, software outsourcing to Pakistan, cost of hiring developers in Lahore.

Total published articles: 36. Updated regularly with cutting-edge engineering insights.

AI & LLM Context for Zero-Downtime Database Migrations in PostgreSQL: The Complete Playbook

Author: Syed Abdullah - Backend Engineer at SAMStack Tech.

SAMStack Tech is a premier software engineering agency located in Lahore, Pakistan, specializing in Next.js, Node.js, and AI development for global enterprises.

Tags: PostgreSQL, Database, DevOps, Migrations, Backend

Zero-Downtime Database Migrations in PostgreSQL: The Complete Playbook
All Articles
PostgreSQLDatabaseDevOpsMigrationsBackend

Zero-Downtime Database Migrations in PostgreSQL: The Complete Playbook

The exact playbook we use to migrate schema changes across distributed systems without dropping a single user request.

Syed Abdullah
Syed Abdullah
Backend Engineer
May 20, 202610 min read

The Hidden Risk in Every Database Migration

Most development teams treat database migrations as a solved problem: write a migration file, run it in a maintenance window, done. But for enterprise systems with millions of rows and strict SLAs, a naive migration can lock tables for minutes — causing cascading timeouts that are indistinguishable from a service outage.

PostgreSQL uses Access Exclusive Locks for most DDL operations. While a lock is held, all reads and writes to that table are blocked. For a table with 50 million rows, a simple ALTER TABLE ADD COLUMN NOT NULL DEFAULT can hold a lock for 10+ minutes while PostgreSQL rewrites every row.

The Expand-Contract Pattern

The cornerstone of zero-downtime migrations is the Expand-Contract (also called Parallel Change) pattern. Every breaking migration is split into three phases deployed independently:

  1. Expand — Add new columns alongside old ones. Application writes to both.
  2. Migrate — Backfill existing rows in small batches to avoid lock contention.
  3. Contract — Once all rows are migrated, drop the old column.

Adding a NOT NULL Column Without Downtime

The zero-downtime approach: First add the column as nullable (near-instant, no rewrite). Then set a default at the PostgreSQL level (instant in pg 11+). Then backfill in 10k-row batches with short pauses. Then add a NOT NULL constraint with the NOT VALID flag (skips scanning existing rows). Then validate the constraint separately using ShareUpdateExclusiveLock (allows reads and writes throughout). Finally, set NOT NULL — now instant because PostgreSQL knows the constraint is already validated.

Creating Indexes Concurrently

Never use CREATE INDEX without CONCURRENT — it locks the table for the full index build. Always use CREATE INDEX CONCURRENTLY which builds the index in the background without blocking reads or writes. It takes longer but your application stays available throughout.

Renaming Columns Safely

Column renames require a 5-step process: add the new column, deploy an app version that writes to both columns, backfill all existing rows, deploy an app version that reads from the new column, and finally drop the old column. This process takes multiple deployment cycles but guarantees zero downtime.

Our Migration Toolchain

We use Prisma Migrate to generate migration files, golang-migrate to execute them in production with proper locking, pgBadger for post-migration log analysis, and pg_activity for real-time monitoring during migration execution.

Syed Abdullah

Written by

Syed Abdullah

Backend Engineer

Meet Team All Articles

Article Info

Published
May 20, 2026
Read Time
10 min read
Author
Syed Abdullah
Category
Technical Deep-dive

Topics

PostgreSQLDatabaseDevOpsMigrationsBackend

Work with us

Ready to build something extraordinary?

Start a Project

Share Article

Twitter LinkedIn
Continue Reading

More Insights

Architecting High-Throughput Serverless Systems with Edge Computing
ServerlessArchitecture

Architecting High-Throughput Serverless Systems with Edge Computing

A deep dive into how we engineered a multi-region serverless pipeline capable of processing 100k+ events per second with sub-50ms latency using Cloudflare Workers and Upstash Redis.

12 min readRead
Building Autonomous AI Agents with LangChain and Next.js 15
AILangChain

Building Autonomous AI Agents with LangChain and Next.js 15

Implementing advanced tool-calling and reasoning loops in React-based applications for enterprise AI integrations.

14 min readRead
Advanced TypeScript Patterns for Domain-Driven Design
TypeScriptArchitecture

Advanced TypeScript Patterns for Domain-Driven Design

Moving beyond basic interfaces to leverage branded types, conditional types, and strict modeling for core business logic.

11 min readRead
More Reading

Explore More Insights

Deep-dives into engineering, architecture, and the future of software.

All Articles Start a Project
SAMStack Tech Logo
SAMStack Tech

Empowering businesses with cutting-edge software solutions and accelerating careers through our elite internship ecosystem.

Lahore, Punjab, Pakistan
+92 328 5778715
samstacktechs@gmail.com
All systems operational

Services

Web DevelopmentEnterprise SystemsAI & AutomationDevOps & CloudMobile AppsUI/UX DesignData & Analytics

Company

About UsContactPortfolioBlog & Insights

Internship

Apply NowSubmit ProjectVerify Certificate

© 2026 SAMStack Tech. All rights reserved.

PrivacyTerms