Mafiree logo
  • About
  • Services
  • Blogs
  • Careers
  • Products
    • orbit logo Orbit
    • streamer logo Xstreami
  • Contact
Schedule a Call
Menu
  • About
  • Services
  • Blogs
  • Careers
  • Products
    • orbit logo Orbit
    • streamer logo Xstreami
  • Contact
  • Schedule a Call
Database
Database Database Managed Services
MySQL MySQL
MySQL Consulting Services
MySQL Migration Services
MySQL Optimization & Query Tuning
MySQL Database Administration
MySQL Backup & Recovery
MySQL Security & Maintenance
MySQL Cloud Services (AWS RDS, Aurora, Google Cloud SQL, Azure)
MySQL for Ecommerce
MySQL High Availability & Replication
MongoDB MongoDB
MongoDB Consulting Services
MongoDB Migration Services
MongoDB Optimization & Query Tuning
MongoDB Database Administration
MongoDB Backup & Recovery
MongoDB Security & Maintenance
MongoDB Cloud (Atlas)
MongoDB Solutions by Industry
MongoDB High Availability & Replication
PostgreSQL PostgreSQL
PostgreSQL Consulting
PostgreSQL Migration & Upgrades
Performance Tuning & Query Optimization
PostgreSQL Administration & Managed Services
High Availability, Clustering & Replication
PostgreSQL Backup, Recovery & Disaster Planning
PostgreSQL Security, Compliance & Auditing
PostgreSQL for Analytics & Data Warehousing
PostgreSQL on Cloud & Containers
PostgreSQL Extensions & Open-Source Integrations
PostgreSQL for Every Industry
SQL Server MSSQL
MSSQL Consulting Services
MSSQL Migration Services
MSSQL Optimization & Query Tuning Services
MSSQL Database Administration Services
MSSQL Backup & Recovery Services
MSSQL High Availability & Replication Services
MSSQL Security & Compliance Services
MSSQL Performance Monitoring & Health Checks
MSSQL Solutions by Industry
Aerospike Aerospike
Aerospike Consulting Services
Aerospike Migration Services
Aerospike Performance Optimization & Tuning
Aerospike Database Administration
Aerospike Backup & Recovery
Aerospike High Availability
Aerospike Cloud & Hybrid Deployments
Aerospike for Real-Time Applications (AdTech, FinTech, Retail, IoT)
Analytics DB
Analytics DB Analytics DB Services
Clickhouse Clickhouse
ClickHouse Consulting Services
ClickHouse Migration Services
ClickHouse Optimization & Query Tuning
ClickHouse Database Administration
ClickHouse Backup & Recovery
ClickHouse Security & Maintenance
ClickHouse Cloud Services (ClickHouse Cloud, AWS, GCP, Azure)
ClickHouse Solutions by Industry
ClickHouse High Availability & Replication
TiDB TiDB
TiDB Consulting & Architecture Planning
TiDB Administration & Maintenance
TiDB Security and Privacy Maintenance
TiDB Performance & Query Optimization
TiDB Migration Services
TiDB Backup & Disaster Recovery
TiDB High Availability Solutions
TiDB Solutions by Industry
TiDB Cloud Services
ScyllaDB ScyllaDB
ScyllaDB Consulting & Architecture Planning
ScyllaDB Administration & Maintenance
ScyllaDB Security and Privacy Maintenance
ScyllaDB Performance & Query Optimization
ScyllaDB Migration Services
ScyllaDB Backup & Disaster Recovery
ScyllaDB High Availability Solutions
ScyllaDB Solutions by Industry
ScyllaDB Cloud Services
DevOps
DevOps DevOps Services
Version Control Version Control
Kubernetes Kubernetes
Infrastructure Infrastructure Management
Web Servers Web Servers
Networking
Networking Networking Services
Basic Basic
Advanced Advanced
MySQL MySQL
MongoDB MongoDB
PostgreSQL PostgreSQL
MSSQL MSSQL
Aerospike Aerospike
Clickhouse Clickhouse
TiDB TiDB
ScyllaDB ScyllaDB
Version Control Version Control
Kubernetes Kubernetes
Infrastructure Infrastructure Management
Web Servers Web Servers
Basic Basic
Advanced Advanced
MySQL Consulting Services
MySQL Migration Services
MySQL Optimization & Query Tuning
MySQL Database Administration
MySQL Backup & Recovery
MySQL Security & Maintenance
MySQL Cloud Services (AWS RDS, Aurora, Google Cloud SQL, Azure)
MySQL for Ecommerce
MySQL High Availability & Replication
MongoDB Consulting Services
MongoDB Migration Services
MongoDB Optimization & Query Tuning
MongoDB Database Administration
MongoDB Backup & Recovery
MongoDB Security & Maintenance
MongoDB Cloud (Atlas)
MongoDB Solutions by Industry
MongoDB High Availability & Replication
PostgreSQL Consulting
PostgreSQL Migration & Upgrades
Performance Tuning & Query Optimization
PostgreSQL Administration & Managed Services
High Availability, Clustering & Replication
PostgreSQL Backup, Recovery & Disaster Planning
PostgreSQL Security, Compliance & Auditing
PostgreSQL for Analytics & Data Warehousing
PostgreSQL on Cloud & Containers
PostgreSQL Extensions & Open-Source Integrations
PostgreSQL for Every Industry
MSSQL Consulting Services
MSSQL Migration Services
MSSQL Optimization & Query Tuning Services
MSSQL Database Administration Services
MSSQL Backup & Recovery Services
MSSQL High Availability & Replication Services
MSSQL Security & Compliance Services
MSSQL Performance Monitoring & Health Checks
MSSQL Solutions by Industry
Aerospike Consulting Services
Aerospike Migration Services
Aerospike Performance Optimization & Tuning
Aerospike Database Administration
Aerospike Backup & Recovery
Aerospike High Availability
Aerospike Cloud & Hybrid Deployments
Aerospike for Real-Time Applications (AdTech, FinTech, Retail, IoT)
ClickHouse Consulting Services
ClickHouse Migration Services
ClickHouse Optimization & Query Tuning
ClickHouse Database Administration
ClickHouse Backup & Recovery
ClickHouse Security & Maintenance
ClickHouse Cloud Services (ClickHouse Cloud, AWS, GCP, Azure)
ClickHouse Solutions by Industry
ClickHouse High Availability & Replication
TiDB Consulting & Architecture Planning
TiDB Administration & Maintenance
TiDB Security and Privacy Maintenance
TiDB Performance & Query Optimization
TiDB Migration Services
TiDB Backup & Disaster Recovery
TiDB High Availability Solutions
TiDB Solutions by Industry
TiDB Cloud Services
ScyllaDB Consulting & Architecture Planning
ScyllaDB Administration & Maintenance
ScyllaDB Security and Privacy Maintenance
ScyllaDB Performance & Query Optimization
ScyllaDB Migration Services
ScyllaDB Backup & Disaster Recovery
ScyllaDB High Availability Solutions
ScyllaDB Solutions by Industry
ScyllaDB Cloud Services
  1. Home
  2. > Blogs
  3. > PostgreSQL
  4. > PostgreSQL Schema Changes pg_osc: Zero-Downtime Migrations for Production Systems

