Forum OpenACS Q&A: locking and deadlocks

Collapse
Posted by Jonathan Ellis on
I read the Lock documentation, and the relevant passage for me seems to be
...if a transaction is going to change data in a table, then SHARE ROW EXCLUSIVE lock mode should be acquired to prevent deadlock conditions when two concurrent transactions attempt to lock the table in SHARE mode and then try to change data in this table, both (implicitly) acquiring ROW EXCLUSIVE lock mode that conflicts with a concurrent SHARE lock.
So basically when only a single table is involved, the only way to get a deadlock is when you have a transaction containing both selects and dml on it?

How about with subselects? If I have one transaction containing

update foo set ... where id in (select foo_id from bar)
could that deadlock with
insert into bar values (...)
?
Collapse
Posted by Jonathan Ellis on
Ack, I shouldn't post at night. That didn't make sense at all. Ignore that last example. Here's what I mean:
update foo f set i = i * 2 where 1 < (select count(*) from foo f2 where ft.field = f.field)
Could a query like this deadlock with itself, or is postgres smart enough to see the update and grab an exclusive lock before evaluating the subquery and getting a shared lock?
Collapse
Posted by Don Baccus on
Internal locking's very consistent in PG.