MySQL  8.0.17
Source Code Documentation
sql_base.cc File Reference
#include "sql/sql_base.h"
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <atomic>
#include <functional>
#include <memory>
#include <unordered_map>
#include <utility>
#include "m_ctype.h"
#include "m_string.h"
#include "map_helpers.h"
#include "mf_wcomp.h"
#include "mutex_lock.h"
#include "my_alloc.h"
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_dir.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_psi_config.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "my_systime.h"
#include "my_table_map.h"
#include "my_thread_local.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/mysql_cond_bits.h"
#include "mysql/components/services/psi_cond_bits.h"
#include "mysql/components/services/psi_mutex_bits.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_cond.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/psi/mysql_table.h"
#include "mysql/psi/psi_base.h"
#include "mysql/psi/psi_table.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/thread_type.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/binlog.h"
#include "sql/check_stack.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/dd/dd_schema.h"
#include "sql/dd/dd_table.h"
#include "sql/dd/dd_tablespace.h"
#include "sql/dd/string_type.h"
#include "sql/dd/types/abstract_table.h"
#include "sql/dd/types/column.h"
#include "sql/dd/types/column_statistics.h"
#include "sql/dd/types/foreign_key.h"
#include "sql/dd/types/function.h"
#include "sql/dd/types/procedure.h"
#include "sql/dd/types/schema.h"
#include "sql/dd/types/table.h"
#include "sql/dd/types/view.h"
#include "sql/dd_table_share.h"
#include "sql/debug_sync.h"
#include "sql/derror.h"
#include "sql/error_handler.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/histograms/histogram.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_subselect.h"
#include "sql/lock.h"
#include "sql/log.h"
#include "sql/log_event.h"
#include "sql/mysqld.h"
#include "sql/nested_join.h"
#include "sql/partition_info.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/rpl_gtid.h"
#include "sql/rpl_handler.h"
#include "sql/rpl_rli.h"
#include "sql/session_tracker.h"
#include "sql/sp.h"
#include "sql/sp_cache.h"
#include "sql/sp_head.h"
#include "sql/sql_audit.h"
#include "sql/sql_backup_lock.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_data_change.h"
#include "sql/sql_error.h"
#include "sql/sql_handler.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h"
#include "sql/sql_prepare.h"
#include "sql/sql_select.h"
#include "sql/sql_show.h"
#include "sql/sql_sort.h"
#include "sql/sql_table.h"
#include "sql/sql_update.h"
#include "sql/sql_view.h"
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/table_cache.h"
#include "sql/table_trigger_dispatcher.h"
#include "sql/thd_raii.h"
#include "sql/transaction.h"
#include "sql/transaction_info.h"
#include "sql/xa.h"
#include "sql_string.h"
#include "table_id.h"
#include "template_utils.h"
#include "thr_mutex.h"

Classes

class  Repair_mrg_table_error_handler
 This internal handler is used to trap ER_NO_SUCH_TABLE and ER_WRONG_MRG_TABLE errors during CHECK/REPAIR TABLE for MERGE tables. More...
 
class  MDL_deadlock_handler
 An error handler which converts, if possible, ER_LOCK_DEADLOCK error that can occur when we are trying to acquire a metadata lock to a request for back-off and re-start of open_tables() process. More...
 
class  Fix_row_type_error_handler
 Error handler class for supressing HA_ERR_ROW_FORMAT_CHANGED errors from SE. More...
 
class  MDL_deadlock_discovery_repair_handler
 An error handler to mark transaction to rollback on DEADLOCK error during DISCOVER / REPAIR. More...
 

Macros

#define WRONG_GRANT   (Field *)-1
 

Typedefs

using Table_definition_cache = malloc_unordered_map< std::string, std::unique_ptr< TABLE_SHARE, Table_share_deleter > >
 

Functions

static void init_tdc_psi_keys (void)
 Initialize performance schema instrumentation points used by the table cache. More...
 
