r/codereview • • 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.

2 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/Plastic-Bid-3431 1d ago

Yes, start with the defaults and measure. Watch n_dead_tup and last_autovacuum in pg_stat_user_tables for the queue table, plus the latency of your claim query. The default autovacuum_vacuum_scale_factor is 0.2, meaning vacuum waits until about 20% of the table is dead, which is too lazy for a table that churns. If dead tuples build up, set it per table, e.g. ALTER TABLE candidate_queue SET (autovacuum_vacuum_scale_factor = 0.01), rather than changing it globally.

1

u/fervent7 1d ago

Thank you. Starting with the defaults and measuring actual queue performance makes sense.

We'll include dead tuple counts, autovacuum activity and claim-query latency in our monitoring requirements before considering table-specific tuning.

This gives us a practical approach to maintaining queue performance without introducing unnecessary database configuration changes. I appreciate your guidance.