Skip to content

Commit

Permalink
perfschema sp instrumentation related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vuvova committed Mar 10, 2020
1 parent d5a0069 commit 70e7b50
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 83 deletions.
4 changes: 4 additions & 0 deletions sql/event_data_objects.cc
Expand Up @@ -32,6 +32,7 @@
#include "event_db_repository.h"
#include "sp_head.h"
#include "sql_show.h" // append_definer, append_identifier
#include "mysql/psi/mysql_sp.h"
#ifdef WITH_WSREP
#include "wsrep_trans_observer.h"
#endif /* WITH_WSREP */
Expand Down Expand Up @@ -1456,6 +1457,9 @@ Event_job_data::execute(THD *thd, bool drop)
sphead->set_creation_ctx(creation_ctx);
sphead->optimize();

sphead->m_sp_share= MYSQL_GET_SP_SHARE(SP_TYPE_EVENT,
dbname.str, dbname.length,
name.str, name.length);
ret= sphead->execute_procedure(thd, &empty_item_list);
/*
There is no pre-locking and therefore there should be no
Expand Down
4 changes: 4 additions & 0 deletions sql/event_queue.cc
Expand Up @@ -24,6 +24,7 @@
#include "tztime.h" // my_tz_find, my_tz_OFFSET0, struct Time_zone
#include "log.h" // sql_print_error
#include "sql_class.h" // struct THD
#include "mysql/psi/mysql_sp.h"

/**
@addtogroup Event_Scheduler
Expand Down Expand Up @@ -351,6 +352,9 @@ Event_queue::drop_matching_events(THD *thd, const LEX_CSTRING *pattern,
is ok.
*/
queue_remove(&queue, i);
/* Drop statistics for this stored program from performance schema. */
MYSQL_DROP_SP(SP_TYPE_EVENT, et->dbname.str, et->dbname.length,
et->name.str, et->name.length);
delete et;
}
else
Expand Down
4 changes: 4 additions & 0 deletions sql/events.cc
Expand Up @@ -34,6 +34,7 @@
#include "sp_head.h" // for Stored_program_creation_ctx
#include "set_var.h"
#include "lock.h" // lock_object_name
#include "mysql/psi/mysql_sp.h"

/**
@addtogroup Event_Scheduler
Expand Down Expand Up @@ -620,6 +621,9 @@ Events::drop_event(THD *thd, const LEX_CSTRING *dbname,
/* Binlog the drop event. */
DBUG_ASSERT(thd->query() && thd->query_length());
ret= write_bin_log(thd, TRUE, thd->query(), thd->query_length());
/* Drop statistics for this stored program from performance schema. */
MYSQL_DROP_SP(SP_TYPE_EVENT,
dbname->str, dbname->length, name->str, name->length);
}

thd->restore_stmt_binlog_format(save_binlog_format);
Expand Down
3 changes: 3 additions & 0 deletions sql/mysqld.cc
Expand Up @@ -113,6 +113,7 @@
#include "sp_rcontext.h"
#include "sp_cache.h"
#include "sql_reload.h" // reload_acl_and_cache
#include "sp_head.h" // init_sp_psi_keys

#ifdef HAVE_POLL_H
#include <poll.h>
Expand Down Expand Up @@ -9698,6 +9699,8 @@ void init_server_psi_keys(void)
count= array_elements(sql_statement_info);
mysql_statement_register(category, sql_statement_info, count);

init_sp_psi_keys();

category= "com";
init_com_statement_info();

Expand Down
58 changes: 39 additions & 19 deletions sql/sp.cc
Expand Up @@ -34,6 +34,7 @@
#include "lock.h" // lock_object_name

#include <my_user.h>
#include "mysql/psi/mysql_sp.h"