static bool check_and_update_table_version (THD *thd, TABLE_LIST *tables, TABLE_SHARE *table_share)
 Compare metadata versions of an element obtained from the table definition cache and its corresponding node in the parse tree. More...
 
static bool open_table_entry_fini (THD *thd, TABLE_SHARE *share, const dd::Table *table, TABLE *entry)
 Finalize the process of TABLE creation by loading table triggers and taking action if a HEAP table content was emptied implicitly. More...
 
static bool auto_repair_table (THD *thd, TABLE_LIST *table_list)
 Auxiliary routine which is used for performing automatical table repair. More...
 
static TABLEfind_temporary_table (THD *thd, const char *table_key, size_t table_key_length)
 Find a temporary table specified by a key in the THD::temporary_tables list. More...
 
static bool tdc_open_view (THD *thd, TABLE_LIST *table_list, const char *cache_key, size_t cache_key_length)
 Open view by getting its definition from disk (and table cache in future). More...
 
static size_t create_table_def_key (const char *db_name, const char *table_name, char *key)
 Create a table cache/table definition cache key for a table. More...
 
static size_t create_table_def_key_tmp (const THD *thd, const char *db_name, const char *table_name, char *key)
 Create a table cache/table definition cache key for a temporary table. More...
 
static std::string create_table_def_key_secondary (const char *db_name, const char *table_name)
 Create a table cache/table definition cache key for a table in a secondary storage engine. More...
 
size_t get_table_def_key (const TABLE_LIST *table_list, const char **key)
 Get table cache key for a table list element. More...
 
bool table_def_init (void)
 
void table_def_start_shutdown (void)
 Notify table definition cache that process of shutting down server has started so it has to keep number of TABLE and TABLE_SHARE objects minimal in order to reduce number of references to pluggable engines. More...
 
void table_def_free (void)
 
uint cached_table_definitions (void)
 
static TABLE_SHAREprocess_found_table_share (THD *thd, TABLE_SHARE *share, bool open_view)
 
static bool read_histograms (THD *thd, TABLE_SHARE *share, const dd::Schema *schema, const dd::Abstract_table *table_def)
 Read any existing histogram statistics from the data dictionary and store a copy of them in the TABLE_SHARE. More...
 
TABLE_SHAREget_table_share (THD *thd, const char *db, const char *table_name, const char *key, size_t key_length, bool open_view, bool open_secondary)
 Get the TABLE_SHARE for a table. More...
 
static TABLE_SHAREget_table_share_with_discover (THD *thd, TABLE_LIST *table_list, const char *key, size_t key_length, bool open_secondary, int *error)
 Get a table share. More...
 
void release_table_share (TABLE_SHARE *share)
 Mark that we are not using table share anymore. More...
 
static TABLE_SHAREget_cached_table_share (const char *db, const char *table_name)
 Get an existing table definition from the table definition cache. More...
 
OPEN_TABLE_LISTlist_open_tables (THD *thd, const char *db, const char *wild)
 
void intern_close_table (TABLE *table)
 
void free_io_cache (TABLE *table)
 
bool close_cached_tables (THD *thd, TABLE_LIST *tables, bool wait_for_refresh, ulong timeout)
 
static void mark_temp_tables_as_free_for_reuse (THD *thd)
 Mark all temporary tables which were used by the current statement or substatement as free for reuse, but only if the query_id can be cleared. More...
 
void mark_tmp_table_for_reuse (TABLE *table)
 Reset a single temporary table. More...
 
static void mark_used_tables_as_free_for_reuse (THD *thd, TABLE *table)
 
static void close_open_tables (THD *thd)
 Auxiliary function to close all tables in the open_tables list. More...
 
static void close_all_tables_for_name (THD *thd, const char *key, size_t key_length, const char *db, const char *table_name, bool remove_from_locked_tables, TABLE *skip_table)
 Close all open instances of the table but keep the MDL lock. More...
 
