MySQL 9.0.0
Source Code Documentation
sql_thd_api.cc File Reference
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <atomic>
#include "lex_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_sqlcommand.h"
#include "my_thread.h"
#include "my_thread_local.h"
#include "mysql/components/services/bits/psi_stage_bits.h"
#include "mysql/components/services/bits/psi_thread_bits.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_thd_engine_lock.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/conn_handler/connection_handler_manager.h"
#include "sql/current_thd.h"
#include "sql/handler.h"
#include "sql/mysqld.h"
#include "sql/protocol_classic.h"
#include "sql/query_options.h"
#include "sql/resourcegroups/platform/thread_attrs_api.h"
#include "sql/rpl_replica_commit_order_manager.h"
#include "sql/rpl_rli.h"
#include "sql/sql_alter.h"
#include "sql/sql_callback.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_plugin.h"
#include "sql/sql_plugin_ref.h"
#include "sql/sql_thd_internal_api.h"
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/transaction_info.h"
#include "sql/xa.h"
#include "sql_string.h"
#include "string_with_len.h"
#include "violite.h"

Functions

void * thd_get_scheduler_data (THD *thd)
 Get reference to scheduler data object. More...
 
void thd_set_scheduler_data (THD *thd, void *data)
 Set reference to Scheduler data object for THD object. More...
 
PSI_threadthd_get_psi (THD *thd)
 Get reference to Performance Schema object for THD object. More...
 
ulong thd_get_net_wait_timeout (THD *thd)
 Get net_wait_timeout for THD object. More...
 
void thd_set_psi (THD *thd, PSI_thread *psi)
 Set reference to Performance Schema object for THD object. More...
 
void thd_set_killed (THD *thd)
 Set the state on connection to killed. More...
 
void thd_clear_errors (THD *thd)
 Clear errors from the previous THD. More...
 
void thd_close_connection (THD *thd)
 Close the socket used by this connection. More...
 
THDthd_get_current_thd ()
 Get current THD object from thread local data. More...
 
void reset_thread_globals (THD *thd)
 Reset thread globals associated. More...
 
void thd_lock_data (THD *thd)
 Lock data that needs protection in THD object. More...
 
void thd_unlock_data (THD *thd)
 Unlock data that needs protection in THD object. More...
 
bool thd_is_transaction_active (THD *thd)
 Support method to check if connection has already started transaction. More...
 
bool thd_in_active_multi_stmt_transaction (const THD *thd)
 Predicate for determining if connection is in active multi-statement transaction. More...
 
int thd_connection_has_data (THD *thd)
 Check if there is buffered data on the socket representing the connection. More...
 
uint thd_get_net_read_write (THD *thd)
 Get reading/writing on socket from THD object. More...
 
void thd_set_net_read_write (THD *thd, uint val)
 Set reading/writing on socket, used by SHOW PROCESSLIST. More...
 
void thd_set_not_killable (THD *thd)
 Mark the THD as not killable as it is not currently used by a thread. More...
 
my_socket thd_get_fd (THD *thd)
 Get socket file descriptor for this connection. More...
 
MYSQL_SOCKET thd_get_mysql_socket (THD *thd)
 Get MYSQL_SOCKET struct for this connection. More...
 
void thd_store_globals (THD *thd)
 Set thread specific environment required for thd cleanup in thread pool. More...
 
my_thread_attr_tget_connection_attrib ()
 Get thread attributes for connection threads. More...
 
ulong get_max_connections ()
 Get max number of connections. More...
 
void thd_binlog_pos (const MYSQL_THD thd, const char **file_var, unsigned long long *pos_var)
 Get binary log position for latest written entry. More...
 
int mysql_tmpfile (const char *prefix)
 Create a temporary file. More...
 
int thd_in_lock_tables (const MYSQL_THD thd)
 
int thd_tablespace_op (const MYSQL_THD thd)
 
static void set_thd_stage_info (MYSQL_THD thd, const PSI_stage_info *new_stage, PSI_stage_info *old_stage, const char *calling_func, const char *calling_file, const unsigned int calling_line)
 
const char * set_thd_proc_info (MYSQL_THD thd_arg, const char *info, const char *calling_function, const char *calling_file, const unsigned int calling_line)
 
void ** thd_ha_data (const MYSQL_THD thd, const struct handlerton *hton)
 
void thd_storage_lock_wait (MYSQL_THD thd, long long value)
 
void * thd_get_ha_data (const MYSQL_THD thd, const struct handlerton *hton)
 Provide a handler data getter to simplify coding. More...
 
