MySQL 9.1.0
Source Code Documentation
sql_base.h File Reference
#include <stddef.h>
#include <sys/types.h>
#include <memory>
#include <string>
#include "lex_string.h"
#include "map_helpers.h"
#include "mem_root_deque.h"
#include "my_base.h"
#include "my_inttypes.h"
#include "mysql/components/services/bits/mysql_mutex_bits.h"
#include "prealloced_array.h"
#include "sql/auth/auth_acls.h"
#include "sql/locked_tables_list.h"
#include "sql/mdl.h"
#include "sql/sql_array.h"
#include "sql/sql_const.h"
#include "sql/trigger_def.h"
#include "thr_lock.h"

Go to the source code of this file.

Classes

struct  Table_share_deleter
 
class  Prelocking_strategy
 An abstract class for a strategy specifying how the prelocking algorithm should extend the prelocking set while processing already existing elements in the set. More...
 
class  DML_prelocking_strategy
 A Strategy for prelocking algorithm suitable for DML statements. More...
 
class  Lock_tables_prelocking_strategy
 A strategy for prelocking algorithm to be used for LOCK TABLES statement. More...
 
class  Alter_table_prelocking_strategy
 Strategy for prelocking algorithm to be used for ALTER TABLE statements. More...
 
class  Open_table_context
 A context of open_tables() function, used to recover from a failed open_table() or open_routine() attempt. More...
 

Namespaces

namespace  dd
 The version of the current data dictionary table definitions.
 

Macros

#define TEMP_PREFIX   "MY"
 
#define READ_ALL   1 /* openfrm: Read all parameters */
 
#define EXTRA_RECORD   8 /* Reservera plats f|r extra record */
 
#define DELAYED_OPEN   4096 /* Open table later */
 
#define SKIP_NEW_HANDLER   32768
 If set, open_table_from_share() will skip calling get_new_handler() to create a new handler object for the table. More...
 
#define MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK   0x0001
 
#define MYSQL_OPEN_IGNORE_FLUSH   0x0002
 
#define MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY   0x0008
 
#define MYSQL_LOCK_LOG_TABLE   0x0010
 
#define MYSQL_OPEN_HAS_MDL_LOCK   0x0020
 Do not try to acquire a metadata lock on the table: we already have one. More...
 
#define MYSQL_OPEN_GET_NEW_TABLE   0x0040
 If in locked tables mode, ignore the locked tables and get a new instance of the table. More...
 
#define MYSQL_OPEN_FAIL_ON_MDL_CONFLICT   0x0100
 Fail instead of waiting when conficting metadata lock is discovered. More...
 
#define MYSQL_OPEN_FORCE_SHARED_MDL   0x0200
 Open tables using MDL_SHARED lock instead of one specified in parser. More...
 
#define MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL   0x0400
 Open tables using MDL_SHARED_HIGH_PRIO lock instead of one specified in parser. More...
 
#define MYSQL_LOCK_IGNORE_TIMEOUT   0x0800
 When opening or locking the table, use the maximum timeout (LONG_TIMEOUT = 1 year) rather than the user-supplied timeout value. More...
 
#define MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK   0x1000
 When acquiring "strong" (SNW, SNRW, X) metadata locks on tables to be open do not acquire global, tablespace-scope and schema-scope IX locks. More...
 
#define MYSQL_LOCK_RPL_INFO_TABLE   0x2000
 When opening or locking a replication table through an internal operation rather than explicitly through an user thread. More...
 
