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