MySQL 8.0.39
Source Code Documentation
|
Cache for open TABLE objects. More...
#include <table_cache.h>
Public Member Functions | |
bool | init () |
Initialize instance of table cache. More... | |
void | destroy () |
Destroy instance of table cache. More... | |
void | lock () |
Acquire lock on table cache instance. More... | |
void | unlock () |
Release lock on table cache instance. More... | |
void | assert_owner () |
Assert that caller owns lock on the table cache. More... | |
TABLE * | get_table (THD *thd, const char *key, size_t key_length, TABLE_SHARE **share) |
Get an unused TABLE instance from the table cache. More... | |
void | release_table (THD *thd, TABLE *table) |
Put used TABLE instance back to the table cache and mark it as unused. More... | |
bool | add_used_table (THD *thd, TABLE *table) |
Add newly created TABLE object which is going to be used right away to the table cache. More... | |
void | remove_table (TABLE *table) |
Prepare used or unused TABLE instance for destruction by removing it from the table cache. More... | |
uint | cached_tables () const |
Get number of TABLE instances in the cache. More... | |
void | free_all_unused_tables () |
Free all unused TABLE objects in the table cache. More... | |
void | print_tables () |
Print debug information for the contents of the table cache. More... | |
Static Public Member Functions | |
static void | init_psi_keys () |
Init P_S instrumentation key for mutex protecting Table_cache instance. More... | |
Private Member Functions | |
void | check_unused () |
void | link_unused_table (TABLE *table) |
Add table to the tail of unused tables list for table cache (i.e. More... | |
void | unlink_unused_table (TABLE *table) |
Remove table from the unused tables list for table cache. More... | |
void | free_unused_tables_if_necessary (THD *thd) |
Free unused TABLE instances if total number of TABLE objects in table cache has exceeded table_cache_size_per_instance limit. More... | |
Private Attributes | |
mysql_mutex_t | m_lock |
The table cache lock protects the following data: More... | |
std::unordered_map< std::string, std::unique_ptr< Table_cache_element > > | m_cache |
The hash of Table_cache_element objects, each table/table share that has any TABLE object in the Table_cache has a Table_cache_element from which the list of free TABLE objects in this table cache AND the list of used TABLE objects in this table cache is stored. More... | |
TABLE * | m_unused_tables |
List that contains all TABLE instances for tables in this particular table cache that are in not use by any thread. More... | |
uint | m_table_count |
Total number of TABLE instances for tables in this particular table cache (both in use by threads and not in use). More... | |
Static Private Attributes | |
static PSI_mutex_key | m_lock_key |
static PSI_mutex_info | m_mutex_keys [] |
Cache for open TABLE objects.
The idea behind this cache is that most statements don't need to go to a central table definition cache to get a TABLE object and therefore don't need to lock LOCK_open mutex. Instead they only need to go to one Table_cache instance (the specific instance is determined by thread id) and only lock the mutex protecting this cache. DDL statements that need to remove all TABLE objects from all caches need to lock mutexes for all Table_cache instances, but they are rare.
This significantly increases scalability in some scenarios.
Add newly created TABLE object which is going to be used right away to the table cache.
false | - success. |
true | - failure. |
|
inline |
Assert that caller owns lock on the table cache.
|
inline |
Get number of TABLE instances in the cache.
|
inlineprivate |
void Table_cache::destroy | ( | ) |
Destroy instance of table cache.
void Table_cache::free_all_unused_tables | ( | ) |
Free all unused TABLE objects in the table cache.
|
inlineprivate |
Free unused TABLE instances if total number of TABLE objects in table cache has exceeded table_cache_size_per_instance limit.
|
inline |
Get an unused TABLE instance from the table cache.
thd | Thread context. | |
key | Key identifying table. | |
key_length | Length of key for the table. | |
[out] | share | NULL - if table cache doesn't contain any information about the table (i.e. doesn't have neither used nor unused TABLE objects for it). Pointer to TABLE_SHARE for the table otherwise. |
non-NULL | - pointer to unused TABLE object, "share" out-parameter contains pointer to TABLE_SHARE for this table. |
NULL | - no unused TABLE object was found, "share" parameter contains pointer to TABLE_SHARE for this table if there are used TABLE objects in cache and NULL otherwise. |
bool Table_cache::init | ( | void | ) |
Initialize instance of table cache.
false | - success. |
true | - failure. |
|
static |
Init P_S instrumentation key for mutex protecting Table_cache instance.
|
inlineprivate |
Add table to the tail of unused tables list for table cache (i.e.
as the most recently used table in this list).
|
inline |
Acquire lock on table cache instance.
void Table_cache::print_tables | ( | ) |
Print debug information for the contents of the table cache.
Put used TABLE instance back to the table cache and mark it as unused.
|
inline |
Prepare used or unused TABLE instance for destruction by removing it from the table cache.
|
inlineprivate |
Remove table from the unused tables list for table cache.
|
inline |
Release lock on table cache instance.
|
private |
The hash of Table_cache_element objects, each table/table share that has any TABLE object in the Table_cache has a Table_cache_element from which the list of free TABLE objects in this table cache AND the list of used TABLE objects in this table cache is stored.
We use Table_cache_element::share::table_cache_key as key for this hash.
|
private |
The table cache lock protects the following data:
1) m_unused_tables list. 2) m_cache hash. 3) used_tables, free_tables lists in Table_cache_element objects in this cache. 4) m_table_count - total number of TABLE objects in this cache. 5) the element in TABLE_SHARE::cache_element[] array that corresponds to this cache, 6) in_use member in TABLE object. 7) Also ownership of mutexes for all caches are required to update the refresh_version and table_def_shutdown_in_progress variables and TABLE_SHARE::version member.
The intention is that any query that finds a cached table object in its designated table cache should only need to lock this mutex instance and there should be no need to lock LOCK_open. LOCK_open is still required however to create and release TABLE objects. However most usage of the MySQL Server should be able to set the cache size big enough so that the majority of the queries only need to lock this mutex instance and not LOCK_open.
|
staticprivate |
|
staticprivate |
|
private |
Total number of TABLE instances for tables in this particular table cache (both in use by threads and not in use).
This value summed over all table caches is accessible to users as Open_tables status variable.
|
private |
List that contains all TABLE instances for tables in this particular table cache that are in not use by any thread.
Recently used TABLE instances are appended to the end of the list. Thus the beginning of the list contains which have been least recently used.