33
#include "drizzled/internal/my_pthread.h"
34
#include "drizzled/internal/thread_var.h"
35
#include <mysys/my_pthread.h>
36
37
#include <drizzled/sql_select.h>
38
#include <mysys/my_dir.h>
37
39
#include <drizzled/error.h>
38
40
#include <drizzled/gettext.h>
39
41
#include <drizzled/nested_join.h>
40
42
#include <drizzled/sql_base.h>
41
43
#include <drizzled/show.h>
42
44
#include <drizzled/item/cmpfunc.h>
43
#include <drizzled/replication_services.h>
45
#include <drizzled/replicator.h>
44
46
#include <drizzled/check_stack_overrun.h>
45
47
#include <drizzled/lock.h>
46
#include <drizzled/plugin/listen.h>
47
#include "drizzled/cached_directory.h"
48
#include <drizzled/field/timestamp.h>
49
#include <drizzled/field/null.h>
50
#include "drizzled/sql_table.h"
51
#include "drizzled/global_charset_info.h"
52
#include "drizzled/pthread_globals.h"
53
#include "drizzled/internal/iocache.h"
54
#include "drizzled/drizzled.h"
55
#include "drizzled/plugin/authorization.h"
56
#include "drizzled/table/temporary.h"
57
#include "drizzled/table/placeholder.h"
58
#include "drizzled/table/unused.h"
51
@defgroup Data_Dictionary Data Dictionary
54
Table *unused_tables; /* Used by mysql_test */
55
HASH open_cache; /* Used by mysql_test */
56
static HASH table_def_cache;
57
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
58
static pthread_mutex_t LOCK_table_share;
59
static bool table_def_inited= 0;
61
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
63
char *cache_key, uint32_t cache_key_length);
64
static void free_cache_entry(void *entry);
65
static void close_old_data_files(Session *session, Table *table, bool morph_locks,
69
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
72
Table *entry=(Table*) record;
73
*length= entry->s->table_cache_key.length;
74
return (unsigned char*) entry->s->table_cache_key.str;
65
extern bool volatile shutdown_in_progress;
67
78
bool table_cache_init(void)
80
return hash_init(&open_cache, &my_charset_bin, table_cache_size+16,
81
0, 0, table_cache_key,
85
void table_cache_free(void)
89
close_cached_tables(NULL, NULL, false, false, false);
90
if (!open_cache.records) // Safety first
91
hash_free(&open_cache);
72
96
uint32_t cached_open_tables(void)
74
return table::getCache().size();
77
void table_cache_free(void)
79
refresh_version++; // Force close of open tables
81
table::getUnused().clear();
82
table::getCache().clear();
86
Close cursor handle, but leave the table in the table cache
89
close_handle_and_leave_table_as_lock()
93
By leaving the table in the table cache, it disallows any other thread
96
session->getKilled() will be set if we run out of memory
98
If closing a MERGE child, the calling function has to take care for
99
closing the parent too, if necessary.
98
return open_cache.records;
102
Create a table cache key
105
create_table_def_key()
106
session Thread handler
107
key Create key here (must be of size MAX_DBKEY_LENGTH)
108
table_list Table definition
109
tmp_table Set if table is a tmp table
112
The table cache_key is created from:
116
if the table is a tmp table, we add the following to make each tmp table
119
4 bytes for master thread id
120
4 bytes pseudo thread id
126
uint32_t create_table_def_key(Session *session, char *key, TableList *table_list,
131
key_pos= strcpy(key_pos, table_list->db) + strlen(table_list->db);
132
key_pos= strcpy(key_pos+1, table_list->table_name) +
133
strlen(table_list->table_name);
134
key_length= (uint32_t)(key_pos-key)+1;
138
int4store(key + key_length, session->server_id);
139
int4store(key + key_length + 4, session->variables.pseudo_thread_id);
140
key_length+= TMP_TABLE_KEY_EXTRA;
147
/*****************************************************************************
148
Functions to handle table definition cach (TABLE_SHARE)
149
*****************************************************************************/
151
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
154
TABLE_SHARE *entry=(TABLE_SHARE*) record;
155
*length= entry->table_cache_key.length;
156
return (unsigned char*) entry->table_cache_key.str;
160
static void table_def_free_entry(TABLE_SHARE *share)
164
/* remove from old_unused_share list */
165
pthread_mutex_lock(&LOCK_table_share);
166
*share->prev= share->next;
167
share->next->prev= share->prev;
168
pthread_mutex_unlock(&LOCK_table_share);
170
free_table_share(share);
175
bool table_def_init(void)
178
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
179
oldest_unused_share= &end_of_unused_share;
180
end_of_unused_share.prev= &oldest_unused_share;
182
return hash_init(&table_def_cache, &my_charset_bin, table_def_size,
184
(hash_free_key) table_def_free_entry, 0);
188
void table_def_free(void)
190
if (table_def_inited)
193
pthread_mutex_destroy(&LOCK_table_share);
194
hash_free(&table_def_cache);
200
uint32_t cached_table_definitions(void)
202
return table_def_cache.records;
207
Get TABLE_SHARE for a table.
210
session Thread handle
211
table_list Table that should be opened
213
key_length Length of key
214
db_flags Flags to open_table_def():
216
error out: Error code from open_table_def()
219
Get a table definition from the table definition cache.
220
If it doesn't exist, create a new from the table definition file.
223
We must have wrlock on LOCK_open when we come here
224
(To be changed later)
231
TABLE_SHARE *get_table_share(Session *session, TableList *table_list, char *key,
232
uint32_t key_length, uint32_t db_flags, int *error)
238
/* Read table definition from cache */
239
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
243
if (!(share= alloc_table_share(table_list, key, key_length)))
249
Lock mutex to be able to read table definition from file without
252
(void) pthread_mutex_lock(&share->mutex);
255
We assign a new table id under the protection of the LOCK_open and
256
the share's own mutex. We do this insted of creating a new mutex
257
and using it for the sole purpose of serializing accesses to a
258
static variable, we assign the table id here. We assign it to the
259
share before inserting it into the table_def_cache to be really
260
sure that it cannot be read from the cache without having a table
263
CAVEAT. This means that the table cannot be used for
264
binlogging/replication purposes, unless get_table_share() has been
265
called directly or indirectly.
267
assign_new_table_id(share);
269
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
271
free_table_share(share);
272
return(0); // return error
274
if (open_table_def(session, share, db_flags))
276
*error= share->error;
277
(void) hash_delete(&table_def_cache, (unsigned char*) share);
280
share->ref_count++; // Mark in use
281
(void) pthread_mutex_unlock(&share->mutex);
286
We found an existing table definition. Return it if we didn't get
287
an error when reading the table definition from file.
290
/* We must do a lock to ensure that the structure is initialized */
291
(void) pthread_mutex_lock(&share->mutex);
294
/* Table definition contained an error */
295
open_table_error(share, share->error, share->open_errno, share->errarg);
296
(void) pthread_mutex_unlock(&share->mutex);
300
if (!share->ref_count++ && share->prev)
303
Share was not used before and it was in the old_unused_share list
304
Unlink share from this list
306
pthread_mutex_lock(&LOCK_table_share);
307
*share->prev= share->next;
308
share->next->prev= share->prev;
311
pthread_mutex_unlock(&LOCK_table_share);
313
(void) pthread_mutex_unlock(&share->mutex);
315
/* Free cache if too big */
316
while (table_def_cache.records > table_def_size &&
317
oldest_unused_share->next)
319
pthread_mutex_lock(&oldest_unused_share->mutex);
320
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
328
Get a table share. If it didn't exist, try creating it from engine
330
For arguments and return values, see get_table_from_share()
334
*get_table_share_with_create(Session *session, TableList *table_list,
335
char *key, uint32_t key_length,
336
uint32_t db_flags, int *error)
340
share= get_table_share(session, table_list, key, key_length, db_flags, error);
342
If share is not NULL, we found an existing share.
344
If share is NULL, and there is no error, we're inside
345
pre-locking, which silences 'ER_NO_SUCH_TABLE' errors
346
with the intention to silently drop non-existing tables
347
from the pre-locking list. In this case we still need to try
348
auto-discover before returning a NULL share.
350
If share is NULL and the error is ER_NO_SUCH_TABLE, this is
351
the same as above, only that the error was not silenced by
352
pre-locking. Once again, we need to try to auto-discover
355
Finally, if share is still NULL, it's a real error and we need
358
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
360
if (share || (session->is_error() && (session->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
369
Mark that we are not using table share anymore.
372
release_table_share()
374
release_type How the release should be done:
376
- Release without checking
377
RELEASE_WAIT_FOR_DROP
378
- Don't return until we get a signal that the
379
table is deleted or the thread is killed.
382
If ref_count goes to zero and (we have done a refresh or if we have
383
already too many open table shares) then delete the definition.
385
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
386
that the table is deleted or the thread is killed.
389
void release_table_share(TABLE_SHARE *share,
392
bool to_be_deleted= 0;
394
safe_mutex_assert_owner(&LOCK_open);
396
pthread_mutex_lock(&share->mutex);
397
if (!--share->ref_count)
399
if (share->version != refresh_version)
403
/* Link share last in used_table_share list */
404
assert(share->next == 0);
405
pthread_mutex_lock(&LOCK_table_share);
406
share->prev= end_of_unused_share.prev;
407
*end_of_unused_share.prev= share;
408
end_of_unused_share.prev= &share->next;
409
share->next= &end_of_unused_share;
410
pthread_mutex_unlock(&LOCK_table_share);
412
to_be_deleted= (table_def_cache.records > table_def_size);
418
hash_delete(&table_def_cache, (unsigned char*) share);
421
pthread_mutex_unlock(&share->mutex);
427
Check if table definition exits in cache
430
get_cached_table_share()
432
table_name Table name
436
# TABLE_SHARE for table
439
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
441
char key[NAME_LEN*2+2];
442
TableList table_list;
444
safe_mutex_assert_owner(&LOCK_open);
446
table_list.db= (char*) db;
447
table_list.table_name= (char*) table_name;
448
key_length= create_table_def_key((Session*) 0, key, &table_list, 0);
449
return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
454
Close file handle, but leave the table in the table cache
457
close_handle_and_leave_table_as_lock()
461
By leaving the table in the table cache, it disallows any other thread
464
session->killed will be set if we run out of memory
466
If closing a MERGE child, the calling function has to take care for
467
closing the parent too, if necessary.
103
471
void close_handle_and_leave_table_as_lock(Table *table)
473
TABLE_SHARE *share, *old_share= table->s;
475
MEM_ROOT *mem_root= &table->mem_root;
105
477
assert(table->db_stat);
106
assert(table->getShare()->getType() == message::Table::STANDARD);
109
480
Make a local copy of the table share and free the current one.
110
481
This has to be done to ensure that the table share is removed from
111
482
the table defintion cache as soon as the last instance is removed
113
TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
114
const TableIdentifier::Key &key(identifier.getKey());
115
TableShare *share= new TableShare(identifier.getType(),
117
const_cast<char *>(key.vector()), static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
119
table->cursor->close();
120
table->db_stat= 0; // Mark cursor closed
121
TableShare::release(table->getMutableShare());
122
table->setShare(share);
484
if (multi_alloc_root(mem_root,
485
&share, sizeof(*share),
486
&key_buff, old_share->table_cache_key.length,
489
memset(share, 0, sizeof(*share));
490
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
491
old_share->table_cache_key.length);
492
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
495
table->file->close();
496
table->db_stat= 0; // Mark file closed
497
release_table_share(table->s, RELEASE_NORMAL);
499
table->file->change_table_ptr(table, table->s);
507
Create a list for all open tables matching SQL expression
511
wild SQL like expression
514
One gets only a list of tables for which one has any kind of privilege.
515
db and table names are allocated in result struct, so one doesn't need
516
a lock on LOCK_open when traversing the return list.
519
NULL Error (Probably OOM)
520
# Pointer to list of names of open tables.
523
OPEN_TableList *list_open_tables(const char *db, const char *wild)
526
OPEN_TableList **start_list, *open_list;
527
TableList table_list;
529
pthread_mutex_lock(&LOCK_open);
530
memset(&table_list, 0, sizeof(table_list));
531
start_list= &open_list;
534
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
536
OPEN_TableList *table;
537
Table *entry=(Table*) hash_element(&open_cache,idx);
538
TABLE_SHARE *share= entry->s;
540
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
542
if (wild && wild_compare(share->table_name.str, wild, 0))
545
/* Check if user has SELECT privilege for any column in the table */
546
table_list.db= share->db.str;
547
table_list.table_name= share->table_name.str;
549
/* need to check if we haven't already listed it */
550
for (table= open_list ; table ; table=table->next)
552
if (!strcmp(table->table, share->table_name.str) &&
553
!strcmp(table->db, share->db.str))
557
if (entry->locked_by_name)
564
if (!(*start_list = (OPEN_TableList *)
565
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
567
open_list=0; // Out of memory
570
strcpy((*start_list)->table=
571
strcpy(((*start_list)->db= (char*) ((*start_list)+1)),
572
share->db.str)+share->db.length+1,
573
share->table_name.str);
574
(*start_list)->in_use= entry->in_use ? 1 : 0;
575
(*start_list)->locked= entry->locked_by_name ? 1 : 0;
576
start_list= &(*start_list)->next;
579
pthread_mutex_unlock(&LOCK_open);
126
583
/*****************************************************************************
127
584
* Functions to free open table cache
128
585
****************************************************************************/
131
void Table::intern_close_table()
588
void intern_close_table(Table *table)
132
589
{ // Free all structures
134
if (cursor) // Not true if name lock
590
free_io_cache(table);
591
if (table->file) // Not true if name lock
592
table->closefrm(true); // close file
597
Remove table from the open table cache
601
entry Table to remove
604
We need to have a lock on LOCK_open when calling this
607
static void free_cache_entry(void *entry)
609
Table *table= static_cast<Table *>(entry);
610
intern_close_table(table);
136
delete_table(true); // close cursor
613
table->next->prev=table->prev; /* remove from used chain */
614
table->prev->next=table->next;
615
if (table == unused_tables)
617
unused_tables=unused_tables->next;
618
if (table == unused_tables)
140
626
/* Free resources allocated by filesort() and read_record() */
142
void Table::free_io_cache()
628
void free_io_cache(Table *table)
630
if (table->sort.io_cache)
146
sort.io_cache->close_cached_file();
147
delete sort.io_cache;
632
close_cached_file(table->sort.io_cache);
633
delete table->sort.io_cache;
634
table->sort.io_cache= 0;
154
641
Close all tables which aren't in use by any thread
156
@param session Thread context (may be NULL)
643
@param session Thread context
157
644
@param tables List of tables to remove from the cache
158
@param have_lock If table::Cache::singleton().mutex() is locked
645
@param have_lock If LOCK_open is locked
159
646
@param wait_for_refresh Wait for a impending flush
160
647
@param wait_for_placeholders Wait for tables being reopened so that the GRL
161
won't proceed while write-locked tables are being reopened by other
648
won't proceed while write-locked tables are being reopened by other
164
651
@remark Session can be NULL, but then wait_for_refresh must be false
165
and tables must be NULL.
168
bool Session::close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders)
652
and tables must be NULL.
655
bool close_cached_tables(Session *session, TableList *tables, bool have_lock,
656
bool wait_for_refresh, bool wait_for_placeholders)
659
assert(session || (!wait_for_refresh && !tables));
662
pthread_mutex_lock(&LOCK_open);
665
refresh_version++; // Force close of open tables
666
while (unused_tables)
669
if (hash_delete(&open_cache,(unsigned char*) unused_tables))
670
printf("Warning: Couldn't delete open table from hash\n");
672
hash_delete(&open_cache,(unsigned char*) unused_tables);
675
/* Free table shares */
676
while (oldest_unused_share->next)
678
pthread_mutex_lock(&oldest_unused_share->mutex);
679
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
681
if (wait_for_refresh)
684
Other threads could wait in a loop in open_and_lock_tables(),
685
trying to lock one or more of our tables.
687
If they wait for the locks in thr_multi_lock(), their lock
688
request is aborted. They loop in open_and_lock_tables() and
689
enter open_table(). Here they notice the table is refreshed and
690
wait for COND_refresh. Then they loop again in
691
open_and_lock_tables() and this time open_table() succeeds. At
692
this moment, if we (the FLUSH TABLES thread) are scheduled and
693
on another FLUSH TABLES enter close_cached_tables(), they could
694
awake while we sleep below, waiting for others threads (us) to
695
close their open tables. If this happens, the other threads
696
would find the tables unlocked. They would get the locks, one
697
after the other, and could do their destructive work. This is an
698
issue if we have LOCK TABLES in effect.
700
The problem is that the other threads passed all checks in
701
open_table() before we refresh the table.
703
The fix for this problem is to set some_tables_deleted for all
704
threads with open tables. These threads can still get their
705
locks, but will immediately release them again after checking
706
this variable. They will then loop in open_and_lock_tables()
707
again. There they will wait until we update all tables version
710
Setting some_tables_deleted is done by remove_table_from_cache()
713
In other words (reviewer suggestion): You need this setting of
714
some_tables_deleted for the case when table was opened and all
715
related checks were passed before incrementing refresh_version
716
(which you already have) but attempt to lock the table happened
717
after the call to close_old_data_files() i.e. after removal of
718
current thread locks.
720
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
722
Table *table=(Table*) hash_element(&open_cache,idx);
724
table->in_use->some_tables_deleted= 1;
731
for (TableList *table= tables; table; table= table->next_local)
733
if (remove_table_from_cache(session, table->db, table->table_name,
734
RTFC_OWNED_BY_Session_FLAG))
738
wait_for_refresh=0; // Nothing to wait for
741
if (wait_for_refresh)
744
If there is any table that has a lower refresh_version, wait until
745
this is closed (or this thread is killed) before returning
747
session->mysys_var->current_mutex= &LOCK_open;
748
session->mysys_var->current_cond= &COND_refresh;
749
session->set_proc_info("Flushing tables");
751
close_old_data_files(session,session->open_tables,1,1);
752
mysql_ha_flush(session);
755
/* Wait until all threads has closed all the tables we had locked */
756
while (found && ! session->killed)
759
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
761
Table *table=(Table*) hash_element(&open_cache,idx);
762
/* Avoid a self-deadlock. */
763
if (table->in_use == session)
766
Note that we wait here only for tables which are actually open, and
767
not for placeholders with Table::open_placeholder set. Waiting for
768
latter will cause deadlock in the following scenario, for example:
770
conn1: lock table t1 write;
771
conn2: lock table t2 write;
775
It also does not make sense to wait for those of placeholders that
776
are employed by CREATE TABLE as in this case table simply does not
779
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
780
(table->open_placeholder && wait_for_placeholders)))
783
pthread_cond_wait(&COND_refresh,&LOCK_open);
789
No other thread has the locked tables open; reopen them and get the
790
old locks. This should always succeed (unless some external process
791
has removed the tables)
793
session->in_lock_tables=1;
794
result=reopen_tables(session,1,1);
795
session->in_lock_tables=0;
796
/* Set version for table */
797
for (Table *table=session->open_tables; table ; table= table->next)
800
Preserve the version (0) of write locked tables so that a impending
801
global read lock won't sneak in.
803
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
804
table->s->version= refresh_version;
808
pthread_mutex_unlock(&LOCK_open);
809
if (wait_for_refresh)
811
pthread_mutex_lock(&session->mysys_var->mutex);
812
session->mysys_var->current_mutex= 0;
813
session->mysys_var->current_cond= 0;
814
session->set_proc_info(0);
815
pthread_mutex_unlock(&session->mysys_var->mutex);
822
Close all tables which match specified connection string or
823
if specified string is NULL, then any table with a connection string.
826
bool close_cached_connection_tables(Session *session, bool if_wait_for_refresh,
827
LEX_STRING *connection, bool have_lock)
830
TableList tmp, *tables= NULL;
170
831
bool result= false;
171
Session *session= this;
834
memset(&tmp, 0, sizeof(TableList));
837
pthread_mutex_lock(&LOCK_open);
839
for (idx= 0; idx < table_def_cache.records; idx++)
174
table::Cache::singleton().mutex().lock(); /* Optionally lock for remove tables from open_cahe if not in use */
178
refresh_version++; // Force close of open tables
180
table::getUnused().clear();
182
if (wait_for_refresh)
185
Other threads could wait in a loop in open_and_lock_tables(),
186
trying to lock one or more of our tables.
188
If they wait for the locks in thr_multi_lock(), their lock
189
request is aborted. They loop in open_and_lock_tables() and
190
enter open_table(). Here they notice the table is refreshed and
191
wait for COND_refresh. Then they loop again in
192
openTablesLock() and this time open_table() succeeds. At
193
this moment, if we (the FLUSH TABLES thread) are scheduled and
194
on another FLUSH TABLES enter close_cached_tables(), they could
195
awake while we sleep below, waiting for others threads (us) to
196
close their open tables. If this happens, the other threads
197
would find the tables unlocked. They would get the locks, one
198
after the other, and could do their destructive work. This is an
199
issue if we have LOCK TABLES in effect.
201
The problem is that the other threads passed all checks in
202
open_table() before we refresh the table.
204
The fix for this problem is to set some_tables_deleted for all
205
threads with open tables. These threads can still get their
206
locks, but will immediately release them again after checking
207
this variable. They will then loop in openTablesLock()
208
again. There they will wait until we update all tables version
211
Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
214
In other words (reviewer suggestion): You need this setting of
215
some_tables_deleted for the case when table was opened and all
216
related checks were passed before incrementing refresh_version
217
(which you already have) but attempt to lock the table happened
218
after the call to Session::close_old_data_files() i.e. after removal of
219
current thread locks.
221
for (table::CacheMap::const_iterator iter= table::getCache().begin();
222
iter != table::getCache().end();
225
Table *table= (*iter).second;
227
table->in_use->some_tables_deleted= false;
234
for (TableList *table= tables; table; table= table->next_local)
236
TableIdentifier identifier(table->getSchemaName(), table->getTableName());
237
if (table::Cache::singleton().removeTable(session, identifier,
238
RTFC_OWNED_BY_Session_FLAG))
244
wait_for_refresh= false; // Nothing to wait for
247
if (wait_for_refresh)
250
If there is any table that has a lower refresh_version, wait until
251
this is closed (or this thread is killed) before returning
253
session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
254
session->mysys_var->current_cond= &COND_refresh;
255
session->set_proc_info("Flushing tables");
257
session->close_old_data_files();
260
/* Wait until all threads has closed all the tables we had locked */
261
while (found && ! session->getKilled())
264
for (table::CacheMap::const_iterator iter= table::getCache().begin();
265
iter != table::getCache().end();
268
Table *table= (*iter).second;
269
/* Avoid a self-deadlock. */
270
if (table->in_use == session)
273
Note that we wait here only for tables which are actually open, and
274
not for placeholders with Table::open_placeholder set. Waiting for
275
latter will cause deadlock in the following scenario, for example:
277
conn1-> lock table t1 write;
278
conn2-> lock table t2 write;
279
conn1-> flush tables;
280
conn2-> flush tables;
282
It also does not make sense to wait for those of placeholders that
283
are employed by CREATE TABLE as in this case table simply does not
286
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
287
(table->open_placeholder && wait_for_placeholders)))
290
boost_unique_lock_t scoped(table::Cache::singleton().mutex(), boost::adopt_lock_t());
291
COND_refresh.wait(scoped);
298
No other thread has the locked tables open; reopen them and get the
299
old locks. This should always succeed (unless some external process
300
has removed the tables)
302
result= session->reopen_tables(true, true);
304
/* Set version for table */
305
for (Table *table= session->open_tables; table ; table= table->getNext())
308
Preserve the version (0) of write locked tables so that a impending
309
global read lock won't sneak in.
311
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
312
table->getMutableShare()->refreshVersion();
316
table::Cache::singleton().mutex().unlock();
841
TABLE_SHARE *share= (TABLE_SHARE *) hash_element(&table_def_cache, idx);
843
/* Ignore if table is not open or does not have a connect_string */
844
if (!share->connect_string.length || !share->ref_count)
847
/* Compare the connection string */
849
(connection->length > share->connect_string.length ||
850
(connection->length < share->connect_string.length &&
851
(share->connect_string.str[connection->length] != '/' &&
852
share->connect_string.str[connection->length] != '\\')) ||
853
strncasecmp(connection->str, share->connect_string.str,
854
connection->length)))
857
/* close_cached_tables() only uses these elements */
858
tmp.db= share->db.str;
859
tmp.table_name= share->table_name.str;
860
tmp.next_local= tables;
862
tables= (TableList *) memdup_root(session->mem_root, (char*)&tmp,
319
if (wait_for_refresh)
867
result= close_cached_tables(session, tables, true, false, false);
870
pthread_mutex_unlock(&LOCK_open);
872
if (if_wait_for_refresh)
321
boost_unique_lock_t scopedLock(session->mysys_var->mutex);
874
pthread_mutex_lock(&session->mysys_var->mutex);
322
875
session->mysys_var->current_mutex= 0;
323
876
session->mysys_var->current_cond= 0;
324
877
session->set_proc_info(0);
878
pthread_mutex_unlock(&session->mysys_var->mutex);
332
move one table to free list
335
bool Session::free_cached_table()
337
bool found_old_table= false;
338
table::Concurrent *table= static_cast<table::Concurrent *>(open_tables);
340
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
341
assert(table->key_read == 0);
342
assert(!table->cursor || table->cursor->inited == Cursor::NONE);
344
open_tables= table->getNext();
346
if (table->needs_reopen_or_name_lock() ||
347
version != refresh_version || !table->db_stat)
349
table::remove_table(table);
350
found_old_table= true;
355
Open placeholders have Table::db_stat set to 0, so they should be
356
handled by the first alternative.
358
assert(not table->open_placeholder);
360
/* Free memory and reset for next loop */
361
table->cursor->ha_reset();
364
table::getUnused().link(table);
367
return found_old_table;
886
Mark all temporary tables which were used by the current statement or
887
substatement as free for reuse, but only if the query_id can be cleared.
889
@param session thread context
891
@remark For temp tables associated with a open SQL HANDLER the query_id
892
is not reset until the HANDLER is closed.
895
static void mark_temp_tables_as_free_for_reuse(Session *session)
897
for (Table *table= session->temporary_tables ; table ; table= table->next)
899
if ((table->query_id == session->query_id) && ! table->open_by_handler)
902
table->file->ha_reset();
909
Mark all tables in the list which were used by current substatement
913
mark_used_tables_as_free_for_reuse()
914
session - thread context
915
table - head of the list of tables
918
Marks all tables in the list which were used by current substatement
919
(they are marked by its query_id) as free for reuse.
922
The reason we reset query_id is that it's not enough to just test
923
if table->query_id != session->query_id to know if a table is in use.
926
SELECT f1_that_uses_t1() FROM t1;
927
In f1_that_uses_t1() we will see one instance of t1 where query_id is
928
set to query_id of original query.
931
static void mark_used_tables_as_free_for_reuse(Session *session, Table *table)
933
for (; table ; table= table->next)
935
if (table->query_id == session->query_id)
938
table->file->ha_reset();
627
1320
@retval 0 the table was found and dropped successfully.
628
1321
@retval 1 the table was not found in the list of temporary tables
630
1323
@retval -1 the table is in use by a outer query
633
int Open_tables_state::drop_temporary_table(const drizzled::TableIdentifier &identifier)
1326
int drop_temporary_table(Session *session, TableList *table_list)
637
if (not (table= find_temporary_table(identifier)))
1330
if (!(table= find_temporary_table(session, table_list)))
640
1333
/* Table might be in use by some outer statement. */
641
if (table->query_id && table->query_id != getQueryId())
643
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
647
close_temporary_table(table);
1334
if (table->query_id && table->query_id != session->query_id)
1336
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1341
If LOCK TABLES list is not empty and contains this table,
1342
unlock the table and remove the table from this list.
1344
mysql_lock_remove(session, session->locked_tables, table, false);
1345
close_temporary_table(session, table, 1, 1);
1350
unlink from session->temporary tables and close temporary table
1353
void close_temporary_table(Session *session, Table *table,
1354
bool free_share, bool delete_table)
1358
table->prev->next= table->next;
1359
if (table->prev->next)
1360
table->next->prev= table->prev;
1364
/* removing the item from the list */
1365
assert(table == session->temporary_tables);
1367
slave must reset its temporary list pointer to zero to exclude
1368
passing non-zero value to end_slave via rli->save_temporary_tables
1369
when no temp tables opened, see an invariant below.
1371
session->temporary_tables= table->next;
1372
if (session->temporary_tables)
1373
table->next->prev= 0;
1375
close_temporary(table, free_share, delete_table);
1381
Close and delete a temporary table
1384
This dosn't unlink table from session->temporary
1385
If this is needed, use close_temporary_table()
1388
void close_temporary(Table *table, bool free_share, bool delete_table)
1390
handlerton *table_type= table->s->db_type();
1392
free_io_cache(table);
1393
table->closefrm(false);
1396
rm_temporary_table(table_type, table->s->path.str);
1400
free_table_share(table->s);
1401
free((char*) table);
1408
Used by ALTER Table when the table is a temporary one. It changes something
1409
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1411
Prepares a table cache key, which is the concatenation of db, table_name and
1412
session->slave_proxy_id, separated by '\0'.
1415
bool rename_temporary_table(Session* session, Table *table, const char *db,
1416
const char *table_name)
1419
uint32_t key_length;
1420
TABLE_SHARE *share= table->s;
1421
TableList table_list;
1423
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1424
return true; /* purecov: inspected */
1426
table_list.db= (char*) db;
1427
table_list.table_name= (char*) table_name;
1428
key_length= create_table_def_key(session, key, &table_list, 1);
1429
share->set_table_cache_key(key, key_length);
1435
/* move table first in unused links */
1437
static void relink_unused(Table *table)
1439
if (table != unused_tables)
1441
table->prev->next=table->next; /* Remove from unused list */
1442
table->next->prev=table->prev;
1443
table->next=unused_tables; /* Link in unused tables */
1444
table->prev=unused_tables->prev;
1445
unused_tables->prev->next=table;
1446
unused_tables->prev=table;
1447
unused_tables=table;
654
Remove all instances of table from thread's open list and
657
@param session Thread context
658
@param find Table to remove
660
@note because we risk the chance of deleting the share, we can't assume that it will exist past, this should be modified once we can use a TableShare::shared_ptr here.
1453
Remove all instances of table from thread's open list and
1456
@param session Thread context
1457
@param find Table to remove
1458
@param unlock true - free all locks on tables removed that are
1459
done with LOCK TABLES
1462
@note When unlock parameter is false or current thread doesn't have
1463
any tables locked with LOCK TABLES, tables are assumed to be
1464
not locked (for example already unlocked).
663
void Session::unlink_open_table(Table *find)
1467
void unlink_open_table(Session *session, Table *find, bool unlock)
665
const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
667
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
1469
char key[MAX_DBKEY_LENGTH];
1470
uint32_t key_length= find->s->table_cache_key.length;
1471
Table *list, **prev;
1473
safe_mutex_assert_owner(&LOCK_open);
1475
memcpy(key, find->s->table_cache_key.str, key_length);
670
Note that we need to hold table::Cache::singleton().mutex() while changing the
1477
Note that we need to hold LOCK_open while changing the
671
1478
open_tables list. Another thread may work on it.
672
(See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
1479
(See: remove_table_from_cache(), mysql_wait_completed_table())
673
1480
Closing a MERGE child before the parent would be fatal if the
674
1481
other thread tries to abort the MERGE lock in between.
676
for (prev= &open_tables; *prev; )
1483
for (prev= &session->open_tables; *prev; )
680
if (list->getShare()->getCacheKey() == find_key)
1487
if (list->s->table_cache_key.length == key_length &&
1488
!memcmp(list->s->table_cache_key.str, key, key_length))
1490
if (unlock && session->locked_tables)
1491
mysql_lock_remove(session, session->locked_tables, list, true);
682
1493
/* Remove table from open_tables list. */
683
*prev= list->getNext();
685
1495
/* Close table. */
686
table::remove_table(static_cast<table::Concurrent *>(list));
1496
hash_delete(&open_cache,(unsigned char*) list); // Close table
690
1500
/* Step to next entry in open_tables list. */
691
prev= list->getNextPtr();
695
1505
// Notify any 'refresh' threads
696
locking::broadcast_refresh();
1506
broadcast_refresh();
701
Auxiliary routine which closes and drops open table.
703
@param session Thread handle
704
@param table Table object for table to be dropped
705
@param db_name Name of database for this table
706
@param table_name Name of this table
708
@note This routine assumes that table to be closed is open only
709
by calling thread so we needn't wait until other threads
710
will close the table. Also unless called under implicit or
711
explicit LOCK TABLES mode it assumes that table to be
712
dropped is already unlocked. In the former case it will
713
also remove lock on the table. But one should not rely on
714
this behaviour as it may change in future.
715
Currently, however, this function is never called for a
716
table that was locked with LOCK TABLES.
1512
Auxiliary routine which closes and drops open table.
1514
@param session Thread handle
1515
@param table Table object for table to be dropped
1516
@param db_name Name of database for this table
1517
@param table_name Name of this table
1519
@note This routine assumes that table to be closed is open only
1520
by calling thread so we needn't wait until other threads
1521
will close the table. Also unless called under implicit or
1522
explicit LOCK TABLES mode it assumes that table to be
1523
dropped is already unlocked. In the former case it will
1524
also remove lock on the table. But one should not rely on
1525
this behaviour as it may change in future.
1526
Currently, however, this function is never called for a
1527
table that was locked with LOCK TABLES.
719
void Session::drop_open_table(Table *table, const TableIdentifier &identifier)
1530
void drop_open_table(Session *session, Table *table, const char *db_name,
1531
const char *table_name)
721
if (table->getShare()->getType())
723
close_temporary_table(table);
1533
if (table->s->tmp_table)
1534
close_temporary_table(session, table, 1, 1);
727
boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close and drop a table (AUX routine) */
1537
handlerton *table_type= table->s->db_type();
1538
pthread_mutex_lock(&LOCK_open);
729
1540
unlink_open_table() also tells threads waiting for refresh or close
730
1541
that something has happened.
732
unlink_open_table(table);
733
plugin::StorageEngine::dropTable(*this, identifier);
1543
unlink_open_table(session, table, false);
1544
quick_rm_table(table_type, db_name, table_name, 0);
1545
pthread_mutex_unlock(&LOCK_open);
739
Wait for condition but allow the user to send a kill to mysqld
1551
Wait for condition but allow the user to send a kill to mysqld
743
session Thread Cursor
744
mutex mutex that is currently hold that is associated with condition
745
Will be unlocked on return
746
cond Condition to wait for
1554
wait_for_condition()
1555
session Thread handler
1556
mutex mutex that is currently hold that is associated with condition
1557
Will be unlocked on return
1558
cond Condition to wait for
749
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
1561
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
751
1563
/* Wait until the current table is up to date */
752
const char *saved_proc_info;
753
mysys_var->current_mutex= &mutex;
754
mysys_var->current_cond= &cond;
755
saved_proc_info= get_proc_info();
756
set_proc_info("Waiting for table");
759
We must unlock mutex first to avoid deadlock becasue conditions are
760
sent to this thread by doing locks in the following order:
761
lock(mysys_var->mutex)
762
lock(mysys_var->current_mutex)
764
One by effect of this that one can only use wait_for_condition with
765
condition variables that are guranteed to not disapper (freed) even if this
768
boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
771
cond.wait(scopedLock);
774
boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
775
mysys_var->current_mutex= 0;
776
mysys_var->current_cond= 0;
777
set_proc_info(saved_proc_info);
782
Create and insert into table cache placeholder for table
783
which will prevent its opening (or creation) (a.k.a lock
786
@param session Thread context
787
@param key Table cache key for name to be locked
788
@param key_length Table cache key length
790
@return Pointer to Table object used for name locking or 0 in
794
table::Placeholder *Session::table_cache_insert_placeholder(const drizzled::TableIdentifier &arg)
796
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
1564
const char *proc_info;
1565
session->mysys_var->current_mutex= mutex;
1566
session->mysys_var->current_cond= cond;
1567
proc_info=session->get_proc_info();
1568
session->set_proc_info("Waiting for table");
1569
if (!session->killed)
1570
(void) pthread_cond_wait(cond, mutex);
1573
We must unlock mutex first to avoid deadlock becasue conditions are
1574
sent to this thread by doing locks in the following order:
1575
lock(mysys_var->mutex)
1576
lock(mysys_var->current_mutex)
1578
One by effect of this that one can only use wait_for_condition with
1579
condition variables that are guranteed to not disapper (freed) even if this
1583
pthread_mutex_unlock(mutex);
1584
pthread_mutex_lock(&session->mysys_var->mutex);
1585
session->mysys_var->current_mutex= 0;
1586
session->mysys_var->current_cond= 0;
1587
session->set_proc_info(proc_info);
1588
pthread_mutex_unlock(&session->mysys_var->mutex);
1594
Exclusively name-lock a table that is already write-locked by the
1597
@param session current thread context
1598
@param tables table list containing one table to open.
1600
@return false on success, true otherwise.
1603
bool name_lock_locked_table(Session *session, TableList *tables)
1605
/* Under LOCK TABLES we must only accept write locked tables. */
1606
tables->table= find_locked_table(session, tables->db, tables->table_name);
1609
my_error(ER_TABLE_NOT_LOCKED, MYF(0), tables->alias);
1610
else if (tables->table->reginfo.lock_type < TL_WRITE_LOW_PRIORITY)
1611
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), tables->alias);
1615
Ensures that table is opened only by this thread and that no
1616
other statement will open this table.
1618
wait_while_table_is_used(session, tables->table, HA_EXTRA_FORCE_REOPEN);
1627
Open table which is already name-locked by this thread.
1630
reopen_name_locked_table()
1631
session Thread handle
1632
table_list TableList object for table to be open, TableList::table
1633
member should point to Table object which was used for
1635
link_in true - if Table object for table to be opened should be
1636
linked into Session::open_tables list.
1637
false - placeholder used for name-locking is already in
1638
this list so we only need to preserve Table::next
1642
This function assumes that its caller already acquired LOCK_open mutex.
1649
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in)
1651
Table *table= table_list->table;
1653
char *table_name= table_list->table_name;
1656
safe_mutex_assert_owner(&LOCK_open);
1658
if (session->killed || !table)
1663
if (open_unireg_entry(session, table, table_list, table_name,
1664
table->s->table_cache_key.str,
1665
table->s->table_cache_key.length))
1667
intern_close_table(table);
1669
If there was an error during opening of table (for example if it
1670
does not exist) '*table' object can be wiped out. To be able
1671
properly release name-lock in this case we should restore this
1672
object to its original state.
1680
We want to prevent other connections from opening this table until end
1681
of statement as it is likely that modifications of table's metadata are
1682
not yet finished (for example CREATE TRIGGER have to change .TRG file,
1683
or we might want to drop table if CREATE TABLE ... SELECT fails).
1684
This also allows us to assume that no other connection will sneak in
1685
before we will get table-level lock on this table.
1688
table->in_use = session;
1692
table->next= session->open_tables;
1693
session->open_tables= table;
1698
Table object should be already in Session::open_tables list so we just
1699
need to set Table::next correctly.
1701
table->next= orig_table.next;
1704
table->tablenr=session->current_tablenr++;
1705
table->used_fields=0;
1706
table->const_table=0;
1707
table->null_row= false;
1708
table->maybe_null= false;
1709
table->force_index= false;
1710
table->status=STATUS_NO_RECORD;
1716
Create and insert into table cache placeholder for table
1717
which will prevent its opening (or creation) (a.k.a lock
1720
@param session Thread context
1721
@param key Table cache key for name to be locked
1722
@param key_length Table cache key length
1724
@return Pointer to Table object used for name locking or 0 in
1728
Table *table_cache_insert_placeholder(Session *session, const char *key,
1729
uint32_t key_length)
1735
safe_mutex_assert_owner(&LOCK_open);
799
1738
Create a table entry with the right key and with an old refresh version
1739
Note that we must use my_multi_malloc() here as this is freed by the
801
TableIdentifier identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
802
table::Placeholder *table= new table::Placeholder(this, identifier);
804
if (not table::Cache::singleton().insert(table))
1742
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
1743
&table, sizeof(*table),
1744
&share, sizeof(*share),
1745
&key_buff, key_length,
1750
share->set_table_cache_key(key_buff, key, key_length);
1751
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1752
table->in_use= session;
1753
table->locked_by_name=1;
1755
if (my_hash_insert(&open_cache, (unsigned char*)table))
1757
free((unsigned char*) table);
816
Obtain an exclusive name lock on the table if it is not cached
819
@param session Thread context
820
@param db Name of database
821
@param table_name Name of table
822
@param[out] table Out parameter which is either:
823
- set to NULL if table cache contains record for
825
- set to point to the Table instance used for
828
@note This function takes into account all records for table in table
829
cache, even placeholders used for name-locking. This means that
830
'table' parameter can be set to NULL for some situations when
831
table does not really exist.
833
@retval true Error occured (OOM)
834
@retval false Success. 'table' parameter set according to above rules.
1766
Obtain an exclusive name lock on the table if it is not cached
1769
@param session Thread context
1770
@param db Name of database
1771
@param table_name Name of table
1772
@param[out] table Out parameter which is either:
1773
- set to NULL if table cache contains record for
1775
- set to point to the Table instance used for
1778
@note This function takes into account all records for table in table
1779
cache, even placeholders used for name-locking. This means that
1780
'table' parameter can be set to NULL for some situations when
1781
table does not really exist.
1783
@retval true Error occured (OOM)
1784
@retval false Success. 'table' parameter set according to above rules.
836
bool Session::lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table)
1787
bool lock_table_name_if_not_cached(Session *session, const char *db,
1788
const char *table_name, Table **table)
838
const TableIdentifier::Key &key(identifier.getKey());
840
boost_unique_lock_t scope_lock(table::Cache::singleton().mutex()); /* Obtain a name lock even though table is not in cache (like for create table) */
842
table::CacheMap::iterator iter;
844
iter= table::getCache().find(key);
846
if (iter != table::getCache().end())
1790
char key[MAX_DBKEY_LENGTH];
1792
uint32_t key_length;
1794
key_pos= strcpy(key_pos, db) + strlen(db);
1795
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
1796
key_length= (uint32_t) (key_pos-key)+1;
1798
pthread_mutex_lock(&LOCK_open);
1800
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1802
pthread_mutex_unlock(&LOCK_open);
852
if (not (*table= table_cache_insert_placeholder(identifier)))
1806
if (!(*table= table_cache_insert_placeholder(session, key, key_length)))
1808
pthread_mutex_unlock(&LOCK_open);
856
(*table)->open_placeholder= true;
857
(*table)->setNext(open_tables);
1811
(*table)->open_placeholder= 1;
1812
(*table)->next= session->open_tables;
1813
session->open_tables= *table;
1814
pthread_mutex_unlock(&LOCK_open);
868
session Thread context.
869
table_list Open first table in list.
870
refresh INOUT Pointer to memory that will be set to 1 if
871
we need to close all tables and reopen them.
872
If this is a NULL pointer, then the table is not
873
put in the thread-open-list.
874
flags Bitmap of flags to modify how open works:
875
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
876
someone has done a flush or namelock on it.
877
No version number checking is done.
878
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
879
table not the base table or view.
1823
session Thread context.
1824
table_list Open first table in list.
1825
refresh INOUT Pointer to memory that will be set to 1 if
1826
we need to close all tables and reopen them.
1827
If this is a NULL pointer, then the table is not
1828
put in the thread-open-list.
1829
flags Bitmap of flags to modify how open works:
1830
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1831
someone has done a flush or namelock on it.
1832
No version number checking is done.
1833
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1834
table not the base table or view.
882
Uses a cache of open tables to find a table not in use.
1837
Uses a cache of open tables to find a table not in use.
884
If table list element for the table to be opened has "create" flag
885
set and table does not exist, this function will automatically insert
886
a placeholder for exclusive name lock into the open tables cache and
887
will return the Table instance that corresponds to this placeholder.
1839
If table list element for the table to be opened has "create" flag
1840
set and table does not exist, this function will automatically insert
1841
a placeholder for exclusive name lock into the open tables cache and
1842
will return the Table instance that corresponds to this placeholder.
890
NULL Open failed. If refresh is set then one should close
891
all other tables and retry the open.
892
# Success. Pointer to Table object for open table.
1845
NULL Open failed. If refresh is set then one should close
1846
all other tables and retry the open.
1847
# Success. Pointer to Table object for open table.
896
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
1851
Table *open_table(Session *session, TableList *table_list, bool *refresh, uint32_t flags)
899
const char *alias= table_list->alias;
1853
register Table *table;
1854
char key[MAX_DBKEY_LENGTH];
1855
unsigned int key_length;
1856
char *alias= table_list->alias;
1857
HASH_SEARCH_STATE state;
901
1859
/* Parsing of partitioning information from .frm needs session->lex set up. */
902
assert(lex->is_lex_started);
1860
assert(session->lex->is_lex_started);
904
1862
/* find a unused table in the open table cache */
908
1866
/* an open table operation needs a lot of the stack space */
909
if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
915
TableIdentifier identifier(table_list->getSchemaName(), table_list->getTableName());
916
const TableIdentifier::Key &key(identifier.getKey());
917
table::CacheRange ppp;
1867
if (check_stack_overrun(session, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
1870
if (session->killed)
1873
key_length= (create_table_def_key(session, key, table_list, 1) -
1874
TMP_TABLE_KEY_EXTRA);
920
1877
Unless requested otherwise, try to resolve this table in the list
921
1878
of temporary tables of this thread. In MySQL temporary tables
922
1879
are always thread-local and "shadow" possible base tables with the
923
1880
same name. This block implements the behaviour.
924
TODO -> move this block into a separate function.
1881
TODO: move this block into a separate function.
927
for (table= getTemporaryTables(); table ; table=table->getNext())
929
if (table->getShare()->getCacheKey() == key)
932
We're trying to use the same temporary table twice in a query.
933
Right now we don't support this because a temporary table
934
is always represented by only one Table object in Session, and
935
it can not be cloned. Emit an error for an unsupported behaviour.
939
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
942
table->query_id= getQueryId();
950
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
952
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->getSchemaName(), table_list->getTableName());
957
If it's the first table from a list of tables used in a query,
958
remember refresh_version (the version of open_cache state).
959
If the version changes while we're opening the remaining tables,
960
we will have to back off, close all the tables opened-so-far,
961
and try to reopen them.
963
Note-> refresh_version is currently changed only during FLUSH TABLES.
967
version= refresh_version;
969
else if ((version != refresh_version) &&
970
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
972
/* Someone did a refresh while thread was opening tables */
980
Before we test the global cache, we test our local session cache.
984
assert(false); /* Not implemented yet */
988
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
989
this is the normal use case.
991
- try to find the table in the table cache.
992
- if one of the discovered Table instances is name-locked
993
(table->getShare()->version == 0) back off -- we have to wait
994
until no one holds a name lock on the table.
995
- if there is no such Table in the name cache, read the table definition
996
and insert it into the cache.
997
We perform all of the above under table::Cache::singleton().mutex() which currently protects
998
the open cache (also known as table cache) and table definitions stored
1003
table::Cache::singleton().mutex().lock(); /* Lock for FLUSH TABLES for open table */
1006
Actually try to find the table in the open_cache.
1007
The cache may contain several "Table" instances for the same
1008
physical table. The instances that are currently "in use" by
1009
some thread have their "in_use" member != NULL.
1010
There is no good reason for having more than one entry in the
1011
hash for the same physical table, except that we use this as
1012
an implicit "pending locks queue" - see
1013
wait_for_locked_table_names for details.
1015
ppp= table::getCache().equal_range(key);
1018
for (table::CacheMap::const_iterator iter= ppp.first;
1019
iter != ppp.second; ++iter, table= NULL)
1021
table= (*iter).second;
1023
if (not table->in_use)
1884
for (table= session->temporary_tables; table ; table=table->next)
1886
if (table->s->table_cache_key.length == key_length +
1887
TMP_TABLE_KEY_EXTRA &&
1888
!memcmp(table->s->table_cache_key.str, key,
1889
key_length + TMP_TABLE_KEY_EXTRA))
1026
Here we flush tables marked for flush.
1027
Normally, table->getShare()->version contains the value of
1028
refresh_version from the moment when this table was
1029
(re-)opened and added to the cache.
1030
If since then we did (or just started) FLUSH TABLES
1031
statement, refresh_version has been increased.
1032
For "name-locked" Table instances, table->getShare()->version is set
1033
to 0 (see lock_table_name for details).
1034
In case there is a pending FLUSH TABLES or a name lock, we
1035
need to back off and re-start opening tables.
1036
If we do not back off now, we may dead lock in case of lock
1037
order mismatch with some other thread:
1038
c1-> name lock t1; -- sort of exclusive lock
1039
c2-> open t2; -- sort of shared lock
1040
c1-> name lock t2; -- blocks
1041
c2-> open t1; -- blocks
1892
We're trying to use the same temporary table twice in a query.
1893
Right now we don't support this because a temporary table
1894
is always represented by only one Table object in Session, and
1895
it can not be cloned. Emit an error for an unsupported behaviour.
1043
if (table->needs_reopen_or_name_lock())
1897
if (table->query_id)
1899
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1902
table->query_id= session->query_id;
1903
session->thread_specific_used= true;
1909
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1911
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1916
The table is not temporary - if we're in pre-locked or LOCK TABLES
1917
mode, let's try to find the requested table in the list of pre-opened
1918
and locked tables. If the table is not there, return an error - we can't
1919
open not pre-opened tables in pre-locked/LOCK TABLES mode.
1920
TODO: move this block into a separate function.
1922
if (session->locked_tables)
1923
{ // Using table locks
1924
Table *best_table= 0;
1925
int best_distance= INT_MIN;
1926
bool check_if_used= false;
1927
for (table=session->open_tables; table ; table=table->next)
1929
if (table->s->table_cache_key.length == key_length &&
1930
!memcmp(table->s->table_cache_key.str, key, key_length))
1932
if (check_if_used && table->query_id &&
1933
table->query_id != session->query_id)
1045
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1047
/* Force close at once after usage */
1048
version= table->getShare()->getVersion();
1052
/* Avoid self-deadlocks by detecting self-dependencies. */
1053
if (table->open_placeholder && table->in_use == this)
1055
table::Cache::singleton().mutex().unlock();
1056
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1061
Back off, part 1: mark the table as "unused" for the
1062
purpose of name-locking by setting table->db_stat to 0. Do
1063
that only for the tables in this thread that have an old
1064
table->getShare()->version (this is an optimization (?)).
1065
table->db_stat == 0 signals wait_for_locked_table_names
1066
that the tables in question are not used any more. See
1067
table_is_used call for details.
1069
close_old_data_files(false, false);
1072
Back-off part 2: try to avoid "busy waiting" on the table:
1073
if the table is in use by some other thread, we suspend
1074
and wait till the operation is complete: when any
1075
operation that juggles with table->getShare()->version completes,
1076
it broadcasts COND_refresh condition variable.
1077
If 'old' table we met is in use by current thread we return
1078
without waiting since in this situation it's this thread
1079
which is responsible for broadcasting on COND_refresh
1080
(and this was done already in Session::close_old_data_files()).
1081
Good example of such situation is when we have statement
1082
that needs two instances of table and FLUSH TABLES comes
1083
after we open first instance but before we open second
1086
if (table->in_use != this)
1088
/* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
1089
wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
1093
table::Cache::singleton().mutex().unlock();
1096
There is a refresh in progress for this table.
1097
Signal the caller that it has to try again.
1936
If we are in stored function or trigger we should ensure that
1937
we won't change table that is already used by calling statement.
1938
So if we are opening table for writing, we should check that it
1939
is not already open by some calling stamement.
1941
my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
1942
table->s->table_name.str);
1106
table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1107
table->in_use= this;
1111
/* Insert a new Table instance into the open cache */
1113
/* Free cache if too big */
1114
table::getUnused().cull();
1116
if (table_list->isCreate())
1946
When looking for a usable Table, ignore MERGE children, as they
1947
belong to their parent and cannot be used explicitly.
1949
if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
1950
table->query_id != session->query_id) /* skip tables already used */
1118
TableIdentifier lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1952
int distance= ((int) table->reginfo.lock_type -
1953
(int) table_list->lock_type);
1955
Find a table that either has the exact lock type requested,
1956
or has the best suitable lock. In case there is no locked
1957
table that has an equal or higher lock than requested,
1958
we us the closest matching lock to be able to produce an error
1959
message about wrong lock mode on the table. The best_table
1960
is changed if bd < 0 <= d or bd < d < 0 or 0 <= d < bd.
1120
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1962
distance < 0 - No suitable lock found
1963
distance > 0 - we have lock mode higher then we require
1964
distance == 0 - we have lock mode exactly which we need
1966
if ((best_distance < 0 && distance > best_distance) || (distance >= 0 && distance < best_distance))
1123
Table to be created, so we need to create placeholder in table-cache.
1125
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1968
best_distance= distance;
1970
if (best_distance == 0 && !check_if_used)
1127
table::Cache::singleton().mutex().unlock();
1973
If we have found perfect match and we don't need to check that
1974
table is not used by one of calling statements (assuming that
1975
we are inside of function or trigger) we can finish iterating
1976
through open tables list.
1131
Link placeholder to the open tables list so it will be automatically
1132
removed once tables are closed. Also mark it so it won't be ignored
1133
by other trying to take name-lock.
1135
table->open_placeholder= true;
1136
table->setNext(open_tables);
1138
table::Cache::singleton().mutex().unlock();
1142
/* Table exists. Let us try to open it. */
1145
/* make a new table */
1147
table::Concurrent *new_table= new table::Concurrent;
1149
if (new_table == NULL)
1151
table::Cache::singleton().mutex().unlock();
1155
error= new_table->open_unireg_entry(this, alias, identifier);
1159
table::Cache::singleton().mutex().unlock();
1162
(void)table::Cache::singleton().insert(new_table);
1166
table::Cache::singleton().mutex().unlock();
1987
table->query_id= session->query_id;
1991
No table in the locked tables list. In case of explicit LOCK TABLES
1992
this can happen if a user did not include the able into the list.
1993
In case of pre-locked mode locked tables list is generated automatically,
1994
so we may only end up here if the table did not exist when
1995
locked tables list was created.
1997
my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
2002
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
2003
this is the normal use case.
2005
- try to find the table in the table cache.
2006
- if one of the discovered Table instances is name-locked
2007
(table->s->version == 0) or some thread has started FLUSH TABLES
2008
(refresh_version > table->s->version), back off -- we have to wait
2009
until no one holds a name lock on the table.
2010
- if there is no such Table in the name cache, read the table definition
2011
and insert it into the cache.
2012
We perform all of the above under LOCK_open which currently protects
2013
the open cache (also known as table cache) and table definitions stored
2017
pthread_mutex_lock(&LOCK_open);
2020
If it's the first table from a list of tables used in a query,
2021
remember refresh_version (the version of open_cache state).
2022
If the version changes while we're opening the remaining tables,
2023
we will have to back off, close all the tables opened-so-far,
2024
and try to reopen them.
2025
Note: refresh_version is currently changed only during FLUSH TABLES.
2027
if (!session->open_tables)
2028
session->version=refresh_version;
2029
else if ((session->version != refresh_version) &&
2030
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
2032
/* Someone did a refresh while thread was opening tables */
1170
table->setNext(open_tables); /* Link into simple list */
1173
table->reginfo.lock_type= TL_READ; /* Assume read */
1176
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
2035
pthread_mutex_unlock(&LOCK_open);
2040
In order for the back off and re-start process to work properly,
2041
handler tables having old versions (due to FLUSH TABLES or pending
2042
name-lock) MUST be closed. This is specially important if a name-lock
2043
is pending for any table of the handler_tables list, otherwise a
2046
if (session->handler_tables)
2047
mysql_ha_flush(session);
2050
Actually try to find the table in the open_cache.
2051
The cache may contain several "Table" instances for the same
2052
physical table. The instances that are currently "in use" by
2053
some thread have their "in_use" member != NULL.
2054
There is no good reason for having more than one entry in the
2055
hash for the same physical table, except that we use this as
2056
an implicit "pending locks queue" - see
2057
wait_for_locked_table_names for details.
2059
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
2061
table && table->in_use ;
2062
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
2066
Here we flush tables marked for flush.
2067
Normally, table->s->version contains the value of
2068
refresh_version from the moment when this table was
2069
(re-)opened and added to the cache.
2070
If since then we did (or just started) FLUSH TABLES
2071
statement, refresh_version has been increased.
2072
For "name-locked" Table instances, table->s->version is set
2073
to 0 (see lock_table_name for details).
2074
In case there is a pending FLUSH TABLES or a name lock, we
2075
need to back off and re-start opening tables.
2076
If we do not back off now, we may dead lock in case of lock
2077
order mismatch with some other thread:
2078
c1: name lock t1; -- sort of exclusive lock
2079
c2: open t2; -- sort of shared lock
2080
c1: name lock t2; -- blocks
2081
c2: open t1; -- blocks
2083
if (table->needs_reopen_or_name_lock())
2085
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
2087
/* Force close at once after usage */
2088
session->version= table->s->version;
2092
/* Avoid self-deadlocks by detecting self-dependencies. */
2093
if (table->open_placeholder && table->in_use == session)
2095
pthread_mutex_unlock(&LOCK_open);
2096
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2101
Back off, part 1: mark the table as "unused" for the
2102
purpose of name-locking by setting table->db_stat to 0. Do
2103
that only for the tables in this thread that have an old
2104
table->s->version (this is an optimization (?)).
2105
table->db_stat == 0 signals wait_for_locked_table_names
2106
that the tables in question are not used any more. See
2107
table_is_used call for details.
2109
Notice that HANDLER tables were already taken care of by
2110
the earlier call to mysql_ha_flush() in this same critical
2113
close_old_data_files(session,session->open_tables,0,0);
2115
Back-off part 2: try to avoid "busy waiting" on the table:
2116
if the table is in use by some other thread, we suspend
2117
and wait till the operation is complete: when any
2118
operation that juggles with table->s->version completes,
2119
it broadcasts COND_refresh condition variable.
2120
If 'old' table we met is in use by current thread we return
2121
without waiting since in this situation it's this thread
2122
which is responsible for broadcasting on COND_refresh
2123
(and this was done already in close_old_data_files()).
2124
Good example of such situation is when we have statement
2125
that needs two instances of table and FLUSH TABLES comes
2126
after we open first instance but before we open second
2129
if (table->in_use != session)
2131
/* wait_for_conditionwill unlock LOCK_open for us */
2132
wait_for_condition(session, &LOCK_open, &COND_refresh);
2136
pthread_mutex_unlock(&LOCK_open);
2139
There is a refresh in progress for this table.
2140
Signal the caller that it has to try again.
2149
/* Unlink the table from "unused_tables" list. */
2150
if (table == unused_tables)
2152
unused_tables=unused_tables->next; // Remove from link
2153
if (table == unused_tables)
2156
table->prev->next=table->next; /* Remove from unused list */
2157
table->next->prev=table->prev;
2158
table->in_use= session;
2162
/* Insert a new Table instance into the open cache */
2164
/* Free cache if too big */
2165
while (open_cache.records > table_cache_size && unused_tables)
2166
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
2168
if (table_list->create)
2170
if(ha_table_exists_in_engine(session, table_list->db,
2171
table_list->table_name)
2172
== HA_ERR_TABLE_EXIST)
2174
pthread_mutex_unlock(&LOCK_open);
2180
Table to be created, so we need to create placeholder in table-cache.
2182
if (!(table= table_cache_insert_placeholder(session, key, key_length)))
2184
pthread_mutex_unlock(&LOCK_open);
2188
Link placeholder to the open tables list so it will be automatically
2189
removed once tables are closed. Also mark it so it won't be ignored
2190
by other trying to take name-lock.
2192
table->open_placeholder= 1;
2193
table->next= session->open_tables;
2194
session->open_tables= table;
2195
pthread_mutex_unlock(&LOCK_open);
2198
/* Table exists. Let us try to open it. */
2201
/* make a new table */
2202
table= (Table *) malloc(sizeof(*table));
2205
pthread_mutex_unlock(&LOCK_open);
2209
error= open_unireg_entry(session, table, table_list, alias, key, key_length);
2213
pthread_mutex_unlock(&LOCK_open);
2216
my_hash_insert(&open_cache,(unsigned char*) table);
2219
pthread_mutex_unlock(&LOCK_open);
2222
table->next=session->open_tables; /* Link into simple list */
2223
session->open_tables=table;
2225
table->reginfo.lock_type=TL_READ; /* Assume read */
2228
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2230
if (session->lex->need_correct_ident())
2231
table->alias_name_used= my_strcasecmp(table_alias_charset,
2232
table->s->table_name.str, alias);
1178
2233
/* Fix alias if table name changes */
1179
if (strcmp(table->getAlias(), alias))
2234
if (strcmp(table->alias, alias))
1181
table->setAlias(alias);
2236
uint32_t length=(uint32_t) strlen(alias)+1;
2237
table->alias= (char*) realloc((char*) table->alias, length);
2238
memcpy((void*) table->alias, alias, length);
1184
2240
/* These variables are also set in reopen_table() */
1185
table->tablenr= current_tablenr++;
1186
table->used_fields= 0;
1187
table->const_table= 0;
2241
table->tablenr=session->current_tablenr++;
2242
table->used_fields=0;
2243
table->const_table=0;
1188
2244
table->null_row= false;
1189
2245
table->maybe_null= false;
1190
2246
table->force_index= false;
1191
2247
table->status=STATUS_NO_RECORD;
1192
table->insert_values.clear();
2248
table->insert_values= 0;
1193
2249
/* Catch wrong handling of the auto_increment_field_not_null. */
1194
2250
assert(!table->auto_increment_field_not_null);
1195
2251
table->auto_increment_field_not_null= false;
1196
2252
if (table->timestamp_field)
1198
2253
table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
1200
2254
table->pos_in_table_list= table_list;
1201
2255
table->clear_column_bitmaps();
1202
2256
assert(table->key_read == 0);
2261
Table *find_locked_table(Session *session, const char *db,const char *table_name)
2263
char key[MAX_DBKEY_LENGTH];
2265
uint32_t key_length;
2267
key_pos= strcpy(key_pos, db) + strlen(db);
2268
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
2269
key_length= (uint32_t)(key_pos-key)+1;
2271
for (Table *table=session->open_tables; table ; table=table->next)
2273
if (table->s->table_cache_key.length == key_length &&
2274
!memcmp(table->s->table_cache_key.str, key, key_length))
2282
Reopen an table because the definition has changed.
2289
The data file for the table is already closed and the share is released
2290
The table has a 'dummy' share that mainly contains database and table name.
2294
1 error. The old table object is not changed.
2297
bool reopen_table(Table *table)
2303
TableList table_list;
2304
Session *session= table->in_use;
2306
assert(table->s->ref_count == 0);
2307
assert(!table->sort.io_cache);
2311
errmsg_printf(ERRMSG_LVL_ERROR, _("Table %s had a open data handler in reopen_table"),
2314
memset(&table_list, 0, sizeof(TableList));
2315
table_list.db= table->s->db.str;
2316
table_list.table_name= table->s->table_name.str;
2317
table_list.table= table;
2319
if (wait_for_locked_table_names(session, &table_list))
2320
return(1); // Thread was killed
2322
if (open_unireg_entry(session, &tmp, &table_list,
2324
table->s->table_cache_key.str,
2325
table->s->table_cache_key.length))
2328
/* This list copies variables set by open_table */
2329
tmp.tablenr= table->tablenr;
2330
tmp.used_fields= table->used_fields;
2331
tmp.const_table= table->const_table;
2332
tmp.null_row= table->null_row;
2333
tmp.maybe_null= table->maybe_null;
2334
tmp.status= table->status;
2336
tmp.s->table_map_id= table->s->table_map_id;
2339
tmp.in_use= session;
2340
tmp.reginfo.lock_type=table->reginfo.lock_type;
2342
/* Replace table in open list */
2343
tmp.next= table->next;
2344
tmp.prev= table->prev;
2347
table->closefrm(true); // close file, free everything
2350
table->default_column_bitmaps();
2351
table->file->change_table_ptr(table, table->s);
2353
assert(table->alias != 0);
2354
for (field=table->field ; *field ; field++)
2356
(*field)->table= (*field)->orig_table= table;
2357
(*field)->table_name= &table->alias;
2359
for (key=0 ; key < table->s->keys ; key++)
2361
for (part=0 ; part < table->key_info[key].usable_key_parts ; part++)
2362
table->key_info[key].key_part[part].field->table= table;
2365
Do not attach MERGE children here. The children might be reopened
2366
after the parent. Attach children after reopening all tables that
2367
require reopen. See for example reopen_tables().
2370
broadcast_refresh();
1209
Close all instances of a table open by this thread and replace
1210
them with exclusive name-locks.
1212
@param session Thread context
1213
@param db Database name for the table to be closed
1214
@param table_name Name of the table to be closed
1216
@note This function assumes that if we are not under LOCK TABLES,
1217
then there is only one table open and locked. This means that
1218
the function probably has to be adjusted before it can be used
1219
anywhere outside ALTER Table.
1221
@note Must not use TableShare::table_name/db of the table being closed,
1222
the strings are used in a loop even after the share may be freed.
2379
Close all instances of a table open by this thread and replace
2380
them with exclusive name-locks.
2382
@param session Thread context
2383
@param db Database name for the table to be closed
2384
@param table_name Name of the table to be closed
2386
@note This function assumes that if we are not under LOCK TABLES,
2387
then there is only one table open and locked. This means that
2388
the function probably has to be adjusted before it can be used
2389
anywhere outside ALTER Table.
2391
@note Must not use TABLE_SHARE::table_name/db of the table being closed,
2392
the strings are used in a loop even after the share may be freed.
1225
void Session::close_data_files_and_morph_locks(const TableIdentifier &identifier)
2395
void close_data_files_and_morph_locks(Session *session, const char *db,
2396
const char *table_name)
1227
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle()); /* Adjust locks at the end of ALTER TABLEL */
2400
safe_mutex_assert_owner(&LOCK_open);
1232
2405
If we are not under LOCK TABLES we should have only one table
1233
2406
open and locked so it makes sense to remove the lock at once.
2408
mysql_unlock_tables(session, session->lock);