Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL InnoDB lock question

I have a question about MySQL InnoDB. For example: I have the following table created:

   mysql>CREATE TABLE IF NOT EXISTS `SeqNum`
   (
     `id` varchar(10) NOT NULL,
     `seq_num` BIGINT(30) default 0,
      PRIMARY KEY(`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  Query OK, 0 rows affected (0.00 sec)

  mysql>INSERT IGNORE INTO `SeqNum` VALUES('current',0);
  Query OK, 1 rows affected (0.00 sec)

Now, I have two mysql connections to the same database, I name them as Thread A and B. In thread A, I have the following SQL statement:

    mysql> begin;
    Query OK, 0 rows affected (0.00 sec)

    mysql> select `seq_num` from SeqNum where `id`='current' FOR UPDATE;
       +---------+
       | seq_num |
       +---------+
       |       0 | 
       +---------+
       1 row in set (0.01 sec)

and then, I just leave the thread A as it is.

In thread B, I would like to do the same query:

   mysql> begin;
   Query OK, 0 rows affected (0.00 sec)

   mysql>SELECT `current_seq_num` FROM SeqNum WHERE `id` = 'current' FOR UPDATE;

thread B will throw an MySQL 1205 Error after the lock waiting time out: Lock wait timeout exceeded; try restarting transaction.

It makes sense, because threadA put a 'X' lock on that row, so that thread B can NOT get the 'X' lock until thread A release the lock.

Now, my question is: from the perspective of thread B, how could I know which thread/connection block my request (to obtain the 'UPDATE' privilege for the table 'SeqNum') when MySQL return Error 1205 to me? If threadA is doing nothing after it obtains the X lock, and I run 'show processlist' in thread B, all I have are: several threads with 'Sleep' Status (I assume there are more than two threads connected to the datbase), I can NOT identify which thread blocked my request?

Hopefully, I explained the question clearly. Thanks!

like image 761
WilliamLou Avatar asked Dec 12 '22 18:12

WilliamLou


2 Answers

InnoDB plugin will give you a clear picture of the locked and the locking queries.

For instance

SELECT r.trx_id waiting_trx_id,  r.trx_mysql_thread_id waiting_thread,
      r.trx_query waiting_query,
      b.trx_id blocking_trx_id, b.trx_mysql_thread_id blocking_thread,
      b.trx_query blocking_query
FROM       information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b  ON  b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r  ON  r.trx_id = w.requesting_trx_id;

will give you a locking and blocking transactions. Only you have to have innodb plugin installed.

like image 119
Rituparna Kashyap Avatar answered Jan 03 '23 08:01

Rituparna Kashyap


You can only easily tell if you use a newer (InnoDB Plugin) release. There are some tables in information_schema which you can query:

  • SELECT * FROM information_schema.innodb_trx;
  • SELECT * FROM information_schema.innodb_locks;

In the innodb_trx table there should be a column called 'trx_mysql_thread_id' (or similar - it is trx_mysql_thread_id in MySQL 5.5). That is the id in SHOW PROCESSLIST.

(Note that innodb_locks is misnamed. It will only be populated for lock-waits, not locks).

like image 33
Morgan Tocker Avatar answered Jan 03 '23 08:01

Morgan Tocker