Articles

How to rename a PostgreSQL column without breaking a rolling deploy

Rename a PostgreSQL column during a rolling deploy. Keep old and new writes in sync, backfill safely, and verify that code rollback preserves the data.

Updated

Don't use ALTER TABLE … RENAME COLUMN while an older app still uses the old name. During a rolling deploy, both versions can be live at the same time. Add the new column first, keep the two names in sync, and remove the old column only after every old reader and allowed rollback target is gone.

Gregale publishes this guide. The example moves a FastAPI app from display_name to full_name in one shared PostgreSQL database and keeps the API field name the same.

1. Add the new column without removing the old one

Add full_name as nullable. In the same transaction, install a small trigger that mirrors a write to either column into the other. This matters because v1 is already serving and still writes display_name; a dual write in v2 alone cannot keep up with v1. The sample rejects conflicting values rather than silently choosing one.

PostgreSQL still takes a table lock for this additive DDL. Set a short lock timeout so a long-running transaction fails the change instead of making it wait behind live traffic. See the PostgreSQL ALTER TABLE reference. Keep the backfill out of that transaction.

2. Backfill and test both revisions

The new code writes full_name and reads COALESCE(full_name, display_name) until all old rows are copied. Backfill in bounded transactions. The sample selects missing rows with FOR UPDATE SKIP LOCKED and rechecks that they are still missing in the update. An empty batch can mean rows are locked, not that the backfill is complete. Before removing the fallback, verify that there are no missing or mismatched values.

Download the example. Its pre-traffic release command uses a migration-only database credential. On Gregale, wait for the v2 release and test while traffic is split. Send synthetic writes through both revisions and check the returned revision name, not just the HTTP status. Then wait for v2 to serve all traffic before using an exact code rollback to v1. Keep the database in place and verify that v1 still reads both revisions' writes.

3. Remove the old column later

Ship v3 so it reads and writes only full_name, with the mirror still installed. Retire v1 and v2, including workers, delayed jobs and rollback targets that still depend on the old column. After that window, run a separate reviewed migration to remove the mirror and old column. The sample contract SQL is not wired to the deployment. On a large table, plan the constraint validation and lock separately.

If a breaking migration has already landed, see recovering code and database together. A code rollback does not undo SQL or recover writes dropped by a schema change.

What we verified

With local PostgreSQL and two HTTP revisions, writes through either version remained visible to the other. A deliberately held table lock blocked expansion within the configured timeout and v1 kept reading. After stopping both old processes, v3 read and wrote successfully after the contract step.

On a private Gregale Scale app with managed PostgreSQL, v1 and v2 both accepted a synthetic write during the same canary. Each version then read all three rows, including the seed row and the other version's write. After v2 reached 100% traffic, an exact code-only rollback returned traffic to v1 on the same database. The stable URL still returned all three names and accepted a fresh v1 write. We ran the column removal only in the local test, not on the hosted database.

For a runnable Prisma v7 release task, see run Prisma migrate deploy before your Node.js API takes traffic.

Questions

Can I rename a PostgreSQL column during a rolling deploy?

A direct rename breaks any live revision that still references the old name. Add a new column, keep the two names in sync, and remove the old column after incompatible releases and rollback targets are retired.

Does rolling back a deployment reverse its database migration?

A code-only rollback leaves PostgreSQL as it is. Test that the old version can read and write the expanded schema before treating it as a valid rollback target.