void thd_set_ha_data (MYSQL_THD thd, const struct handlerton *hton, const void *ha_data)
 Provide a handler data setter to simplify coding. More...
 
long long thd_test_options (const MYSQL_THD thd, long long test_options)
 
int thd_sql_command (const MYSQL_THD thd)
 
int thd_tx_isolation (const MYSQL_THD thd)
 
int thd_tx_is_read_only (const MYSQL_THD thd)
 
int thd_tx_priority (const MYSQL_THD thd)
 
MYSQL_THD thd_tx_arbitrate (MYSQL_THD requestor, MYSQL_THD holder)
 
int thd_tx_is_dd_trx (const MYSQL_THD thd)
 
void thd_inc_row_count (MYSQL_THD thd)
 
static size_t truncated_str_length (const CHARSET_INFO *cs, const char *start, size_t original_size, size_t max_size)
 Returns the size of the beginning part of a (multibyte) string, which can fit in max_size bytes. More...
 
char * thd_security_context (MYSQL_THD thd, char *buffer, size_t length, size_t max_query_len)
 Dumps a text description of a thread, its security context (user, host) and the current query. More...
 
void thd_get_xid (const MYSQL_THD thd, MYSQL_XID *xid)
 Get the XID for this connection's transaction. More...
 
int thd_killed (const void *v_thd)
 Check the killed state of a connection. More...
 
void thd_set_kill_status (const MYSQL_THD thd)
 Set the killed status of the current statement. More...
 
unsigned long thd_get_thread_id (const MYSQL_THD thd)
 Return the thread id of a user thread. More...
 
int thd_allow_batch (MYSQL_THD thd)
 Check if batching is allowed for the thread. More...
 
void thd_mark_transaction_to_rollback (MYSQL_THD thd, int all)
 Mark transaction to rollback and mark error as fatal to a sub-statement if in sub statement mode. More...
 
void * thd_alloc (MYSQL_THD thd, size_t size)
 Allocate memory in the connection's local memory pool. More...
 
void * thd_calloc (MYSQL_THD thd, size_t size)
 
char * thd_strdup (MYSQL_THD thd, const char *str)
 
char * thd_strmake (MYSQL_THD thd, const char *str, size_t size)
 
MYSQL_LEX_STRINGthd_make_lex_string (MYSQL_THD thd, MYSQL_LEX_STRING *lex_str, const char *str, size_t size, int allocate_lex_string)
 Create a LEX_STRING in this connection's local memory pool. More...
 
void * thd_memdup (MYSQL_THD thd, const void *str, size_t size)
 
void thd_wait_begin (MYSQL_THD thd, int wait_type)
 Interface for MySQL Server, plugins and storage engines to report when they are going to sleep/stall. More...
 
void thd_wait_end (MYSQL_THD thd)
 Interface for MySQL Server, plugins and storage engines to report when they waking up from a sleep/stall. More...
 
void thd_report_row_lock_wait (THD *self, THD *wait_for)
 
void thd_report_lock_wait (THD *self, THD *wait_for, bool)
 Call it just when the engine find a transaction should wait another transaction to release a lock. More...
 
void remove_ssl_err_thread_state ()
 Interface for cleaning the openssl per thread error queue. More...
 
unsigned int thd_get_num_vcpus ()
 Interface to get the number of VCPUs. More...
 
bool thd_check_connection_admin_privilege (MYSQL_THD thd)
 
unsigned int thd_get_current_thd_terminology_use_previous ()
 Return @session.terminology_use_previous for the current THD. More...
 

Function Documentation

◆ get_connection_attrib()

my_thread_attr_t * get_connection_attrib ( void  )

Get thread attributes for connection threads.

Returns the server connection attribute.

Return values
Referenceto thread attribute for connection threads

◆ get_max_connections()

ulong get_max_connections ( void  )

Get max number of connections.

Return values
Maxnumber of connections for MySQL Server

◆ mysql_tmpfile()

int mysql_tmpfile ( const char *  prefix)

Create a temporary file.

The temporary file is created in a location specified by the mysql server configuration (–tmpdir option). The caller does not need to delete the file, it will be deleted automatically.

Parameters
prefixprefix for temporary file name
Return values
-1error
>=0 a file handle that can be passed to dup or my_close

◆ remove_ssl_err_thread_state()

void remove_ssl_err_thread_state ( )

Interface for cleaning the openssl per thread error queue.

Interface to remove the per thread openssl error queue.

