I'm building a queuing mechanism of sorts. There are rows of data that need processing, and a status flag. I'm using an update .. returning
clause to manage it:
UPDATE stuff SET computed = 'working' WHERE id = (SELECT id from STUFF WHERE computed IS NULL LIMIT 1) RETURNING *
Is the nested select part the same lock as the update, or do I have a race condition here? If so, does the inner select need to be a select for update
?
While Erwin's suggestion is possibly the simplest way to get correct behavior (so long as you retry your transaction if you get an exception with SQLSTATE
of 40001), queuing applications by their nature tend to work better with requests blocking for a chance to take their turn at the queue than with the PostgreSQL implementation of SERIALIZABLE
transactions, which allows higher concurrency and is somewhat more "optimistic" about the chances of collision.
The example query in the question, as it stands, in the default READ COMMITTED
transaction isolation level would allow two (or more) concurrent connections to both "claim" the same row from the queue. What will happen is this:
UPDATE
phase.COMMIT
or ROLLBACK
of T1.id
matches), and also "claims" the row.It can be modified to work correctly (if you are using a version of PostgreSQL which allows the FOR UPDATE
clause in a subquery). Just add FOR UPDATE
to the end of the subquery which selects the id, and this will happen:
COMMIT
or ROLLBACK
of T1.At the REPEATABLE READ
or SERIALIZABLE
transaction isolation level, the write conflict would throw an error, which you could catch and determine was a serialization failure based on the SQLSTATE, and retry.
If you generally want SERIALIZABLE transactions but you want to avoid retries in the queuing area, you might be able to accomplish that by using an advisory lock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With