11
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
17
*****************************************************************************/
251
251
/**************************************************************//**
252
Pad a column with spaces. */
257
ulint mbminlen, /*!< in: minimum size of a character,
259
byte* pad, /*!< out: padded buffer */
260
ulint len) /*!< in: number of bytes to pad */
264
switch (UNIV_EXPECT(mbminlen, 1)) {
269
memset(pad, 0x20, len);
278
} while (pad < pad_end);
281
/* space=0x00000020 */
289
} while (pad < pad_end);
294
/**************************************************************//**
295
252
Stores a non-SQL-NULL field given in the MySQL format in the InnoDB format.
296
253
The counterpart of this function is row_sel_field_store_in_mysql_format() in
383
340
/* Remove trailing spaces from old style VARCHAR
386
/* Handle Unicode strings differently. */
343
/* Handle UCS2 strings differently. */
387
344
ulint mbminlen = dtype_get_mbminlen(dtype);
389
346
ptr = mysql_data;
395
/* space=0x00000020 */
396
/* Trim "half-chars", just in case. */
400
&& ptr[col_len - 4] == 0x00
401
&& ptr[col_len - 3] == 0x00
402
&& ptr[col_len - 2] == 0x00
403
&& ptr[col_len - 1] == 0x20) {
408
349
/* space=0x0020 */
409
350
/* Trim "half-chars", just in case. */
921
851
/*********************************************************************//**
922
Unlocks AUTO_INC type locks that were possibly reserved by a trx. This
923
function should be called at the the end of an SQL statement, by the
924
connection thread that owns the transaction (trx->mysql_thd). */
852
Unlocks AUTO_INC type locks that were possibly reserved by a trx. */
927
855
row_unlock_table_autoinc_for_mysql(
928
856
/*===============================*/
929
857
trx_t* trx) /*!< in/out: transaction */
931
if (lock_trx_holds_autoinc_locks(trx)) {
932
mutex_enter(&kernel_mutex);
934
lock_release_autoinc_locks(trx);
936
mutex_exit(&kernel_mutex);
859
mutex_enter(&kernel_mutex);
861
lock_release_autoinc_locks(trx);
863
mutex_exit(&kernel_mutex);
940
866
/*********************************************************************//**
1497
1408
/*********************************************************************//**
1498
This can only be used when srv_locks_unsafe_for_binlog is TRUE or this
1499
session is using a READ COMMITTED or READ UNCOMMITTED isolation level.
1500
Before calling this function row_search_for_mysql() must have
1501
initialized prebuilt->new_rec_locks to store the information which new
1502
record locks really were set. This function removes a newly set
1503
clustered index record lock under prebuilt->pcur or
1504
prebuilt->clust_pcur. Thus, this implements a 'mini-rollback' that
1505
releases the latest clustered index record lock we set.
1506
@return error code or DB_SUCCESS */
1409
This can only be used when srv_locks_unsafe_for_binlog is TRUE or
1410
this session is using a READ COMMITTED isolation level. Before
1411
calling this function we must use trx_reset_new_rec_lock_info() and
1412
trx_register_new_rec_lock() to store the information which new record locks
1413
really were set. This function removes a newly set lock under prebuilt->pcur,
1414
and also under prebuilt->clust_pcur. Currently, this is only used and tested
1415
in the case of an UPDATE or a DELETE statement, where the row lock is of the
1417
Thus, this implements a 'mini-rollback' that releases the latest record
1419
@return error code or DB_SUCCESS */
1509
1422
row_unlock_for_mysql(
1510
1423
/*=================*/
1511
row_prebuilt_t* prebuilt, /*!< in/out: prebuilt struct in MySQL
1424
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in MySQL
1513
ibool has_latches_on_recs)/*!< in: TRUE if called so
1514
that we have the latches on
1515
the records under pcur and
1516
clust_pcur, and we do not need
1517
to reposition the cursors. */
1426
ibool has_latches_on_recs)/*!< TRUE if called so that we have
1427
the latches on the records under pcur
1428
and clust_pcur, and we do not need to
1429
reposition the cursors. */
1519
1431
btr_pcur_t* pcur = prebuilt->pcur;
1520
1432
btr_pcur_t* clust_pcur = prebuilt->clust_pcur;
1643
1555
trx = thr_get_trx(thr);
1645
/* Increment fk_cascade_depth to record the recursive call depth on
1646
a single update/delete that affects multiple tables chained
1647
together with foreign key relations. */
1648
thr->fk_cascade_depth++;
1650
if (thr->fk_cascade_depth > FK_MAX_CASCADE_DEL) {
1651
return (DB_FOREIGN_EXCEED_MAX_CASCADE);
1654
1557
thr->run_node = node;
1655
1558
thr->prev_node = node;
1657
1560
row_upd_step(thr);
1659
/* The recursive call for cascading update/delete happens
1660
in above row_upd_step(), reset the counter once we come
1661
out of the recursive call, so it does not accumulate for
1662
different row deletes */
1663
thr->fk_cascade_depth = 0;
1665
1562
err = trx->error_state;
1667
1564
/* Note that the cascade node is a subnode of another InnoDB
1729
1626
/*********************************************************************//**
1627
Calculates the key number used inside MySQL for an Innobase index. We have
1628
to take into account if we generated a default clustered index for the table
1629
@return the key number used inside MySQL */
1632
row_get_mysql_key_number_for_index(
1633
/*===============================*/
1634
const dict_index_t* index) /*!< in: index */
1636
const dict_index_t* ind;
1642
ind = dict_table_get_first_index(index->table);
1644
while (index != ind) {
1645
ind = dict_table_get_next_index(ind);
1649
if (row_table_got_default_clust_index(index->table)) {
1657
/*********************************************************************//**
1730
1658
Locks the data dictionary in shared mode from modifications, for performing
1731
1659
foreign key check, rollback, or other operation invisible to MySQL. */
1941
1878
case DB_DUPLICATE_KEY:
1879
ut_print_timestamp(stderr);
1880
fputs(" InnoDB: Error: table ", stderr);
1881
ut_print_name(stderr, trx, TRUE, table->name);
1882
fputs(" already exists in InnoDB internal\n"
1883
"InnoDB: data dictionary. Have you deleted"
1885
"InnoDB: and not used DROP TABLE?"
1886
" Have you used DROP DATABASE\n"
1887
"InnoDB: for InnoDB tables in"
1888
" MySQL version <= 3.23.43?\n"
1889
"InnoDB: See the Restrictions section"
1890
" of the InnoDB manual.\n"
1891
"InnoDB: You can drop the orphaned table"
1892
" inside InnoDB by\n"
1893
"InnoDB: creating an InnoDB table with"
1894
" the same name in another\n"
1895
"InnoDB: database and copying the .frm file"
1896
" to the current database.\n"
1897
"InnoDB: Then MySQL thinks the table exists,"
1898
" and DROP TABLE will\n"
1899
"InnoDB: succeed.\n"
1900
"InnoDB: You can look for further help from\n"
1901
"InnoDB: " REFMAN "innodb-troubleshooting.html\n",
1942
1904
/* We may also get err == DB_ERROR if the .ibd file for the
1943
1905
table already exists */
2121
2082
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
2123
err = dict_create_foreign_constraints(trx, sql_string, sql_length,
2084
err = dict_create_foreign_constraints(trx, sql_string, name,
2125
2086
if (err == DB_SUCCESS) {
2126
2087
/* Check that also referencing constraints are ok */
2127
err = dict_load_foreigns(name, FALSE, TRUE);
2088
err = dict_load_foreigns(name, TRUE);
2130
2091
if (err != DB_SUCCESS) {
2818
2779
trx->table_id = table->id;
2820
/* Lock all index trees for this table, as we will
2821
truncate the table/index and possibly change their metadata.
2822
All DML/DDL are blocked by table level lock, with
2823
a few exceptions such as queries into information schema
2824
about the table, MySQL could try to access index stats
2825
for this kind of query, we need to use index locks to
2827
dict_table_x_lock_indexes(table);
2829
2781
if (table->space && !table->dir_path_of_temp_table) {
2830
2782
/* Discard and create the single-table tablespace. */
2831
2783
ulint space = table->space;
2837
2789
dict_index_t* index;
2839
dict_hdr_get_new_id(NULL, NULL, &space);
2841
if (space == ULINT_UNDEFINED
2842
|| fil_create_new_single_table_tablespace(
2843
space, table->name, FALSE, flags,
2793
if (fil_create_new_single_table_tablespace(
2794
&space, table->name, FALSE, flags,
2844
2795
FIL_IBD_FILE_INITIAL_SIZE) != DB_SUCCESS) {
2845
dict_table_x_unlock_indexes(table);
2846
2796
ut_print_timestamp(stderr);
2847
2797
fprintf(stderr,
2848
2798
" InnoDB: TRUNCATE TABLE %s failed to"
2947
2897
mem_heap_free(heap);
2949
/* Done with index truncation, release index tree locks,
2950
subsequent work relates to table level metadata change */
2951
dict_table_x_unlock_indexes(table);
2953
dict_hdr_get_new_id(&new_id, NULL, NULL);
2899
new_id = dict_hdr_get_new_id(DICT_HDR_TABLE_ID);
2955
2901
info = pars_info_create();
2957
2903
pars_info_add_int4_literal(info, "space", (lint) table->space);
2958
pars_info_add_ull_literal(info, "old_id", table->id);
2959
pars_info_add_ull_literal(info, "new_id", new_id);
2904
pars_info_add_dulint_literal(info, "old_id", table->id);
2905
pars_info_add_dulint_literal(info, "new_id", new_id);
2961
2907
err = que_eval_sql(info,
2962
2908
"PROCEDURE RENUMBER_TABLESPACE_PROC () IS\n"
3332
3284
space_id = table->space;
3334
3286
if (table->dir_path_of_temp_table != NULL) {
3335
3288
name_or_path = mem_heap_strdup(
3336
3289
heap, table->dir_path_of_temp_table);
3339
3292
name_or_path = name;
3340
is_temp = (table->flags >> DICT_TF2_SHIFT)
3341
& DICT_TF2_TEMPORARY;
3344
3295
dict_table_remove_from_cache(table);
3346
if (dict_load_table(name, TRUE) != NULL) {
3297
if (dict_load_table(name) != NULL) {
3347
3298
ut_print_timestamp(stderr);
3348
3299
fputs(" InnoDB: Error: not able to remove table ",
3390
3341
mem_heap_free(heap);
3393
case DB_TOO_MANY_CONCURRENT_TRXS:
3394
/* Cannot even find a free slot for the
3395
the undo log. We can directly exit here
3396
and return the DB_TOO_MANY_CONCURRENT_TRXS
3400
case DB_OUT_OF_FILE_SPACE:
3401
err = DB_MUST_GET_MORE_FILE_SPACE;
3403
row_mysql_handle_errors(&err, trx, NULL, NULL);
3405
/* Fall through to raise error */
3408
/* No other possible error returns */
3414
3345
if (locked_dictionary) {
3424
3355
return((int) err);
3427
/*********************************************************************//**
3428
Drop all temporary tables during crash recovery. */
3431
row_mysql_drop_temp_tables(void)
3432
/*============================*/
3439
trx = trx_allocate_for_background();
3440
trx->op_info = "dropping temporary tables";
3441
row_mysql_lock_data_dictionary(trx);
3443
heap = mem_heap_create(200);
3447
btr_pcur_open_at_index_side(
3449
dict_table_get_first_index(dict_sys->sys_tables),
3450
BTR_SEARCH_LEAF, &pcur, TRUE, &mtr);
3456
const char* table_name;
3457
dict_table_t* table;
3459
btr_pcur_move_to_next_user_rec(&pcur, &mtr);
3461
if (!btr_pcur_is_on_user_rec(&pcur)) {
3465
rec = btr_pcur_get_rec(&pcur);
3466
field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
3467
if (len != 4 || !(mach_read_from_4(field) & 0x80000000UL)) {
3471
/* Because this is not a ROW_FORMAT=REDUNDANT table,
3472
the is_temp flag is valid. Examine it. */
3474
field = rec_get_nth_field_old(rec, 7/*MIX_LEN*/, &len);
3476
|| !(mach_read_from_4(field) & DICT_TF2_TEMPORARY)) {
3480
/* This is a temporary table. */
3481
field = rec_get_nth_field_old(rec, 0/*NAME*/, &len);
3482
if (len == UNIV_SQL_NULL || len == 0) {
3483
/* Corrupted SYS_TABLES.NAME */
3487
table_name = mem_heap_strdupl(heap, (const char*) field, len);
3489
btr_pcur_store_position(&pcur, &mtr);
3490
btr_pcur_commit_specify_mtr(&pcur, &mtr);
3492
table = dict_load_table(table_name, TRUE);
3495
row_drop_table_for_mysql(table_name, trx, FALSE);
3496
trx_commit_for_mysql(trx);
3500
btr_pcur_restore_position(BTR_SEARCH_LEAF,
3504
btr_pcur_close(&pcur);
3506
mem_heap_free(heap);
3507
row_mysql_unlock_data_dictionary(trx);
3508
trx_free_for_background(trx);
3511
3358
/*******************************************************************//**
3512
3359
Drop all foreign keys in a database, see Bug#18942.
3513
3360
Called at the end of row_drop_database_for_mysql().
4057
3904
constraint is not broken, and calculates the number of index entries
4058
3905
in the read view of the current transaction.
4059
3906
@return TRUE if ok */
4062
row_check_index_for_mysql(
4063
/*======================*/
4064
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct
4066
const dict_index_t* index, /*!< in: index */
4067
ulint* n_rows) /*!< out: number of entries
4068
seen in the consistent read */
3909
row_scan_and_check_index(
3910
/*=====================*/
3911
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in MySQL */
3912
dict_index_t* index, /*!< in: index */
3913
ulint* n_rows) /*!< out: number of entries seen in the
3914
current consistent read */
4070
3916
dtuple_t* prev_entry = NULL;
4071
3917
ulint matched_fields;
3935
if (!row_merge_is_index_usable(prebuilt->trx, index)) {
3936
/* A newly created index may lack some delete-marked
3937
records that may exist in the read view of
3938
prebuilt->trx. Thus, such indexes must not be
3939
accessed by consistent read. */
4089
3943
buf = mem_alloc(UNIV_PAGE_SIZE);
4090
3944
heap = mem_heap_create(100);
3946
/* Make a dummy template in prebuilt, which we will use
3947
in scanning the index entries */
3949
prebuilt->index = index;
3950
/* row_merge_is_index_usable() was already checked above. */
3951
prebuilt->index_usable = TRUE;
3952
prebuilt->sql_stat_start = TRUE;
3953
prebuilt->template_type = ROW_MYSQL_DUMMY_TEMPLATE;
3954
prebuilt->n_template = 0;
3955
prebuilt->need_to_access_clustered = FALSE;
3957
dtuple_set_n_fields(prebuilt->search_tuple, 0);
3959
prebuilt->select_lock_type = LOCK_NONE;
4094
3962
ret = row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, 0);
4209
4077
/*********************************************************************//**
4078
Checks a table for corruption.
4079
@return DB_ERROR or DB_SUCCESS */
4082
row_check_table_for_mysql(
4083
/*======================*/
4084
row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
4087
dict_table_t* table = prebuilt->table;
4088
dict_index_t* index;
4090
ulint n_rows_in_table = ULINT_UNDEFINED;
4091
ulint ret = DB_SUCCESS;
4092
ulint old_isolation_level;
4094
if (table->ibd_file_missing) {
4095
ut_print_timestamp(stderr);
4096
fprintf(stderr, " InnoDB: Error:\n"
4097
"InnoDB: MySQL is trying to use a table handle"
4098
" but the .ibd file for\n"
4099
"InnoDB: table %s does not exist.\n"
4100
"InnoDB: Have you deleted the .ibd file"
4101
" from the database directory under\n"
4102
"InnoDB: the MySQL datadir, or have you"
4103
" used DISCARD TABLESPACE?\n"
4104
"InnoDB: Look from\n"
4105
"InnoDB: " REFMAN "innodb-troubleshooting.html\n"
4106
"InnoDB: how you can resolve the problem.\n",
4111
prebuilt->trx->op_info = "checking table";
4113
old_isolation_level = prebuilt->trx->isolation_level;
4115
/* We must run the index record counts at an isolation level
4116
>= READ COMMITTED, because a dirty read can see a wrong number
4117
of records in some index; to play safe, we use always
4118
REPEATABLE READ here */
4120
prebuilt->trx->isolation_level = TRX_ISO_REPEATABLE_READ;
4122
/* Enlarge the fatal lock wait timeout during CHECK TABLE. */
4123
mutex_enter(&kernel_mutex);
4124
srv_fatal_semaphore_wait_threshold += 7200; /* 2 hours */
4125
mutex_exit(&kernel_mutex);
4127
index = dict_table_get_first_index(table);
4129
while (index != NULL) {
4130
/* fputs("Validating index ", stderr);
4131
ut_print_name(stderr, trx, FALSE, index->name);
4132
putc('\n', stderr); */
4134
if (!btr_validate_index(index, prebuilt->trx)) {
4137
if (!row_scan_and_check_index(prebuilt,index, &n_rows)){
4141
if (trx_is_interrupted(prebuilt->trx)) {
4145
/* fprintf(stderr, "%lu entries in index %s\n", n_rows,
4148
if (index == dict_table_get_first_index(table)) {
4149
n_rows_in_table = n_rows;
4150
} else if (n_rows != n_rows_in_table) {
4154
fputs("Error: ", stderr);
4155
dict_index_name_print(stderr,
4156
prebuilt->trx, index);
4158
" contains %lu entries,"
4161
(ulong) n_rows_in_table);
4165
index = dict_table_get_next_index(index);
4168
/* Restore the original isolation level */
4169
prebuilt->trx->isolation_level = old_isolation_level;
4171
/* We validate also the whole adaptive hash index for all tables
4172
at every CHECK TABLE */
4174
if (!btr_search_validate()) {
4179
/* Restore the fatal lock wait timeout after CHECK TABLE. */
4180
mutex_enter(&kernel_mutex);
4181
srv_fatal_semaphore_wait_threshold -= 7200; /* 2 hours */
4182
mutex_exit(&kernel_mutex);
4184
prebuilt->trx->op_info = "";
4189
/*********************************************************************//**
4210
4190
Determines if a table is a magic monitor table.
4211
4191
@return TRUE if monitor table */