◆ reset_thread_globals()

void reset_thread_globals ( THD thd)

Reset thread globals associated.

Parameters
thdTHD object

◆ set_thd_proc_info()

const char * set_thd_proc_info ( MYSQL_THD  thd_arg,
const char *  info,
const char *  calling_function,
const char *  calling_file,
const unsigned int  calling_line 
)

◆ set_thd_stage_info()

static void set_thd_stage_info ( MYSQL_THD  thd,
const PSI_stage_info new_stage,
PSI_stage_info old_stage,
const char *  calling_func,
const char *  calling_file,
const unsigned int  calling_line 
)
static

◆ thd_alloc()

void * thd_alloc ( MYSQL_THD  thd,
size_t  size 
)

Allocate memory in the connection's local memory pool.

When properly used in place of my_malloc(), this can significantly improve concurrency. Don't use this or related functions to allocate large chunks of memory. Use for temporary storage only. The memory will be freed automatically at the end of the statement; no explicit code is required to prevent memory leaks.

See also
alloc_root()

◆ thd_allow_batch()

int thd_allow_batch ( MYSQL_THD  thd)

Check if batching is allowed for the thread.

Parameters
thduser thread
Return values
1batching allowed
0batching not allowed

◆ thd_binlog_pos()

void thd_binlog_pos ( const MYSQL_THD  thd,
const char **  file_var,
unsigned long long *  pos_var 
)

Get binary log position for latest written entry.

Note
The file variable will be set to a buffer holding the name of the file name currently, but this can change if a rotation occur. Copy the string if you want to retain it.
Parameters
thdUse thread connection handle
file_varPointer to variable that will hold the file name.
pos_varPointer to variable that will hold the file position.

◆ thd_calloc()

void * thd_calloc ( MYSQL_THD  thd,
size_t  size 
)
See also
thd_alloc()

◆ thd_check_connection_admin_privilege()

bool thd_check_connection_admin_privilege ( MYSQL_THD  thd)

◆ thd_clear_errors()

void thd_clear_errors ( THD thd)

Clear errors from the previous THD.

Parameters
thdTHD object

◆ thd_close_connection()

void thd_close_connection ( THD thd)

Close the socket used by this connection.

Parameters
thdTHD object
Note
Expects lock on thd->LOCK_thd_data.

◆ thd_connection_has_data()

int thd_connection_has_data ( THD thd)

Check if there is buffered data on the socket representing the connection.

Parameters
thdTHD object

◆ thd_get_current_thd()

THD * thd_get_current_thd ( )

Get current THD object from thread local data.

Return values
TheTHD object for the thread, NULL if not connection thread

◆ thd_get_current_thd_terminology_use_previous()

unsigned int thd_get_current_thd_terminology_use_previous ( )

Return @session.terminology_use_previous for the current THD.

Returns
the integer value of one of the enumeration values in terminology_use_previous::enum_compatibility_version.

◆ thd_get_fd()

my_socket thd_get_fd ( THD thd)

Get socket file descriptor for this connection.

Parameters
thdTHD object
Return values
Socketof the connection

◆ thd_get_ha_data()

void * thd_get_ha_data ( const MYSQL_THD  thd,
const struct handlerton hton 
)

Provide a handler data getter to simplify coding.

◆ thd_get_mysql_socket()

MYSQL_SOCKET thd_get_mysql_socket ( THD thd)

Get MYSQL_SOCKET struct for this connection.

Parameters
thdTHD object
Return values
MYSQL_SOCKETstruct of the connection

◆ thd_get_net_read_write()

uint thd_get_net_read_write ( THD thd)

Get reading/writing on socket from THD object.

Parameters
thdTHD object
Return values
net.reading_or_writingvalue for thread on THD.

◆ thd_get_net_wait_timeout()

ulong thd_get_net_wait_timeout ( THD thd)

Get net_wait_timeout for THD object.

Parameters
thdTHD object
Return values
net_wait_timeoutvalue for thread on THD

◆ thd_get_num_vcpus()

unsigned int thd_get_num_vcpus ( )

Interface to get the number of VCPUs.

◆ thd_get_psi()

PSI_thread * thd_get_psi ( THD thd)

Get reference to Performance Schema object for THD object.

Parameters
thdTHD object
Return values
Performanceschema object for thread on THD

◆ thd_get_scheduler_data()

void * thd_get_scheduler_data ( THD thd)

Get reference to scheduler data object.

Parameters
thdTHD object
Return values
Schedulerdata object on THD

