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
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:
In PostgreSQL, most ALTER TABLE operations require an ACCESS EXCLUSIVE lock.
This lock:
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.
For high-traffic SaaS and e-commerce platforms:
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
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:
At Mafiree, pg_osc has become a standard component in our production change management framework for large PostgreSQL deployments.
The migration flow:
Only the final swap requires a short lock — typically milliseconds to seconds
Because:
This makes pg_osc ideal for large, active production tables — especially those managed under strict SLA environments like the ones Mafiree supports.
pg_repack is designed primarily for reclaiming space and rebuilding tables/indexes online.
| Feature | pg_osc | pg_repack |
|---|---|---|
| Schema changes | Yes | Limited |
| Shadow table | Yes | Yes |
| Index handling | Automatic | Rebuilt |
| Foreign keys | Managed | Limited |
| Custom copy SQL | Yes | No |
At Mafiree, we typically use:
pgroll uses an expand-and-contract migration strategy.
| Method | pg_osc | pgroll |
|---|---|---|
| Strategy | Shadow table swap | Dual schema versioning |
| Application changes required | No | Often yes |
| Backward compatibility | Limited | Strong |
| Complexity | Moderate | High |
For enterprises requiring application-version compatibility across deployments, pgroll may be appropriate. For operational simplicity and controlled production changes, Mafiree commonly recommends pg_osc.
Native ALTER TABLE in PostgreSQL:
In high-availability environments, we rarely recommend native ALTER for large tables without an online strategy.
At Mafiree, we also validate WAL volume and replication capacity before starting.
| 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 |
| 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 |
| 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 |
| 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.
Using --copy-statement, we often perform:
| 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.
pg_osc:
On 100GB+ tables:
We always simulate migration in staging before production execution.
Monitor:
At Mafiree, we use structured runbooks to monitor migrations in real time.
Across multiple enterprise PostgreSQL deployments, Mafiree has:
Choose Mafiree when:
We specialize in PostgreSQL schema changes pg_osc, replication strategy, performance tuning, and high-availability architecture.
Contact: sales@mafiree.com
With the right strategy — and the right expertise — schema changes don’t have to mean downtime.
Miru IT Park, Vallankumaranvillai,
Nagercoil, Tamilnadu - 629 002.
Unit 303, Vanguard Rise,
5th Main, Konena Agrahara,
Old Airport Road, Bangalore - 560 017.
Call: +91 6383016411
Email: sales@mafiree.com