r/SQL • u/ClickOk5811 • 28d ago
PostgreSQL PostgreSQL recipe: renaming a column without downtime (expand and contract)
A plain ALTER TABLE ... RENAME COLUMN is instant, but it breaks any running old version of the app during a rolling deploy. The usual workaround is to never have a moment where only one name exists. Here’s the sequence, with Postgres commands. Test it on a scratch database first.
1. Expand: add the new column
ALTER TABLE orders ADD COLUMN customer_ref text;A nullable column with no default is a metadata-only change, so it’s fast.
2. Deploy the app writing both columns, still reading the old one.
The app sets customer_id_old and customer_ref on every insert and update. (A trigger can do this instead, but then the logic lives in the database. Decide which you prefer.)
3. Backfill in batches
UPDATE orders
SET customer_ref = customer_id_old
WHERE id BETWEEN 1 AND 10000
AND customer_ref IS DISTINCT FROM customer_id_old;Repeat for the next id range. Small batches avoid long locks and big bursts of WAL and replication lag. Run VACUUM as needed afterwards, since each update leaves dead rows.
4. Verify
SELECT count(*)
FROM orders
WHERE customer_ref IS DISTINCT FROM customer_id_old;IS DISTINCT FROM treats NULLs properly, so you expect 0.
5. Deploy the app reading the new column (still writing both).
6. Deploy the app that no longer writes the old column.
7. Contract: drop the old column in a separate, later migration
ALTER TABLE orders DROP COLUMN customer_id_old;This is also quick, but it’s irreversible without a backup, so check first that nothing reads it: views, functions, reports, other services, replicas used for analytics.
If the new column needs a constraint or index:
- Use
CREATE INDEX CONCURRENTLYso you don’t block writes. - For
NOT NULL, adding aCHECK (customer_ref IS NOT NULL) NOT VALIDand thenVALIDATE CONSTRAINTavoids a long lock. Check the docs for your version, since behavior has changed across releases.
When I’d skip all this: a table with no other readers, where a short maintenance window is acceptable. Then a plain rename is simpler.
What do you do differently, for example triggers versus dual writes in the app?
12
u/Thadrea Data Science Manager 28d ago
Alternative: Just don't rename columns on your production tables.
Non-ideal column names can be unfortunate, but they aren't tech debt. Users will not see them, and if you maintain your documentation properly your developers will know what the column represents. They do not create meaningful inefficiencies even if you might find the choice you or another team member made annoying.
You shouldn't risk breaking your application because you are angry about an internal labeling choice that only five people ever see.
This is a classic example of why you shouldn't be using AI tools to think for you. They will give you a plan (this one, which you probably copy-pasted from ChatGPT), but have no sense of the big picture or prioritization.
1
u/bogdanelcs 28d ago
Triggers for me, as long as more than one thing writes to the table.
App-level dual writes drift as soon as someone misses a code path, a cron script, or an old pod still running mid-deploy, and a BEFORE INSERT OR UPDATE trigger that copies the value covers all of them.
Drop it in the same migration as the contract step so it doesn’t hang around forever. I’d also set a short
lock_timeouton every DDL step. Otherwise even the “instant” ALTER can sit behind a long-running transaction and queue every other query behind it.1
u/vincent_sch 28d ago
You should write to both columns while you still need the option to roll back. After step 6, the app writes to
customer_refbut stops updatingcustomer_id_old. If you roll back after more writes, the old app won't see those changes. I wrote about the rollback window in my expand and contract article.1
u/Significant_Tune9219 28d ago
Two things I'd add for the contract step. Before the drop, look at pg_stat_statements, if you have it enabled, for any query still mentioning the old column, since that is how the forgotten reporting query shows up before it breaks. Also run the DROP COLUMN with a short lock_timeout, because it needs an ACCESS EXCLUSIVE lock and will queue behind a long-running transaction, and everything behind it then queues too. Retrying a few times is better than blocking the table.
1
u/aMusicLover 28d ago
Or put a view between it.
1
u/CanProfessional766 23d ago
I usually prefer dual writes at the application level because the migration logic stays visible and easier to test. Triggers can work too, especially when multiple services write to the same table, but they add another layer to maintain. The key is making the backfill repeatable and verifying both columns stay in sync before removing the old one.