1
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
17
/* Basic functions needed by many modules */
23
#if TIME_WITH_SYS_TIME
24
# include <sys/time.h>
28
# include <sys/time.h>
33
#include "drizzled/internal/my_pthread.h"
34
#include "drizzled/internal/thread_var.h"
18
#include <drizzled/server_includes.h>
36
19
#include <drizzled/sql_select.h>
37
#include <drizzled/error.h>
38
#include <drizzled/gettext.h>
39
#include <drizzled/nested_join.h>
40
#include <drizzled/sql_base.h>
41
#include <drizzled/show.h>
42
#include <drizzled/item/cmpfunc.h>
43
#include <drizzled/replication_services.h>
44
#include <drizzled/check_stack_overrun.h>
45
#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"
20
#include <mysys/my_dir.h>
21
#include <drizzled/drizzled_error_messages.h>
22
#include <libdrizzle/gettext.h>
24
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
27
@defgroup Data_Dictionary Data Dictionary
30
Table *unused_tables; /* Used by mysql_test */
31
HASH open_cache; /* Used by mysql_test */
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,
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)))
48
Table *entry=(Table*) record;
49
*length= entry->s->table_cache_key.length;
50
return (uchar*) entry->s->table_cache_key.str;
65
extern bool volatile shutdown_in_progress;
67
54
bool table_cache_init(void)
72
uint32_t cached_open_tables(void)
74
return table::getCache().size();
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);
77
61
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.
65
close_cached_tables(NULL, NULL, false, false, false);
66
if (!open_cache.records) // Safety first
67
hash_free(&open_cache);
72
uint cached_open_tables(void)
74
return open_cache.records;
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.
103
462
void close_handle_and_leave_table_as_lock(Table *table)
464
TABLE_SHARE *share, *old_share= table->s;
466
MEM_ROOT *mem_root= &table->mem_root;
105
468
assert(table->db_stat);
106
assert(table->getShare()->getType() == message::Table::STANDARD);
109
471
Make a local copy of the table share and free the current one.
110
472
This has to be done to ensure that the table share is removed from
111
473
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);
475
if (multi_alloc_root(mem_root,
476
&share, sizeof(*share),
477
&key_buff, old_share->table_cache_key.length,
480
memset(share, 0, sizeof(*share));
481
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
482
old_share->table_cache_key.length);
483
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
486
table->file->close();
487
table->db_stat= 0; // Mark file closed
488
release_table_share(table->s, RELEASE_NORMAL);
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));
126
576
/*****************************************************************************
127
577
* Functions to free open table cache
128
578
****************************************************************************/
131
void Table::intern_close_table()
581
void intern_close_table(Table *table)
132
582
{ // Free all structures
134
if (cursor) // Not true if name lock
583
free_io_cache(table);
584
if (table->file) // Not true if name lock
585
VOID(closefrm(table, 1)); // close file
590
Remove table from the open table cache
594
table Table to remove
597
We need to have a lock on LOCK_open when calling this
600
static void free_cache_entry(Table *table)
602
intern_close_table(table);
136
delete_table(true); // close cursor
605
table->next->prev=table->prev; /* remove from used chain */
606
table->prev->next=table->next;
607
if (table == unused_tables)
609
unused_tables=unused_tables->next;
610
if (table == unused_tables)
614
my_free((uchar*) table,MYF(0));
140
618
/* Free resources allocated by filesort() and read_record() */
142
void Table::free_io_cache()
620
void free_io_cache(Table *table)
622
if (table->sort.io_cache)
146
sort.io_cache->close_cached_file();
147
delete sort.io_cache;
624
close_cached_file(table->sort.io_cache);
625
my_free((uchar*) table->sort.io_cache,MYF(0));
626
table->sort.io_cache=0;
154
633
Close all tables which aren't in use by any thread
156
@param session Thread context (may be NULL)
635
@param thd Thread context
157
636
@param tables List of tables to remove from the cache
158
@param have_lock If table::Cache::singleton().mutex() is locked
637
@param have_lock If LOCK_open is locked
159
638
@param wait_for_refresh Wait for a impending flush
160
639
@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
164
@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)
640
won't proceed while write-locked tables are being reopened by other
643
@remark THD can be NULL, but then wait_for_refresh must be false
644
and tables must be NULL.
647
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
648
bool wait_for_refresh, bool wait_for_placeholders)
651
assert(thd || (!wait_for_refresh && !tables));
654
VOID(pthread_mutex_lock(&LOCK_open));
657
refresh_version++; // Force close of open tables
658
while (unused_tables)
661
if (hash_delete(&open_cache,(uchar*) unused_tables))
662
printf("Warning: Couldn't delete open table from hash\n");
664
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
667
/* Free table shares */
668
while (oldest_unused_share->next)
670
pthread_mutex_lock(&oldest_unused_share->mutex);
671
VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
673
if (wait_for_refresh)
676
Other threads could wait in a loop in open_and_lock_tables(),
677
trying to lock one or more of our tables.
679
If they wait for the locks in thr_multi_lock(), their lock
680
request is aborted. They loop in open_and_lock_tables() and
681
enter open_table(). Here they notice the table is refreshed and
682
wait for COND_refresh. Then they loop again in
683
open_and_lock_tables() and this time open_table() succeeds. At
684
this moment, if we (the FLUSH TABLES thread) are scheduled and
685
on another FLUSH TABLES enter close_cached_tables(), they could
686
awake while we sleep below, waiting for others threads (us) to
687
close their open tables. If this happens, the other threads
688
would find the tables unlocked. They would get the locks, one
689
after the other, and could do their destructive work. This is an
690
issue if we have LOCK TABLES in effect.
692
The problem is that the other threads passed all checks in
693
open_table() before we refresh the table.
695
The fix for this problem is to set some_tables_deleted for all
696
threads with open tables. These threads can still get their
697
locks, but will immediately release them again after checking
698
this variable. They will then loop in open_and_lock_tables()
699
again. There they will wait until we update all tables version
702
Setting some_tables_deleted is done by remove_table_from_cache()
705
In other words (reviewer suggestion): You need this setting of
706
some_tables_deleted for the case when table was opened and all
707
related checks were passed before incrementing refresh_version
708
(which you already have) but attempt to lock the table happened
709
after the call to close_old_data_files() i.e. after removal of
710
current thread locks.
712
for (uint idx=0 ; idx < open_cache.records ; idx++)
714
Table *table=(Table*) hash_element(&open_cache,idx);
716
table->in_use->some_tables_deleted= 1;
723
for (TableList *table= tables; table; table= table->next_local)
725
if (remove_table_from_cache(thd, table->db, table->table_name,
726
RTFC_OWNED_BY_THD_FLAG))
730
wait_for_refresh=0; // Nothing to wait for
733
if (wait_for_refresh)
736
If there is any table that has a lower refresh_version, wait until
737
this is closed (or this thread is killed) before returning
739
thd->mysys_var->current_mutex= &LOCK_open;
740
thd->mysys_var->current_cond= &COND_refresh;
741
thd_proc_info(thd, "Flushing tables");
743
close_old_data_files(thd,thd->open_tables,1,1);
747
/* Wait until all threads has closed all the tables we had locked */
748
while (found && ! thd->killed)
751
for (uint idx=0 ; idx < open_cache.records ; idx++)
753
Table *table=(Table*) hash_element(&open_cache,idx);
754
/* Avoid a self-deadlock. */
755
if (table->in_use == thd)
758
Note that we wait here only for tables which are actually open, and
759
not for placeholders with Table::open_placeholder set. Waiting for
760
latter will cause deadlock in the following scenario, for example:
762
conn1: lock table t1 write;
763
conn2: lock table t2 write;
767
It also does not make sense to wait for those of placeholders that
768
are employed by CREATE TABLE as in this case table simply does not
771
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
772
(table->open_placeholder && wait_for_placeholders)))
775
pthread_cond_wait(&COND_refresh,&LOCK_open);
781
No other thread has the locked tables open; reopen them and get the
782
old locks. This should always succeed (unless some external process
783
has removed the tables)
785
thd->in_lock_tables=1;
786
result=reopen_tables(thd,1,1);
787
thd->in_lock_tables=0;
788
/* Set version for table */
789
for (Table *table=thd->open_tables; table ; table= table->next)
792
Preserve the version (0) of write locked tables so that a impending
793
global read lock won't sneak in.
795
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
796
table->s->version= refresh_version;
800
VOID(pthread_mutex_unlock(&LOCK_open));
801
if (wait_for_refresh)
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;
170
823
bool result= false;
171
Session *session= this;
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();
319
if (wait_for_refresh)
321
boost_unique_lock_t scopedLock(session->mysys_var->mutex);
322
session->mysys_var->current_mutex= 0;
323
session->mysys_var->current_cond= 0;
324
session->set_proc_info(0);
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());
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;
341
1085
assert(table->key_read == 0);
342
assert(!table->cursor || table->cursor->inited == Cursor::NONE);
1086
assert(!table->file || table->file->inited == handler::NONE);
344
open_tables= table->getNext();
1088
*table_ptr=table->next;
346
1090
if (table->needs_reopen_or_name_lock() ||
347
version != refresh_version || !table->db_stat)
1091
thd->version != refresh_version || !table->db_stat)
349
table::remove_table(table);
350
found_old_table= true;
1093
VOID(hash_delete(&open_cache,(uchar*) table));
520
void Open_tables_state::doGetTableNames(const SchemaIdentifier &schema_identifier,
521
std::set<std::string>& set_of_names)
523
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
525
if (schema_identifier.compare(table->getShare()->getSchemaName()))
527
set_of_names.insert(table->getShare()->getTableName());
532
void Open_tables_state::doGetTableNames(CachedDirectory &,
533
const SchemaIdentifier &schema_identifier,
534
std::set<std::string> &set_of_names)
536
doGetTableNames(schema_identifier, set_of_names);
539
void Open_tables_state::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
540
TableIdentifier::vector &set_of_identifiers)
542
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
544
if (schema_identifier.compare(table->getShare()->getSchemaName()))
546
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
547
table->getShare()->getTableName(),
548
table->getShare()->getPath()));
553
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
554
const SchemaIdentifier &schema_identifier,
555
TableIdentifier::vector &set_of_identifiers)
557
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
560
bool Open_tables_state::doDoesTableExist(const TableIdentifier &identifier)
562
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
564
if (table->getShare()->getType() == message::Table::TEMPORARY)
566
if (identifier.getKey() == table->getShare()->getCacheKey())
576
int Open_tables_state::doGetTableDefinition(const TableIdentifier &identifier,
577
message::Table &table_proto)
579
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
581
if (table->getShare()->getType() == message::Table::TEMPORARY)
583
if (identifier.getKey() == table->getShare()->getCacheKey())
585
table_proto.CopyFrom(*(table->getShare()->getTableProto()));
595
Table *Open_tables_state::find_temporary_table(const TableIdentifier &identifier)
597
for (Table *table= temporary_tables ; table ; table= table->getNext())
599
if (identifier.getKey() == table->getShare()->getCacheKey())
603
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
608
1446
Drop a temporary table.
610
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.
611
1449
If the table is found:
612
- if the table is being used by some outer statement, fail.
613
- if the table is in session->locked_tables, unlock it and
614
remove it from the list of locked tables. Currently only transactional
615
temporary tables are present in the locked_tables list.
616
- Close the temporary table, remove its .FRM
617
- 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
619
1457
This function is used to drop user temporary tables, as well as
620
1458
internal tables created in CREATE TEMPORARY TABLE ... SELECT
621
1459
or ALTER Table. Even though part of the work done by this function
622
1460
is redundant when the table is internal, as long as we
623
1461
link both internal and user temporary tables into the same
624
session->temporary_tables list, it's impossible to tell here whether
1462
thd->temporary_tables list, it's impossible to tell here whether
625
1463
we're dealing with an internal or a user temporary table.
627
1465
@retval 0 the table was found and dropped successfully.
628
1466
@retval 1 the table was not found in the list of temporary tables
630
1468
@retval -1 the table is in use by a outer query
633
int Open_tables_state::drop_temporary_table(const drizzled::TableIdentifier &identifier)
1471
int drop_temporary_table(THD *thd, TableList *table_list)
637
if (not (table= find_temporary_table(identifier)))
1475
if (!(table= find_temporary_table(thd, table_list)))
640
1478
/* 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);
1479
if (table->query_id && table->query_id != thd->query_id)
1481
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
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 */
1590
static void relink_unused(Table *table)
1592
if (table != unused_tables)
1594
table->prev->next=table->next; /* Remove from unused list */
1595
table->next->prev=table->prev;
1596
table->next=unused_tables; /* Link in unused tables */
1597
table->prev=unused_tables->prev;
1598
unused_tables->prev->next=table;
1599
unused_tables->prev=table;
1600
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.
1606
Remove all instances of table from thread's open list and
1609
@param thd Thread context
1610
@param find Table to remove
1611
@param unlock true - free all locks on tables removed that are
1612
done with LOCK TABLES
1615
@note When unlock parameter is false or current thread doesn't have
1616
any tables locked with LOCK TABLES, tables are assumed to be
1617
not locked (for example already unlocked).
663
void Session::unlink_open_table(Table *find)
1620
void unlink_open_table(THD *thd, Table *find, bool unlock)
665
const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
667
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
1622
char key[MAX_DBKEY_LENGTH];
1623
uint key_length= find->s->table_cache_key.length;
1624
Table *list, **prev;
1626
safe_mutex_assert_owner(&LOCK_open);
1628
memcpy(key, find->s->table_cache_key.str, key_length);
670
Note that we need to hold table::Cache::singleton().mutex() while changing the
1630
Note that we need to hold LOCK_open while changing the
671
1631
open_tables list. Another thread may work on it.
672
(See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
1632
(See: remove_table_from_cache(), mysql_wait_completed_table())
673
1633
Closing a MERGE child before the parent would be fatal if the
674
1634
other thread tries to abort the MERGE lock in between.
676
for (prev= &open_tables; *prev; )
1636
for (prev= &thd->open_tables; *prev; )
680
if (list->getShare()->getCacheKey() == find_key)
1640
if (list->s->table_cache_key.length == key_length &&
1641
!memcmp(list->s->table_cache_key.str, key, key_length))
1643
if (unlock && thd->locked_tables)
1644
mysql_lock_remove(thd, thd->locked_tables, list, true);
682
1646
/* Remove table from open_tables list. */
683
*prev= list->getNext();
685
1648
/* Close table. */
686
table::remove_table(static_cast<table::Concurrent *>(list));
1649
VOID(hash_delete(&open_cache,(uchar*) list)); // Close table
690
1653
/* Step to next entry in open_tables list. */
691
prev= list->getNextPtr();
695
1658
// Notify any 'refresh' threads
696
locking::broadcast_refresh();
1659
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.
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.
719
void Session::drop_open_table(Table *table, const TableIdentifier &identifier)
1683
void drop_open_table(THD *thd, Table *table, const char *db_name,
1684
const char *table_name)
721
if (table->getShare()->getType())
723
close_temporary_table(table);
1686
if (table->s->tmp_table)
1687
close_temporary_table(thd, table, 1, 1);
727
boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close and drop a table (AUX routine) */
1690
handlerton *table_type= table->s->db_type();
1691
VOID(pthread_mutex_lock(&LOCK_open));
729
1693
unlink_open_table() also tells threads waiting for refresh or close
730
1694
that something has happened.
732
unlink_open_table(table);
733
plugin::StorageEngine::dropTable(*this, identifier);
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));
739
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
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
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
749
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
1714
void wait_for_condition(THD *thd, pthread_mutex_t *mutex, pthread_cond_t *cond)
751
1716
/* 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());
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");
1723
(void) pthread_cond_wait(cond, mutex);
1726
We must unlock mutex first to avoid deadlock becasue conditions are
1727
sent to this thread by doing locks in the following order:
1728
lock(mysys_var->mutex)
1729
lock(mysys_var->current_mutex)
1731
One by effect of this that one can only use wait_for_condition with
1732
condition variables that are guranteed to not disapper (freed) even if this
1736
pthread_mutex_unlock(mutex);
1737
pthread_mutex_lock(&thd->mysys_var->mutex);
1738
thd->mysys_var->current_mutex= 0;
1739
thd->mysys_var->current_cond= 0;
1740
thd_proc_info(thd, proc_info);
1741
pthread_mutex_unlock(&thd->mysys_var->mutex);
1747
Exclusively name-lock a table that is already write-locked by the
1750
@param thd current thread context
1751
@param tables table list containing one table to open.
1753
@return false on success, true otherwise.
1756
bool name_lock_locked_table(THD *thd, TableList *tables)
1758
/* Under LOCK TABLES we must only accept write locked tables. */
1759
tables->table= find_locked_table(thd, tables->db, tables->table_name);
1762
my_error(ER_TABLE_NOT_LOCKED, MYF(0), tables->alias);
1763
else if (tables->table->reginfo.lock_type < TL_WRITE_LOW_PRIORITY)
1764
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), tables->alias);
1768
Ensures that table is opened only by this thread and that no
1769
other statement will open this table.
1771
wait_while_table_is_used(thd, tables->table, HA_EXTRA_FORCE_REOPEN);
1780
Open table which is already name-locked by this thread.
1783
reopen_name_locked_table()
1785
table_list TableList object for table to be open, TableList::table
1786
member should point to Table object which was used for
1788
link_in true - if Table object for table to be opened should be
1789
linked into THD::open_tables list.
1790
false - placeholder used for name-locking is already in
1791
this list so we only need to preserve Table::next
1795
This function assumes that its caller already acquired LOCK_open mutex.
1802
bool reopen_name_locked_table(THD* thd, TableList* table_list, bool link_in)
1804
Table *table= table_list->table;
1806
char *table_name= table_list->table_name;
1809
safe_mutex_assert_owner(&LOCK_open);
1811
if (thd->killed || !table)
1816
if (open_unireg_entry(thd, table, table_list, table_name,
1817
table->s->table_cache_key.str,
1818
table->s->table_cache_key.length))
1820
intern_close_table(table);
1822
If there was an error during opening of table (for example if it
1823
does not exist) '*table' object can be wiped out. To be able
1824
properly release name-lock in this case we should restore this
1825
object to its original state.
1833
We want to prevent other connections from opening this table until end
1834
of statement as it is likely that modifications of table's metadata are
1835
not yet finished (for example CREATE TRIGGER have to change .TRG file,
1836
or we might want to drop table if CREATE TABLE ... SELECT fails).
1837
This also allows us to assume that no other connection will sneak in
1838
before we will get table-level lock on this table.
1841
table->in_use = thd;
1845
table->next= thd->open_tables;
1846
thd->open_tables= table;
1851
Table object should be already in THD::open_tables list so we just
1852
need to set Table::next correctly.
1854
table->next= orig_table.next;
1857
table->tablenr=thd->current_tablenr++;
1858
table->used_fields=0;
1859
table->const_table=0;
1860
table->null_row= false;
1861
table->maybe_null= false;
1862
table->force_index= false;
1863
table->status=STATUS_NO_RECORD;
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
1881
Table *table_cache_insert_placeholder(THD *thd, const char *key,
1888
safe_mutex_assert_owner(&LOCK_open);
799
1891
Create a table entry with the right key and with an old refresh version
1892
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))
1895
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
1896
&table, sizeof(*table),
1897
&share, sizeof(*share),
1898
&key_buff, key_length,
1903
share->set_table_cache_key(key_buff, key, key_length);
1904
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1906
table->locked_by_name=1;
1908
if (my_hash_insert(&open_cache, (uchar*)table))
1910
my_free((uchar*) table, MYF(0));
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.
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.
836
bool Session::lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table)
1940
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1941
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())
1943
char key[MAX_DBKEY_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))
1951
VOID(pthread_mutex_unlock(&LOCK_open));
852
if (not (*table= table_cache_insert_placeholder(identifier)))
856
(*table)->open_placeholder= true;
857
(*table)->setNext(open_tables);
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);
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.
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.
882
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.
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.
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.
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.
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.
896
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
2064
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint flags)
899
const char *alias= table_list->alias;
2066
register Table *table;
2067
char key[MAX_DBKEY_LENGTH];
2068
unsigned int key_length;
2069
char *alias= table_list->alias;
2070
HASH_SEARCH_STATE state;
901
/* Parsing of partitioning information from .frm needs session->lex set up. */
902
assert(lex->is_lex_started);
2072
/* Parsing of partitioning information from .frm needs thd->lex set up. */
2073
assert(thd->lex->is_lex_started);
904
2075
/* find a unused table in the open table cache */
908
2079
/* 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;
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);
920
2090
Unless requested otherwise, try to resolve this table in the list
921
2091
of temporary tables of this thread. In MySQL temporary tables
922
2092
are always thread-local and "shadow" possible base tables with the
923
2093
same name. This block implements the behaviour.
924
TODO -> move this block into a separate function.
2094
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)
2097
for (table= thd->temporary_tables; table ; table=table->next)
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))
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
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.
1043
if (table->needs_reopen_or_name_lock())
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;
2122
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
2124
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
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)
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.
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);
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())
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 */
1118
TableIdentifier lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
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.
1120
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
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))
1123
Table to be created, so we need to create placeholder in table-cache.
1125
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
2181
best_distance= distance;
2183
if (best_distance == 0 && !check_if_used)
1127
table::Cache::singleton().mutex().unlock();
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.
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();
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);
2215
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
2216
this is the normal use case.
2218
- try to find the table in the table cache.
2219
- if one of the discovered Table instances is name-locked
2220
(table->s->version == 0) or some thread has started FLUSH TABLES
2221
(refresh_version > table->s->version), back off -- we have to wait
2222
until no one holds a name lock on the table.
2223
- if there is no such Table in the name cache, read the table definition
2224
and insert it into the cache.
2225
We perform all of the above under LOCK_open which currently protects
2226
the open cache (also known as table cache) and table definitions stored
2230
VOID(pthread_mutex_lock(&LOCK_open));
2233
If it's the first table from a list of tables used in a query,
2234
remember refresh_version (the version of open_cache state).
2235
If the version changes while we're opening the remaining tables,
2236
we will have to back off, close all the tables opened-so-far,
2237
and try to reopen them.
2238
Note: refresh_version is currently changed only during FLUSH TABLES.
2240
if (!thd->open_tables)
2241
thd->version=refresh_version;
2242
else if ((thd->version != refresh_version) &&
2243
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
2245
/* 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);
2248
VOID(pthread_mutex_unlock(&LOCK_open));
2253
In order for the back off and re-start process to work properly,
2254
handler tables having old versions (due to FLUSH TABLES or pending
2255
name-lock) MUST be closed. This is specially important if a name-lock
2256
is pending for any table of the handler_tables list, otherwise a
2259
if (thd->handler_tables)
2260
mysql_ha_flush(thd);
2263
Actually try to find the table in the open_cache.
2264
The cache may contain several "Table" instances for the same
2265
physical table. The instances that are currently "in use" by
2266
some thread have their "in_use" member != NULL.
2267
There is no good reason for having more than one entry in the
2268
hash for the same physical table, except that we use this as
2269
an implicit "pending locks queue" - see
2270
wait_for_locked_table_names for details.
2272
for (table= (Table*) hash_first(&open_cache, (uchar*) key, key_length,
2274
table && table->in_use ;
2275
table= (Table*) hash_next(&open_cache, (uchar*) key, key_length,
2279
Here we flush tables marked for flush.
2280
Normally, table->s->version contains the value of
2281
refresh_version from the moment when this table was
2282
(re-)opened and added to the cache.
2283
If since then we did (or just started) FLUSH TABLES
2284
statement, refresh_version has been increased.
2285
For "name-locked" Table instances, table->s->version is set
2286
to 0 (see lock_table_name for details).
2287
In case there is a pending FLUSH TABLES or a name lock, we
2288
need to back off and re-start opening tables.
2289
If we do not back off now, we may dead lock in case of lock
2290
order mismatch with some other thread:
2291
c1: name lock t1; -- sort of exclusive lock
2292
c2: open t2; -- sort of shared lock
2293
c1: name lock t2; -- blocks
2294
c2: open t1; -- blocks
2296
if (table->needs_reopen_or_name_lock())
2298
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
2300
/* Force close at once after usage */
2301
thd->version= table->s->version;
2305
/* Avoid self-deadlocks by detecting self-dependencies. */
2306
if (table->open_placeholder && table->in_use == thd)
2308
VOID(pthread_mutex_unlock(&LOCK_open));
2309
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2314
Back off, part 1: mark the table as "unused" for the
2315
purpose of name-locking by setting table->db_stat to 0. Do
2316
that only for the tables in this thread that have an old
2317
table->s->version (this is an optimization (?)).
2318
table->db_stat == 0 signals wait_for_locked_table_names
2319
that the tables in question are not used any more. See
2320
table_is_used call for details.
2322
Notice that HANDLER tables were already taken care of by
2323
the earlier call to mysql_ha_flush() in this same critical
2326
close_old_data_files(thd,thd->open_tables,0,0);
2328
Back-off part 2: try to avoid "busy waiting" on the table:
2329
if the table is in use by some other thread, we suspend
2330
and wait till the operation is complete: when any
2331
operation that juggles with table->s->version completes,
2332
it broadcasts COND_refresh condition variable.
2333
If 'old' table we met is in use by current thread we return
2334
without waiting since in this situation it's this thread
2335
which is responsible for broadcasting on COND_refresh
2336
(and this was done already in close_old_data_files()).
2337
Good example of such situation is when we have statement
2338
that needs two instances of table and FLUSH TABLES comes
2339
after we open first instance but before we open second
2342
if (table->in_use != thd)
2344
/* wait_for_conditionwill unlock LOCK_open for us */
2345
wait_for_condition(thd, &LOCK_open, &COND_refresh);
2349
VOID(pthread_mutex_unlock(&LOCK_open));
2352
There is a refresh in progress for this table.
2353
Signal the caller that it has to try again.
2362
/* Unlink the table from "unused_tables" list. */
2363
if (table == unused_tables)
2365
unused_tables=unused_tables->next; // Remove from link
2366
if (table == unused_tables)
2369
table->prev->next=table->next; /* Remove from unused list */
2370
table->next->prev=table->prev;
2375
/* Insert a new Table instance into the open cache */
2377
/* Free cache if too big */
2378
while (open_cache.records > table_cache_size && unused_tables)
2379
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
2381
if (table_list->create)
2385
if (check_if_table_exists(thd, table_list, &exists))
2387
VOID(pthread_mutex_unlock(&LOCK_open));
2394
Table to be created, so we need to create placeholder in table-cache.
2396
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
2398
VOID(pthread_mutex_unlock(&LOCK_open));
2402
Link placeholder to the open tables list so it will be automatically
2403
removed once tables are closed. Also mark it so it won't be ignored
2404
by other trying to take name-lock.
2406
table->open_placeholder= 1;
2407
table->next= thd->open_tables;
2408
thd->open_tables= table;
2409
VOID(pthread_mutex_unlock(&LOCK_open));
2412
/* Table exists. Let us try to open it. */
2415
/* make a new table */
2416
if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
2418
VOID(pthread_mutex_unlock(&LOCK_open));
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));
2439
VOID(pthread_mutex_unlock(&LOCK_open));
2442
table->next=thd->open_tables; /* Link into simple list */
2443
thd->open_tables=table;
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())
2451
table->alias_name_used= my_strcasecmp(table_alias_charset,
2452
table->s->table_name.str, alias);
1178
2453
/* Fix alias if table name changes */
1179
if (strcmp(table->getAlias(), alias))
2454
if (strcmp(table->alias, alias))
1181
table->setAlias(alias);
2456
uint length=(uint) strlen(alias)+1;
2457
table->alias= (char*) my_realloc((char*) table->alias, length,
2459
memcpy((void*) table->alias, alias, length);
1184
2461
/* These variables are also set in reopen_table() */
1185
table->tablenr= current_tablenr++;
1186
table->used_fields= 0;
1187
table->const_table= 0;
2462
table->tablenr=thd->current_tablenr++;
2463
table->used_fields=0;
2464
table->const_table=0;
1188
2465
table->null_row= false;
1189
2466
table->maybe_null= false;
1190
2467
table->force_index= false;
1191
2468
table->status=STATUS_NO_RECORD;
1192
table->insert_values.clear();
2469
table->insert_values= 0;
1193
2470
/* Catch wrong handling of the auto_increment_field_not_null. */
1194
2471
assert(!table->auto_increment_field_not_null);
1195
2472
table->auto_increment_field_not_null= false;
1196
2473
if (table->timestamp_field)
1198
2474
table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
1200
2475
table->pos_in_table_list= table_list;
1201
2476
table->clear_column_bitmaps();
1202
2477
assert(table->key_read == 0);
2482
Table *find_locked_table(THD *thd, const char *db,const char *table_name)
2484
char key[MAX_DBKEY_LENGTH];
2485
uint key_length=(uint) (stpcpy(stpcpy(key,db)+1,table_name)-key)+1;
2487
for (Table *table=thd->open_tables; table ; table=table->next)
2489
if (table->s->table_cache_key.length == key_length &&
2490
!memcmp(table->s->table_cache_key.str, key, key_length))
2498
Reopen an table because the definition has changed.
2505
The data file for the table is already closed and the share is released
2506
The table has a 'dummy' share that mainly contains database and table name.
2510
1 error. The old table object is not changed.
2513
bool reopen_table(Table *table)
2519
TableList table_list;
2520
THD *thd= table->in_use;
2522
assert(table->s->ref_count == 0);
2523
assert(!table->sort.io_cache);
2527
sql_print_error(_("Table %s had a open data handler in reopen_table"),
2530
memset(&table_list, 0, sizeof(TableList));
2531
table_list.db= table->s->db.str;
2532
table_list.table_name= table->s->table_name.str;
2533
table_list.table= table;
2535
if (wait_for_locked_table_names(thd, &table_list))
2536
return(1); // Thread was killed
2538
if (open_unireg_entry(thd, &tmp, &table_list,
2540
table->s->table_cache_key.str,
2541
table->s->table_cache_key.length))
2544
/* This list copies variables set by open_table */
2545
tmp.tablenr= table->tablenr;
2546
tmp.used_fields= table->used_fields;
2547
tmp.const_table= table->const_table;
2548
tmp.null_row= table->null_row;
2549
tmp.maybe_null= table->maybe_null;
2550
tmp.status= table->status;
2552
tmp.s->table_map_id= table->s->table_map_id;
2556
tmp.reginfo.lock_type=table->reginfo.lock_type;
2558
/* Replace table in open list */
2559
tmp.next= table->next;
2560
tmp.prev= table->prev;
2563
VOID(closefrm(table, 1)); // close file, free everything
2566
table->default_column_bitmaps();
2567
table->file->change_table_ptr(table, table->s);
2569
assert(table->alias != 0);
2570
for (field=table->field ; *field ; field++)
2572
(*field)->table= (*field)->orig_table= table;
2573
(*field)->table_name= &table->alias;
2575
for (key=0 ; key < table->s->keys ; key++)
2577
for (part=0 ; part < table->key_info[key].usable_key_parts ; part++)
2578
table->key_info[key].key_part[part].field->table= table;
2581
Do not attach MERGE children here. The children might be reopened
2582
after the parent. Attach children after reopening all tables that
2583
require reopen. See for example reopen_tables().
2586
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.
2595
Close all instances of a table open by this thread and replace
2596
them with exclusive name-locks.
2598
@param thd Thread context
2599
@param db Database name for the table to be closed
2600
@param table_name Name of the table to be closed
2602
@note This function assumes that if we are not under LOCK TABLES,
2603
then there is only one table open and locked. This means that
2604
the function probably has to be adjusted before it can be used
2605
anywhere outside ALTER Table.
2607
@note Must not use TABLE_SHARE::table_name/db of the table being closed,
2608
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)
2611
void close_data_files_and_morph_locks(THD *thd, const char *db,
2612
const char *table_name)
1227
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle()); /* Adjust locks at the end of ALTER TABLEL */
2616
safe_mutex_assert_owner(&LOCK_open);
1232
2621
If we are not under LOCK TABLES we should have only one table
1233
2622
open and locked so it makes sense to remove the lock at once.
2624
mysql_unlock_tables(thd, thd->lock);