PostgreSQL Schema Changes pg_osc: Zero-Downtime Migrations for Production Systems

Altering large tables in PostgreSQL can cause locks, performance issues, and downtime in production environments. This blog explains how pg_osc (PostgreSQL Online Schema Change) enables safe schema modifications using a shadow table approach, allowing applications to continue operating during migrations. The article covers how pg_osc works, compares it with alternatives like pg_repack, explores real-world use cases, and provides practical steps for implementing online schema changes in production. It also highlights best practices used by Mafiree to perform reliable zero-downtime PostgreSQL schema migrations in high-traffic environments.

Jethish March 19, 2026

Subscribe for email updates

Managing PostgreSQL schema changes pg_osc in large-scale production databases is one of the most challenging tasks for database teams. A simple ALTER TABLE on a 100GB+ table can block writes, stall applications, and create costly downtime.

 

At Mafiree, we regularly support mission-critical PostgreSQL environments where even a few seconds of blocking can impact revenue, customer experience, and SLAs. Over the years, we’ve implemented safe, zero-downtime schema migration strategies using pg_osc for SaaS platforms, e-commerce systems, fintech workloads, and enterprise reporting databases.

 

This guide explains:

  • Why schema changes cause downtime
  • How pg_osc (PostgreSQL Online Schema Change) solves the problem
  • How it compares to alternatives like pg_repack and pgroll
  • Production best practices we apply at Mafiree

