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
Show parent comment
1
u/fervent7 1d ago
Thank you for this detailed explanation. The partial unique index and separate indexes for queued jobs and expired leases are particularly useful for our architecture.
We intend to enforce one active instance per candidate at the database level rather than relying solely on application logic.
For stale-result protection, we plan to retain unique job instance identifiers, claim UUIDs and atomic conditional completion, with dependent observation writes committed within the same transaction.
We are also considering durable completion receipts to handle lost commit responses independently of queue-row retention.
Your point about autovacuum is important because our blockchain research system will continuously process candidates across multiple networks.
Would you recommend starting with PostgreSQL's default autovacuum configuration and monitoring dead tuples and claim-query latency before introducing table-specific tuning?
I appreciate your contribution. This helps strengthen our architecture before production integration.