Can two “SELECT FOR UPDATE” statements on the same table cause a deadlock?

后端 未结 2 536
清歌不尽
清歌不尽 2021-02-13 18:29

Suppose that two simultaneous transactions execute the following queries on a Postgresql DB:

Transaction A:

SELECT * FROM mytable WHERE id IN (1         


        
2条回答
  •  太阳男子
    2021-02-13 18:50

    Sorry, I had another answer but it was wrong.

    The documentation states that an ORDER BY clause is applied before the FOR UPDATE clause. So the locks are acquired in whatever order the rows are selected (I have confirmed as such by testing). If you need to select them in a different order, you can use:

    SELECT * FROM (SELECT * FROM table ORDER BY id FOR UPDATE) ORDER BY another_column;
    

    You may want to try your question on the PostgreSQL mailing list.

提交回复
热议问题