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

1 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/Automatic-Fan-3993 1d ago

a uuid is enough here. your check and your write are the same UPDATE on the same row, so postgres already tells you whether the claim is still yours. an increasing token only helps when the result goes somewhere outside postgres, like an api or a bucket, that can't look up the current claim and can only compare numbers. if you want both, use an integer claim_version and bump it on every claim.

1

u/fervent7 1d ago

Thank you, that clarifies the distinction very well.

Since our current observation and event writes are contained within PostgreSQL, we'll focus on atomic claim UUID validation and ensuring that all dependent writes occur within the same transaction.

We'll also test expired leases, worker reassignment and recovery after a lost commit response.

For future external integrations, we'll evaluate whether a monotonically increasing claim_version is necessary alongside the UUID.

I appreciate your practical explanation. It helps us avoid unnecessary architectural complexity.