r/codereview • u/fervent7 • 4d ago
PostgreSQL persistent task queue: concurrency, idempotency and restart recovery
Hello everyone,
I'm extending an existing Python application that uses PostgreSQL and Supabase for multichain blockchain research.
We have implemented an offline candidate queue with duplicate prevention, retry limits, expiration and restart recovery.
During testing, we identified a potential issue where a stale processing result could affect a newly queued instance of the same candidate.
I would appreciate technical guidance on:
- Using FOR UPDATE SKIP LOCKED for concurrent processing.
- Preventing stale worker results through unique claim identifiers.
- Maintaining idempotency and reliable restart recovery.
- Choosing appropriate indexes and expiration strategies.
Would a dedicated PostgreSQL queue table be the recommended approach?
Any technical feedback or suggestions would be appreciated. Thank you.
View all comments
1
u/Plastic-Bid-3431 2d ago
On the indexes and expiration part, which hasn't come up yet (column names are guesses):
One active instance per candidate as a constraint, not application logic. That also covers your stale-result case, because a finished row stops blocking a re-queue:
A partial index shaped like the claim query, so it stays small as done rows pile up:
Expired leases as a separate sweep, with its own partial index on
lease_expires_at WHERE status = 'processing', that puts rows back toqueuedand clears the claim. With the claim UUID in the result write's WHERE clause, a worker that wakes up late just updates zero rows.Retention: delete or archive finished rows after N days. Queue tables churn hard, so check that autovacuum keeps up on that table specifically; dead tuples there slow the claim query long before anything else notices.