69
30
Table *unused_tables; /* Used by mysql_test */
70
31
HASH open_cache; /* Used by mysql_test */
71
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
32
static HASH table_def_cache;
33
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
34
static pthread_mutex_t LOCK_table_share;
35
static bool table_def_inited= 0;
37
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
73
char *cache_key, uint32_t cache_key_length);
74
void free_cache_entry(void *entry);
75
unsigned char *table_cache_key(const unsigned char *record,
80
unsigned char *table_cache_key(const unsigned char *record,
39
char *cache_key, uint cache_key_length);
40
static void free_cache_entry(Table *entry);
41
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
45
extern "C" uchar *table_cache_key(const uchar *record, size_t *length,
46
bool not_used __attribute__((unused)))
84
48
Table *entry=(Table*) record;
85
49
*length= entry->s->table_cache_key.length;
86
return (unsigned char*) entry->s->table_cache_key.str;
89
HASH *get_open_cache()
50
return (uchar*) entry->s->table_cache_key.str;
95
54
bool table_cache_init(void)
97
return hash_init(&open_cache, &my_charset_bin,
98
(size_t) table_cache_size+16,
99
0, 0, table_cache_key,
100
free_cache_entry, 0);
56
return hash_init(&open_cache, &my_charset_bin, table_cache_size+16,
57
0, 0, table_cache_key,
58
(hash_free_key) free_cache_entry, 0);
103
61
void table_cache_free(void)
105
refresh_version++; // Force close of open tables
107
while (unused_tables)
108
hash_delete(&open_cache, (unsigned char*) unused_tables);
110
if (not open_cache.records) // Safety first
111
hash_free(&open_cache);
65
close_cached_tables(NULL, NULL, false, false, false);
66
if (!open_cache.records) // Safety first
67
hash_free(&open_cache);
114
uint32_t cached_open_tables(void)
72
uint cached_open_tables(void)
116
74
return open_cache.records;
121
Close cursor handle, but leave the table in the table cache
124
close_handle_and_leave_table_as_lock()
128
By leaving the table in the table cache, it disallows any other thread
131
session->killed will be set if we run out of memory
133
If closing a MERGE child, the calling function has to take care for
134
closing the parent too, if necessary.
78
Create a table cache key
81
create_table_def_key()
83
key Create key here (must be of size MAX_DBKEY_LENGTH)
84
table_list Table definition
85
tmp_table Set if table is a tmp table
88
The table cache_key is created from:
92
if the table is a tmp table, we add the following to make each tmp table
95
4 bytes for master thread id
96
4 bytes pseudo thread id
102
uint create_table_def_key(THD *thd, char *key, TableList *table_list,
105
uint key_length= (uint) (stpcpy(stpcpy(key, table_list->db)+1,
106
table_list->table_name)-key)+1;
109
int4store(key + key_length, thd->server_id);
110
int4store(key + key_length + 4, thd->variables.pseudo_thread_id);
111
key_length+= TMP_TABLE_KEY_EXTRA;
118
/*****************************************************************************
119
Functions to handle table definition cach (TABLE_SHARE)
120
*****************************************************************************/
122
extern "C" uchar *table_def_key(const uchar *record, size_t *length,
123
bool not_used __attribute__((unused)))
125
TABLE_SHARE *entry=(TABLE_SHARE*) record;
126
*length= entry->table_cache_key.length;
127
return (uchar*) entry->table_cache_key.str;
131
static void table_def_free_entry(TABLE_SHARE *share)
135
/* remove from old_unused_share list */
136
pthread_mutex_lock(&LOCK_table_share);
137
*share->prev= share->next;
138
share->next->prev= share->prev;
139
pthread_mutex_unlock(&LOCK_table_share);
141
free_table_share(share);
146
bool table_def_init(void)
149
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
150
oldest_unused_share= &end_of_unused_share;
151
end_of_unused_share.prev= &oldest_unused_share;
153
return hash_init(&table_def_cache, &my_charset_bin, table_def_size,
155
(hash_free_key) table_def_free_entry, 0);
159
void table_def_free(void)
161
if (table_def_inited)
164
pthread_mutex_destroy(&LOCK_table_share);
165
hash_free(&table_def_cache);
171
uint cached_table_definitions(void)
173
return table_def_cache.records;
178
Get TABLE_SHARE for a table.
182
table_list Table that should be opened
184
key_length Length of key
185
db_flags Flags to open_table_def():
187
error out: Error code from open_table_def()
190
Get a table definition from the table definition cache.
191
If it doesn't exist, create a new from the table definition file.
194
We must have wrlock on LOCK_open when we come here
195
(To be changed later)
202
TABLE_SHARE *get_table_share(THD *thd, TableList *table_list, char *key,
203
uint key_length, uint db_flags, int *error)
209
/* Read table definition from cache */
210
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
214
if (!(share= alloc_table_share(table_list, key, key_length)))
220
Lock mutex to be able to read table definition from file without
223
(void) pthread_mutex_lock(&share->mutex);
226
We assign a new table id under the protection of the LOCK_open and
227
the share's own mutex. We do this insted of creating a new mutex
228
and using it for the sole purpose of serializing accesses to a
229
static variable, we assign the table id here. We assign it to the
230
share before inserting it into the table_def_cache to be really
231
sure that it cannot be read from the cache without having a table
234
CAVEAT. This means that the table cannot be used for
235
binlogging/replication purposes, unless get_table_share() has been
236
called directly or indirectly.
238
assign_new_table_id(share);
240
if (my_hash_insert(&table_def_cache, (uchar*) share))
242
free_table_share(share);
243
return(0); // return error
245
if (open_table_def(thd, share, db_flags))
247
*error= share->error;
248
(void) hash_delete(&table_def_cache, (uchar*) share);
251
share->ref_count++; // Mark in use
252
(void) pthread_mutex_unlock(&share->mutex);
257
We found an existing table definition. Return it if we didn't get
258
an error when reading the table definition from file.
261
/* We must do a lock to ensure that the structure is initialized */
262
(void) pthread_mutex_lock(&share->mutex);
265
/* Table definition contained an error */
266
open_table_error(share, share->error, share->open_errno, share->errarg);
267
(void) pthread_mutex_unlock(&share->mutex);
271
if (!share->ref_count++ && share->prev)
274
Share was not used before and it was in the old_unused_share list
275
Unlink share from this list
277
pthread_mutex_lock(&LOCK_table_share);
278
*share->prev= share->next;
279
share->next->prev= share->prev;
282
pthread_mutex_unlock(&LOCK_table_share);
284
(void) pthread_mutex_unlock(&share->mutex);
286
/* Free cache if too big */
287
while (table_def_cache.records > table_def_size &&
288
oldest_unused_share->next)
290
pthread_mutex_lock(&oldest_unused_share->mutex);
291
VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
299
Get a table share. If it didn't exist, try creating it from engine
301
For arguments and return values, see get_table_from_share()
305
*get_table_share_with_create(THD *thd, TableList *table_list,
306
char *key, uint key_length,
307
uint db_flags, int *error)
312
share= get_table_share(thd, table_list, key, key_length, db_flags, error);
314
If share is not NULL, we found an existing share.
316
If share is NULL, and there is no error, we're inside
317
pre-locking, which silences 'ER_NO_SUCH_TABLE' errors
318
with the intention to silently drop non-existing tables
319
from the pre-locking list. In this case we still need to try
320
auto-discover before returning a NULL share.
322
If share is NULL and the error is ER_NO_SUCH_TABLE, this is
323
the same as above, only that the error was not silenced by
324
pre-locking. Once again, we need to try to auto-discover
327
Finally, if share is still NULL, it's a real error and we need
330
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
332
if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
336
/* Table didn't exist. Check if some engine can provide it */
337
if ((tmp= ha_create_table_from_engine(thd, table_list->db,
338
table_list->table_name)) < 0)
343
/* Give right error message */
345
my_printf_error(ER_UNKNOWN_ERROR,
346
"Failed to open '%-.64s', error while "
347
"unpacking from engine",
348
MYF(0), table_list->table_name);
351
/* Table existed in engine. Let's open it */
352
drizzle_reset_errors(thd, 1); // Clear warnings
353
thd->clear_error(); // Clear error message
354
return(get_table_share(thd, table_list, key, key_length,
360
Mark that we are not using table share anymore.
363
release_table_share()
365
release_type How the release should be done:
367
- Release without checking
368
RELEASE_WAIT_FOR_DROP
369
- Don't return until we get a signal that the
370
table is deleted or the thread is killed.
373
If ref_count goes to zero and (we have done a refresh or if we have
374
already too many open table shares) then delete the definition.
376
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
377
that the table is deleted or the thread is killed.
380
void release_table_share(TABLE_SHARE *share,
381
enum release_type type __attribute__((unused)))
383
bool to_be_deleted= 0;
385
safe_mutex_assert_owner(&LOCK_open);
387
pthread_mutex_lock(&share->mutex);
388
if (!--share->ref_count)
390
if (share->version != refresh_version)
394
/* Link share last in used_table_share list */
395
assert(share->next == 0);
396
pthread_mutex_lock(&LOCK_table_share);
397
share->prev= end_of_unused_share.prev;
398
*end_of_unused_share.prev= share;
399
end_of_unused_share.prev= &share->next;
400
share->next= &end_of_unused_share;
401
pthread_mutex_unlock(&LOCK_table_share);
403
to_be_deleted= (table_def_cache.records > table_def_size);
409
hash_delete(&table_def_cache, (uchar*) share);
412
pthread_mutex_unlock(&share->mutex);
418
Check if table definition exits in cache
421
get_cached_table_share()
423
table_name Table name
427
# TABLE_SHARE for table
430
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
432
char key[NAME_LEN*2+2];
433
TableList table_list;
435
safe_mutex_assert_owner(&LOCK_open);
437
table_list.db= (char*) db;
438
table_list.table_name= (char*) table_name;
439
key_length= create_table_def_key((THD*) 0, key, &table_list, 0);
440
return (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key, key_length);
445
Close file handle, but leave the table in the table cache
448
close_handle_and_leave_table_as_lock()
452
By leaving the table in the table cache, it disallows any other thread
455
thd->killed will be set if we run out of memory
457
If closing a MERGE child, the calling function has to take care for
458
closing the parent too, if necessary.
138
462
void close_handle_and_leave_table_as_lock(Table *table)
140
TableShare *share, *old_share= table->s;
464
TABLE_SHARE *share, *old_share= table->s;
142
memory::Root *mem_root= &table->mem_root;
466
MEM_ROOT *mem_root= &table->mem_root;
144
468
assert(table->db_stat);
156
480
memset(share, 0, sizeof(*share));
157
481
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
158
482
old_share->table_cache_key.length);
159
share->tmp_table= message::Table::INTERNAL; // for intern_close_table()
483
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
162
table->cursor->close();
163
table->db_stat= 0; // Mark cursor closed
164
TableShare::release(table->s);
486
table->file->close();
487
table->db_stat= 0; // Mark file closed
488
release_table_share(table->s, RELEASE_NORMAL);
166
table->cursor->change_table_ptr(table, table->s);
490
table->file->change_table_ptr(table, table->s);
498
Create a list for all open tables matching SQL expression
503
wild SQL like expression
506
One gets only a list of tables for which one has any kind of privilege.
507
db and table names are allocated in result struct, so one doesn't need
508
a lock on LOCK_open when traversing the return list.
511
NULL Error (Probably OOM)
512
# Pointer to list of names of open tables.
515
OPEN_TableList *list_open_tables(THD *thd __attribute__((unused)),
516
const char *db, const char *wild)
519
OPEN_TableList **start_list, *open_list;
520
TableList table_list;
522
VOID(pthread_mutex_lock(&LOCK_open));
523
memset(&table_list, 0, sizeof(table_list));
524
start_list= &open_list;
527
for (uint idx=0 ; result == 0 && idx < open_cache.records; idx++)
529
OPEN_TableList *table;
530
Table *entry=(Table*) hash_element(&open_cache,idx);
531
TABLE_SHARE *share= entry->s;
533
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
535
if (wild && wild_compare(share->table_name.str, wild, 0))
538
/* Check if user has SELECT privilege for any column in the table */
539
table_list.db= share->db.str;
540
table_list.table_name= share->table_name.str;
542
/* need to check if we haven't already listed it */
543
for (table= open_list ; table ; table=table->next)
545
if (!strcmp(table->table, share->table_name.str) &&
546
!strcmp(table->db, share->db.str))
550
if (entry->locked_by_name)
557
if (!(*start_list = (OPEN_TableList *)
558
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
560
open_list=0; // Out of memory
563
stpcpy((*start_list)->table=
564
stpcpy(((*start_list)->db= (char*) ((*start_list)+1)),
566
share->table_name.str);
567
(*start_list)->in_use= entry->in_use ? 1 : 0;
568
(*start_list)->locked= entry->locked_by_name ? 1 : 0;
569
start_list= &(*start_list)->next;
572
VOID(pthread_mutex_unlock(&LOCK_open));
170
576
/*****************************************************************************
171
577
* Functions to free open table cache
172
578
****************************************************************************/
175
void Table::intern_close_table()
581
void intern_close_table(Table *table)
176
582
{ // Free all structures
178
if (cursor) // Not true if name lock
179
closefrm(true); // close cursor
583
free_io_cache(table);
584
if (table->file) // Not true if name lock
585
VOID(closefrm(table, 1)); // close file
183
590
Remove table from the open table cache
187
entry Table to remove
594
table Table to remove
190
We need to have a lock on LOCK_open when calling this
597
We need to have a lock on LOCK_open when calling this
193
void free_cache_entry(void *entry)
600
static void free_cache_entry(Table *table)
195
Table *table= static_cast<Table *>(entry);
196
table->intern_close_table();
197
if (not table->in_use)
602
intern_close_table(table);
199
605
table->next->prev=table->prev; /* remove from used chain */
200
606
table->prev->next=table->next;
373
796
table->s->version= refresh_version;
377
pthread_mutex_unlock(&LOCK_open);
800
VOID(pthread_mutex_unlock(&LOCK_open));
379
801
if (wait_for_refresh)
381
pthread_mutex_lock(&session->mysys_var->mutex);
382
session->mysys_var->current_mutex= 0;
383
session->mysys_var->current_cond= 0;
384
session->set_proc_info(0);
385
pthread_mutex_unlock(&session->mysys_var->mutex);
393
move one table to free list
396
bool Session::free_cached_table()
398
bool found_old_table= false;
399
Table *table= open_tables;
401
safe_mutex_assert_owner(&LOCK_open);
803
pthread_mutex_lock(&thd->mysys_var->mutex);
804
thd->mysys_var->current_mutex= 0;
805
thd->mysys_var->current_cond= 0;
806
thd_proc_info(thd, 0);
807
pthread_mutex_unlock(&thd->mysys_var->mutex);
814
Close all tables which match specified connection string or
815
if specified string is NULL, then any table with a connection string.
818
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
819
LEX_STRING *connection, bool have_lock)
822
TableList tmp, *tables= NULL;
826
memset(&tmp, 0, sizeof(TableList));
829
VOID(pthread_mutex_lock(&LOCK_open));
831
for (idx= 0; idx < table_def_cache.records; idx++)
833
TABLE_SHARE *share= (TABLE_SHARE *) hash_element(&table_def_cache, idx);
835
/* Ignore if table is not open or does not have a connect_string */
836
if (!share->connect_string.length || !share->ref_count)
839
/* Compare the connection string */
841
(connection->length > share->connect_string.length ||
842
(connection->length < share->connect_string.length &&
843
(share->connect_string.str[connection->length] != '/' &&
844
share->connect_string.str[connection->length] != '\\')) ||
845
strncasecmp(connection->str, share->connect_string.str,
846
connection->length)))
849
/* close_cached_tables() only uses these elements */
850
tmp.db= share->db.str;
851
tmp.table_name= share->table_name.str;
852
tmp.next_local= tables;
854
tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp,
859
result= close_cached_tables(thd, tables, true, false, false);
862
VOID(pthread_mutex_unlock(&LOCK_open));
864
if (if_wait_for_refresh)
866
pthread_mutex_lock(&thd->mysys_var->mutex);
867
thd->mysys_var->current_mutex= 0;
868
thd->mysys_var->current_cond= 0;
869
thd->set_proc_info(0);
870
pthread_mutex_unlock(&thd->mysys_var->mutex);
878
Mark all temporary tables which were used by the current statement or
879
substatement as free for reuse, but only if the query_id can be cleared.
881
@param thd thread context
883
@remark For temp tables associated with a open SQL HANDLER the query_id
884
is not reset until the HANDLER is closed.
887
static void mark_temp_tables_as_free_for_reuse(THD *thd)
889
for (Table *table= thd->temporary_tables ; table ; table= table->next)
891
if ((table->query_id == thd->query_id) && ! table->open_by_handler)
894
table->file->ha_reset();
901
Mark all tables in the list which were used by current substatement
905
mark_used_tables_as_free_for_reuse()
907
table - head of the list of tables
910
Marks all tables in the list which were used by current substatement
911
(they are marked by its query_id) as free for reuse.
914
The reason we reset query_id is that it's not enough to just test
915
if table->query_id != thd->query_id to know if a table is in use.
918
SELECT f1_that_uses_t1() FROM t1;
919
In f1_that_uses_t1() we will see one instance of t1 where query_id is
920
set to query_id of original query.
923
static void mark_used_tables_as_free_for_reuse(THD *thd, Table *table)
925
for (; table ; table= table->next)
927
if (table->query_id == thd->query_id)
930
table->file->ha_reset();
937
Auxiliary function to close all tables in the open_tables list.
939
@param thd Thread context.
941
@remark It should not ordinarily be called directly.
944
static void close_open_tables(THD *thd)
946
bool found_old_table= 0;
948
safe_mutex_assert_not_owner(&LOCK_open);
950
VOID(pthread_mutex_lock(&LOCK_open));
952
while (thd->open_tables)
953
found_old_table|= close_thread_table(thd, &thd->open_tables);
954
thd->some_tables_deleted= 0;
956
/* Free tables to hold down open files */
957
while (open_cache.records > table_cache_size && unused_tables)
958
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
961
/* Tell threads waiting for refresh that something has happened */
965
VOID(pthread_mutex_unlock(&LOCK_open));
970
Close all tables used by the current substatement, or all tables
971
used by this thread if we are on the upper level.
974
close_thread_tables()
978
Unlocks tables and frees derived tables.
979
Put all normal tables used by thread in free list.
981
It will only close/mark as free for reuse tables opened by this
982
substatement, it will also check if we are closing tables after
983
execution of complete query (i.e. we are on upper level) and will
984
leave prelocked mode if needed.
987
void close_thread_tables(THD *thd)
992
We are assuming here that thd->derived_tables contains ONLY derived
993
tables for this substatement. i.e. instead of approach which uses
994
query_id matching for determining which of the derived tables belong
995
to this substatement we rely on the ability of substatements to
996
save/restore thd->derived_tables during their execution.
998
TODO: Probably even better approach is to simply associate list of
999
derived tables with (sub-)statement instead of thread and destroy
1000
them at the end of its execution.
1002
if (thd->derived_tables)
1006
Close all derived tables generated in queries like
1007
SELECT * FROM (SELECT * FROM t1)
1009
for (table= thd->derived_tables ; table ; table= next)
1012
table->free_tmp_table(thd);
1014
thd->derived_tables= 0;
1018
Mark all temporary tables used by this statement as free for reuse.
1020
mark_temp_tables_as_free_for_reuse(thd);
1022
Let us commit transaction for statement. Since in 5.0 we only have
1023
one statement transaction and don't allow several nested statement
1024
transactions this call will do nothing if we are inside of stored
1025
function or trigger (i.e. statement transaction is already active and
1026
does not belong to statement for which we do close_thread_tables()).
1027
TODO: This should be fixed in later releases.
1029
if (!(thd->state_flags & Open_tables_state::BACKUPS_AVAIL))
1031
thd->main_da.can_overwrite_status= true;
1032
ha_autocommit_or_rollback(thd, thd->is_error());
1033
thd->main_da.can_overwrite_status= false;
1034
thd->transaction.stmt.reset();
1037
if (thd->locked_tables)
1040
/* Ensure we are calling ha_reset() for all used tables */
1041
mark_used_tables_as_free_for_reuse(thd, thd->open_tables);
1044
We are under simple LOCK TABLES so should not do anything else.
1052
For RBR we flush the pending event just before we unlock all the
1053
tables. This means that we are at the end of a topmost
1054
statement, so we ensure that the STMT_END_F flag is set on the
1055
pending event. For statements that are *inside* stored
1056
functions, the pending event will not be flushed: that will be
1057
handled either before writing a query log event (inside
1058
binlog_query()) or when preparing a pending event.
1060
thd->binlog_flush_pending_rows_event(true);
1061
mysql_unlock_tables(thd, thd->lock);
1065
Note that we need to hold LOCK_open while changing the
1066
open_tables list. Another thread may work on it.
1067
(See: remove_table_from_cache(), mysql_wait_completed_table())
1068
Closing a MERGE child before the parent would be fatal if the
1069
other thread tries to abort the MERGE lock in between.
1071
if (thd->open_tables)
1072
close_open_tables(thd);
1078
/* move one table to free list */
1080
bool close_thread_table(THD *thd, Table **table_ptr)
1082
bool found_old_table= 0;
1083
Table *table= *table_ptr;
402
1085
assert(table->key_read == 0);
403
assert(!table->cursor || table->cursor->inited == Cursor::NONE);
1086
assert(!table->file || table->file->inited == handler::NONE);
405
open_tables= table->next;
1088
*table_ptr=table->next;
407
1090
if (table->needs_reopen_or_name_lock() ||
408
version != refresh_version || !table->db_stat)
1091
thd->version != refresh_version || !table->db_stat)
410
hash_delete(&open_cache,(unsigned char*) table);
411
found_old_table= true;
1093
VOID(hash_delete(&open_cache,(uchar*) table));
416
1099
Open placeholders have Table::db_stat set to 0, so they should be
417
1100
handled by the first alternative.
419
assert(not table->open_placeholder);
1102
assert(!table->open_placeholder);
421
1104
/* Free memory and reset for next loop */
422
table->cursor->ha_reset();
423
table->in_use= false;
1105
table->file->ha_reset();
425
1107
if (unused_tables)
427
table->next= unused_tables; /* Link in last */
428
table->prev= unused_tables->prev;
429
unused_tables->prev= table;
430
table->prev->next= table;
1109
table->next=unused_tables; /* Link in last */
1110
table->prev=unused_tables->prev;
1111
unused_tables->prev=table;
1112
table->prev->next=table;
433
unused_tables= table->next=table->prev=table;
1115
unused_tables=table->next=table->prev=table;
436
return found_old_table;
441
Auxiliary function to close all tables in the open_tables list.
443
@param session Thread context.
445
@remark It should not ordinarily be called directly.
1117
return(found_old_table);
1121
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1122
static inline uint tmpkeyval(THD *thd __attribute__((unused)),
1125
return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1130
Close all temporary tables created by 'CREATE TEMPORARY TABLE' for thread
1131
creates one DROP TEMPORARY Table binlog event for each pseudo-thread
448
void Session::close_open_tables()
1134
void close_temporary_tables(THD *thd)
450
bool found_old_table= false;
452
safe_mutex_assert_not_owner(&LOCK_open);
454
pthread_mutex_lock(&LOCK_open); /* Close all open tables on Session */
457
found_old_table|= free_cached_table();
458
some_tables_deleted= false;
462
/* Tell threads waiting for refresh that something has happened */
466
pthread_mutex_unlock(&LOCK_open);
1139
/* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */
1140
bool was_quote_show= true;
1142
if (!thd->temporary_tables)
1145
if (!mysql_bin_log.is_open() || thd->current_stmt_binlog_row_based)
1148
for (table= thd->temporary_tables; table; table= tmp_next)
1150
tmp_next= table->next;
1151
close_temporary(table, 1, 1);
1153
thd->temporary_tables= 0;
1157
/* Better add "if exists", in case a RESET MASTER has been done */
1158
const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1159
uint stub_len= sizeof(stub) - 1;
1161
String s_query= String(buf, sizeof(buf), system_charset_info);
1162
bool found_user_tables= false;
1164
memcpy(buf, stub, stub_len);
1167
Insertion sort of temp tables by pseudo_thread_id to build ordered list
1168
of sublists of equal pseudo_thread_id
1171
for (prev_table= thd->temporary_tables, table= prev_table->next;
1173
prev_table= table, table= table->next)
1175
Table *prev_sorted /* same as for prev_table */, *sorted;
1176
if (is_user_table(table))
1178
if (!found_user_tables)
1179
found_user_tables= true;
1180
for (prev_sorted= NULL, sorted= thd->temporary_tables; sorted != table;
1181
prev_sorted= sorted, sorted= sorted->next)
1183
if (!is_user_table(sorted) ||
1184
tmpkeyval(thd, sorted) > tmpkeyval(thd, table))
1186
/* move into the sorted part of the list from the unsorted */
1187
prev_table->next= table->next;
1188
table->next= sorted;
1191
prev_sorted->next= table;
1195
thd->temporary_tables= table;
1204
/* We always quote db,table names though it is slight overkill */
1205
if (found_user_tables &&
1206
!(was_quote_show= test(thd->options & OPTION_QUOTE_SHOW_CREATE)))
1208
thd->options |= OPTION_QUOTE_SHOW_CREATE;
1211
/* scan sorted tmps to generate sequence of DROP */
1212
for (table= thd->temporary_tables; table; table= next)
1214
if (is_user_table(table))
1216
my_thread_id save_pseudo_thread_id= thd->variables.pseudo_thread_id;
1217
/* Set pseudo_thread_id to be that of the processed table */
1218
thd->variables.pseudo_thread_id= tmpkeyval(thd, table);
1220
Loop forward through all tables within the sublist of
1221
common pseudo_thread_id to create single DROP query.
1223
for (s_query.length(stub_len);
1224
table && is_user_table(table) &&
1225
tmpkeyval(thd, table) == thd->variables.pseudo_thread_id;
1229
We are going to add 4 ` around the db/table names and possible more
1230
due to special characters in the names
1232
append_identifier(thd, &s_query, table->s->db.str, strlen(table->s->db.str));
1233
s_query.append('.');
1234
append_identifier(thd, &s_query, table->s->table_name.str,
1235
strlen(table->s->table_name.str));
1236
s_query.append(',');
1238
close_temporary(table, 1, 1);
1241
const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
1242
thd->variables.character_set_client= system_charset_info;
1243
Query_log_event qinfo(thd, s_query.ptr(),
1244
s_query.length() - 1 /* to remove trailing ',' */,
1246
thd->variables.character_set_client= cs_save;
1248
Imagine the thread had created a temp table, then was doing a
1249
SELECT, and the SELECT was killed. Then it's not clever to
1250
mark the statement above as "killed", because it's not really
1251
a statement updating data, and there are 99.99% chances it
1252
will succeed on slave. If a real update (one updating a
1253
persistent table) was killed on the master, then this real
1254
update will be logged with error_code=killed, rightfully
1255
causing the slave to stop.
1257
qinfo.error_code= 0;
1258
mysql_bin_log.write(&qinfo);
1259
thd->variables.pseudo_thread_id= save_pseudo_thread_id;
1264
close_temporary(table, 1, 1);
1267
if (!was_quote_show)
1268
thd->options&= ~OPTION_QUOTE_SHOW_CREATE; /* restore option */
1269
thd->temporary_tables=0;
470
1273
Find table in list.
474
table Pointer to table list
475
offset Offset to which list in table structure to use
476
db_name Data base name
477
table_name Table name
480
This is called by find_table_in_global_list().
484
# Pointer to found table.
1276
find_table_in_list()
1277
table Pointer to table list
1278
offset Offset to which list in table structure to use
1279
db_name Data base name
1280
table_name Table name
1283
This is called by find_table_in_local_list() and
1284
find_table_in_global_list().
1287
NULL Table not found
1288
# Pointer to found table.
487
1291
TableList *find_table_in_list(TableList *table,
488
TableList *TableList::*link,
490
const char *table_name)
1292
TableList *TableList::*link,
1293
const char *db_name,
1294
const char *table_name)
492
1296
for (; table; table= table->*link )
494
if ((table->table == 0 || table->table->s->tmp_table == message::Table::STANDARD) &&
495
strcasecmp(table->db, db_name) == 0 &&
496
strcasecmp(table->table_name, table_name) == 0)
1298
if ((table->table == 0 || table->table->s->tmp_table == NO_TMP_TABLE) &&
1299
strcmp(table->db, db_name) == 0 &&
1300
strcmp(table->table_name, table_name) == 0)
589
void Session::doGetTableNames(SchemaIdentifier &schema_identifier,
590
std::set<std::string>& set_of_names)
592
for (Table *table= temporary_tables ; table ; table= table->next)
594
if (schema_identifier.compare(table->s->getSchemaName()))
596
set_of_names.insert(table->s->table_name.str);
601
void Session::doGetTableNames(CachedDirectory &,
602
SchemaIdentifier &schema_identifier,
603
std::set<std::string> &set_of_names)
605
doGetTableNames(schema_identifier, set_of_names);
608
void Session::doGetTableIdentifiers(SchemaIdentifier &schema_identifier,
609
TableIdentifiers &set_of_identifiers)
611
for (Table *table= temporary_tables ; table ; table= table->next)
613
if (schema_identifier.compare(table->s->getSchemaName()))
615
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
616
table->getShare()->getTableName(),
617
table->getShare()->getPath()));
622
void Session::doGetTableIdentifiers(CachedDirectory &,
623
SchemaIdentifier &schema_identifier,
624
TableIdentifiers &set_of_identifiers)
626
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
629
bool Session::doDoesTableExist(TableIdentifier &identifier)
631
for (Table *table= temporary_tables ; table ; table= table->next)
633
if (table->s->tmp_table == message::Table::TEMPORARY)
635
if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
645
int Session::doGetTableDefinition(TableIdentifier &identifier,
646
message::Table &table_proto)
648
for (Table *table= temporary_tables ; table ; table= table->next)
650
if (table->s->tmp_table == message::Table::TEMPORARY)
652
if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
654
table_proto.CopyFrom(*(table->s->getTableProto()));
664
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
666
char key[MAX_DBKEY_LENGTH];
669
key_length= TableShare::createKey(key, new_db, table_name);
671
for (Table *table= temporary_tables ; table ; table= table->next)
673
if (table->s->table_cache_key.length == key_length &&
674
not memcmp(table->s->table_cache_key.str, key, key_length))
679
return NULL; // Not a temporary table
682
Table *Session::find_temporary_table(TableList *table_list)
684
return find_temporary_table(table_list->db, table_list->table_name);
687
Table *Session::find_temporary_table(TableIdentifier &identifier)
689
char key[MAX_DBKEY_LENGTH];
692
key_length= TableShare::createKey(key, identifier);
694
for (Table *table= temporary_tables ; table ; table= table->next)
696
if (table->s->table_cache_key.length == key_length &&
697
not memcmp(table->s->table_cache_key.str, key, key_length))
702
return NULL; // Not a temporary table
1397
Issue correct error message in case we found 2 duplicate tables which
1398
prevent some update operation
1401
update_non_unique_table_error()
1402
update table which we try to update
1403
operation name of update operation
1404
duplicate duplicate table which we found
1407
here we hide view underlying tables if we have them
1410
void update_non_unique_table_error(TableList *update,
1411
const char *operation __attribute__((unused)),
1412
TableList *duplicate __attribute__((unused)))
1414
my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1418
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
1420
TableList table_list;
1422
table_list.db= (char*) db;
1423
table_list.table_name= (char*) table_name;
1424
return find_temporary_table(thd, &table_list);
1428
Table *find_temporary_table(THD *thd, TableList *table_list)
1430
char key[MAX_DBKEY_LENGTH];
1434
key_length= create_table_def_key(thd, key, table_list, 1);
1435
for (table=thd->temporary_tables ; table ; table= table->next)
1437
if (table->s->table_cache_key.length == key_length &&
1438
!memcmp(table->s->table_cache_key.str, key, key_length))
1441
return(0); // Not a temporary table
707
1446
Drop a temporary table.
709
Try to locate the table in the list of session->temporary_tables.
1448
Try to locate the table in the list of thd->temporary_tables.
710
1449
If the table is found:
711
- if the table is being used by some outer statement, fail.
712
- if the table is in session->locked_tables, unlock it and
713
remove it from the list of locked tables. Currently only transactional
714
temporary tables are present in the locked_tables list.
715
- Close the temporary table, remove its .FRM
716
- remove the table from the list of temporary tables
1450
- if the table is being used by some outer statement, fail.
1451
- if the table is in thd->locked_tables, unlock it and
1452
remove it from the list of locked tables. Currently only transactional
1453
temporary tables are present in the locked_tables list.
1454
- Close the temporary table, remove its .FRM
1455
- remove the table from the list of temporary tables
718
1457
This function is used to drop user temporary tables, as well as
719
1458
internal tables created in CREATE TEMPORARY TABLE ... SELECT
720
1459
or ALTER Table. Even though part of the work done by this function
721
1460
is redundant when the table is internal, as long as we
722
1461
link both internal and user temporary tables into the same
723
session->temporary_tables list, it's impossible to tell here whether
1462
thd->temporary_tables list, it's impossible to tell here whether
724
1463
we're dealing with an internal or a user temporary table.
726
1465
@retval 0 the table was found and dropped successfully.
727
1466
@retval 1 the table was not found in the list of temporary tables
729
1468
@retval -1 the table is in use by a outer query
732
int Session::drop_temporary_table(TableList *table_list)
1471
int drop_temporary_table(THD *thd, TableList *table_list)
736
if (not (table= find_temporary_table(table_list)))
1475
if (!(table= find_temporary_table(thd, table_list)))
739
1478
/* Table might be in use by some outer statement. */
740
if (table->query_id && table->query_id != query_id)
1479
if (table->query_id && table->query_id != thd->query_id)
742
1481
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
746
close_temporary_table(table);
752
/* move table first in unused links */
1486
If LOCK TABLES list is not empty and contains this table,
1487
unlock the table and remove the table from this list.
1489
mysql_lock_remove(thd, thd->locked_tables, table, false);
1490
close_temporary_table(thd, table, 1, 1);
1495
unlink from thd->temporary tables and close temporary table
1498
void close_temporary_table(THD *thd, Table *table,
1499
bool free_share, bool delete_table)
1503
table->prev->next= table->next;
1504
if (table->prev->next)
1505
table->next->prev= table->prev;
1509
/* removing the item from the list */
1510
assert(table == thd->temporary_tables);
1512
slave must reset its temporary list pointer to zero to exclude
1513
passing non-zero value to end_slave via rli->save_temporary_tables
1514
when no temp tables opened, see an invariant below.
1516
thd->temporary_tables= table->next;
1517
if (thd->temporary_tables)
1518
table->next->prev= 0;
1520
if (thd->slave_thread)
1522
/* natural invariant of temporary_tables */
1523
assert(slave_open_temp_tables || !thd->temporary_tables);
1524
slave_open_temp_tables--;
1526
close_temporary(table, free_share, delete_table);
1532
Close and delete a temporary table
1535
This dosn't unlink table from thd->temporary
1536
If this is needed, use close_temporary_table()
1539
void close_temporary(Table *table, bool free_share, bool delete_table)
1541
handlerton *table_type= table->s->db_type();
1543
free_io_cache(table);
1546
Check that temporary table has not been created with
1547
frm_only because it has then not been created in any storage engine
1550
rm_temporary_table(table_type, table->s->path.str,
1551
table->s->tmp_table == TMP_TABLE_FRM_FILE_ONLY);
1554
free_table_share(table->s);
1555
my_free((char*) table,MYF(0));
1562
Used by ALTER Table when the table is a temporary one. It changes something
1563
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1565
Prepares a table cache key, which is the concatenation of db, table_name and
1566
thd->slave_proxy_id, separated by '\0'.
1569
bool rename_temporary_table(THD* thd, Table *table, const char *db,
1570
const char *table_name)
1574
TABLE_SHARE *share= table->s;
1575
TableList table_list;
1577
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1578
return(1); /* purecov: inspected */
1580
table_list.db= (char*) db;
1581
table_list.table_name= (char*) table_name;
1582
key_length= create_table_def_key(thd, key, &table_list, 1);
1583
share->set_table_cache_key(key, key_length);
1588
/* move table first in unused links */
754
1590
static void relink_unused(Table *table)
812
1658
// Notify any 'refresh' threads
813
1659
broadcast_refresh();
818
Auxiliary routine which closes and drops open table.
820
@param session Thread handle
821
@param table Table object for table to be dropped
822
@param db_name Name of database for this table
823
@param table_name Name of this table
825
@note This routine assumes that table to be closed is open only
826
by calling thread so we needn't wait until other threads
827
will close the table. Also unless called under implicit or
828
explicit LOCK TABLES mode it assumes that table to be
829
dropped is already unlocked. In the former case it will
830
also remove lock on the table. But one should not rely on
831
this behaviour as it may change in future.
832
Currently, however, this function is never called for a
833
table that was locked with LOCK TABLES.
1665
Auxiliary routine which closes and drops open table.
1667
@param thd Thread handle
1668
@param table Table object for table to be dropped
1669
@param db_name Name of database for this table
1670
@param table_name Name of this table
1672
@note This routine assumes that table to be closed is open only
1673
by calling thread so we needn't wait until other threads
1674
will close the table. Also unless called under implicit or
1675
explicit LOCK TABLES mode it assumes that table to be
1676
dropped is already unlocked. In the former case it will
1677
also remove lock on the table. But one should not rely on
1678
this behaviour as it may change in future.
1679
Currently, however, this function is never called for a
1680
table that was locked with LOCK TABLES.
836
void Session::drop_open_table(Table *table, TableIdentifier &identifier)
1683
void drop_open_table(THD *thd, Table *table, const char *db_name,
1684
const char *table_name)
838
1686
if (table->s->tmp_table)
840
close_temporary_table(table);
1687
close_temporary_table(thd, table, 1, 1);
844
pthread_mutex_lock(&LOCK_open); /* Close and drop a table (AUX routine) */
1690
handlerton *table_type= table->s->db_type();
1691
VOID(pthread_mutex_lock(&LOCK_open));
846
1693
unlink_open_table() also tells threads waiting for refresh or close
847
1694
that something has happened.
849
unlink_open_table(table);
850
quick_rm_table(*this, identifier);
851
pthread_mutex_unlock(&LOCK_open);
1696
unlink_open_table(thd, table, false);
1697
quick_rm_table(table_type, db_name, table_name, 0);
1698
VOID(pthread_mutex_unlock(&LOCK_open));
857
Wait for condition but allow the user to send a kill to mysqld
1704
Wait for condition but allow the user to send a kill to mysqld
861
session Thread Cursor
862
mutex mutex that is currently hold that is associated with condition
863
Will be unlocked on return
864
cond Condition to wait for
1707
wait_for_condition()
1709
mutex mutex that is currently hold that is associated with condition
1710
Will be unlocked on return
1711
cond Condition to wait for
867
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
1714
void wait_for_condition(THD *thd, pthread_mutex_t *mutex, pthread_cond_t *cond)
869
1716
/* Wait until the current table is up to date */
870
const char *saved_proc_info;
871
mysys_var->current_mutex= mutex;
872
mysys_var->current_cond= cond;
873
saved_proc_info= get_proc_info();
874
set_proc_info("Waiting for table");
1717
const char *proc_info;
1718
thd->mysys_var->current_mutex= mutex;
1719
thd->mysys_var->current_cond= cond;
1720
proc_info=thd->get_proc_info();
1721
thd_proc_info(thd, "Waiting for table");
876
1723
(void) pthread_cond_wait(cond, mutex);
944
1825
object to its original state.
946
1827
*table= orig_table;
950
1831
share= table->s;
952
1833
We want to prevent other connections from opening this table until end
953
1834
of statement as it is likely that modifications of table's metadata are
954
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
1835
not yet finished (for example CREATE TRIGGER have to change .TRG file,
955
1836
or we might want to drop table if CREATE TABLE ... SELECT fails).
956
1837
This also allows us to assume that no other connection will sneak in
957
1838
before we will get table-level lock on this table.
959
1840
share->version=0;
960
table->in_use = this;
1841
table->in_use = thd;
964
table->next= open_tables;
1845
table->next= thd->open_tables;
1846
thd->open_tables= table;
970
Table object should be already in Session::open_tables list so we just
1851
Table object should be already in THD::open_tables list so we just
971
1852
need to set Table::next correctly.
973
1854
table->next= orig_table.next;
976
table->tablenr= current_tablenr++;
977
table->used_fields= 0;
978
table->const_table= 0;
1857
table->tablenr=thd->current_tablenr++;
1858
table->used_fields=0;
1859
table->const_table=0;
979
1860
table->null_row= false;
980
1861
table->maybe_null= false;
981
1862
table->force_index= false;
982
table->status= STATUS_NO_RECORD;
1863
table->status=STATUS_NO_RECORD;
989
Create and insert into table cache placeholder for table
990
which will prevent its opening (or creation) (a.k.a lock
993
@param session Thread context
994
@param key Table cache key for name to be locked
995
@param key_length Table cache key length
997
@return Pointer to Table object used for name locking or 0 in
1869
Create and insert into table cache placeholder for table
1870
which will prevent its opening (or creation) (a.k.a lock
1873
@param thd Thread context
1874
@param key Table cache key for name to be locked
1875
@param key_length Table cache key length
1877
@return Pointer to Table object used for name locking or 0 in
1001
Table *Session::table_cache_insert_placeholder(const char *key, uint32_t key_length)
1881
Table *table_cache_insert_placeholder(THD *thd, const char *key,
1005
1886
char *key_buff;
1007
1888
safe_mutex_assert_owner(&LOCK_open);
1010
1891
Create a table entry with the right key and with an old refresh version
1011
Note that we must use multi_malloc() here as this is freed by the
1892
Note that we must use my_multi_malloc() here as this is freed by the
1014
if (! memory::multi_malloc(true,
1015
&table, sizeof(*table),
1016
&share, sizeof(*share),
1017
&key_buff, key_length,
1895
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
1896
&table, sizeof(*table),
1897
&share, sizeof(*share),
1898
&key_buff, key_length,
1021
1902
table->s= share;
1022
1903
share->set_table_cache_key(key_buff, key, key_length);
1023
share->tmp_table= message::Table::INTERNAL; // for intern_close_table
1024
table->in_use= this;
1904
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1025
1906
table->locked_by_name=1;
1027
if (my_hash_insert(&open_cache, (unsigned char*)table))
1908
if (my_hash_insert(&open_cache, (uchar*)table))
1029
free((unsigned char*) table);
1910
my_free((uchar*) table, MYF(0));
1038
Obtain an exclusive name lock on the table if it is not cached
1041
@param session Thread context
1042
@param db Name of database
1043
@param table_name Name of table
1044
@param[out] table Out parameter which is either:
1045
- set to NULL if table cache contains record for
1047
- set to point to the Table instance used for
1050
@note This function takes into account all records for table in table
1051
cache, even placeholders used for name-locking. This means that
1052
'table' parameter can be set to NULL for some situations when
1053
table does not really exist.
1055
@retval true Error occured (OOM)
1056
@retval false Success. 'table' parameter set according to above rules.
1919
Obtain an exclusive name lock on the table if it is not cached
1922
@param thd Thread context
1923
@param db Name of database
1924
@param table_name Name of table
1925
@param[out] table Out parameter which is either:
1926
- set to NULL if table cache contains record for
1928
- set to point to the Table instance used for
1931
@note This function takes into account all records for table in table
1932
cache, even placeholders used for name-locking. This means that
1933
'table' parameter can be set to NULL for some situations when
1934
table does not really exist.
1936
@retval true Error occured (OOM)
1937
@retval false Success. 'table' parameter set according to above rules.
1058
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
1060
return lock_table_name_if_not_cached(identifier.getSchemaName().c_str(), identifier.getTableName().c_str(), table);
1063
bool Session::lock_table_name_if_not_cached(const char *new_db,
1064
const char *table_name, Table **table)
1940
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1941
const char *table_name, Table **table)
1066
1943
char key[MAX_DBKEY_LENGTH];
1068
uint32_t key_length;
1070
key_pos= strcpy(key_pos, new_db) + strlen(new_db);
1071
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
1072
key_length= (uint32_t) (key_pos-key)+1;
1074
pthread_mutex_lock(&LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table) */
1076
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1946
key_length= (uint)(stpcpy(stpcpy(key, db) + 1, table_name) - key) + 1;
1947
VOID(pthread_mutex_lock(&LOCK_open));
1949
if (hash_search(&open_cache, (uchar *)key, key_length))
1078
pthread_mutex_unlock(&LOCK_open);
1951
VOID(pthread_mutex_unlock(&LOCK_open));
1082
if (not (*table= table_cache_insert_placeholder(key, key_length)))
1084
pthread_mutex_unlock(&LOCK_open);
1087
(*table)->open_placeholder= true;
1088
(*table)->next= open_tables;
1089
open_tables= *table;
1090
pthread_mutex_unlock(&LOCK_open);
1955
if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1957
VOID(pthread_mutex_unlock(&LOCK_open));
1960
(*table)->open_placeholder= 1;
1961
(*table)->next= thd->open_tables;
1962
thd->open_tables= *table;
1963
VOID(pthread_mutex_unlock(&LOCK_open));
1969
Check that table exists in table definition cache, on disk
1970
or in some storage engine.
1972
@param thd Thread context
1973
@param table Table list element
1974
@param[out] exists Out parameter which is set to true if table
1975
exists and to false otherwise.
1977
@note This function assumes that caller owns LOCK_open mutex.
1978
It also assumes that the fact that there are no name-locks
1979
on the table was checked beforehand.
1981
@note If there is no .FRM file for the table but it exists in one
1982
of engines (e.g. it was created on another node of NDB cluster)
1983
this function will fetch and create proper .FRM file for it.
1985
@retval true Some error occured
1986
@retval false No error. 'exists' out parameter set accordingly.
1989
bool check_if_table_exists(THD *thd, TableList *table, bool *exists)
1991
char path[FN_REFLEN];
1994
safe_mutex_assert_owner(&LOCK_open);
1998
if (get_cached_table_share(table->db, table->table_name))
2001
build_table_filename(path, sizeof(path) - 1, table->db, table->table_name,
2004
if (!access(path, F_OK))
2007
/* .FRM file doesn't exist. Check if some engine can provide it. */
2009
rc= ha_create_table_from_engine(thd, table->db, table->table_name);
2013
/* Table does not exists in engines as well. */
2019
/* Table exists in some engine and .FRM for it was created. */
2024
my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while "
2025
"unpacking from engine", MYF(0), table->table_name);
1100
session Thread context.
1101
table_list Open first table in list.
1102
refresh INOUT Pointer to memory that will be set to 1 if
1103
we need to close all tables and reopen them.
1104
If this is a NULL pointer, then the table is not
1105
put in the thread-open-list.
1106
flags Bitmap of flags to modify how open works:
1107
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1108
someone has done a flush or namelock on it.
1109
No version number checking is done.
1110
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1111
table not the base table or view.
2037
table_list Open first table in list.
2038
refresh INOUT Pointer to memory that will be set to 1 if
2039
we need to close all tables and reopen them.
2040
If this is a NULL pointer, then the table is not
2041
put in the thread-open-list.
2042
flags Bitmap of flags to modify how open works:
2043
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
2044
someone has done a flush or namelock on it.
2045
No version number checking is done.
2046
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
2047
table not the base table or view.
1114
Uses a cache of open tables to find a table not in use.
2050
Uses a cache of open tables to find a table not in use.
1116
If table list element for the table to be opened has "create" flag
1117
set and table does not exist, this function will automatically insert
1118
a placeholder for exclusive name lock into the open tables cache and
1119
will return the Table instance that corresponds to this placeholder.
2052
If table list element for the table to be opened has "create" flag
2053
set and table does not exist, this function will automatically insert
2054
a placeholder for exclusive name lock into the open tables cache and
2055
will return the Table instance that corresponds to this placeholder.
1122
NULL Open failed. If refresh is set then one should close
1123
all other tables and retry the open.
1124
# Success. Pointer to Table object for open table.
2058
NULL Open failed. If refresh is set then one should close
2059
all other tables and retry the open.
2060
# Success. Pointer to Table object for open table.
1128
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
2064
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint flags)
2066
register Table *table;
1131
2067
char key[MAX_DBKEY_LENGTH];
1132
2068
unsigned int key_length;
1133
const char *alias= table_list->alias;
2069
char *alias= table_list->alias;
1134
2070
HASH_SEARCH_STATE state;
1136
/* Parsing of partitioning information from .frm needs session->lex set up. */
1137
assert(lex->is_lex_started);
2072
/* Parsing of partitioning information from .frm needs thd->lex set up. */
2073
assert(thd->lex->is_lex_started);
1139
2075
/* find a unused table in the open table cache */
1143
2079
/* an open table operation needs a lot of the stack space */
1144
if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
1150
key_length= table_list->create_table_def_key(key);
2080
if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
2086
key_length= (create_table_def_key(thd, key, table_list, 1) -
2087
TMP_TABLE_KEY_EXTRA);
1153
2090
Unless requested otherwise, try to resolve this table in the list
1154
2091
of temporary tables of this thread. In MySQL temporary tables
1155
2092
are always thread-local and "shadow" possible base tables with the
1156
2093
same name. This block implements the behaviour.
1157
TODO -> move this block into a separate function.
2094
TODO: move this block into a separate function.
1159
for (table= temporary_tables; table ; table=table->next)
1161
if (table->s->table_cache_key.length == key_length && !memcmp(table->s->table_cache_key.str, key, key_length))
2097
for (table= thd->temporary_tables; table ; table=table->next)
1164
We're trying to use the same temporary table twice in a query.
1165
Right now we don't support this because a temporary table
1166
is always represented by only one Table object in Session, and
1167
it can not be cloned. Emit an error for an unsupported behaviour.
1169
if (table->query_id)
2099
if (table->s->table_cache_key.length == key_length +
2100
TMP_TABLE_KEY_EXTRA &&
2101
!memcmp(table->s->table_cache_key.str, key,
2102
key_length + TMP_TABLE_KEY_EXTRA))
1171
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2105
We're trying to use the same temporary table twice in a query.
2106
Right now we don't support this because a temporary table
2107
is always represented by only one Table object in THD, and
2108
it can not be cloned. Emit an error for an unsupported behaviour.
2110
if (table->query_id)
2112
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2115
table->query_id= thd->query_id;
2116
thd->thread_specific_used= true;
1174
table->query_id= getQueryId();
1179
2122
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1181
2124
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1186
If it's the first table from a list of tables used in a query,
1187
remember refresh_version (the version of open_cache state).
1188
If the version changes while we're opening the remaining tables,
1189
we will have to back off, close all the tables opened-so-far,
1190
and try to reopen them.
1192
Note-> refresh_version is currently changed only during FLUSH TABLES.
1195
version= refresh_version;
1196
else if ((version != refresh_version) &&
1197
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1199
/* Someone did a refresh while thread was opening tables */
1207
Before we test the global cache, we test our local session cache.
1211
assert(false); /* Not implemented yet */
2129
The table is not temporary - if we're in pre-locked or LOCK TABLES
2130
mode, let's try to find the requested table in the list of pre-opened
2131
and locked tables. If the table is not there, return an error - we can't
2132
open not pre-opened tables in pre-locked/LOCK TABLES mode.
2133
TODO: move this block into a separate function.
2135
if (thd->locked_tables)
2136
{ // Using table locks
2137
Table *best_table= 0;
2138
int best_distance= INT_MIN;
2139
bool check_if_used= false;
2140
for (table=thd->open_tables; table ; table=table->next)
2142
if (table->s->table_cache_key.length == key_length &&
2143
!memcmp(table->s->table_cache_key.str, key, key_length))
2145
if (check_if_used && table->query_id &&
2146
table->query_id != thd->query_id)
2149
If we are in stored function or trigger we should ensure that
2150
we won't change table that is already used by calling statement.
2151
So if we are opening table for writing, we should check that it
2152
is not already open by some calling stamement.
2154
my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
2155
table->s->table_name.str);
2159
When looking for a usable Table, ignore MERGE children, as they
2160
belong to their parent and cannot be used explicitly.
2162
if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
2163
table->query_id != thd->query_id) /* skip tables already used */
2165
int distance= ((int) table->reginfo.lock_type -
2166
(int) table_list->lock_type);
2168
Find a table that either has the exact lock type requested,
2169
or has the best suitable lock. In case there is no locked
2170
table that has an equal or higher lock than requested,
2171
we us the closest matching lock to be able to produce an error
2172
message about wrong lock mode on the table. The best_table
2173
is changed if bd < 0 <= d or bd < d < 0 or 0 <= d < bd.
2175
distance < 0 - No suitable lock found
2176
distance > 0 - we have lock mode higher then we require
2177
distance == 0 - we have lock mode exactly which we need
2179
if ((best_distance < 0 && distance > best_distance) || (distance >= 0 && distance < best_distance))
2181
best_distance= distance;
2183
if (best_distance == 0 && !check_if_used)
2186
If we have found perfect match and we don't need to check that
2187
table is not used by one of calling statements (assuming that
2188
we are inside of function or trigger) we can finish iterating
2189
through open tables list.
2200
table->query_id= thd->query_id;
2204
No table in the locked tables list. In case of explicit LOCK TABLES
2205
this can happen if a user did not include the able into the list.
2206
In case of pre-locked mode locked tables list is generated automatically,
2207
so we may only end up here if the table did not exist when
2208
locked tables list was created.
2210
my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
1343
2377
/* Free cache if too big */
1344
2378
while (open_cache.records > table_cache_size && unused_tables)
1345
hash_delete(&open_cache,(unsigned char*) unused_tables);
2379
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
1347
2381
if (table_list->create)
1349
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1351
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
2385
if (check_if_table_exists(thd, table_list, &exists))
2387
VOID(pthread_mutex_unlock(&LOCK_open));
1354
2394
Table to be created, so we need to create placeholder in table-cache.
1356
if (!(table= table_cache_insert_placeholder(key, key_length)))
2396
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
1358
pthread_mutex_unlock(&LOCK_open);
2398
VOID(pthread_mutex_unlock(&LOCK_open));
1362
2402
Link placeholder to the open tables list so it will be automatically
1363
2403
removed once tables are closed. Also mark it so it won't be ignored
1364
2404
by other trying to take name-lock.
1366
table->open_placeholder= true;
1367
table->next= open_tables;
1369
pthread_mutex_unlock(&LOCK_open);
2406
table->open_placeholder= 1;
2407
table->next= thd->open_tables;
2408
thd->open_tables= table;
2409
VOID(pthread_mutex_unlock(&LOCK_open));
1373
2412
/* Table exists. Let us try to open it. */
1376
2415
/* make a new table */
1377
table= (Table *)malloc(sizeof(Table));
2416
if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
1380
pthread_mutex_unlock(&LOCK_open);
2418
VOID(pthread_mutex_unlock(&LOCK_open));
1384
error= open_unireg_entry(this, table, table_list, alias, key, key_length);
1388
pthread_mutex_unlock(&LOCK_open);
1391
my_hash_insert(&open_cache, (unsigned char*) table);
2422
error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
2423
/* Combine the follow two */
2426
my_free((uchar*)table, MYF(0));
2427
VOID(pthread_mutex_unlock(&LOCK_open));
2432
my_free((uchar*)table, MYF(0));
2433
VOID(pthread_mutex_unlock(&LOCK_open));
2436
VOID(my_hash_insert(&open_cache,(uchar*) table));
1394
pthread_mutex_unlock(&LOCK_open);
2439
VOID(pthread_mutex_unlock(&LOCK_open));
1397
table->next= open_tables; /* Link into simple list */
2442
table->next=thd->open_tables; /* Link into simple list */
2443
thd->open_tables=table;
1400
table->reginfo.lock_type= TL_READ; /* Assume read */
1403
assert(table->s->ref_count > 0 || table->s->tmp_table != message::Table::STANDARD);
1405
if (lex->need_correct_ident())
2445
table->reginfo.lock_type=TL_READ; /* Assume read */
2448
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2450
if (thd->lex->need_correct_ident())
1406
2451
table->alias_name_used= my_strcasecmp(table_alias_charset,
1407
2452
table->s->table_name.str, alias);
1408
2453
/* Fix alias if table name changes */
1409
2454
if (strcmp(table->alias, alias))
1411
uint32_t length=(uint32_t) strlen(alias)+1;
1412
table->alias= (char*) realloc((char*) table->alias, length);
2456
uint length=(uint) strlen(alias)+1;
2457
table->alias= (char*) my_realloc((char*) table->alias, length,
1413
2459
memcpy((void*) table->alias, alias, length);
1416
2461
/* These variables are also set in reopen_table() */
1417
table->tablenr= current_tablenr++;
1418
table->used_fields= 0;
1419
table->const_table= 0;
2462
table->tablenr=thd->current_tablenr++;
2463
table->used_fields=0;
2464
table->const_table=0;
1420
2465
table->null_row= false;
1421
2466
table->maybe_null= false;
1422
2467
table->force_index= false;
1926
2995
other threads trying to get the lock.
1929
void abort_locked_tables(Session *session,const char *db, const char *table_name)
2998
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
1932
for (table= session->open_tables; table ; table= table->next)
3001
for (table= thd->open_tables; table ; table= table->next)
1934
3003
if (!strcmp(table->s->table_name.str, table_name) &&
1935
!strcmp(table->s->getSchemaName(), db))
3004
!strcmp(table->s->db.str, db))
1937
3006
/* If MERGE child, forward lock handling to parent. */
1938
mysql_lock_abort(session, table);
3007
mysql_lock_abort(thd, table, true);
1945
Load a table definition from cursor and open unireg table
3015
Function to assign a new table map id to a table share.
3019
share - Pointer to table share structure
3023
We are intentionally not checking that share->mutex is locked
3024
since this function should only be called when opening a table
3025
share and before it is entered into the table_def_cache (meaning
3026
that it cannot be fetched by another thread, even accidentally).
3031
The LOCK_open mutex is locked
3035
share->table_map_id is given a value that with a high certainty is
3036
not used by any other table (the only case where a table id can be
3037
reused is on wrap-around, which means more than 4 billion table
3038
share opens have been executed while one table was open all the
3041
share->table_map_id is not UINT32_MAX.
3043
void assign_new_table_id(TABLE_SHARE *share)
3045
static uint32_t last_table_id= UINT32_MAX;
3048
assert(share != NULL);
3049
safe_mutex_assert_owner(&LOCK_open);
3051
ulong tid= ++last_table_id; /* get next id */
3053
There is one reserved number that cannot be used. Remember to
3054
change this when 6-byte global table id's are introduced.
3056
if (unlikely(tid == UINT32_MAX))
3057
tid= ++last_table_id;
3058
share->table_map_id= tid;
3060
/* Post conditions */
3061
assert(share->table_map_id != UINT32_MAX);
3067
Load a table definition from file and open unireg table
1949
session Thread handle
1950
entry Store open table definition here
1951
table_list TableList with db, table_name
1953
cache_key Key for share_cache
1954
cache_key_length length of cache_key
3072
entry Store open table definition here
3073
table_list TableList with db, table_name
3075
cache_key Key for share_cache
3076
cache_key_length length of cache_key
1957
Extra argument for open is taken from session->open_options
1958
One must have a lock on LOCK_open when calling this function
3079
Extra argument for open is taken from thd->open_options
3080
One must have a lock on LOCK_open when calling this function
1965
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
3087
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
1966
3088
const char *alias,
1967
char *cache_key, uint32_t cache_key_length)
3089
char *cache_key, uint cache_key_length)
1971
uint32_t discover_retry_count= 0;
3093
uint discover_retry_count= 0;
1973
3095
safe_mutex_assert_owner(&LOCK_open);
1975
if (not (share= TableShare::getShare(session, table_list, cache_key,
1977
table_list->i_s_requested_object,
3097
if (!(share= get_table_share_with_create(thd, table_list, cache_key,
3099
table_list->i_s_requested_object,
1981
while ((error= open_table_from_share(session, share, alias,
1982
(uint32_t) (HA_OPEN_KEYFILE |
1986
session->open_options, entry)))
3103
while ((error= open_table_from_share(thd, share, alias,
3104
(uint) (HA_OPEN_KEYFILE |
3109
thd->open_options, entry, OTM_OPEN)))
1988
3111
if (error == 7) // Table def changed
2013
3138
if (share->ref_count != 1)
2015
3140
/* Free share and wait until it's released by all threads */
2016
TableShare::release(share);
2018
if (!session->killed)
3141
release_table_share(share, RELEASE_WAIT_FOR_DROP);
2020
drizzle_reset_errors(session, 1); // Clear warnings
2021
session->clear_error(); // Clear error message
3144
drizzle_reset_errors(thd, 1); // Clear warnings
3145
thd->clear_error(); // Clear error message
3150
if (!entry->s || !entry->s->crashed)
3152
// Code below is for repairing a crashed file
3153
if ((error= lock_table_name(thd, table_list, true)))
3157
if (wait_for_locked_table_names(thd, table_list))
3159
unlock_table_name(thd, table_list);
3163
pthread_mutex_unlock(&LOCK_open);
3164
thd->clear_error(); // Clear error message
3166
if (open_table_from_share(thd, share, alias,
3167
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3171
ha_open_options | HA_OPEN_FOR_REPAIR,
3172
entry, OTM_OPEN) || ! entry->file ||
3173
(entry->file->is_crashed() && entry->file->ha_check_and_repair(thd)))
3175
/* Give right error message */
3177
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3178
sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
3179
share->table_name.str);
3185
thd->clear_error(); // Clear error message
3186
pthread_mutex_lock(&LOCK_open);
3187
unlock_table_name(thd, table_list);
3195
If we are here, there was no fatal error (but error may be still
3198
if (unlikely(entry->file->implicit_emptied))
3200
entry->file->implicit_emptied= 0;
3201
if (mysql_bin_log.is_open())
3204
uint query_buf_size= 20 + share->db.length + share->table_name.length +1;
3205
if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
3207
/* this DELETE FROM is needed even with row-based binlogging */
3208
end = strxmov(stpcpy(query, "DELETE FROM `"),
3209
share->db.str,"`.`",share->table_name.str,"`", NullS);
3210
thd->binlog_query(THD::STMT_QUERY_TYPE,
3211
query, (ulong)(end-query), false, false);
3212
my_free(query, MYF(0));
3217
As replication is maybe going to be corrupted, we need to warn the
3218
DBA on top of warning the client (which will automatically be done
3219
because of MYF(MY_WME) in my_malloc() above).
3221
sql_print_error(_("When opening HEAP table, could not allocate memory "
3222
"to write 'DELETE FROM `%s`.`%s`' to the binary log"),
3223
table_list->db, table_list->table_name);
2033
TableShare::release(share);
3232
release_table_share(share, RELEASE_NORMAL);
2146
3351
result= -1; // Fatal error
2149
if (tables->lock_type != TL_UNLOCK)
3354
if (tables->lock_type != TL_UNLOCK && ! thd->locked_tables)
2151
3356
if (tables->lock_type == TL_WRITE_DEFAULT)
2152
tables->table->reginfo.lock_type= update_lock_default;
2153
else if (tables->table->s->tmp_table == message::Table::STANDARD)
3357
tables->table->reginfo.lock_type= thd->update_lock_default;
3358
else if (tables->table->s->tmp_table == NO_TMP_TABLE)
2154
3359
tables->table->reginfo.lock_type= tables->lock_type;
3363
thd_proc_info(thd, 0);
3364
free_root(&new_frm_mem, MYF(0)); // Free pre-alloced block
2160
3366
if (result && tables)
2163
3369
Some functions determine success as (tables->table != NULL).
2164
tables->table is in session->open_tables.
3370
tables->table is in thd->open_tables.
2166
3372
tables->table= NULL;
2169
3374
return(result);
3379
Check that lock is ok for tables; Call start stmt if ok
3382
check_lock_and_start_stmt()
3384
table_list Table to check
3385
lock_type Lock used for table
3392
static bool check_lock_and_start_stmt(THD *thd, Table *table,
3393
thr_lock_type lock_type)
3397
if ((int) lock_type >= (int) TL_WRITE_ALLOW_READ &&
3398
(int) table->reginfo.lock_type < (int) TL_WRITE_ALLOW_READ)
3400
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3403
if ((error=table->file->start_stmt(thd, lock_type)))
3405
table->file->print_error(error,MYF(0));
3413
@brief Open and lock one table
3415
@param[in] thd thread handle
3416
@param[in] table_l table to open is first table in this list
3417
@param[in] lock_type lock to use for table
3420
@retval != NULL OK, opened table returned
3424
If ok, the following are also set:
3425
table_list->lock_type lock_type
3426
table_list->table table
3429
If table_l is a list, not a single table, the list is temporarily
3433
This function is meant as a replacement for open_ltable() when
3434
MERGE tables can be opened. open_ltable() cannot open MERGE tables.
3436
There may be more differences between open_n_lock_single_table() and
3437
open_ltable(). One known difference is that open_ltable() does
3438
neither call decide_logging_format() nor handle some other logging
3439
and locking issues because it does not call lock_tables().
3442
Table *open_n_lock_single_table(THD *thd, TableList *table_l,
3443
thr_lock_type lock_type)
3445
TableList *save_next_global;
3447
/* Remember old 'next' pointer. */
3448
save_next_global= table_l->next_global;
3450
table_l->next_global= NULL;
3452
/* Set requested lock type. */
3453
table_l->lock_type= lock_type;
3455
/* Open the table. */
3456
if (simple_open_n_lock_tables(thd, table_l))
3457
table_l->table= NULL; /* Just to be sure. */
3460
table_l->next_global= save_next_global;
3462
return(table_l->table);
2174
3467
Open and lock one table
2178
session Thread Cursor
2179
table_list Table to open is first table in this list
2180
lock_type Lock to use for open
2181
lock_flags Flags passed to mysql_lock_table
3472
table_list Table to open is first table in this list
3473
lock_type Lock to use for open
3474
lock_flags Flags passed to mysql_lock_table
2184
This function don't do anything like SP/SF/views/triggers analysis done
2185
in open_tables(). It is intended for opening of only one concrete table.
2186
And used only in special contexts.
3477
This function don't do anything like SP/SF/views/triggers analysis done
3478
in open_tables(). It is intended for opening of only one concrete table.
3479
And used only in special contexts.
2192
If ok, the following are also set:
2193
table_list->lock_type lock_type
2194
table_list->table table
3485
If ok, the following are also set:
3486
table_list->lock_type lock_type
3487
table_list->table table
2197
Table *Session::openTableLock(TableList *table_list, thr_lock_type lock_type)
3490
Table *open_ltable(THD *thd, TableList *table_list, thr_lock_type lock_type,
2202
set_proc_info("Opening table");
2204
while (!(table= openTable(table_list, &refresh)) &&
3496
thd_proc_info(thd, "Opening table");
3497
thd->current_tablenr= 0;
3498
while (!(table= open_table(thd, table_list, &refresh, 0)) &&
2210
3504
table_list->lock_type= lock_type;
2211
3505
table_list->table= table;
2213
assert(lock == 0); // You must lock everything at once
2214
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
2215
if (! (lock= mysql_lock_tables(this, &table_list->table, 1, 0, &refresh)))
3506
if (thd->locked_tables)
3508
if (check_lock_and_start_stmt(thd, table, lock_type))
3513
assert(thd->lock == 0); // You must lock everything at once
3514
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3515
if (! (thd->lock= mysql_lock_tables(thd, &table_list->table, 1,
3516
lock_flags, &refresh)))
3521
thd_proc_info(thd, 0);
3527
Open all tables in list, locks them and optionally process derived tables.
3530
open_and_lock_tables_derived()
3531
thd - thread handler
3532
tables - list of tables for open&locking
3533
derived - if to handle derived tables
3540
The lock will automaticaly be freed by close_thread_tables()
3543
There are two convenience functions:
3544
- simple_open_n_lock_tables(thd, tables) without derived handling
3545
- open_and_lock_tables(thd, tables) with derived handling
3546
Both inline functions call open_and_lock_tables_derived() with
3547
the third argument set appropriately.
3550
int open_and_lock_tables_derived(THD *thd, TableList *tables, bool derived)
3557
if (open_tables(thd, &tables, &counter, 0))
3560
if (!lock_tables(thd, tables, counter, &need_reopen))
3564
close_tables_for_reopen(thd, &tables);
3567
(mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3568
(thd->fill_derived_tables() &&
3569
mysql_handle_derived(thd->lex, &mysql_derived_filling))))
3570
return(true); /* purecov: inspected */
3576
Open all tables in list and process derived tables
3579
open_normal_and_derived_tables
3580
thd - thread handler
3581
tables - list of tables for open
3582
flags - bitmap of flags to modify how the tables will be open:
3583
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3584
done a flush or namelock on it.
3591
This is to be used on prepare stage when you don't read any
3592
data from the tables.
3595
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint flags)
3598
assert(!thd->fill_derived_tables());
3599
if (open_tables(thd, &tables, &counter, flags) ||
3600
mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3601
return(true); /* purecov: inspected */
3607
Decide on logging format to use for the statement.
3609
Compute the capabilities vector for the involved storage engines
3610
and mask out the flags for the binary log. Right now, the binlog
3611
flags only include the capabilities of the storage engines, so this
3614
We now have three alternatives that prevent the statement from
3617
1. If there are no capabilities left (all flags are clear) it is
3618
not possible to log the statement at all, so we roll back the
3619
statement and report an error.
3621
2. Statement mode is set, but the capabilities indicate that
3622
statement format is not possible.
3624
3. Row mode is set, but the capabilities indicate that row
3625
format is not possible.
3627
4. Statement is unsafe, but the capabilities indicate that row
3628
format is not possible.
3630
If we are in MIXED mode, we then decide what logging format to use:
3632
1. If the statement is unsafe, row-based logging is used.
3634
2. If statement-based logging is not possible, row-based logging is
3637
3. Otherwise, statement-based logging is used.
3639
@param thd Client thread
3640
@param tables Tables involved in the query
3643
int decide_logging_format(THD *thd, TableList *tables)
3645
if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3647
handler::Table_flags flags_some_set= handler::Table_flags();
3648
handler::Table_flags flags_all_set= ~handler::Table_flags();
3649
bool multi_engine= false;
3650
void* prev_ht= NULL;
3651
for (TableList *table= tables; table; table= table->next_global)
3653
if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3655
uint64_t const flags= table->table->file->ha_table_flags();
3656
if (prev_ht && prev_ht != table->table->file->ht)
3658
prev_ht= table->table->file->ht;
3659
flags_all_set &= flags;
3660
flags_some_set |= flags;
3665
if (flags_all_set == 0)
3667
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3668
"Statement cannot be logged to the binary log in"
3669
" row-based nor statement-based format");
3671
else if (thd->variables.binlog_format == BINLOG_FORMAT_STMT &&
3672
(flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3674
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3675
"Statement-based format required for this statement,"
3676
" but not allowed by this combination of engines");
3678
else if ((thd->variables.binlog_format == BINLOG_FORMAT_ROW ||
3679
thd->lex->is_stmt_unsafe()) &&
3680
(flags_all_set & HA_BINLOG_ROW_CAPABLE) == 0)
3682
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3683
"Row-based format required for this statement,"
3684
" but not allowed by this combination of engines");
3688
If more than one engine is involved in the statement and at
3689
least one is doing it's own logging (is *self-logging*), the
3690
statement cannot be logged atomically, so we generate an error
3691
rather than allowing the binlog to become corrupt.
3694
(flags_some_set & HA_HAS_OWN_BINLOGGING))
3696
error= ER_BINLOG_LOGGING_IMPOSSIBLE;
3697
my_error(error, MYF(0),
3698
"Statement cannot be written atomically since more"
3699
" than one engine involved and at least one engine"
3700
" is self-logging");
3707
We switch to row-based format if we are in mixed mode and one of
3708
the following are true:
3710
1. If the statement is unsafe
3711
2. If statement format cannot be used
3713
Observe that point to cannot be decided before the tables
3714
involved in a statement has been checked, i.e., we cannot put
3715
this code in reset_current_stmt_binlog_row_based(), it has to be
3718
if (thd->lex->is_stmt_unsafe() ||
3719
(flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3721
thd->set_current_stmt_binlog_row_based_if_mixed();
2225
3729
Lock all tables in list
2229
session Thread Cursor
2230
tables Tables to lock
2231
count Number of opened tables
2232
need_reopen Out parameter which if true indicates that some
2233
tables were dropped or altered during this call
2234
and therefore invoker should reopen tables and
2235
try to lock them once again (in this case
2236
lock_tables() will also return error).
3734
tables Tables to lock
3735
count Number of opened tables
3736
need_reopen Out parameter which if true indicates that some
3737
tables were dropped or altered during this call
3738
and therefore invoker should reopen tables and
3739
try to lock them once again (in this case
3740
lock_tables() will also return error).
2239
You can't call lock_tables twice, as this would break the dead-lock-free
2240
handling thr_lock gives us. You most always get all needed locks at
3743
You can't call lock_tables twice, as this would break the dead-lock-free
3744
handling thr_lock gives us. You most always get all needed locks at
2243
If query for which we are calling this function marked as requring
2244
prelocking, this function will do implicit LOCK TABLES and change
2245
session::prelocked_mode accordingly.
3747
If query for which we are calling this function marked as requring
3748
prelocking, this function will do implicit LOCK TABLES and change
3749
thd::prelocked_mode accordingly.
2252
int Session::lock_tables(TableList *tables, uint32_t count, bool *need_reopen)
3756
int lock_tables(THD *thd, TableList *tables, uint count, bool *need_reopen)
2254
3758
TableList *table;
2255
Session *session= this;
2258
3761
We can't meet statement requiring prelocking if we already
2289
3843
Open a single table without table caching and don't set it in open_list
2292
open_temporary_table()
2293
session Thread object
2294
path Path (without .frm)
2296
table_name Table name
2297
link_in_list 1 if table should be linked into session->temporary_tables
2300
Used by alter_table to open a temporary table and when creating
2301
a temporary table with CREATE TEMPORARY ...
3846
open_temporary_table()
3848
path Path (without .frm)
3850
table_name Table name
3851
link_in_list 1 if table should be linked into thd->temporary_tables
3854
Used by alter_table to open a temporary table and when creating
3855
a temporary table with CREATE TEMPORARY ...
2308
Table *Session::open_temporary_table(TableIdentifier &identifier,
3862
Table *open_temporary_table(THD *thd, const char *path, const char *db,
3863
const char *table_name, bool link_in_list,
3864
open_table_mode open_mode)
2311
Table *new_tmp_table;
2313
3868
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2314
uint32_t key_length, path_length;
2315
3870
TableList table_list;
2317
table_list.db= const_cast<char*>(identifier.getSchemaName().c_str());
2318
table_list.table_name= const_cast<char*>(identifier.getTableName().c_str());
3872
table_list.db= (char*) db;
3873
table_list.table_name= (char*) table_name;
2319
3874
/* Create the cache_key for temporary tables */
2320
key_length= table_list.create_table_def_key(cache_key);
2321
path_length= identifier.getPath().length();
2323
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2324
path_length + 1 + key_length)))
2327
share= (TableShare*) (new_tmp_table+1);
3875
key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3877
if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3878
strlen(path)+1 + key_length,
3880
return(0); /* purecov: inspected */
3882
share= (TABLE_SHARE*) (tmp_table+1);
2328
3883
tmp_path= (char*) (share+1);
2329
saved_cache_key= strcpy(tmp_path, identifier.getPath().c_str())+path_length+1;
3884
saved_cache_key= stpcpy(tmp_path, path)+1;
2330
3885
memcpy(saved_cache_key, cache_key, key_length);
2332
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
3887
init_tmp_table_share(thd, share, saved_cache_key, key_length,
3888
strchr(saved_cache_key, '\0')+1, tmp_path);
2335
First open the share, and then open the table from the share we just opened.
2337
if (open_table_def(*this, identifier, share) ||
2338
open_table_from_share(this, share, identifier.getTableName().c_str(),
2339
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3890
if (open_table_def(thd, share, 0) ||
3891
open_table_from_share(thd, share, table_name,
3892
(open_mode == OTM_ALTER) ? 0 :
3893
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3895
(open_mode == OTM_ALTER) ?
3896
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
2341
3898
ha_open_options,
3899
tmp_table, open_mode))
2344
3901
/* No need to lock share->mutex as this is not needed for tmp tables */
2345
share->free_table_share();
2346
free((char*) new_tmp_table);
3902
free_table_share(share);
3903
my_free((char*) tmp_table,MYF(0));
2350
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2351
share->tmp_table= message::Table::TEMPORARY;
3907
tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
3908
if (open_mode == OTM_ALTER)
3911
Temporary table has been created with frm_only
3912
and has not been created in any storage engine
3914
share->tmp_table= TMP_TABLE_FRM_FILE_ONLY;
3917
share->tmp_table= (tmp_table->file->has_transactions() ?
3918
TRANSACTIONAL_TMP_TABLE : NON_TRANSACTIONAL_TMP_TABLE);
2353
3920
if (link_in_list)
2355
3922
/* growing temp list at the head */
2356
new_tmp_table->next= this->temporary_tables;
2357
if (new_tmp_table->next)
2358
new_tmp_table->next->prev= new_tmp_table;
2359
this->temporary_tables= new_tmp_table;
2360
this->temporary_tables->prev= 0;
2362
new_tmp_table->pos_in_table_list= 0;
2364
return new_tmp_table;
3923
tmp_table->next= thd->temporary_tables;
3924
if (tmp_table->next)
3925
tmp_table->next->prev= tmp_table;
3926
thd->temporary_tables= tmp_table;
3927
thd->temporary_tables->prev= 0;
3928
if (thd->slave_thread)
3929
slave_open_temp_tables++;
3931
tmp_table->pos_in_table_list= 0;
3936
bool rm_temporary_table(handlerton *base, char *path, bool frm_only)
3942
stpcpy(ext= strchr(path, '\0'), reg_ext);
3943
if (my_delete(path,MYF(0)))
3944
error=1; /* purecov: inspected */
3945
*ext= 0; // remove extension
3946
file= get_new_handler((TABLE_SHARE*) 0, current_thd->mem_root, base);
3947
if (!frm_only && file && file->ha_delete_table(path))
3950
sql_print_warning(_("Could not remove temporary table: '%s', error: %d"),
2368
3958
/*****************************************************************************
2369
* The following find_field_in_XXX procedures implement the core of the
2370
* name resolution functionality. The entry point to resolve a column name in a
2371
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
2372
* for each table reference. In turn, depending on the type of table reference,
2373
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
2374
* below specific for the type of table reference.
2375
******************************************************************************/
3959
* The following find_field_in_XXX procedures implement the core of the
3960
* name resolution functionality. The entry point to resolve a column name in a
3961
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
3962
* for each table reference. In turn, depending on the type of table reference,
3963
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
3964
* below specific for the type of table reference.
3965
******************************************************************************/
2377
3967
/* Special Field pointers as return values of find_field_in_XXX functions. */
2378
3968
Field *not_found_field= (Field*) 0x1;
2379
Field *view_ref_found= (Field*) 0x2;
2381
static void update_field_dependencies(Session *session, Field *field, Table *table)
3969
Field *view_ref_found= (Field*) 0x2;
3971
#define WRONG_GRANT (Field*) -1
3973
static void update_field_dependencies(THD *thd, Field *field, Table *table)
2383
if (session->mark_used_columns != MARK_COLUMNS_NONE)
3975
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
2385
MyBitmap *current_bitmap, *other_bitmap;
3977
MY_BITMAP *current_bitmap, *other_bitmap;
2388
3980
We always want to register the used keys, as the column bitmap may have
2389
3981
been set for all fields (for example for view).
2392
table->covering_keys&= field->part_of_key;
2393
table->merge_keys|= field->part_of_key;
2395
if (session->mark_used_columns == MARK_COLUMNS_READ)
3984
table->covering_keys.intersect(field->part_of_key);
3985
table->merge_keys.merge(field->part_of_key);
3987
if (thd->mark_used_columns == MARK_COLUMNS_READ)
2397
3989
current_bitmap= table->read_set;
2398
3990
other_bitmap= table->write_set;
2559
4158
Find field in a table reference.
2562
find_field_in_table_ref()
2563
session [in] thread Cursor
2564
table_list [in] table reference to search
2565
name [in] name of field
2566
length [in] field length of name
2567
item_name [in] name of item if it will be created (VIEW)
2568
db_name [in] optional database name that qualifies the
2569
table_name [in] optional table name that qualifies the field
2570
ref [in/out] if 'name' is resolved to a view field, ref
2571
is set to point to the found view field
2572
allow_rowid [in] do allow finding of "_rowid" field?
2573
cached_field_index_ptr [in] cached position in field list (used to
2574
speedup lookup for fields in prepared tables)
2575
register_tree_change [in] true if ref is not stack variable and we
2576
need register changes in item tree
2577
actual_table [out] the original table reference where the field
2578
belongs - differs from 'table_list' only for
2579
NATURAL_USING joins.
4161
find_field_in_table_ref()
4162
thd [in] thread handler
4163
table_list [in] table reference to search
4164
name [in] name of field
4165
length [in] field length of name
4166
item_name [in] name of item if it will be created (VIEW)
4167
db_name [in] optional database name that qualifies the
4168
table_name [in] optional table name that qualifies the field
4169
ref [in/out] if 'name' is resolved to a view field, ref
4170
is set to point to the found view field
4171
check_privileges [in] check privileges
4172
allow_rowid [in] do allow finding of "_rowid" field?
4173
cached_field_index_ptr [in] cached position in field list (used to
4174
speedup lookup for fields in prepared tables)
4175
register_tree_change [in] true if ref is not stack variable and we
4176
need register changes in item tree
4177
actual_table [out] the original table reference where the field
4178
belongs - differs from 'table_list' only for
4179
NATURAL_USING joins.
2582
Find a field in a table reference depending on the type of table
2583
reference. There are three types of table references with respect
2584
to the representation of their result columns:
2585
- an array of Field_translator objects for MERGE views and some
2586
information_schema tables,
2587
- an array of Field objects (and possibly a name hash) for stored
2589
- a list of Natural_join_column objects for NATURAL/USING joins.
2590
This procedure detects the type of the table reference 'table_list'
2591
and calls the corresponding search routine.
4182
Find a field in a table reference depending on the type of table
4183
reference. There are three types of table references with respect
4184
to the representation of their result columns:
4185
- an array of Field_translator objects for MERGE views and some
4186
information_schema tables,
4187
- an array of Field objects (and possibly a name hash) for stored
4189
- a list of Natural_join_column objects for NATURAL/USING joins.
4190
This procedure detects the type of the table reference 'table_list'
4191
and calls the corresponding search routine.
2594
0 field is not found
2595
view_ref_found found value in VIEW (real result is in *ref)
4194
0 field is not found
4195
view_ref_found found value in VIEW (real result is in *ref)
2600
find_field_in_table_ref(Session *session, TableList *table_list,
2601
const char *name, uint32_t length,
4200
find_field_in_table_ref(THD *thd, TableList *table_list,
4201
const char *name, uint length,
2602
4202
const char *item_name, const char *db_name,
2603
4203
const char *table_name, Item **ref,
2605
uint32_t *cached_field_index_ptr,
4204
bool check_privileges, bool allow_rowid,
4205
uint *cached_field_index_ptr,
2606
4206
bool register_tree_change, TableList **actual_table)
2608
4208
Field *fld= NULL;
2682
4286
natural join, thus if the field is not qualified, we will search
2683
4287
directly the top-most NATURAL/USING join.
2685
fld= find_field_in_natural_join(session, table_list, name, length, ref,
4289
fld= find_field_in_natural_join(thd, table_list, name, length, ref,
2686
4290
register_tree_change, actual_table);
2691
if (session->mark_used_columns != MARK_COLUMNS_NONE)
2694
Get rw_set correct for this field so that the Cursor
2695
knows that this field is involved in the query and gets
2698
Field *field_to_set= NULL;
2699
if (fld == view_ref_found)
2701
Item *it= (*ref)->real_item();
2702
if (it->type() == Item::FIELD_ITEM)
2703
field_to_set= ((Item_field*)it)->field;
2706
if (session->mark_used_columns == MARK_COLUMNS_READ)
2707
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
2714
Table *table= field_to_set->table;
2715
if (session->mark_used_columns == MARK_COLUMNS_READ)
2716
table->setReadSet(field_to_set->field_index);
2718
table->setWriteSet(field_to_set->field_index);
4295
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
4298
Get rw_set correct for this field so that the handler
4299
knows that this field is involved in the query and gets
4302
Field *field_to_set= NULL;
4303
if (fld == view_ref_found)
4305
Item *it= (*ref)->real_item();
4306
if (it->type() == Item::FIELD_ITEM)
4307
field_to_set= ((Item_field*)it)->field;
4310
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4311
it->walk(&Item::register_field_in_read_map, 1, (uchar *) 0);
4318
Table *table= field_to_set->table;
4319
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4320
bitmap_set_bit(table->read_set, field_to_set->field_index);
4322
bitmap_set_bit(table->write_set, field_to_set->field_index);
4331
Find field in table, no side effects, only purpose is to check for field
4332
in table object and get reference to the field if found.
4335
find_field_in_table_sef()
4337
table table where to find
4338
name Name of field searched for
4341
0 field is not found
4345
Field *find_field_in_table_sef(Table *table, const char *name)
4348
if (table->s->name_hash.records)
4350
field_ptr= (Field**)hash_search(&table->s->name_hash,(uchar*) name,
4355
field_ptr points to field in TABLE_SHARE. Convert it to the matching
4358
field_ptr= (table->field + (field_ptr - table->s->field));
4363
if (!(field_ptr= table->field))
4365
for (; *field_ptr; ++field_ptr)
4366
if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
2727
4377
Find field in table list.
2730
find_field_in_tables()
2731
session pointer to current thread structure
2732
item field item that should be found
2733
first_table list of tables to be searched for item
2734
last_table end of the list of tables to search for item. If NULL
2735
then search to the end of the list 'first_table'.
2736
ref if 'item' is resolved to a view field, ref is set to
2737
point to the found view field
2738
report_error Degree of error reporting:
2739
- IGNORE_ERRORS then do not report any error
2740
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
2741
fields, suppress all other errors
2742
- REPORT_EXCEPT_NON_UNIQUE report all other errors
2743
except when non-unique fields were found
2745
register_tree_change true if ref is not a stack variable and we
2746
to need register changes in item tree
4380
find_field_in_tables()
4381
thd pointer to current thread structure
4382
item field item that should be found
4383
first_table list of tables to be searched for item
4384
last_table end of the list of tables to search for item. If NULL
4385
then search to the end of the list 'first_table'.
4386
ref if 'item' is resolved to a view field, ref is set to
4387
point to the found view field
4388
report_error Degree of error reporting:
4389
- IGNORE_ERRORS then do not report any error
4390
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
4391
fields, suppress all other errors
4392
- REPORT_EXCEPT_NON_UNIQUE report all other errors
4393
except when non-unique fields were found
4395
check_privileges need to check privileges
4396
register_tree_change true if ref is not a stack variable and we
4397
to need register changes in item tree
2749
0 If error: the found field is not unique, or there are
2750
no sufficient access priviliges for the found field,
2751
or the field is qualified with non-existing table.
2752
not_found_field The function was called with report_error ==
2753
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
2754
field was not found.
2755
view_ref_found View field is found, item passed through ref parameter
2756
found field If a item was resolved to some field
4400
0 If error: the found field is not unique, or there are
4401
no sufficient access priviliges for the found field,
4402
or the field is qualified with non-existing table.
4403
not_found_field The function was called with report_error ==
4404
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
4405
field was not found.
4406
view_ref_found View field is found, item passed through ref parameter
4407
found field If a item was resolved to some field
2760
find_field_in_tables(Session *session, Item_ident *item,
4411
find_field_in_tables(THD *thd, Item_ident *item,
2761
4412
TableList *first_table, TableList *last_table,
2762
Item **ref, find_item_error_report_type report_error,
2763
bool register_tree_change)
4413
Item **ref, find_item_error_report_type report_error,
4414
bool check_privileges, bool register_tree_change)
2765
4416
Field *found=0;
2766
4417
const char *db= item->db_name;
2767
4418
const char *table_name= item->table_name;
2768
4419
const char *name= item->field_name;
2769
uint32_t length=(uint32_t) strlen(name);
4420
uint length=(uint) strlen(name);
2770
4421
char name_buff[NAME_LEN+1];
2771
4422
TableList *cur_table= first_table;
2772
4423
TableList *actual_table;
4410
6173
if (value->save_in_field(field, 0) < 0)
4414
return(session->is_error());
6176
return(thd->is_error());
4418
6180
table->auto_increment_field_not_null= false;
4424
bool drizzle_rm_tmp_tables()
6185
bool mysql_rm_tmp_tables(void)
4428
assert(drizzle_tmpdir);
4430
if (!(session= new Session(plugin::Listen::getNullClient())))
4432
session->thread_stack= (char*) &session;
4433
session->storeGlobals();
4435
plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir);
6188
char filePath[FN_REFLEN], *tmpdir, filePathCopy[FN_REFLEN];
6194
if (!(thd= new THD))
6196
thd->thread_stack= (char*) &thd;
6197
thd->store_globals();
6199
for (i=0; i<=mysql_tmpdir_list.max; i++)
6201
tmpdir=mysql_tmpdir_list.list[i];
6202
/* See if the directory exists */
6203
if (!(dirp = my_dir(tmpdir,MYF(MY_WME | MY_DONT_SORT))))
6206
/* Remove all SQLxxx tables from directory */
6208
for (idx=0 ; idx < (uint) dirp->number_off_files ; idx++)
6210
file=dirp->dir_entry+idx;
6212
/* skiping . and .. */
6213
if (file->name[0] == '.' && (!file->name[1] ||
6214
(file->name[1] == '.' && !file->name[2])))
6217
if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
6219
char *ext= fn_ext(file->name);
6220
uint ext_len= strlen(ext);
6221
uint filePath_len= snprintf(filePath, sizeof(filePath),
6222
"%s%c%s", tmpdir, FN_LIBCHAR,
6224
if (!memcmp(reg_ext, ext, ext_len))
6226
handler *handler_file= 0;
6227
/* We should cut file extention before deleting of table */
6228
memcpy(filePathCopy, filePath, filePath_len - ext_len);
6229
filePathCopy[filePath_len - ext_len]= 0;
6230
init_tmp_table_share(thd, &share, "", 0, "", filePathCopy);
6231
if (!open_table_def(thd, &share, 0) &&
6232
((handler_file= get_new_handler(&share, thd->mem_root,
6235
handler_file->ha_delete_table(filePathCopy);
6236
delete handler_file;
6238
free_table_share(&share);
6241
File can be already deleted by tmp_table.file->delete_table().
6242
So we hide error messages which happnes during deleting of these
6245
VOID(my_delete(filePath, MYF(0)));
6251
my_pthread_setspecific_ptr(THR_THD, 0);
4444
6257
/*****************************************************************************
4445
unireg support functions
4446
*****************************************************************************/
6258
unireg support functions
6259
*****************************************************************************/
4449
6262
Invalidate any cache entries that are for some DB
4452
remove_db_from_cache()
4453
db Database name. This will be in lower case if
4454
lower_case_table_name is set
6265
remove_db_from_cache()
6266
db Database name. This will be in lower case if
6267
lower_case_table_name is set
4457
We can't use hash_delete when looping hash_elements. We mark them first
4458
and afterwards delete those marked unused.
6270
We can't use hash_delete when looping hash_elements. We mark them first
6271
and afterwards delete those marked unused.
4461
void remove_db_from_cache(SchemaIdentifier &schema_identifier)
6274
void remove_db_from_cache(const char *db)
4463
safe_mutex_assert_owner(&LOCK_open);
4465
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
6276
for (uint idx=0 ; idx < open_cache.records ; idx++)
4467
6278
Table *table=(Table*) hash_element(&open_cache,idx);
4468
if (not schema_identifier.getPath().compare(table->s->getSchemaName()))
6279
if (!strcmp(table->s->db.str, db))
4470
6281
table->s->version= 0L; /* Free when thread is ready */
4471
if (not table->in_use)
4472
relink_unused(table);
6283
relink_unused(table);
4475
6286
while (unused_tables && !unused_tables->s->version)
4476
hash_delete(&open_cache,(unsigned char*) unused_tables);
6287
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6292
free all unused tables
6295
This is called by 'handle_manager' when one wants to periodicly flush
6296
all not used tables.
6301
(void) pthread_mutex_lock(&LOCK_open);
6302
while (unused_tables)
6303
hash_delete(&open_cache,(uchar*) unused_tables);
6304
(void) pthread_mutex_unlock(&LOCK_open);
4484
6312
close_thread_tables() is called.
4490
0 This thread now have exclusive access to this table and no other thread
4491
can access the table until close_thread_tables() is called.
4492
1 Table is in use by another thread
6318
0 This thread now have exclusive access to this table and no other thread
6319
can access the table until close_thread_tables() is called.
6320
1 Table is in use by another thread
4495
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
6323
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
4498
6326
char key[MAX_DBKEY_LENGTH];
4500
uint32_t key_length;
4503
bool signalled= false;
4505
key_pos= strcpy(key_pos, db) + strlen(db);
4506
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
4507
key_length= (uint32_t) (key_pos-key)+1;
6330
bool result= 0, signalled= 0;
6332
key_length=(uint) (stpcpy(stpcpy(key,db)+1,table_name)-key)+1;
4511
6335
HASH_SEARCH_STATE state;
4512
result= signalled= false;
6336
result= signalled= 0;
4514
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
6338
for (table= (Table*) hash_first(&open_cache, (uchar*) key, key_length,
4517
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
6341
table= (Table*) hash_next(&open_cache, (uchar*) key, key_length,
4522
6346
table->s->version=0L; /* Free when thread is ready */
4523
6347
if (!(in_use=table->in_use))
4525
6349
relink_unused(table);
4527
else if (in_use != session)
6351
else if (in_use != thd)
4530
6354
Mark that table is going to be deleted from cache. This will
4531
6355
force threads that are in mysql_lock_tables() (but not yet
4532
6356
in thr_multi_lock()) to abort it's locks, close all tables and retry
4534
in_use->some_tables_deleted= true;
6358
in_use->some_tables_deleted= 1;
4535
6359
if (table->is_name_opened())
4540
Now we must abort all tables locks used by this thread
4541
as the thread may be waiting to get a lock for another table.
6364
Now we must abort all tables locks used by this thread
6365
as the thread may be waiting to get a lock for another table.
4542
6366
Note that we need to hold LOCK_open while going through the
4543
6367
list. So that the other thread cannot change it. The other
4544
6368
thread must also hold LOCK_open whenever changing the
4545
6369
open_tables list. Aborting the MERGE lock after a child was
4546
6370
closed and before the parent is closed would be fatal.
4548
for (Table *session_table= in_use->open_tables;
4550
session_table= session_table->next)
6372
for (Table *thd_table= in_use->open_tables;
6374
thd_table= thd_table->next)
4552
6376
/* Do not handle locks of MERGE children. */
4553
if (session_table->db_stat) // If table is open
4554
signalled|= mysql_lock_abort_for_thread(session, session_table);
6377
if (thd_table->db_stat) // If table is open
6378
signalled|= mysql_lock_abort_for_thread(thd, thd_table);
4558
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
6382
result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
4560
6384
while (unused_tables && !unused_tables->s->version)
4561
hash_delete(&open_cache,(unsigned char*) unused_tables);
6385
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
4563
6387
/* Remove table from table definition cache if it's not in use */
4564
TableShare::release(key, key_length);
6388
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
6391
share->version= 0; // Mark for delete
6392
if (share->ref_count == 0)
6394
pthread_mutex_lock(&share->mutex);
6395
VOID(hash_delete(&table_def_cache, (uchar*) share));
4566
6399
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))