2

Everybody knows that if one process goes

  SELECT id FROM dead WHERE id = 'A' FOR UPDATE;
  SELECT id FROM dead WHERE id = 'B' FOR UPDATE;

and another goes

SELECT id FROM dead WHERE id = 'B' FOR UPDATE;
SELECT id FROM dead WHERE id = 'A' FOR UPDATE;

then you risk deadlock. I would rather assume that

SELECT id FROM dead WHERE id IN ('A', 'B') FOR UPDATE;

will deadlock against

SELECT id FROM dead WHERE id IN ('B', 'A') FOR UPDATE;

but is the order of locking the IN list defined -- which of those will deadlock against my single-select process?

More precisely, if I have an alphabetic locking order convention then can I safely merge lock A and lock B into one SQL statement (given that slow networks might make reducing the number of network trips to the database a good thing).

3
  • 3
    Which DBMS are you using? Oracle? Postgres? Nov 20, 2013 at 11:44
  • If there is a defined order, it will be product specific and it's not in the SQL Standard, so we'd definitely need to know what product you're working with. Nov 20, 2013 at 11:57
  • I want my code to work on multiple databases so I think you are telling it's not in the standard, therefore I can't use an IN list in this context. If you can tell me that Oracle promise to support one order and not change it then I'd consider special case code for that particular database. Nov 20, 2013 at 15:18

1 Answer 1

0

Experiment suggests that Oracle sorts the IN list by rowid, which means that I can't expect the IN list to be processed in my deadlock-ordering conventions.

So the answer to my question is that select ... IN (..) FOR UPDATE; is a deadlock risk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.