1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

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
This commit is contained in:
Alexander Barkov
2018-06-27 12:53:49 +04:00
parent 1d6bc0f01f
commit 56145be295
7 changed files with 200 additions and 117 deletions

View File

@ -4904,6 +4904,7 @@ public:
*/
virtual int send_data(List<Item> &items)=0;
virtual ~select_result_sink() {};
void reset(THD *thd_arg) { thd= thd_arg; }
};
@ -4981,6 +4982,11 @@ public:
*/
virtual void cleanup();
void set_thd(THD *thd_arg) { thd= thd_arg; }
void reset(THD *thd_arg)
{
select_result_sink::reset(thd_arg);
unit= NULL;
}
#ifdef EMBEDDED_LIBRARY
virtual void begin_dataset() {}
#else
@ -5077,11 +5083,114 @@ public:
elsewhere. (this is used by ANALYZE $stmt feature).
*/
void disable_my_ok_calls() { suppress_my_ok= true; }
void reset(THD *thd_arg)
{
select_result::reset(thd_arg);
suppress_my_ok= false;
}
protected:
bool suppress_my_ok;
};
class sp_cursor_statistics
{
protected:
ulonglong m_fetch_count; // Number of FETCH commands since last OPEN
ulonglong m_row_count; // Number of successful FETCH since last OPEN
bool m_found; // If last FETCH fetched a row
public:
sp_cursor_statistics()
:m_fetch_count(0),
m_row_count(0),
m_found(false)
{ }
bool found() const
{ return m_found; }
ulonglong row_count() const
{ return m_row_count; }
ulonglong fetch_count() const
{ return m_fetch_count; }
void reset() { *this= sp_cursor_statistics(); }
};
/* A mediator between stored procedures and server side cursors */
class sp_lex_keeper;
class sp_cursor: public sp_cursor_statistics
{
private:
/// An interceptor of cursor result set used to implement
/// FETCH <cname> INTO <varlist>.
class Select_fetch_into_spvars: public select_result_interceptor
{
List<sp_variable> *spvar_list;
uint field_count;
bool send_data_to_variable_list(List<sp_variable> &vars, List<Item> &items);
public:
Select_fetch_into_spvars(THD *thd_arg): select_result_interceptor(thd_arg) {}
void reset(THD *thd_arg)
{
select_result_interceptor::reset(thd_arg);
spvar_list= NULL;
field_count= 0;
}
uint get_field_count() { return field_count; }
void set_spvar_list(List<sp_variable> *vars) { spvar_list= vars; }
virtual bool send_eof() { return FALSE; }
virtual int send_data(List<Item> &items);
virtual int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
};
public:
sp_cursor()
:result(NULL),
m_lex_keeper(NULL),
server_side_cursor(NULL)
{ }
sp_cursor(THD *thd_arg, sp_lex_keeper *lex_keeper)
:result(thd_arg),
m_lex_keeper(lex_keeper),
server_side_cursor(NULL)
{}
virtual ~sp_cursor()
{ destroy(); }
sp_lex_keeper *get_lex_keeper() { return m_lex_keeper; }
int open(THD *thd);
int open_view_structure_only(THD *thd);
int close(THD *thd);
my_bool is_open()
{ return MY_TEST(server_side_cursor); }
int fetch(THD *, List<sp_variable> *vars, bool error_on_no_data);
bool export_structure(THD *thd, Row_definition_list *list);
void reset(THD *thd_arg, sp_lex_keeper *lex_keeper)
{
sp_cursor_statistics::reset();
result.reset(thd_arg);
m_lex_keeper= lex_keeper;
server_side_cursor= NULL;
}
private:
Select_fetch_into_spvars result;
sp_lex_keeper *m_lex_keeper;
Server_side_cursor *server_side_cursor;
void destroy();
};
class select_send :public select_result {
/**
True if we have sent result set metadata to the client.