You have to be careful and read the docs regarding exactly what SELECT ... FOR UPDATE locks.
Specifically, with Postgres, SELECT ... FOR UPDATE is a row level lock on the returned rows. As a result you can't use aggregates.
So, you have to analyze carefully what your entire transaction needs to do, then decide if you need to lock or the natural transaction consistency will suffice. If you do need to lock, make sure to lock as little as possible, but be sure to lock it such that you can avoid deadlocks.
For example, with a recent application I was writing that uses Postgres, I needed to do some aggregating to determine what to insert. I could've locked with SHARE mode. However, two sessions locking with SHARE mode don't conflict, so the subsequent implicit ROW EXCLUSIVE lock performed by the insert later might've resulted in a deadlock.
|