Is Insert ... Select An Atomic Transaction?
Solution 1:
Yes, FOR UPDATE OF t2 SKIP LOCKED is the right approach to prevent race conditions with default Read Committed transaction isolation.
The added SKIP LOCKED also prevents deadlocks. Be aware that competing transactions might each get a partial set from the SELECT - whatever it could lock first.
While any transaction is atomic in Postgres, it would not prevent another (also atomic) transaction from selecting (and inserting - or at least trying) the same row, because SELECTwithoutFOR UPDATE does not take an exclusive lock.
The Postgres manual about transactions:
A transaction is said to be atomic: from the point of view of other transactions, it either happens completely or not at all.
Related:
Clarifications:
An SQL DML command like
INSERTis always automatically atomic, since it cannot run outside a transaction. But you can't say thatINSERTis a transaction. Wrong terminology.In Postgres all locks are kept until and released at the end of the current transaction.
Post a Comment for "Is Insert ... Select An Atomic Transaction?"