summaryrefslogtreecommitdiff
path: root/storage/xtradb/dict/dict0dict.cc
diff options
context:
space:
mode:
Diffstat (limited to 'storage/xtradb/dict/dict0dict.cc')
-rw-r--r--storage/xtradb/dict/dict0dict.cc662
1 files changed, 565 insertions, 97 deletions
diff --git a/storage/xtradb/dict/dict0dict.cc b/storage/xtradb/dict/dict0dict.cc
index 3b044c74b52..8738e2bbf9e 100644
--- a/storage/xtradb/dict/dict0dict.cc
+++ b/storage/xtradb/dict/dict0dict.cc
@@ -1,6 +1,6 @@
/*****************************************************************************
-Copyright (c) 1996, 2014, Oracle and/or its affiliates. All Rights Reserved.
+Copyright (c) 1996, 2015, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2012, Facebook Inc.
Copyright (c) 2013, SkySQL Ab. All Rights Reserved.
@@ -684,6 +684,33 @@ dict_table_get_col_name_for_mysql(
return(s);
}
#ifndef UNIV_HOTBACKUP
+/** Allocate and init the autoinc latch of a given table.
+This function must not be called concurrently on the same table object.
+@param[in,out] table_void table whose autoinc latch to create */
+void
+dict_table_autoinc_alloc(
+ void* table_void)
+{
+ dict_table_t* table = static_cast<dict_table_t*>(table_void);
+ table->autoinc_mutex = new (std::nothrow) ib_mutex_t();
+ ut_a(table->autoinc_mutex != NULL);
+ mutex_create(autoinc_mutex_key,
+ table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
+}
+
+/** Allocate and init the zip_pad_mutex of a given index.
+This function must not be called concurrently on the same index object.
+@param[in,out] index_void index whose zip_pad_mutex to create */
+void
+dict_index_zip_pad_alloc(
+ void* index_void)
+{
+ dict_index_t* index = static_cast<dict_index_t*>(index_void);
+ index->zip_pad.mutex = new (std::nothrow) os_fast_mutex_t;
+ ut_a(index->zip_pad.mutex != NULL);
+ os_fast_mutex_init(zip_pad_mutex_key, index->zip_pad.mutex);
+}
+
/********************************************************************//**
Acquire the autoinc lock. */
UNIV_INTERN
@@ -692,7 +719,32 @@ dict_table_autoinc_lock(
/*====================*/
dict_table_t* table) /*!< in/out: table */
{
- mutex_enter(&table->autoinc_mutex);
+#ifdef HAVE_ATOMIC_BUILTINS
+ os_once::do_or_wait_for_done(
+ &table->autoinc_mutex_created,
+ dict_table_autoinc_alloc, table);
+#else /* HAVE_ATOMIC_BUILTINS */
+ ut_ad(table->autoinc_mutex_created == os_once::DONE);
+#endif /* HAVE_ATOMIC_BUILTINS */
+
+ mutex_enter(table->autoinc_mutex);
+}
+
+/** Acquire the zip_pad_mutex latch.
+@param[in,out] index the index whose zip_pad_mutex to acquire.*/
+void
+dict_index_zip_pad_lock(
+ dict_index_t* index)
+{
+#ifdef HAVE_ATOMIC_BUILTINS
+ os_once::do_or_wait_for_done(
+ &index->zip_pad.mutex_created,
+ dict_index_zip_pad_alloc, index);
+#else /* HAVE_ATOMIC_BUILTINS */
+ ut_ad(index->zip_pad.mutex_created == os_once::DONE);
+#endif /* HAVE_ATOMIC_BUILTINS */
+
+ os_fast_mutex_lock(index->zip_pad.mutex);
}
/********************************************************************//**
@@ -704,7 +756,7 @@ dict_table_autoinc_initialize(
dict_table_t* table, /*!< in/out: table */
ib_uint64_t value) /*!< in: next value to assign to a row */
{
- ut_ad(mutex_own(&table->autoinc_mutex));
+ ut_ad(dict_table_autoinc_own(table));
table->autoinc = value;
}
@@ -746,7 +798,7 @@ dict_table_autoinc_read(
/*====================*/
const dict_table_t* table) /*!< in: table */
{
- ut_ad(mutex_own(&table->autoinc_mutex));
+ ut_ad(dict_table_autoinc_own(table));
return(table->autoinc);
}
@@ -762,7 +814,7 @@ dict_table_autoinc_update_if_greater(
dict_table_t* table, /*!< in/out: table */
ib_uint64_t value) /*!< in: value which was assigned to a row */
{
- ut_ad(mutex_own(&table->autoinc_mutex));
+ ut_ad(dict_table_autoinc_own(table));
if (value > table->autoinc) {
@@ -778,7 +830,7 @@ dict_table_autoinc_unlock(
/*======================*/
dict_table_t* table) /*!< in/out: table */
{
- mutex_exit(&table->autoinc_mutex);
+ mutex_exit(table->autoinc_mutex);
}
#endif /* !UNIV_HOTBACKUP */
@@ -1099,8 +1151,28 @@ dict_table_open_on_name(
if (table != NULL) {
- /* If table is corrupted, return NULL */
+ /* If table is encrypted return table */
if (ignore_err == DICT_ERR_IGNORE_NONE
+ && table->is_encrypted) {
+ /* Make life easy for drop table. */
+ if (table->can_be_evicted) {
+ dict_table_move_from_lru_to_non_lru(table);
+ }
+
+ if (table->can_be_evicted) {
+ dict_move_to_mru(table);
+ }
+
+ ++table->n_ref_count;
+
+ if (!dict_locked) {
+ mutex_exit(&dict_sys->mutex);
+ }
+
+ return (table);
+ }
+ /* If table is corrupted, return NULL */
+ else if (ignore_err == DICT_ERR_IGNORE_NONE
&& table->corrupted) {
/* Make life easy for drop table. */
@@ -1626,15 +1698,18 @@ dict_table_rename_in_cache(
} else if (table->space != TRX_SYS_SPACE) {
char* new_path = NULL;
- if (table->dir_path_of_temp_table != NULL) {
+ if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY)) {
ut_print_timestamp(stderr);
fputs(" InnoDB: Error: trying to rename a"
" TEMPORARY TABLE ", stderr);
ut_print_name(stderr, NULL, TRUE, old_name);
- fputs(" (", stderr);
- ut_print_filename(stderr,
- table->dir_path_of_temp_table);
- fputs(" )\n", stderr);
+ if (table->dir_path_of_temp_table != NULL) {
+ fputs(" (", stderr);
+ ut_print_filename(
+ stderr, table->dir_path_of_temp_table);
+ fputs(" )\n", stderr);
+ }
+
return(DB_ERROR);
} else if (DICT_TF_HAS_DATA_DIR(table->flags)) {
@@ -3225,6 +3300,11 @@ dict_index_build_internal_fts(
}
/*====================== FOREIGN KEY PROCESSING ========================*/
+#define DB_FOREIGN_KEY_IS_PREFIX_INDEX 200
+#define DB_FOREIGN_KEY_COL_NOT_NULL 201
+#define DB_FOREIGN_KEY_COLS_NOT_EQUAL 202
+#define DB_FOREIGN_KEY_INDEX_NOT_FOUND 203
+
/*********************************************************************//**
Checks if a table is referenced by foreign keys.
@return TRUE if table is referenced by a foreign key */
@@ -3379,15 +3459,26 @@ dict_foreign_find_index(
/*!< in: whether to check
charsets. only has an effect
if types_idx != NULL */
- ulint check_null)
+ ulint check_null,
/*!< in: nonzero if none of
the columns must be declared
NOT NULL */
+ ulint* error, /*!< out: error code */
+ ulint* err_col_no,
+ /*!< out: column number where
+ error happened */
+ dict_index_t** err_index)
+ /*!< out: index where error
+ happened */
{
dict_index_t* index;
ut_ad(mutex_own(&dict_sys->mutex));
+ if (error) {
+ *error = DB_FOREIGN_KEY_INDEX_NOT_FOUND;
+ }
+
index = dict_table_get_first_index(table);
while (index != NULL) {
@@ -3397,7 +3488,12 @@ dict_foreign_find_index(
&& dict_foreign_qualify_index(
table, col_names, columns, n_cols,
index, types_idx,
- check_charsets, check_null)) {
+ check_charsets, check_null,
+ error, err_col_no,err_index)) {
+ if (error) {
+ *error = DB_SUCCESS;
+ }
+
return(index);
}
@@ -3426,7 +3522,7 @@ wsrep_dict_foreign_find_index(
{
return dict_foreign_find_index(
table, col_names, columns, n_cols, types_idx, check_charsets,
- check_null);
+ check_null, NULL, NULL, NULL);
}
#endif /* WITH_WSREP */
/**********************************************************************//**
@@ -3528,7 +3624,7 @@ dict_foreign_add_to_cache(
ref_table, NULL,
for_in_cache->referenced_col_names,
for_in_cache->n_fields, for_in_cache->foreign_index,
- check_charsets, false);
+ check_charsets, false, NULL, NULL, NULL);
if (index == NULL
&& !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) {
@@ -3560,6 +3656,10 @@ dict_foreign_add_to_cache(
}
if (for_table && !for_in_cache->foreign_table) {
+ ulint index_error;
+ ulint err_col;
+ dict_index_t *err_index=NULL;
+
index = dict_foreign_find_index(
for_table, col_names,
for_in_cache->foreign_col_names,
@@ -3567,7 +3667,8 @@ dict_foreign_add_to_cache(
for_in_cache->referenced_index, check_charsets,
for_in_cache->type
& (DICT_FOREIGN_ON_DELETE_SET_NULL
- | DICT_FOREIGN_ON_UPDATE_SET_NULL));
+ | DICT_FOREIGN_ON_UPDATE_SET_NULL),
+ &index_error, &err_col, &err_index);
if (index == NULL
&& !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) {
@@ -4227,6 +4328,8 @@ static
void
dict_foreign_report_syntax_err(
/*===========================*/
+ const char* fmt, /*!< in: syntax err msg */
+ const char* oper, /*!< in: operation */
const char* name, /*!< in: table name */
const char* start_of_latest_foreign,
/*!< in: start of the foreign key clause
@@ -4239,12 +4342,102 @@ dict_foreign_report_syntax_err(
mutex_enter(&dict_foreign_err_mutex);
dict_foreign_error_report_low(ef, name);
- fprintf(ef, "%s:\nSyntax error close to:\n%s\n",
- start_of_latest_foreign, ptr);
+ fprintf(ef, fmt, oper, name, start_of_latest_foreign, ptr);
mutex_exit(&dict_foreign_err_mutex);
}
/*********************************************************************//**
+Push warning message to SQL-layer based on foreign key constraint
+index match error. */
+static
+void
+dict_foreign_push_index_error(
+/*==========================*/
+ trx_t* trx, /*!< in: trx */
+ const char* operation, /*!< in: operation create or alter
+ */
+ const char* create_name, /*!< in: table name in create or
+ alter table */
+ const char* latest_foreign, /*!< in: start of latest foreign key
+ constraint name */
+ const char** columns, /*!< in: foreign key columns */
+ ulint index_error, /*!< in: error code */
+ ulint err_col, /*!< in: column where error happened
+ */
+ dict_index_t* err_index, /*!< in: index where error happened
+ */
+ dict_table_t* table, /*!< in: table */
+ FILE* ef) /*!< in: output stream */
+{
+ switch (index_error) {
+ case DB_FOREIGN_KEY_INDEX_NOT_FOUND: {
+ fprintf(ef,
+ "%s table '%s' with foreign key constraint"
+ " failed. There is no index in the referenced"
+ " table where the referenced columns appear"
+ " as the first columns. Error close to %s.\n",
+ operation, create_name, latest_foreign);
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table '%s' with foreign key constraint"
+ " failed. There is no index in the referenced"
+ " table where the referenced columns appear"
+ " as the first columns. Error close to %s.",
+ operation, create_name, latest_foreign);
+ break;
+ }
+ case DB_FOREIGN_KEY_IS_PREFIX_INDEX: {
+ fprintf(ef,
+ "%s table '%s' with foreign key constraint"
+ " failed. There is only prefix index in the referenced"
+ " table where the referenced columns appear"
+ " as the first columns. Error close to %s.\n",
+ operation, create_name, latest_foreign);
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table '%s' with foreign key constraint"
+ " failed. There is only prefix index in the referenced"
+ " table where the referenced columns appear"
+ " as the first columns. Error close to %s.",
+ operation, create_name, latest_foreign);
+ break;
+ }
+ case DB_FOREIGN_KEY_COL_NOT_NULL: {
+ fprintf(ef,
+ "%s table %s with foreign key constraint"
+ " failed. You have defined a SET NULL condition but "
+ "field %s on index is defined as NOT NULL close to %s\n",
+ operation, create_name, columns[err_col], latest_foreign);
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. You have defined a SET NULL condition but "
+ "field %s on index is defined as NOT NULL close to %s",
+ operation, create_name, columns[err_col], latest_foreign);
+ break;
+ }
+ case DB_FOREIGN_KEY_COLS_NOT_EQUAL: {
+ dict_field_t* field;
+ const char* col_name;
+ field = dict_index_get_nth_field(err_index, err_col);
+
+ col_name = dict_table_get_col_name(
+ table, dict_col_get_no(field->col));
+ fprintf(ef,
+ "%s table %s with foreign key constraint"
+ " failed. Field type or character set for column %s "
+ "does not mach referenced column %s close to %s\n",
+ operation, create_name, columns[err_col], col_name, latest_foreign);
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Field type or character set for column %s "
+ "does not mach referenced column %s close to %s",
+ operation, create_name, columns[err_col], col_name, latest_foreign);
+ break;
+ }
+ default:
+ ut_error;
+ }
+}
+
+/*********************************************************************//**
Scans a table create SQL string and adds to the data dictionary the foreign
key constraints declared in the string. This function should be called after
the indexes for a table have been created. Each foreign key constraint must
@@ -4272,16 +4465,21 @@ dict_create_foreign_constraints_low(
DB_CANNOT_ADD_CONSTRAINT if any foreign
keys are found. */
{
- dict_table_t* table;
- dict_table_t* referenced_table;
- dict_table_t* table_to_alter;
+ dict_table_t* table = NULL;
+ dict_table_t* referenced_table = NULL;
+ dict_table_t* table_to_alter = NULL;
+ dict_table_t* table_to_create = NULL;
ulint highest_id_so_far = 0;
ulint number = 1;
- dict_index_t* index;
- dict_foreign_t* foreign;
+ dict_index_t* index = NULL;
+ dict_foreign_t* foreign = NULL;
const char* ptr = sql_string;
const char* start_of_latest_foreign = sql_string;
+ const char* start_of_latest_set = NULL;
FILE* ef = dict_foreign_err_file;
+ ulint index_error = DB_SUCCESS;
+ dict_index_t* err_index = NULL;
+ ulint err_col;
const char* constraint_name;
ibool success;
dberr_t error;
@@ -4294,37 +4492,80 @@ dict_create_foreign_constraints_low(
ulint n_on_updates;
const dict_col_t*columns[500];
const char* column_names[500];
+ const char* ref_column_names[500];
const char* referenced_table_name;
dict_foreign_set local_fk_set;
dict_foreign_set_free local_fk_set_free(local_fk_set);
+ const char* create_table_name;
+ const char* orig;
+ char create_name[MAX_TABLE_NAME_LEN + 1];
+ char operation[8];
ut_ad(!srv_read_only_mode);
ut_ad(mutex_own(&(dict_sys->mutex)));
table = dict_table_get_low(name);
+ /* First check if we are actually doing an ALTER TABLE, and in that
+ case look for the table being altered */
+ orig = ptr;
+ ptr = dict_accept(cs, ptr, "ALTER", &success);
+
+ strcpy((char *)operation, success ? "Alter " : "Create ");
+
+ if (!success) {
+ orig = ptr;
+ ptr = dict_scan_to(ptr, "CREATE");
+ ptr = dict_scan_to(ptr, "TABLE");
+ ptr = dict_accept(cs, ptr, "TABLE", &success);
+
+ if (success) {
+ ptr = dict_scan_table_name(cs, ptr, &table_to_create, name,
+ &success, heap, &create_table_name);
+ }
+
+ if (success) {
+ char *bufend;
+ bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
+ create_table_name, strlen(create_table_name),
+ trx->mysql_thd, TRUE);
+ create_name[bufend-create_name]='\0';
+ ptr = orig;
+ } else {
+ char *bufend;
+ ptr = orig;
+ bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
+ name, strlen(name), trx->mysql_thd, TRUE);
+ create_name[bufend-create_name]='\0';
+ }
+
+ goto loop;
+ }
if (table == NULL) {
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
- fprintf(ef,
- "Cannot find the table in the internal"
- " data dictionary of InnoDB.\n"
- "Create table statement:\n%s\n", sql_string);
+ dict_foreign_error_report_low(ef, create_name);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef, "%s table %s with foreign key constraint"
+ " failed. Table %s not found from data dictionary."
+ " Error close to %s.\n",
+ operation, create_name, create_name, start_of_latest_foreign);
mutex_exit(&dict_foreign_err_mutex);
+ ib_push_warning(trx, DB_ERROR,
+ "%s table %s with foreign key constraint"
+ " failed. Table %s not found from data dictionary."
+ " Error close to %s.",
+ operation, create_name, create_name, start_of_latest_foreign);
return(DB_ERROR);
}
- /* First check if we are actually doing an ALTER TABLE, and in that
- case look for the table being altered */
-
- ptr = dict_accept(cs, ptr, "ALTER", &success);
-
+ /* If not alter table jump to loop */
if (!success) {
goto loop;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "TABLE", &success);
if (!success) {
@@ -4334,13 +4575,40 @@ dict_create_foreign_constraints_low(
/* We are doing an ALTER TABLE: scan the table name we are altering */
+ orig = ptr;
ptr = dict_scan_table_name(cs, ptr, &table_to_alter, name,
&success, heap, &referenced_table_name);
+
+ if (table_to_alter) {
+ char *bufend;
+ bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
+ table_to_alter->name, strlen(table_to_alter->name),
+ trx->mysql_thd, TRUE);
+ create_name[bufend-create_name]='\0';
+ } else {
+ char *bufend;
+ bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
+ referenced_table_name, strlen(referenced_table_name),
+ trx->mysql_thd, TRUE);
+ create_name[bufend-create_name]='\0';
+
+ }
+
if (!success) {
- fprintf(stderr,
- "InnoDB: Error: could not find"
- " the table being ALTERED in:\n%s\n",
- sql_string);
+ mutex_enter(&dict_foreign_err_mutex);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef,
+ "%s table %s with foreign key constraint"
+ " failed. Table %s not found from data dictionary."
+ " Error close to %s.\n",
+ operation, create_name, create_name, orig);
+ mutex_exit(&dict_foreign_err_mutex);
+
+ ib_push_warning(trx, DB_ERROR,
+ "%s table %s with foreign key constraint"
+ " failed. Table %s not found from data dictionary."
+ " Error close to %s.",
+ operation, create_name, create_name, orig);
return(DB_ERROR);
}
@@ -4377,6 +4645,7 @@ loop:
of the constraint to system tables. */
ptr = ptr1;
+ orig = ptr;
ptr = dict_accept(cs, ptr, "CONSTRAINT", &success);
ut_a(success);
@@ -4407,6 +4676,19 @@ loop:
if so, immediately reject the command if the table is a
temporary one. For now, this kludge will work. */
if (reject_fks && !local_fk_set.empty()) {
+ mutex_enter(&dict_foreign_err_mutex);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef, "%s table %s with foreign key constraint"
+ " failed. Temporary tables can't have foreign key constraints."
+ " Error close to %s.\n",
+ operation, create_name, start_of_latest_foreign);
+ mutex_exit(&dict_foreign_err_mutex);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Temporary tables can't have foreign key constraints."
+ " Error close to %s.",
+ operation, create_name, start_of_latest_foreign);
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4432,6 +4714,7 @@ loop:
start_of_latest_foreign = ptr;
+ orig = ptr;
ptr = dict_accept(cs, ptr, "FOREIGN", &success);
if (!success) {
@@ -4442,6 +4725,7 @@ loop:
goto loop;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "KEY", &success);
if (!success) {
@@ -4467,6 +4751,7 @@ loop:
}
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "(", &success);
if (!success) {
@@ -4476,8 +4761,17 @@ loop:
ptr = dict_skip_word(cs, ptr, &success);
if (!success) {
dict_foreign_report_syntax_err(
- name, start_of_latest_foreign, ptr);
- return(DB_CANNOT_ADD_CONSTRAINT);
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, orig);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, orig);
+ return(DB_CANNOT_ADD_CONSTRAINT);
}
}
else {
@@ -4504,15 +4798,26 @@ loop:
/* Scan the columns in the first list */
col_loop1:
ut_a(i < (sizeof column_names) / sizeof *column_names);
+ orig = ptr;
ptr = dict_scan_col(cs, ptr, &success, table, columns + i,
heap, column_names + i);
if (!success) {
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
- fprintf(ef, "%s:\nCannot resolve column name close to:\n%s\n",
- start_of_latest_foreign, ptr);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, orig);
+
mutex_exit(&dict_foreign_err_mutex);
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, orig);
+
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4524,11 +4829,22 @@ col_loop1:
goto col_loop1;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, ")", &success);
if (!success) {
dict_foreign_report_syntax_err(
- name, start_of_latest_foreign, ptr);
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, orig);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, orig);
+
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4539,27 +4855,41 @@ col_loop1:
set to 0 */
index = dict_foreign_find_index(
- table, NULL, column_names, i, NULL, TRUE, FALSE);
+ table, NULL, column_names, i,
+ NULL, TRUE, FALSE, &index_error, &err_col, &err_index);
if (!index) {
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
+ dict_foreign_error_report_low(ef, create_name);
fputs("There is no index in table ", ef);
- ut_print_name(ef, NULL, TRUE, name);
+ ut_print_name(ef, NULL, TRUE, create_name);
fprintf(ef, " where the columns appear\n"
"as the first columns. Constraint:\n%s\n"
"See " REFMAN "innodb-foreign-key-constraints.html\n"
"for correct foreign key definition.\n",
start_of_latest_foreign);
- mutex_exit(&dict_foreign_err_mutex);
+ dict_foreign_push_index_error(trx, operation, create_name, start_of_latest_foreign,
+ column_names, index_error, err_col, err_index, table, ef);
- return(DB_CHILD_NO_INDEX);
+ mutex_exit(&dict_foreign_err_mutex);
+ return(DB_CANNOT_ADD_CONSTRAINT);
}
+
+ orig = ptr;
ptr = dict_accept(cs, ptr, "REFERENCES", &success);
if (!success || !my_isspace(cs, *ptr)) {
dict_foreign_report_syntax_err(
- name, start_of_latest_foreign, ptr);
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, orig);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, orig);
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4628,21 +4958,46 @@ col_loop1:
checking of foreign key constraints! */
if (!success || (!referenced_table && trx->check_foreigns)) {
+ char buf[MAX_TABLE_NAME_LEN + 1] = "";
+ char* bufend;
+
+ bufend = innobase_convert_name(buf, MAX_TABLE_NAME_LEN,
+ referenced_table_name, strlen(referenced_table_name),
+ trx->mysql_thd, TRUE);
+ buf[bufend - buf] = '\0';
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint failed. Referenced table %s not found in the data dictionary "
+ "close to %s.",
+ operation, create_name, buf, start_of_latest_foreign);
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
- fprintf(ef, "%s:\nCannot resolve table name close to:\n"
- "%s\n",
- start_of_latest_foreign, ptr);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef,
+ "%s table %s with foreign key constraint failed. Referenced table %s not found in the data dictionary "
+ "close to %s.\n",
+ operation, create_name, buf, start_of_latest_foreign);
+
mutex_exit(&dict_foreign_err_mutex);
return(DB_CANNOT_ADD_CONSTRAINT);
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "(", &success);
if (!success) {
- dict_foreign_report_syntax_err(name, start_of_latest_foreign,
- ptr);
+ dict_foreign_report_syntax_err(
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, orig);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, orig);
+
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4650,34 +5005,54 @@ col_loop1:
i = 0;
col_loop2:
+ orig = ptr;
ptr = dict_scan_col(cs, ptr, &success, referenced_table, columns + i,
- heap, column_names + i);
+ heap, ref_column_names + i);
i++;
if (!success) {
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
- fprintf(ef, "%s:\nCannot resolve column name close to:\n"
- "%s\n",
- start_of_latest_foreign, ptr);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, orig);
mutex_exit(&dict_foreign_err_mutex);
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, orig);
return(DB_CANNOT_ADD_CONSTRAINT);
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, ",", &success);
if (success) {
goto col_loop2;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, ")", &success);
if (!success || foreign->n_fields != i) {
- dict_foreign_report_syntax_err(name, start_of_latest_foreign,
- ptr);
+ dict_foreign_report_syntax_err(
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s. Too few referenced columns.\n",
+ operation, create_name, start_of_latest_foreign, orig);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s. Too few referenced columns, you have %d when you should have %d.",
+ operation, create_name, start_of_latest_foreign, orig, i, foreign->n_fields);
+
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4687,6 +5062,7 @@ col_loop2:
scan_on_conditions:
/* Loop here as long as we can find ON ... conditions */
+ start_of_latest_set = ptr;
ptr = dict_accept(cs, ptr, "ON", &success);
if (!success) {
@@ -4694,15 +5070,27 @@ scan_on_conditions:
goto try_find_index;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "DELETE", &success);
if (!success) {
+ orig = ptr;
ptr = dict_accept(cs, ptr, "UPDATE", &success);
if (!success) {
dict_foreign_report_syntax_err(
- name, start_of_latest_foreign, ptr);
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4713,12 +5101,14 @@ scan_on_conditions:
n_on_deletes++;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "RESTRICT", &success);
if (success) {
goto scan_on_conditions;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "CASCADE", &success);
if (success) {
@@ -4731,14 +5121,25 @@ scan_on_conditions:
goto scan_on_conditions;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "NO", &success);
if (success) {
+ orig = ptr;
ptr = dict_accept(cs, ptr, "ACTION", &success);
if (!success) {
dict_foreign_report_syntax_err(
- name, start_of_latest_foreign, ptr);
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
return(DB_CANNOT_ADD_CONSTRAINT);
}
@@ -4752,38 +5153,69 @@ scan_on_conditions:
goto scan_on_conditions;
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "SET", &success);
if (!success) {
- dict_foreign_report_syntax_err(name, start_of_latest_foreign,
- ptr);
+ dict_foreign_report_syntax_err(
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
return(DB_CANNOT_ADD_CONSTRAINT);
}
+ orig = ptr;
ptr = dict_accept(cs, ptr, "NULL", &success);
if (!success) {
- dict_foreign_report_syntax_err(name, start_of_latest_foreign,
- ptr);
+ dict_foreign_report_syntax_err(
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.\n",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. Foreign key constraint parse error in %s"
+ " close to %s.",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
return(DB_CANNOT_ADD_CONSTRAINT);
}
for (j = 0; j < foreign->n_fields; j++) {
if ((dict_index_get_nth_col(foreign->foreign_index, j)->prtype)
& DATA_NOT_NULL) {
+ const dict_col_t* col
+ = dict_index_get_nth_col(foreign->foreign_index, j);
+ const char* col_name = dict_table_get_col_name(foreign->foreign_index->table,
+ dict_col_get_no(col));
/* It is not sensible to define SET NULL
if the column is not allowed to be NULL! */
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
- fprintf(ef, "%s:\n"
- "You have defined a SET NULL condition"
- " though some of the\n"
- "columns are defined as NOT NULL.\n",
- start_of_latest_foreign);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef,
+ "%s table %s with foreign key constraint"
+ " failed. You have defined a SET NULL condition but column %s is defined as NOT NULL"
+ " in %s close to %s.\n",
+ operation, create_name, col_name, start_of_latest_foreign, start_of_latest_set);
mutex_exit(&dict_foreign_err_mutex);
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. You have defined a SET NULL condition but column %s is defined as NOT NULL"
+ " in %s close to %s.",
+ operation, create_name, col_name, start_of_latest_foreign, start_of_latest_set);
+
return(DB_CANNOT_ADD_CONSTRAINT);
}
}
@@ -4801,11 +5233,20 @@ try_find_index:
/* It is an error to define more than 1 action */
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
- fprintf(ef, "%s:\n"
- "You have twice an ON DELETE clause"
- " or twice an ON UPDATE clause.\n",
- start_of_latest_foreign);
+ dict_foreign_error_report_low(ef, create_name);
+ fprintf(ef,
+ "%s table %s with foreign key constraint"
+ " failed. You have more than one on delete or on update clause"
+ " in %s close to %s.\n",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
+ ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
+ "%s table %s with foreign key constraint"
+ " failed. You have more than one on delete or on update clause"
+ " in %s close to %s.",
+ operation, create_name, start_of_latest_foreign, start_of_latest_set);
+
+ dict_foreign_free(foreign);
mutex_exit(&dict_foreign_err_mutex);
return(DB_CANNOT_ADD_CONSTRAINT);
@@ -4817,12 +5258,12 @@ try_find_index:
if (referenced_table) {
index = dict_foreign_find_index(referenced_table, NULL,
- column_names, i,
+ ref_column_names, i,
foreign->foreign_index,
- TRUE, FALSE);
+ TRUE, FALSE, &index_error, &err_col, &err_index);
if (!index) {
mutex_enter(&dict_foreign_err_mutex);
- dict_foreign_error_report_low(ef, name);
+ dict_foreign_error_report_low(ef, create_name);
fprintf(ef, "%s:\n"
"Cannot find an index in the"
" referenced table where the\n"
@@ -4840,9 +5281,13 @@ try_find_index:
"innodb-foreign-key-constraints.html\n"
"for correct foreign key definition.\n",
start_of_latest_foreign);
+
+ dict_foreign_push_index_error(trx, operation, create_name, start_of_latest_foreign,
+ column_names, index_error, err_col, err_index, referenced_table, ef);
+
mutex_exit(&dict_foreign_err_mutex);
- return(DB_PARENT_NO_INDEX);
+ return(DB_CANNOT_ADD_CONSTRAINT);
}
} else {
ut_a(trx->check_foreigns == FALSE);
@@ -4861,11 +5306,12 @@ try_find_index:
for (i = 0; i < foreign->n_fields; i++) {
foreign->referenced_col_names[i]
- = mem_heap_strdup(foreign->heap, column_names[i]);
+ = mem_heap_strdup(foreign->heap, ref_column_names[i]);
}
goto loop;
}
+
/**************************************************************************
Determines whether a string starts with the specified keyword.
@return TRUE if str starts with keyword */
@@ -5945,8 +6391,7 @@ dict_ind_init(void)
dict_table_t* table;
/* create dummy table and index for REDUNDANT infimum and supremum */
- table = dict_mem_table_create("SYS_DUMMY1", DICT_HDR_SPACE, 1, 0, 0,
- true);
+ table = dict_mem_table_create("SYS_DUMMY1", DICT_HDR_SPACE, 1, 0, 0);
dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
DATA_ENGLISH | DATA_NOT_NULL, 8);
@@ -5959,7 +6404,7 @@ dict_ind_init(void)
/* create dummy table and index for COMPACT infimum and supremum */
table = dict_mem_table_create("SYS_DUMMY2",
DICT_HDR_SPACE, 1,
- DICT_TF_COMPACT, 0, true);
+ DICT_TF_COMPACT, 0);
dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
DATA_ENGLISH | DATA_NOT_NULL, 8);
dict_ind_compact = dict_mem_index_create("SYS_DUMMY2", "SYS_DUMMY2",
@@ -6056,7 +6501,8 @@ dict_foreign_replace_index(
foreign->foreign_table, col_names,
foreign->foreign_col_names,
foreign->n_fields, index,
- /*check_charsets=*/TRUE, /*check_null=*/FALSE);
+ /*check_charsets=*/TRUE, /*check_null=*/FALSE,
+ NULL, NULL, NULL);
if (new_index) {
ut_ad(new_index->table == index->table);
ut_ad(!new_index->to_be_dropped);
@@ -6080,7 +6526,8 @@ dict_foreign_replace_index(
foreign->referenced_table, NULL,
foreign->referenced_col_names,
foreign->n_fields, index,
- /*check_charsets=*/TRUE, /*check_null=*/FALSE);
+ /*check_charsets=*/TRUE, /*check_null=*/FALSE,
+ NULL, NULL, NULL);
/* There must exist an alternative index,
since this must have been checked earlier. */
if (new_index) {
@@ -6641,10 +7088,15 @@ dict_foreign_qualify_index(
/*!< in: whether to check
charsets. only has an effect
if types_idx != NULL */
- ulint check_null)
+ ulint check_null,
/*!< in: nonzero if none of
the columns must be declared
NOT NULL */
+ ulint* error, /*!< out: error code */
+ ulint* err_col_no,
+ /*!< out: column number where error happened */
+ dict_index_t** err_index)
+ /*!< out: index where error happened */
{
if (dict_index_get_n_fields(index) < n_cols) {
return(false);
@@ -6661,11 +7113,21 @@ dict_foreign_qualify_index(
if (field->prefix_len != 0) {
/* We do not accept column prefix
indexes here */
+ if (error && err_col_no && err_index) {
+ *error = DB_FOREIGN_KEY_IS_PREFIX_INDEX;
+ *err_col_no = i;
+ *err_index = (dict_index_t*)index;
+ }
return(false);
}
if (check_null
&& (field->col->prtype & DATA_NOT_NULL)) {
+ if (error && err_col_no && err_index) {
+ *error = DB_FOREIGN_KEY_COL_NOT_NULL;
+ *err_col_no = i;
+ *err_index = (dict_index_t*)index;
+ }
return(false);
}
@@ -6681,6 +7143,12 @@ dict_foreign_qualify_index(
dict_index_get_nth_col(index, i),
dict_index_get_nth_col(types_idx, i),
check_charsets)) {
+ if (error && err_col_no && err_index) {
+ *error = DB_FOREIGN_KEY_COLS_NOT_EQUAL;
+ *err_col_no = i;
+ *err_index = (dict_index_t*)index;
+ }
+
return(false);
}
}
@@ -6795,10 +7263,10 @@ dict_index_zip_success(
return;
}
- os_fast_mutex_lock(&index->zip_pad.mutex);
+ dict_index_zip_pad_lock(index);
++index->zip_pad.success;
dict_index_zip_pad_update(&index->zip_pad, zip_threshold);
- os_fast_mutex_unlock(&index->zip_pad.mutex);
+ dict_index_zip_pad_unlock(index);
}
/*********************************************************************//**
@@ -6818,10 +7286,10 @@ dict_index_zip_failure(
return;
}
- os_fast_mutex_lock(&index->zip_pad.mutex);
+ dict_index_zip_pad_lock(index);
++index->zip_pad.failure;
dict_index_zip_pad_update(&index->zip_pad, zip_threshold);
- os_fast_mutex_unlock(&index->zip_pad.mutex);
+ dict_index_zip_pad_unlock(index);
}
@@ -6853,9 +7321,9 @@ dict_index_zip_pad_optimal_page_size(
#ifdef HAVE_ATOMIC_BUILTINS
pad = os_atomic_increment_ulint(&index->zip_pad.pad, 0);
#else /* HAVE_ATOMIC_BUILTINS */
- os_fast_mutex_lock(&index->zip_pad.mutex);
+ dict_index_zip_pad_lock(index);
pad = index->zip_pad.pad;
- os_fast_mutex_unlock(&index->zip_pad.mutex);
+ dict_index_zip_pad_unlock(index);
#endif /* HAVE_ATOMIC_BUILTINS */
ut_ad(pad < UNIV_PAGE_SIZE);