#define MYSQL_OPEN_IGNORE_KILLED   0x4000
 Only check THD::killed if waits happen (e.g. More...
 
#define MYSQL_OPEN_NO_NEW_TABLE_IN_SE   0x8000
 For new TABLE instances constructed do not open table in the storage engine. More...
 
#define MYSQL_OPEN_SECONDARY_ENGINE   0x10000
 Open a shadow copy of a table from a secondary storage engine. More...
 
#define MYSQL_OPEN_REOPEN
 Please refer to the internals manual. More...
 

Typedefs

typedef Bounds_checked_array< Item * > Ref_item_array
 

Enumerations

enum  Find_field_result { FIELD_NOT_FOUND , BASE_FIELD_FOUND , VIEW_FIELD_FOUND }
 
enum  enum_tdc_remove_table_type {
  TDC_RT_REMOVE_ALL , TDC_RT_REMOVE_NOT_OWN , TDC_RT_REMOVE_UNUSED , TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE ,
  TDC_RT_MARK_FOR_REOPEN
}
 

Functions

bool table_def_init (void)
 
void table_def_free (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 assign_new_table_id (TABLE_SHARE *share)
 
uint cached_table_definitions (void)
 
size_t get_table_def_key (const Table_ref *table_list, const char **key)
 Get table cache key for a table list element. 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=false)
 Get the TABLE_SHARE for a table. More...
 
void release_table_share (TABLE_SHARE *share)
 Mark that we are not using table share anymore. More...
 
TABLEopen_ltable (THD *thd, Table_ref *table_list, thr_lock_type update, uint lock_flags)
 
bool open_table (THD *thd, Table_ref *table_list, Open_table_context *ot_ctx)
 Open a base table. 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...
 
TABLEfind_locked_table (TABLE *list, const char *db, const char *table_name)
 Find table in the list of open tables. More...
 
thr_lock_type read_lock_type_for_table (THD *thd, Query_tables_list *prelocking_ctx, Table_ref *table_list, bool routine_modifies_data)
 
bool mysql_rm_tmp_tables (void)
 Drop all temporary tables which have been left from previous server run. More...
 
bool rm_temporary_table (THD *thd, handlerton *base, const char *path, const dd::Table *table_def)
 Delete a temporary table. More...
 
void close_tables_for_reopen (THD *thd, Table_ref **tables, const MDL_savepoint &start_of_statement_svp)
 Prepare statement for reopening of tables and recalculation of set of prelocked tables. 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_ref *tl)
 Find a temporary table specified by Table_ref instance in the THD::temporary_tables list. More...
 
void close_thread_tables (THD *thd)
 Close all tables used by the current substatement, or all tables used by this thread if we are on the outer-most level. More...
 
bool fill_record_n_invoke_before_triggers (THD *thd, COPY_INFO *optype_info, const mem_root_deque< Item * > &fields, const mem_root_deque< Item * > &values, TABLE *table, enum enum_trigger_event_type event, int num_fields, bool raise_autoinc_has_expl_non_null_val, bool *is_row_changed)
 Fill fields in list with values from the list of items and invoke before triggers. More...
 
bool fill_record_n_invoke_before_triggers (THD *thd, Field **field, const mem_root_deque< Item * > &values, TABLE *table, enum enum_trigger_event_type event, int num_fields)
 Fill fields in array with values from the list of items and invoke before triggers. More...
 
bool resolve_var_assignments (THD *thd, LEX *lex)
 Resolve variable assignments from LEX object. More...
 
bool insert_fields (THD *thd, Query_block *query_block, const char *db_name, const char *table_name, mem_root_deque< Item * > *fields, mem_root_deque< Item * >::iterator *it, bool any_privileges)
 
bool setup_fields (THD *thd, Access_bitmask want_privilege, bool allow_sum_func, bool split_sum_funcs, bool column_update, const mem_root_deque< Item * > *typed_items, mem_root_deque< Item * > *fields, Ref_item_array ref_item_array)
 Resolve a list of expressions and setup appropriate data. More...
 
bool fill_record (THD *thd, TABLE *table, const mem_root_deque< Item * > &fields, const mem_root_deque< Item * > &values, MY_BITMAP *bitmap, MY_BITMAP *insert_into_fields_bitmap, bool raise_autoinc_has_expl_non_null_val)
 Fill fields with given items. More...
 
bool fill_record (THD *thd, TABLE *table, Field **field, const mem_root_deque< Item * > &values, MY_BITMAP *bitmap, MY_BITMAP *insert_into_fields_bitmap, bool raise_autoinc_has_expl_non_null_val)
 Fill field buffer with values from Field list. More...
 
bool check_record (THD *thd, Field **ptr)
 Check the NOT NULL constraint on all the fields of the current record. More...
 
bool invoke_table_check_constraints (THD *thd, const TABLE *table)
 Invoke check constraints defined on the table. More...
 
bool find_field_in_tables (THD *thd, Item_ident *item, Table_ref *first_table, Table_ref *last_table, int report_error, Access_bitmask want_privilege, Find_field_result *result, Field **base_field, Item_ident **ref_field)
 Find field in list of tables inside one query block. More...
 
bool find_field_in_table_ref (THD *thd, Table_ref *tr, const char *field_name, size_t length, const char *alias, const char *db_name, const char *table_name, Access_bitmask want_privilege, bool allow_rowid, Find_field_result *result, Field **base_field, Item_ident **ref_field)
 Given a table reference and the name of a field, attempt to find the field in the referenced table. More...
 
Fieldfind_field_in_table (TABLE *table, const char *field_name, bool allow_rowid, uint *field_index)
 Find field by name in a base table. More...
 
Fieldfind_field_in_table_sef (TABLE *table, const char *field_name)
 Find field in table, no side effects, only purpose is to check for field in table object and get reference to the field if found. More...
 
bool find_item_in_list (THD *thd, Item *item, mem_root_deque< Item * > *items, Item ***found, uint *counter, enum_resolution_type *resolution)
 Find Item in list of items (find_field_in_tables analog) More...
 
bool setup_natural_join_row_types (THD *thd, mem_root_deque< Table_ref * > *from_clause, Name_resolution_context *context)
 
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...
 
void update_non_unique_table_error (Table_ref *update, const char *operation, Table_ref *duplicate)
 Issue correct error message in case we found 2 duplicate tables which prevent some update operation. More...
 
int setup_ftfuncs (const THD *thd, Query_block *select)
 
bool init_ftfuncs (THD *thd, Query_block *select)
 
int run_before_dml_hook (THD *thd)
 Run the server hook called "before_dml". More...
 
bool get_and_lock_tablespace_names (THD *thd, Table_ref *tables_start, Table_ref *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_ref *table_list, Table_ref *table_list_end, ulong lock_wait_timeout, uint flags, Prealloced_array< MDL_request *, 1 > *schema_reqs=nullptr)
 Acquire "strong" (SRO, SNW, SNRW) metadata locks on tables used by LOCK TABLES or by a DDL statement. More...
 
bool open_tables (THD *thd, Table_ref **tables, uint *counter, uint flags, Prelocking_strategy *prelocking_strategy)
 Open all tables in list. More...
 
bool open_and_lock_tables (THD *thd, Table_ref *tables, uint flags, Prelocking_strategy *prelocking_strategy)
 Open all tables in list, locks them and optionally process derived tables. More...
 
TABLEopen_n_lock_single_table (THD *thd, Table_ref *table_l, thr_lock_type lock_type, uint flags, Prelocking_strategy *prelocking_strategy)
 Open and lock one table. More...
 
bool open_tables_for_query (THD *thd, Table_ref *tables, uint flags)
 Open all tables for a query or statement, in list started by "tables". More...
 
bool lock_tables (THD *thd, Table_ref *tables, uint counter, uint flags)
 Lock all tables in a list. More...
 
bool lock_dictionary_tables (THD *thd, Table_ref *tables, uint count, uint flags)
 Simplified version of lock_tables() call to be used for locking data-dictionary tables when reading or storing data-dictionary objects. More...
 
void free_io_cache (TABLE *entry)
 
void intern_close_table (TABLE *entry)
 
void close_thread_table (THD *thd, TABLE **table_ptr)
 
bool close_temporary_tables (THD *thd)
 
