Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-26712

row events never reset thd->mem_root

    XMLWordPrintable

Details

    Description

      row event execution starts from Rows_log_event::do_apply_event which calls open_and_lock_tables() which calls lock_tables(), which does

      if (!(ptr=start=(TABLE**) thd->alloc(sizeof(TABLE*)*count)))
      

      thus allocating an array of points of the thd's memroot. This is just few bytes, 8 in the test below. But this memory is never freed, not by row events at least. So, by creating a transaction with a huge number of row events we can consume any amount of memory.

      source include/have_innodb.inc;
      source include/have_binlog_format_row.inc;
      source include/master-slave.inc;
      create table t1 (a int) engine=innodb;
      insert t1 values (1);
       
      sync_slave_with_master;
      stop slave;
      set global max_session_mem_used=10*1024*1024;
      start slave;
       
      connection master;
      let $a=1000000;
      start transaction;
      disable_query_log;
      while ($a) {
        update t1 set a=a+1;
        dec $a;
      }
      enable_query_log;
      commit;
      drop table t1;
      sync_slave_with_master;
      set global max_session_mem_used=default;
      source include/rpl_end.inc;
      

      To fix that, rbr events must do

      free_root(thd->mem_root, MYF(MY_KEEP_PREALLOC));
      

      somewhere. Where it's safe, because it'll destroy anything allocated on the thd memroot.
      This patch appears to be working fine, it frees thd memroot after closing all opened tables:

      --- a/sql/rpl_rli.cc
      +++ b/sql/rpl_rli.cc
      @@ -2326,6 +2326,7 @@ void rpl_group_info::cleanup_context(THD *thd, bool error)
         */
         reset_row_stmt_start_timestamp();
         unset_long_find_row_note_printed();
      +  free_root(thd->mem_root, MYF(MY_KEEP_PREALLOC));
       
         DBUG_EXECUTE_IF("inject_sleep_gtid_100_x_x", {
             if (current_gtid.domain_id == 100)
      

      Attachments

        Issue Links

          Activity

            People

              Elkin Andrei Elkin
              serg Sergei Golubchik
              Votes:
              2 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.