◆ thd_get_thread_id()

unsigned long thd_get_thread_id ( const MYSQL_THD  thd)

Return the thread id of a user thread.

Parameters
thduser thread connection handle
Returns
thread id

◆ thd_get_xid()

void thd_get_xid ( const MYSQL_THD  thd,
MYSQL_XID xid 
)

Get the XID for this connection's transaction.

Parameters
thduser thread connection handle
xidlocation where identifier is stored

◆ thd_ha_data()

void ** thd_ha_data ( const MYSQL_THD  thd,
const struct handlerton hton 
)

◆ thd_in_active_multi_stmt_transaction()

bool thd_in_active_multi_stmt_transaction ( const THD thd)

Predicate for determining if connection is in active multi-statement transaction.

◆ thd_in_lock_tables()

int thd_in_lock_tables ( const MYSQL_THD  thd)

◆ thd_inc_row_count()

void thd_inc_row_count ( MYSQL_THD  thd)

◆ thd_is_transaction_active()

bool thd_is_transaction_active ( THD thd)

Support method to check if connection has already started transaction.

Parameters
thdCurrent thread
Return values
trueif connection already started transaction

◆ thd_killed()

int thd_killed ( const void *  v_thd)

Check the killed state of a connection.

In MySQL support for the KILL statement is cooperative. The KILL statement only sets a "killed" flag. This function returns the value of that flag. A thread should check it often, especially inside time-consuming loops, and gracefully abort the operation if it is non-zero.

Parameters
v_thduser thread connection handle
Return values
0the connection is active
1the connection has been killed

◆ thd_lock_data()

void thd_lock_data ( THD thd)

Lock data that needs protection in THD object.

Parameters
thdTHD object

◆ thd_make_lex_string()

MYSQL_LEX_STRING * thd_make_lex_string ( MYSQL_THD  thd,
MYSQL_LEX_STRING lex_str,
const char *  str,
size_t  size,
int  allocate_lex_string 
)

Create a LEX_STRING in this connection's local memory pool.

Parameters
thduser thread connection handle
lex_strpointer to LEX_STRING object to be initialized
strinitializer to be copied into lex_str
sizelength of str, in bytes
allocate_lex_stringflag: if TRUE, allocate new LEX_STRING object, instead of using lex_str value
Returns
NULL on failure, or pointer to the LEX_STRING object
See also
thd_alloc()

◆ thd_mark_transaction_to_rollback()

void thd_mark_transaction_to_rollback ( MYSQL_THD  thd,
int  all 
)

Mark transaction to rollback and mark error as fatal to a sub-statement if in sub statement mode.

Parameters
thduser thread connection handle
allif all != 0, rollback the main transaction

◆ thd_memdup()

void * thd_memdup ( MYSQL_THD  thd,
const void *  str,
size_t  size 
)
See also
thd_alloc()

◆ thd_report_lock_wait()

void thd_report_lock_wait ( THD self,
THD wait_for,
bool  may_survive_prepare 
)

Call it just when the engine find a transaction should wait another transaction to release a lock.

Interface for Engine to report lock conflict. The caller should guarantee self and thd_wait_for does not be freed, while it is called.

Parameters
[in]selfThe thd session which is waiting for the lock to release
[in]wait_forThe session which is holding the lock
[in]may_survive_preparetrue: edge MAY remain even after wait_for session PREPAREs its transaction, false: edge CERTAINLY will be removed before or during PREPARE of transaction run by the wait_for session.

◆ thd_report_row_lock_wait()

void thd_report_row_lock_wait ( THD self,
THD wait_for 
)
Deprecated:
Please use thd_report_lock_wait(self, wait_for, true) instead.
See also
thd_report_lock_wait Call it just when the engine find a transaction should wait another transaction to release a row lock.
Parameters
[in]selfThe thd session which is waiting for the lock to release
[in]wait_forThe session which is holding the lock

◆ thd_security_context()

char * thd_security_context ( MYSQL_THD  thd,
char *  buffer,
size_t  length,
size_t  max_query_len 
)

Dumps a text description of a thread, its security context (user, host) and the current query.

Parameters
thdthread context
bufferpointer to preferred result buffer
lengthlength of buffer
max_query_lenhow many chars of query to copy (0 for all)
Returns
Pointer to string

◆ thd_set_ha_data()

void thd_set_ha_data ( MYSQL_THD  thd,
const struct handlerton hton,
const void *  ha_data 
)

Provide a handler data setter to simplify coding.

