Bug #102375 A deadlock occurred while truncate was executing
Submitted: 26 Jan 2021 8:57 Modified: 26 Jan 2021 15:17
Reporter: linfeng chen Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S5 (Performance)
Version:5.7.21 OS:Any
Assigned to: CPU Architecture:Any

[26 Jan 2021 8:57] linfeng chen
Description:
A deadlock occurred while truncate was executing;
When I execute truncate it locks the entire adaptive hash table.
When I find a block that has an adaptive hash.
I'm going to add an X lock to the adaptive hash table,Then a deadlock occurs.
The following code.
fil_reinit_space_header_for_table
	btr_search_s_lock_all();
	DEBUG_SYNC_C("simulate_buffer_pool_scan");
	buf_LRU_flush_or_remove_pages(id, BUF_REMOVE_ALL_NO_WRITE, 0);
	btr_search_s_unlock_all();
            buf_LRU_drop_page_hash_for_tablespace
                buf_LRU_drop_page_hash_batch
                    btr_search_drop_page_hash_when_freed
                        btr_search_drop_page_hash_index
                            btr_search_drop_page_hash_index
                                rw_lock_x_lock(latch);

How to repeat:
1:Comment out the following code
if (ahi) {
		//btr_search_drop_page_hash_when_freed(page_id, page_size);
	}
if (ahi) {
		for (i = 0; i < FSP_EXTENT_SIZE; i++) {
			if (!xdes_mtr_get_bit(descr, XDES_FREE_BIT, i, mtr)) {

				/* Drop search system page hash index
				if the page is found in the pool and
				is hashed */
                
				//btr_search_drop_page_hash_when_freed(
				//	page_id_t(space,
				//		  first_page_in_extent + i),
				//	page_size);
			}
		}
	}
2:Frequent access to table T1 causes table T1 to generate adaptive hash data
3:truncate T1;

Suggested fix:
When we execute the function:fil_reinit_space_header_for_table,We don't have to clean up adaptive hashes.
If we clean up adaptive hashes, a deadlock will occur.
[26 Jan 2021 15:17] MySQL Verification Team
Hi Mr. chen,

Thank you for your bug report.

However, this is not a bug.

Adaptive hashes have to be locked during truncate, because AHI points to the rows and many of those rows might be among those that are removed.

Not a bug.