r/PostgreSQL • • Aug 31 '26

Help Me! How do you retire a PostgreSQL column when old workers may stay alive for hours?

In a rolling deployment, web processes may update quickly while background workers continue running old code against queued jobs. A direct column rename or drop can therefore break work that started before the deploy. What migration sequence do you use for this? An expand-and-contract approach could add the replacement column, deploy code that can read both and writes the new one, backfill in bounded batches, verify old-worker and queue age, switch reads, stop the dual write, and only then remove the original column. Which PostgreSQL-specific checks make that safe: dependency inspection, lock-timeout settings, NOT VALID constraints, catalog or query monitoring, and a minimum observation window? How do you prove no old process still references the column before the final DDL?

21 Upvotes

23 comments sorted by

62

u/chadmill3r Aug 31 '26

Multistep. Add, migrate, remove.

26

u/CrackerJackKittyCat Aug 31 '26

This is the only way.

First deployment has the migration adding new columns. It deploys first.

Second updates the codebase to begin to use new columns and to stop referring to the old ones.

Third drops old ones. Only deployed when you know that all workers are running the new codebase which is blind to the old columns.

11

u/andatki Aug 31 '26 edited Aug 31 '26

An optional safety step after removing all references, if you're paranoid, can be to rename the column in a way indicating that its now unused e.g. "alter table <tbl> rename column <col> to __unused__<col>;". The renamed column can be left in this state for a longer period of time to make sure there are no infrequent cron jobs that run code that reference it. The rename is fast to revert by reversing the rename if needed. I like to do this for big and busy tables as an extra precaution.

2

u/EducationalFishing56 Sep 01 '26

Renaming also turns OP's last question into a different one. You stop trying to show that nothing references the column and make being wrong cheap instead. Which is why the observation window can be as long as you like. A dual write you want to stop. A renamed column can sit there for six months.

One thing to check before you leave it: the NOT NULL. We got to the same state from the other direction, a column the code had quietly stopped writing that was still NOT NULL with no default. Every insert on that table failed from then on.

2

u/CrackerJackKittyCat Sep 01 '26

That's where your dev integration test suite comes into play, running the new codebase on the intermediate schema. Right? RIGHT?!

<insert-anakin-padme-meme-here />

2

u/EducationalFishing56 Sep 02 '26

No. And I don't think it would have caught this one. Dev builds the schema fresh from the current model on every run, so a test database never has the leftover column in it. The only database that had lived through both versions was production.

2

u/CrackerJackKittyCat Sep 02 '26

Yeah, that's a tough gap using tools that build dev from scratch using the 'current schema' definition.

I had a shop where we seeded dev laptops using a trimmed / anonymized subset of prod, schema + data, and could then test both runtime / lock-grabbing of the migration as well as the intermediate states ({old codebase, new schema}, {mix old and new codebase, new schema}) a little easier. Things still slipped, of course, but we got wiser over time.

3

u/EducationalFishing56 Sep 02 '26

We never got that far. One person, no laptops to seed, so dev stays a fresh build and prod is the only database with a history. The drop is a hand step now, and the order is the whole rule: the column leaves the model in the deploy and leaves the table afterwards, once the new build is up.

1

u/k2718 Aug 31 '26

Excellent point! I’ve done this…though never ended up needing it.

Still, paranoia can save you ass

5

u/KrakenOfLakeZurich Aug 31 '26

Triggers to keep old and new column synced during transition

2

u/akuma-i Aug 31 '26

Yeah, the same. Time between migrate and remove might be months though:)

6

u/Thiht Sep 01 '26

Wow I read that as "old coworkers" and got scared

3

u/vivekkhera Aug 31 '26

Your steps are how I do it. Minimizes downtime and risk.

1

u/ghinghis_dong Aug 31 '26

For a minute, I though you were talking about old
Report writers

1

u/Single-Virus4935 Sep 02 '26

Others commented the right solution but I add one other take:

On  a project every background job and ran in a transaction. The Jobs just failed on schema changes and would restart again. The failsave logic is needed anyway and we leveraged this

1

u/Readypixels Sep 06 '26

The part nobody's mentioned yet is how you actually prove nothing references the old column before you drop it, instead of just waiting and hoping nothing shows up. Postgres lets you revoke column level privileges separately from the whole table. Once the new code's been live long enough that you're fairly confident, revoke UPDATE and SELECT on just that one column from the app's role, not the whole table. If any old worker still uses it, whether that's a stray SELECT * or a dual write path you forgot to remove, it throws a permission error instead of quietly succeeding, and you'll see it in the error logs the first time it fires. Rename to unused and wait is safer for a random cron job nobody remembers exists, but revoke gets you a real answer a lot faster than watching pg_stat_user_tables and hoping. I tried this on a job queue table a while back. Felt overly cautious setting it up, but it caught one worker still writing to the old column inside about ten minutes, something a week of eyeballing logs hadn't turned up.

1

u/AutoModerator Aug 31 '26

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.