Skip to content
OrionHub Developer tooling and cloud development
Section
archive
Kind
Archive · commissioned
Reading time
8 min

Zero-downtime Postgres schema changes

23 November 2026

When you need to alter a Postgres schema, downtime is a constant risk. But it doesn't have to be. With the right planning and techniques, you can avoid it.

What “zero-downtime” means in Postgres

Zero-downtime migrations aim to hold heavyweight locks only for milliseconds, avoid full table rewrites on busy tables, and remain deployable while the previous application version is still running and rollable back without a data migration. That means no service disruption, no long-running operations that impact performance, and an ability to roll back if something goes wrong.

The expand/contract pattern

The core pattern for zero-downtime schema changes is expand/contract: add the new shape, migrate traffic and data, then remove the old shape only after the application no longer depends on it. The xata.io expand and contract strategy, also called parallel change, migrates schemas by adding the new schema and data first, keeping the application aware of both versions for a period, and deleting the old schema only after backfill is complete.

This way, the application can fall back to the old schema if needed. It also avoids the risk of a long-running DDL operation holding a heavyweight lock that blocks other queries.

Why lock waits cause outages

ALTER TABLE can require an ACCESS EXCLUSIVE lock, and if a migration waits for that lock, later queries queue behind it, which is why lock queues are a major source of outages.

Guardrails: lock_timeout, statement_timeout, and retries

To avoid long-running locks, set lock_timeout for your migration session. This will cause the migration to fail out if it queues behind other traffic, rather than waiting and potentially causing an outage.

Similarly, set statement_timeout. This will abort the migration if it runs too long, again avoiding a queue that impacts other traffic.

Both of these guardrails can be set in the session using commands:

SET lock_timeout = '5s';
SET statement_timeout = '30s';

Retries are also important. If the migration fails, postgres.ai recommends exponential backoff and jitter to improve success rates when lock acquisition fails.

Safer patterns for indexes, constraints, and backfills

For some schema changes, readyset.io highlights special strategies to avoid downtime:

For indexes, use CREATE INDEX CONCURRENTLY instead of the blocking form. This builds the index in the background, allowing reads and writes to continue.

For constraints, first add them as NOT VALID, then validate them in a separate step later. This allows the migration to complete quickly, with validation happening in a separate operation.

For backfills, use small batches rather than one large UPDATE. This spreads out the work, reducing the impact on replicas and writers. For instance:

UPDATE my_table SET new_column = value WHERE id IN (SELECT id FROM my_table LIMIT 1000);

How to handle renames without breaking running code

Renaming a column requires multiple steps:

1. Add the new column
2. Write to both columns while old and new code overlap
3. Backfill existing rows into the new column
4. Switch reads to the new column
5. Drop the old column

To keep the old and new columns in sync during the overlap, you can use a trigger:

CREATE OR REPLACE FUNCTION sync_column() RETURNS trigger AS $$
BEGIN
NEW.new_column := NEW.old_column;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER sync_column_trigger
BEFORE INSERT OR UPDATE ON my_table
FOR EACH ROW EXECUTE FUNCTION sync_column();

Rollout, monitoring, and rollback checklist

Before deploying a zero-downtime migration, test it on a production-sized copy of your database. Monitor pg_stat_activity for long-running statements, and check for invalid indexes after concurrent operations.

Separate code release from migration release, so the production code remains compatible with the next schema version during rollout.

Split schema changes into multiple individual transactions when a migration touches multiple tables or contains multiple changes.

Common mistakes and when a simpler migration is acceptable

A common pitfall is subtransactions in migrations. They can complicate retries, so use them carefully.

Another mistake is trying to do too much in one migration. When possible, break changes into smaller steps.

Sometimes, zero-downtime is not strictly necessary. If a migration is very small, or can be deployed during a known maintenance window, a simpler approach may suffice.

But for large, complex changes, zero-downtime is the safest approach.