diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 3ad3b6adf89..3d34f657ae3 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -4448,6 +4448,14 @@ TABLE *open_purge_table(THD *thd, const char *db, size_t dblen, DBUG_RETURN(error ? NULL : tl->table); } +TABLE *get_purge_table(THD *thd) +{ + /* see above, at most one table can be opened */ + DBUG_ASSERT(thd->open_tables == NULL || thd->open_tables->next == NULL); + return thd->open_tables; +} + + /** Find an open table in the list of prelocked tabled Used for foreign key actions, for example, in UPDATE t1 SET a=1; diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index a1093fd24fd..ac94658687a 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -132,6 +132,7 @@ void destroy_thd(MYSQL_THD thd); void reset_thd(MYSQL_THD thd); TABLE *open_purge_table(THD *thd, const char *db, size_t dblen, const char *tb, size_t tblen); +TABLE *get_purge_table(THD *thd); /** Check if user has used xtradb extended system variable that is not currently supported by innodb or marked as deprecated. */ @@ -22119,8 +22120,17 @@ innobase_find_mysql_table_for_vc( THD* thd, dict_table_t* table) { - if (table->vc_templ->mysql_table_query_id == thd_get_query_id(thd)) { - return table->vc_templ->mysql_table; + TABLE *mysql_table; + bool bg_thread = THDVAR(thd, background_thread); + + if (bg_thread) { + if ((mysql_table = get_purge_table(thd))) { + return mysql_table; + } + } else { + if (table->vc_templ->mysql_table_query_id == thd_get_query_id(thd)) { + return table->vc_templ->mysql_table; + } } char dbname[MAX_DATABASE_NAME_LEN + 1]; @@ -22150,15 +22160,14 @@ innobase_find_mysql_table_for_vc( tbnamelen = filename_to_tablename(tbname, t_tbname, MAX_TABLE_NAME_LEN + 1); - TABLE *mysql_table = find_fk_open_table(thd, t_dbname, dbnamelen, - t_tbname, tbnamelen); - - if (!mysql_table && THDVAR(thd, background_thread)) { - /* only open the table in background purge threads */ - mysql_table = open_purge_table(thd, t_dbname, dbnamelen, - t_tbname, tbnamelen); + if (bg_thread) { + return open_purge_table(thd, t_dbname, dbnamelen, + t_tbname, tbnamelen); } + mysql_table = find_fk_open_table(thd, t_dbname, dbnamelen, + t_tbname, tbnamelen); + table->vc_templ->mysql_table = mysql_table; table->vc_templ->mysql_table_query_id = thd_get_query_id(thd); return mysql_table;