如何获取Postgresql中未锁定的下一条记录

如何获取Postgresql中未锁定的下一条记录,postgresql,spring-boot,Postgresql,Spring Boot,我有一个多线程应用程序,在Postgresql 12中使用一个表,如下所示 试验 id int pk 名称字符串 标志字符(1) 逻辑: (1) 使用建议锁获取一条记录 select * from test where flag = '0' and pg_try_advisory_xact_lock(id) order by id limit 1; (2) 更新“标志”字段 当逻辑在一个线程中运行时,其他线程从表中什么也得不到 我可以获得未锁定在其他线程中的下一条记录吗 谢谢 update t

我有一个多线程应用程序,在Postgresql 12中使用一个表,如下所示

试验

  • id int pk
  • 名称字符串
  • 标志字符(1)
  • 逻辑:

    (1) 使用建议锁获取一条记录

    select * from test where flag = '0' and pg_try_advisory_xact_lock(id) order by id limit 1;
    
    (2) 更新“标志”字段

    当逻辑在一个线程中运行时,其他线程从表中什么也得不到

    我可以获得未锁定在其他线程中的下一条记录吗

    谢谢

    update test set flag = '1' where id =  ↑.id