diff --git a/mysql-test/r/change_user.result b/mysql-test/r/change_user.result index 28b55dfd5e5..4cfaf0e90ea 100644 --- a/mysql-test/r/change_user.result +++ b/mysql-test/r/change_user.result @@ -44,3 +44,10 @@ IS_FREE_LOCK('bug31418') SELECT IS_USED_LOCK('bug31418'); IS_USED_LOCK('bug31418') NULL +FLUSH STATUS; +SHOW GLOBAL STATUS LIKE 'com_select'; +Variable_name Value +Com_select 112 +SHOW GLOBAL STATUS LIKE 'com_select'; +Variable_name Value +Com_select 112 diff --git a/mysql-test/t/change_user.test b/mysql-test/t/change_user.test index d0cdfc8a741..eec2a6f39d3 100644 --- a/mysql-test/t/change_user.test +++ b/mysql-test/t/change_user.test @@ -33,3 +33,31 @@ SELECT IS_USED_LOCK('bug31418') = CONNECTION_ID(); --change_user SELECT IS_FREE_LOCK('bug31418'); SELECT IS_USED_LOCK('bug31418'); + +# +# Bug#31222: com_% global status counters behave randomly with +# mysql_change_user. +# + +FLUSH STATUS; + +--disable_result_log +--disable_query_log + +let $i = 100; + +while ($i) +{ + dec $i; + + SELECT 1; +} + +--enable_query_log +--enable_result_log + +SHOW GLOBAL STATUS LIKE 'com_select'; + +--change_user + +SHOW GLOBAL STATUS LIKE 'com_select'; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 5180cafc774..5bdde066575 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -769,6 +769,10 @@ void THD::init_for_queries() void THD::change_user(void) { + pthread_mutex_lock(&LOCK_status); + add_to_status(&global_status_var, &status_var); + pthread_mutex_unlock(&LOCK_status); + cleanup(); killed= NOT_KILLED; cleanup_done= 0;