The Problem: Why PostgreSQL Schema Changes Cause Downtime

Understanding PostgreSQL Locking During Schema Changes

In PostgreSQL, most ALTER TABLE operations require an ACCESS EXCLUSIVE lock.

 

This lock:

  • Blocks reads and writes
  • Queues application queries
  • Can cascade into connection pile-ups
  • Impacts replication lag

In production systems we manage at Mafiree, we’ve seen minor schema changes trigger cascading slowdowns because of lock waits — especially in high-concurrency environments.

 

Real-World Cost of Downtime

For high-traffic SaaS and e-commerce platforms:

  • 5–10 minutes of blocking can mean lost revenue
  • Queue buildup may require restarts
  • Background workers may fail
  • Customer trust may be impacted

In one real client scenario handled by Mafiree, a 70GB table rewrite during business hours caused replication lag to spike significantly. After implementing pg_osc-based migration processes, similar changes were executed with no customer-visible impact

 

What is pg_osc? (Shadow Table Approach Explained)

pg_osc (PostgreSQL Online Schema Change) is an open-source tool that enables near zero-downtime schema modifications using a shadow table approach.

 

Instead of modifying the live table directly, it:

  1. Creates a shadow table with the new schema
  2. Copies data in batches
  3. Tracks changes via triggers
  4. Swaps tables with minimal locking

At Mafiree, pg_osc has become a standard component in our production change management framework for large PostgreSQL deployments.

 

How pg_osc Works: The Shadow Table Method

The migration flow:

  1. Create audit triggers on the original table
  2. Build shadow table with desired schema
  3. Copy existing rows
  4. Replay changes from audit log
  5. Acquire brief lock
  6. Swap tables
  7. Drop or retain old table

Only the final swap requires a short lock — typically milliseconds to seconds

 

Why Shadow Tables Enable Zero-Downtime Changes

Because:

  • Data copying happens without blocking writes
  • Changes are continuously synchronized
  • The final swap is metadata-level

This makes pg_osc ideal for large, active production tables — especially those managed under strict SLA environments like the ones Mafiree supports.

 

pg_osc vs Alternatives: pgroll vs pg_repack vs Native PostgreSQL

pg_osc vs pg_repack: Which One to Choose?

pg_repack is designed primarily for reclaiming space and rebuilding tables/indexes online.

Featurepg_oscpg_repack
Schema changesYesLimited
Shadow tableYesYes
Index handlingAutomaticRebuilt
Foreign keysManagedLimited
Custom copy SQLYesNo

At Mafiree, we typically use:

  • pg_repack for bloat cleanup
  • pg_osc for structural production schema changes

pg_osc vs pgroll: The Expand & Contract Approach

pgroll uses an expand-and-contract migration strategy.

Methodpg_oscpgroll
StrategyShadow table swapDual schema versioning
Application changes requiredNoOften yes
Backward compatibilityLimitedStrong
ComplexityModerateHigh

For enterprises requiring application-version compatibility across deployments, pgroll may be appropriate. For operational simplicity and controlled production changes, Mafiree commonly recommends pg_osc.

 

pg_osc vs Native PostgreSQL ALTER TABLE

Native ALTER TABLE in PostgreSQL:

  • May rewrite entire tables
  • Requires ACCESS EXCLUSIVE locks
  • Blocks application traffic

In high-availability environments, we rarely recommend native ALTER for large tables without an online strategy.

 

Step-by-Step: Implementing pg_osc in Production

Installation & Prerequisites

  • pg-online-schema-change binary
  • Required PostgreSQL privileges
  • Adequate disk space (shadow table creation)

At Mafiree, we also validate WAL volume and replication capacity before starting.

 

