From 56145be2951e0085848187bf6df8a0269e53fc7a Mon Sep 17 00:00:00 2001 From: Alexander Barkov <bar@mariadb.com> Date: Wed, 27 Jun 2018 12:53:49 +0400 Subject: MDEV-16584 SP with a cursor inside a loop wastes THD memory aggressively Problem: push_cursor() created sp_cursor instances on THD::main_mem_root, which is freed only after the SP instructions loop. Changes: - Moving sp_cursor declaration from sp_rcontext.h to sql_class.h - Deriving sp_instr_cpush from sp_cursor. So now sp_cursor is created only once (at the SP parse time) and then reused on all loop iterations - Adding a new method reset() into sp_cursor (and its parent classes) to reset an sp_cursor instance before reuse. - Moving former sp_cursor members m_fetch_count, m_row_count, m_found into a separate class sp_cursor_statistics. This helps to reuse the code in sp_cursor constructors, and in sp_cursor::reset() - Adding a helper method sp_rcontext::pop_cursor(). - Adding "THD*" parameter to so_rcontext::pop_cursors() and pop_all_cursors() - Removing "new" and "delete" from sp_rcontext::push_cursor() and sp_rconext::pop_cursor(). - Fixing sp_cursor not to derive from Sql_alloc, as it's now allocated only as a part of sp_instr_cpush (and not allocated separately). - Moving lex_keeper->disable_query_cache() from sp_cursor::sp_cursor() to sp_instr_cpush::execute(). - Adding tests --- sql/sp_head.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'sql/sp_head.cc') diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 40e5a71de21..6b116cb4550 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -1406,7 +1406,7 @@ sp_head::execute(THD *thd, bool merge_da_on_success) /* Only pop cursors when we're done with group aggregate running. */ if (m_chistics.agg_type != GROUP_AGGREGATE || (m_chistics.agg_type == GROUP_AGGREGATE && thd->spcont->quit_func)) - thd->spcont->pop_all_cursors(); // To avoid memory leaks after an error + thd->spcont->pop_all_cursors(thd); // To avoid memory leaks after an error /* Restore all saved */ if (m_chistics.agg_type == GROUP_AGGREGATE) @@ -4186,11 +4186,13 @@ sp_instr_cpush::execute(THD *thd, uint *nextp) { DBUG_ENTER("sp_instr_cpush::execute"); - int ret= thd->spcont->push_cursor(thd, &m_lex_keeper); + sp_cursor::reset(thd, &m_lex_keeper); + m_lex_keeper.disable_query_cache(); + thd->spcont->push_cursor(this); *nextp= m_ip+1; - DBUG_RETURN(ret); + DBUG_RETURN(false); } @@ -4224,7 +4226,7 @@ int sp_instr_cpop::execute(THD *thd, uint *nextp) { DBUG_ENTER("sp_instr_cpop::execute"); - thd->spcont->pop_cursors(m_count); + thd->spcont->pop_cursors(thd, m_count); *nextp= m_ip+1; DBUG_RETURN(0); } -- cgit v1.2.1