-
Nisha Gopalakrishnan authored
ALTER TABLE T MAY DEADLOCK Analysis: -------- OPTIMIZE TABLE and ONLINE ALTER TABLE on the same table using INNODB storage engine may cause a deadlock. OPTIMIZE TABLE for INNODB tables is mapped to recreate and analyze of the table. The deadlock occurs because InnoDB: 1) Tries to support "online" ALTER TABLE which at some point tries to upgrade SU metadata lock to X lock, while holding TL_READ thr_lock lock. 2) The analyze phase of the OPTIMIZE TABLE statement, acquires SW metadata lock (which is compatible with SU lock) and then tries to acquire "strong" TL_WRITE lock, which is not compatible with TL_READ. I.e. there is a discrepancy between "weak" MDL and "strong" thr_lock requested. The problem does not occur for other statements and other phases of OPTIMIZE TABLE, since they either acquire strong metadata locks which are not compatible with SU lock or downgrade the request to acquire TL_WRITE, to request for TL_WRITE_ALLOW_WRITE lock, which is compatible with TL_READ. Fix: ---- For the normal ANALYZE TABLE operation, the locks requested are MDL_SHARED_READ and TL_READ_NO_INSERT locks. In case of OPTIMIZE TABLE operation, after recreate, the table is closed and locks are released. The table is re-opened and locks are re-acquired for the analyze phase. Since the analyze operation does not involve changing the data or metadata, a MDL_SHARED_READ and TL_READ_NO_INSERT locks are requested instead of MDL_SHARED_WRITE and TL_WRITE locks(same as the normal ANALYZE TABLE operation). This prevents the deadlock reported.
Nisha Gopalakrishnan authoredALTER TABLE T MAY DEADLOCK Analysis: -------- OPTIMIZE TABLE and ONLINE ALTER TABLE on the same table using INNODB storage engine may cause a deadlock. OPTIMIZE TABLE for INNODB tables is mapped to recreate and analyze of the table. The deadlock occurs because InnoDB: 1) Tries to support "online" ALTER TABLE which at some point tries to upgrade SU metadata lock to X lock, while holding TL_READ thr_lock lock. 2) The analyze phase of the OPTIMIZE TABLE statement, acquires SW metadata lock (which is compatible with SU lock) and then tries to acquire "strong" TL_WRITE lock, which is not compatible with TL_READ. I.e. there is a discrepancy between "weak" MDL and "strong" thr_lock requested. The problem does not occur for other statements and other phases of OPTIMIZE TABLE, since they either acquire strong metadata locks which are not compatible with SU lock or downgrade the request to acquire TL_WRITE, to request for TL_WRITE_ALLOW_WRITE lock, which is compatible with TL_READ. Fix: ---- For the normal ANALYZE TABLE operation, the locks requested are MDL_SHARED_READ and TL_READ_NO_INSERT locks. In case of OPTIMIZE TABLE operation, after recreate, the table is closed and locks are released. The table is re-opened and locks are re-acquired for the analyze phase. Since the analyze operation does not involve changing the data or metadata, a MDL_SHARED_READ and TL_READ_NO_INSERT locks are requested instead of MDL_SHARED_WRITE and TL_WRITE locks(same as the normal ANALYZE TABLE operation). This prevents the deadlock reported.
Loading