diff --git a/mysql-test/main/sp.result b/mysql-test/main/sp.result
index 37c22e45b45..26f39b9bf15 100644
--- a/mysql-test/main/sp.result
+++ b/mysql-test/main/sp.result
@@ -8972,6 +8972,18 @@ select @counter;
 5
 drop function f1;
 drop table t1,t2;
+# 
+# MDEV-31877: ASAN errors in Exec_time_tracker::get_cycles with innodb slow log verbosity
+# 
+CREATE TABLE t1 (a INT, b INT);
+INSERT INTO t1 VALUES (1,2);
+SET @tmp=@@log_slow_verbosity;
+SET SESSION log_slow_verbosity= 'innodb';
+BEGIN NOT ATOMIC DECLARE r ROW TYPE OF t1 DEFAULT (SELECT * FROM t1); SELECT r.a; END $
+r.a
+1
+SET SESSION log_slow_verbosity= @tmp;
+DROP TABLE t1;
 #
 # MDEV-28129: MariaDB UAF issue at lex_end_nops(LEX*)
 #
diff --git a/mysql-test/main/sp.test b/mysql-test/main/sp.test
index 4314fb8661f..a087518ed6f 100644
--- a/mysql-test/main/sp.test
+++ b/mysql-test/main/sp.test
@@ -10574,6 +10574,22 @@ select @counter;
 drop function f1;
 drop table t1,t2;
 
+--echo # 
+--echo # MDEV-31877: ASAN errors in Exec_time_tracker::get_cycles with innodb slow log verbosity
+--echo # 
+CREATE TABLE t1 (a INT, b INT);
+INSERT INTO t1 VALUES (1,2);
+SET @tmp=@@log_slow_verbosity;
+SET SESSION log_slow_verbosity= 'innodb';
+--delimiter $
+BEGIN NOT ATOMIC DECLARE r ROW TYPE OF t1 DEFAULT (SELECT * FROM t1); SELECT r.a; END $
+--delimiter ;
+
+SET SESSION log_slow_verbosity= @tmp;
+# Cleanup
+DROP TABLE t1;
+
+
 --echo #
 --echo # MDEV-28129: MariaDB UAF issue at lex_end_nops(LEX*)
 --echo #
diff --git a/sql/sp_head.cc b/sql/sp_head.cc
index aecd20ad61a..fda72bd6616 100644
--- a/sql/sp_head.cc
+++ b/sql/sp_head.cc
@@ -3885,7 +3885,6 @@ int
 sp_instr_set::exec_core(THD *thd, uint *nextp)
 {
   int res= get_rcontext(thd)->set_variable(thd, m_offset, &m_value);
-  delete_explain_query(thd->lex);
   *nextp = m_ip+1;
   return res;
 }
@@ -3927,7 +3926,6 @@ sp_instr_set_row_field::exec_core(THD *thd, uint *nextp)
   int res= get_rcontext(thd)->set_variable_row_field(thd, m_offset,
                                                      m_field_offset,
                                                      &m_value);
-  delete_explain_query(thd->lex);
   *nextp= m_ip + 1;
   return res;
 }
@@ -3975,7 +3973,6 @@ sp_instr_set_row_field_by_name::exec_core(THD *thd, uint *nextp)
   int res= get_rcontext(thd)->set_variable_row_field_by_name(thd, m_offset,
                                                              m_field_name,
                                                              &m_value);
-  delete_explain_query(thd->lex);
   *nextp= m_ip + 1;
   return res;
 }