# wait/synch/sxlock/innodb/fil_space_latch
## 待機イベントの意味
stackoverflow によると...
>テーブルスペースに関する操作で取得された latch に対する待機イベント。
大量のレコードを持ってきて JOIN などで一時テーブルを作成する場合、一時テーブルへの I/O 操作が発生し、この待機イベントの増加が確認できる。
ref: https://stackoverflow.com/questions/74840332/mysql-wait-synch-sxlock-innodb-fil-space-latch-understanding
## latch を作成している場所
### fil_space_create()
fil_space_create() の中でのみ "fil_space_latch" キーでの latch は作成されている。
```cpp=
/** Create a space memory object and put it to the fil_system hash table.
The tablespace name is independent from the tablespace file-name.
Error messages are issued to the server log.
@param[in] name Tablespace name
@param[in] id Tablespace identifier
@param[in] flags Tablespace flags
@param[in] purpose Tablespace purpose
@return pointer to created tablespace, to be filled in with fil_node_create()
@retval NULL on failure (such as when the same tablespace exists) */
fil_space_t*
fil_space_create(
const char* name,
ulint id,
ulint flags,
fil_type_t purpose)
```
```cpp=
rw_lock_create(fil_space_latch_key, &space->latch, SYNC_FSP);
```
https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fil/fil0fil.cc#L1407
この関数自体は、tablespace object をメモリに作成し、それを fil_system ハッシュテーブルに置くもの。今のところ意味不明。
- tablespace: テーブルのデータとインデックスを格納するファイル。拡張子は .ibd
- innodb_file_per_table が MySQL 5.6 以降有効になっているので、ibd ファイル = tablespace はテーブルごとに作られる。
- fil_system: テーブルスペースをメモリ上にキャッシュしたもの
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fil/fil0fil.cc#L218
- 型は fil_system_t
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fil/fil0fil.cc#L165
- hash_table_t: tablespaces をハッシュテーブルで持っている
### fil_space_create() が呼ばれてる箇所
以下の6箇所.
- innobase_start_or_create_for_mysql
- innodb engine 起動のための関数
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/srv/srv0start.cc#L1452
- fil_ibd_create
- tablespace 作成
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fil/fil0fil.cc#L3528
- fil_ibd_open
- tablespace を開く
- mysqld が起動した時 or IMPORT TABLESPACE で使用される関数
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fil/fil0fil.cc#L3846
- fil_ibd_load
- tablespace ファイルを開き、InnoDB データ構造に加える
- REDO log を処理している時に呼ばれるということ以外は、fil_ibd_open と似ている
- data dictionary は使えない
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fil/fil0fil.cc#L4472
- Tablespace::open_or_create
- データファイルを開く
- is_temp
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fsp/fsp0space.cc#L101
- SysTablespace::open_or_create
- https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fsp/fsp0sysspace.cc#L865
# 3/4
## rwlock とは
rwlock = [Readers-writer lock](https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock).
読み取りは共有ロックを取得し、書き込みは排他ロックを取得する仕組み。
書き込み優先の rwlock の場合、書き込みリクエストはキューに貯められ、その間読み取りリクエストが新規にロックを取ることはできない。聞いた感じは、MDL の動作と同じっぽい?
そして、[fil_space_create()](https://github.com/mysql/mysql-server/blob/5.7/storage/innobase/fil/fil0fil.cc#L1407)でも、この rwlock を取得している。
>- rwlock は共有リソース (buffer pool のページ、tablespace、データディクショナリ、information_schema)に対するアクセスを制御するために使われる。
>- rwlock は spin lock によって実現されている。
>- rwlock には S(共有), X(排他), SX(共有排他) の3種類のモードがある。
ref: https://mysqlonarm.github.io/Understanding-InnoDB-rwlock-stats/
## tablespace とは
- データとインデックスが入っているファイル構造とそのオブジェクト
- 具体的な構造は以下
https://baotiao.github.io/2020/09/08/innodb-page-management.html

## debug build1
client SQL
```sql
SHOW FULL FIELDS FROM test_1
```
[create_innodb_tmp_table](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/sql/sql_tmp_table.cc#L2234) にブレークポイントを打ってみた。
```
Breakpoint 1, create_innodb_tmp_table (table=0xffff109258d0, keyinfo=0xffff10926890) at /mysql-5.7.12/sql/sql_tmp_table.cc:2236
2236 TABLE_SHARE *share= table->s;
(gdb) bt
#0 create_innodb_tmp_table (table=0xffff109258d0, keyinfo=0xffff10926890) at /mysql-5.7.12/sql/sql_tmp_table.cc:2236
#1 0x0000000001581650 in instantiate_tmp_table (table=0xffff109258d0, keyinfo=0xffff10926890, start_recinfo=0xffff10926930, recinfo=0xffff10005dd8, options=4096, big_tables=0 '\000', trace=0xffff10014808) at /mysql-5.7.12/sql/sql_tmp_table.cc:2350
#2 0x000000000157ee10 in create_tmp_table (thd=0xffff100124c0, param=0xffff10005d98, fields=..., group=0x0, distinct=false, save_sum_fields=false, select_options=4096, rows_limit=18446744073709551615, table_alias=0xffff10010b90 "COLUMNS")
at /mysql-5.7.12/sql/sql_tmp_table.cc:1517
#3 0x000000000154cca4 in create_schema_table (thd=0xffff100124c0, table_list=0xffff100049b0) at /mysql-5.7.12/sql/sql_show.cc:7614
#4 0x000000000154d9e0 in mysql_schema_table (thd=0xffff100124c0, lex=0xffff10014820, table_list=0xffff100049b0) at /mysql-5.7.12/sql/sql_show.cc:7843
#5 0x000000000144c9f0 in open_and_process_table (thd=0xffff100124c0, lex=0xffff10014820, tables=0xffff100049b0, counter=0xffff100148e0, flags=0, prelocking_strategy=0xffff7516d648, has_prelocking_list=false, ot_ctx=0xffff7516d5c8)
at /mysql-5.7.12/sql/sql_base.cc:4997
#6 0x000000000144dd58 in open_tables (thd=0xffff100124c0, start=0xffff7516d630, counter=0xffff100148e0, flags=0, prelocking_strategy=0xffff7516d648) at /mysql-5.7.12/sql/sql_base.cc:5718
#7 0x000000000144ef38 in open_tables_for_query (thd=0xffff100124c0, tables=0xffff100049b0, flags=0) at /mysql-5.7.12/sql/sql_base.cc:6484
#8 0x00000000014d7dac in execute_sqlcom_select (thd=0xffff100124c0, all_tables=0xffff100049b0) at /mysql-5.7.12/sql/sql_parse.cc:5080
#9 0x00000000014cfd58 in mysql_execute_command (thd=0xffff100124c0, first_level=true) at /mysql-5.7.12/sql/sql_parse.cc:2758
#10 0x00000000014d8d98 in mysql_parse (thd=0xffff100124c0, parser_state=0xffff7516ece8) at /mysql-5.7.12/sql/sql_parse.cc:5519
#11 0x00000000014cca24 in dispatch_command (thd=0xffff100124c0, com_data=0xffff7516f8c0, command=COM_QUERY) at /mysql-5.7.12/sql/sql_parse.cc:1429
#12 0x00000000014cb984 in do_command (thd=0xffff100124c0) at /mysql-5.7.12/sql/sql_parse.cc:997
#13 0x000000000160165c in handle_connection (arg=0x32742010) at /mysql-5.7.12/sql/conn_handler/connection_handler_per_thread.cc:301
#14 0x00000000018d59b4 in pfs_spawn_thread (arg=0x31d15ce0) at /mysql-5.7.12/storage/perfschema/pfs.cc:2188
#15 0x0000ffffb817be48 in start_thread (arg=0xffff751701d0) at pthread_create.c:314
#16 0x0000ffffb7f1c320 in clone () at ../ports/sysdeps/unix/sysv/linux/aarch64/nptl/../clone.S:96
```
latch を取ってそうなところは[一箇所あった](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/storage/innobase/handler/ha_innodb.cc#L11926)がそこを通ってたわけではなかった。
あとは重要そうな関数
- [create_tmp_table](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/sql/sql_tmp_table.cc#L653)
- [instantiate_tmp_table](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/sql/sql_tmp_table.cc#L2337)
## debug build2
client SQL
```sql
SHOW FULL FIELDS FROM test_1
```
[rw_lock_x_lock_func](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/storage/innobase/sync/sync0rw.cc#L711) にブレークポイントを打ってみる
backtrace
```
Breakpoint 1, rw_lock_x_lock_func (lock=0x29268a90, pass=0, file_name=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=2613) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
719 ulint i = 0;
(gdb) bt
#0 rw_lock_x_lock_func (lock=0x29268a90, pass=0, file_name=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=2613) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
#1 0x00000000019db094 in pfs_rw_lock_x_lock_func (lock=0x29268a90, pass=0, file_name=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=2613) at /mysql-5.7.12/storage/innobase/include/sync0rw.ic:705
#2 0x00000000019ede78 in mtr_t::x_lock (this=0xffff4c050cf8, lock=0x29268a90, file=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=2613) at /mysql-5.7.12/storage/innobase/include/mtr0mtr.ic:255
#3 0x0000000001a329b0 in mtr_t::x_lock_space (this=0xffff4c050cf8, space_id=25, file=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=2613) at /mysql-5.7.12/storage/innobase/mtr/mtr0mtr.cc:703
#4 0x0000000001cd2f30 in fseg_create_general (space_id=25, page=0, byte_offset=84, has_done_reservation=0, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:2613
#5 0x0000000001cd33dc in fseg_create (space=25, page=0, byte_offset=84, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:2735
#6 0x0000000001bdbc1c in btr_create (type=1, space=25, page_size=..., index_id=1, index=0xfffee4020aa0, btr_redo_create_info=0x0, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/btr/btr0btr.cc:1003
#7 0x0000000001c6b358 in dict_create_index_tree_in_mem (index=0xfffee4020aa0, trx=0xffff84eacd08) at /mysql-5.7.12/storage/innobase/dict/dict0crea.cc:1080
#8 0x0000000001ade514 in row_create_index_for_mysql (index=0xfffee4020aa0, trx=0xffff84eacd08, field_lengths=0x0, handler=0xfffee4010420) at /mysql-5.7.12/storage/innobase/row/row0mysql.cc:3293
#9 0x000000000198df08 in create_clustered_index_when_no_primary (trx=0xffff84eacd08, flags=33, table_name=0xffff4c051538 "tmp/#sql_492a_0") at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:10356
#10 0x000000000197a498 in create_table_info_t::create_table (this=0xffff4c051420) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11592
#11 0x000000000197aff0 in ha_innobase::create (this=0xfffee401eb58, name=0xfffee401e320 "/tmp/#sql_492a_0", form=0xfffee401ca20, create_info=0xffff4c051ba8) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11929
#12 0x0000000001581148 in create_innodb_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0) at /mysql-5.7.12/sql/sql_tmp_table.cc:2250
#13 0x0000000001581650 in instantiate_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0, start_recinfo=0xfffee401da80, recinfo=0xfffee401be38, options=4096, big_tables=0 '\000', trace=0xfffee4002eb8) at /mysql-5.7.12/sql/sql_tmp_table.cc:2350
#14 0x000000000157ee10 in create_tmp_table (thd=0xfffee4000b70, param=0xfffee401bdf8, fields=..., group=0x0, distinct=false, save_sum_fields=false, select_options=4096, rows_limit=18446744073709551615, table_alias=0xfffee40074d0 "COLUMNS")
at /mysql-5.7.12/sql/sql_tmp_table.cc:1517
#15 0x000000000154cca4 in create_schema_table (thd=0xfffee4000b70, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7614
#16 0x000000000154d9e0 in mysql_schema_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7843
#17 0x000000000144c9f0 in open_and_process_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, tables=0xfffee401aa10, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648, has_prelocking_list=false, ot_ctx=0xffff4c0525c8) at /mysql-5.7.12/sql/sql_base.cc:4997
#18 0x000000000144dd58 in open_tables (thd=0xfffee4000b70, start=0xffff4c052630, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648) at /mysql-5.7.12/sql/sql_base.cc:5718
#19 0x000000000144ef38 in open_tables_for_query (thd=0xfffee4000b70, tables=0xfffee401aa10, flags=0) at /mysql-5.7.12/sql/sql_base.cc:6484
#20 0x00000000014d7dac in execute_sqlcom_select (thd=0xfffee4000b70, all_tables=0xfffee401aa10) at /mysql-5.7.12/sql/sql_parse.cc:5080
#21 0x00000000014cfd58 in mysql_execute_command (thd=0xfffee4000b70, first_level=true) at /mysql-5.7.12/sql/sql_parse.cc:2758
#22 0x00000000014d8d98 in mysql_parse (thd=0xfffee4000b70, parser_state=0xffff4c053ce8) at /mysql-5.7.12/sql/sql_parse.cc:5519
#23 0x00000000014cca24 in dispatch_command (thd=0xfffee4000b70, com_data=0xffff4c0548c0, command=COM_QUERY) at /mysql-5.7.12/sql/sql_parse.cc:1429
#24 0x00000000014cb984 in do_command (thd=0xfffee4000b70) at /mysql-5.7.12/sql/sql_parse.cc:997
#25 0x000000000160165c in handle_connection (arg=0x29569ed0) at /mysql-5.7.12/sql/conn_handler/connection_handler_per_thread.cc:301
#26 0x00000000018d59b4 in pfs_spawn_thread (arg=0x28b4a360) at /mysql-5.7.12/storage/perfschema/pfs.cc:2188
#27 0x0000ffff8f072e48 in start_thread (arg=0xffff4c0551d0) at pthread_create.c:314
#28 0x0000ffff8ee13320 in clone () at ../ports/sysdeps/unix/sysv/linux/aarch64/nptl/../clone.S:96
```
[ha_innobase::create](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/storage/innobase/handler/ha_innodb.cc#L11929)
一つのクエリで2回目止まった。1回目との違いは `fsp_reserve_free_extents` が挟まっていること。
```
Breakpoint 1, rw_lock_x_lock_func (lock=0x29268a90, pass=0, file_name=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=3361) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
719 ulint i = 0;
(gdb) bt
#0 rw_lock_x_lock_func (lock=0x29268a90, pass=0, file_name=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=3361) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
#1 0x00000000019db094 in pfs_rw_lock_x_lock_func (lock=0x29268a90, pass=0, file_name=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=3361) at /mysql-5.7.12/storage/innobase/include/sync0rw.ic:705
#2 0x00000000019ede78 in mtr_t::x_lock (this=0xffff4c050cf8, lock=0x29268a90, file=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=3361) at /mysql-5.7.12/storage/innobase/include/mtr0mtr.ic:255
#3 0x0000000001a329b0 in mtr_t::x_lock_space (this=0xffff4c050cf8, space_id=25, file=0x22e0390 "/mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc", line=3361) at /mysql-5.7.12/storage/innobase/mtr/mtr0mtr.cc:703
#4 0x0000000001cd547c in fsp_reserve_free_extents (n_reserved=0xffff4c050b90, space_id=25, n_ext=2, alloc_type=FSP_NORMAL, mtr=0xffff4c050cf8, n_pages=2) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:3361
#5 0x0000000001cd3040 in fseg_create_general (space_id=25, page=0, byte_offset=84, has_done_reservation=0, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:2641
#6 0x0000000001cd33dc in fseg_create (space=25, page=0, byte_offset=84, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:2735
#7 0x0000000001bdbc1c in btr_create (type=1, space=25, page_size=..., index_id=1, index=0xfffee4020aa0, btr_redo_create_info=0x0, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/btr/btr0btr.cc:1003
#8 0x0000000001c6b358 in dict_create_index_tree_in_mem (index=0xfffee4020aa0, trx=0xffff84eacd08) at /mysql-5.7.12/storage/innobase/dict/dict0crea.cc:1080
#9 0x0000000001ade514 in row_create_index_for_mysql (index=0xfffee4020aa0, trx=0xffff84eacd08, field_lengths=0x0, handler=0xfffee4010420) at /mysql-5.7.12/storage/innobase/row/row0mysql.cc:3293
#10 0x000000000198df08 in create_clustered_index_when_no_primary (trx=0xffff84eacd08, flags=33, table_name=0xffff4c051538 "tmp/#sql_492a_0") at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:10356
#11 0x000000000197a498 in create_table_info_t::create_table (this=0xffff4c051420) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11592
#12 0x000000000197aff0 in ha_innobase::create (this=0xfffee401eb58, name=0xfffee401e320 "/tmp/#sql_492a_0", form=0xfffee401ca20, create_info=0xffff4c051ba8) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11929
#13 0x0000000001581148 in create_innodb_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0) at /mysql-5.7.12/sql/sql_tmp_table.cc:2250
#14 0x0000000001581650 in instantiate_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0, start_recinfo=0xfffee401da80, recinfo=0xfffee401be38, options=4096, big_tables=0 '\000', trace=0xfffee4002eb8) at /mysql-5.7.12/sql/sql_tmp_table.cc:2350
#15 0x000000000157ee10 in create_tmp_table (thd=0xfffee4000b70, param=0xfffee401bdf8, fields=..., group=0x0, distinct=false, save_sum_fields=false, select_options=4096, rows_limit=18446744073709551615, table_alias=0xfffee40074d0 "COLUMNS")
at /mysql-5.7.12/sql/sql_tmp_table.cc:1517
#16 0x000000000154cca4 in create_schema_table (thd=0xfffee4000b70, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7614
#17 0x000000000154d9e0 in mysql_schema_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7843
#18 0x000000000144c9f0 in open_and_process_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, tables=0xfffee401aa10, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648, has_prelocking_list=false, ot_ctx=0xffff4c0525c8) at /mysql-5.7.12/sql/sql_base.cc:4997
#19 0x000000000144dd58 in open_tables (thd=0xfffee4000b70, start=0xffff4c052630, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648) at /mysql-5.7.12/sql/sql_base.cc:5718
#20 0x000000000144ef38 in open_tables_for_query (thd=0xfffee4000b70, tables=0xfffee401aa10, flags=0) at /mysql-5.7.12/sql/sql_base.cc:6484
#21 0x00000000014d7dac in execute_sqlcom_select (thd=0xfffee4000b70, all_tables=0xfffee401aa10) at /mysql-5.7.12/sql/sql_parse.cc:5080
#22 0x00000000014cfd58 in mysql_execute_command (thd=0xfffee4000b70, first_level=true) at /mysql-5.7.12/sql/sql_parse.cc:2758
#23 0x00000000014d8d98 in mysql_parse (thd=0xfffee4000b70, parser_state=0xffff4c053ce8) at /mysql-5.7.12/sql/sql_parse.cc:5519
#24 0x00000000014cca24 in dispatch_command (thd=0xfffee4000b70, com_data=0xffff4c0548c0, command=COM_QUERY) at /mysql-5.7.12/sql/sql_parse.cc:1429
#25 0x00000000014cb984 in do_command (thd=0xfffee4000b70) at /mysql-5.7.12/sql/sql_parse.cc:997
#26 0x000000000160165c in handle_connection (arg=0x29569ed0) at /mysql-5.7.12/sql/conn_handler/connection_handler_per_thread.cc:301
#27 0x00000000018d59b4 in pfs_spawn_thread (arg=0x28b4a360) at /mysql-5.7.12/storage/perfschema/pfs.cc:2188
#28 0x0000ffff8f072e48 in start_thread (arg=0xffff4c0551d0) at pthread_create.c:314
#29 0x0000ffff8ee13320 in clone () at ../ports/sysdeps/unix/sysv/linux/aarch64/nptl/../clone.S:96
```
3回目
1回目、2回目との違いは `fseg_alloc_free_page_low`, `fsp_alloc_free_page` が挟まっている。
```
Breakpoint 1, rw_lock_x_lock_func (lock=0x291f5458, pass=0, file_name=0x22bdc78 "/mysql-5.7.12/storage/innobase/buf/buf0buf.cc", line=5348) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
719 ulint i = 0;
(gdb) bt
#0 rw_lock_x_lock_func (lock=0x291f5458, pass=0, file_name=0x22bdc78 "/mysql-5.7.12/storage/innobase/buf/buf0buf.cc", line=5348) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
#1 0x0000000001c226d0 in pfs_rw_lock_x_lock_func (lock=0x291f5458, pass=0, file_name=0x22bdc78 "/mysql-5.7.12/storage/innobase/buf/buf0buf.cc", line=5348) at /mysql-5.7.12/storage/innobase/include/sync0rw.ic:705
#2 0x0000000001c34454 in buf_page_create (page_id=..., page_size=..., mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/buf/buf0buf.cc:5348
#3 0x0000000001cd0960 in fsp_page_create (page_id=..., page_size=..., rw_latch=RW_SX_LATCH, mtr=0xffff4c050cf8, init_mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:1859
#4 0x0000000001cd123c in fsp_alloc_free_page (space=25, page_size=..., hint=0, rw_latch=RW_SX_LATCH, mtr=0xffff4c050cf8, init_mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:2020
#5 0x0000000001cd4a24 in fseg_alloc_free_page_low (space=0x292689e8, page_size=..., seg_inode=0xffff4dbcd832 "", hint=0, direction=111 'o', rw_latch=RW_SX_LATCH, mtr=0xffff4c050cf8, init_mtr=0xffff4c050cf8, has_done_reservation=0)
at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:3099
#6 0x0000000001cd3224 in fseg_create_general (space_id=25, page=0, byte_offset=84, has_done_reservation=0, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:2681
#7 0x0000000001cd33dc in fseg_create (space=25, page=0, byte_offset=84, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/fsp/fsp0fsp.cc:2735
#8 0x0000000001bdbc1c in btr_create (type=1, space=25, page_size=..., index_id=1, index=0xfffee4020aa0, btr_redo_create_info=0x0, mtr=0xffff4c050cf8) at /mysql-5.7.12/storage/innobase/btr/btr0btr.cc:1003
#9 0x0000000001c6b358 in dict_create_index_tree_in_mem (index=0xfffee4020aa0, trx=0xffff84eacd08) at /mysql-5.7.12/storage/innobase/dict/dict0crea.cc:1080
#10 0x0000000001ade514 in row_create_index_for_mysql (index=0xfffee4020aa0, trx=0xffff84eacd08, field_lengths=0x0, handler=0xfffee4010420) at /mysql-5.7.12/storage/innobase/row/row0mysql.cc:3293
#11 0x000000000198df08 in create_clustered_index_when_no_primary (trx=0xffff84eacd08, flags=33, table_name=0xffff4c051538 "tmp/#sql_492a_0") at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:10356
#12 0x000000000197a498 in create_table_info_t::create_table (this=0xffff4c051420) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11592
#13 0x000000000197aff0 in ha_innobase::create (this=0xfffee401eb58, name=0xfffee401e320 "/tmp/#sql_492a_0", form=0xfffee401ca20, create_info=0xffff4c051ba8) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11929
#14 0x0000000001581148 in create_innodb_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0) at /mysql-5.7.12/sql/sql_tmp_table.cc:2250
#15 0x0000000001581650 in instantiate_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0, start_recinfo=0xfffee401da80, recinfo=0xfffee401be38, options=4096, big_tables=0 '\000', trace=0xfffee4002eb8) at /mysql-5.7.12/sql/sql_tmp_table.cc:2350
#16 0x000000000157ee10 in create_tmp_table (thd=0xfffee4000b70, param=0xfffee401bdf8, fields=..., group=0x0, distinct=false, save_sum_fields=false, select_options=4096, rows_limit=18446744073709551615, table_alias=0xfffee40074d0 "COLUMNS")
at /mysql-5.7.12/sql/sql_tmp_table.cc:1517
#17 0x000000000154cca4 in create_schema_table (thd=0xfffee4000b70, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7614
#18 0x000000000154d9e0 in mysql_schema_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7843
#19 0x000000000144c9f0 in open_and_process_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, tables=0xfffee401aa10, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648, has_prelocking_list=false, ot_ctx=0xffff4c0525c8) at /mysql-5.7.12/sql/sql_base.cc:4997
#20 0x000000000144dd58 in open_tables (thd=0xfffee4000b70, start=0xffff4c052630, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648) at /mysql-5.7.12/sql/sql_base.cc:5718
#21 0x000000000144ef38 in open_tables_for_query (thd=0xfffee4000b70, tables=0xfffee401aa10, flags=0) at /mysql-5.7.12/sql/sql_base.cc:6484
#22 0x00000000014d7dac in execute_sqlcom_select (thd=0xfffee4000b70, all_tables=0xfffee401aa10) at /mysql-5.7.12/sql/sql_parse.cc:5080
#23 0x00000000014cfd58 in mysql_execute_command (thd=0xfffee4000b70, first_level=true) at /mysql-5.7.12/sql/sql_parse.cc:2758
#24 0x00000000014d8d98 in mysql_parse (thd=0xfffee4000b70, parser_state=0xffff4c053ce8) at /mysql-5.7.12/sql/sql_parse.cc:5519
#25 0x00000000014cca24 in dispatch_command (thd=0xfffee4000b70, com_data=0xffff4c0548c0, command=COM_QUERY) at /mysql-5.7.12/sql/sql_parse.cc:1429
#26 0x00000000014cb984 in do_command (thd=0xfffee4000b70) at /mysql-5.7.12/sql/sql_parse.cc:997
#27 0x000000000160165c in handle_connection (arg=0x29569ed0) at /mysql-5.7.12/sql/conn_handler/connection_handler_per_thread.cc:301
#28 0x00000000018d59b4 in pfs_spawn_thread (arg=0x28b4a360) at /mysql-5.7.12/storage/perfschema/pfs.cc:2188
#29 0x0000ffff8f072e48 in start_thread (arg=0xffff4c0551d0) at pthread_create.c:314
#30 0x0000ffff8ee13320 in clone () at ../ports/sysdeps/unix/sysv/linux/aarch64/nptl/../clone.S:96
```
4回目は1回目と同じ、5回目は2回目と同じ backtrace だった。
6回目
```
Breakpoint 1, rw_lock_x_lock_func (lock=0xfffee40242a8, pass=0, file_name=0x22cf3c8 "/mysql-5.7.12/storage/innobase/dict/dict0dict.cc", line=373) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
719 ulint i = 0;
(gdb) bt
#0 rw_lock_x_lock_func (lock=0xfffee40242a8, pass=0, file_name=0x22cf3c8 "/mysql-5.7.12/storage/innobase/dict/dict0dict.cc", line=373) at /mysql-5.7.12/storage/innobase/sync/sync0rw.cc:719
#1 0x0000000001c6fd90 in pfs_rw_lock_x_lock_func (lock=0xfffee40242a8, pass=0, file_name=0x22cf3c8 "/mysql-5.7.12/storage/innobase/dict/dict0dict.cc", line=373) at /mysql-5.7.12/storage/innobase/include/sync0rw.ic:705
#2 0x0000000001c75848 in dict_table_stats_lock (table=0xfffee4010420, latch_mode=2) at /mysql-5.7.12/storage/innobase/dict/dict0dict.cc:373
#3 0x0000000001ca2940 in dict_stats_empty_table (table=0xfffee4010420) at /mysql-5.7.12/storage/innobase/dict/dict0stats.cc:545
#4 0x0000000001ca7df0 in dict_stats_update (table=0xfffee4010420, stats_upd_option=DICT_STATS_EMPTY_TABLE) at /mysql-5.7.12/storage/innobase/dict/dict0stats.cc:3125
#5 0x000000000197ac84 in create_table_info_t::create_table_update_dict (this=0xffff4c051420) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11809
#6 0x000000000197b0c8 in ha_innobase::create (this=0xfffee401eb58, name=0xfffee401e320 "/tmp/#sql_492a_0", form=0xfffee401ca20, create_info=0xffff4c051ba8) at /mysql-5.7.12/storage/innobase/handler/ha_innodb.cc:11944
#7 0x0000000001581148 in create_innodb_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0) at /mysql-5.7.12/sql/sql_tmp_table.cc:2250
#8 0x0000000001581650 in instantiate_tmp_table (table=0xfffee401ca20, keyinfo=0xfffee401d9e0, start_recinfo=0xfffee401da80, recinfo=0xfffee401be38, options=4096, big_tables=0 '\000', trace=0xfffee4002eb8) at /mysql-5.7.12/sql/sql_tmp_table.cc:2350
#9 0x000000000157ee10 in create_tmp_table (thd=0xfffee4000b70, param=0xfffee401bdf8, fields=..., group=0x0, distinct=false, save_sum_fields=false, select_options=4096, rows_limit=18446744073709551615, table_alias=0xfffee40074d0 "COLUMNS")
at /mysql-5.7.12/sql/sql_tmp_table.cc:1517
#10 0x000000000154cca4 in create_schema_table (thd=0xfffee4000b70, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7614
#11 0x000000000154d9e0 in mysql_schema_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, table_list=0xfffee401aa10) at /mysql-5.7.12/sql/sql_show.cc:7843
#12 0x000000000144c9f0 in open_and_process_table (thd=0xfffee4000b70, lex=0xfffee4002ed0, tables=0xfffee401aa10, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648, has_prelocking_list=false, ot_ctx=0xffff4c0525c8) at /mysql-5.7.12/sql/sql_base.cc:4997
#13 0x000000000144dd58 in open_tables (thd=0xfffee4000b70, start=0xffff4c052630, counter=0xfffee4002f90, flags=0, prelocking_strategy=0xffff4c052648) at /mysql-5.7.12/sql/sql_base.cc:5718
#14 0x000000000144ef38 in open_tables_for_query (thd=0xfffee4000b70, tables=0xfffee401aa10, flags=0) at /mysql-5.7.12/sql/sql_base.cc:6484
#15 0x00000000014d7dac in execute_sqlcom_select (thd=0xfffee4000b70, all_tables=0xfffee401aa10) at /mysql-5.7.12/sql/sql_parse.cc:5080
#16 0x00000000014cfd58 in mysql_execute_command (thd=0xfffee4000b70, first_level=true) at /mysql-5.7.12/sql/sql_parse.cc:2758
#17 0x00000000014d8d98 in mysql_parse (thd=0xfffee4000b70, parser_state=0xffff4c053ce8) at /mysql-5.7.12/sql/sql_parse.cc:5519
#18 0x00000000014cca24 in dispatch_command (thd=0xfffee4000b70, com_data=0xffff4c0548c0, command=COM_QUERY) at /mysql-5.7.12/sql/sql_parse.cc:1429
#19 0x00000000014cb984 in do_command (thd=0xfffee4000b70) at /mysql-5.7.12/sql/sql_parse.cc:997
#20 0x000000000160165c in handle_connection (arg=0x29569ed0) at /mysql-5.7.12/sql/conn_handler/connection_handler_per_thread.cc:301
#21 0x00000000018d59b4 in pfs_spawn_thread (arg=0x28b4a360) at /mysql-5.7.12/storage/perfschema/pfs.cc:2188
#22 0x0000ffff8f072e48 in start_thread (arg=0xffff4c0551d0) at pthread_create.c:314
#23 0x0000ffff8ee13320 in clone () at ../ports/sysdeps/unix/sysv/linux/aarch64/nptl/../clone.S:96
```
この後も何回か止まる。
重要そうな場所:
- [create_table_info_t::create_table() の index 作成箇所](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/storage/innobase/handler/ha_innodb.cc#L11591)
- [fsp_reserve_free_extents](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/storage/innobase/fsp/fsp0fsp.cc#L3342)
- 表領域から空きページを予約する。表領域から複数のページを使用する可能性のある全てのミニトランザクションは、事前にこの関数を呼び出し、B-treeページ分割のような操作を完全に行えるように、十分な空きエクステントを予約しておく必要があります。
- [mtr_t::x_lock_space](https://github.com/mysql/mysql-server/blob/99077a555bc197b8be46287543cbde36ab4da8dd/storage/innobase/mtr/mtr0mtr.cc#L668)
- `Acquire a tablespace X-latch.`
- 対あり
結論: 内部一時テーブルをディスクに作るときに InnoDB エンジンが使われる。InnoDB エンジンは PK を指定しないでテーブルを作ると、内部的に PK 相当のインデックスを作成するが、それは内部一時テーブルに対しても同様である。その際、テーブルスペースに対して事前に空き領域を確保するところで、rwlock (mode = X) を取っている。