News for hackers. Stuff that matters.
Re:Choose Postgres Queue Technology by deredede on Monday September 25, 2023 @02:27AM (#37640242)
The part about checking the number of affected rows hints at using `UPDATE ... WHERE ...` which should act as an atomic CAS regardless of isolation level.

Edit: To clarify, I mean `SELECT id WHERE used = 0` followed by `UPDATE ... SET used = 1 WHERE id = ... AND used = 0`

Re:Choose Postgres Queue Technology by matsemann on Monday September 25, 2023 @02:42AM (#37640329)
This is spot on! We let the db provide the atomics.
Re:Choose Postgres Queue Technology by RichieAHB on Monday September 25, 2023 @02:59AM (#37640426)
This works fine as long as you’re happy to do the same task multiple times. I.e. the task is idempotent and cheap.
Re:Choose Postgres Queue Technology by kdps on Monday September 25, 2023 @04:15AM (#37640845)
I don't get it :(. Why could the same task be executed more than once? From my understanding, if the UPDATE is atomic, only one worker will be able to set `used = 1`. If the update statement is not successful (affected != 1), then the worker should drop the task and do another select.
Re:Choose Postgres Queue Technology by klauserc on Monday September 25, 2023 @04:59AM (#37641104)
With a transaction isolation level below SERIALIZABLE you can have two transactions that both read the old row (with `used = 0`) at the time they perform the update (but before they commit the transaction). In that case, both transactions will have performed an update (rows affected = 1).

Why would both transactions see `used = 0`? The DB server tries to isolate transactions and actively hides effects of other transactions that have not committed yet.

Re:Choose Postgres Queue Technology by singron on Monday September 25, 2023 @06:11AM (#37641526)
This is not true in postgres. When the second transaction tries to update the row, it will wait for the first transaction to commit first and then recheck the WHERE.

https://www.postgresql.org/docs/current/transaction-iso.html...