Let me start my question with an SQL example.
Here is the table setup:
- Create table
x
andy
. Withy.x
refers tox.id
. - Insert a row into
x
(id=1).
START TRANSACTION;
CREATE TABLE `x` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`value` INT(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB;
CREATE TABLE `y` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`x_id` INT(11) NOT NULL,
`value` INT(11) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_x` FOREIGN KEY (`x_id`)
REFERENCES `x` (`id`)
) ENGINE=INNODB;
INSERT INTO x values (1,123456);
COMMIT;
Now start a transaction (Trx A) to update the row in x
.
START TRANSACTION;
UPDATE x SET value=value+1 WHERE id = 1;
Before it is committed, I am starting another transaction (Trx B) to insert a row to y
.
START TRANSACTION;
INSERT INTO y VALUES (null,1,123456);
---- HANGED ----
-- Until Trx A is committed or rolled-back, the Trx B is hanged here.
The question is - is it expected that Trx B to be hanged at that point? Why and any way to workaround that?
This has been tested on MySQL 5.7.21, Percona 5.7.21, MariaDB 10.2.14