Bug #91496 Regression: Slow_queries status variable isn't incremented if slow query is off
Submitted: 29 Jun 2018 11:40
Reporter: Mikiya Okuno Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Logging Severity:S3 (Non-critical)
Version:5.7.3 OS:Any
Assigned to: CPU Architecture:Any

[29 Jun 2018 11:40] Mikiya Okuno
Description:
https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html#statvar_Slow_queries

The manual says "The number of queries that have taken more than long_query_time seconds. This counter increments regardless of whether the slow query log is enabled."

Actually, the counter isn't incremented when slow query is not enabled. This problem happenes after 5.7.3 due to fix for bug #69420 (Oracle bug #16924125).

How to repeat:
* Disable slow query log.
* Execute a query which takes longer than long_query_time.
* See "SHOW GLOBAL STATUS LIKE 'Slow_queries'" output.

Suggested fix:
diff --git a/sql/log.cc b/sql/log.cc
index e9b43a6..d812c74 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -1711,7 +1711,6 @@ bool log_slow_applicable(THD *thd)
 void log_slow_do(THD *thd)
 {
   THD_STAGE_INFO(thd, stage_logging_slow_query);
-  thd->status_var.long_query_count++;
 
   if (thd->rewritten_query.length())
     query_logger.slow_log_write(thd,
@@ -1724,6 +1723,7 @@ void log_slow_do(THD *thd)
 
 void log_slow_statement(THD *thd)
 {
+  thd->status_var.long_query_count++;
   if (log_slow_applicable(thd))
     log_slow_do(thd);
 }
[29 Jun 2018 12:05] Mikiya Okuno
The suggest fix was wrong. The following patch is the true fix.

diff --git a/sql/log.cc b/sql/log.cc
index e9b43a6..1a0669a 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -1711,7 +1711,6 @@ bool log_slow_applicable(THD *thd)
 void log_slow_do(THD *thd)
 {
   THD_STAGE_INFO(thd, stage_logging_slow_query);
-  thd->status_var.long_query_count++;
 
   if (thd->rewritten_query.length())
     query_logger.slow_log_write(thd,
@@ -1724,6 +1723,10 @@ void log_slow_do(THD *thd)
 
 void log_slow_statement(THD *thd)
 {
+  if (thd->server_status & SERVER_QUERY_WAS_SLOW)
+  {
+    thd->status_var.long_query_count++;
+  }
   if (log_slow_applicable(thd))
     log_slow_do(thd);
 }