See also
thd_set_ha_data() definition in plugin.h

◆ thd_set_kill_status()

void thd_set_kill_status ( const MYSQL_THD  thd)

Set the killed status of the current statement.

Parameters
thduser thread connection handle

◆ thd_set_killed()

void thd_set_killed ( THD thd)

Set the state on connection to killed.

Parameters
thdTHD object

◆ thd_set_net_read_write()

void thd_set_net_read_write ( THD thd,
uint  val 
)

Set reading/writing on socket, used by SHOW PROCESSLIST.

Parameters
thdTHD object
valValue to set it to (0 or 1)

◆ thd_set_not_killable()

void thd_set_not_killable ( THD thd)

Mark the THD as not killable as it is not currently used by a thread.

Parameters
thdTHD object

◆ thd_set_psi()

void thd_set_psi ( THD thd,
PSI_thread psi 
)

Set reference to Performance Schema object for THD object.

Parameters
thdTHD object
psiPerformance schema object for thread

◆ thd_set_scheduler_data()

void thd_set_scheduler_data ( THD thd,
void *  data 
)

Set reference to Scheduler data object for THD object.

Parameters
thdTHD object
dataScheduler data object to set on THD

◆ thd_sql_command()

int thd_sql_command ( const MYSQL_THD  thd)

◆ thd_storage_lock_wait()

void thd_storage_lock_wait ( MYSQL_THD  thd,
long long  value 
)

◆ thd_store_globals()

void thd_store_globals ( THD thd)

Set thread specific environment required for thd cleanup in thread pool.

Parameters
thdTHD object

◆ thd_strdup()

char * thd_strdup ( MYSQL_THD  thd,
const char *  str 
)
See also
thd_alloc()

◆ thd_strmake()

char * thd_strmake ( MYSQL_THD  thd,
const char *  str,
size_t  size 
)
See also
thd_alloc()

◆ thd_tablespace_op()

int thd_tablespace_op ( const MYSQL_THD  thd)

◆ thd_test_options()

long long thd_test_options ( const MYSQL_THD  thd,
long long  test_options 
)

◆ thd_tx_arbitrate()

MYSQL_THD thd_tx_arbitrate ( MYSQL_THD  requestor,
MYSQL_THD  holder 
)

◆ thd_tx_is_dd_trx()

int thd_tx_is_dd_trx ( const MYSQL_THD  thd)

◆ thd_tx_is_read_only()

int thd_tx_is_read_only ( const MYSQL_THD  thd)

◆ thd_tx_isolation()

int thd_tx_isolation ( const MYSQL_THD  thd)

◆ thd_tx_priority()

int thd_tx_priority ( const MYSQL_THD  thd)

◆ thd_unlock_data()

void thd_unlock_data ( THD thd)

Unlock data that needs protection in THD object.

Parameters
thdTHD object

◆ thd_wait_begin()

void thd_wait_begin ( MYSQL_THD  thd,
int  wait_type 
)

Interface for MySQL Server, plugins and storage engines to report when they are going to sleep/stall.

This is currently only implemented by by the threadpool and used to have better knowledge of which threads that currently are actively running on CPUs. When not running with TP this makes a call, possibly through a service, to an empty function.

thd_wait_end MUST be called immediately after waking up again.

More info can be found in the TP documentation.

Parameters
thdCalling thread context. If nullptr is passed, current_thd is used.
wait_typeAn enum value from the enum thd_wait_type (defined in include/mysql/service_thd_wait.h) but passed as int to preserve compatibility with exported service api.

◆ thd_wait_end()

void thd_wait_end ( MYSQL_THD  thd)

Interface for MySQL Server, plugins and storage engines to report when they waking up from a sleep/stall.

This is currently only implemented by by the threadpool and used to have better knowledge of which threads that currently are actively running on CPUs. When not running with TP this makes a call, possibly through a service, to an empty function.

More info can be found in the TP documentation.

Parameters
thdCalling thread context. If nullptr is passed, current_thd is used.

◆ truncated_str_length()

static size_t truncated_str_length ( const CHARSET_INFO cs,
const char *  start,
size_t  original_size,
size_t  max_size 
)
static

Returns the size of the beginning part of a (multibyte) string, which can fit in max_size bytes.

Parameters
[in]cscharset_info
[in]startpointer to the string
[in]original_sizethe length of the string (in bytes)
[in]max_sizethe size of the buffer which needs to hold the string
Returns
the maximum length of a prefix of the string, that can be stored