[MDEV-23560] Deadlock detected on SELECT when only one record being processed Created: 2020-08-24 Updated: 2020-10-17 |
|
| Status: | Open |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 10.4.14 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major |
| Reporter: | Steve Millington | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Vanilla Docker image of MariaDb |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
I have a complex situation that I have boiled down to a relatively simple testcase. I have been unable to use any MySQL Unit Test infrastructure as the issue involves having 3 simultaneous clients. I am attaching a JDBC program which can be used as described below to demonstrate the problem. I have confirmed this is not a JDBC issue as I have an equivalent ODBC program that exhibits the same behaviour. I have one client session that has successfully done a 'SELECT ... FOR UPDATE' on a record (with autocommit off), and I have two other sessions (with autocommit off) that are doing the same select on the same key. These two other sessions are blocked, waiting for a lock. The session that has the lock, then does a delete of the selected record (in JDBC, it does a resultSet.deleteRow(), in ODBC it does a SQLSetPos(..., SQL_DELETE)). That is fine. Finally, that session does a commit. At this point the two blocked sessions come to life. I am expecting both of the 'SELECT' statements to return successfully with 'no rows found'. The bug is that instead of both doing that, one of them returns successfully with 'no rows found', but the other one produces a 'Deadlock found when trying to get lock; try restarting transaction'. Setup:
created using 'CREATE TABLE BUG_DEMO (C1 INTEGER NOT NULL PRIMARY KEY, C2 INTEGER NOT NULL UNIQUE KEY)' I then insert a single row into this table using for example "INSERT INTO BUG_DEMO(C1,C2) VALUES(12345, 12345)" (These two operations can be done with my attached program by running 'make create' and 'make insert'. Reproduce: Problem Specifics: |
| Comments |
| Comment by Marko Mäkelä [ 2020-08-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Where is the client program? Could you repeat this with something like
If you are using InnoDB, then this might be working as designed. InnoDB does not have a concept of ‘table row locks’, but instead it has ‘index record’ locks. If the SELECT is accessing the secondary UNIQUE index first, it can easily deadlock with an INSERT that would first lock (and modify) the primary key index before locking the secondary indexes, one by one. This report could actually be a duplicate of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Steve Millington [ 2020-08-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Sorry for the delay in putting my test program on - my network died. .. it is there now. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Steve Millington [ 2020-08-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Marko, Thanks for getting back. I think that 'If the SELECT is accessing the secondary UNIQUE index first, it can easily deadlock with an INSERT that would first lock (and modify) the primary key index before locking the secondary indexes, one by one.' is not the situation I am in. My situation is purely with SELECT ... / DELETE. The only INSERT is prior to the issue setting up the database. This does look a bit similar to 10962, but it is really hard for me to tell. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Steve Millington [ 2020-08-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
It is intuitively odd that the exact same select issues by two clients would produce deadlocks. If any locks were taken, one would expect them to be taken in the same order and therefore no deadlock to occur! However. I am aware that my intuition is not necessarily what would occur! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Steve Millington [ 2020-08-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Here is the output of innodb status
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Steve Millington [ 2020-08-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I know nothing of this, but to my unknowing eyes, it looks like the deadlock is with itself! |