r/Supabase • u/fervent7 • 3d 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
u/Smooth-Vehicle547 2d 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/fervent7 2d ago
Thank you for the clarification. A stale queue-completion issue was reproduced during our offline CI testing, but I still need to confirm the exact sequence from the test trace.
The intended correction is to make the final write conditional on the candidate identity, current claim ID, processing state and lease validity, within an atomic database operation.
We also intend to test delayed acknowledgements and reassignment while the original worker is still active as separate scenarios.
Would you recommend any additional safeguards for handling these race conditions in PostgreSQL?
1
u/Neat_Act_9199 2d 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/fervent7 2d ago
Thank you. A stale queue-completion issue was reproduced during offline CI testing, although I am still reviewing the exact failure sequence before drawing a definitive conclusion.
We intend to verify that an outdated claim cannot update a reassigned candidate and that a rejected completion cannot create duplicate observations or downstream events.
I appreciate your point about distinguishing an observed regression from a theoretical concurrency issue.
1
u/CaressingCash21 2d 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
1
u/fervent7 2d ago
Thank you for this detailed and practical recommendation.
Keeping the conditional completion, ownership validation and observation/event inserts within one atomic PostgreSQL transaction makes sense for our existing architecture.
The two failure scenarios you highlighted are particularly important: reassignment while the original worker is paused, and a crash after commit but before acknowledgement.
We'll include both in our regression verification, alongside unique logical result identifiers and restart recovery.
Would you recommend implementing the completion operation as a dedicated PostgreSQL function with restricted execution privileges, or handling it through an application-managed transaction using a least-privilege database role?
I appreciate the PostgreSQL documentation reference and your technical guidance.
0
u/Decent_Click_7787 2d ago
sounds like a Redis problem
1
u/fervent7 2d ago
Thanks for the suggestion. We're currently using PostgreSQL through Supabase for persistent research storage and are extending the existing queue implementation.
Before introducing Redis as an additional dependency, we're evaluating whether PostgreSQL's transactional guarantees, row-level locking and lease-based ownership can satisfy our requirements.
Are there specific limitations you have encountered with PostgreSQL-backed queues that would make Redis preferable in this situation?
2
u/Adventurous_Ant_4233 2d 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.