MySQL 8.2.0
Source Code Documentation
thd_raii.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2023, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef THD_RAII_INCLUDED
24#define THD_RAII_INCLUDED 1
25
26/**
27 * @file thd_raii.h
28 * Some RAII classes that set THD state.
29 */
30
31#include <sys/types.h>
32
33#include "my_dbug.h"
34#include "my_inttypes.h"
35#include "sql/query_options.h"
36#include "sql/rpl_replica_commit_order_manager.h" // has_commit_order_manager
37#include "sql/sql_alter.h"
38#include "sql/sql_class.h"
39#include "sql/sql_lex.h" // thd->lex
42
43struct MEM_ROOT;
44
45/*************************************************************************/
46
47/** RAII class for temporarily turning off @@autocommit in the connection. */
48
50 public:
51 /**
52 @param thd non-NULL - pointer to the context of connection in which
53 @@autocommit mode needs to be disabled.
54 NULL - if @@autocommit mode needs to be left as is.
55 */
57 : m_thd(thd), m_save_option_bits(thd ? thd->variables.option_bits : 0) {
58 if (m_thd) {
59 /*
60 We can't disable auto-commit if there is ongoing transaction as this
61 might easily break statement/session transaction invariants.
62 */
65
66 m_thd->variables.option_bits &= ~OPTION_AUTOCOMMIT;
68 }
69 }
70
72 if (m_thd) {
73 /*
74 Both session and statement transactions need to be finished by the
75 time when we enable auto-commit mode back OR there must be a
76 transactional DDL being executed.
77 */
82 m_thd->variables.option_bits = m_save_option_bits;
83 }
84 }
85
86 private:
89};
90
91/**
92 The mode for Implicit_substatement_state_guard based on which it determine
93 whether to enable or disable updating Gtid_state and invocation of commit
94 order.
95*/
100};
101
102/**
103 RAII class which allows to temporary disable updating Gtid_state and
104 disable invocation of commit order for intermediate commits.
105*/
107 public:
108 /**
109 Constructs a new object and set thd->is_operating_substatement_implicitly
110 and thd->skip_gtid_rollback according to
111 enum_implicit_substatement_guard_mode mode argument.
112
113 @param thd Thread context.
114 @param mode If mode is not ENABLE_GTID_AND_SPCO_IF_SPCO_ACTIVE then
115 temporary disable updating Gtid_state and invocation of
116 commit order (Commit_order_manager::wait).
117 */
119 THD *thd,
122 : m_thd(thd),
124 thd->is_operating_substatement_implicitly),
125 m_save_skip_gtid_rollback(thd->skip_gtid_rollback),
126 m_guard_ignored(false) {
127 /*
128 For modes depending on SPCO being active,
129 ignore and return if SPCO is not active.
130 */
133 m_guard_ignored = true;
134 return;
135 }
136
137 const bool disable_gtid_and_spco =
138 (mode != enum_implicit_substatement_guard_mode ::
139 ENABLE_GTID_AND_SPCO_IF_SPCO_ACTIVE);
140 m_thd->is_operating_substatement_implicitly = disable_gtid_and_spco;
141 m_thd->skip_gtid_rollback = disable_gtid_and_spco;
142 }
143
146 if (m_guard_ignored) return;
150 }
151
152 private:
157};
158
159/**
160 RAII class to temporarily disable binlogging.
161*/
162
164 public:
166 : m_thd(thd),
167 m_binlog_disabled(thd->variables.option_bits & OPTION_BIN_LOG) {
168 thd->variables.option_bits &= ~OPTION_BIN_LOG;
169 }
170
172 if (m_binlog_disabled) m_thd->variables.option_bits |= OPTION_BIN_LOG;
173 }
174
175 private:
176 THD *const m_thd;
178};
179
181 public:
183 : m_thd(thd), m_saved_sql_log_bin(thd->variables.sql_log_bin) {
184 thd->variables.sql_log_bin = false;
185 }
186
188 m_thd->variables.sql_log_bin = m_saved_sql_log_bin;
189 }
190
191 private:
192 THD *const m_thd;
194};
195
196/**
197 RAII class which allows to save, clear and store binlog format state
198 There are two variables in THD class that will decide the binlog
199 format of a statement
200 i) THD::current_stmt_binlog_format
201 ii) THD::variables.binlog_format
202 Saving or Clearing or Storing of binlog format state should be done
203 for these two variables together all the time.
204*/
206 public:
208 : m_thd(thd),
209 m_global_binlog_format(thd->variables.binlog_format),
213
214 thd->variables.binlog_format = BINLOG_FORMAT_STMT;
216 }
217
220 m_thd->variables.binlog_format = m_global_binlog_format;
223 }
224
225 private:
229};
230
231/**
232 RAII class to temporarily turn off SQL modes that affect parsing
233 of expressions. Can also be used when printing expressions even
234 if it turns off more SQL modes than strictly necessary for it
235 (these extra modes are harmless as they do not affect expression
236 printing).
237*/
239 public:
241 : m_thd(thd), m_old_sql_mode(thd->variables.sql_mode) {
242 /*
243 Switch off modes which can prevent normal parsing of expressions:
244
245 - MODE_REAL_AS_FLOAT affect only CREATE TABLE parsing
246 + MODE_PIPES_AS_CONCAT affect expression parsing
247 + MODE_ANSI_QUOTES affect expression parsing
248 + MODE_IGNORE_SPACE affect expression parsing
249 - MODE_NOT_USED not used :)
250 * MODE_ONLY_FULL_GROUP_BY affect execution
251 * MODE_NO_UNSIGNED_SUBTRACTION affect execution
252 - MODE_NO_DIR_IN_CREATE affect table creation only
253 - MODE_POSTGRESQL compounded from other modes
254 - MODE_ORACLE compounded from other modes
255 - MODE_MSSQL compounded from other modes
256 - MODE_DB2 compounded from other modes
257 - MODE_MAXDB affect only CREATE TABLE parsing
258 - MODE_NO_KEY_OPTIONS affect only SHOW
259 - MODE_NO_TABLE_OPTIONS affect only SHOW
260 - MODE_NO_FIELD_OPTIONS affect only SHOW
261 - MODE_MYSQL323 affect only SHOW
262 - MODE_MYSQL40 affect only SHOW
263 - MODE_ANSI compounded from other modes
264 (+ transaction mode)
265 ? MODE_NO_AUTO_VALUE_ON_ZERO affect UPDATEs
266 + MODE_NO_BACKSLASH_ESCAPES affect expression parsing
267 */
270 }
271
273
274 private:
277};
278
279/**
280 RAII class to temporarily swap thd->mem_root to a different mem_root.
281*/
283 public:
285 : m_thd(thd), m_old_mem_root(thd->mem_root) {
286 thd->mem_root = mem_root;
287 }
288
290
292
293 private:
296};
297
298/**
299 A simple holder for Internal_error_handler.
300 The class utilizes RAII technique to not forget to pop the handler.
301
302 @tparam Error_handler Internal_error_handler to instantiate.
303 @tparam Error_handler_arg Type of the error handler ctor argument.
304*/
305template <typename Error_handler, typename Error_handler_arg>
310
311 public:
312 Internal_error_handler_holder(THD *thd, bool activate, Error_handler_arg *arg)
315 }
316
319 }
320};
321
322/**
323 A simple holder for the Prepared Statement Query_arena instance in THD.
324 The class utilizes RAII technique to not forget to restore the THD arena.
325*/
327 public:
328 /**
329 Constructs a new object, activates the persistent arena if requested and if
330 a prepared statement or a stored procedure statement is being executed.
331
332 @param thd Thread context.
333 @param activate_now_if_needed Attempt to activate the persistent arena in
334 the constructor or not.
335 */
337 bool activate_now_if_needed = true)
338 : m_thd(thd), m_arena(nullptr) {
339 if (activate_now_if_needed && !m_thd->stmt_arena->is_regular() &&
343 }
344 }
345
346 /**
347 Deactivate the persistent arena (restore the previous arena) if it has
348 been activated.
349 */
352 }
353
354 bool is_activated() const { return m_arena != nullptr; }
355
356 private:
357 /// The thread context to work with.
358 THD *const m_thd;
359
360 /// The arena set by this holder (by activate()).
362
363 /// The arena state to be restored.
365};
366
367/**
368 RAII class for pushed LEX object.
369*/
371 public:
372 Pushed_lex_guard(THD *thd, LEX *new_lex) : m_thd(thd), m_old_lex(thd->lex) {
373 thd->lex = new_lex;
374 lex_start(thd);
375 }
377 // Clean up this statement context and restore the old one:
378 m_thd->lex->cleanup(true);
379 lex_end(m_thd->lex);
380
382 }
383
384 private:
385 THD *const m_thd;
387};
388
389/**
390 RAII class for column privilege checking
391*/
393 public:
394 Column_privilege_tracker(THD *thd, ulong privilege)
395 : m_thd(thd), m_saved_privilege(thd->want_privilege) {
396 thd->want_privilege = privilege;
397 }
399
400 private:
401 THD *const m_thd;
402 const ulong m_saved_privilege;
403};
404
405/**
406 RAII class to temporarily enable derived_merge optimizer_switch for SHOW
407 commands that are based on INFORMATION_SCHEMA system views.
408*/
410 public:
411 explicit Enable_derived_merge_guard(THD *thd, bool enable_derived_merge)
412 : m_thd(thd), m_derived_merge(enable_derived_merge) {
413 if (m_derived_merge) {
414 m_save_optimizer_switch = m_thd->variables.optimizer_switch;
416 }
417 }
418
420 if (m_derived_merge)
421 m_thd->variables.optimizer_switch = m_save_optimizer_switch;
422 }
423
424 private:
425 THD *const m_thd{nullptr};
426 bool m_derived_merge{false};
428};
429
430#endif // THD_RAII_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
RAII class for column privilege checking.
Definition: thd_raii.h:392
THD *const m_thd
Definition: thd_raii.h:401
const ulong m_saved_privilege
Definition: thd_raii.h:402
~Column_privilege_tracker()
Definition: thd_raii.h:398
Column_privilege_tracker(THD *thd, ulong privilege)
Definition: thd_raii.h:394
RAII class for temporarily turning off @autocommit in the connection.
Definition: thd_raii.h:49
Disable_autocommit_guard(THD *thd)
Definition: thd_raii.h:56
ulonglong m_save_option_bits
Definition: thd_raii.h:88
THD * m_thd
Definition: thd_raii.h:87
~Disable_autocommit_guard()
Definition: thd_raii.h:71
RAII class to temporarily disable binlogging.
Definition: thd_raii.h:163
THD *const m_thd
Definition: thd_raii.h:176
~Disable_binlog_guard()
Definition: thd_raii.h:171
Disable_binlog_guard(THD *thd)
Definition: thd_raii.h:165
const bool m_binlog_disabled
Definition: thd_raii.h:177
Definition: thd_raii.h:180
~Disable_sql_log_bin_guard()
Definition: thd_raii.h:187
Disable_sql_log_bin_guard(THD *thd)
Definition: thd_raii.h:182
THD *const m_thd
Definition: thd_raii.h:192
const bool m_saved_sql_log_bin
Definition: thd_raii.h:193
RAII class to temporarily enable derived_merge optimizer_switch for SHOW commands that are based on I...
Definition: thd_raii.h:409
Enable_derived_merge_guard(THD *thd, bool enable_derived_merge)
Definition: thd_raii.h:411
bool m_derived_merge
Definition: thd_raii.h:426
THD *const m_thd
Definition: thd_raii.h:425
~Enable_derived_merge_guard()
Definition: thd_raii.h:419
ulonglong m_save_optimizer_switch
Definition: thd_raii.h:427
RAII class which allows to temporary disable updating Gtid_state and disable invocation of commit ord...
Definition: thd_raii.h:106
THD * m_thd
Definition: thd_raii.h:153
bool m_guard_ignored
Definition: thd_raii.h:156
bool m_save_is_operating_substatement_implicitly
Definition: thd_raii.h:154
bool m_save_skip_gtid_rollback
Definition: thd_raii.h:155
Implicit_substatement_state_guard(THD *thd, enum_implicit_substatement_guard_mode mode=enum_implicit_substatement_guard_mode::DISABLE_GTID_AND_SPCO)
Constructs a new object and set thd->is_operating_substatement_implicitly and thd->skip_gtid_rollback...
Definition: thd_raii.h:118
~Implicit_substatement_state_guard()
Definition: thd_raii.h:144
A simple holder for Internal_error_handler.
Definition: thd_raii.h:306
~Internal_error_handler_holder()
Definition: thd_raii.h:317
bool m_activate
Definition: thd_raii.h:308
Error_handler m_error_handler
Definition: thd_raii.h:309
THD * m_thd
Definition: thd_raii.h:307
Internal_error_handler_holder(THD *thd, bool activate, Error_handler_arg *arg)
Definition: thd_raii.h:312
A simple holder for the Prepared Statement Query_arena instance in THD.
Definition: thd_raii.h:326
Query_arena * m_arena
The arena set by this holder (by activate()).
Definition: thd_raii.h:361
~Prepared_stmt_arena_holder()
Deactivate the persistent arena (restore the previous arena) if it has been activated.
Definition: thd_raii.h:350
Prepared_stmt_arena_holder(THD *thd, bool activate_now_if_needed=true)
Constructs a new object, activates the persistent arena if requested and if a prepared statement or a...
Definition: thd_raii.h:336
THD *const m_thd
The thread context to work with.
Definition: thd_raii.h:358
Query_arena m_backup
The arena state to be restored.
Definition: thd_raii.h:364
bool is_activated() const
Definition: thd_raii.h:354
RAII class for pushed LEX object.
Definition: thd_raii.h:370
~Pushed_lex_guard()
Definition: thd_raii.h:376
THD *const m_thd
Definition: thd_raii.h:385
LEX * m_old_lex
Definition: thd_raii.h:386
Pushed_lex_guard(THD *thd, LEX *new_lex)
Definition: thd_raii.h:372
Definition: sql_class.h:347
bool is_regular() const
Definition: sql_class.h:419
void swap_query_arena(const Query_arena &source, Query_arena *backup)
Copy the current arena to backup and set the current arena to match source
Definition: sql_class.cc:2044
MEM_ROOT * mem_root
Definition: sql_class.h:356
enum_sql_command sql_command
SQL command for this statement.
Definition: sql_lex.h:2525
RAII class which allows to save, clear and store binlog format state There are two variables in THD c...
Definition: thd_raii.h:205
~Save_and_Restore_binlog_format_state()
Definition: thd_raii.h:218
Save_and_Restore_binlog_format_state(THD *thd)
Definition: thd_raii.h:207
enum_binlog_format m_current_stmt_binlog_format
Definition: thd_raii.h:228
THD * m_thd
Definition: thd_raii.h:226
ulong m_global_binlog_format
Definition: thd_raii.h:227
RAII class to temporarily turn off SQL modes that affect parsing of expressions.
Definition: thd_raii.h:238
THD * m_thd
Definition: thd_raii.h:275
const sql_mode_t m_old_sql_mode
Definition: thd_raii.h:276
~Sql_mode_parse_guard()
Definition: thd_raii.h:272
Sql_mode_parse_guard(THD *thd)
Definition: thd_raii.h:240
RAII class to temporarily swap thd->mem_root to a different mem_root.
Definition: thd_raii.h:282
MEM_ROOT * m_old_mem_root
Definition: thd_raii.h:295
~Swap_mem_root_guard()
Definition: thd_raii.h:289
THD * m_thd
Definition: thd_raii.h:294
Swap_mem_root_guard(THD *thd, MEM_ROOT *mem_root)
Definition: thd_raii.h:284
MEM_ROOT * old_mem_root()
Definition: thd_raii.h:291
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
Query_arena * stmt_arena
Definition: sql_class.h:2129
LEX * lex
Definition: sql_class.h:989
Internal_error_handler * pop_internal_handler()
Remove the error handler last pushed.
Definition: sql_class.cc:924
void clear_current_stmt_binlog_format_row()
Definition: sql_class.h:3437
int is_current_stmt_binlog_format_row() const
Determine the binlog format of the current statement.
Definition: sql_class.h:1710
void push_internal_handler(Internal_error_handler *handler)
Add an internal error handler to the thread execution context.
Definition: sql_class.cc:903
void set_current_stmt_binlog_format_row()
Definition: sql_class.h:3432
bool skip_gtid_rollback
Definition: sql_class.h:3870
ulong want_privilege
Used by Item::check_column_privileges() to tell which privileges to check for.
Definition: sql_class.h:977
System_variables variables
Definition: sql_lexer_thd.h:63
MEM_ROOT * mem_root
Definition: sql_lexer_thd.h:39
Transaction_ctx * get_transaction()
Definition: sql_class.h:2070
bool is_operating_substatement_implicitly
Definition: sql_class.h:2313
bool is_empty(enum_trx_scope scope) const
Definition: transaction_info.h:331
@ SESSION
Definition: transaction_info.h:55
@ STMT
Definition: transaction_info.h:55
static MEM_ROOT mem_root
Definition: client_plugin.cc:113
uint64_t sql_mode_t
Definition: dd_event.h:38
void lex_end(LEX *lex)
Call this function after preparation and execution of a query.
Definition: sql_lex.cc:524
bool lex_start(THD *thd)
Call lex_start() before every query that is to be prepared and executed.
Definition: sql_lex.cc:500
#define DBUG_TRACE
Definition: my_dbug.h:145
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
@ SQLCOM_CREATE_TABLE
Definition: my_sqlcommand.h:47
static const char * sql_mode
Definition: mysqlslap.cc:198
mode
Definition: file_handle.h:59
#define OPTION_BIN_LOG
Definition: query_options.h:72
#define OPTION_NOT_AUTOCOMMIT
Definition: query_options.h:73
bool has_commit_order_manager(const THD *thd)
Determines whether current thread shall run the procedure here to check whether it waits for its turn...
Definition: rpl_replica_commit_order_manager.cc:503
constexpr const uint64_t OPTIMIZER_SWITCH_DERIVED_MERGE
Definition: sql_const.h:216
Definition: mysql_command_services_imp.cc:41
bool m_transactional_ddl
Definition: handler.h:3209
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3721
HA_CREATE_INFO * create_info
Definition: sql_lex.h:3883
void cleanup(bool full)
Definition: sql_lex.h:4095
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
sql_mode_t sql_mode
Definition: sql_lexer_thd.h:57
constexpr sql_mode_t MODE_NO_BACKSLASH_ESCAPES
Definition: system_variables.h:139
constexpr sql_mode_t MODE_PIPES_AS_CONCAT
Definition: system_variables.h:130
enum_binlog_format
Definition: system_variables.h:45
@ BINLOG_FORMAT_ROW
row-based
Definition: system_variables.h:48
@ BINLOG_FORMAT_STMT
statement-based
Definition: system_variables.h:47
constexpr sql_mode_t MODE_IGNORE_SPACE
Definition: system_variables.h:132
constexpr sql_mode_t MODE_ANSI_QUOTES
Definition: system_variables.h:131
static task_env * activate(task_env *t)
Definition: task.cc:545
enum_implicit_substatement_guard_mode
The mode for Implicit_substatement_state_guard based on which it determine whether to enable or disab...
Definition: thd_raii.h:96