Hacker News new | ask | show | jobs
by JohnyTex 40 days ago
> In MySQL, if you acquire a lock on Alice, then attempt acquire on Bob... then another session acquire Bob, then acquires Alice: The engine notices. Nothing bad happens. One of the threads gets marked as a deadlock and is rolled back, the other succeeds. So... yeah, not really a problem. MySql chooses at "random" which one lives.

That my program terminates and must be restarted by an external process is something I would consider "a problem", actually. This is not to cast shade on MySQL, it's just that if you really want deterministic transactions it's hard / impossible with most SQL databases. For most use cases optimistic concurrency is perfectly fine though!

> Actually... hang on, pause, let me remind you what you learned in your undergrad CS 400 class: Acquire Locks in consistent order.

The problem is that most SQL database engines acquire locks in the order that the rows are read, not in the order that they are sorted. So lock ordering depends on the query plan, not on the query itself. This is definitely true in SQL Server; I'm pretty sure it's the same in Postgres, but I haven't found anything to corroborate that claim yet. However, it's very clear from reading the MySQL / InnoDB documentation that there's basically no way to guarantee consistent lock ordering: https://dev.mysql.com/doc/refman/8.4/en/innodb-deadlocks.htm...

1 comments

>That my program terminates and must be restarted by an external process

Your program terminates for absolutely _regular_ SQL signals that are literally documented in the API? This is a 'not knowing how the tool works', knowledge comprehension issue. This is like complaining your car wont run because you have to put gas in it.

>The problem is that most SQL database engines acquire locks in the order that the rows are read, not in the order that they are sorted.

Most in fact are undefined. Read order is a convenient optimization.

> So lock ordering depends on the query plan, not on the query itself.

Right, so don't do that.

My example is 100% deterministic. Go try it right now on your favorite ANSI Sql Database ;) It works every single time.