MySQL 8.0.40
Source Code Documentation
Table_cache Class Reference

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...
 
TABLEget_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...
 
TABLEm_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 []
 

Detailed Description

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.

Member Function Documentation

◆ add_used_table()

bool Table_cache::add_used_table ( THD thd,
TABLE table 
)
inline

Add newly created TABLE object which is going to be used right away to the table cache.

Note
Caller should own lock on the table cache.
Sets TABLE::in_use member as side effect.
Return values
false- success.
true- failure.

◆ assert_owner()

void Table_cache::assert_owner ( )
inline

Assert that caller owns lock on the table cache.

◆ cached_tables()

uint Table_cache::cached_tables ( ) const
inline

Get number of TABLE instances in the cache.

◆ check_unused()

void Table_cache::check_unused ( )
inlineprivate

◆ destroy()

void Table_cache::destroy ( )

Destroy instance of table cache.

◆ free_all_unused_tables()

void Table_cache::free_all_unused_tables ( )

Free all unused TABLE objects in the table cache.

◆ free_unused_tables_if_necessary()

void Table_cache::free_unused_tables_if_necessary ( THD thd)
inlineprivate

Free unused TABLE instances if total number of TABLE objects in table cache has exceeded table_cache_size_per_instance limit.

Note
That we might need to free more than one instance during this call if table_cache_size was changed dynamically.

◆ get_table()

TABLE * Table_cache::get_table ( THD thd,
const char *  key,
size_t  key_length,
TABLE_SHARE **  share 
)
inline

Get an unused TABLE instance from the table cache.

Parameters
thdThread context.
keyKey identifying table.
key_lengthLength of key for the table.
[out]shareNULL - 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.
Note
Caller should own lock on the table cache.
Sets TABLE::in_use member as side effect.
Return values
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.

◆ init()

bool Table_cache::init ( void  )

Initialize instance of table cache.

Return values
false- success.
true- failure.

◆ init_psi_keys()

void Table_cache::init_psi_keys ( void  )
static

Init P_S instrumentation key for mutex protecting Table_cache instance.

◆ link_unused_table()

void Table_cache::link_unused_table ( TABLE table)
inlineprivate

Add table to the tail of unused tables list for table cache (i.e.

as the most recently used table in this list).

◆ lock()

void Table_cache::lock ( )
inline

Acquire lock on table cache instance.

◆ print_tables()

void Table_cache::print_tables ( )

Print debug information for the contents of the table cache.

◆ release_table()

void Table_cache::release_table ( THD thd,
TABLE table 
)
inline

Put used TABLE instance back to the table cache and mark it as unused.

Note
Caller should own lock on the table cache.
Sets TABLE::in_use member as side effect.

◆ remove_table()

void Table_cache::remove_table ( TABLE table)
inline

Prepare used or unused TABLE instance for destruction by removing it from the table cache.

Note
Caller should own lock on the table cache.

◆ unlink_unused_table()

void Table_cache::unlink_unused_table ( TABLE table)
inlineprivate

Remove table from the unused tables list for table cache.

◆ unlock()

void Table_cache::unlock ( )
inline

Release lock on table cache instance.

Member Data Documentation

◆ m_cache

std::unordered_map<std::string, std::unique_ptr<Table_cache_element> > Table_cache::m_cache
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.

◆ m_lock

mysql_mutex_t Table_cache::m_lock
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.

◆ m_lock_key

PSI_mutex_key Table_cache::m_lock_key
staticprivate

◆ m_mutex_keys

PSI_mutex_info Table_cache::m_mutex_keys
staticprivate
Initial value:
= {
{&m_lock_key, "LOCK_table_cache", 0, 0, PSI_DOCUMENT_ME}}
static PSI_mutex_key m_lock_key
Definition: table_cache.h:121
#define PSI_DOCUMENT_ME
Definition: component_common.h:29

◆ m_table_count

uint Table_cache::m_table_count
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.

◆ m_unused_tables

TABLE* Table_cache::m_unused_tables
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.


The documentation for this class was generated from the following files: