572
573
"InnoDB: If the mysqld server crashes"
573
574
" after the startup or when\n"
574
575
"InnoDB: you dump the tables, look at\n"
575
"InnoDB: " REFMAN "forcing-recovery.html"
576
"InnoDB: " REFMAN "forcing-innodb-recovery.html"
576
577
" for help.\n", stderr);
579
case DB_FOREIGN_EXCEED_MAX_CASCADE:
580
fprintf(stderr, "InnoDB: Cannot delete/update rows with"
581
" cascading foreign key constraints that exceed max"
583
"Please drop excessive foreign constraints"
584
" and try again\n", (ulong) DICT_FK_MAX_RECURSIVE_LOAD);
579
587
fprintf(stderr, "InnoDB: unknown error code %lu\n",
1429
1449
/*********************************************************************//**
1430
This can only be used when srv_locks_unsafe_for_binlog is TRUE or
1431
this session is using a READ COMMITTED isolation level. Before
1432
calling this function we must use trx_reset_new_rec_lock_info() and
1433
trx_register_new_rec_lock() to store the information which new record locks
1434
really were set. This function removes a newly set lock under prebuilt->pcur,
1435
and also under prebuilt->clust_pcur. Currently, this is only used and tested
1436
in the case of an UPDATE or a DELETE statement, where the row lock is of the
1438
Thus, this implements a 'mini-rollback' that releases the latest record
1440
@return error code or DB_SUCCESS */
1450
This can only be used when srv_locks_unsafe_for_binlog is TRUE or this
1451
session is using a READ COMMITTED or READ UNCOMMITTED isolation level.
1452
Before calling this function row_search_for_mysql() must have
1453
initialized prebuilt->new_rec_locks to store the information which new
1454
record locks really were set. This function removes a newly set
1455
clustered index record lock under prebuilt->pcur or
1456
prebuilt->clust_pcur. Thus, this implements a 'mini-rollback' that
1457
releases the latest clustered index record lock we set.
1458
@return error code or DB_SUCCESS */
1443
1461
row_unlock_for_mysql(
1444
1462
/*=================*/
1445
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in MySQL
1463
row_prebuilt_t* prebuilt, /*!< in/out: prebuilt struct in MySQL
1447
ibool has_latches_on_recs)/*!< TRUE if called so that we have
1448
the latches on the records under pcur
1449
and clust_pcur, and we do not need to
1450
reposition the cursors. */
1465
ibool has_latches_on_recs)/*!< in: TRUE if called so
1466
that we have the latches on
1467
the records under pcur and
1468
clust_pcur, and we do not need
1469
to reposition the cursors. */
1452
1471
btr_pcur_t* pcur = prebuilt->pcur;
1453
1472
btr_pcur_t* clust_pcur = prebuilt->clust_pcur;
1576
1595
trx = thr_get_trx(thr);
1597
/* Increment fk_cascade_depth to record the recursive call depth on
1598
a single update/delete that affects multiple tables chained
1599
together with foreign key relations. */
1600
thr->fk_cascade_depth++;
1602
if (thr->fk_cascade_depth > FK_MAX_CASCADE_DEL) {
1603
return (DB_FOREIGN_EXCEED_MAX_CASCADE);
1578
1606
thr->run_node = node;
1579
1607
thr->prev_node = node;
1581
1609
row_upd_step(thr);
1611
/* The recursive call for cascading update/delete happens
1612
in above row_upd_step(), reset the counter once we come
1613
out of the recursive call, so it does not accumulate for
1614
different row deletes */
1615
thr->fk_cascade_depth = 0;
1583
1617
err = trx->error_state;
1585
1619
/* Note that the cascade node is a subnode of another InnoDB
1647
1681
/*********************************************************************//**
1648
Calculates the key number used inside MySQL for an Innobase index. We have
1649
to take into account if we generated a default clustered index for the table
1650
@return the key number used inside MySQL */
1653
row_get_mysql_key_number_for_index(
1654
/*===============================*/
1655
const dict_index_t* index) /*!< in: index */
1657
const dict_index_t* ind;
1663
ind = dict_table_get_first_index(index->table);
1665
while (index != ind) {
1666
ind = dict_table_get_next_index(ind);
1670
if (row_table_got_default_clust_index(index->table)) {
1678
/*********************************************************************//**
1679
1682
Locks the data dictionary in shared mode from modifications, for performing
1680
1683
foreign key check, rollback, or other operation invisible to MySQL. */
1900
1903
case DB_DUPLICATE_KEY:
1901
ut_print_timestamp(stderr);
1902
fputs(" InnoDB: Error: table ", stderr);
1903
ut_print_name(stderr, trx, TRUE, table->name);
1904
fputs(" already exists in InnoDB internal\n"
1905
"InnoDB: data dictionary. Have you deleted"
1907
"InnoDB: and not used DROP TABLE?"
1908
" Have you used DROP DATABASE\n"
1909
"InnoDB: for InnoDB tables in"
1910
" MySQL version <= 3.23.43?\n"
1911
"InnoDB: See the Restrictions section"
1912
" of the InnoDB manual.\n"
1913
"InnoDB: You can drop the orphaned table"
1914
" inside InnoDB by\n"
1915
"InnoDB: creating an InnoDB table with"
1916
" the same name in another\n"
1917
"InnoDB: database and copying the .frm file"
1918
" to the current database.\n"
1919
"InnoDB: Then MySQL thinks the table exists,"
1920
" and DROP TABLE will\n"
1921
"InnoDB: succeed.\n"
1922
"InnoDB: You can look for further help from\n"
1923
"InnoDB: " REFMAN "innodb-troubleshooting.html\n",
1926
1905
/* We may also get err == DB_ERROR if the .ibd file for the
1927
1906
table already exists */
1908
trx->error_state = DB_SUCCESS;
1909
trx_general_rollback_for_mysql(trx, NULL);
1910
dict_mem_table_free(table);
2104
2087
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
2106
err = dict_create_foreign_constraints(trx, sql_string, name,
2089
err = dict_create_foreign_constraints(trx, sql_string, sql_length,
2108
2091
if (err == DB_SUCCESS) {
2109
2092
/* Check that also referencing constraints are ok */
2110
err = dict_load_foreigns(name, TRUE);
2093
err = dict_load_foreigns(name, FALSE, TRUE);
2113
2096
if (err != DB_SUCCESS) {
2811
2794
dict_index_t* index;
2815
if (fil_create_new_single_table_tablespace(
2816
&space, table->name, FALSE, flags,
2796
dict_hdr_get_new_id(NULL, NULL, &space);
2798
/* Lock all index trees for this table. We must
2799
do so after dict_hdr_get_new_id() to preserve
2801
dict_table_x_lock_indexes(table);
2803
if (space == ULINT_UNDEFINED
2804
|| fil_create_new_single_table_tablespace(
2805
space, table->name, FALSE, flags,
2817
2806
FIL_IBD_FILE_INITIAL_SIZE) != DB_SUCCESS) {
2807
dict_table_x_unlock_indexes(table);
2818
2808
ut_print_timestamp(stderr);
2819
2809
fprintf(stderr,
2820
2810
" InnoDB: TRUNCATE TABLE %s failed to"
2843
2833
FIL_IBD_FILE_INITIAL_SIZE, &mtr);
2844
2834
mtr_commit(&mtr);
2837
/* Lock all index trees for this table, as we will
2838
truncate the table/index and possibly change their metadata.
2839
All DML/DDL are blocked by table level lock, with
2840
a few exceptions such as queries into information schema
2841
about the table, MySQL could try to access index stats
2842
for this kind of query, we need to use index locks to
2844
dict_table_x_lock_indexes(table);
2848
2847
/* scan SYS_INDEXES for all indexes of the table */
3357
3365
mem_heap_free(heap);
3368
case DB_TOO_MANY_CONCURRENT_TRXS:
3369
/* Cannot even find a free slot for the
3370
the undo log. We can directly exit here
3371
and return the DB_TOO_MANY_CONCURRENT_TRXS
3375
case DB_OUT_OF_FILE_SPACE:
3376
err = DB_MUST_GET_MORE_FILE_SPACE;
3378
row_mysql_handle_errors(&err, trx, NULL, NULL);
3380
/* Fall through to raise error */
3383
/* No other possible error returns */
3361
3389
if (locked_dictionary) {
3371
3399
return((int) err);
3402
/*********************************************************************//**
3403
Drop all temporary tables during crash recovery. */
3406
row_mysql_drop_temp_tables(void)
3407
/*============================*/
3414
trx = trx_allocate_for_background();
3415
trx->op_info = "dropping temporary tables";
3416
row_mysql_lock_data_dictionary(trx);
3418
heap = mem_heap_create(200);
3422
btr_pcur_open_at_index_side(
3424
dict_table_get_first_index(dict_sys->sys_tables),
3425
BTR_SEARCH_LEAF, &pcur, TRUE, &mtr);
3431
const char* table_name;
3432
dict_table_t* table;
3434
btr_pcur_move_to_next_user_rec(&pcur, &mtr);
3436
if (!btr_pcur_is_on_user_rec(&pcur)) {
3440
rec = btr_pcur_get_rec(&pcur);
3441
field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
3442
if (len != 4 || !(mach_read_from_4(field) & 0x80000000UL)) {
3446
/* Because this is not a ROW_FORMAT=REDUNDANT table,
3447
the is_temp flag is valid. Examine it. */
3449
field = rec_get_nth_field_old(rec, 7/*MIX_LEN*/, &len);
3451
|| !(mach_read_from_4(field) & DICT_TF2_TEMPORARY)) {
3455
/* This is a temporary table. */
3456
field = rec_get_nth_field_old(rec, 0/*NAME*/, &len);
3457
if (len == UNIV_SQL_NULL || len == 0) {
3458
/* Corrupted SYS_TABLES.NAME */
3462
table_name = mem_heap_strdupl(heap, (const char*) field, len);
3464
btr_pcur_store_position(&pcur, &mtr);
3465
btr_pcur_commit_specify_mtr(&pcur, &mtr);
3467
table = dict_load_table(table_name);
3470
row_drop_table_for_mysql(table_name, trx, FALSE);
3471
trx_commit_for_mysql(trx);
3475
btr_pcur_restore_position(BTR_SEARCH_LEAF,
3479
btr_pcur_close(&pcur);
3481
mem_heap_free(heap);
3482
row_mysql_unlock_data_dictionary(trx);
3483
trx_free_for_background(trx);
3374
3486
/*******************************************************************//**
3375
3487
Drop all foreign keys in a database, see Bug#18942.
3376
3488
Called at the end of row_drop_database_for_mysql().
3922
4035
constraint is not broken, and calculates the number of index entries
3923
4036
in the read view of the current transaction.
3924
4037
@return TRUE if ok */
3927
row_scan_and_check_index(
3928
/*=====================*/
3929
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in MySQL */
3930
dict_index_t* index, /*!< in: index */
3931
ulint* n_rows) /*!< out: number of entries seen in the
3932
current consistent read */
4040
row_check_index_for_mysql(
4041
/*======================*/
4042
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct
4044
const dict_index_t* index, /*!< in: index */
4045
ulint* n_rows) /*!< out: number of entries
4046
seen in the consistent read */
3934
4048
dtuple_t* prev_entry = NULL;
3935
4049
ulint matched_fields;
3953
if (!row_merge_is_index_usable(prebuilt->trx, index)) {
3954
/* A newly created index may lack some delete-marked
3955
records that may exist in the read view of
3956
prebuilt->trx. Thus, such indexes must not be
3957
accessed by consistent read. */
3961
4067
buf = mem_alloc(UNIV_PAGE_SIZE);
3962
4068
heap = mem_heap_create(100);
3964
/* Make a dummy template in prebuilt, which we will use
3965
in scanning the index entries */
3967
prebuilt->index = index;
3968
/* row_merge_is_index_usable() was already checked above. */
3969
prebuilt->index_usable = TRUE;
3970
prebuilt->sql_stat_start = TRUE;
3971
prebuilt->template_type = ROW_MYSQL_DUMMY_TEMPLATE;
3972
prebuilt->n_template = 0;
3973
prebuilt->need_to_access_clustered = FALSE;
3975
dtuple_set_n_fields(prebuilt->search_tuple, 0);
3977
prebuilt->select_lock_type = LOCK_NONE;
3980
4072
ret = row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, 0);
4095
4187
/*********************************************************************//**
4096
Checks a table for corruption.
4097
@return DB_ERROR or DB_SUCCESS */
4100
row_check_table_for_mysql(
4101
/*======================*/
4102
row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
4105
dict_table_t* table = prebuilt->table;
4106
dict_index_t* index;
4108
ulint n_rows_in_table = ULINT_UNDEFINED;
4109
ulint ret = DB_SUCCESS;
4110
ulint old_isolation_level;
4112
if (table->ibd_file_missing) {
4113
ut_print_timestamp(stderr);
4114
fprintf(stderr, " InnoDB: Error:\n"
4115
"InnoDB: MySQL is trying to use a table handle"
4116
" but the .ibd file for\n"
4117
"InnoDB: table %s does not exist.\n"
4118
"InnoDB: Have you deleted the .ibd file"
4119
" from the database directory under\n"
4120
"InnoDB: the MySQL datadir, or have you"
4121
" used DISCARD TABLESPACE?\n"
4122
"InnoDB: Look from\n"
4123
"InnoDB: " REFMAN "innodb-troubleshooting.html\n"
4124
"InnoDB: how you can resolve the problem.\n",
4129
prebuilt->trx->op_info = "checking table";
4131
old_isolation_level = prebuilt->trx->isolation_level;
4133
/* We must run the index record counts at an isolation level
4134
>= READ COMMITTED, because a dirty read can see a wrong number
4135
of records in some index; to play safe, we use always
4136
REPEATABLE READ here */
4138
prebuilt->trx->isolation_level = TRX_ISO_REPEATABLE_READ;
4140
/* Enlarge the fatal lock wait timeout during CHECK TABLE. */
4141
mutex_enter(&kernel_mutex);
4142
srv_fatal_semaphore_wait_threshold += 7200; /* 2 hours */
4143
mutex_exit(&kernel_mutex);
4145
index = dict_table_get_first_index(table);
4147
while (index != NULL) {
4148
/* fputs("Validating index ", stderr);
4149
ut_print_name(stderr, trx, FALSE, index->name);
4150
putc('\n', stderr); */
4152
if (!btr_validate_index(index, prebuilt->trx)) {
4155
if (!row_scan_and_check_index(prebuilt,index, &n_rows)){
4159
if (trx_is_interrupted(prebuilt->trx)) {
4163
/* fprintf(stderr, "%lu entries in index %s\n", n_rows,
4166
if (index == dict_table_get_first_index(table)) {
4167
n_rows_in_table = n_rows;
4168
} else if (n_rows != n_rows_in_table) {
4172
fputs("Error: ", stderr);
4173
dict_index_name_print(stderr,
4174
prebuilt->trx, index);
4176
" contains %lu entries,"
4179
(ulong) n_rows_in_table);
4183
index = dict_table_get_next_index(index);
4186
/* Restore the original isolation level */
4187
prebuilt->trx->isolation_level = old_isolation_level;
4189
/* We validate also the whole adaptive hash index for all tables
4190
at every CHECK TABLE */
4192
if (!btr_search_validate()) {
4197
/* Restore the fatal lock wait timeout after CHECK TABLE. */
4198
mutex_enter(&kernel_mutex);
4199
srv_fatal_semaphore_wait_threshold -= 7200; /* 2 hours */
4200
mutex_exit(&kernel_mutex);
4202
prebuilt->trx->op_info = "";
4207
/*********************************************************************//**
4208
4188
Determines if a table is a magic monitor table.
4209
4189
@return TRUE if monitor table */