--source include/have_innodb_16k.inc --source include/have_debug.inc --echo # Create a table with rows which can be small or large --echo # depending on our wish: CREATE TABLE t1(id INT PRIMARY key, val VARCHAR(16000)); --echo # Create 1024 records with id=0,1000,2000,... INSERT INTO t1 (id,val) VALUES (0, "x"); --let k=0 while($k < 10){ INSERT INTO t1 (id,val) SELECT (SELECT MAX(id) FROM t1)+1000+id, "x" FROM t1; --inc $k } SELECT COUNT(*) FROM t1; --echo # It so happens that 894000 is the largest record on its page, --echo # which we can see by looking at engine_lock_id --echo # being *:space_id:page_id:heap_no:* and verifying that --echo # 894000 has a different page_id than 895000. We'll call them "left" --echo # and "right" page. CREATE FUNCTION lock_page_no (engine_lock_id CHAR(100)) RETURNS CHAR(20) DETERMINISTIC RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(engine_lock_id, ':', 4), ':', -1); BEGIN; SELECT * FROM t1 WHERE id=894000 FOR UPDATE; SELECT * FROM t1 WHERE id=895000 FOR UPDATE; SELECT lock_page_no(engine_lock_id) FROM performance_schema.data_locks WHERE LOCK_TYPE='RECORD' AND lock_data='894000' INTO @left; SELECT lock_page_no(engine_lock_id) FROM performance_schema.data_locks WHERE LOCK_TYPE='RECORD' AND lock_data='895000' INTO @right; COMMIT; --let $assert_text = The two records should be on two different pages --let $assert_cond = @right <> @left --source include/assert.inc CREATE FUNCTION lock_page_name (engine_lock_id CHAR(100)) RETURNS CHAR(20) DETERMINISTIC RETURN CASE lock_page_no(engine_lock_id) WHEN @left THEN "left" WHEN @right THEN "right" ELSE "other" END; --echo # Now, we stop the purge as we want to just delete-mark the record, --echo # not physically remove it. SET GLOBAL innodb_purge_stop_now = ON; --echo # We delete-mark the largest record on "left" page. DELETE FROM t1 WHERE id = 894000; SET GLOBAL TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN; --echo # This will return no result, as the record is delete-marked. SELECT * FROM t1 WHERE id=894000 FOR UPDATE; --echo # But, should place an X,REC_NOT_GAP lock on the delete-marked record: --sorted_result SELECT lock_page_name(engine_lock_id),lock_mode,lock_status,lock_data FROM performance_schema.data_locks WHERE LOCK_TYPE='RECORD'; --echo # We now, unpause the purge, so that the delete-marked record gets --echo # physically removed, and the X,REC_NOT_GAP on it gets --echo # converted/inherited into a lock on the gap before "next record". SET GLOBAL innodb_purge_run_now=ON; --sleep 1 --echo # However, 894000 was the largest on its page, so the "next record" is --echo # the imaginary "supremum pseudo-record", and indeed we --echo # expect to see a lock on this "supremum pseudo-record" (P_S displays --echo # it as X, as opposed to X,GAP, because locks on supremum are always --echo # implicitly "gap" locks, as the record itself doesn't really exist). --echo # Note that the lock is *not* duplicated on the gap before first --echo # record of the "right" page, which is "another" gap after 894000. --sorted_result SELECT lock_page_name(engine_lock_id),lock_mode,lock_status,lock_data FROM performance_schema.data_locks WHERE LOCK_TYPE='RECORD'; --echo # In many cases this doesn't cause a problem: for example if some --echo # other transaction tried to insert 894000 again, it would get --echo # routed trough the B-tree to the "left" page, where the gap lock on --echo # supremum would prevent it. And only values larger-equal than --echo # the split point in the parent (895000) would get routed to the --echo # "right" page, which doesn't have the GAP lock. --echo # However, as we shall see, it's possible for us to "move the split --echo # point to the left", by inserting 893007 to the "right" page, --echo # which forces update of the split point from 895000 to 893007 - the --echo # new minimum of "right" page. --echo # As said before, by default, INSERT of a value smaller than 895000 --echo # gets routed to "left" page, but if the row is so large that it --echo # doesn't fit on the "left" page, we try to use --echo # btr_insert_into_right_sibling() heuristic to first push it to the --echo # "right" page, in hope to avoid the costly spliting of the "left" page --echo # due to overflow. --echo # So, let's see what happens if we insert a 2KB record: INSERT INTO t1 (id,val) VALUES (893007, REPEAT("x",2000)); --sorted_result SELECT lock_page_name(engine_lock_id),lock_mode,lock_status,lock_data FROM performance_schema.data_locks WHERE LOCK_TYPE='RECORD'; --connect (con1, localhost, root,,) SET @@SESSION.innodb_lock_wait_timeout = 1; --echo # Now we verify ACID properties. The below query is a --echo # transaction, which should wait to insert 894000 and timeout, because --echo # of conflicting lock on the gap before 895000. BEGIN; --error ER_LOCK_WAIT_TIMEOUT INSERT INTO t1 (id,val) VALUES (894000, "x"); COMMIT; --echo # No transaction should see the 894000 record: BEGIN; SELECT * FROM t1 WHERE id = 894000 FOR UPDATE; COMMIT; --connection default --echo # The default's transaction should still not see the record. SELECT * FROM t1 WHERE id = 894000; --echo # Just to prove that default's transaction hasn't commited meanwhile, --echo # let's look at its engine_transaction_id: --sorted_result SELECT lock_page_name(engine_lock_id),lock_mode,lock_status,lock_data FROM performance_schema.data_locks WHERE LOCK_TYPE='RECORD'; COMMIT; --echo #cleanup --disconnect con1 DROP TABLE t1; SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ; DROP FUNCTION lock_page_name; DROP FUNCTION lock_page_no;