From f5e4c2f2052b4b5def852b83d5bf9c30628f41f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= <marko.makela@mariadb.com>
Date: Sat, 18 Nov 2017 03:26:59 +0200
Subject: [PATCH] WIP: do not acquire record locks if covering table locks
 exist

---
 storage/innobase/lock/lock0lock.cc | 50 +++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/storage/innobase/lock/lock0lock.cc b/storage/innobase/lock/lock0lock.cc
index ed3281e1453..99d43a2fc65 100644
--- a/storage/innobase/lock/lock0lock.cc
+++ b/storage/innobase/lock/lock0lock.cc
@@ -1529,31 +1529,35 @@ lock_sec_rec_some_has_impl(
 }
 
 #ifdef UNIV_DEBUG
-/*********************************************************************//**
-Checks if some transaction, other than given trx_id, has an explicit
-lock on the given rec, in the given precise_mode.
+/** Check if some other transaction has an explicit
+lock on a record or table.
+@param[in]	mode	LOCK_S or LOCK_X, possibly ORed with
+			LOCK_GAP or LOCK_REC_NOT_GAP
+@param[in]	trx_id	ID of the transaction holding implicit record lock
+@param[in]	rec	possibly implicitly locked record
+@param[in]	index	B-tree index of the record
+@param[in]	block	buffer block containing the record
 @return	the transaction, whose id is not equal to trx_id, that has an
-explicit lock on the given rec, in the given precise_mode or NULL.*/
+explicit lock on the given rec, in the given mode
+(NOTE: the pointer is not safe to dereference!)
+@retval	NULL	if no such transaction exists */
 static
-trx_t*
+const trx_t*
 lock_rec_other_trx_holds_expl(
-/*==========================*/
-	ulint			precise_mode,	/*!< in: LOCK_S or LOCK_X
-						possibly ORed to LOCK_GAP or
-						LOCK_REC_NOT_GAP. */
-	trx_t*			trx,		/*!< in: trx holding implicit
-						lock on rec */
-	const rec_t*		rec,		/*!< in: user record */
-	const buf_block_t*	block)		/*!< in: buffer block
-						containing the record */
+	ulint			mode,
+	trx_id_t		trx_id,
+	const rec_t*		rec,
+	const dict_index_t*	index,
+	const buf_block_t*	block)
 {
 	ut_ad(!page_rec_is_default_row(rec));
+	ut_ad(page_align(rec) == block->frame);
 
 	trx_t* holds = NULL;
 
 	lock_mutex_enter();
 
-	if (trx_t* impl_trx = trx_rw_is_active(trx->id, NULL, false)) {
+	if (trx_t* impl_trx = trx_rw_is_active(trx_id, NULL, false)) {
 		ulint heap_no = page_rec_get_heap_no(rec);
 		mutex_enter(&trx_sys->mutex);
 
@@ -1561,13 +1565,14 @@ lock_rec_other_trx_holds_expl(
 		     t != NULL;
 		     t = UT_LIST_GET_NEXT(trx_list, t)) {
 
-			lock_t* expl_lock = lock_rec_has_expl(
-				precise_mode, block, heap_no, t);
+			if (t == impl_trx) {
+				continue;
+			}
 
-			if (expl_lock && expl_lock->trx != impl_trx) {
-				/* An explicit lock is held by trx other than
-				the trx holding the implicit lock. */
-				holds = expl_lock->trx;
+			if (lock_table_has(t, index->table,
+					   lock_mode(mode & LOCK_MODE_MASK))
+			    || lock_rec_has_expl(mode, block, heap_no, t)) {
+				holds = t;
 				break;
 			}
 		}
@@ -6912,7 +6917,8 @@ lock_rec_convert_impl_to_expl(
 		trx = lock_sec_rec_some_has_impl(rec, index, offsets);
 
 		ut_ad(!trx || !lock_rec_other_trx_holds_expl(
-				LOCK_S | LOCK_REC_NOT_GAP, trx, rec, block));
+				LOCK_S | LOCK_REC_NOT_GAP, trx->id,
+				rec, index, block));
 	}
 
 	if (trx != 0) {
-- 
2.15.0

