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
Upvotes
View all comments
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