It is true that INSERT , UPDATE or DELETE must receive a ROW EXCLUSIVE table lock to update.
However, this lock does not interfere with the normal operation of SELECT . SELECT only requires an ACCESS SHARE lock. This lock is compatible with ROW EXCLUSIVE - in other words, you can do a SELECT perfectly, while other data is updated using INSERT , UPDATE or DELETE if you have not acquired any explicit locks.
In other words, you will never see any deadlocks using the second approach (just don't use SELECT FOR UPDATE , and everything will be fine).
See more in the PostgreSQL Documentation .
mvp Apr 09 '13 at 5:35 on 2013-04-09 05:35
source share