GreatSQL社区

搜索

[已解决] 有人研究过源码中locked_tables_mode enum 值的意思么分别什么...

690 3 2022-9-27 16:15

enum enum_locked_tables_mode {
  LTM_NONE = 0,
  LTM_LOCK_TABLES,
  LTM_PRELOCKED,
  LTM_PRELOCKED_UNDER_LOCK_TABLES
};


全部回复(3)
baguai 2022-9-28 14:44:27
  /*
    Enum enum_locked_tables_mode and locked_tables_mode member are
    used to indicate whether the so-called "locked tables mode" is on,
    and what kind of mode is active.

    Locked tables mode is used when it's necessary to open and
    lock many tables at once, for usage across multiple
    (sub-)statements.
    This may be necessary either for queries that use stored functions
    and triggers, in which case the statements inside functions and
    triggers may be executed many times, or for implementation of
    LOCK TABLES, in which case the opened tables are reused by all
    subsequent statements until a call to UNLOCK TABLES.

    The kind of locked tables mode employed for stored functions and
    triggers is also called "prelocked mode".
    In this mode, first open_tables() call to open the tables used
    in a statement analyses all functions used by the statement
    and adds all indirectly used tables to the list of tables to
    open and lock.
    It also marks the parse tree of the statement as requiring
    prelocking. After that, lock_tables() locks the entire list
    of tables and changes THD::locked_tables_modeto LTM_PRELOCKED.
    All statements executed inside functions or triggers
    use the prelocked tables, instead of opening their own ones.
    Prelocked mode is turned off automatically once close_thread_tables()
    of the main statement is called.
  */
  enum enum_locked_tables_mode locked_tables_mode;
baguai 2022-9-28 14:45:27
这个和lock tables和unlock 有关。很容易观察到的状态,LTM_LOCK_TABLES的栈如下:
(gdb) bt
#0  THD::enter_locked_tables_mode (this=0x7ffe900062f0, mode_arg=LTM_LOCK_TABLES) at /newdata/mysql-8.0.23/sql/sql_class.h:3908
#1  0x0000000003dba76f in Locked_tables_list::init_locked_tables (this=0x7ffe90008570, thd=0x7ffe900062f0) at /newdata/mysql-8.0.23/sql/locked_tables_list.cc:114
#2  0x00000000037f4c65 in lock_tables_open_and_lock_tables (thd=0x7ffe900062f0, tables=0x7ffe90106db0) at /newdata/mysql-8.0.23/sql/sql_parse.cc:2600
#3  0x00000000037f7772 in mysql_execute_command (thd=0x7ffe900062f0, first_level=true) at /newdata/mysql-8.0.23/sql/sql_parse.cc:3575
#4  0x00000000037fbf41 in dispatch_sql_command (thd=0x7ffe900062f0, parser_state=0x7ffebc181a50) at /newdata/mysql-8.0.23/sql/sql_parse.cc:4988
#5  0x00000000037f2543 in dispatch_command (thd=0x7ffe900062f0, com_data=0x7ffebc182b00, command=COM_QUERY) at /newdata/mysql-8.0.23/sql/sql_parse.cc:1836
#6  0x00000000037f095e in do_command (thd=0x7ffe900062f0) at /newdata/mysql-8.0.23/sql/sql_parse.cc:1320
#7  0x00000000039c5c91 in handle_connection (arg=0x9bee090) at /newdata/mysql-8.0.23/sql/conn_handler/connection_handler_per_thread.cc:301
#8  0x000000000562ce9c in pfs_spawn_thread (arg=0xab5e670) at /newdata/mysql-8.0.23/storage/perfschema/pfs.cc:2900
#9  0x00007ffff7bc6ea5 in start_thread () from /lib64/libpthread.so.0
#10 0x00007ffff5e388dd in clone () from /lib64/libc.so.6
baguai 2022-9-28 14:47:02
打开后会发现做的这事
if (mode_arg == LTM_LOCK_TABLES) {
      /*
        When entering LOCK TABLES mode we should set explicit duration
        for all metadata locks acquired so far in order to avoid releasing
        them till UNLOCK TABLES statement.
        We don't do this when entering prelocked mode since sub-statements
        don't release metadata locks and restoring status-quo after leaving
        prelocking mode gets complicated.
      */
      mdl_context.set_explicit_duration_for_all_locks();
    }

可以返现实际上这事了DML LOCK的持续周期为 显示 也就是人为加锁和解锁,其他后面两种和routine和trigger 有关,可以自行看看,因为用得不多就不细看了。
xxcmysql

1

主题

0

博客

2

贡献

新手上路

Rank: 1

积分
3

合作电话:010-64087828

社区邮箱:greatsql@greatdb.com

社区公众号
社区小助手
QQ群
GMT+8, 2024-11-22 00:12 , Processed in 0.017773 second(s), 12 queries , Redis On.
快速回复 返回顶部 返回列表