Basic Syntax & Commands

PGPASSWORD='******' bundle exec ./bin/pg-online-schema-change perform -a "ALTER TABLE customers ADD COLUMN phone_number VARCHAR(15);" -s public -d mafiree -h localhost -u pgosc -p 5432 -v

Performing Multiple ALTER statements

PGPASSWORD='******' bundle exec ./bin/pg-online-schema-change perform -a "ALTER TABLE customers ADD COLUMN phone_number VARCHAR(15); ALTER TABLE customers RENAME COLUMN contact_email TO email;" -s public -d mafiree -h localhost -u pgosc -p 5432 -v

Performing an ALTER statement with drop option which will drop the optional table [--drop]

PGPASSWORD='******' bundle exec ./bin/pg-online-schema-change perform -a "ALTER TABLE customers ADD COLUMN phone_number VARCHAR(15);" -s public -d mafiree -h localhost -u pgosc -p 5432 -v --drop

Handling Locks: --wait-time & --kill-backends

PGPASSWORD='******' bundle exec ./bin/pg-online-schema-change perform -a "ALTER TABLE orders ALTER COLUMN total_amount TYPE NUMERIC(12, 2);" -s public -d mafiree -h localhost -u pgosc -p 5432 -v --kill-backends --wait-time-for-lock=5

Used carefully in production environments with clear operational approval.

 

Backfilling Data During Migration

Using --copy-statement, we often perform:

  • Data transformation
  • Tenant-aware enrichment
  • Historical normalization
PGPASSWORD='******' bundle exec ./bin/pg-online-schema-change perform -a "ALTER TABLE categories ADD COLUMN "tenant_id" VARCHAR;" -s public -d mafiree -h localhost -u pgosc -p 5432 -v --copy-statement "/home/mafiree/pg-osc/pgosc.sql"

This flexibility is especially useful in multi-tenant SaaS systems.

 

Index & Foreign Key Handling

pg_osc:

  • Replicates indexes
  • Handles foreign keys
  • Supports NOT VALID constraints
  • Preserves referential integrity

Key Features, Limitations & Workarounds

What pg_osc Can Do Well

  • Add/drop columns
  • Modify column types
  • Rename columns
  • Backfill data
  • Maintain availability
  • Reduce operational risk

What pg_osc Cannot Do

  • Complex partition restructures
  • Massive schema redesigns across dozens of dependent tables
  • Cross-database migrations

Performance Considerations on Large Tables

On 100GB+ tables:

  • Disk usage temporarily doubles
  • WAL generation increases
  • Replication lag must be monitored
  • I/O spikes possible

We always simulate migration in staging before production execution.

 

Production Best Practices & Safety Measures

Testing Schema Changes Before Production

  • Rehearse on staging
  • Benchmark execution
  • Validate constraints
  • Confirm application compatibility

Backup & Rollback Strategy

  • Take logical or physical backups
  • Avoid --drop initially
  • Validate row counts
  • Keep rollback window

Monitoring During Schema Changes

Monitor:

  • pg_stat_activity
  • Replication lag
  • WAL volume
  • I/O throughput

At Mafiree, we use structured runbooks to monitor migrations in real time.

 

Coordinating with Application Deployments

  • Avoid peak traffic
  • Ensure backward compatibility
  • Inform DevOps and product teams
  • Validate API and ORM mappings

Real-World Case Studies & Results

Across multiple enterprise PostgreSQL deployments, Mafiree has:

  • Migrated 200GB+ production tables
  • Reduced schema-change downtime risk to near zero
  • Improved operational confidence during large releases
  • Standardized safe migration pipelines

PostgreSQL Schema Change Without Downtime

 

When to Choose Mafiree's PostgreSQL Consulting

Choose Mafiree when:

  • You manage 50GB+ production tables
  • Your system operates 24/7
  • Downtime is unacceptable
  • You need expert-guided schema migration planning
  • You want guaranteed safe execution

We specialize in PostgreSQL schema changes pg_osc, replication strategy, performance tuning, and high-availability architecture.

 

Contact: sales@mafiree.com
 

