r/Supabase • u/fervent7 • 17h ago
database PostgreSQL persistent task queue: concurrency, idempotency and restart recovery
/r/codereview/comments/1y6b5t7/postgresql_persistent_task_queue_concurrency/For those using Supabase, would you recommend Supabase Queues (pgmq) or a dedicated PostgreSQL queue table for this use case? I'm particularly interested in concurrency, stale worker protection and reliable restart recovery.
1
Upvotes
1
u/Adventurous_Ant_4233 15h ago
You already have a queue table, so asking if a dedicated one is recommended is circular. The stale-result thing is a lease and fencing token problem, not a table design problem.
1
u/Smooth-Vehicle547 14h ago
A claim id on its own prevents nothing, it only works if the final write checks it (update... where claim_id = $1). And what did the test actually show on the stale result? 'Potential issue' can mean a slow ack or a re-queue while the old worker is still alive, and the fix isn't the same.
1
u/Neat_Act_9199 14h ago
Stale result landing on a fresh row for the same candidate is fixed by putting the claim id in the where clause of your final update, that part isn't really a design question. Did you actually see it happen during testing or is "we identified a potential issue" just you reasoning about the code? Because those need very different amounts of work.
1
u/CaressingCash21 4h ago
Since you're keeping the existing Postgres queue, I'd make completion ONE database function/transaction. First do a conditional UPDATE matching the job instance, current claim ID, processing state and an unexpired lease, then use RETURNING to gate the observation/event inserts. Zero rows means stale: no result writes. That keeps the ownership check and all completion effects together, instead of checking a claim and writing results in separate requests. Add a unique key per logical result/event as well, so retries after a lost response can't duplicate them. If you later use pgmq for delivery, put its acknowledgment in that transaction too. I'd test reassignment while the old worker is paused, and a crash after commit but before it gets the response!!! PostgreSQL explains the concurrent UPDATE/WHERE recheck here: https://www.postgresql.org/docs/current/transaction-iso.html
0
u/Decent_Click_7787 14h ago
sounds like a Redis problem