void close_all_tables_for_name (THD *thd, TABLE_SHARE *share, bool remove_from_locked_tables, TABLE *skip_table)
 Close all open instances of the table but keep the MDL lock. More...
 
void close_all_tables_for_name (THD *thd, const char *db, const char *table_name, bool remove_from_locked_tables)
 Close all open instances of the table but keep the MDL lock. More...
 
static bool in_LTM (THD *thd)
 
static bool belongs_to_dd_table (const TABLE_LIST *tl)
 Check if the given TABLE_LIST belongs to a a DD table. More...
 
static bool belongs_to_p_s (TABLE_LIST *tl)
 Performance Schema tables must be accessible independently of the LOCK TABLE mode. More...
 
void close_thread_tables (THD *thd)
 
static void release_or_close_table (THD *thd, TABLE *table)
 Helper function which returns TABLE to Table Cache or closes if table is marked as needing re-open. More...
 
void close_thread_table (THD *thd, TABLE **table_ptr)
 
static uint tmpkeyval (TABLE *table)
 
bool close_temporary_tables (THD *thd)
 
TABLE_LISTfind_table_in_global_list (TABLE_LIST *table, const char *db_name, const char *table_name)
 Find table in global list. More...
 
static TABLE_LISTfind_dup_table (const TABLE_LIST *table, TABLE_LIST *table_list, bool check_alias)
 Test that table is unique (It's only exists once in the table list) More...
 
TABLE_LISTunique_table (const TABLE_LIST *table, TABLE_LIST *table_list, bool check_alias)
 Test that the subject table of INSERT/UPDATE/DELETE/CREATE or (in case of MyISAMMRG) one of its children are not used later in the query. More...
 
void update_non_unique_table_error (TABLE_LIST *update, const char *operation, TABLE_LIST *duplicate)
 Issue correct error message in case we found 2 duplicate tables which prevent some update operation. More...
 
TABLEfind_temporary_table (THD *thd, const char *db, const char *table_name)
 Find temporary table specified by database and table names in the THD::temporary_tables list. More...
 
TABLEfind_temporary_table (THD *thd, const TABLE_LIST *tl)
 Find a temporary table specified by TABLE_LIST instance in the THD::temporary_tables list. More...
 
void drop_temporary_table (THD *thd, TABLE_LIST *table_list)
 Drop a temporary table. More...
 
void close_temporary_table (THD *thd, TABLE *table, bool free_share, bool delete_table)
 
void close_temporary (THD *thd, TABLE *table, bool free_share, bool delete_table)
 
bool rename_temporary_table (THD *thd, TABLE *table, const char *db, const char *table_name)
 
bool wait_while_table_is_used (THD *thd, TABLE *table, enum ha_extra_function function)
 Force all other threads to stop using the table by upgrading metadata lock on it and remove unused TABLE instances from cache. More...
 
static bool check_if_table_exists (THD *thd, TABLE_LIST *table, bool *exists)
 Check that table exists in data-dictionary or in some storage engine. More...
 
static bool open_table_get_mdl_lock (THD *thd, Open_table_context *ot_ctx, TABLE_LIST *table_list, uint flags, MDL_ticket **mdl_ticket)
 Try to acquire an MDL lock for a table being opened. More...
 
static bool tdc_wait_for_old_version (THD *thd, const char *db, const char *table_name, ulong wait_timeout, uint deadlock_weight)
 Check if table's share is being removed from the table definition cache and, if yes, wait until the flush is complete. More...
 
bool open_table (THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
 Open a base table. More...
 
TABLEfind_locked_table (TABLE *list, const char *db, const char *table_name)
 Find table in the list of open tables. More...
 
TABLEfind_table_for_mdl_upgrade (THD *thd, const char *db, const char *table_name, bool no_error)
 Find instance of TABLE with upgradable or exclusive metadata lock from the list of open tables, emit error if no such table found. More...
 
void assign_new_table_id (TABLE_SHARE *share)
 
static bool check_and_update_routine_version (THD *thd, Sroutine_hash_entry *rt, sp_head *sp)
 Compares versions of a stored routine obtained from the sp cache and the version used at prepare. More...
 
static bool fix_row_type (THD *thd, TABLE_LIST *table_list)
 Auxiliary routine for automatically updating row format for the table. More...
 
thr_lock_type read_lock_type_for_table (THD *thd, Query_tables_list *prelocking_ctx, TABLE_LIST *table_list, bool routine_modifies_data)
 
static void process_table_fks (THD *thd, Query_tables_list *prelocking_ctx, TABLE_SHARE *share, bool is_insert, bool is_update, bool is_delete, TABLE_LIST *belong_to_view, bool *need_prelocking)
 Process table's foreign keys (if any) by prelocking algorithm. More...
 
static bool open_and_process_routine (THD *thd, Query_tables_list *prelocking_ctx, Sroutine_hash_entry *rt, Prelocking_strategy *prelocking_strategy, bool has_prelocking_list, Open_table_context *ot_ctx, bool *need_prelocking, bool *routine_modifies_data)
 Handle element of prelocking set other than table. More...
 
static bool open_and_process_table (THD *thd, LEX *lex, TABLE_LIST *const tables, uint *counter, Prelocking_strategy *prelocking_strategy, bool has_prelocking_list, Open_table_context *ot_ctx)
 Handle table list element by obtaining metadata lock, opening table or view and, if prelocking strategy prescribes so, extending the prelocking set with tables and routines used by it. More...
 
int run_before_dml_hook (THD *thd)
 Run the server hook called "before_dml". More...
 
static bool is_temporary_table_being_opened (TABLE_LIST *table)
 Check whether a table being opened is a temporary table. More...
 
bool get_and_lock_tablespace_names (THD *thd, TABLE_LIST *tables_start, TABLE_LIST *tables_end, ulong lock_wait_timeout, uint flags)
 Acquire IX metadata locks on tablespace names used by LOCK TABLES or by a DDL statement. More...
 
bool lock_table_names (THD *thd, TABLE_LIST *tables_start, TABLE_LIST *tables_end, ulong lock_wait_timeout, uint flags, Prealloced_array< MDL_request *, 1 > *schema_reqs)
 Acquire "strong" (SRO, SNW, SNRW) metadata locks on tables used by LOCK TABLES or by a DDL statement. More...
 
static bool open_tables_check_upgradable_mdl (THD *thd, TABLE_LIST *tables_start, TABLE_LIST *tables_end)
 Check for upgradable (SNW, SNRW) metadata locks on tables to be opened for a DDL statement. More...
 
static bool acquire_backup_lock_in_lock_tables_mode (THD *thd, TABLE_LIST *tables_start, TABLE_LIST *tables_end)
 Iterate along a list of tables and acquire BACKUP LOCK in shared mode in case a strong MDL request (DDL/LOCK TABLES-type) was specified for a table. More...
 
bool open_tables (THD *thd, TABLE_LIST **start, uint *counter, uint flags, Prelocking_strategy *prelocking_strategy)
 Open all tables in list. More...
 
static bool check_lock_and_start_stmt (THD *thd, Query_tables_list *prelocking_ctx, TABLE_LIST *table_list)
 Check that lock is ok for tables; Call start stmt if ok. More...
 
TABLEopen_n_lock_single_table (THD *thd, TABLE_LIST *table_l, thr_lock_type lock_type, uint flags, Prelocking_strategy *prelocking_strategy)
 Open and lock one table. More...
 
TABLEopen_ltable (THD *thd, TABLE_LIST *table_list, thr_lock_type lock_type, uint lock_flags)
 
bool open_and_lock_tables (THD *thd, TABLE_LIST *tables, uint flags, Prelocking_strategy *prelocking_strategy)
 Open all tables in list, locks them and optionally process derived tables. More...
 
static bool open_secondary_engine_tables (THD *thd, uint flags)
 Check if a secondary engine can be used to execute the current statement, and if so, replace the opened tables with their secondary counterparts. More...
 
bool open_tables_for_query (THD *thd, TABLE_LIST *tables, uint flags)
 Open all tables for a query or statement, in list started by "tables". More...
 
static void mark_real_tables_as_free_for_reuse (TABLE_LIST *table_list)
 
bool lock_tables (THD *thd, TABLE_LIST *tables, uint count, uint flags)
 Lock all tables in a list. More...
 
void close_tables_for_reopen (THD *thd, TABLE_LIST **tables, const MDL_savepoint &start_of_statement_svp)
 Prepare statement for reopening of tables and recalculation of set of prelocked tables. More...
 
TABLEopen_table_uncached (THD *thd, const char *path, const char *db, const char *table_name, bool add_to_temporary_tables_list, bool open_in_engine, const dd::Table &table_def)
 Open a single table without table caching and don't add it to THD::open_tables. More...
 
bool rm_temporary_table (THD *thd, handlerton *base, const char *path, const dd::Table *table_def)
 Delete a temporary table. More...
 
bool open_temporary_table (THD *thd, TABLE_LIST *tl)
 Find a temporary table specified by TABLE_LIST instance in the cache and prepare its TABLE instance for use. More...
 
bool open_temporary_tables (THD *thd, TABLE_LIST *tl_list)
 Pre-open temporary tables corresponding to table list elements. More...
 
static Fieldfind_field_in_view (THD *thd, TABLE_LIST *table_list, const char *name, Item **ref, bool register_tree_change)
 
static Fieldfind_field_in_natural_join (THD *thd, TABLE_LIST *table_ref, const char *name, Item **ref, bool register_tree_change, TABLE_LIST **actual_table)
 Find field by name in a NATURAL/USING join table reference. More...
 
Fieldfind_field_in_table (TABLE *table, const char *name, size_t length, bool allow_rowid, uint *cached_field_index_ptr)
 
Fieldfind_field_in_table_ref (THD *thd, TABLE_LIST *table_list, const char *name, size_t length, const char *item_name, const char *db_name, const char *table_name, Item **ref, ulong want_privilege, bool allow_rowid, uint *cached_field_index_ptr, bool register_tree_change, TABLE_LIST **actual_table)
 
Fieldfind_field_in_table_sef (TABLE *table, const char *name)
 
Fieldfind_field_in_tables (THD *thd, Item_ident *item, TABLE_LIST *first_table, TABLE_LIST *last_table, Item **ref, find_item_error_report_type report_error, ulong want_privilege, bool register_tree_change)
 
Item ** find_item_in_list (THD *thd, Item *find, List< Item > &items, uint *counter, find_item_error_report_type report_error, enum_resolution_type *resolution)
 
static bool test_if_string_in_list (const char *find, List< String > *str_list)
 
static bool set_new_item_local_context (THD *thd, Item_ident *item, TABLE_LIST *table_ref)
 
static bool mark_common_columns (THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, List< String > *using_fields, uint *found_using_fields)
 
static bool store_natural_using_join_columns (THD *thd, TABLE_LIST *natural_using_join, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, List< String > *using_fields, uint found_using_fields)
 
static bool store_top_level_join_columns (THD *thd, TABLE_LIST *table_ref, TABLE_LIST *left_neighbor, TABLE_LIST *right_neighbor)
 
bool setup_natural_join_row_types (THD *thd, List< TABLE_LIST > *from_clause, Name_resolution_context *context)
 
bool resolve_var_assignments (THD *thd, LEX *lex)
 Resolve variable assignments from LEX object. More...
 
bool setup_fields (THD *thd, Ref_item_array ref_item_array, List< Item > &fields, ulong want_privilege, List< Item > *sum_func_list, bool allow_sum_func, bool column_update)
 Resolve a list of expressions and setup appropriate data. More...
 
bool insert_fields (THD *thd, Name_resolution_context *context, const char *db_name, const char *table_name, List_iterator< Item > *it, bool any_privileges)
 
bool fill_record (THD *thd, TABLE *table, List< Item > &fields, List< Item > &values, MY_BITMAP *bitmap, MY_BITMAP *insert_into_fields_bitmap)
 
static bool check_record (THD *thd, List< Item > &fields)
 Check the NOT NULL constraint on all the fields of the current record. More...
 
bool check_record (THD *thd, Field **ptr)
 Check the NOT NULL constraint on all the fields of the current record. More...
 
static bool check_inserting_record (THD *thd, Field **ptr)
 Check the NOT NULL constraint on all the fields explicitly set in INSERT INTO statement or implicitly set in BEFORE trigger. More...
 
bool invoke_table_check_constraints (THD *thd, const TABLE *table)
 Invoke check constraints defined on the table. More...
 
static bool command_can_invoke_insert_triggers (enum enum_trigger_event_type event, enum_sql_command sql_command)
 Check if SQL-statement is INSERT/INSERT SELECT/REPLACE/REPLACE SELECT and trigger event is ON INSERT. More...
 
bool call_before_insert_triggers (THD *thd, TABLE *table, enum enum_trigger_event_type event, MY_BITMAP *insert_into_fields_bitmap)
 Execute BEFORE INSERT trigger. More...
 
bool fill_record_n_invoke_before_triggers (THD *thd, COPY_INFO *optype_info, List< Item > &fields, List< Item > &values, TABLE *table, enum enum_trigger_event_type event, int num_fields)
 
bool fill_record (THD *thd, TABLE *table, Field **ptr, List< Item > &values, MY_BITMAP *bitmap, MY_BITMAP *insert_into_fields_bitmap)
 Fill field buffer with values from Field list. More...
 
bool fill_record_n_invoke_before_triggers (THD *thd, Field **ptr, List< Item > &values, TABLE *table, enum enum_trigger_event_type event, int num_fields)
 
bool mysql_rm_tmp_tables (void)
 Drop all temporary tables which have been left from previous server run. More...
 
void tdc_flush_unused_tables ()
 
void tdc_remove_table (THD *thd, enum_tdc_remove_table_type remove_type, const char *db, const char *table_name, bool has_lock)
 Remove all or some (depending on parameter) instances of TABLE and TABLE_SHARE from the table definition cache. More...
 
int setup_ftfuncs (const THD *thd, SELECT_LEX *select_lex)
 
bool init_ftfuncs (THD *thd, SELECT_LEX *select_lex)
 
bool open_trans_system_tables_for_read (THD *thd, TABLE_LIST *table_list)
 Open and lock transactional system tables for read. More...
 
void close_trans_system_tables (THD *thd)
 Close transactional system tables, opened with open_trans_system_tables_for_read(). More...
 
void close_mysql_tables (THD *thd)
 A helper function to close a mysql. More...
 
TABLEopen_log_table (THD *thd, TABLE_LIST *one_table, Open_tables_backup *backup)
 Open a log table. More...
 
void close_log_table (THD *thd, Open_tables_backup *backup)
 Close a log table. More...
 

Variables

mysql_mutex_t LOCK_open
 LOCK_open protects the following variables/objects: More...
 
mysql_cond_t COND_open
 COND_open synchronizes concurrent opening of the same share: More...
 
static PSI_mutex_key key_LOCK_open
 
static PSI_cond_key key_COND_open
 
static PSI_mutex_info all_tdc_mutexes []
 
static PSI_cond_info all_tdc_conds []
 
Table_definition_cachetable_def_cache
 
static TABLE_SHAREoldest_unused_share
 
static TABLE_SHARE end_of_unused_share
 
static bool table_def_shutdown_in_progress = false
 
static Table_id last_table_id
 
Fieldnot_found_field = (Field *)0x1
 
Fieldview_ref_found = (Field *)0x2
 
Item ** not_found_item = (Item **)0x1