Imagine deploying a new version of an application that expects a column named display_name, while some older instances still read full_name. If a migration renames or removes the old column immediately, requests handled by those older instances can fail. A rolling deployment makes this overlap normal, not exceptional.

The safer approach is to make schema changes in stages. First add the new structure while keeping the old one working. Then deploy code that can tolerate both. Move data and traffic gradually. Only remove the old structure after no running code depends on it.

The expand-and-contract principle

Expand means adding a compatible schema shape without breaking existing code. Contract means removing obsolete structures only after every application version and background worker has stopped using them.

This is not a magic zero-downtime switch. Locks, long-running transactions, slow backfills, connection pools and deployment mistakes can still cause an outage. The method reduces the risk of incompatible changes by separating them into reversible steps.

Example: renaming a customer column

Suppose a table has customers.full_name and the application is moving to customers.display_name. Do not rename the column in one migration while deploying code that only knows the new name. Use a staged rollout instead.

1. Expand the schema

ALTER TABLE customers
ADD COLUMN display_name text;

At this point the old column remains. Check the exact lock behavior for your PostgreSQL version and table shape before running any DDL on a busy production table. Even a short metadata operation can wait behind a long transaction and then create a queue of blocked queries.

2. Deploy code that supports both columns

Release application code that continues to read full_name safely, but can also write the new value. During a rolling deployment, old and new application instances may coexist, so compatibility must work in both directions. A common strategy is to temporarily write both columns from the application layer, with one clearly designated source of truth.

Avoid having two independent values silently diverge. Define which field wins, log mismatches, and make the transition observable.

3. Backfill existing rows in batches

UPDATE customers
SET display_name = full_name
WHERE display_name IS NULL
  AND id >= 1
  AND id < 1001;

Repeat over bounded primary-key ranges, monitoring execution time, replication lag where applicable, lock waits and database load. The numbers above are an illustration, not a universal batch size. Choose a batch size based on measured impact. Make the operation resumable so a failed worker can continue without restarting the entire table.

For large tables, avoid one enormous transaction. Smaller batches limit how much work is held in a single transaction and make progress easier to observe. Ensure new writes are handled while the backfill runs, otherwise rows changed after the backfill passes them can become inconsistent.

4. Verify before switching reads

Measure how many rows remain null and compare old and new values using checks that match your data model. Investigate mismatches rather than overwriting them blindly. Only after the backfill and ongoing writes are consistent should the application begin reading the new column as authoritative.

5. Contract in a later release

Deploy code that no longer reads or writes full_name. Wait until old web instances, scheduled jobs, queue workers, scripts and one-off admin tools are all updated. Then, in a separate migration and maintenance window appropriate to your risk, remove the old column.

ALTER TABLE customers
DROP COLUMN full_name;

Do not combine this cleanup with the first deployment. Keeping the old column for an extra release is usually cheaper than discovering that an overlooked worker still needs it.

Indexes and constraints need their own rollout plan

Adding an index to a live PostgreSQL table can interfere with writes. PostgreSQL supports CREATE INDEX CONCURRENTLY, which avoids locking out ordinary inserts, updates and deletes for the full build, but it takes more work, performs multiple scans and cannot run inside a transaction block. A failed concurrent build can leave an invalid index that needs inspection and cleanup.

CREATE INDEX CONCURRENTLY
customers_display_name_idx
ON customers (display_name);

Treat this as an operational migration, not just another line in a transactional migration file. Check whether your migration tool wraps scripts in a transaction and follow its guidance for non-transactional statements.

For constraints, PostgreSQL can add certain foreign-key or check constraints as NOT VALID and validate existing rows separately with VALIDATE CONSTRAINT. That can reduce the disruptive part of the initial change, but it does not remove the need to understand lock levels and existing data quality.

A deployment sequence you can reuse

  1. Read the migration plan and identify every application version that may overlap.
  2. Add new nullable columns, tables or indexes without removing old structures.
  3. Deploy code that tolerates the old and expanded schema.
  4. Start a resumable backfill and monitor database load and errors.
  5. Verify row counts, nulls, mismatches and application metrics.
  6. Switch reads to the new representation and observe it under real traffic.
  7. Deploy code that no longer references the old schema.
  8. Remove obsolete columns or constraints in a later release.

Plan rollback before the migration runs

Application rollback is not always database rollback. Once new code has written data in a new format, reverting the application may not restore the old assumptions. During the compatibility window, keep both versions usable where possible, preserve old data until verification is complete, and document the point after which a rollback requires a forward fix instead.

Test the migration on a realistic copy of production data. Include the largest tables, concurrent writes, long-running transactions, background workers and a partially completed backfill. A migration that succeeds on an empty development database proves very little about its production behavior.

Practical rule

Make additive changes first, move data in observable batches, switch application behavior only after verification, and delete old schema only after old code is gone.

Pre-deployment checklist

  • Have you checked the PostgreSQL lock level and whether the operation rewrites the table?
  • Can old and new application instances both run against the expanded schema?
  • Can the backfill pause, resume and safely repeat?
  • Are concurrent writes handled during the backfill?
  • Do you have metrics for locks, query latency, errors and replication lag?
  • Is the rollback path documented, including when a forward fix is required?
  • Are background workers and scheduled jobs included in the compatibility check?

Safer migrations are a sequence, not a single SQL command

Production schema changes become dangerous when the database and application are forced to change at exactly the same moment. Expand-and-contract avoids that dependency: add first, deploy compatible code, backfill, verify, switch, and clean up later. It takes more planning than a one-line rename, but it gives teams a controlled path through rolling deployments and reduces the chance that one old process brings the whole application down.

For related backend performance work, see our guide to PostgreSQL indexes and query performance, and our practical guide to reliable webhook processing.

Sources and further reading

  1. PostgreSQL documentation: CREATE INDEX — concurrent index builds and their caveats.
  2. PostgreSQL documentation: ALTER TABLE — lock behavior, table rewrites and staged constraint validation.
  3. PostgreSQL documentation: Explicit Locking — table-level lock modes and conflicts.
  4. Supabase documentation: Database migrations — managing schema changes in Supabase projects.