19 #include "my_global.h"
29 #include "sql_parse.h"
30 #include "sql_insert.h"
34 #include "sql_partition.h"
35 #include "sql_derived.h"
38 #include "sql_handler.h"
39 #include "sql_partition.h"
45 #include "sql_trigger.h"
46 #include "transaction.h"
47 #include "sql_prepare.h"
51 #include "rpl_filter.h"
52 #include "sql_table.h"
59 #include "table_cache.h"
63 No_such_table_error_handler::handle_condition(THD *,
66 Sql_condition::enum_warning_level,
71 if (sql_errno == ER_NO_SUCH_TABLE)
89 return ((m_handled_errors > 0) && (m_unhandled_errors == 0));
103 : m_handled_errors(
false), m_unhandled_errors(
false)
106 bool handle_condition(THD *thd,
108 const char* sqlstate,
109 Sql_condition::enum_warning_level
level,
126 return (m_handled_errors && (! m_unhandled_errors));
130 bool m_handled_errors;
131 bool m_unhandled_errors;
136 Repair_mrg_table_error_handler::handle_condition(THD *,
139 Sql_condition::enum_warning_level
level,
144 if (sql_errno == ER_NO_SUCH_TABLE || sql_errno == ER_WRONG_MRG_TABLE)
146 m_handled_errors=
true;
150 m_unhandled_errors=
true;
195 #ifdef HAVE_PSI_INTERFACE
196 static PSI_mutex_key key_LOCK_open;
197 static PSI_mutex_info all_tdc_mutexes[]= {
198 { &key_LOCK_open,
"LOCK_open", PSI_FLAG_GLOBAL }
206 static void init_tdc_psi_keys(
void)
208 const char *category=
"sql";
211 count= array_elements(all_tdc_mutexes);
216 static void modify_slave_open_temp_tables(THD *thd,
int inc)
218 if (thd->system_thread == SYSTEM_THREAD_SLAVE_WORKER)
220 my_atomic_rwlock_wrlock(&slave_open_temp_tables_lock);
221 my_atomic_add32(&slave_open_temp_tables, inc);
222 my_atomic_rwlock_wrunlock(&slave_open_temp_tables_lock);
226 slave_open_temp_tables += inc;
231 HASH table_def_cache;
232 static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
233 static bool table_def_inited=
false;
234 static bool table_def_shutdown_in_progress=
false;
236 static bool check_and_update_table_version(THD *thd,
TABLE_LIST *tables,
239 static bool auto_repair_table(THD *thd,
TABLE_LIST *table_list);
241 has_write_table_with_auto_increment(
TABLE_LIST *tables);
243 has_write_table_with_auto_increment_and_select(
TABLE_LIST *tables);
244 static bool has_write_table_auto_increment_not_first_in_pk(
TABLE_LIST *tables);
271 static uint create_table_def_key(THD *thd,
char *key,
280 DBUG_ASSERT(strlen(db_name) <= NAME_LEN && strlen(table_name) <= NAME_LEN);
281 uint key_length=
static_cast<uint
>(strmake(strmake(key, db_name, NAME_LEN) +
282 1, table_name, NAME_LEN) - key +
287 int4store(key + key_length, thd->server_id);
288 int4store(key + key_length + 4, thd->variables.pseudo_thread_id);
289 key_length+= TMP_TABLE_KEY_EXTRA;
323 table_list->mdl_request.
key.db_name()) &&
325 table_list->mdl_request.
key.name()));
327 *key= (
const char*)table_list->mdl_request.
key.ptr() + 1;
328 return table_list->mdl_request.
key.length() - 1;
337 extern "C" uchar *table_def_key(
const uchar *
record,
size_t *length,
338 my_bool not_used __attribute__((unused)))
341 *length= entry->table_cache_key.length;
342 return (uchar*) entry->table_cache_key.str;
346 static void table_def_free_entry(
TABLE_SHARE *share)
348 DBUG_ENTER(
"table_def_free_entry");
353 *share->prev= share->next;
354 share->next->prev= share->prev;
356 free_table_share(share);
361 bool table_def_init(
void)
363 #ifdef HAVE_PSI_INTERFACE
367 oldest_unused_share= &end_of_unused_share;
368 end_of_unused_share.prev= &oldest_unused_share;
370 if (table_cache_manager.
init())
380 table_def_inited=
true;
382 return my_hash_init(&table_def_cache, &my_charset_bin, table_def_size,
384 (my_hash_free_key) table_def_free_entry, 0) != 0;
396 if (table_def_inited)
405 table_def_shutdown_in_progress=
true;
408 close_cached_tables(NULL, NULL, FALSE, LONG_TIMEOUT);
413 void table_def_free(
void)
415 DBUG_ENTER(
"table_def_free");
416 if (table_def_inited)
418 table_def_inited=
false;
420 my_hash_free(&table_def_cache);
428 uint cached_table_definitions(
void)
430 return table_def_cache.records;
460 const char *key, uint key_length,
461 uint db_flags,
int *error,
462 my_hash_value_type hash_value)
465 DBUG_ENTER(
"get_table_share");
473 DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE,
475 table_list->table_name,
479 if ((share= (
TABLE_SHARE*) my_hash_search_using_hash_value(&table_def_cache,
480 hash_value, (uchar*) key, key_length)))
483 if (!(share= alloc_table_share(table_list, key, key_length)))
501 assign_new_table_id(share);
503 if (my_hash_insert(&table_def_cache, (uchar*) share))
505 free_table_share(share);
508 if (open_table_def(thd, share, db_flags))
510 *error= share->error;
511 (void) my_hash_delete(&table_def_cache, (uchar*) share);
516 #ifdef HAVE_PSI_TABLE_INTERFACE
517 share->
m_psi= PSI_TABLE_CALL(get_table_share)(
false, share);
522 DBUG_PRINT(
"exit", (
"share: 0x%lx ref_count: %u",
523 (ulong) share, share->ref_count));
534 open_table_error(share, share->error, share->open_errno, share->errarg);
537 if (share->is_view && !(db_flags & OPEN_VIEW))
539 open_table_error(share, 1, ENOENT, 0);
545 if (share->ref_count == 1 && share->prev)
551 DBUG_PRINT(
"info", (
"Unlinking from not used list"));
552 *share->prev= share->next;
553 share->next->prev= share->prev;
559 while (table_def_cache.records > table_def_size &&
560 oldest_unused_share->next)
561 my_hash_delete(&table_def_cache, (uchar*) oldest_unused_share);
563 DBUG_PRINT(
"exit", (
"share: 0x%lx ref_count: %u",
564 (ulong) share, share->ref_count));
576 get_table_share_with_discover(THD *thd,
TABLE_LIST *table_list,
577 const char *key, uint key_length,
578 uint db_flags,
int *error,
579 my_hash_value_type hash_value)
584 DBUG_ENTER(
"get_table_share_with_create");
586 share= get_table_share(thd, table_list, key, key_length, db_flags, error,
612 if (share || (thd->is_error() &&
613 thd->get_stmt_da()->sql_errno() != ER_NO_SUCH_TABLE))
626 my_error(ER_OUT_OF_RESOURCES, MYF(0));
639 if (table_list->parent_l)
642 my_error(ER_WRONG_MRG_TABLE, MYF(0));
644 else if (table_list->belong_to_view)
648 my_error(ER_VIEW_INVALID, MYF(0),
649 view->view_db.str, view->view_name.str);
674 DBUG_ENTER(
"release_table_share");
676 (
"share: 0x%lx table: %s.%s ref_count: %u version: %lu",
677 (ulong) share, share->db.str, share->table_name.str,
678 share->ref_count, share->version));
682 DBUG_ASSERT(share->ref_count);
683 if (!--share->ref_count)
685 if (share->has_old_version() || table_def_shutdown_in_progress)
686 my_hash_delete(&table_def_cache, (uchar*) share);
690 DBUG_PRINT(
"info",(
"moving share to unused list"));
692 DBUG_ASSERT(share->next == 0);
693 share->prev= end_of_unused_share.prev;
694 *end_of_unused_share.prev= share;
695 end_of_unused_share.prev= &share->next;
696 share->next= &end_of_unused_share;
698 if (table_def_cache.records > table_def_size)
701 my_hash_delete(&table_def_cache, (uchar*) oldest_unused_share);
723 TABLE_SHARE *get_cached_table_share(
const char *db,
const char *table_name)
725 char key[MAX_DBKEY_LENGTH];
729 key_length= create_table_def_key((THD*) 0, key, db, table_name, 0);
730 return (
TABLE_SHARE*) my_hash_search(&table_def_cache,
731 (uchar*) key, key_length);
753 OPEN_TABLE_LIST *list_open_tables(THD *thd,
const char *db,
const char *wild)
758 DBUG_ENTER(
"list_open_tables");
760 memset(&table_list, 0,
sizeof(table_list));
761 start_list= &open_list;
766 for (uint idx=0 ; result == 0 && idx < table_def_cache.records; idx++)
770 if (db && my_strcasecmp(system_charset_info, db, share->db.str))
772 if (wild && wild_compare(share->table_name.str, wild, 0))
776 table_list.db= share->db.str;
777 table_list.table_name= share->table_name.str;
784 sql_alloc(
sizeof(**start_list)+share->table_cache_key.length)))
789 strmov((*start_list)->table=
790 strmov(((*start_list)->db= (
char*) ((*start_list)+1)),
792 share->table_name.str);
793 (*start_list)->in_use= 0;
796 ++(*start_list)->in_use;
797 (*start_list)->locked= 0;
798 start_list= &(*start_list)->next;
802 DBUG_RETURN(open_list);
812 DBUG_ENTER(
"intern_close_table");
813 DBUG_PRINT(
"tcache", (
"table: '%s'.'%s' 0x%lx",
814 table->s ? table->s->db.str :
"?",
815 table->s ? table->s->table_name.str :
"?",
818 free_io_cache(table);
819 delete table->triggers;
821 (void) closefrm(table, 1);
829 void free_io_cache(
TABLE *table)
831 DBUG_ENTER(
"free_io_cache");
832 if (table->sort.io_cache)
834 close_cached_file(table->sort.io_cache);
835 my_free(table->sort.io_cache);
836 table->sort.io_cache=0;
851 static void kill_delayed_threads_for_table(
TABLE_SHARE *share)
860 THD *in_use= tab->in_use;
862 if ((in_use->system_thread & SYSTEM_THREAD_DELAYED_INSERT) &&
865 in_use->killed= THD::KILL_CONNECTION;
867 if (in_use->mysys_var->current_cond)
898 bool close_cached_tables(THD *thd,
TABLE_LIST *tables,
899 bool wait_for_refresh, ulong timeout)
904 DBUG_ENTER(
"close_cached_tables");
905 DBUG_ASSERT(thd || (!wait_for_refresh && !tables));
920 DBUG_PRINT(
"tcache", (
"incremented global refresh_version to: %lu",
922 kill_delayed_threads();
929 while (oldest_unused_share->next)
930 (void) my_hash_delete(&table_def_cache, (uchar*) oldest_unused_share);
937 TABLE_SHARE *share= get_cached_table_share(table->db, table->table_name);
941 kill_delayed_threads_for_table(share);
944 table->table_name, TRUE);
954 if (!wait_for_refresh)
957 set_timespec(abstime, timeout);
959 if (thd->locked_tables_mode)
967 TABLE_LIST *tables_to_reopen= (tables ? tables :
968 thd->locked_tables_list.locked_tables());
971 mysql_ha_flush_tables(thd, tables_to_reopen);
973 for (
TABLE_LIST *table_list= tables_to_reopen; table_list;
974 table_list= table_list->next_global)
978 table_list->table_name, TRUE);
987 goto err_with_reopen;
994 DBUG_PRINT(
"info", (
"Waiting for other threads to close their open tables"));
996 while (found && ! thd->killed)
1004 mysql_ha_flush(thd);
1005 DEBUG_SYNC(thd,
"after_flush_unlock");
1011 for (uint idx=0 ; idx < table_def_cache.records ; idx++)
1013 share= (
TABLE_SHARE*) my_hash_element(&table_def_cache, idx);
1025 share= get_cached_table_share(table->db, table->table_name);
1041 MDL_wait_for_subgraph::DEADLOCK_WEIGHT_DDL))
1045 goto err_with_reopen;
1053 if (thd->locked_tables_mode)
1060 thd->locked_tables_list.reopen_tables(thd);
1066 for (
TABLE *tab= thd->open_tables; tab; tab= tab->next)
1069 DBUG_RETURN(result);
1083 DBUG_ENTER(
"close_cached_connections");
1090 for (idx= 0; idx < table_def_cache.records; idx++)
1095 if (!share->connect_string.length || !share->ref_count)
1100 (connection->length > share->connect_string.length ||
1101 (connection->length < share->connect_string.length &&
1102 (share->connect_string.str[connection->length] !=
'/' &&
1103 share->connect_string.str[connection->length] !=
'\\')) ||
1104 strncasecmp(connection->str, share->connect_string.str,
1105 connection->length)))
1109 tmp.db= share->db.str;
1110 tmp.table_name= share->table_name.str;
1111 tmp.next_local= tables;
1113 tables= (
TABLE_LIST *) memdup_root(thd->mem_root, (
char*)&tmp,
1119 result= close_cached_tables(thd, tables, FALSE, LONG_TIMEOUT);
1121 DBUG_RETURN(result);
1135 static void mark_temp_tables_as_free_for_reuse(THD *thd)
1137 for (
TABLE *table= thd->temporary_tables ; table ; table= table->next)
1139 if ((table->query_id == thd->query_id) && ! table->open_by_handler)
1155 DBUG_ASSERT(table->s->tmp_table);
1161 DBUG_ASSERT(table->file);
1162 table->file->extra(HA_EXTRA_DETACH_CHILDREN);
1184 table->reginfo.lock_type= TL_WRITE;
1211 static void mark_used_tables_as_free_for_reuse(THD *thd,
TABLE *table)
1213 for (;
table ; table= table->next)
1215 DBUG_ASSERT(table->pos_in_locked_tables == NULL ||
1216 table->pos_in_locked_tables->table == table);
1217 if (table->query_id == thd->query_id)
1234 static void close_open_tables(THD *thd)
1238 DBUG_PRINT(
"info", (
"thd->open_tables: 0x%lx", (
long) thd->open_tables));
1240 while (thd->open_tables)
1241 close_thread_table(thd, &thd->open_tables);
1268 bool remove_from_locked_tables,
1271 char key[MAX_DBKEY_LENGTH];
1272 uint key_length= share->table_cache_key.length;
1273 const char *db= key;
1274 const char *table_name= db + share->db.length + 1;
1276 memcpy(key, share->table_cache_key.str, key_length);
1279 for (
TABLE **prev= &thd->open_tables; *prev; )
1281 TABLE *table= *prev;
1283 if (table->s->table_cache_key.length == key_length &&
1284 !memcmp(table->s->table_cache_key.str, key, key_length) &&
1285 table != skip_table)
1287 thd->locked_tables_list.unlink_from_list(thd,
1288 table->pos_in_locked_tables,
1289 remove_from_locked_tables);
1298 if (table->db_stat &&
1300 table->file->extra(HA_EXTRA_PREPARE_FOR_DROP);
1301 close_thread_table(thd, prev);
1309 if (skip_table == NULL)
1336 void close_thread_tables(THD *thd)
1339 DBUG_ENTER(
"close_thread_tables");
1342 DBUG_PRINT(
"tcache", (
"open tables:"));
1343 for (table= thd->open_tables; table; table= table->next)
1344 DBUG_PRINT(
"tcache", (
"table: '%s'.'%s' 0x%lx", table->s->db.str,
1345 table->s->table_name.str, (
long) table));
1348 #if defined(ENABLED_DEBUG_SYNC)
1350 if (thd->debug_sync_control)
1351 DEBUG_SYNC(thd,
"before_close_thread_tables");
1354 DBUG_ASSERT(thd->transaction.stmt.is_empty() || thd->in_sub_stmt ||
1355 (thd->state_flags & Open_tables_state::BACKUPS_AVAIL));
1358 for (table= thd->open_tables; table; table= table->next)
1361 DBUG_PRINT(
"tcache", (
"table: '%s' query_id: %lu",
1362 table->s->table_name.str, (ulong) table->query_id));
1363 if (thd->locked_tables_mode <= LTM_LOCK_TABLES ||
1364 table->query_id == thd->query_id)
1366 DBUG_ASSERT(table->file);
1367 table->file->extra(HA_EXTRA_DETACH_CHILDREN);
1382 if (thd->derived_tables)
1389 for (table= thd->derived_tables ; table ; table= next)
1392 free_tmp_table(thd, table);
1394 thd->derived_tables= 0;
1400 mark_temp_tables_as_free_for_reuse(thd);
1402 if (thd->locked_tables_mode)
1406 mark_used_tables_as_free_for_reuse(thd, thd->open_tables);
1414 thd->lex->lock_tables_state= Query_tables_list::LTS_NOT_LOCKED;
1425 if (! thd->lex->requires_prelocking())
1433 if (thd->locked_tables_mode == LTM_PRELOCKED_UNDER_LOCK_TABLES)
1434 thd->locked_tables_mode= LTM_LOCK_TABLES;
1436 if (thd->locked_tables_mode == LTM_LOCK_TABLES)
1439 thd->leave_locked_tables_mode();
1455 (void)thd->binlog_flush_pending_rows_event(TRUE);
1456 mysql_unlock_tables(thd, thd->lock);
1460 thd->lex->lock_tables_state= Query_tables_list::LTS_NOT_LOCKED;
1466 if (thd->open_tables)
1467 close_open_tables(thd);
1475 void close_thread_table(THD *thd,
TABLE **table_ptr)
1477 TABLE *table= *table_ptr;
1478 DBUG_ENTER(
"close_thread_table");
1480 DBUG_ASSERT(!table->file || table->file->inited == handler::NONE);
1486 DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE,
1488 table->s->table_name.str,
1490 table->mdl_ticket= NULL;
1493 *table_ptr=table->next;
1499 table->file->extra(HA_EXTRA_DETACH_CHILDREN);
1501 free_field_buffers_larger_than(table, MAX_TDC_BLOB_SIZE);
1506 if (table->file != NULL)
1507 table->file->unbind_psi();
1514 table_def_shutdown_in_progress)
1518 intern_close_table(table);
1530 static inline uint tmpkeyval(THD *thd,
TABLE *table)
1532 return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1541 bool close_temporary_tables(THD *thd)
1543 DBUG_ENTER(
"close_temporary_tables");
1548 bool was_quote_show= TRUE;
1551 if (!thd->temporary_tables)
1554 if (!mysql_bin_log.is_open())
1557 for (table= thd->temporary_tables; table; table= tmp_next)
1559 tmp_next= table->next;
1560 close_temporary(table, 1, 1);
1562 thd->temporary_tables= 0;
1567 const char stub[]=
"DROP /*!40005 TEMPORARY */ TABLE IF EXISTS ";
1568 uint stub_len=
sizeof(stub) - 1;
1570 String s_query=
String(buf,
sizeof(buf), system_charset_info);
1571 bool found_user_tables= FALSE;
1573 memcpy(buf, stub, stub_len);
1580 for (prev_table= thd->temporary_tables, table= prev_table->next;
1582 prev_table= table, table= table->next)
1584 TABLE *prev_sorted , *sorted;
1585 if (is_user_table(table))
1587 if (!found_user_tables)
1588 found_user_tables=
true;
1589 for (prev_sorted= NULL, sorted= thd->temporary_tables; sorted != table;
1590 prev_sorted= sorted, sorted= sorted->next)
1592 if (!is_user_table(sorted) ||
1593 tmpkeyval(thd, sorted) > tmpkeyval(thd, table))
1596 prev_table->next= table->next;
1597 table->next= sorted;
1600 prev_sorted->next=
table;
1604 thd->temporary_tables=
table;
1614 if (found_user_tables &&
1615 !(was_quote_show=
test(thd->variables.option_bits & OPTION_QUOTE_SHOW_CREATE)))
1617 thd->variables.option_bits |= OPTION_QUOTE_SHOW_CREATE;
1621 for (table= thd->temporary_tables; table; table= next)
1623 if (is_user_table(table))
1625 bool save_thread_specific_used= thd->thread_specific_used;
1626 my_thread_id save_pseudo_thread_id= thd->variables.pseudo_thread_id;
1628 thd->variables.pseudo_thread_id= tmpkeyval(thd, table);
1630 db.append(table->s->db.str);
1635 for (s_query.length(stub_len);
1636 table && is_user_table(table) &&
1637 tmpkeyval(thd, table) == thd->variables.pseudo_thread_id &&
1638 table->s->db.length == db.length() &&
1639 strcmp(table->s->db.str, db.ptr()) == 0;
1646 append_identifier(thd, &s_query, table->s->table_name.str,
1647 strlen(table->s->table_name.str));
1648 s_query.append(
',');
1650 close_temporary(table, 1, 1);
1653 const CHARSET_INFO *cs_save= thd->variables.character_set_client;
1654 thd->variables.character_set_client= system_charset_info;
1655 thd->thread_specific_used= TRUE;
1657 s_query.length() - 1 ,
1658 FALSE, TRUE, FALSE, 0);
1660 qinfo.db_len= db.length();
1661 thd->variables.character_set_client= cs_save;
1663 thd->get_stmt_da()->set_overwrite_status(
true);
1665 mysql_bin_log.
commit(thd,
true) ||
1680 sql_print_error(
"Failed to write the DROP statement for "
1681 "temporary tables to binary log");
1683 thd->get_stmt_da()->set_overwrite_status(
false);
1685 thd->variables.pseudo_thread_id= save_pseudo_thread_id;
1686 thd->thread_specific_used= save_thread_specific_used;
1691 close_temporary(table, 1, 1);
1694 if (!was_quote_show)
1695 thd->variables.option_bits&= ~OPTION_QUOTE_SHOW_CREATE;
1696 thd->temporary_tables=0;
1722 const char *db_name,
1723 const char *table_name)
1725 for (;
table; table= table->*link )
1727 if ((table->table == 0 || table->table->s->tmp_table == NO_TMP_TABLE) &&
1728 strcmp(table->db, db_name) == 0 &&
1729 strcmp(table->table_name, table_name) == 0)
1774 const char *d_name, *t_name, *t_alias;
1775 DBUG_ENTER(
"find_dup_table");
1776 DBUG_PRINT(
"enter", (
"table alias: %s", table->alias));
1791 DBUG_ASSERT(table->table->file->ht->db_type != DB_TYPE_MRG_MYISAM);
1794 if (table->table && table->table->s->tmp_table != NO_TMP_TABLE)
1796 table= table->find_underlying_table(table->table);
1804 t_name= table->table_name;
1805 t_alias= table->alias;
1807 DBUG_PRINT(
"info", (
"real table: %s.%s", d_name, t_name));
1814 if (! (res= find_table_in_global_list(table_list, d_name, t_name)))
1818 if (res->table && (res->table == table->table))
1824 if (lower_case_table_names ?
1825 my_strcasecmp(files_charset_info, t_alias, res->alias) :
1826 strcmp(t_alias, res->alias))
1834 if (res->select_lex &&
1835 !res->select_lex->exclude_from_table_unique_test &&
1836 !res->prelocking_placeholder)
1845 table_list= res->next_global;
1847 (
"found same copy of table or table which we should skip"));
1873 if (table->table && table->table->file->ht->db_type == DB_TYPE_MRG_MYISAM)
1878 for (child= table->next_global; child && child->parent_l == table;
1879 child= child->next_global)
1881 if ((dup= find_dup_table(thd, child, child->next_global, check_alias)))
1886 dup= find_dup_table(thd, table, table_list, check_alias);
1903 void update_non_unique_table_error(
TABLE_LIST *update,
1904 const char *operation,
1907 update= update->top_table();
1908 duplicate= duplicate->top_table();
1909 if (!update->view || !duplicate->view ||
1910 update->view == duplicate->view ||
1911 update->view_name.length != duplicate->view_name.length ||
1912 update->view_db.length != duplicate->view_db.length ||
1913 my_strcasecmp(table_alias_charset,
1914 update->view_name.str, duplicate->view_name.str) != 0 ||
1915 my_strcasecmp(table_alias_charset,
1916 update->view_db.str, duplicate->view_db.str) != 0)
1925 if (update->view == duplicate->view)
1926 my_error(!strncmp(operation,
"INSERT", 6) ?
1927 ER_NON_INSERTABLE_TABLE : ER_NON_UPDATABLE_TABLE, MYF(0),
1928 update->alias, operation);
1930 my_error(ER_VIEW_PREVENT_UPDATE, MYF(0),
1931 (duplicate->view ? duplicate->alias : update->alias),
1932 operation, update->alias);
1935 if (duplicate->view)
1937 my_error(ER_VIEW_PREVENT_UPDATE, MYF(0), duplicate->alias, operation,
1942 my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1955 char key[MAX_DBKEY_LENGTH];
1956 uint key_length= create_table_def_key(thd, key, db, table_name, 1);
1972 char key_suffix[TMP_TABLE_KEY_EXTRA];
1977 int4store(key_suffix, thd->server_id);
1978 int4store(key_suffix + 4, thd->variables.pseudo_thread_id);
1980 for (table= thd->temporary_tables; table; table= table->next)
1982 if ((table->s->table_cache_key.length == key_length +
1983 TMP_TABLE_KEY_EXTRA) &&
1984 !memcmp(table->s->table_cache_key.str, key, key_length) &&
1985 !memcmp(table->s->table_cache_key.str + key_length, key_suffix,
1986 TMP_TABLE_KEY_EXTRA))
2000 const char *table_key,
2001 uint table_key_length)
2003 for (
TABLE *table= thd->temporary_tables; table; table= table->next)
2005 if (table->s->table_cache_key.length == table_key_length &&
2006 !memcmp(table->s->table_cache_key.str, table_key, table_key_length))
2052 DBUG_ENTER(
"drop_temporary_table");
2053 DBUG_PRINT(
"tmptable", (
"closing table: '%s'.'%s'",
2054 table_list->db, table_list->table_name));
2056 if (!is_temporary_table(table_list))
2059 TABLE *table= table_list->table;
2062 if (table->query_id && table->query_id != thd->query_id)
2064 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2068 *is_trans= table->file->has_transactions();
2075 close_temporary_table(thd, table, 1, 1);
2076 table_list->table= NULL;
2084 void close_temporary_table(THD *thd,
TABLE *table,
2085 bool free_share,
bool delete_table)
2087 DBUG_ENTER(
"close_temporary_table");
2088 DBUG_PRINT(
"tmptable", (
"closing table: '%s'.'%s' 0x%lx alias: '%s'",
2089 table->s->db.str, table->s->table_name.str,
2090 (
long) table, table->alias));
2094 table->prev->next= table->next;
2095 if (table->prev->next)
2096 table->next->prev= table->prev;
2101 DBUG_ASSERT(table == thd->temporary_tables);
2107 thd->temporary_tables= table->next;
2108 if (thd->temporary_tables)
2109 table->next->prev= 0;
2111 if (thd->slave_thread)
2114 DBUG_ASSERT(slave_open_temp_tables || !thd->temporary_tables);
2115 modify_slave_open_temp_tables(thd, -1);
2117 close_temporary(table, free_share, delete_table);
2130 void close_temporary(
TABLE *table,
bool free_share,
bool delete_table)
2133 DBUG_ENTER(
"close_temporary");
2134 DBUG_PRINT(
"tmptable", (
"closing table: '%s'.'%s'",
2135 table->s->db.str, table->s->table_name.str));
2137 free_io_cache(table);
2143 free_table_share(table->s);
2158 bool rename_temporary_table(THD* thd,
TABLE *table,
const char *db,
2159 const char *table_name)
2164 DBUG_ENTER(
"rename_temporary_table");
2166 if (!(key=(
char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
2169 key_length= create_table_def_key(thd, key, db, table_name, 1);
2170 share->set_table_cache_key(key, key_length);
2193 enum ha_extra_function
function)
2195 DBUG_ENTER(
"wait_while_table_is_used");
2196 DBUG_PRINT(
"enter", (
"table: '%s' share: 0x%lx db_stat: %u version: %lu",
2197 table->s->table_name.str, (ulong) table->s,
2198 table->db_stat, table->s->version));
2200 if (thd->mdl_context.upgrade_shared_lock(
2201 table->mdl_ticket, MDL_EXCLUSIVE,
2202 thd->variables.lock_wait_timeout))
2206 table->s->db.str, table->s->table_name.str,
2209 (void) table->file->extra(
function);
2234 const char *table_name)
2236 DBUG_ENTER(
"drop_open_table");
2237 if (table->s->tmp_table)
2238 close_temporary_table(thd, table, 1, 1);
2241 DBUG_ASSERT(table == thd->open_tables);
2245 table->file->extra(HA_EXTRA_PREPARE_FOR_DROP);
2246 close_thread_table(thd, &thd->open_tables);
2251 quick_rm_table(thd, table_type, db_name, table_name, 0);
2278 char path[FN_REFLEN + 1];
2280 DBUG_ENTER(
"check_if_table_exists");
2284 DBUG_ASSERT(thd->mdl_context.
2285 is_lock_owner(MDL_key::TABLE, table->db,
2286 table->table_name, MDL_SHARED));
2289 share= get_cached_table_share(table->db, table->table_name);
2295 build_table_filename(path,
sizeof(path) - 1, table->db, table->table_name,
2298 if (!access(path, F_OK))
2304 my_printf_error(ER_OUT_OF_RESOURCES,
"Failed to open '%-.64s', error while "
2305 "unpacking from engine", MYF(0), table->table_name);
2323 : m_ot_ctx(ot_ctx_arg), m_is_active(FALSE)
2328 virtual bool handle_condition(THD *thd,
2330 const char* sqlstate,
2331 Sql_condition::enum_warning_level level,
2347 bool MDL_deadlock_handler::handle_condition(THD *,
2350 Sql_condition::enum_warning_level,
2355 if (! m_is_active && sql_errno == ER_LOCK_DEADLOCK)
2360 Open_table_context::OT_BACKOFF_AND_RETRY,
2409 if (flags & (MYSQL_OPEN_FORCE_SHARED_MDL |
2410 MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL))
2431 DBUG_ASSERT(!(flags & MYSQL_OPEN_FORCE_SHARED_MDL) ||
2432 !(flags & MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL));
2434 mdl_request_shared.
init(&mdl_request->
key,
2435 (flags & MYSQL_OPEN_FORCE_SHARED_MDL) ?
2436 MDL_SHARED : MDL_SHARED_HIGH_PRIO,
2438 mdl_request= &mdl_request_shared;
2441 if (flags & MYSQL_OPEN_FAIL_ON_MDL_CONFLICT)
2454 if (thd->mdl_context.try_acquire_lock(mdl_request))
2456 if (mdl_request->
ticket == NULL)
2458 my_error(ER_WARN_I_S_SKIPPED_TABLE, MYF(0),
2459 mdl_request->
key.db_name(), mdl_request->
key.name());
2490 thd->push_internal_handler(&mdl_deadlock_handler);
2491 bool result= thd->mdl_context.acquire_lock(mdl_request,
2492 ot_ctx->get_timeout());
2493 thd->pop_internal_handler();
2495 if (result && !ot_ctx->can_recover_from_failed_open())
2498 *mdl_ticket= mdl_request->
ticket;
2518 tdc_wait_for_old_version(THD *thd,
const char *db,
const char *table_name,
2519 ulong wait_timeout, uint deadlock_weight)
2525 if ((share= get_cached_table_share(db, table_name)) &&
2529 set_timespec(abstime, wait_timeout);
2575 char *alias= table_list->alias;
2576 uint flags= ot_ctx->get_flags();
2580 my_hash_value_type hash_value;
2582 DBUG_ENTER(
"open_table");
2590 DBUG_ASSERT(!table_list->table);
2596 if (!(flags & MYSQL_OPEN_IGNORE_KILLED) && thd->killed)
2605 if (table_list->mdl_request.
type >= MDL_SHARED_WRITE &&
2606 thd->tx_read_only &&
2607 !(flags & (MYSQL_LOCK_LOG_TABLE | MYSQL_OPEN_HAS_MDL_LOCK)))
2609 my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0));
2622 if (thd->locked_tables_mode &&
2623 ! (flags & MYSQL_OPEN_GET_NEW_TABLE))
2625 TABLE *best_table= 0;
2626 int best_distance= INT_MIN;
2627 for (table=thd->open_tables; table ; table=table->next)
2629 if (table->s->table_cache_key.length == key_length &&
2630 !memcmp(table->s->table_cache_key.str, key, key_length))
2632 if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
2633 table->query_id != thd->query_id &&
2634 (thd->locked_tables_mode == LTM_LOCK_TABLES ||
2635 table->query_id == 0))
2637 int distance= ((int) table->reginfo.lock_type -
2638 (
int) table_list->lock_type);
2652 if ((best_distance < 0 && distance > best_distance) ||
2653 (distance >= 0 && distance < best_distance))
2655 best_distance= distance;
2657 if (best_distance == 0)
2674 table->query_id= thd->query_id;
2675 DBUG_PRINT(
"info",(
"Using locked table"));
2687 if (thd->mdl_context.is_lock_owner(MDL_key::TABLE,
2689 table_list->table_name,
2692 char path[FN_REFLEN + 1];
2693 enum legacy_db_type not_used;
2694 build_table_filename(path,
sizeof(path) - 1,
2695 table_list->db, table_list->table_name, reg_ext, 0);
2703 if (dd_frm_type(thd, path, ¬_used) == FRMTYPE_VIEW)
2706 CHECK_METADATA_VERSION))
2708 DBUG_ASSERT(table_list->view != 0);
2720 if (thd->locked_tables_mode == LTM_PRELOCKED)
2721 my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->alias);
2723 my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
2729 if (! (flags & MYSQL_OPEN_HAS_MDL_LOCK))
2753 if (table_list->mdl_request.
type >= MDL_SHARED_WRITE &&
2754 ! (flags & (MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |
2755 MYSQL_OPEN_FORCE_SHARED_MDL |
2756 MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL |
2757 MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK)) &&
2758 ! ot_ctx->has_protection_against_grl())
2763 if (thd->global_read_lock.can_acquire_protection())
2766 protection_request.
init(MDL_key::GLOBAL,
"",
"", MDL_INTENTION_EXCLUSIVE,
2773 thd->push_internal_handler(&mdl_deadlock_handler);
2774 bool result= thd->mdl_context.acquire_lock(&protection_request,
2775 ot_ctx->get_timeout());
2776 thd->pop_internal_handler();
2784 if (open_table_get_mdl_lock(thd, ot_ctx, &table_list->mdl_request,
2785 flags, &mdl_ticket) ||
2788 DEBUG_SYNC(thd,
"before_open_table_wait_refresh");
2791 DEBUG_SYNC(thd,
"after_open_table_mdl_shared");
2799 mdl_ticket= table_list->mdl_request.
ticket;
2802 hash_value= my_calc_hash(&table_def_cache, (uchar*) key, key_length);
2805 if (table_list->
open_strategy == TABLE_LIST::OPEN_IF_EXISTS ||
2815 if (table_list->
open_strategy == TABLE_LIST::OPEN_FOR_CREATE &&
2816 ! (flags & (MYSQL_OPEN_FORCE_SHARED_MDL |
2817 MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL)))
2821 thd->push_internal_handler(&mdl_deadlock_handler);
2823 DEBUG_SYNC(thd,
"before_upgrading_lock_from_S_to_X_for_create_table");
2824 bool wait_result= thd->mdl_context.upgrade_shared_lock(
2825 table_list->mdl_request.
ticket,
2827 thd->variables.lock_wait_timeout);
2829 thd->pop_internal_handler();
2830 DEBUG_SYNC(thd,
"after_upgrading_lock_from_S_to_X_for_create_table");
2842 else if (table_list->
open_strategy == TABLE_LIST::OPEN_STUB)
2855 table= tc->
get_table(thd, hash_value, key, key_length, &share);
2861 if (!(flags & MYSQL_OPEN_IGNORE_FLUSH))
2885 if (thd->open_tables &&
2886 thd->open_tables->s->version != share->version)
2891 Open_table_context::OT_REOPEN_TABLES,
2899 DBUG_ASSERT(table->file != NULL);
2900 table->file->rebind_psi();
2902 thd->status_var.table_open_cache_hits++;
2933 if (!(share= get_table_share_with_discover(thd, table_list, key,
2934 key_length, OPEN_VIEW,
2945 if (error == 7 && !thd->is_error())
2964 if (table_list->parent_l)
2966 my_error(ER_WRONG_MRG_TABLE, MYF(0));
2974 if (check_and_update_table_version(thd, table_list, share))
2976 if (table_list->i_s_requested_object & OPEN_TABLE_ONLY)
2978 my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db,
2979 table_list->table_name);
2984 if (mysql_make_view(thd, share, table_list,
false))
2990 DBUG_ASSERT(table_list->view);
3003 if (table_list->i_s_requested_object & OPEN_VIEW_ONLY)
3005 my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db,
3006 table_list->table_name);
3011 if (!(flags & MYSQL_OPEN_IGNORE_FLUSH))
3030 thd->push_internal_handler(&mdl_deadlock_handler);
3031 wait_result= tdc_wait_for_old_version(thd, table_list->db,
3032 table_list->table_name,
3033 ot_ctx->get_timeout(),
3035 thd->pop_internal_handler();
3043 if (thd->open_tables && thd->open_tables->s->version != share->version)
3062 if (!(table= (
TABLE*) my_malloc(
sizeof(*table), MYF(MY_WME))))
3065 error= open_table_from_share(thd, share, alias,
3066 (uint) (HA_OPEN_KEYFILE |
3070 (READ_KEYINFO | COMPUTE_TYPES |
3072 thd->open_options, table, FALSE);
3081 else if (share->crashed)
3086 if (open_table_entry_fini(thd, share, table))
3105 thd->status_var.table_open_cache_misses++;
3108 table->mdl_ticket= mdl_ticket;
3110 table->next= thd->open_tables;
3111 thd->set_open_tables(table);
3113 table->reginfo.lock_type=TL_READ;
3116 table->set_created();
3121 DBUG_ASSERT(table->file->pushed_cond == NULL);
3122 table_list->updatable= 1;
3123 table_list->table=
table;
3125 #ifdef WITH_PARTITION_STORAGE_ENGINE
3126 if (table->part_info)
3129 if (table->part_info->set_partition_bitmaps(table_list))
3132 else if (table_list->partition_names)
3135 my_error(ER_PARTITION_CLAUSE_ON_NONPARTITIONED, MYF(0));
3140 table->init(thd, table_list);
3166 char key[MAX_DBKEY_LENGTH];
3167 uint key_length= create_table_def_key((THD*)NULL, key, db, table_name,
3170 for (
TABLE *table= list;
table ; table=table->next)
3172 if (table->s->table_cache_key.length == key_length &&
3173 !memcmp(table->s->table_cache_key.str, key, key_length))
3202 const char *table_name,
bool no_error)
3209 my_error(ER_TABLE_NOT_LOCKED, MYF(0), table_name);
3219 if (!thd->mdl_context.is_lock_owner(MDL_key::GLOBAL,
"",
"",
3220 MDL_INTENTION_EXCLUSIVE))
3223 my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), table_name);
3227 while (tab->mdl_ticket != NULL &&
3228 !tab->mdl_ticket->is_upgradable_or_exclusive() &&
3232 if (!tab && !no_error)
3233 my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), table_name);
3256 Locked_tables_list::init_locked_tables(THD *thd)
3258 DBUG_ASSERT(thd->locked_tables_mode == LTM_NONE);
3259 DBUG_ASSERT(m_locked_tables == NULL);
3260 DBUG_ASSERT(m_reopen_array == NULL);
3261 DBUG_ASSERT(m_locked_tables_count == 0);
3263 for (
TABLE *table= thd->open_tables; table;
3264 table= table->next, m_locked_tables_count++)
3266 TABLE_LIST *src_table_list= table->pos_in_table_list;
3268 size_t db_len= src_table_list->db_length;
3269 size_t table_name_len= src_table_list->table_name_length;
3270 size_t alias_len= strlen(src_table_list->alias);
3273 if (! multi_alloc_root(&m_locked_tables_root,
3274 &dst_table_list,
sizeof(*dst_table_list),
3276 &table_name, table_name_len + 1,
3277 &alias, alias_len + 1,
3280 unlock_locked_tables(0);
3284 memcpy(db, src_table_list->db, db_len + 1);
3285 memcpy(table_name, src_table_list->table_name, table_name_len + 1);
3286 memcpy(alias, src_table_list->alias, alias_len + 1);
3294 dst_table_list->
init_one_table(db, db_len, table_name, table_name_len,
3296 src_table_list->table->reginfo.lock_type);
3297 dst_table_list->table=
table;
3298 dst_table_list->mdl_request.
ticket= src_table_list->mdl_request.
ticket;
3301 *(dst_table_list->prev_global= m_locked_tables_last)= dst_table_list;
3302 m_locked_tables_last= &dst_table_list->next_global;
3303 table->pos_in_locked_tables= dst_table_list;
3305 if (m_locked_tables_count)
3312 m_reopen_array= (
TABLE**)alloc_root(&m_locked_tables_root,
3314 (m_locked_tables_count+1));
3315 if (m_reopen_array == NULL)
3317 unlock_locked_tables(0);
3321 thd->enter_locked_tables_mode(LTM_LOCK_TABLES);
3336 Locked_tables_list::unlock_locked_tables(THD *thd)
3341 DBUG_ASSERT(!thd->in_sub_stmt &&
3342 !(thd->state_flags & Open_tables_state::BACKUPS_AVAIL));
3349 if (thd->locked_tables_mode != LTM_LOCK_TABLES)
3352 for (
TABLE_LIST *table_list= m_locked_tables;
3353 table_list; table_list= table_list->next_global)
3359 table_list->table->pos_in_locked_tables= NULL;
3361 thd->leave_locked_tables_mode();
3363 DBUG_ASSERT(thd->transaction.stmt.is_empty());
3364 close_thread_tables(thd);
3374 free_root(&m_locked_tables_root, MYF(0));
3375 m_locked_tables= NULL;
3376 m_locked_tables_last= &m_locked_tables;
3377 m_reopen_array= NULL;
3378 m_locked_tables_count= 0;
3403 void Locked_tables_list::unlink_from_list(THD *thd,
3405 bool remove_from_locked_tables)
3411 if (thd->locked_tables_mode != LTM_LOCK_TABLES)
3418 DBUG_ASSERT(table_list->table->pos_in_locked_tables == table_list);
3421 table_list->table->pos_in_locked_tables= NULL;
3424 table_list->table= NULL;
3431 if (remove_from_locked_tables)
3433 *table_list->prev_global= table_list->next_global;
3434 if (table_list->next_global == NULL)
3435 m_locked_tables_last= table_list->prev_global;
3437 table_list->next_global->prev_global= table_list->prev_global;
3450 void Locked_tables_list::
3451 unlink_all_closed_tables(THD *thd,
MYSQL_LOCK *lock,
size_t reopen_count)
3455 mysql_unlock_tables(thd, lock);
3463 while (reopen_count--)
3472 DBUG_ASSERT(thd->open_tables == m_reopen_array[reopen_count]);
3474 thd->open_tables->pos_in_locked_tables->table= NULL;
3476 close_thread_table(thd, &thd->open_tables);
3480 for (
TABLE_LIST *table_list= m_locked_tables; table_list; table_list=
3481 table_list->next_global)
3483 if (table_list->table == NULL)
3486 *table_list->prev_global= table_list->next_global;
3487 if (table_list->next_global == NULL)
3488 m_locked_tables_last= table_list->prev_global;
3490 table_list->next_global->prev_global= table_list->prev_global;
3509 Locked_tables_list::reopen_tables(THD *thd)
3512 size_t reopen_count= 0;
3516 for (
TABLE_LIST *table_list= m_locked_tables;
3517 table_list; table_list= table_list->next_global)
3519 if (table_list->table)
3525 unlink_all_closed_tables(thd, 0, reopen_count);
3528 table_list->table->pos_in_locked_tables= table_list;
3530 table_list->table->reginfo.lock_type= table_list->lock_type;
3532 DBUG_ASSERT(reopen_count < m_locked_tables_count);
3533 m_reopen_array[reopen_count++]= table_list->table;
3537 thd->in_lock_tables= 1;
3551 thd->in_lock_tables= 0;
3552 if (lock == NULL || (merged_lock=
3553 mysql_lock_merge(thd->lock, lock)) == NULL)
3555 unlink_all_closed_tables(thd, lock, reopen_count);
3557 my_error(ER_LOCK_DEADLOCK, MYF(0));
3560 thd->lock= merged_lock;
3599 DBUG_ENTER(
"assign_new_table_id");
3602 DBUG_ASSERT(share != NULL);
3605 DBUG_EXECUTE_IF(
"dbug_table_map_id_500", last_table_id= 500;);
3606 DBUG_EXECUTE_IF(
"dbug_table_map_id_4B_UINT_MAX+501",
3607 last_table_id= 501ULL + UINT_MAX;);
3608 DBUG_EXECUTE_IF(
"dbug_table_map_id_6B_UINT_MAX",
3609 last_table_id= (~0ULL >> 16););
3611 share->table_map_id= last_table_id++;
3612 DBUG_PRINT(
"info", (
"table_id=%llu", share->table_map_id.id()));
3619 static bool inject_reprepare(THD *thd)
3623 if (reprepare_observer && !thd->stmt_arena->is_reprepared)
3666 check_and_update_table_version(THD *thd,
3673 if (reprepare_observer &&
3681 DBUG_ASSERT(thd->is_error());
3688 DBUG_EXECUTE_IF(
"reprepare_each_statement",
return inject_reprepare(thd););
3723 ulong spc_version= sp_cache_version();
3733 (version != spc_version && !sp->
is_invoked()))
3737 if (reprepare_observer &&
3745 DBUG_ASSERT(thd->is_error());
3772 const char *cache_key, uint cache_key_length, uint flags)
3775 my_hash_value_type hash_value;
3778 hash_value= my_calc_hash(&table_def_cache, (uchar*) cache_key,
3782 if (!(share= get_table_share(thd, table_list, cache_key,
3788 if ((flags & CHECK_METADATA_VERSION))
3799 if (check_and_update_table_version(thd, table_list, share))
3806 if (share->is_view &&
3807 !mysql_make_view(thd, share, table_list, (flags & OPEN_VIEW_NO_PARSE)))
3814 my_error(ER_WRONG_OBJECT, MYF(0), share->db.str, share->table_name.str,
"VIEW");
3827 static bool open_table_entry_fini(THD *thd,
TABLE_SHARE *share,
TABLE *entry)
3830 share->table_name.str, entry, 0))
3837 if (unlikely(entry->file->implicit_emptied))
3839 entry->file->implicit_emptied= 0;
3840 if (mysql_bin_log.is_open())
3844 error= temp_buf.append(
"DELETE FROM ");
3845 append_identifier(thd, &temp_buf, share->db.str, strlen(share->db.str));
3846 error= temp_buf.append(
".");
3847 append_identifier(thd, &temp_buf, share->table_name.str,
3848 strlen(share->table_name.str));
3849 int errcode= query_error_code(thd, TRUE);
3850 if (thd->binlog_query(THD::STMT_QUERY_TYPE,
3851 temp_buf.c_ptr_safe(), temp_buf.length(),
3852 FALSE, TRUE, FALSE, errcode))
3861 sql_print_error(
"When opening HEAP table, could not allocate memory "
3862 "to write 'DELETE FROM `%s`.`%s`' to the binary log",
3863 share->db.str, share->table_name.str);
3864 delete entry->triggers;
3877 static bool auto_repair_table(THD *thd,
TABLE_LIST *table_list)
3879 const char *cache_key;
3880 uint cache_key_length;
3885 my_hash_value_type hash_value;
3891 hash_value= my_calc_hash(&table_def_cache, (uchar*) cache_key,
3895 if (!(share= get_table_share(thd, table_list, cache_key,
3897 OPEN_VIEW, ¬_used,
3907 if (!(entry= (
TABLE*)my_malloc(
sizeof(
TABLE), MYF(MY_WME))))
3914 if (open_table_from_share(thd, share, table_list->alias,
3915 (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3918 READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD,
3919 ha_open_options | HA_OPEN_FOR_REPAIR,
3920 entry, FALSE) || ! entry->file ||
3925 my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str);
3926 sql_print_error(
"Couldn't repair table: %s.%s", share->db.str,
3927 share->table_name.str);
3943 table_list->db, table_list->table_name,
3957 m_failed_table(NULL),
3958 m_start_of_statement_svp(thd->mdl_context.mdl_savepoint()),
3959 m_timeout(flags & MYSQL_LOCK_IGNORE_TIMEOUT ?
3960 LONG_TIMEOUT : thd->variables.lock_wait_timeout),
3962 m_action(OT_NO_ACTION),
3963 m_has_locks(thd->mdl_context.has_locks()),
3964 m_has_protection_against_grl(FALSE)
4031 if (action_arg != OT_REOPEN_TABLES && m_has_locks)
4033 my_error(ER_LOCK_DEADLOCK, MYF(0));
4034 mark_transaction_to_rollback(m_thd,
true);
4043 DBUG_ASSERT(action_arg == OT_DISCOVER || action_arg == OT_REPAIR);
4045 if (m_failed_table == NULL)
4049 table->table_name_length,
4050 table->alias, TL_WRITE);
4051 m_failed_table->mdl_request.
set_type(MDL_EXCLUSIVE);
4053 m_action= action_arg;
4076 case OT_BACKOFF_AND_RETRY:
4078 case OT_REOPEN_TABLES:
4087 m_failed_table->table_name, FALSE);
4089 m_failed_table->table_name);
4091 m_thd->get_stmt_da()->clear_warning_info(m_thd->query_id);
4092 m_thd->clear_error();
4093 m_thd->mdl_context.release_transactional_locks();
4103 m_failed_table->table_name, FALSE);
4105 result= auto_repair_table(m_thd, m_failed_table);
4106 m_thd->mdl_context.release_transactional_locks();
4117 m_failed_table= NULL;
4123 m_has_protection_against_grl= FALSE;
4125 m_action= OT_NO_ACTION;
4170 thr_lock_type read_lock_type_for_table(THD *thd,
4171 Query_tables_list *prelocking_ctx,
4173 bool routine_modifies_data)
4182 bool log_on= mysql_bin_log.is_open() && thd->variables.sql_log_bin;
4183 ulong binlog_format= thd->variables.binlog_format;
4184 if ((log_on == FALSE) || (binlog_format == BINLOG_FORMAT_ROW) ||
4186 (table_list->table->s->
table_category == TABLE_CATEGORY_RPL_INFO) ||
4187 (table_list->table->s->
table_category == TABLE_CATEGORY_PERFORMANCE) ||
4188 !(is_update_query(prelocking_ctx->sql_command) ||
4189 (routine_modifies_data && table_list->prelocking_placeholder) ||
4190 (thd->locked_tables_mode > LTM_LOCK_TABLES)))
4193 return TL_READ_NO_INSERT;
4223 open_and_process_routine(THD *thd, Query_tables_list *prelocking_ctx,
4226 bool has_prelocking_list,
4228 bool *need_prelocking,
bool *routine_modifies_data)
4231 *routine_modifies_data=
false;
4232 DBUG_ENTER(
"open_and_process_routine");
4236 case MDL_key::FUNCTION:
4237 case MDL_key::PROCEDURE:
4248 mdl_type != MDL_key::PROCEDURE)
4267 thd->push_internal_handler(&mdl_deadlock_handler);
4268 bool result= thd->mdl_context.acquire_lock(&rt->
mdl_request,
4269 ot_ctx->get_timeout());
4270 thd->pop_internal_handler();
4275 DEBUG_SYNC(thd,
"after_shared_lock_pname");
4278 if (sp_cache_routine(thd, rt, has_prelocking_list, &sp))
4282 if (check_and_update_routine_version(thd, rt, sp))
4290 if (!has_prelocking_list)
4291 prelocking_strategy->handle_routine(thd, prelocking_ctx, rt, sp,
4303 if (sp_cache_routine(thd, rt, FALSE, &sp))
4308 case MDL_key::TRIGGER:
4373 open_and_process_table(THD *thd, LEX *lex,
TABLE_LIST *tables,
4374 uint *counter, uint flags,
4376 bool has_prelocking_list,
4380 bool safe_to_ignore_table= FALSE;
4381 DBUG_ENTER(
"open_and_process_table");
4382 DEBUG_SYNC(thd,
"open_and_process_table");
4400 tables->db= tables->view_db.str;
4401 tables->db_length= tables->view_db.length;
4402 tables->table_name= tables->view_name.str;
4403 tables->table_name_length= tables->view_name.length;
4411 if (tables->schema_table)
4425 if (!open_table_get_mdl_lock(thd, ot_ctx, &tables->mdl_request,
4426 flags, &mdl_ticket) &&
4428 goto process_view_routines;
4431 else if (!mysql_schema_table(thd, lex, tables) &&
4432 !check_and_update_table_version(thd, tables, tables->table->s))
4439 DBUG_PRINT(
"tcache", (
"opening table: '%s'.'%s' item: %p",
4440 tables->db, tables->table_name, tables));
4452 DBUG_ASSERT(is_temporary_table(tables));
4454 else if (tables->
open_type == OT_TEMPORARY_ONLY)
4465 DBUG_ASSERT(!tables->prelocking_placeholder);
4466 DBUG_ASSERT(!tables->parent_l);
4468 else if (tables->prelocking_placeholder)
4477 thd->push_internal_handler(&no_such_table_handler);
4504 if (!error && !tables->table)
4507 thd->pop_internal_handler();
4510 else if (tables->parent_l && (thd->open_options & HA_OPEN_FOR_REPAIR))
4519 thd->push_internal_handler(&repair_mrg_table_handler);
4522 if (!error && !tables->table)
4525 thd->pop_internal_handler();
4530 if (tables->parent_l)
4540 if (!error && !tables->table)
4546 if (! ot_ctx->can_recover_from_failed_open() && safe_to_ignore_table)
4548 DBUG_PRINT(
"info", (
"open_table: ignoring table '%s'.'%s'",
4549 tables->db, tables->alias));
4562 if (!tables->table && tables->view)
4577 if (lex->query_tables_own_last == &(tables->next_global) &&
4578 tables->view->query_tables)
4579 lex->query_tables_own_last= tables->view->query_tables_last;
4584 my_hash_free(&tables->view->sroutines);
4585 goto process_view_routines;
4603 if (thd->locked_tables_mode <= LTM_LOCK_TABLES &&
4604 ! has_prelocking_list &&
4605 tables->lock_type >= TL_WRITE_ALLOW_WRITE)
4607 bool need_prelocking= FALSE;
4608 TABLE_LIST **save_query_tables_last= lex->query_tables_last;
4618 error= prelocking_strategy->handle_table(thd, lex, tables,
4621 if (need_prelocking && ! lex->requires_prelocking())
4622 lex->mark_as_requiring_prelocking(save_query_tables_last);
4629 tables->table->grant= tables->grant;
4632 error= check_and_update_table_version(thd, tables, tables->table->s);
4642 DBUG_ASSERT(tables->table->pos_in_table_list == tables);
4644 if (tables->table->file->extra(HA_EXTRA_ADD_CHILDREN_LIST))
4650 process_view_routines:
4656 thd->locked_tables_mode <= LTM_LOCK_TABLES &&
4657 ! has_prelocking_list)
4659 bool need_prelocking= FALSE;
4660 TABLE_LIST **save_query_tables_last= lex->query_tables_last;
4662 error= prelocking_strategy->handle_view(thd, lex, tables,
4665 if (need_prelocking && ! lex->requires_prelocking())
4666 lex->mark_as_requiring_prelocking(save_query_tables_last);
4676 extern "C" uchar *schema_set_get_key(
const uchar *record,
size_t *length,
4677 my_bool not_used __attribute__((unused)))
4680 *length= table->db_length;
4681 return (uchar*) table->db;
4707 ulong lock_wait_timeout, uint flags)
4714 DBUG_ASSERT(!thd->locked_tables_mode);
4716 for (table= tables_start; table && table != tables_end;
4717 table= table->next_global)
4719 if ((table->mdl_request.
type < MDL_SHARED_UPGRADABLE &&
4721 table->
open_type == OT_TEMPORARY_ONLY ||
4722 (table->
open_type == OT_TEMPORARY_OR_BASE && is_temporary_table(table)))
4728 if (thd->tx_read_only)
4730 my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0));
4734 if (! (flags & MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK) && schema_set.
insert(table))
4737 mdl_requests.push_front(&table->mdl_request);
4740 if (! (flags & MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK) &&
4741 ! mdl_requests.is_empty())
4748 while ((table= it++))
4751 if (schema_request == NULL)
4753 schema_request->
init(MDL_key::SCHEMA, table->db,
"",
4754 MDL_INTENTION_EXCLUSIVE,
4756 mdl_requests.push_front(schema_request);
4764 if (thd->global_read_lock.can_acquire_protection())
4766 global_request.
init(MDL_key::GLOBAL,
"",
"", MDL_INTENTION_EXCLUSIVE,
4768 mdl_requests.push_front(&global_request);
4771 if (thd->mdl_context.acquire_locks(&mdl_requests, lock_wait_timeout))
4795 open_tables_check_upgradable_mdl(THD *thd,
TABLE_LIST *tables_start,
4800 DBUG_ASSERT(thd->locked_tables_mode);
4802 for (table= tables_start; table && table != tables_end;
4803 table= table->next_global)
4805 if (table->mdl_request.
type < MDL_SHARED_UPGRADABLE ||
4806 table->
open_type == OT_TEMPORARY_ONLY ||
4807 (table->
open_type == OT_TEMPORARY_OR_BASE && is_temporary_table(table)))
4882 bool some_routine_modifies_data= FALSE;
4883 bool has_prelocking_list;
4884 DBUG_ENTER(
"open_tables");
4887 enum xa_states xa_state= thd->transaction.xid_state.xa_state;
4888 if (*start && (xa_state == XA_IDLE || xa_state == XA_PREPARED))
4890 my_error(ER_XAER_RMFAIL, MYF(0), xa_state_names[xa_state]);
4894 thd->current_tablenr= 0;
4904 if (thd->handler_tables_hash.records)
4905 mysql_ha_flush(thd);
4907 has_prelocking_list= thd->lex->requires_prelocking();
4908 table_to_open= start;
4911 THD_STAGE_INFO(thd, stage_opening_tables);
4927 if (! (flags & (MYSQL_OPEN_HAS_MDL_LOCK |
4928 MYSQL_OPEN_FORCE_SHARED_MDL |
4929 MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL)))
4931 if (thd->locked_tables_mode)
4937 if (open_tables_check_upgradable_mdl(thd, *start,
4938 thd->lex->first_not_own_table(),
4949 ot_ctx.get_timeout(),
flags))
4954 for (table= *start; table && table != thd->lex->first_not_own_table();
4955 table= table->next_global)
4957 if (table->mdl_request.
type >= MDL_SHARED_UPGRADABLE ||
4959 table->mdl_request.
ticket= NULL;
4968 while (*table_to_open ||
4969 (thd->locked_tables_mode <= LTM_LOCK_TABLES &&
4976 for (tables= *table_to_open; tables;
4977 table_to_open= &tables->next_global, tables= tables->next_global)
4979 error= open_and_process_table(thd, thd->lex, tables, counter,
4980 flags, prelocking_strategy,
4981 has_prelocking_list, &ot_ctx);
4985 if (ot_ctx.can_recover_from_failed_open())
5022 DEBUG_SYNC(thd,
"open_tables_after_open_and_process_table");
5033 if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
5035 bool routine_modifies_data;
5046 sroutine_to_open= &rt->
next, rt= rt->
next)
5048 bool need_prelocking=
false;
5049 TABLE_LIST **save_query_tables_last= thd->lex->query_tables_last;
5051 error= open_and_process_routine(thd, thd->lex, rt, prelocking_strategy,
5052 has_prelocking_list, &ot_ctx,
5054 &routine_modifies_data);
5057 if (need_prelocking && ! thd->lex->requires_prelocking())
5058 thd->lex->mark_as_requiring_prelocking(save_query_tables_last);
5060 if (need_prelocking && ! *start)
5061 *start= thd->lex->query_tables;
5065 if (ot_ctx.can_recover_from_failed_open())
5088 some_routine_modifies_data|= routine_modifies_data;
5103 for (tables= *start; tables; tables= tables->next_global)
5105 TABLE *tbl= tables->table;
5113 if (tbl && tbl->file->ht->db_type == DB_TYPE_MRG_MYISAM)
5116 DBUG_ASSERT(tbl->pos_in_table_list == tables);
5117 if (tbl->file->extra(HA_EXTRA_ATTACH_CHILDREN))
5125 if (tbl && tables->lock_type != TL_UNLOCK &&
5126 !thd->locked_tables_mode)
5128 if (tables->lock_type == TL_WRITE_DEFAULT)
5129 tbl->reginfo.lock_type= thd->update_lock_default;
5130 else if (tables->lock_type == TL_READ_DEFAULT)
5131 tbl->reginfo.lock_type=
5132 read_lock_type_for_table(thd, thd->lex, tables,
5133 some_routine_modifies_data);
5135 tbl->reginfo.lock_type= tables->lock_type;
5141 if (error && *table_to_open)
5143 (*table_to_open)->table= NULL;
5145 DBUG_PRINT(
"open_tables", (
"returning: %d", (
int) error));
5188 *need_prelocking= TRUE;
5189 sp_update_stmt_used_routines(thd, prelocking_ctx, &sp->
m_sroutines,
5192 &prelocking_ctx->query_tables_last,
5195 sp->propagate_attributes(prelocking_ctx);
5224 TABLE_LIST *table_list,
bool *need_prelocking)
5227 DBUG_ASSERT(table_list->lock_type >= TL_WRITE_ALLOW_WRITE);
5231 if (table_list->table->triggers)
5233 *need_prelocking= TRUE;
5235 if (table_list->table->triggers->
5236 add_tables_and_routines_for_triggers(thd, prelocking_ctx, table_list))
5262 TABLE_LIST *table_list,
bool *need_prelocking)
5264 if (table_list->view->uses_stored_routines())
5266 *need_prelocking= TRUE;
5268 sp_update_stmt_used_routines(thd, prelocking_ctx,
5269 &table_list->view->sroutines_list,
5270 table_list->top_table());
5291 bool Lock_tables_prelocking_strategy::
5292 handle_table(THD *thd, Query_tables_list *prelocking_ctx,
5293 TABLE_LIST *table_list,
bool *need_prelocking)
5300 DBUG_ASSERT(table_list->lock_type >= TL_WRITE_ALLOW_WRITE);
5343 TABLE_LIST *table_list,
bool *need_prelocking)
5358 TABLE_LIST *table_list,
bool *need_prelocking)
5375 static bool check_lock_and_start_stmt(THD *thd,
5376 Query_tables_list *prelocking_ctx,
5380 thr_lock_type lock_type;
5381 DBUG_ENTER(
"check_lock_and_start_stmt");
5394 if (table_list->lock_type == TL_WRITE_DEFAULT)
5395 lock_type= thd->update_lock_default;
5396 else if (table_list->lock_type == TL_READ_DEFAULT)
5397 lock_type= read_lock_type_for_table(thd, prelocking_ctx, table_list,
true);
5399 lock_type= table_list->lock_type;
5401 if ((
int) lock_type > (
int) TL_WRITE_ALLOW_WRITE &&
5402 (
int) table_list->table->reginfo.lock_type <= (
int) TL_WRITE_ALLOW_WRITE)
5404 my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), table_list->alias);
5407 if ((error= table_list->table->file->start_stmt(thd, lock_type)))
5409 table_list->table->file->
print_error(error, MYF(0));
5451 thr_lock_type lock_type, uint flags,
5455 DBUG_ENTER(
"open_n_lock_single_table");
5458 save_next_global= table_l->next_global;
5460 table_l->next_global= NULL;
5463 table_l->lock_type= lock_type;
5465 table_l->required_type= FRMTYPE_TABLE;
5469 prelocking_strategy))
5470 table_l->table= NULL;
5473 table_l->next_global= save_next_global;
5475 DBUG_RETURN(table_l->table);
5503 TABLE *open_ltable(THD *thd,
TABLE_LIST *table_list, thr_lock_type lock_type,
5509 DBUG_ENTER(
"open_ltable");
5512 DBUG_ASSERT(thd->locked_tables_mode < LTM_PRELOCKED);
5514 THD_STAGE_INFO(thd, stage_opening_tables);
5515 thd->current_tablenr= 0;
5517 table_list->required_type= FRMTYPE_TABLE;
5520 DBUG_ASSERT(table_list->mdl_request.
type < MDL_SHARED_UPGRADABLE);
5522 while ((error=
open_table(thd, table_list, &ot_ctx)) &&
5523 ot_ctx.can_recover_from_failed_open())
5531 table_list->mdl_request.
ticket= 0;
5542 DBUG_ASSERT(table_list->table);
5543 table= table_list->table;
5544 if (table->file->ht->db_type == DB_TYPE_MRG_MYISAM)
5548 my_error(ER_WRONG_OBJECT, MYF(0), table->s->db.str,
5549 table->s->table_name.str,
"BASE TABLE");
5555 table_list->lock_type= lock_type;
5556 table->grant= table_list->grant;
5557 if (thd->locked_tables_mode)
5559 if (check_lock_and_start_stmt(thd, thd->lex, table_list))
5564 DBUG_ASSERT(thd->lock == 0);
5565 if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
5579 if (!thd->in_sub_stmt)
5580 trans_rollback_stmt(thd);
5581 close_thread_tables(thd);
5608 bool derived, uint flags,
5612 MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
5613 DBUG_ENTER(
"open_and_lock_tables");
5614 DBUG_PRINT(
"enter", (
"derived handling: %d", derived));
5616 if (
open_tables(thd, &tables, &counter, flags, prelocking_strategy))
5619 DBUG_EXECUTE_IF(
"sleep_open_and_lock_after_open", {
5620 const char *old_proc_info= thd->proc_info;
5621 thd->proc_info=
"DBUG sleep";
5623 thd->proc_info= old_proc_info;});
5629 (mysql_handle_derived(thd->lex, &mysql_derived_prepare)))
5634 if (! thd->in_sub_stmt)
5635 trans_rollback_stmt(thd);
5636 close_thread_tables(thd);
5638 thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
5666 MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
5667 DBUG_ENTER(
"open_normal_and_derived_tables");
5668 if (
open_tables(thd, &tables, &thd->lex->table_count, flags,
5669 &prelocking_strategy) ||
5670 mysql_handle_derived(thd->lex, &mysql_derived_prepare))
5681 DBUG_ASSERT(thd->transaction.stmt.is_empty() ||
5682 (thd->state_flags & Open_tables_state::BACKUPS_AVAIL) ||
5684 close_thread_tables(thd);
5686 thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
5705 static void mark_real_tables_as_free_for_reuse(
TABLE_LIST *table_list)
5708 for (table= table_list;
table; table= table->next_global)
5709 if (!table->placeholder())
5711 table->table->query_id= 0;
5713 for (table= table_list;
table; table= table->next_global)
5714 if (!table->placeholder())
5722 table->table->file->extra(HA_EXTRA_DETACH_CHILDREN);
5752 DBUG_ENTER(
"lock_tables");
5757 DBUG_ASSERT(thd->locked_tables_mode <= LTM_LOCK_TABLES ||
5758 !thd->lex->requires_prelocking());
5764 DBUG_ASSERT(thd->lex->lock_tables_state == Query_tables_list::LTS_NOT_LOCKED);
5766 if (!tables && !thd->lex->requires_prelocking())
5774 thd->lex->lock_tables_state= Query_tables_list::LTS_LOCKED;
5775 DBUG_RETURN(thd->decide_logging_format(tables));
5788 if (! thd->locked_tables_mode)
5790 DBUG_ASSERT(thd->lock == 0);
5791 TABLE **start,**ptr;
5793 if (!(ptr=start=(
TABLE**) thd->alloc(
sizeof(
TABLE*)*count)))
5795 for (table= tables;
table; table= table->next_global)
5797 if (!table->placeholder())
5798 *(ptr++)= table->table;
5807 if (thd->variables.binlog_format != BINLOG_FORMAT_ROW && tables &&
5808 has_write_table_with_auto_increment_and_select(tables))
5809 thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_WRITE_AUTOINC_SELECT);
5811 if (thd->variables.binlog_format != BINLOG_FORMAT_ROW && tables)
5813 if (has_write_table_auto_increment_not_first_in_pk(tables))
5814 thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_AUTOINC_NOT_FIRST);
5821 uint unique_keys= 0;
5822 for (
TABLE_LIST *query_table= tables; query_table && unique_keys <= 1;
5823 query_table= query_table->next_global)
5824 if(query_table->table)
5826 uint keys= query_table->table->s->keys,
i= 0;
5828 for (
KEY* keyinfo= query_table->table->s->key_info;
5829 i < keys && unique_keys <= 1;
i++, keyinfo++)
5831 if (keyinfo->flags & HA_NOSAME)
5834 if (!query_table->placeholder() &&
5835 query_table->lock_type >= TL_WRITE_ALLOW_WRITE &&
5836 unique_keys > 1 && thd->lex->sql_command == SQLCOM_INSERT &&
5838 thd->get_command() != COM_DELAYED_INSERT &&
5839 thd->lex->duplicates == DUP_UPDATE)
5840 thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_INSERT_TWO_KEYS);
5845 if (thd->lex->requires_prelocking())
5854 if (thd->variables.binlog_format != BINLOG_FORMAT_ROW && tables &&
5855 has_write_table_with_auto_increment(thd->lex->first_not_own_table()))
5856 thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_AUTOINC_COLUMNS);
5859 DEBUG_SYNC(thd,
"before_lock_tables_takes_lock");
5865 DEBUG_SYNC(thd,
"after_lock_tables_takes_lock");
5867 if (thd->lex->requires_prelocking() &&
5868 thd->lex->sql_command != SQLCOM_LOCK_TABLES)
5870 TABLE_LIST *first_not_own= thd->lex->first_not_own_table();
5883 table && table != first_not_own;
5884 table= table->next_global)
5886 if (!table->placeholder())
5888 table->table->query_id= thd->query_id;
5889 if (check_lock_and_start_stmt(thd, thd->lex, table))
5891 mysql_unlock_tables(thd, thd->lock);
5901 mark_real_tables_as_free_for_reuse(first_not_own);
5902 DBUG_PRINT(
"info",(
"locked_tables_mode= LTM_PRELOCKED"));
5903 thd->enter_locked_tables_mode(LTM_PRELOCKED);
5908 TABLE_LIST *first_not_own= thd->lex->first_not_own_table();
5918 table && table != first_not_own;
5919 table= table->next_global)
5921 if (table->placeholder())
5928 if (thd->locked_tables_mode >= LTM_PRELOCKED &&
5929 table->lock_type >= TL_WRITE_ALLOW_WRITE)
5931 for (
TABLE* opentab= thd->open_tables; opentab; opentab= opentab->next)
5933 if (table->table->s == opentab->s && opentab->query_id &&
5934 table->table->query_id != opentab->query_id)
5936 my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
5937 table->table->s->table_name.str);
5943 if (check_lock_and_start_stmt(thd, thd->lex, table))
5953 if (thd->lex->requires_prelocking())
5955 mark_real_tables_as_free_for_reuse(first_not_own);
5957 (
"thd->locked_tables_mode= LTM_PRELOCKED_UNDER_LOCK_TABLES"));
5958 thd->locked_tables_mode= LTM_PRELOCKED_UNDER_LOCK_TABLES;
5967 thd->lex->lock_tables_state= Query_tables_list::LTS_LOCKED;
5969 DBUG_RETURN(thd->decide_logging_format(tables));
5992 TABLE_LIST *first_not_own_table= thd->lex->first_not_own_table();
5999 if (first_not_own_table == *tables)
6001 thd->lex->chop_off_not_own_tables();
6007 sp_remove_not_own_routines(thd->lex);
6008 for (tmp= *tables; tmp; tmp= tmp->next_global)
6011 tmp->mdl_request.
ticket= NULL;
6013 tmp->cleanup_items();
6021 DBUG_ASSERT(thd->transaction.stmt.is_empty() ||
6022 (thd->state_flags & Open_tables_state::BACKUPS_AVAIL));
6023 close_thread_tables(thd);
6024 thd->mdl_context.rollback_to_savepoint(start_of_statement_svp);
6053 const char *table_name,
6054 bool add_to_temporary_tables_list,
6055 bool open_in_engine)
6059 char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
6061 DBUG_ENTER(
"open_table_uncached");
6063 (
"table: '%s'.'%s' path: '%s' server_id: %u "
6064 "pseudo_thread_id: %lu",
6065 db, table_name, path,
6066 (uint) thd->server_id, (ulong) thd->variables.pseudo_thread_id));
6069 key_length= create_table_def_key(thd, cache_key, db, table_name, 1);
6071 if (!(tmp_table= (
TABLE*) my_malloc(
sizeof(*tmp_table) +
sizeof(*share) +
6072 strlen(path)+1 + key_length,
6078 DBUG_ASSERT(!my_hash_search(&table_def_cache, (uchar*) cache_key,
6084 tmp_path= (
char*) (share+1);
6085 saved_cache_key= strmov(tmp_path, path)+1;
6086 memcpy(saved_cache_key, cache_key, key_length);
6088 init_tmp_table_share(thd, share, saved_cache_key, key_length,
6089 strend(saved_cache_key)+1, tmp_path);
6091 if (open_table_def(thd, share, 0))
6094 free_table_share(share);
6099 #ifdef HAVE_PSI_TABLE_INTERFACE
6100 share->
m_psi= PSI_TABLE_CALL(get_table_share)(
true, share);
6105 if (open_table_from_share(thd, share, table_name,
6107 (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
6109 READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD,
6116 open_in_engine ?
false :
true))
6119 free_table_share(share);
6124 tmp_table->reginfo.lock_type= TL_WRITE;
6125 share->tmp_table= (tmp_table->file->has_transactions() ?
6126 TRANSACTIONAL_TMP_TABLE : NON_TRANSACTIONAL_TMP_TABLE);
6128 if (add_to_temporary_tables_list)
6131 tmp_table->next= thd->temporary_tables;
6132 if (tmp_table->next)
6133 tmp_table->next->prev= tmp_table;
6134 thd->temporary_tables= tmp_table;
6135 thd->temporary_tables->prev= 0;
6136 if (thd->slave_thread)
6137 modify_slave_open_temp_tables(thd, 1);
6139 tmp_table->pos_in_table_list= 0;
6143 DBUG_PRINT(
"tmptable", (
"opened table: '%s'.'%s' 0x%lx", tmp_table->s->db.str,
6144 tmp_table->s->table_name.str, (
long) tmp_table));
6145 DBUG_RETURN(tmp_table);
6164 char frm_path[FN_REFLEN + 1];
6165 DBUG_ENTER(
"rm_temporary_table");
6167 strxnmov(frm_path,
sizeof(frm_path) - 1, path, reg_ext, NullS);
6170 file= get_new_handler((
TABLE_SHARE*) 0, current_thd->mem_root, base);
6174 sql_print_warning(
"Could not remove temporary table: '%s', error: %d",
6195 #define WRONG_GRANT (Field*) -1
6197 static void update_field_dependencies(THD *thd,
Field *field,
TABLE *table)
6199 DBUG_ENTER(
"update_field_dependencies");
6200 if (thd->mark_used_columns != MARK_COLUMNS_NONE)
6209 table->covering_keys.intersect(field->part_of_key);
6210 table->merge_keys.merge(field->part_of_key);
6212 if (thd->mark_used_columns == MARK_COLUMNS_READ)
6213 bitmap= table->read_set;
6215 bitmap= table->write_set;
6224 if (bitmap_fast_test_and_set(bitmap, field->field_index))
6226 if (thd->mark_used_columns == MARK_COLUMNS_WRITE)
6228 DBUG_PRINT(
"warning", (
"Found duplicated field"));
6229 thd->dup_field= field;
6233 DBUG_PRINT(
"note", (
"Field found before"));
6237 if (table->get_fields_in_item_tree)
6238 field->flags|= GET_FIXED_FIELDS_FLAG;
6239 table->used_fields++;
6241 else if (table->get_fields_in_item_tree)
6242 field->flags|= GET_FIXED_FIELDS_FLAG;
6274 DBUG_ENTER(
"open_temporary_table");
6275 DBUG_PRINT(
"enter", (
"table: '%s'.'%s'", tl->db, tl->table_name));
6281 DBUG_ASSERT(tl->table == NULL);
6289 DBUG_ASSERT(!tl->
derived && !tl->schema_table);
6293 DBUG_PRINT(
"info", (
"skip_temporary is set"));
6301 if (tl->
open_type == OT_TEMPORARY_ONLY &&
6304 my_error(ER_NO_SUCH_TABLE, MYF(0), tl->db, tl->table_name);
6310 #ifdef WITH_PARTITION_STORAGE_ENGINE
6311 if (tl->partition_names)
6314 DBUG_ASSERT(!table->part_info);
6315 my_error(ER_PARTITION_CLAUSE_ON_NONPARTITIONED, MYF(0));
6320 if (table->query_id)
6330 (
"query_id: %lu server_id: %u pseudo_thread_id: %lu",
6331 (ulong) table->query_id, (uint) thd->server_id,
6332 (ulong) thd->variables.pseudo_thread_id));
6333 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
6337 table->query_id= thd->query_id;
6338 thd->thread_specific_used= TRUE;
6343 table->
init(thd, tl);
6345 DBUG_PRINT(
"info", (
"Using temporary table"));
6365 TABLE_LIST *first_not_own= thd->lex->first_not_own_table();
6366 DBUG_ENTER(
"open_temporary_tables");
6368 for (
TABLE_LIST *tl= tl_list; tl && tl != first_not_own; tl= tl->next_global)
6370 if (tl->derived || tl->schema_table)
6408 find_field_in_view(THD *thd,
TABLE_LIST *table_list,
6409 const char *
name, uint length,
6410 const char *item_name,
Item **ref,
6411 bool register_tree_change)
6413 DBUG_ENTER(
"find_field_in_view");
6415 (
"view: '%s', field name: '%s', item name: '%s', ref 0x%lx",
6416 table_list->alias, name, item_name, (ulong) ref));
6418 field_it.set(table_list);
6420 DBUG_ASSERT(table_list->schema_table_reformed ||
6421 (ref != 0 && table_list->view != 0));
6422 for (; !field_it.end_of_fields(); field_it.next())
6424 if (!my_strcasecmp(system_charset_info, field_it.name(),
name))
6433 Prepared_stmt_arena_holder ps_arena_holder(
6435 register_tree_change &&
6436 thd->stmt_arena->is_stmt_prepare_or_first_stmt_execute());
6442 item= field_it.create_item(thd);
6456 item->item_name= (*ref)->item_name;
6457 item->real_item()->item_name= (*ref)->item_name;
6459 if (register_tree_change)
6460 thd->change_item_tree(ref, item);
6463 DBUG_RETURN((
Field*) view_ref_found);
6500 find_field_in_natural_join(THD *thd,
TABLE_LIST *table_ref,
const char *name,
6501 uint length,
Item **ref,
bool register_tree_change,
6505 field_it(*(table_ref->join_columns));
6508 DBUG_ENTER(
"find_field_in_natural_join");
6509 DBUG_PRINT(
"enter", (
"field name: '%s', ref 0x%lx",
6510 name, (ulong) ref));
6511 DBUG_ASSERT(table_ref->is_natural_join && table_ref->join_columns);
6512 DBUG_ASSERT(*actual_table == NULL);
6514 LINT_INIT(found_field);
6516 for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col;
6517 curr_nj_col= field_it++)
6519 if (!my_strcasecmp(system_charset_info, curr_nj_col->name(),
name))
6523 my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
6526 nj_col= curr_nj_col;
6532 if (nj_col->view_field)
6537 Prepared_stmt_arena_holder ps_arena_holder(thd, register_tree_change);
6543 item= nj_col->create_item(thd);
6557 item->item_name= (*ref)->item_name;
6558 item->real_item()->item_name= (*ref)->item_name;
6561 DBUG_ASSERT(nj_col->table_field == NULL);
6562 if (nj_col->table_ref->schema_table_reformed)
6569 DBUG_RETURN(((
Item_field*) (nj_col->view_field->item))->field);
6571 if (register_tree_change)
6572 thd->change_item_tree(ref, item);
6575 found_field= (
Field*) view_ref_found;
6580 DBUG_ASSERT(nj_col->view_field == NULL);
6587 if (!nj_col->table_field->fixed &&
6588 nj_col->table_field->
fix_fields(thd, (
Item **)&nj_col->table_field))
6590 DBUG_PRINT(
"info", (
"column '%s' was dropped by the concurrent connection",
6591 nj_col->table_field->item_name.
ptr()));
6594 DBUG_ASSERT(nj_col->table_ref->table == nj_col->table_field->field->table);
6595 found_field= nj_col->table_field->field;
6596 update_field_dependencies(thd, found_field, nj_col->table_ref->table);
6599 *actual_table= nj_col->table_ref;
6601 DBUG_RETURN(found_field);
6626 find_field_in_table(THD *thd,
TABLE *table,
const char *name, uint length,
6627 bool allow_rowid, uint *cached_field_index_ptr)
6629 Field **field_ptr, *field;
6630 uint cached_field_index= *cached_field_index_ptr;
6631 DBUG_ENTER(
"find_field_in_table");
6632 DBUG_PRINT(
"enter", (
"table: '%s', field name: '%s'", table->alias, name));
6635 if (cached_field_index < table->s->fields &&
6636 !my_strcasecmp(system_charset_info,
6637 table->field[cached_field_index]->field_name, name))
6638 field_ptr= table->field + cached_field_index;
6639 else if (table->s->name_hash.records)
6641 field_ptr= (
Field**) my_hash_search(&table->s->name_hash, (uchar*)
name,
6649 field_ptr= (table->field + (field_ptr - table->s->field));
6654 if (!(field_ptr= table->field))
6655 DBUG_RETURN((
Field *)0);
6656 for (; *field_ptr; ++field_ptr)
6657 if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
6661 if (field_ptr && *field_ptr)
6663 *cached_field_index_ptr= field_ptr - table->field;
6669 my_strcasecmp(system_charset_info, name,
"_rowid") ||
6670 table->s->rowid_field_offset == 0)
6671 DBUG_RETURN((
Field*) 0);
6672 field= table->field[table->s->rowid_field_offset-1];
6675 update_field_dependencies(thd, field, table);
6726 find_field_in_table_ref(THD *thd,
TABLE_LIST *table_list,
6727 const char *name, uint length,
6728 const char *item_name,
const char *db_name,
6729 const char *table_name,
Item **ref,
6730 bool check_privileges,
bool allow_rowid,
6731 uint *cached_field_index_ptr,
6732 bool register_tree_change,
TABLE_LIST **actual_table)
6735 DBUG_ENTER(
"find_field_in_table_ref");
6736 DBUG_ASSERT(table_list->alias);
6738 DBUG_ASSERT(item_name);
6740 (
"table: '%s' field name: '%s' item name: '%s' ref 0x%lx",
6741 table_list->alias, name, item_name, (ulong) ref));
6761 (!table_list->nested_join ||
6763 table_list->field_translation) &&
6768 table_name && table_name[0] &&
6769 (my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
6770 (db_name && db_name[0] && table_list->db && table_list->db[0] &&
6771 (table_list->schema_table ?
6772 my_strcasecmp(system_charset_info, db_name, table_list->db) :
6773 strcmp(db_name, table_list->db)))))
6776 *actual_table= NULL;
6778 if (table_list->field_translation)
6781 if ((fld= find_field_in_view(thd, table_list, name, length, item_name, ref,
6782 register_tree_change)))
6783 *actual_table= table_list;
6785 else if (!table_list->nested_join)
6788 DBUG_ASSERT(table_list->table);
6789 if ((fld= find_field_in_table(thd, table_list->table, name, length,
6791 cached_field_index_ptr)))
6792 *actual_table= table_list;
6803 if (table_name && table_name[0])
6807 while ((table= it++))
6809 if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
6810 db_name, table_name, ref,
6811 check_privileges, allow_rowid,
6812 cached_field_index_ptr,
6813 register_tree_change, actual_table)))
6824 fld= find_field_in_natural_join(thd, table_list, name, length, ref,
6825 register_tree_change, actual_table);
6830 #ifndef NO_EMBEDDED_ACCESS_CHECKS
6832 if (check_privileges &&
6833 check_column_grant_in_table_ref(thd, *actual_table, name, length))
6837 if (thd->mark_used_columns != MARK_COLUMNS_NONE)
6844 Field *field_to_set= NULL;
6845 if (fld == view_ref_found)
6847 Item *it= (*ref)->real_item();
6848 if (it->type() == Item::FIELD_ITEM)
6852 if (thd->mark_used_columns == MARK_COLUMNS_READ)
6853 it->walk(&Item::register_field_in_read_map, 1, (uchar *) 0);
6860 TABLE *table= field_to_set->table;
6861 if (thd->mark_used_columns == MARK_COLUMNS_READ)
6862 bitmap_set_bit(table->read_set, field_to_set->field_index);
6864 bitmap_set_bit(table->write_set, field_to_set->field_index);
6887 Field *find_field_in_table_sef(
TABLE *table,
const char *name)
6890 if (table->s->name_hash.records)
6892 field_ptr= (
Field**)my_hash_search(&table->s->name_hash,(uchar*)
name,
6900 field_ptr= (table->field + (field_ptr - table->s->field));
6905 if (!(field_ptr= table->field))
6907 for (; *field_ptr; ++field_ptr)
6908 if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
6953 find_field_in_tables(THD *thd,
Item_ident *item,
6955 Item **ref, find_item_error_report_type report_error,
6956 bool check_privileges,
bool register_tree_change)
6959 const char *db= item->db_name;
6960 const char *table_name= item->table_name;
6961 const char *name= item->field_name;
6962 uint length=(uint) strlen(name);
6963 char name_buff[NAME_LEN+1];
6968 if (!table_name || !table_name[0])
6974 allow_rowid= table_name || (cur_table && !cur_table->next_local);
6976 if (item->cached_table)
6993 if (table_ref->table && !table_ref->view)
6995 found= find_field_in_table(thd, table_ref->table, name, length,
6996 TRUE, &(item->cached_field_index));
6997 #ifndef NO_EMBEDDED_ACCESS_CHECKS
6999 if (found && check_privileges &&
7000 check_column_grant_in_table_ref(thd, table_ref, name, length))
7005 found= find_field_in_table_ref(thd, table_ref, name, length,
7006 item->item_name.
ptr(),
7007 NULL, NULL, ref, check_privileges,
7008 TRUE, &(item->cached_field_index),
7009 register_tree_change,
7013 if (found == WRONG_GRANT)
7020 if (!table_ref->belong_to_view)
7022 SELECT_LEX *current_sel= thd->lex->current_select;
7023 SELECT_LEX *last_select= table_ref->select_lex;
7028 if (current_sel != last_select)
7029 mark_select_range_as_dependent(thd, last_select, current_sel,
7036 if (db && lower_case_table_names)
7043 strmake(name_buff, db,
sizeof(name_buff)-1);
7044 my_casedn_str(files_charset_info, name_buff);
7049 last_table= last_table->next_name_resolution_table;
7051 for (; cur_table != last_table ;
7052 cur_table= cur_table->next_name_resolution_table)
7054 Field *cur_field= find_field_in_table_ref(thd, cur_table, name, length,
7056 (thd->lex->sql_command ==
7058 ?
false : check_privileges,
7060 &(item->cached_field_index),
7061 register_tree_change,
7065 if (cur_field == WRONG_GRANT)
7067 if (thd->lex->sql_command != SQLCOM_SHOW_FIELDS)
7071 cur_field= find_field_in_table_ref(thd, cur_table, name, length,
7075 &(item->cached_field_index),
7076 register_tree_change,
7081 cur_field->field_name,
7083 nf->init(cur_table->table);
7092 item->cached_table= (!actual_table->cacheable_table || found) ?
7095 DBUG_ASSERT(thd->where);
7105 if (report_error == REPORT_ALL_ERRORS ||
7106 report_error == IGNORE_EXCEPT_NON_UNIQUE)
7107 my_error(ER_NON_UNIQ_ERROR, MYF(0),
7108 table_name ? item->full_name() :
name, thd->where);
7125 if (table_name && (cur_table == first_table) &&
7126 (report_error == REPORT_ALL_ERRORS ||
7127 report_error == REPORT_EXCEPT_NON_UNIQUE))
7129 char buff[NAME_LEN*2 + 2];
7132 strxnmov(buff,
sizeof(buff)-1,db,
".",table_name,NullS);
7135 my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
7139 if (report_error == REPORT_ALL_ERRORS ||
7140 report_error == REPORT_EXCEPT_NON_UNIQUE)
7141 my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd->where);
7143 found= not_found_field;
7183 Item **not_found_item= (
Item**) 0x1;
7188 find_item_error_report_type report_error,
7189 enum_resolution_type *resolution)
7192 Item **found=0, **found_unaliased= 0, *item;
7193 const char *db_name=0;
7194 const char *field_name=0;
7195 const char *table_name=0;
7196 bool found_unaliased_non_uniq= 0;
7201 bool is_ref_by_name= 0;
7202 uint unaliased_counter= 0;
7204 *resolution= NOT_RESOLVED;
7206 is_ref_by_name= (find->type() == Item::FIELD_ITEM ||
7207 find->type() == Item::REF_ITEM);
7210 field_name= ((
Item_ident*) find)->field_name;
7211 table_name= ((
Item_ident*) find)->table_name;
7215 for (uint
i= 0; (item=li++);
i++)
7217 if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
7227 if (!item_field->item_name.
is_set())
7248 if (item_field->field_name && item_field->table_name &&
7249 !my_strcasecmp(system_charset_info, item_field->field_name,
7251 !my_strcasecmp(table_alias_charset, item_field->table_name,
7253 (!db_name || (item_field->db_name &&
7254 !strcmp(item_field->db_name, db_name))))
7256 if (found_unaliased)
7258 if ((*found_unaliased)->eq(item, 0))
7265 if (report_error != IGNORE_ERRORS)
7266 my_error(ER_NON_UNIQ_ERROR, MYF(0),
7267 find->full_name(), current_thd->where);
7270 found_unaliased= li.ref();
7271 unaliased_counter=
i;
7272 *resolution= RESOLVED_IGNORING_ALIAS;
7279 int fname_cmp= my_strcasecmp(system_charset_info,
7280 item_field->field_name,
7282 if (item_field->item_name.eq_safe(field_name))
7293 if ((*found)->eq(item, 0))
7295 if (report_error != IGNORE_ERRORS)
7296 my_error(ER_NON_UNIQ_ERROR, MYF(0),
7297 find->full_name(), current_thd->where);
7302 *resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS:
7303 RESOLVED_WITH_NO_ALIAS;
7305 else if (!fname_cmp)
7313 if (found_unaliased)
7315 if ((*found_unaliased)->eq(item, 0))
7317 found_unaliased_non_uniq= 1;
7319 found_unaliased= li.ref();
7320 unaliased_counter=
i;
7324 else if (!table_name)
7326 if (is_ref_by_name && item->item_name.eq_safe(find->item_name))
7330 *resolution= RESOLVED_AGAINST_ALIAS;
7333 else if (find->
eq(item,0))
7337 *resolution= RESOLVED_IGNORING_ALIAS;
7341 else if (table_name && item->type() == Item::REF_ITEM &&
7342 ((
Item_ref *)item)->ref_type() == Item_ref::VIEW_REF)
7357 if (item_ref->item_name.eq_safe(field_name) &&
7358 item_ref->table_name &&
7359 !my_strcasecmp(table_alias_charset, item_ref->table_name,
7361 (!db_name || (item_ref->db_name &&
7362 !strcmp (item_ref->db_name, db_name))))
7366 *resolution= RESOLVED_IGNORING_ALIAS;
7373 if (found_unaliased_non_uniq)
7375 if (report_error != IGNORE_ERRORS)
7376 my_error(ER_NON_UNIQ_ERROR, MYF(0),
7377 find->full_name(), current_thd->where);
7380 if (found_unaliased)
7382 found= found_unaliased;
7383 *counter= unaliased_counter;
7384 *resolution= RESOLVED_BEHIND_ALIAS;
7389 if (report_error != REPORT_EXCEPT_NOT_FOUND)
7391 if (report_error == REPORT_ALL_ERRORS)
7392 my_error(ER_BAD_FIELD_ERROR, MYF(0),
7393 find->full_name(), current_thd->where);
7397 return (
Item **) not_found_item;
7419 test_if_string_in_list(
const char *find,
List<String> *str_list)
7423 size_t find_length= strlen(find);
7424 while ((curr_str= str_list_it++))
7426 if (find_length != curr_str->length())
7428 if (!my_strcasecmp(system_charset_info, find, curr_str->ptr()))
7461 context->first_name_resolution_table=
7462 context->last_name_resolution_table= table_ref;
7463 item->context= context;
7505 bool first_outer_loop= TRUE;
7510 TABLE_LIST *leaf_1= (table_ref_1->nested_join &&
7511 !table_ref_1->is_natural_join) ?
7513 TABLE_LIST *leaf_2= (table_ref_2->nested_join &&
7514 !table_ref_2->is_natural_join) ?
7517 DBUG_ENTER(
"mark_common_columns");
7518 DBUG_PRINT(
"info", (
"operand_1: %s operand_2: %s",
7519 table_ref_1->alias, table_ref_2->alias));
7521 Prepared_stmt_arena_holder ps_arena_holder(thd);
7523 *found_using_fields= 0;
7525 for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
7528 const char *field_name_1;
7530 bool is_using_column_1;
7531 if (!(nj_col_1= it_1.get_or_create_column_ref(thd, leaf_1)))
7533 field_name_1= nj_col_1->name();
7534 is_using_column_1= using_fields &&
7535 test_if_string_in_list(field_name_1, using_fields);
7536 DBUG_PRINT (
"info", (
"field_name_1=%s.%s",
7537 nj_col_1->table_name() ? nj_col_1->table_name() :
"",
7548 for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
7551 const char *cur_field_name_2;
7552 if (!(cur_nj_col_2= it_2.get_or_create_column_ref(thd, leaf_2)))
7554 cur_field_name_2= cur_nj_col_2->name();
7555 DBUG_PRINT (
"info", (
"cur_field_name_2=%s.%s",
7556 cur_nj_col_2->table_name() ?
7557 cur_nj_col_2->table_name() :
"",
7571 if (!my_strcasecmp(system_charset_info, field_name_1, cur_field_name_2))
7573 DBUG_PRINT (
"info", (
"match c1.is_common=%d", nj_col_1->is_common));
7574 if (cur_nj_col_2->is_common ||
7575 (found && (!using_fields || is_using_column_1)))
7577 my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, thd->where);
7580 nj_col_2= cur_nj_col_2;
7584 if (first_outer_loop && leaf_2)
7590 leaf_2->is_join_columns_complete= TRUE;
7591 first_outer_loop= FALSE;
7601 if (nj_col_2 && (!using_fields ||is_using_column_1))
7603 Item *item_1= nj_col_1->create_item(thd);
7604 Item *item_2= nj_col_2->create_item(thd);
7605 Field *field_1= nj_col_1->field();
7606 Field *field_2= nj_col_2->field();
7610 if (!item_1 || !item_2)
7617 DBUG_ASSERT(!thd->lex->current_select->no_wrap_view_item);
7622 DBUG_ASSERT(item_1->type() == Item::FIELD_ITEM ||
7623 item_1->type() == Item::REF_ITEM);
7624 DBUG_ASSERT(item_2->type() == Item::FIELD_ITEM ||
7625 item_2->type() == Item::REF_ITEM);
7639 if (set_new_item_local_context(thd, item_ident_1, nj_col_1->table_ref) ||
7640 set_new_item_local_context(thd, item_ident_2, nj_col_2->table_ref))
7643 if (!(eq_cond=
new Item_func_eq(item_ident_1, item_ident_2)))
7651 add_join_on((table_ref_1->outer_join & JOIN_TYPE_RIGHT ?
7652 table_ref_1 : table_ref_2),
7655 nj_col_1->is_common= nj_col_2->is_common= TRUE;
7656 DBUG_PRINT (
"info", (
"%s.%s and %s.%s are common",
7657 nj_col_1->table_name() ?
7658 nj_col_1->table_name() :
"",
7660 nj_col_2->table_name() ?
7661 nj_col_2->table_name() :
"",
7666 TABLE *table_1= nj_col_1->table_ref->table;
7668 bitmap_set_bit(table_1->read_set, field_1->field_index);
7669 table_1->covering_keys.intersect(field_1->part_of_key);
7670 table_1->merge_keys.merge(field_1->part_of_key);
7674 TABLE *table_2= nj_col_2->table_ref->table;
7676 bitmap_set_bit(table_2->read_set, field_2->field_index);
7677 table_2->covering_keys.intersect(field_2->part_of_key);
7678 table_2->merge_keys.merge(field_2->part_of_key);
7681 if (using_fields != NULL)
7682 ++(*found_using_fields);
7686 leaf_1->is_join_columns_complete= TRUE;
7736 store_natural_using_join_columns(THD *thd,
TABLE_LIST *natural_using_join,
7740 uint found_using_fields)
7745 DBUG_ENTER(
"store_natural_using_join_columns");
7747 DBUG_ASSERT(!natural_using_join->join_columns);
7749 Prepared_stmt_arena_holder ps_arena_holder(thd);
7756 for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
7758 nj_col_1= it_1.get_natural_column_ref();
7759 if (nj_col_1->is_common)
7761 natural_using_join->join_columns->push_back(nj_col_1);
7763 nj_col_1->is_common= FALSE;
7766 non_join_columns->push_back(nj_col_1);
7774 if (using_fields && found_using_fields < using_fields->elements)
7776 String *using_field_name;
7778 while ((using_field_name= using_fields_it++))
7780 const char *using_field_name_ptr= using_field_name->c_ptr();
7782 it(*(natural_using_join->join_columns));
7788 if (!(common_field= it++))
7790 my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr,
7791 current_thd->where);
7794 if (!my_strcasecmp(system_charset_info,
7795 common_field->name(), using_field_name_ptr))
7802 for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
7804 nj_col_2= it_2.get_natural_column_ref();
7805 if (!nj_col_2->is_common)
7806 non_join_columns->push_back(nj_col_2);
7810 nj_col_2->is_common= FALSE;
7814 if (non_join_columns->elements > 0)
7815 natural_using_join->join_columns->concat(non_join_columns);
7816 natural_using_join->is_join_columns_complete= TRUE;
7853 store_top_level_join_columns(THD *thd,
TABLE_LIST *table_ref,
7857 DBUG_ENTER(
"store_top_level_join_columns");
7859 Prepared_stmt_arena_holder ps_arena_holder(thd);
7862 if (table_ref->nested_join)
7865 TABLE_LIST *same_level_left_neighbor= nested_it++;
7868 TABLE_LIST *real_left_neighbor, *real_right_neighbor;
7870 while (same_level_left_neighbor)
7872 TABLE_LIST *cur_table_ref= same_level_left_neighbor;
7873 same_level_left_neighbor= nested_it++;
7885 if (same_level_left_neighbor &&
7886 cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
7889 DBUG_ASSERT(table_ref->nested_join->join_list.elements == 2);
7890 swap_variables(
TABLE_LIST*, same_level_left_neighbor, cur_table_ref);
7897 real_left_neighbor= (same_level_left_neighbor) ?
7898 same_level_left_neighbor : left_neighbor;
7899 real_right_neighbor= (same_level_right_neighbor) ?
7900 same_level_right_neighbor : right_neighbor;
7902 if (cur_table_ref->nested_join &&
7903 store_top_level_join_columns(thd, cur_table_ref,
7904 real_left_neighbor, real_right_neighbor))
7906 same_level_right_neighbor= cur_table_ref;
7914 if (table_ref->is_natural_join)
7916 DBUG_ASSERT(table_ref->nested_join &&
7917 table_ref->nested_join->join_list.elements == 2);
7926 List<String> *using_fields= table_ref->join_using_fields;
7927 uint found_using_fields;
7933 if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
7934 swap_variables(
TABLE_LIST*, table_ref_1, table_ref_2);
7935 if (mark_common_columns(thd, table_ref_1, table_ref_2,
7936 using_fields, &found_using_fields))
7944 if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
7945 swap_variables(
TABLE_LIST*, table_ref_1, table_ref_2);
7946 if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
7947 table_ref_2, using_fields,
7948 found_using_fields))
7957 table_ref_1->natural_join= table_ref_2->natural_join= NULL;
7960 if (table_ref_2->outer_join &&
7961 !table_ref_1->join_cond() && !table_ref_2->join_cond())
7962 table_ref_2->set_join_cond(
new Item_int((longlong) 1,1));
7968 last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
7969 last_leaf_on_the_left->next_name_resolution_table= table_ref;
7974 first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
7975 table_ref->next_name_resolution_table= first_leaf_on_the_right;
7978 table_ref->next_name_resolution_table= NULL;
8009 static bool setup_natural_join_row_types(THD *thd,
8013 DBUG_ENTER(
"setup_natural_join_row_types");
8014 thd->where=
"from clause";
8015 if (from_clause->elements == 0)
8026 for (left_neighbor= table_ref_it++; left_neighbor ; )
8028 table_ref= left_neighbor;
8029 left_neighbor= table_ref_it++;
8035 if (context->select_lex->first_natural_join_processing)
8037 if (store_top_level_join_columns(thd, table_ref,
8038 left_neighbor, right_neighbor))
8043 first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
8044 left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
8047 right_neighbor= table_ref;
8056 DBUG_ASSERT(right_neighbor);
8057 context->first_name_resolution_table=
8058 right_neighbor->first_leaf_for_name_resolution();
8059 context->select_lex->first_natural_join_processing=
false;
8061 DBUG_RETURN (
false);
8078 DBUG_ENTER(
"setup_wild");
8084 Prepared_stmt_arena_holder ps_arena_holder(thd);
8087 DBUG_ASSERT(thd->lex->current_select->cur_pos_in_all_fields ==
8088 SELECT_LEX::ALL_FIELDS_UNDEF_POS);
8090 thd->lex->current_select->cur_pos_in_all_fields= 0;
8091 while (wild_num && (item= it++))
8093 if (item->type() == Item::FIELD_ITEM &&
8095 ((
Item_field*) item)->field_name[0] ==
'*' &&
8098 uint elem= fields.elements;
8099 bool any_privileges= ((
Item_field *) item)->any_privileges;
8100 Item_subselect *subsel= thd->lex->current_select->master_unit()->item;
8102 subsel->substype() == Item_subselect::EXISTS_SUBS)
8109 it.replace(
new Item_int(NAME_STRING(
"Not_used"), (longlong) 1,
8110 MY_INT64_NUM_DECIMAL_DIGITS));
8112 else if (insert_fields(thd, ((
Item_field*) item)->context,
8126 sum_func_list->elements+= fields.elements - elem;
8131 thd->lex->current_select->cur_pos_in_all_fields++;
8134 thd->lex->current_select->cur_pos_in_all_fields=
8135 SELECT_LEX::ALL_FIELDS_UNDEF_POS;
8137 if (ps_arena_holder.is_activated())
8140 SELECT_LEX *select_lex= thd->lex->current_select;
8141 select_lex->with_wild= 0;
8147 if (&select_lex->item_list != &fields)
8148 select_lex->item_list= fields;
8157 bool setup_fields(THD *thd,
Ref_ptr_array ref_pointer_array,
8158 List<Item> &fields, enum_mark_columns mark_used_columns,
8159 List<Item> *sum_func_list,
bool allow_sum_func)
8162 enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
8163 nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
8165 bool save_is_item_list_lookup;
8166 DBUG_ENTER(
"setup_fields");
8168 thd->mark_used_columns= mark_used_columns;
8169 DBUG_PRINT(
"info", (
"thd->mark_used_columns: %d", thd->mark_used_columns));
8171 thd->lex->allow_sum_func|=
8172 (nesting_map)1 << thd->lex->current_select->nest_level;
8173 thd->where= THD::DEFAULT_WHERE;
8174 save_is_item_list_lookup= thd->lex->current_select->is_item_list_lookup;
8175 thd->lex->current_select->is_item_list_lookup= 0;
8188 if (!ref_pointer_array.is_null())
8190 DBUG_ASSERT(ref_pointer_array.size() >= fields.elements);
8191 memset(ref_pointer_array.array(), 0,
sizeof(
Item *) * fields.elements);
8208 var->set_entry(thd, FALSE);
8211 DBUG_ASSERT(thd->lex->current_select->cur_pos_in_all_fields ==
8212 SELECT_LEX::ALL_FIELDS_UNDEF_POS);
8213 thd->lex->current_select->cur_pos_in_all_fields= 0;
8214 while ((item= it++))
8216 if ((!item->fixed && item->fix_fields(thd, it.ref())) ||
8217 (item= *(it.ref()))->check_cols(1))
8219 thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
8220 thd->lex->allow_sum_func= save_allow_sum_func;
8221 thd->mark_used_columns= save_mark_used_columns;
8222 DBUG_PRINT(
"info", (
"thd->mark_used_columns: %d", thd->mark_used_columns));
8230 if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
8232 item->split_sum_func(thd, ref_pointer_array, *sum_func_list);
8233 thd->lex->current_select->select_list_tables|= item->used_tables();
8234 thd->lex->used_tables|= item->used_tables();
8235 thd->lex->current_select->cur_pos_in_all_fields++;
8237 thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
8238 thd->lex->current_select->cur_pos_in_all_fields=
8239 SELECT_LEX::ALL_FIELDS_UNDEF_POS;
8241 thd->lex->allow_sum_func= save_allow_sum_func;
8242 thd->mark_used_columns= save_mark_used_columns;
8243 DBUG_PRINT(
"info", (
"thd->mark_used_columns: %d", thd->mark_used_columns));
8244 DBUG_RETURN(
test(thd->is_error()));
8263 if (table->merge_underlying_list)
8265 DBUG_ASSERT(table->view &&
8266 table->effective_algorithm == VIEW_ALGORITHM_MERGE);
8267 list= make_leaves_list(list, table->merge_underlying_list);
8272 list= &table->next_leaf;
8311 DBUG_ENTER(
"setup_tables");
8313 DBUG_ASSERT ((select_insert && !tables->next_name_resolution_table) || !tables ||
8314 (context->table_list && context->first_name_resolution_table));
8319 TABLE_LIST *first_select_table= (select_insert ?
8323 make_leaves_list(leaves, tables);
8326 for (table_list= *leaves;
8328 table_list= table_list->next_leaf, tablenr++)
8330 TABLE *table= table_list->table;
8331 table->pos_in_table_list= table_list;
8332 if (first_select_table &&
8333 table_list->top_table() == first_select_table)
8336 first_select_table= 0;
8339 setup_table_map(table, table_list, tablenr);
8340 if (table_list->process_index_hints(table))
8345 my_error(ER_TOO_MANY_TABLES,MYF(0), static_cast<int>(
MAX_TABLES));
8348 for (table_list= tables;
8350 table_list= table_list->next_local)
8352 if (table_list->merge_underlying_list)
8354 DBUG_ASSERT(table_list->view &&
8357 Prepared_stmt_arena_holder ps_arena_holder(thd);
8358 if (table_list->setup_underlying(thd))
8364 if (setup_natural_join_row_types(thd, from_clause, context))
8404 ulong want_access_first,
8408 bool first_table=
true;
8410 if (setup_tables(thd, context, from_clause, tables,
8411 &leaves_tmp, select_insert))
8415 *leaves= leaves_tmp;
8417 for (; leaves_tmp; leaves_tmp= leaves_tmp->next_leaf)
8419 if (leaves_tmp->belong_to_view &&
8421 want_access, leaves_tmp, FALSE))
8453 bool any_privileges)
8457 char name_buff[NAME_LEN+1];
8458 DBUG_ENTER(
"insert_fields");
8459 DBUG_PRINT(
"arena", (
"stmt arena: 0x%lx", (ulong)thd->stmt_arena));
8461 if (db_name && lower_case_table_names)
8468 strmake(name_buff, db_name,
sizeof(name_buff)-1);
8469 my_casedn_str(files_charset_info, name_buff);
8480 for (
TABLE_LIST *tables= (table_name ? context->table_list :
8481 context->first_name_resolution_table);
8483 tables= (table_name ? tables->next_local :
8484 tables->next_name_resolution_table)
8488 TABLE *table= tables->table;
8490 DBUG_ASSERT(tables->is_leaf_for_name_resolution());
8492 if ((table_name && my_strcasecmp(table_alias_charset, table_name,
8494 (db_name && strcmp(tables->db,db_name)))
8497 #ifndef NO_EMBEDDED_ACCESS_CHECKS
8524 if (!((table && !tables->view && (table->grant.
privilege & SELECT_ACL)) ||
8525 (tables->view && (tables->grant.
privilege & SELECT_ACL))) &&
8528 field_iterator.set(tables);
8529 if (check_grant_all_columns(thd, SELECT_ACL, &field_iterator))
8540 thd->lex->used_tables|= table->map;
8541 thd->lex->current_select->select_list_tables|= table->map;
8550 field_iterator.set(tables);
8552 for (; !field_iterator.end_of_fields(); field_iterator.next())
8556 if (!(item= field_iterator.create_item(thd)))
8558 DBUG_ASSERT(item->fixed);
8560 if (item->type() == Item::FIELD_ITEM && tables->cacheable_table)
8571 #ifndef NO_EMBEDDED_ACCESS_CHECKS
8582 DBUG_ASSERT((tables->field_translation == NULL && table) ||
8583 tables->is_natural_join);
8584 DBUG_ASSERT(item->type() == Item::FIELD_ITEM);
8586 const char *field_table_name= field_iterator.get_table_name();
8588 if (!tables->schema_table &&
8589 !(fld->have_privileges=
8590 (get_column_grant(thd, field_iterator.grant(),
8591 field_iterator.get_db_name(),
8592 field_table_name, fld->field_name) &
8595 my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0),
"ANY",
8596 thd->security_ctx->priv_user,
8597 thd->security_ctx->host_or_ip,
8604 if ((field= field_iterator.field()))
8607 bitmap_set_bit(field->table->read_set, field->field_index);
8610 table->covering_keys.intersect(field->part_of_key);
8611 table->merge_keys.merge(field->part_of_key);
8613 if (tables->is_natural_join)
8621 if (!(nj_col= field_iterator.get_natural_column_ref()))
8623 DBUG_ASSERT(nj_col->table_field);
8624 field_table= nj_col->table_ref->table;
8627 thd->lex->used_tables|= field_table->map;
8628 thd->lex->current_select->select_list_tables|=
8630 field_table->covering_keys.intersect(field->part_of_key);
8631 field_table->merge_keys.merge(field->part_of_key);
8632 field_table->used_fields++;
8638 thd->lex->used_tables|= item->used_tables();
8639 thd->lex->current_select->select_list_tables|=
8640 item->used_tables();
8642 thd->lex->current_select->cur_pos_in_all_fields++;
8651 table->used_fields= table->s->fields;
8661 if (!table_name || !*table_name)
8662 my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
8668 tbl_name.append(
String(db_name,system_charset_info));
8669 tbl_name.append(
'.');
8671 tbl_name.append(
String(table_name,system_charset_info));
8673 my_error(ER_BAD_TABLE_ERROR, MYF(0), tbl_name.c_ptr_safe());
8701 SELECT_LEX *select_lex= thd->lex->current_select;
8711 bool it_is_update= (select_lex == &thd->lex->select_lex) &&
8712 thd->lex->which_check_option_applicable();
8713 bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
8714 select_lex->is_item_list_lookup= 0;
8715 DBUG_ENTER(
"setup_conds");
8717 thd->mark_used_columns= MARK_COLUMNS_READ;
8718 DBUG_PRINT(
"info", (
"thd->mark_used_columns: %d", thd->mark_used_columns));
8719 select_lex->cond_count= 0;
8720 select_lex->between_count= 0;
8721 select_lex->max_equal_elems= 0;
8723 for (table= tables;
table; table= table->next_local)
8725 select_lex->resolve_place= st_select_lex::RESOLVE_CONDITION;
8733 embedding= embedding->embedding)
8735 if (embedding->outer_join)
8742 select_lex->resolve_place= st_select_lex::RESOLVE_JOIN_NEST;
8743 select_lex->resolve_nest= embedding;
8747 if (table->prepare_where(thd, conds, FALSE))
8749 select_lex->resolve_place= st_select_lex::RESOLVE_NONE;
8750 select_lex->resolve_nest= NULL;
8755 select_lex->resolve_place= st_select_lex::RESOLVE_CONDITION;
8756 thd->where=
"where clause";
8757 if ((!(*conds)->fixed && (*conds)->fix_fields(thd, conds)) ||
8758 (*conds)->check_cols(1))
8760 select_lex->where= *conds;
8761 select_lex->resolve_place= st_select_lex::RESOLVE_NONE;
8768 for (table= leaves;
table; table= table->next_leaf)
8774 embedded= embedding;
8775 if (embedded->join_cond())
8778 select_lex->resolve_place= st_select_lex::RESOLVE_JOIN_NEST;
8779 select_lex->resolve_nest= embedded;
8780 thd->where=
"on clause";
8781 if ((!embedded->join_cond()->fixed &&
8782 embedded->join_cond()->fix_fields(thd, embedded->join_cond_ref())) ||
8783 embedded->join_cond()->check_cols(1))
8785 select_lex->cond_count++;
8786 select_lex->resolve_place= st_select_lex::RESOLVE_NONE;
8787 select_lex->resolve_nest= NULL;
8789 embedding= embedded->embedding;
8792 embedding->nested_join->join_list.head() == embedded);
8798 if (view->effective_with_check)
8800 if (view->prepare_check_option(thd))
8802 thd->change_item_tree(&table->check_option, view->check_option);
8807 thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
8808 DBUG_RETURN(
test(thd->is_error()));
8811 select_lex->is_item_list_lookup= save_is_item_list_lookup;
8848 DBUG_ENTER(
"fill_record");
8849 DBUG_ASSERT(fields.elements == values.elements);
8854 if (fields.elements)
8861 if (!(field= fld->field_for_view_update()))
8863 my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), fld->item_name.
ptr());
8866 table= field->field->table;
8867 table->auto_increment_field_not_null= FALSE;
8872 if (!(field= fld->field_for_view_update()))
8874 my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), fld->item_name.
ptr());
8878 Field *rfield= field->field;
8880 if (bitmap && !bitmap_is_set(bitmap, rfield->field_index))
8882 table= rfield->table;
8883 if (rfield == table->next_number_field)
8884 table->auto_increment_field_not_null= TRUE;
8885 if ((value->
save_in_field(rfield, 0) < 0) && !ignore_errors)
8887 my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
8891 DBUG_RETURN(thd->is_error());
8894 table->auto_increment_field_not_null= FALSE;
8923 fill_record_n_invoke_before_triggers(THD *thd,
List<Item> &fields,
8926 enum trg_event_type
event)
8928 return (fill_record(thd, fields, values, ignore_errors, NULL) ||
8930 TRG_ACTION_BEFORE, TRUE)));
8959 DBUG_ENTER(
"fill_record");
8972 table= (*ptr)->table;
8973 table->auto_increment_field_not_null= FALSE;
8975 while ((field = *ptr++) && ! thd->is_error())
8978 table= field->table;
8980 if (bitmap && !bitmap_is_set(bitmap, field->field_index))
8982 if (field == table->next_number_field)
8983 table->auto_increment_field_not_null= TRUE;
8984 if (value->
save_in_field(field, 0) == TYPE_ERR_NULL_CONSTRAINT_VIOLATION)
8987 DBUG_ASSERT(thd->is_error() || !v++);
8988 DBUG_RETURN(thd->is_error());
8992 table->auto_increment_field_not_null= FALSE;
9021 fill_record_n_invoke_before_triggers(THD *thd,
Field **ptr,
9024 enum trg_event_type event)
9026 return (fill_record(thd, ptr, values, ignore_errors, NULL) ||
9028 TRG_ACTION_BEFORE, TRUE)));
9032 my_bool mysql_rm_tmp_tables(
void)
9035 char filePath[FN_REFLEN], *tmpdir, filePathCopy[FN_REFLEN];
9040 DBUG_ENTER(
"mysql_rm_tmp_tables");
9042 if (!(thd=
new THD))
9044 thd->thread_stack= (
char*) &thd;
9045 thd->store_globals();
9047 for (i=0; i<=mysql_tmpdir_list.max; i++)
9049 tmpdir=mysql_tmpdir_list.list[
i];
9051 if (!(dirp = my_dir(tmpdir,MYF(MY_WME | MY_DONT_SORT))))
9056 for (idx=0 ; idx < (uint) dirp->number_off_files ; idx++)
9058 file=dirp->dir_entry+idx;
9061 if (file->name[0] ==
'.' && (!file->name[1] ||
9062 (file->name[1] ==
'.' && !file->name[2])))
9066 tmp_file_prefix_length))
9068 char *ext= fn_ext(file->name);
9069 uint ext_len= strlen(ext);
9070 uint filePath_len= my_snprintf(filePath,
sizeof(filePath),
9071 "%s%c%s", tmpdir, FN_LIBCHAR,
9073 if (!memcmp(reg_ext, ext, ext_len))
9077 memcpy(filePathCopy, filePath, filePath_len - ext_len);
9078 filePathCopy[filePath_len - ext_len]= 0;
9079 init_tmp_table_share(thd, &share,
"", 0,
"", filePathCopy);
9080 if (!open_table_def(thd, &share, 0) &&
9081 ((handler_file= get_new_handler(&share, thd->mem_root,
9085 delete handler_file;
9087 free_table_share(&share);
9100 my_pthread_setspecific_ptr(THR_THD, 0);
9118 void tdc_flush_unused_tables()
9166 const char *db,
const char *table_name,
9169 char key[MAX_DBKEY_LENGTH];
9178 DBUG_ASSERT(remove_type == TDC_RT_REMOVE_UNUSED ||
9179 thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name,
9182 key_length= create_table_def_key(thd, key, db, table_name,
false);
9184 if ((share= (
TABLE_SHARE*) my_hash_search(&table_def_cache,(uchar*) key,
9187 if (share->ref_count)
9200 if (remove_type != TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE)
9202 table_cache_manager.
free_table(thd, remove_type, share);
9206 DBUG_ASSERT(remove_type != TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE);
9207 (void) my_hash_delete(&table_def_cache, (uchar*) share);
9216 int setup_ftfuncs(SELECT_LEX *select_lex)
9219 lj(*(select_lex->ftfunc_list));
9224 if (ftf->fix_index())
9227 while ((ftf2=lj++) != ftf)
9229 if (ftf->
eq(ftf2,1) && !ftf2->master)
9238 int init_ftfuncs(THD *thd, SELECT_LEX *select_lex,
bool no_order)
9240 if (select_lex->ftfunc_list->elements)
9244 DBUG_PRINT(
"info",(
"Performing FULLTEXT search"));
9245 THD_STAGE_INFO(thd, stage_fulltext_initialization);
9248 ifm->init_search(no_order);
9256 return a->length == b->length && !strncmp(a->str, b->str, a->length);
9275 has_write_table_with_auto_increment(
TABLE_LIST *tables)
9280 if (!table->placeholder() &&
9281 table->table->found_next_number_field &&
9282 (table->lock_type >= TL_WRITE_ALLOW_WRITE))
9307 has_write_table_with_auto_increment_and_select(
TABLE_LIST *tables)
9309 bool has_select=
false;
9310 bool has_auto_increment_tables = has_write_table_with_auto_increment(tables);
9313 if (!table->placeholder() &&
9314 (table->lock_type <= TL_READ_NO_INSERT))
9320 return(has_select && has_auto_increment_tables);
9334 has_write_table_auto_increment_not_first_in_pk(
TABLE_LIST *tables)
9339 if (!table->placeholder() &&
9340 table->table->found_next_number_field &&
9341 (table->lock_type >= TL_WRITE_ALLOW_WRITE)
9342 && table->table->s->next_number_keypart != 0)
9376 open_system_tables_for_read(THD *thd,
TABLE_LIST *table_list,
9377 Open_tables_backup *backup)
9379 Query_tables_list query_tables_list_backup;
9382 DBUG_ENTER(
"open_system_tables_for_read");
9390 lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
9391 thd->reset_n_backup_open_tables_state(backup);
9394 MYSQL_OPEN_IGNORE_FLUSH |
9395 MYSQL_LOCK_IGNORE_TIMEOUT))
9397 lex->restore_backup_query_tables_list(&query_tables_list_backup);
9398 thd->restore_backup_open_tables_state(backup);
9402 for (
TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
9404 DBUG_ASSERT(tables->table->s->
table_category == TABLE_CATEGORY_SYSTEM);
9405 tables->table->use_all_columns();
9407 lex->restore_backup_query_tables_list(&query_tables_list_backup);
9425 close_system_tables(THD *thd, Open_tables_backup *backup)
9427 Query_tables_list query_tables_list_backup;
9434 thd->lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
9435 close_thread_tables(thd);
9436 thd->lex->restore_backup_query_tables_list(&query_tables_list_backup);
9437 thd->restore_backup_open_tables_state(backup);
9468 DBUG_ASSERT(thd->transaction.stmt.is_empty());
9469 close_thread_tables(thd);
9470 thd->mdl_context.release_transactional_locks();
9490 open_system_table_for_update(THD *thd,
TABLE_LIST *one_table)
9492 DBUG_ENTER(
"open_system_table_for_update");
9494 TABLE *table= open_ltable(thd, one_table, one_table->lock_type,
9495 MYSQL_LOCK_IGNORE_TIMEOUT);
9499 table->use_all_columns();
9519 uint flags= ( MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |
9520 MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |
9521 MYSQL_OPEN_IGNORE_FLUSH |
9522 MYSQL_LOCK_IGNORE_TIMEOUT |
9523 MYSQL_LOCK_LOG_TABLE);
9526 ulonglong save_utime_after_lock= thd->utime_after_lock;
9527 DBUG_ENTER(
"open_log_table");
9529 thd->reset_n_backup_open_tables_state(backup);
9531 if ((table= open_ltable(thd, one_table, one_table->lock_type, flags)))
9535 table->use_all_columns();
9539 thd->restore_backup_open_tables_state(backup);
9541 thd->utime_after_lock= save_utime_after_lock;
9554 close_system_tables(thd, backup);