Best Practices for Safe PostgreSQL Schema Migrations
When you change a PostgreSQL schema in production, the database has to keep serving queries while you roll out new application versions…
A database migration changes how the database stores data, for example, by adding a column. It can also change data already stored there. A deployment slows when its release pipeline waits for a migration to finish or when the migration makes the database wait before serving application requests.
Keep changes that must happen during deployment short. Make sure the database works with both the old and new application versions, and run large data updates as controlled background jobs. This reduces release delays and gives operators time to monitor, pause, and check the changes.
A short SQL statement can trigger hours of database work. Building an index, a structure that helps the database find data, may require scanning a large table. A schema change may rewrite stored rows, and a constraint check may inspect existing data. Before scheduling a migration, find out whether the database only changes metadata or scans rows. Also check whether it copies or rewrites data, builds an index, or updates many records.
The database’s behavior varies with its engine and version, as well as the data and workload. Test with data similar to production, then check the database documentation for how the operation runs and what it locks. A test on a small staging table does not show how long the same operation will take on a large production table.
Locks also affect how long a migration takes. A lock controls which other database work can proceed while a transaction or schema change runs. A migration may wait for a long-running transaction to release a lock. Once the migration is waiting for that lock, later requests may have to wait too. Check the migration’s lock-wait duration, look for long-running transactions, and track its total duration. For more context, see how databases manage concurrent transactions.
A deployment pipeline may wait for a migration before starting, finishing, or declaring a release successful. That wait delays the release even if application queries can continue during the migration. GitLab’s query performance guidance explains that some operations let database activity continue while still holding up the update.
In the deployment-critical step, make only the schema changes the new code needs right away. When the code can safely run before they finish, run large backfills separately. Run validation and cleanup separately under that same condition. Set a time limit for each kind of migration. If a migration exceeds that limit, change how it runs. GitLab’s migration style guide gives limits for its own organization. Treat its timings as examples for setting local policy, not as universally safe limits.
A release can fail if old application instances still use a column that a migration removes, or if new code expects a column the database does not have yet. During rollout and rollback, make sure both the old and new code versions work with the database schema. See guidance on keeping code compatible with the schema.
To replace a column, first add the new column and keep the old one. Deploy code that supports both columns and, if needed, writes to both. Then update existing rows in a backfill, a job that changes data already in the database. Check that the new values are correct, then change the code to read from the new column. Remove the old column in a later release, after the code stops using it.
This expand-and-contract approach spreads a change across releases, so the old schema stays in place for a while. Each deployment then has a safer point to switch between versions. Take care when code writes to both columns: one write can succeed while the other fails, leaving different values. Monitor these writes and fix any mismatches before relying on the new column. See this related explanation of expand-and-contract migrations.
Updating a large table in one long transaction can use substantial database resources, generate many writes, and delay other work. Divide the backfill into batches with a set limit on the amount of work in each one. Record progress and make the job restartable so an interruption does not force it to start over.
Plan for rows that change while the backfill runs. Track which rows the job has processed, then run another pass to catch up or verify the data before new code depends on it. GitLab’s batched background migration guidance explains how to pace long-running work and respond to database health. A scheduled background migration may still be running, so check its status and confirm how the database marks it complete.
Set conditions for pausing or slowing the job based on signals such as database latency, write load, replication lag, and connection use. GitHub’s May 2026 availability report described a migration that ran for several hours and later contributed to database connections reaching capacity as traffic neared a weekly peak. GitHub said it planned controls to pause migration work when database health crossed defined thresholds. The incident shows why migration work should respond to current load, but it does not set a threshold for every database.
“Online” describes what happens while an operation runs. It does not mean the operation finishes quickly or has no cost. An online operation can still use substantial CPU or disk resources, wait for locks, or hold up the deployment pipeline.
In PostgreSQL, a regular index build blocks writes until it finishes. CREATE INDEX CONCURRENTLY lets writes continue, but PostgreSQL says it scans the table twice, takes longer, and waits for relevant transactions. You cannot run it inside a transaction block. If it fails, it can leave an invalid index that adds work to later updates. Check the index state, then remove or rebuild the index as appropriate. The PostgreSQL index documentation describes these tradeoffs. For some check and foreign-key constraints, PostgreSQL also lets you add the constraint without checking existing rows right away, then validate it separately. Validation still scans the data, as explained in the ALTER TABLE documentation.
MySQL’s InnoDB engine uses different online DDL algorithms and locking behaviors for different operations. Check the MySQL online DDL documentation to see what the specific operation supports. Online DDL can still need a brief exclusive lock on table metadata. A long-running transaction can delay that lock, and the pending change can then make later transactions wait. MySQL describes this in its online DDL limitations. Test the operation you plan to use and watch for lock waits and replication effects.
A success or failure status does not show the full effect of a migration. Track how long it runs, how long it waits for locks, and how long the deployment waits for it. While it runs, watch database latency, connection use, write activity, and replication lag. Record failures and retries. Before the application relies on the results, confirm that each background job has finished.
A concurrent index build reduces the time writes are blocked, but takes longer and uses more resources. Moving a backfill out of the deployment-critical step shortens the release, but the data update takes longer to finish. Measure both outcomes so a faster deployment does not hide database strain or unfinished data work.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com