01 · Before and afterThe pattern
-- claim
UPDATE subs SET claimed_until = now() + interval '5 min'
WHERE id = $1 AND (claimed_until IS NULL OR claimed_until < now()) RETURNING claimed_until;
-- complete: fenced to *this* claim
UPDATE subs SET next_sync_at = $2, claimed_until = NULL
WHERE id = $1 AND claimed_until = $claim AND claimed_until > now(); -- before: WHERE id = $102 · Run it on your appThe check
When a background worker finishes a job, does its final write verify it still owns the job?
If the honest answer is no or not sure, this is worth a look before it reaches production.