Skip to content

Commit dda2e94

Browse files
committed
pass the slow logging information in thd->query_plan_flags
This solves the following issues: * unlike lex->m_sql_cmd and lex->sql_command, thd->query_plan_flags is not reset in Prepared_statement::execute, it survives till the log_slow_statement(), so slow logging behaves correctly in --ps * using thd->query_plan_flags for both slow_log_filter and log_slow_admin_statements means the definition of "admin" statements for the slow log is the same no matter how it is filtered out.
1 parent bc8ae50 commit dda2e94

File tree

7 files changed

+39
-74
lines changed

7 files changed

+39
-74
lines changed

sql/log_slow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@
3434
#define QPLAN_FILESORT_PRIORITY_QUEUE (1U << 9)
3535

3636
/* ... */
37+
#define QPLAN_STATUS (1U << 31) /* not in the slow_log_filter */
3738
#define QPLAN_MAX (1U << 31) /* reserved as placeholder */

sql/sql_admin.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int reassign_keycache_tables(THD* thd, KEY_CACHE *src_cache,
2828
/**
2929
Sql_cmd_analyze_table represents the ANALYZE TABLE statement.
3030
*/
31-
class Sql_cmd_analyze_table : public Sql_cmd_admin
31+
class Sql_cmd_analyze_table : public Sql_cmd
3232
{
3333
public:
3434
/**
@@ -53,7 +53,7 @@ class Sql_cmd_analyze_table : public Sql_cmd_admin
5353
/**
5454
Sql_cmd_check_table represents the CHECK TABLE statement.
5555
*/
56-
class Sql_cmd_check_table : public Sql_cmd_admin
56+
class Sql_cmd_check_table : public Sql_cmd
5757
{
5858
public:
5959
/**
@@ -77,7 +77,7 @@ class Sql_cmd_check_table : public Sql_cmd_admin
7777
/**
7878
Sql_cmd_optimize_table represents the OPTIMIZE TABLE statement.
7979
*/
80-
class Sql_cmd_optimize_table : public Sql_cmd_admin
80+
class Sql_cmd_optimize_table : public Sql_cmd
8181
{
8282
public:
8383
/**
@@ -102,7 +102,7 @@ class Sql_cmd_optimize_table : public Sql_cmd_admin
102102
/**
103103
Sql_cmd_repair_table represents the REPAIR TABLE statement.
104104
*/
105-
class Sql_cmd_repair_table : public Sql_cmd_admin
105+
class Sql_cmd_repair_table : public Sql_cmd
106106
{
107107
public:
108108
/**

sql/sql_alter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class Alter_table_ctx
363363
statements.
364364
@todo move Alter_info and other ALTER generic structures from Lex here.
365365
*/
366-
class Sql_cmd_common_alter_table : public Sql_cmd_admin
366+
class Sql_cmd_common_alter_table : public Sql_cmd
367367
{
368368
protected:
369369
/**

sql/sql_class.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5458,6 +5458,11 @@ class select_dumpvar :public select_result_interceptor {
54585458
*/
54595459
#define CF_UPDATES_DATA (1U << 18)
54605460

5461+
/**
5462+
Not logged into slow log as "admin commands"
5463+
*/
5464+
#define CF_ADMIN_COMMAND (1U << 19)
5465+
54615466
/* Bits in server_command_flags */
54625467

54635468
/**

sql/sql_cmd.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ class Sql_cmd : public Sql_alloc
145145
*/
146146
virtual bool execute(THD *thd) = 0;
147147

148-
virtual bool log_slow_enabled_statement(const THD *thd) const;
149-
150148
protected:
151149
Sql_cmd()
152150
{}
@@ -163,17 +161,4 @@ class Sql_cmd : public Sql_alloc
163161
}
164162
};
165163

166-
167-
class Sql_cmd_admin: public Sql_cmd
168-
{
169-
public:
170-
Sql_cmd_admin()
171-
{}
172-
~Sql_cmd_admin()
173-
{}
174-
bool log_slow_enabled_statement(const THD *thd) const;
175-
};
176-
177-
178-
179164
#endif // SQL_CMD_INCLUDED

sql/sql_lex.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,19 +4720,6 @@ bool LEX::is_partition_management() const
47204720
}
47214721

47224722

4723-
bool Sql_cmd::log_slow_enabled_statement(const THD *thd) const
4724-
{
4725-
return global_system_variables.sql_log_slow && thd->variables.sql_log_slow;
4726-
}
4727-
4728-
4729-
bool Sql_cmd_admin::log_slow_enabled_statement(const THD *thd) const
4730-
{
4731-
return opt_log_slow_admin_statements &&
4732-
Sql_cmd::log_slow_enabled_statement(thd);
4733-
}
4734-
4735-
47364723
#ifdef MYSQL_SERVER
47374724
uint binlog_unsafe_map[256];
47384725

sql/sql_parse.cc

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,11 @@ void init_update_queries(void)
309309
sql_command_flags[SQLCOM_CREATE_TABLE]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
310310
CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS |
311311
CF_CAN_GENERATE_ROW_EVENTS;
312-
sql_command_flags[SQLCOM_CREATE_INDEX]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS;
312+
sql_command_flags[SQLCOM_CREATE_INDEX]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS |
313+
CF_REPORT_PROGRESS | CF_ADMIN_COMMAND;
313314
sql_command_flags[SQLCOM_ALTER_TABLE]= CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND |
314315
CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS |
315-
CF_INSERTS_DATA;
316+
CF_INSERTS_DATA | CF_ADMIN_COMMAND;
316317
sql_command_flags[SQLCOM_TRUNCATE]= CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND |
317318
CF_AUTO_COMMIT_TRANS;
318319
sql_command_flags[SQLCOM_DROP_TABLE]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
@@ -324,7 +325,8 @@ void init_update_queries(void)
324325
sql_command_flags[SQLCOM_ALTER_DB_UPGRADE]= CF_AUTO_COMMIT_TRANS;
325326
sql_command_flags[SQLCOM_ALTER_DB]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
326327
sql_command_flags[SQLCOM_RENAME_TABLE]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
327-
sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS;
328+
sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS |
329+
CF_REPORT_PROGRESS | CF_ADMIN_COMMAND;
328330
sql_command_flags[SQLCOM_CREATE_VIEW]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
329331
CF_AUTO_COMMIT_TRANS;
330332
sql_command_flags[SQLCOM_DROP_VIEW]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
@@ -492,10 +494,14 @@ void init_update_queries(void)
492494
The following admin table operations are allowed
493495
on log tables.
494496
*/
495-
sql_command_flags[SQLCOM_REPAIR]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS;
496-
sql_command_flags[SQLCOM_OPTIMIZE]|= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS;
497-
sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS;
498-
sql_command_flags[SQLCOM_CHECK]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS | CF_REPORT_PROGRESS;
497+
sql_command_flags[SQLCOM_REPAIR]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS |
498+
CF_REPORT_PROGRESS | CF_ADMIN_COMMAND;
499+
sql_command_flags[SQLCOM_OPTIMIZE]|= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS |
500+
CF_REPORT_PROGRESS | CF_ADMIN_COMMAND;
501+
sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS |
502+
CF_REPORT_PROGRESS | CF_ADMIN_COMMAND;
503+
sql_command_flags[SQLCOM_CHECK]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS |
504+
CF_REPORT_PROGRESS | CF_ADMIN_COMMAND;
499505
sql_command_flags[SQLCOM_CHECKSUM]= CF_REPORT_PROGRESS;
500506

501507
sql_command_flags[SQLCOM_CREATE_USER]|= CF_AUTO_COMMIT_TRANS;
@@ -1304,10 +1310,6 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
13041310
m_key);
13051311
thd->set_command(command);
13061312

1307-
/*
1308-
Commands which always take a long time are logged into
1309-
the slow log only if opt_log_slow_admin_statements is set.
1310-
*/
13111313
thd->enable_slow_log= true;
13121314
thd->query_plan_flags= QPLAN_INIT;
13131315
thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
@@ -1718,7 +1720,6 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
17181720

17191721
status_var_increment(thd->status_var.com_other);
17201722

1721-
thd->enable_slow_log&= opt_log_slow_admin_statements;
17221723
thd->query_plan_flags|= QPLAN_ADMIN;
17231724
if (check_global_access(thd, REPL_SLAVE_ACL))
17241725
break;
@@ -2018,31 +2019,6 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
20182019
}
20192020

20202021

2021-
static bool log_slow_enabled_statement(const THD *thd)
2022-
{
2023-
/*
2024-
TODO-10.4: Add classes Sql_cmd_create_index and Sql_cmd_drop_index
2025-
for symmetry with other admin commands, so these statements can be
2026-
handled by this command:
2027-
*/
2028-
if (thd->lex->m_sql_cmd)
2029-
return thd->lex->m_sql_cmd->log_slow_enabled_statement(thd);
2030-
2031-
/*
2032-
Currently CREATE INDEX or DROP INDEX cause a full table rebuild
2033-
and thus classify as slow administrative statements just like
2034-
ALTER TABLE.
2035-
*/
2036-
if ((thd->lex->sql_command == SQLCOM_CREATE_INDEX ||
2037-
thd->lex->sql_command == SQLCOM_DROP_INDEX) &&
2038-
!opt_log_slow_admin_statements)
2039-
return true;
2040-
2041-
return global_system_variables.sql_log_slow &&
2042-
thd->variables.sql_log_slow;
2043-
}
2044-
2045-
20462022
/*
20472023
@note
20482024
This function must call delete_explain_query().
@@ -2075,12 +2051,20 @@ void log_slow_statement(THD *thd)
20752051
((thd->server_status &
20762052
(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED)) &&
20772053
opt_log_queries_not_using_indexes &&
2078-
!(sql_command_flags[thd->lex->sql_command] & CF_STATUS_COMMAND))) &&
2054+
!(thd->query_plan_flags & QPLAN_STATUS))) &&
20792055
thd->get_examined_row_count() >= thd->variables.min_examined_row_limit)
20802056
{
20812057
thd->status_var.long_query_count++;
20822058

2083-
if (!log_slow_enabled_statement(thd))
2059+
/*
2060+
until opt_log_slow_admin_statements is removed, it
2061+
duplicates slow_log_filter=admin
2062+
*/
2063+
if ((thd->query_plan_flags & QPLAN_ADMIN) &&
2064+
!opt_log_slow_admin_statements)
2065+
goto end;
2066+
2067+
if (!global_system_variables.sql_log_slow || !thd->variables.sql_log_slow)
20842068
goto end;
20852069

20862070
/*
@@ -2953,6 +2937,11 @@ mysql_execute_command(THD *thd)
29532937
goto error;
29542938
}
29552939

2940+
if (sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND)
2941+
thd->query_plan_flags|= QPLAN_STATUS;
2942+
if (sql_command_flags[lex->sql_command] & CF_ADMIN_COMMAND)
2943+
thd->query_plan_flags|= QPLAN_ADMIN;
2944+
29562945
/* Start timeouts */
29572946
thd->set_query_timer();
29582947

@@ -3574,7 +3563,6 @@ mysql_execute_command(THD *thd)
35743563
if (check_one_table_access(thd, INDEX_ACL, all_tables))
35753564
goto error; /* purecov: inspected */
35763565
WSREP_TO_ISOLATION_BEGIN(first_table->db, first_table->table_name, NULL)
3577-
thd->query_plan_flags|= QPLAN_ADMIN;
35783566

35793567
bzero((char*) &create_info, sizeof(create_info));
35803568
create_info.db_type= 0;
@@ -5732,7 +5720,6 @@ mysql_execute_command(THD *thd)
57325720
case SQLCOM_REPAIR:
57335721
case SQLCOM_TRUNCATE:
57345722
case SQLCOM_ALTER_TABLE:
5735-
thd->query_plan_flags|= QPLAN_ADMIN;
57365723
DBUG_ASSERT(first_table == all_tables && first_table != 0);
57375724
/* fall through */
57385725
case SQLCOM_SIGNAL:

0 commit comments

Comments
 (0)