Select unlocked row in Postgresql

后端 未结 14 2099
你的背包
你的背包 2020-12-13 06:20

Is there a way to select rows in Postgresql that aren\'t locked? I have a multi-threaded app that will do:

Select... order by id desc limit 1 for update
         


        
14条回答
  •  时光取名叫无心
    2020-12-13 06:54

    How about the following? It might be treated more atomically than the other examples but should still be tested to make sure my assumptions aren't wrong.

    UPDATE users SET flags = 1 WHERE id = ( SELECT id FROM users WHERE flags = 0 ORDER BY id DESC LIMIT 1 ) RETURNING ...;
    

    You'll probably still be stuck with whatever locking scheme postgres uses internally to supply consistent SELECT results in the face of a simultaneous UPDATEs.

提交回复
热议问题