Take Away

  1. Native PostgreSQL schema changes can cause downtime
  2. pg_osc enables near zero-downtime migrations
  3. Proper planning and monitoring are critical
  4. Enterprise-grade execution requires experience

With the right strategy — and the right expertise — schema changes don’t have to mean downtime.

 

FAQ

No, pg_osc currently does not support partitioned tables directly in PostgreSQL.
pg_osc creates a shadow table during the migration process, so additional disk space is required temporarily.
pg_osc is designed to perform schema migrations with minimal or near zero downtime.
Yes, pg_osc can run while the table is actively being used by applications.
If pg_osc fails during migration, the original table remains unchanged and fully intact.
pg_osc automatically recreates indexes and foreign key constraints on the shadow table during the migration process.
pg_repack and pg_osc serve different purposes in PostgreSQL. pg_repack is mainly used to remove table and index bloat and reorganize storage without long blocking operations. It focuses on maintenance rather than schema changes. pg_osc, on the other hand, is designed specifically for online schema modifications, such as adding columns, renaming columns, or changing column data types.
Yes, pg_osc is widely considered safe for production use when executed with proper planning. Mafiree typically runs schema migrations through staging tests, capacity checks, and monitoring to ensure a safe and smooth deployment in production environments.

Author Bio

Jethish

Jethish is a PostgreSQL DBA at Mafiree with expertise in building scalable, reliable, and high-performance database infrastructures. He focuses on PostgreSQL architecture, replication strategies, performance tuning, and high availability for mission-critical systems. Through his technical writing, he shares clear, practical insights on database internals, replication choices, load balancing, and cross-database integrations that help engineers and DBAs tackle real-world data challenges.

Leave a Comment

Related Blogs

AWS Database Storage Optimization: How We Reclaimed 3.6 TB and Cut Costs in Half

A client came to us with a classic AWS database storage optimization problem: 15.2 TB allocated, less than a third actually in use — and a bill that kept growing regardless. Within one week, Mafiree had reclaimed 3.6 TB, validated a safe path to cut allocation nearly in half, and executed a zero-downtime migration. Here's the full story.

  50 views
PostgreSQL Connection Pooling: PgBouncer vs Odyssey – Performance & Configuration

PostgreSQL uses a process-per-connection model, which can limit scalability in high-traffic environments. Connection poolers help manage this challenge by reusing database connections efficiently. This blog compares PgBouncer and Odyssey, two popular PostgreSQL connection poolers, highlighting their architecture, performance characteristics, configuration differences, and ideal use cases. It helps organizations choose the right pooling solution based on workload scale, complexity, and operational requirements.

  1762 views
8 Enhancing Features in PostgreSQL 18

PostgreSQL 18: Efficiency, security, and reliability, all in one upgrade

  4071 views
Optimizing PostgreSQL Queries with Functional Indexes – A Real-World Case Study

Cutting Query Time from 10 Minutes to Under 1 Second – How Functional Indexes Helped Us Optimize Aurora PostgreSQL and Stabilize CPU Performance.

  2728 views
Mastering PostgreSQL Meta-Commands: The Ultimate psql Cheat Sheet

Why memorize SQL queries when \d, \l, and \dx do the heavy lifting? Learn the power of PostgreSQL’s psql meta-commands today.

  9668 views

Subscribe for email updates

Get in touch with us

Highlights

More than 6000 Servers Monitored

Happy Clients

Certified DBAs

24 x 7 x 365 Support

PCI

Database Services

MySQL MongoDB PostgreSQL SQL Server Aerospike Clickhouse TiDB MariaDB Columnstore

Quick Links

Careers Blog Contact Privacy Policy Disclaimer Policy

Contacts

Linkedin Mafiree Facebook Mafiree Twitter Mafiree

Nagercoil Office

Miru IT Park, Vallankumaranvillai,

Nagercoil, Tamilnadu - 629 002.

Bangalore Office

Unit 303, Vanguard Rise,

5th Main, Konena Agrahara,

Old Airport Road, Bangalore - 560 017.

Call: +91 6383016411

Email: sales@mafiree.com


Copyright © - All Rights Reserved - Mafiree