[MDEV-16675] Unnecessary explicit lock acquisition during UPDATE or DELETE Created: 2018-07-03  Updated: 2021-07-21  Resolved: 2018-07-04

Status: Closed
Project: MariaDB Server
Component/s: Storage Engine - InnoDB
Affects Version/s: 5.5, 10.0, 10.1, 10.2, 10.3
Fix Version/s: 10.3.9

Type: Bug Priority: Major
Reporter: Marko Mäkelä Assignee: Marko Mäkelä
Resolution: Fixed Votes: 0
Labels: lock, performance

Issue Links:
Problem/Incident
causes MDEV-26206 gap lock is not set if implicit lock ... Closed
Relates
relates to MDEV-14589 InnoDB should not lock a delete-marke... Closed
relates to MDEV-16232 Use fewer mini-transactions Stalled
relates to MDEV-11215 Several locks taken to same record in... Stalled
relates to MDEV-14479 Do not acquire InnoDB record locks wh... Closed
relates to MDEV-16617 After upgrading from 5.5.52 to 10.1.3... Closed

 Description   

In InnoDB, an INSERT will not create an explicit lock object. Instead, the inserted record is initially implicitly locked by the transaction that wrote its trx_t::id to the hidden system column DB_TRX_ID. (Other transactions would check if DB_TRX_ID is referring to a transaction that has not been committed.)

If a record was inserted in the current transaction, it would be implicitly locked by that transaction. Only if some other transaction is requesting access to the record, the implicit lock should be converted to an explicit one, so that the waits-for graph can be constructed for detecting deadlocks and lock wait timeouts.

The bug is that currently, InnoDB would convert the implicit lock to an explicit one, even if no conflict exists. Here is an example:

--source include/have_innodb.inc
CREATE TABLE t1(id INT PRIMARY KEY, a INT, b CHAR(1), UNIQUE KEY u(a,b)) ENGINE=InnoDB;
BEGIN;
INSERT INTO t1 VALUES(1,1,'a'),(2,9999,'b'),(3,10000,'c'),(4,4,'d');
DELETE FROM t1 WHERE a = 9999 AND b='b';
COMMIT;
DROP TABLE t1;

In this test, there is no conflict, and the DELETE statement should not convert the implicit lock into an explicit one. But, the function lock_rec_convert_impl_to_expl_for_trx() is being invoked during the test.



 Comments   
Comment by Marko Mäkelä [ 2018-07-03 ]

This fix should reduce the amount of transactions aborted due to deadlocks. I had to adjust the test main.xa so that it would continue to generate a deadlock error, instead of getting a report of a lock wait timeout.

I remember reports about deadlocks in INSERT…ON DUPLICATE KEY UPDATE in MySQL. I believe that this fix together with the already implemented MDEV-14589 and the upcoming MDEV-16232 should greatly reduce the probability of deadlocks between transactions.

Comment by Thirunarayanan Balathandayuthapani [ 2018-07-04 ]

Patch looks good to me.

Generated at Thu Feb 08 08:30:42 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.