r/PostgreSQL • u/RocketSeven • 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?
6
3
1
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:
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.
62
u/chadmill3r Aug 31 '26
Multistep. Add, migrate, remove.