sp_cache **Sp_handler_procedure::get_cache(THD *thd) const
{
Expand Down Expand Up @@ -718,7 +719,7 @@ Sp_handler::db_find_routine(THD *thd,

table->field[MYSQL_PROC_FIELD_PARAM_LIST]->val_str_nopad(thd->mem_root,
&params);
if (type() != TYPE_ENUM_FUNCTION)
if (type() != SP_TYPE_FUNCTION)
returns= empty_clex_str;
else if (table->field[MYSQL_PROC_FIELD_RETURNS]->val_str_nopad(thd->mem_root,
&returns))
Expand Down Expand Up @@ -866,6 +867,8 @@ static sp_head *sp_compile(THD *thd, String *defstr, sql_mode_t sql_mode,
thd->spcont= old_spcont;
thd->variables.sql_mode= old_sql_mode;
thd->variables.select_limit= old_select_limit;
if (sp != NULL)
sp->init_psi_share();
return sp;
}

Expand Down Expand Up @@ -1001,7 +1004,7 @@ Sp_handler::db_load_routine(THD *thd, const Database_qualified_name *name,
(*sphp)->set_creation_ctx(creation_ctx);
(*sphp)->optimize();

if (type() == TYPE_ENUM_PACKAGE_BODY)
if (type() == SP_TYPE_PACKAGE_BODY)
{
sp_package *package= (*sphp)->get_package();
List_iterator<LEX> it(package->m_routine_implementations);
Expand Down Expand Up @@ -1104,6 +1107,9 @@ Sp_handler::sp_drop_routine_internal(THD *thd,
DBUG_ASSERT(spc);
if ((sp= sp_cache_lookup(spc, name)))
sp_cache_flush_obsolete(spc, &sp);
/* Drop statistics for this stored program from performance schema. */
MYSQL_DROP_SP(type(), name->m_db.str, name->m_db.length,
name->m_name.str, name->m_name.length);
DBUG_RETURN(SP_OK);
}

Expand Down Expand Up @@ -1231,16 +1237,17 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
if (lex->create_info.or_replace())
{
switch (type()) {
case TYPE_ENUM_PACKAGE:
case SP_TYPE_PACKAGE:
// Drop together with its PACKAGE BODY mysql.proc record
ret= sp_handler_package_spec.sp_find_and_drop_routine(thd, table, sp);
break;
case TYPE_ENUM_PACKAGE_BODY:
case TYPE_ENUM_FUNCTION:
case TYPE_ENUM_PROCEDURE:
case SP_TYPE_PACKAGE_BODY:
case SP_TYPE_FUNCTION:
case SP_TYPE_PROCEDURE:
ret= sp_drop_routine_internal(thd, sp, table);
break;
case TYPE_ENUM_TRIGGER:
case SP_TYPE_TRIGGER:
case SP_TYPE_EVENT:
DBUG_ASSERT(0);
ret= SP_OK;
}
Expand All @@ -1257,7 +1264,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
ret= FALSE;

// Setting retstr as it is used for logging.
if (type() == TYPE_ENUM_FUNCTION)
if (type() == SP_TYPE_FUNCTION)
{
sp_returns_type(thd, retstr, sp);
returns= retstr.lex_cstring();
Expand Down Expand Up @@ -1340,7 +1347,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
table->field[MYSQL_PROC_FIELD_PARAM_LIST]->
store(sp->m_params, system_charset_info);

if (type() == TYPE_ENUM_FUNCTION)
if (type() == SP_TYPE_FUNCTION)
{
sp_returns_type(thd, retstr, sp);
returns= retstr.lex_cstring();
Expand Down Expand Up @@ -1372,7 +1379,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
store(sp->comment(), system_charset_info);
}

if (type() == TYPE_ENUM_FUNCTION &&
if (type() == SP_TYPE_FUNCTION &&
!trust_function_creators && mysql_bin_log.is_open())
{
if (!sp->detistic())
Expand Down Expand Up @@ -1624,7 +1631,7 @@ Sp_handler::sp_update_routine(THD *thd, const Database_qualified_name *name,

if ((ret= db_find_routine_aux(thd, name, table)) == SP_OK)
{
if (type() == TYPE_ENUM_FUNCTION && ! trust_function_creators &&
if (type() == SP_TYPE_FUNCTION && ! trust_function_creators &&
mysql_bin_log.is_open() &&
(chistics->daccess == SP_CONTAINS_SQL ||
chistics->daccess == SP_MODIFIES_SQL_DATA))
Expand Down Expand Up @@ -1770,7 +1777,7 @@ bool lock_db_routines(THD *thd, const char *db)

longlong sp_type= table->field[MYSQL_PROC_MYSQL_TYPE]->val_int();
MDL_request *mdl_request= new (thd->mem_root) MDL_request;
const Sp_handler *sph= Sp_handler::handler((stored_procedure_type)
const Sp_handler *sph= Sp_handler::handler((enum_sp_type)
sp_type);
if (!sph)
sph= &sp_handler_procedure;
Expand Down Expand Up @@ -1813,14 +1820,16 @@ sp_drop_db_routines(THD *thd, const char *db)
uint key_len;
MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
uchar keybuf[MAX_KEY_LENGTH];
size_t db_length= strlen(db);
Sql_mode_instant_remove smir(thd, MODE_PAD_CHAR_TO_FULL_LENGTH); // see below
DBUG_ENTER("sp_drop_db_routines");
DBUG_PRINT("enter", ("db: %s", db));

ret= SP_OPEN_TABLE_FAILED;
if (!(table= open_proc_table_for_update(thd)))
goto err;

table->field[MYSQL_PROC_FIELD_DB]->store(db, strlen(db), system_charset_info);
table->field[MYSQL_PROC_FIELD_DB]->store(db, db_length, system_charset_info);
key_len= table->key_info->key_part[0].store_length;
table->field[MYSQL_PROC_FIELD_DB]->get_key_image(keybuf, key_len, Field::itRAW);

Expand All @@ -1839,7 +1848,18 @@ sp_drop_db_routines(THD *thd, const char *db)
do
{
if (! table->file->ha_delete_row(table->record[0]))
{
deleted= TRUE; /* We deleted something */
#ifdef HAVE_PSI_SP_INTERFACE
String buf;
// the following assumes MODE_PAD_CHAR_TO_FULL_LENGTH being *unset*
String *name= table->field[MYSQL_PROC_FIELD_NAME]->val_str(&buf);

enum_sp_type sp_type= (enum_sp_type) table->field[MYSQL_PROC_MYSQL_TYPE]->ptr[0];
/* Drop statistics for this stored program from performance schema. */
MYSQL_DROP_SP(sp_type, db, db_length, name->ptr(), name->length());
#endif
}
else
{
ret= SP_DELETE_ROW_FAILED;
Expand Down Expand Up @@ -2013,7 +2033,7 @@ Sp_handler::sp_clone_and_link_routine(THD *thd,
DBUG_RETURN(0);
}

if (type() == TYPE_ENUM_FUNCTION)
if (type() == SP_TYPE_FUNCTION)
{
sp_returns_type(thd, retstr, sp);
returns= retstr.lex_cstring();
Expand Down Expand Up @@ -2361,7 +2381,7 @@ is_package_public_routine(THD *thd,
const LEX_CSTRING &db,
const LEX_CSTRING &package,
const LEX_CSTRING &routine,
stored_procedure_type type)
enum_sp_type type)
{
sp_head *sp= NULL;
Database_qualified_name tmp(db, package);
Expand Down Expand Up @@ -2395,7 +2415,7 @@ is_package_public_routine_quick(THD *thd,
const LEX_CSTRING &db,
const LEX_CSTRING &pkgname,
const LEX_CSTRING &name,
stored_procedure_type type)
enum_sp_type type)
{
Database_qualified_name tmp(db, pkgname);
sp_head *sp= sp_cache_lookup(&thd->sp_package_spec_cache, &tmp);
Expand All @@ -2414,7 +2434,7 @@ static bool
is_package_body_routine(THD *thd, sp_package *pkg,
const LEX_CSTRING &name1,
const LEX_CSTRING &name2,
stored_procedure_type type)
enum_sp_type type)
{
return Sp_handler::eq_routine_name(pkg->m_name, name1) &&
(pkg->m_routine_declarations.find(name2, type) ||
Expand Down Expand Up @@ -2842,7 +2862,7 @@ Sp_handler::sp_cache_package_routine(THD *thd,
bool lookup_only, sp_head **sp) const
{
DBUG_ENTER("sp_cache_package_routine");
DBUG_ASSERT(type() == TYPE_ENUM_FUNCTION || type() == TYPE_ENUM_PROCEDURE);
DBUG_ASSERT(type() == SP_TYPE_FUNCTION || type() == SP_TYPE_PROCEDURE);
sp_name pkgname(&name->m_db, &pkgname_cstr, false);
sp_head *ph= NULL;
int ret= sp_handler_package_body.sp_cache_routine(thd, &pkgname,
Expand Down Expand Up @@ -2941,7 +2961,7 @@ Sp_handler::show_create_sp(THD *thd, String *buf,
buf->append('(');
buf->append(&params);
buf->append(')');
if (type() == TYPE_ENUM_FUNCTION)
if (type() == SP_TYPE_FUNCTION)
{
if (sql_mode & MODE_ORACLE)
buf->append(STRING_WITH_LEN(" RETURN "));
Expand Down
45 changes: 24 additions & 21 deletions sql/sp.h
Expand Up @@ -47,17 +47,18 @@ template <typename T> class SQL_I_List;
/*
Values for the type enum. This reflects the order of the enum declaration
in the CREATE TABLE command.
See also storage/perfschema/my_thread.h
*/
enum stored_procedure_type
enum enum_sp_type
{
TYPE_ENUM_FUNCTION=1,
TYPE_ENUM_PROCEDURE=2,
TYPE_ENUM_PACKAGE=3,
TYPE_ENUM_PACKAGE_BODY=4,
TYPE_ENUM_TRIGGER=5
SP_TYPE_FUNCTION=1,
SP_TYPE_PROCEDURE=2,
SP_TYPE_PACKAGE=3,
SP_TYPE_PACKAGE_BODY=4,
SP_TYPE_TRIGGER=5,
SP_TYPE_EVENT=6,
};


class Sp_handler
{
bool sp_resolve_package_routine_explicit(THD *thd,
Expand Down Expand Up @@ -120,13 +121,13 @@ class Sp_handler
public:
virtual ~Sp_handler() {}
static const Sp_handler *handler(enum enum_sql_command cmd);
static const Sp_handler *handler(stored_procedure_type type);
static const Sp_handler *handler(enum_sp_type type);
static const Sp_handler *handler(MDL_key::enum_mdl_namespace ns);
/*
Return a handler only those SP objects that store
definitions in the mysql.proc system table
*/
static const Sp_handler *handler_mysql_proc(stored_procedure_type type)
static const Sp_handler *handler_mysql_proc(enum_sp_type type)
{
const Sp_handler *sph= handler(type);
return sph ? sph->sp_handler_mysql_proc() : NULL;
Expand All @@ -153,7 +154,7 @@ class Sp_handler
{
return this;
}
virtual stored_procedure_type type() const= 0;
virtual enum_sp_type type() const= 0;
virtual LEX_CSTRING type_lex_cstring() const= 0;
virtual LEX_CSTRING empty_body_lex_cstring(sql_mode_t mode) const
{
Expand Down Expand Up @@ -248,7 +249,7 @@ class Sp_handler
class Sp_handler_procedure: public Sp_handler
{
public:
stored_procedure_type type() const { return TYPE_ENUM_PROCEDURE; }
enum_sp_type type() const { return SP_TYPE_PROCEDURE; }
LEX_CSTRING type_lex_cstring() const
{
static LEX_CSTRING m_type_str= { STRING_WITH_LEN("PROCEDURE")};
Expand Down Expand Up @@ -298,7 +299,7 @@ class Sp_handler_package_procedure: public Sp_handler_procedure
class Sp_handler_function: public Sp_handler
{
public:
stored_procedure_type type() const { return TYPE_ENUM_FUNCTION; }
enum_sp_type type() const { return SP_TYPE_FUNCTION; }
LEX_CSTRING type_lex_cstring() const
{
static LEX_CSTRING m_type_str= { STRING_WITH_LEN("FUNCTION")};
Expand Down Expand Up @@ -367,7 +368,7 @@ class Sp_handler_package_spec: public Sp_handler_package
const Database_qualified_name *name)
const;
public:
stored_procedure_type type() const { return TYPE_ENUM_PACKAGE; }
enum_sp_type type() const { return SP_TYPE_PACKAGE; }
LEX_CSTRING type_lex_cstring() const
{
static LEX_CSTRING m_type_str= {STRING_WITH_LEN("PACKAGE")};
Expand Down Expand Up @@ -400,7 +401,7 @@ class Sp_handler_package_spec: public Sp_handler_package
class Sp_handler_package_body: public Sp_handler_package
{
public:
stored_procedure_type type() const { return TYPE_ENUM_PACKAGE_BODY; }
enum_sp_type type() const { return SP_TYPE_PACKAGE_BODY; }
LEX_CSTRING type_lex_cstring() const
{
static LEX_CSTRING m_type_str= {STRING_WITH_LEN("PACKAGE BODY")};
Expand Down Expand Up @@ -433,7 +434,7 @@ class Sp_handler_package_body: public Sp_handler_package
class Sp_handler_trigger: public Sp_handler
{
public:
stored_procedure_type type() const { return TYPE_ENUM_TRIGGER; }
enum_sp_type type() const { return SP_TYPE_TRIGGER; }
LEX_CSTRING type_lex_cstring() const
{
static LEX_CSTRING m_type_str= { STRING_WITH_LEN("TRIGGER")};
Expand Down Expand Up @@ -492,19 +493,21 @@ inline const Sp_handler *Sp_handler::handler(enum_sql_command cmd)
}


inline const Sp_handler *Sp_handler::handler(stored_procedure_type type)
inline const Sp_handler *Sp_handler::handler(enum_sp_type type)
{
switch (type) {
case TYPE_ENUM_PROCEDURE:
case SP_TYPE_PROCEDURE:
return &sp_handler_procedure;
case TYPE_ENUM_FUNCTION:
case SP_TYPE_FUNCTION:
return &sp_handler_function;
case TYPE_ENUM_PACKAGE:
case SP_TYPE_PACKAGE:
return &sp_handler_package_spec;
case TYPE_ENUM_PACKAGE_BODY:
case SP_TYPE_PACKAGE_BODY:
return &sp_handler_package_body;
case TYPE_ENUM_TRIGGER:
case SP_TYPE_TRIGGER:
return &sp_handler_trigger;
case SP_TYPE_EVENT:
break;
}
return NULL;
}
Expand Down

0 comments on commit 70e7b50

Please sign in to comment.