diff options
author | Davi Arnaut <davi.arnaut@oracle.com> | 2010-10-06 11:34:28 -0300 |
---|---|---|
committer | Davi Arnaut <davi.arnaut@oracle.com> | 2010-10-06 11:34:28 -0300 |
commit | a5efb91deaf8a9c3bf53cd82ebd574be0cdabc5d (patch) | |
tree | a3aef309fd9e489e71e25196b22675e9ff9fef9d /storage/innobase | |
parent | 4386615050af0c445a7a95df6b3918f0724fc46d (diff) | |
download | mariadb-git-a5efb91deaf8a9c3bf53cd82ebd574be0cdabc5d.tar.gz |
Bug#49938: Failing assertion: inode or deadlock in fsp/fsp0fsp.c
Bug#54678: InnoDB, TRUNCATE, ALTER, I_S SELECT, crash or deadlock
- Incompatible change: truncate no longer resorts to a row by
row delete if the storage engine does not support the truncate
method. Consequently, the count of affected rows does not, in
any case, reflect the actual number of rows.
- Incompatible change: it is no longer possible to truncate a
table that participates as a parent in a foreign key constraint,
unless it is a self-referencing constraint (both parent and child
are in the same table). To work around this incompatible change
and still be able to truncate such tables, disable foreign checks
with SET foreign_key_checks=0 before truncate. Alternatively, if
foreign key checks are necessary, please use a DELETE statement
without a WHERE condition.
Problem description:
The problem was that for storage engines that do not support
truncate table via a external drop and recreate, such as InnoDB
which implements truncate via a internal drop and recreate, the
delete_all_rows method could be invoked with a shared metadata
lock, causing problems if the engine needed exclusive access
to some internal metadata. This problem originated with the
fact that there is no truncate specific handler method, which
ended up leading to a abuse of the delete_all_rows method that
is primarily used for delete operations without a condition.
Solution:
The solution is to introduce a truncate handler method that is
invoked when the engine does not support truncation via a table
drop and recreate. This method is invoked under a exclusive
metadata lock, so that there is only a single instance of the
table when the method is invoked.
Also, the method is not invoked and a error is thrown if
the table is a parent in a non-self-referencing foreign key
relationship. This was necessary to avoid inconsistency as
some integrity checks are bypassed. This is inline with the
fact that truncate is primarily a DDL operation that was
designed to quickly remove all data from a table.
mysql-test/suite/innodb/t/innodb-truncate.test:
Add test cases for truncate and foreign key checks.
Also test that InnoDB resets auto-increment on truncate.
mysql-test/suite/innodb/t/innodb.test:
FK is not necessary, test is related to auto-increment.
Update error number, truncate is no longer invoked if
table is parent in a FK relationship.
mysql-test/suite/innodb/t/innodb_mysql.test:
Update error number, truncate is no longer invoked if
table is parent in a FK relationship.
Use delete instead of truncate, test is used to check
the interaction of FKs, triggers and delete.
mysql-test/suite/parts/inc/partition_check.inc:
Fix typo.
mysql-test/suite/sys_vars/t/foreign_key_checks_func.test:
Update error number, truncate is no longer invoked if
table is parent in a FK relationship.
mysql-test/t/mdl_sync.test:
Modify test case to reflect and ensure that truncate takes
a exclusive metadata lock.
mysql-test/t/trigger-trans.test:
Update error number, truncate is no longer invoked if
table is parent in a FK relationship.
sql/ha_partition.cc:
Reorganize the various truncate methods. delete_all_rows is now
passed directly to the underlying engines, so as truncate. The
code responsible for truncating individual partitions is moved
to ha_partition::truncate_partition, which is invoked when a
ALTER TABLE t1 TRUNCATE PARTITION p statement is executed.
Since the partition truncate no longer can be invoked via
delete, the bitmap operations are not necessary anymore. The
explicit reset of the auto-increment value is also removed
as the underlying engines are now responsible for reseting
the value.
sql/handler.cc:
Wire up the handler truncate method.
sql/handler.h:
Introduce and document the truncate handler method. It assumes
certain use cases of delete_all_rows.
Add method to retrieve the list of foreign keys referencing a
table. Method is used to avoid truncating tables that are
parent in a foreign key relationship.
sql/share/errmsg-utf8.txt:
Add error message for truncate and FK.
sql/sql_lex.h:
Introduce a flag so that the partition engine can detect when
a partition is being truncated. Used to give a special error.
sql/sql_parse.cc:
Function mysql_truncate_table no longer exists.
sql/sql_partition_admin.cc:
Implement the TRUNCATE PARTITION statement.
sql/sql_truncate.cc:
Change the truncate table implementation to use the new truncate
handler method and to not rely on row-by-row delete anymore.
The truncate handler method is always invoked with a exclusive
metadata lock. Also, it is no longer possible to truncate a
table that is parent in some non-self-referencing foreign key.
storage/archive/ha_archive.cc:
Rename method as the description indicates that in the future
this could be a truncate operation.
storage/blackhole/ha_blackhole.cc:
Implement truncate as no operation for the blackhole engine in
order to remain compatible with older releases.
storage/federated/ha_federated.cc:
Introduce truncate method that invokes delete_all_rows.
This is required to support partition truncate as this
form of truncate does not implement the drop and recreate
protocol.
storage/heap/ha_heap.cc:
Introduce truncate method that invokes delete_all_rows.
This is required to support partition truncate as this
form of truncate does not implement the drop and recreate
protocol.
storage/ibmdb2i/ha_ibmdb2i.cc:
Introduce truncate method that invokes delete_all_rows.
This is required to support partition truncate as this
form of truncate does not implement the drop and recreate
protocol.
storage/innobase/handler/ha_innodb.cc:
Rename delete_all_rows to truncate. InnoDB now does truncate
under a exclusive metadata lock.
Introduce and reorganize methods used to retrieve the list
of foreign keys referenced by a or referencing a table.
storage/myisammrg/ha_myisammrg.cc:
Introduce truncate method that invokes delete_all_rows.
This is required in order to remain compatible with earlier
releases where truncate would resort to a row-by-row delete.
Diffstat (limited to 'storage/innobase')
-rw-r--r-- | storage/innobase/handler/ha_innodb.cc | 333 | ||||
-rw-r--r-- | storage/innobase/handler/ha_innodb.h | 4 | ||||
-rw-r--r-- | storage/innobase/row/row0mysql.c | 3 |
3 files changed, 196 insertions, 144 deletions
diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index eafc73b4f87..d68fcadffad 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -7081,33 +7081,21 @@ Deletes all rows of an InnoDB table. @return error number */ UNIV_INTERN int -ha_innobase::delete_all_rows(void) +ha_innobase::truncate(void) /*==============================*/ { int error; - DBUG_ENTER("ha_innobase::delete_all_rows"); + DBUG_ENTER("ha_innobase::truncate"); /* Get the transaction associated with the current thd, or create one if not yet created, and update prebuilt->trx */ update_thd(ha_thd()); - if (thd_sql_command(user_thd) != SQLCOM_TRUNCATE) { - fallback: - /* We only handle TRUNCATE TABLE t as a special case. - DELETE FROM t will have to use ha_innobase::delete_row(), - because DELETE is transactional while TRUNCATE is not. */ - DBUG_RETURN(my_errno=HA_ERR_WRONG_COMMAND); - } - /* Truncate the table in InnoDB */ error = row_truncate_table_for_mysql(prebuilt->table, prebuilt->trx); - if (error == DB_ERROR) { - /* Cannot truncate; resort to ha_innobase::delete_row() */ - goto fallback; - } error = convert_error_code_to_mysql(error, prebuilt->table->flags, NULL); @@ -8315,136 +8303,199 @@ ha_innobase::get_foreign_key_create_info(void) } +/***********************************************************************//** +Maps a InnoDB foreign key constraint to a equivalent MySQL foreign key info. +@return pointer to foreign key info */ +static +FOREIGN_KEY_INFO* +get_foreign_key_info( +/*=================*/ + THD* thd, /*!< in: user thread handle */ + dict_foreign_t* foreign) /*!< in: foreign key constraint */ +{ + FOREIGN_KEY_INFO f_key_info; + FOREIGN_KEY_INFO* pf_key_info; + uint i = 0; + ulint len; + char tmp_buff[NAME_LEN+1]; + char name_buff[NAME_LEN+1]; + const char* ptr; + LEX_STRING* referenced_key_name; + LEX_STRING* name = NULL; + + ptr = dict_remove_db_name(foreign->id); + f_key_info.foreign_id = thd_make_lex_string(thd, 0, ptr, + (uint) strlen(ptr), 1); + + /* Name format: database name, '/', table name, '\0' */ + + /* Referenced (parent) database name */ + len = dict_get_db_name_len(foreign->referenced_table_name); + ut_a(len < sizeof(tmp_buff)); + ut_memcpy(tmp_buff, foreign->referenced_table_name, len); + tmp_buff[len] = 0; + + len = filename_to_tablename(tmp_buff, name_buff, sizeof(name_buff)); + f_key_info.referenced_db = thd_make_lex_string(thd, 0, name_buff, len, 1); + + /* Referenced (parent) table name */ + ptr = dict_remove_db_name(foreign->referenced_table_name); + len = filename_to_tablename(ptr, name_buff, sizeof(name)); + f_key_info.referenced_table = thd_make_lex_string(thd, 0, name_buff, len, 1); + + /* Dependent (child) database name */ + len = dict_get_db_name_len(foreign->foreign_table_name); + ut_a(len < sizeof(tmp_buff)); + ut_memcpy(tmp_buff, foreign->foreign_table_name, len); + tmp_buff[len] = 0; + + len = filename_to_tablename(tmp_buff, name_buff, sizeof(name_buff)); + f_key_info.foreign_db = thd_make_lex_string(thd, 0, name_buff, len, 1); + + /* Dependent (child) table name */ + ptr = dict_remove_db_name(foreign->foreign_table_name); + len = filename_to_tablename(ptr, name_buff, sizeof(name_buff)); + f_key_info.foreign_table = thd_make_lex_string(thd, 0, name_buff, len, 1); + + do { + ptr = foreign->foreign_col_names[i]; + name = thd_make_lex_string(thd, name, ptr, + (uint) strlen(ptr), 1); + f_key_info.foreign_fields.push_back(name); + ptr = foreign->referenced_col_names[i]; + name = thd_make_lex_string(thd, name, ptr, + (uint) strlen(ptr), 1); + f_key_info.referenced_fields.push_back(name); + } while (++i < foreign->n_fields); + + if (foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE) { + len = 7; + ptr = "CASCADE"; + } else if (foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL) { + len = 8; + ptr = "SET NULL"; + } else if (foreign->type & DICT_FOREIGN_ON_DELETE_NO_ACTION) { + len = 9; + ptr = "NO ACTION"; + } else { + len = 8; + ptr = "RESTRICT"; + } + + f_key_info.delete_method = thd_make_lex_string(thd, + f_key_info.delete_method, + ptr, len, 1); + + if (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE) { + len = 7; + ptr = "CASCADE"; + } else if (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL) { + len = 8; + ptr = "SET NULL"; + } else if (foreign->type & DICT_FOREIGN_ON_UPDATE_NO_ACTION) { + len = 9; + ptr = "NO ACTION"; + } else { + len = 8; + ptr = "RESTRICT"; + } + + f_key_info.update_method = thd_make_lex_string(thd, + f_key_info.update_method, + ptr, len, 1); + + if (foreign->referenced_index && foreign->referenced_index->name) { + referenced_key_name = thd_make_lex_string(thd, + f_key_info.referenced_key_name, + foreign->referenced_index->name, + (uint) strlen(foreign->referenced_index->name), + 1); + } else { + referenced_key_name = NULL; + } + + f_key_info.referenced_key_name = referenced_key_name; + + pf_key_info = (FOREIGN_KEY_INFO *) thd_memdup(thd, &f_key_info, + sizeof(FOREIGN_KEY_INFO)); + + return(pf_key_info); +} + +/*******************************************************************//** +Gets the list of foreign keys in this table. +@return always 0, that is, always succeeds */ UNIV_INTERN int -ha_innobase::get_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list) -{ - dict_foreign_t* foreign; - - DBUG_ENTER("get_foreign_key_list"); - ut_a(prebuilt != NULL); - update_thd(ha_thd()); - prebuilt->trx->op_info = (char*)"getting list of foreign keys"; - trx_search_latch_release_if_reserved(prebuilt->trx); - mutex_enter(&(dict_sys->mutex)); - foreign = UT_LIST_GET_FIRST(prebuilt->table->foreign_list); - - while (foreign != NULL) { - uint i; - FOREIGN_KEY_INFO f_key_info; - LEX_STRING *name= 0; - uint ulen; - char uname[NAME_LEN+1]; /* Unencoded name */ - char db_name[NAME_LEN+1]; - const char *tmp_buff; - - tmp_buff= foreign->id; - i= 0; - while (tmp_buff[i] != '/') - i++; - tmp_buff+= i + 1; - f_key_info.forein_id = thd_make_lex_string(thd, 0, - tmp_buff, (uint) strlen(tmp_buff), 1); - tmp_buff= foreign->referenced_table_name; - - /* Database name */ - i= 0; - while (tmp_buff[i] != '/') - { - db_name[i]= tmp_buff[i]; - i++; - } - db_name[i]= 0; - ulen= filename_to_tablename(db_name, uname, sizeof(uname)); - f_key_info.referenced_db = thd_make_lex_string(thd, 0, - uname, ulen, 1); - - /* Table name */ - tmp_buff+= i + 1; - ulen= filename_to_tablename(tmp_buff, uname, sizeof(uname)); - f_key_info.referenced_table = thd_make_lex_string(thd, 0, - uname, ulen, 1); - - for (i= 0;;) { - tmp_buff= foreign->foreign_col_names[i]; - name = thd_make_lex_string(thd, name, - tmp_buff, (uint) strlen(tmp_buff), 1); - f_key_info.foreign_fields.push_back(name); - tmp_buff= foreign->referenced_col_names[i]; - name = thd_make_lex_string(thd, name, - tmp_buff, (uint) strlen(tmp_buff), 1); - f_key_info.referenced_fields.push_back(name); - if (++i >= foreign->n_fields) - break; - } - - ulong length; - if (foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE) - { - length=7; - tmp_buff= "CASCADE"; - } - else if (foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL) - { - length=8; - tmp_buff= "SET NULL"; - } - else if (foreign->type & DICT_FOREIGN_ON_DELETE_NO_ACTION) - { - length=9; - tmp_buff= "NO ACTION"; - } - else - { - length=8; - tmp_buff= "RESTRICT"; - } - f_key_info.delete_method = thd_make_lex_string( - thd, f_key_info.delete_method, tmp_buff, length, 1); - - - if (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE) - { - length=7; - tmp_buff= "CASCADE"; - } - else if (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL) - { - length=8; - tmp_buff= "SET NULL"; - } - else if (foreign->type & DICT_FOREIGN_ON_UPDATE_NO_ACTION) - { - length=9; - tmp_buff= "NO ACTION"; - } - else - { - length=8; - tmp_buff= "RESTRICT"; - } - f_key_info.update_method = thd_make_lex_string( - thd, f_key_info.update_method, tmp_buff, length, 1); - if (foreign->referenced_index && - foreign->referenced_index->name) - { - f_key_info.referenced_key_name = thd_make_lex_string( - thd, f_key_info.referenced_key_name, - foreign->referenced_index->name, - (uint) strlen(foreign->referenced_index->name), 1); - } - else - f_key_info.referenced_key_name= 0; - - FOREIGN_KEY_INFO *pf_key_info = (FOREIGN_KEY_INFO *) - thd_memdup(thd, &f_key_info, sizeof(FOREIGN_KEY_INFO)); - f_key_list->push_back(pf_key_info); - foreign = UT_LIST_GET_NEXT(foreign_list, foreign); - } - mutex_exit(&(dict_sys->mutex)); - prebuilt->trx->op_info = (char*)""; +ha_innobase::get_foreign_key_list( +/*==============================*/ + THD* thd, /*!< in: user thread handle */ + List<FOREIGN_KEY_INFO>* f_key_list) /*!< out: foreign key list */ +{ + FOREIGN_KEY_INFO* pf_key_info; + dict_foreign_t* foreign; + + ut_a(prebuilt != NULL); + update_thd(ha_thd()); + + prebuilt->trx->op_info = "getting list of foreign keys"; + + trx_search_latch_release_if_reserved(prebuilt->trx); + + mutex_enter(&(dict_sys->mutex)); + + for (foreign = UT_LIST_GET_FIRST(prebuilt->table->foreign_list); + foreign != NULL; + foreign = UT_LIST_GET_NEXT(referenced_list, foreign)) { + pf_key_info = get_foreign_key_info(thd, foreign); + if (pf_key_info) { + f_key_list->push_back(pf_key_info); + } + } + + mutex_exit(&(dict_sys->mutex)); + + prebuilt->trx->op_info = ""; + + return(0); +} + +/*******************************************************************//** +Gets the set of foreign keys where this table is the referenced table. +@return always 0, that is, always succeeds */ +UNIV_INTERN +int +ha_innobase::get_parent_foreign_key_list( +/*=====================================*/ + THD* thd, /*!< in: user thread handle */ + List<FOREIGN_KEY_INFO>* f_key_list) /*!< out: foreign key list */ +{ + FOREIGN_KEY_INFO* pf_key_info; + dict_foreign_t* foreign; - DBUG_RETURN(0); + ut_a(prebuilt != NULL); + update_thd(ha_thd()); + + prebuilt->trx->op_info = "getting list of referencing foreign keys"; + + trx_search_latch_release_if_reserved(prebuilt->trx); + + mutex_enter(&(dict_sys->mutex)); + + for (foreign = UT_LIST_GET_FIRST(prebuilt->table->referenced_list); + foreign != NULL; + foreign = UT_LIST_GET_NEXT(referenced_list, foreign)) { + pf_key_info = get_foreign_key_info(thd, foreign); + if (pf_key_info) { + f_key_list->push_back(pf_key_info); + } + } + + mutex_exit(&(dict_sys->mutex)); + + prebuilt->trx->op_info = ""; + + return(0); } /*****************************************************************//** diff --git a/storage/innobase/handler/ha_innodb.h b/storage/innobase/handler/ha_innodb.h index 8f118199ad8..4e128178f2b 100644 --- a/storage/innobase/handler/ha_innodb.h +++ b/storage/innobase/handler/ha_innodb.h @@ -178,13 +178,15 @@ class ha_innobase: public handler void update_create_info(HA_CREATE_INFO* create_info); int create(const char *name, register TABLE *form, HA_CREATE_INFO *create_info); - int delete_all_rows(); + int truncate(); int delete_table(const char *name); int rename_table(const char* from, const char* to); int check(THD* thd, HA_CHECK_OPT* check_opt); char* update_table_comment(const char* comment); char* get_foreign_key_create_info(); int get_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list); + int get_parent_foreign_key_list(THD *thd, + List<FOREIGN_KEY_INFO> *f_key_list); bool can_switch_engines(); uint referenced_by_foreign_key(); void free_foreign_key_create_info(char* str); diff --git a/storage/innobase/row/row0mysql.c b/storage/innobase/row/row0mysql.c index c12970b40ed..62f9ab07af9 100644 --- a/storage/innobase/row/row0mysql.c +++ b/storage/innobase/row/row0mysql.c @@ -2985,8 +2985,7 @@ next_rec: dict_table_change_id_in_cache(table, new_id); } - /* MySQL calls ha_innobase::reset_auto_increment() which does - the same thing. */ + /* Reset auto-increment. */ dict_table_autoinc_lock(table); dict_table_autoinc_initialize(table, 1); dict_table_autoinc_unlock(table); |