mirror of
https://github.com/MariaDB/server.git
synced 2025-02-10 23:45:34 +01:00
![Marko Mäkelä](/assets/img/avatar_default.png)
https://jepsen.io/analyses/mysql-8.0.34 highlights that the transaction isolation levels in the InnoDB storage engine do not correspond to any widely accepted definitions, such as "Generalized Isolation Level Definitions" https://pmg.csail.mit.edu/papers/icde00.pdf (PL-1 = READ UNCOMMITTED, PL-2 = READ COMMITTED, PL-2.99 = REPEATABLE READ, PL-3 = SERIALIZABLE). Only READ UNCOMMITTED in InnoDB seems to match the above definition. The issue is that InnoDB does not detect write/write conflicts (Section 4.4.3, Definition 6) in the above. It appears that as soon as we implement write/write conflict detection (SET SESSION innodb_snapshot_isolation=ON), the default isolation level (SET TRANSACTION ISOLATION LEVEL REPEATABLE READ) will become Snapshot Isolation (similar to Postgres), as defined in Section 4.2 of "A Critique of ANSI SQL Isolation Levels", MSR-TR-95-51, June 1995 https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-95-51.pdf Locking reads inside InnoDB used to read the latest committed version, ignoring what should actually be visible to the transaction. The added test innodb.lock_isolation illustrates this. The statement UPDATE t SET a=3 WHERE b=2; is executed in a transaction that was started before a read view or a snapshot of the current transaction was created, and committed before the current transaction attempts to execute UPDATE t SET b=3; If SET innodb_snapshot_isolation=ON is in effect when the second transaction was started, the second transaction will be aborted with the error ER_CHECKREAD. By default (innodb_snapshot_isolation=OFF), the second transaction would execute inconsistently, displaying an incorrect SELECT COUNT(*) FROM t in its read view. If innodb_snapshot_isolation=ON, if an attempt to acquire a lock on a record that does not exist in the current read view is made, an error DB_RECORD_CHANGED (HA_ERR_RECORD_CHANGED, ER_CHECKREAD) will be raised. This error will be treated in the same way as a deadlock: the transaction will be rolled back. lock_clust_rec_read_check_and_lock(): If the current transaction has a read view where the record is not visible and innodb_snapshot_isolation=ON, fail before trying to acquire the lock. row_sel_build_committed_vers_for_mysql(): If innodb_snapshot_isolation=ON, disable the "semi-consistent read" logic that had been implemented by myself on the directions of Heikki Tuuri in order to address https://bugs.mysql.com/bug.php?id=3300 that was motivated by a customer wanting UPDATE to skip locked rows that do not match the WHERE condition. It looks like my changes were included in the MySQL 5.1.5 commit ad126d90e019f223470e73e1b2b528f9007c4532; at that time, employees of Innobase Oy (a recent acquisition of Oracle) had lost write access to the repository. The only reason why we set innodb_snapshot_isolation=OFF by default is backward compatibility with applications, such as the one that motivated the implementation of "semi-consistent read" back in 2005. In a later major release, we can default to innodb_snapshot_isolation=ON. Thanks to Peter Alvaro, Kyle Kingsbury and Alexey Gotsman for their work on https://github.com/jepsen-io/ and to Kyle and Alexey for explanations and some testing of this fix. Thanks to Vladislav Lesin for the initial test for MDEV-26643, as well as reviewing these changes.
203 lines
6.7 KiB
Text
203 lines
6.7 KiB
Text
set default_storage_engine=innodb;
|
|
set @old_dbug=@@global.debug_dbug;
|
|
SET @saved_stats_persistent = @@GLOBAL.innodb_stats_persistent;
|
|
SET GLOBAL innodb_stats_persistent = OFF;
|
|
CREATE TABLE `t` (
|
|
`a` BLOB,
|
|
`b` BLOB,
|
|
`c` BLOB GENERATED ALWAYS AS (CONCAT(a,b)) VIRTUAL,
|
|
`h` VARCHAR(10) DEFAULT NULL,
|
|
`i` int
|
|
) ENGINE=InnoDB;
|
|
INSERT INTO t VALUES (REPEAT('g', 16000), REPEAT('x', 16000), DEFAULT, "kk", 1);
|
|
INSERT INTO t VALUES (REPEAT('a', 16000), REPEAT('b', 16000), DEFAULT, "mm", 2);
|
|
CREATE INDEX idx ON t(c(100));
|
|
SET global debug_dbug="d,ib_purge_virtual_index_callback";
|
|
UPDATE t SET a = REPEAT('m', 16000) WHERE a like "aaa%";
|
|
InnoDB 0 transactions not purged
|
|
SET global debug_dbug=@old_dbug;
|
|
DROP TABLE t;
|
|
CREATE TABLE t (
|
|
a TINYBLOB,
|
|
b TINYBLOB,
|
|
c TINYBLOB GENERATED ALWAYS AS (CONCAT(a,b)) VIRTUAL,
|
|
h VARCHAR(10) DEFAULT NULL,
|
|
i INT
|
|
) ROW_FORMAT=COMPACT ENGINE=InnoDB;
|
|
INSERT INTO t VALUES (REPEAT('g', 100), REPEAT('x', 100), DEFAULT, "kk", 1);
|
|
INSERT INTO t VALUES (REPEAT('a', 100), REPEAT('b', 100), DEFAULT, "mm", 2);
|
|
CREATE INDEX idx ON t(c(100));
|
|
SET global debug_dbug="d,ib_purge_virtual_index_callback";
|
|
UPDATE t SET a = REPEAT('m', 100) WHERE a like "aaa%";
|
|
InnoDB 0 transactions not purged
|
|
SET global debug_dbug=@old_dbug;
|
|
DROP TABLE t;
|
|
CREATE TABLE t1 (
|
|
id INT NOT NULL,
|
|
store_id INT NOT NULL,
|
|
x INT GENERATED ALWAYS AS (id + store_id)
|
|
)
|
|
PARTITION BY RANGE (store_id) (
|
|
PARTITION p0 VALUES LESS THAN (6),
|
|
PARTITION p1 VALUES LESS THAN (11),
|
|
PARTITION p2 VALUES LESS THAN (16),
|
|
PARTITION p3 VALUES LESS THAN (21)
|
|
);
|
|
insert into t1 values(1, 2, default);
|
|
insert into t1 values(3, 4, default);
|
|
insert into t1 values(3, 12, default);
|
|
insert into t1 values(4, 18, default);
|
|
CREATE INDEX idx ON t1(x);
|
|
SET global debug_dbug="d,ib_purge_virtual_index_callback";
|
|
UPDATE t1 SET id = 10 WHERE id = 1;
|
|
InnoDB 0 transactions not purged
|
|
SET global debug_dbug=@old_dbug;
|
|
DROP TABLE t1;
|
|
connect con1,localhost,root,,;
|
|
connection default;
|
|
CREATE TABLE t1 (a INT, b INT);
|
|
INSERT INTO t1(a, b) VALUES (1, 1), (2, 2), (3, 3);
|
|
connection con1;
|
|
# disable purge
|
|
CREATE TABLE t0 (a INT) ENGINE=InnoDB;
|
|
BEGIN;
|
|
SELECT * FROM t0;
|
|
a
|
|
connection default;
|
|
DELETE FROM t1 WHERE a = 1;
|
|
UPDATE t1 SET a = 4, b = 4 WHERE a = 3;
|
|
INSERT INTO t1(a, b) VALUES (5, 5);
|
|
SET DEBUG_SYNC= 'inplace_after_index_build SIGNAL uncommitted WAIT_FOR purged';
|
|
ALTER TABLE t1 ADD COLUMN c INT GENERATED ALWAYS AS(a+b), ADD INDEX idx (c), ALGORITHM=INPLACE, LOCK=NONE;
|
|
ERROR 0A000: LOCK=NONE is not supported. Reason: INPLACE ADD or DROP of virtual columns cannot be combined with other ALTER TABLE actions. Try LOCK=SHARED
|
|
ALTER TABLE t1 ADD COLUMN c INT GENERATED ALWAYS AS(a+b), ADD INDEX idx (c), ALGORITHM=INPLACE, LOCK=SHARED;
|
|
connection con1;
|
|
SET DEBUG_SYNC= 'now WAIT_FOR uncommitted';
|
|
# enable purge
|
|
COMMIT;
|
|
# wait for purge to process the deleted records.
|
|
InnoDB 1 transactions not purged
|
|
SET DEBUG_SYNC= 'now SIGNAL purged';
|
|
connection default;
|
|
/* connection default */ ALTER TABLE t1 ADD COLUMN c INT GENERATED ALWAYS AS(a+b), ADD INDEX idx (c), ALGORITHM=INPLACE, LOCK=SHARED;
|
|
SHOW CREATE TABLE t1;
|
|
Table Create Table
|
|
t1 CREATE TABLE `t1` (
|
|
`a` int(11) DEFAULT NULL,
|
|
`b` int(11) DEFAULT NULL,
|
|
`c` int(11) GENERATED ALWAYS AS (`a` + `b`) VIRTUAL,
|
|
KEY `idx` (`c`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
|
|
SELECT * FROM t1;
|
|
a b c
|
|
2 2 4
|
|
4 4 8
|
|
5 5 10
|
|
DROP TABLE t1;
|
|
CREATE TABLE t1 (a INT, b INT, c INT GENERATED ALWAYS AS(a+b));
|
|
INSERT INTO t1(a, b) VALUES (1, 1), (2, 2), (3, 3), (4, 4);
|
|
connect stop_purge,localhost,root,,;
|
|
START TRANSACTION WITH CONSISTENT SNAPSHOT;
|
|
connection default;
|
|
DELETE FROM t1 WHERE a = 1;
|
|
UPDATE t1 SET a = 2, b = 2 WHERE a = 5;
|
|
INSERT INTO t1(a, b) VALUES (6, 6);
|
|
SET DEBUG_SYNC= 'inplace_after_index_build SIGNAL uncommitted WAIT_FOR purged';
|
|
ALTER TABLE t1 ADD INDEX idx (c), ALGORITHM=INPLACE, LOCK=NONE;
|
|
connection con1;
|
|
SET DEBUG_SYNC= 'now WAIT_FOR uncommitted';
|
|
BEGIN;
|
|
DELETE FROM t1 WHERE a = 3;
|
|
UPDATE t1 SET a = 7, b = 7 WHERE a = 4;
|
|
INSERT INTO t1(a, b) VALUES (8, 8);
|
|
disconnect stop_purge;
|
|
COMMIT;
|
|
# wait for purge to process the deleted/updated records.
|
|
InnoDB 2 transactions not purged
|
|
SET DEBUG_SYNC= 'now SIGNAL purged';
|
|
disconnect con1;
|
|
connection default;
|
|
/* connection default */ ALTER TABLE t1 ADD INDEX idx (c), ALGORITHM=INPLACE, LOCK=NONE;
|
|
SHOW CREATE TABLE t1;
|
|
Table Create Table
|
|
t1 CREATE TABLE `t1` (
|
|
`a` int(11) DEFAULT NULL,
|
|
`b` int(11) DEFAULT NULL,
|
|
`c` int(11) GENERATED ALWAYS AS (`a` + `b`) VIRTUAL,
|
|
KEY `idx` (`c`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
|
|
SELECT * FROM t1;
|
|
a b c
|
|
2 2 4
|
|
7 7 14
|
|
6 6 12
|
|
8 8 16
|
|
DROP TABLE t0, t1;
|
|
create table t (a blob, b blob, c blob as (concat(a,b)), h varchar(10), index (c(100)));
|
|
insert t(a,b,h) values (repeat('g', 16000), repeat('x', 16000), "kk");
|
|
insert t(a,b,h) values (repeat('a', 16000), repeat('b', 16000), "mm");
|
|
set global debug_dbug="d,ib_purge_virtual_index_callback";
|
|
connect prevent_purge, localhost, root;
|
|
start transaction with consistent snapshot;
|
|
connection default;
|
|
update t set a = repeat('m', 16000) where a like "aaa%";
|
|
connect lock_table, localhost, root;
|
|
lock table t write;
|
|
connection prevent_purge;
|
|
commit;
|
|
connection default;
|
|
disconnect lock_table;
|
|
InnoDB 0 transactions not purged
|
|
set global debug_dbug=@old_dbug;
|
|
drop table t;
|
|
#
|
|
# MDEV-15165 InnoDB purge for index on virtual column
|
|
# is trying to access an incomplete record
|
|
#
|
|
CREATE TABLE t1(
|
|
u INT PRIMARY KEY, b BLOB, ug INT GENERATED ALWAYS AS (u) VIRTUAL,
|
|
INDEX bug(b(100),ug)
|
|
) ENGINE=InnoDB;
|
|
INSERT INTO t1 (u,b) VALUES(1,REPEAT('a',16384));
|
|
connection prevent_purge;
|
|
start transaction with consistent snapshot;
|
|
connection default;
|
|
DELETE FROM t1;
|
|
SET DEBUG_SYNC='blob_write_middle SIGNAL halfway WAIT_FOR purged';
|
|
INSERT INTO t1 (u,b) VALUES(1,REPEAT('a',16384));
|
|
connection prevent_purge;
|
|
SET DEBUG_SYNC='now WAIT_FOR halfway';
|
|
COMMIT;
|
|
InnoDB 0 transactions not purged
|
|
SET DEBUG_SYNC='now SIGNAL purged';
|
|
connection default;
|
|
DROP TABLE t1;
|
|
CREATE TABLE t1 (y YEAR, vy YEAR AS (y) VIRTUAL UNIQUE, pk INT PRIMARY KEY)
|
|
ENGINE=InnoDB;
|
|
INSERT INTO t1 (pk,y) VALUES (1,2022);
|
|
CREATE TABLE t2(f1 INT NOT NULL, PRIMARY KEY(f1))ENGINE=InnoDB;
|
|
SET GLOBAL debug_dbug = 'd,ib_purge_virtual_index_callback';
|
|
BEGIN;
|
|
INSERT INTO t2(f1) VALUES(1);
|
|
connection prevent_purge;
|
|
SET DEBUG_SYNC=RESET;
|
|
start transaction with consistent snapshot;
|
|
connection default;
|
|
COMMIT;
|
|
connect truncate,localhost,root,,;
|
|
REPLACE INTO t1(pk, y) SELECT pk,y FROM t1;
|
|
TRUNCATE TABLE t1;
|
|
connection prevent_purge;
|
|
COMMIT;
|
|
SET DEBUG_SYNC='now SIGNAL purge_start';
|
|
disconnect prevent_purge;
|
|
connection default;
|
|
SET DEBUG_SYNC='now WAIT_FOR purge_start';
|
|
InnoDB 0 transactions not purged
|
|
SET GLOBAL debug_dbug=@old_dbug;
|
|
connection truncate;
|
|
disconnect truncate;
|
|
connection default;
|
|
DROP TABLE t1, t2;
|
|
set debug_sync=reset;
|
|
SET GLOBAL innodb_stats_persistent = @saved_stats_persistent;
|