secondary index is not neccessary, I do not create it!!I have two sessions:session1: begin; select * from sbtest1 where id=123456 for updatesession2: insert into sbtest1 values(123450, 0,'aaa','bbb');the records in the table like below:(...),(123440, 264454, xxxxxx, xxxxx), (123456, 251028, xxxxx, xxxxxx),(....)in my opinion, the record with the id=123456 existed, so session1 add the X lock on this record, session2 add the insert intention lock between(123440, 123456), these two locks are not conflicted. session2 will not be blocked by session1.unfortunately, in my case, session2 is blocked by session1, what happens?
then I have debugged this instance(row0sel.cc::row_search_mvcc), I got that: when a record existed on the page for the first one, if you want to add the X Lock on it, the supremum of pre-page will be add X Lock(maybe next key lock, supremum can be add gap lock only) also.
In my case, the records' primary key stored like this:(..., 123440, supremum), (infimum, 123456, ...), the id=123456 is the first record of that page, when add X Lock, first, find the pre-record id=123440, and then add the X lock after this record(id=123440) until match the record witch id=123456, so the supremum is locked also..., because of this lock(supremum), session2 is blocked. if id=123456 is not the first record of page, session2 will not be blocked.
so my doubt is: in my case, the session1 lock the supremum is necessary? if it's necessary, the lock on supremum maybe resulting the difference even if two mysql instances have the same records, just as in a replication.
the record located on the page not all the same even if two instances have the same table sturcture & data(if slave define the table compressed, or innodb page_size not set as the same as master, etc..). if lock on supremum is neccessary, two instances execute the same SQLs, master is OK, but slave conflicted? on master, the two transactions may be committed in one group, but on slave maybe conflicted, slave's parallell replication is OK? also in MySQL 5.7.x, the xa prepare recorded in binlog, if on the master, execute two xa transactions:xa start 'aaa'; delete from table_name where id=xxxx; xa end 'aaa';xa prepare 'aaa';xa start 'bbb';insert into table_name values(xxxx); end 'bbb'; xa prepare 'bbb';xa commit 'aaa'; xa commit 'bbb'on the master, these xa not conflicted, I can committed these transaction, but on the slave, maybe conflicted sometimes, the replication is broken.
how to repeat:
- create a table with primary key(id auto_increment is better)
- insert some records in this table
- use 3 sessions to find the first record on one page(perhaps, there are many pages, any one is OK).how to?
session1: begin; select * from table_name where id=xxx for update // id=xxx must be exist
session2: select * from table_name where id=xxx for update
session3: when session2 is blocked, execute: show engine innodb status\G;
in the transaction section, you can find some details like this:
------- TRX HAS BEEN WAITING 2 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 29 page no 2772 n bits 144 index PRIMARY of table `mydb`.`sbtest1` trx id 2388 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
from the information "space id 29 page no 2772", we can find the page of this record.
then test for next id until the page no become to 2773, remember this id, this record is the first one of page 2773
- delete some records before this id witch get in step 3
- use 2 sessions to do what i described