Table_refunique_table (const Table_ref *table, Table_ref *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 drop_temporary_table (THD *thd, Table_ref *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 *new_db, const char *table_name)
 
bool open_temporary_tables (THD *thd, Table_ref *tl_list)
 Pre-open temporary tables corresponding to table list elements. More...
 
bool open_temporary_table (THD *thd, Table_ref *tl)
 Find a temporary table specified by Table_ref instance in the cache and prepare its TABLE instance for use. More...
 
bool open_trans_system_tables_for_read (THD *thd, Table_ref *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_ref *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...
 
bool close_cached_tables (THD *thd, Table_ref *tables, bool wait_for_refresh, ulong timeout)
 
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...
 
OPEN_TABLE_LISTlist_open_tables (THD *thd, const char *db, const char *wild)
 
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...
 
void tdc_flush_unused_tables ()
 
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 mark_tmp_table_for_reuse (TABLE *table)
 Reset a single temporary table. More...
 
Table_reffind_table_in_global_list (Table_ref *table, const char *db_name, const char *table_name)
 Find table in global list. More...
 
bool open_tables (THD *thd, Table_ref **tables, uint *counter, uint flags)
 
TABLEopen_n_lock_single_table (THD *thd, Table_ref *table_l, thr_lock_type lock_type, uint flags)
 
bool open_and_lock_tables (THD *thd, Table_ref *tables, uint flags)
 
TABLE_SHAREget_cached_table_share (const char *db, const char *table_name)
 Get an existing table definition from the table definition cache. More...
 
bool is_acl_table_in_non_LTM (const Table_ref *tl, enum enum_locked_tables_mode ltm)
 Check if given Table_ref is a acl table and is being read and not in LOCK TABLE mode. More...
 
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...
 

Variables

constexpr int REPORT_NO_ERRORS = 0x00
 
constexpr int REPORT_NON_UNIQUE = 0x01
 
constexpr int REPORT_UNKNOWN_TABLE = 0x02
 
constexpr int REPORT_BAD_FIELD = 0x04
 
constexpr int REPORT_ALL_ERRORS
 
mysql_mutex_t LOCK_open
 LOCK_open protects the following variables/objects: More...
 
Fieldnot_found_field
 
Fieldview_ref_found
 
malloc_unordered_map< std::string, std::unique_ptr< TABLE_SHARE, Table_share_deleter > > * table_def_cache
 

Macro Definition Documentation

◆ DELAYED_OPEN

#define DELAYED_OPEN   4096 /* Open table later */

◆ EXTRA_RECORD

#define EXTRA_RECORD   8 /* Reservera plats f|r extra record */

◆ MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY

#define MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY   0x0008

◆ MYSQL_LOCK_IGNORE_TIMEOUT

#define MYSQL_LOCK_IGNORE_TIMEOUT   0x0800

When opening or locking the table, use the maximum timeout (LONG_TIMEOUT = 1 year) rather than the user-supplied timeout value.

◆ MYSQL_LOCK_LOG_TABLE

#define MYSQL_LOCK_LOG_TABLE   0x0010

◆ MYSQL_LOCK_RPL_INFO_TABLE

#define MYSQL_LOCK_RPL_INFO_TABLE   0x2000

When opening or locking a replication table through an internal operation rather than explicitly through an user thread.

◆ MYSQL_OPEN_FAIL_ON_MDL_CONFLICT

#define MYSQL_OPEN_FAIL_ON_MDL_CONFLICT   0x0100

Fail instead of waiting when conficting metadata lock is discovered.

◆ MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL

#define MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL   0x0400

Open tables using MDL_SHARED_HIGH_PRIO lock instead of one specified in parser.

◆ MYSQL_OPEN_FORCE_SHARED_MDL

#define MYSQL_OPEN_FORCE_SHARED_MDL   0x0200

Open tables using MDL_SHARED lock instead of one specified in parser.

◆ MYSQL_OPEN_GET_NEW_TABLE

#define MYSQL_OPEN_GET_NEW_TABLE   0x0040

If in locked tables mode, ignore the locked tables and get a new instance of the table.

◆ MYSQL_OPEN_HAS_MDL_LOCK

#define MYSQL_OPEN_HAS_MDL_LOCK   0x0020

Do not try to acquire a metadata lock on the table: we already have one.

◆ MYSQL_OPEN_IGNORE_FLUSH

#define MYSQL_OPEN_IGNORE_FLUSH   0x0002

◆ MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK

#define MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK   0x0001

◆ MYSQL_OPEN_IGNORE_KILLED

#define MYSQL_OPEN_IGNORE_KILLED   0x4000

Only check THD::killed if waits happen (e.g.

wait on MDL, wait on table flush, wait on thr_lock.c locks) while opening and locking table.

◆ MYSQL_OPEN_NO_NEW_TABLE_IN_SE

#define MYSQL_OPEN_NO_NEW_TABLE_IN_SE   0x8000

For new TABLE instances constructed do not open table in the storage engine.

Existing TABLE instances for which there is a handler object which represents table open in storage engines can still be used.

◆ MYSQL_OPEN_REOPEN

#define MYSQL_OPEN_REOPEN
Value:
MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY | MYSQL_LOCK_IGNORE_TIMEOUT | \
MYSQL_OPEN_IGNORE_KILLED | MYSQL_OPEN_GET_NEW_TABLE | \
MYSQL_OPEN_HAS_MDL_LOCK)
#define MYSQL_OPEN_IGNORE_FLUSH
Definition: sql_base.h:127
#define MYSQL_LOCK_IGNORE_TIMEOUT
When opening or locking the table, use the maximum timeout (LONG_TIMEOUT = 1 year) rather than the us...
Definition: sql_base.h:155
#define MYSQL_OPEN_GET_NEW_TABLE
If in locked tables mode, ignore the locked tables and get a new instance of the table.
Definition: sql_base.h:140
#define MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK
Definition: sql_base.h:126

Please refer to the internals manual.

◆ MYSQL_OPEN_SECONDARY_ENGINE

#define MYSQL_OPEN_SECONDARY_ENGINE   0x10000

Open a shadow copy of a table from a secondary storage engine.

◆ MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK

#define MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK   0x1000

When acquiring "strong" (SNW, SNRW, X) metadata locks on tables to be open do not acquire global, tablespace-scope and schema-scope IX locks.

◆ READ_ALL

#define READ_ALL   1 /* openfrm: Read all parameters */

◆ SKIP_NEW_HANDLER

#define SKIP_NEW_HANDLER   32768

If set, open_table_from_share() will skip calling get_new_handler() to create a new handler object for the table.

Designed to be used when opening a table from inside storage engines.

◆ TEMP_PREFIX

#define TEMP_PREFIX   "MY"

Typedef Documentation

◆ Ref_item_array

Enumeration Type Documentation

◆ enum_tdc_remove_table_type

Enumerator
TDC_RT_REMOVE_ALL 
TDC_RT_REMOVE_NOT_OWN 
TDC_RT_REMOVE_UNUSED 
TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE 
TDC_RT_MARK_FOR_REOPEN 

◆ Find_field_result

Enumerator
FIELD_NOT_FOUND 
BASE_FIELD_FOUND 
VIEW_FIELD_FOUND 

Function Documentation

◆ open_and_lock_tables()

bool open_and_lock_tables ( THD thd,
Table_ref tables,
uint  flags 
)
inline

◆ open_n_lock_single_table()

TABLE * open_n_lock_single_table ( THD thd,
Table_ref table_l,
thr_lock_type  lock_type,
uint  flags 
)
inline

◆ open_tables()

bool open_tables ( THD thd,
Table_ref **  tables,
uint *  counter,
uint  flags 
)
inline

Variable Documentation

◆ not_found_field

Field* not_found_field
extern

◆ REPORT_ALL_ERRORS

constexpr int REPORT_ALL_ERRORS
constexpr
Initial value:
=
constexpr int REPORT_NON_UNIQUE
Definition: sql_base.h:94
constexpr int REPORT_BAD_FIELD
Definition: sql_base.h:96
constexpr int REPORT_UNKNOWN_TABLE
Definition: sql_base.h:95

◆ REPORT_BAD_FIELD

constexpr int REPORT_BAD_FIELD = 0x04
constexpr

◆ REPORT_NO_ERRORS

constexpr int REPORT_NO_ERRORS = 0x00
constexpr

◆ REPORT_NON_UNIQUE

constexpr int REPORT_NON_UNIQUE = 0x01
constexpr

◆ REPORT_UNKNOWN_TABLE

constexpr int REPORT_UNKNOWN_TABLE = 0x02
constexpr

◆ view_ref_found

Field* view_ref_found
extern