If I have two READ COMMITTED
PostgreSQL database transactions that both create a new row with the same primary key and then lock this row, is it possible to acquire both locks successfully at the same time?
My instinct is yes since these new rows both only exist in the individual transactions' scopes, but I was curious if new rows and locking is handled differently between transactions.
No.
Primary keys are implemented with a
UNIQUE
(currently only) b-tree index. This is what happens when trying to write to the index, per documentation:Bold emphasis mine.
You can just try it with two open transactions (two different sessions) in parallel.