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
*****************************************************************************/
95
93
((str1_len) == sizeof(str2_onstack) \
96
94
&& memcmp(str1, str2_onstack, sizeof(str2_onstack)) == 0)
96
/*******************************************************************//**
97
Determine if the given name is a name reserved for MySQL system tables.
98
@return TRUE if name is a MySQL system table name */
101
row_mysql_is_system_table(
102
/*======================*/
105
if (strncmp(name, "mysql/", 6) != 0) {
110
return(0 == strcmp(name + 6, "host")
111
|| 0 == strcmp(name + 6, "user")
112
|| 0 == strcmp(name + 6, "db"));
98
115
/*********************************************************************//**
99
116
If a table is not yet in the drop list, adds the table to the list of tables
100
117
which the master thread drops in background. We need this on Unix because in
251
268
/**************************************************************//**
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
269
Stores a non-SQL-NULL field given in the MySQL format in the InnoDB format.
296
270
The counterpart of this function is row_sel_field_store_in_mysql_format() in
383
357
/* Remove trailing spaces from old style VARCHAR
386
/* Handle Unicode strings differently. */
360
/* Handle UCS2 strings differently. */
387
361
ulint mbminlen = dtype_get_mbminlen(dtype);
389
363
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
366
/* space=0x0020 */
409
367
/* Trim "half-chars", just in case. */
914
861
if (counter > 2000000000
915
862
|| ((ib_int64_t)counter > 16 + table->stat_n_rows / 16)) {
917
dict_update_statistics(table, FALSE /* update even if stats
864
dict_update_statistics(table);
922
868
/*********************************************************************//**
923
Unlocks AUTO_INC type locks that were possibly reserved by a trx. This
924
function should be called at the the end of an SQL statement, by the
925
connection thread that owns the transaction (trx->mysql_thd). */
869
Unlocks AUTO_INC type locks that were possibly reserved by a trx. */
928
872
row_unlock_table_autoinc_for_mysql(
929
873
/*===============================*/
930
874
trx_t* trx) /*!< in/out: transaction */
932
if (lock_trx_holds_autoinc_locks(trx)) {
933
mutex_enter(&kernel_mutex);
935
lock_release_autoinc_locks(trx);
937
mutex_exit(&kernel_mutex);
876
mutex_enter(&kernel_mutex);
878
lock_release_autoinc_locks(trx);
880
mutex_exit(&kernel_mutex);
941
883
/*********************************************************************//**
1066
1008
trx_start_if_not_started(trx);
1069
err = lock_table(0, table, static_cast<lock_mode>(mode), thr);
1011
err = lock_table(0, table, mode, thr);
1071
1013
err = lock_table(0, prebuilt->table,
1072
static_cast<lock_mode>(prebuilt->select_lock_type), thr);
1014
prebuilt->select_lock_type, thr);
1075
1017
trx->error_state = err;
1498
1425
/*********************************************************************//**
1499
This can only be used when srv_locks_unsafe_for_binlog is TRUE or this
1500
session is using a READ COMMITTED or READ UNCOMMITTED isolation level.
1501
Before calling this function row_search_for_mysql() must have
1502
initialized prebuilt->new_rec_locks to store the information which new
1503
record locks really were set. This function removes a newly set
1504
clustered index record lock under prebuilt->pcur or
1505
prebuilt->clust_pcur. Thus, this implements a 'mini-rollback' that
1506
releases the latest clustered index record lock we set.
1507
@return error code or DB_SUCCESS */
1426
This can only be used when srv_locks_unsafe_for_binlog is TRUE or
1427
this session is using a READ COMMITTED isolation level. Before
1428
calling this function we must use trx_reset_new_rec_lock_info() and
1429
trx_register_new_rec_lock() to store the information which new record locks
1430
really were set. This function removes a newly set lock under prebuilt->pcur,
1431
and also under prebuilt->clust_pcur. Currently, this is only used and tested
1432
in the case of an UPDATE or a DELETE statement, where the row lock is of the
1434
Thus, this implements a 'mini-rollback' that releases the latest record
1436
@return error code or DB_SUCCESS */
1510
1439
row_unlock_for_mysql(
1511
1440
/*=================*/
1512
row_prebuilt_t* prebuilt, /*!< in/out: prebuilt struct in MySQL
1441
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in MySQL
1514
ibool has_latches_on_recs)/*!< in: TRUE if called so
1515
that we have the latches on
1516
the records under pcur and
1517
clust_pcur, and we do not need
1518
to reposition the cursors. */
1443
ibool has_latches_on_recs)/*!< TRUE if called so that we have
1444
the latches on the records under pcur
1445
and clust_pcur, and we do not need to
1446
reposition the cursors. */
1520
1448
btr_pcur_t* pcur = prebuilt->pcur;
1521
1449
btr_pcur_t* clust_pcur = prebuilt->clust_pcur;
1601
if (rec_trx_id != trx->id) {
1529
if (ut_dulint_cmp(rec_trx_id, trx->id) != 0) {
1602
1530
/* We did not update the record: unlock it */
1604
1532
rec = btr_pcur_get_rec(pcur);
1605
1533
index = btr_pcur_get_btr_cur(pcur)->index;
1607
1535
lock_rec_unlock(trx, btr_pcur_get_block(pcur),
1608
rec, static_cast<lock_mode>(prebuilt->select_lock_type));
1536
rec, prebuilt->select_lock_type);
1610
1538
if (prebuilt->new_rec_locks >= 2) {
1611
1539
rec = btr_pcur_get_rec(clust_pcur);
1644
1572
trx = thr_get_trx(thr);
1646
/* Increment fk_cascade_depth to record the recursive call depth on
1647
a single update/delete that affects multiple tables chained
1648
together with foreign key relations. */
1649
thr->fk_cascade_depth++;
1651
if (thr->fk_cascade_depth > FK_MAX_CASCADE_DEL) {
1652
return (DB_FOREIGN_EXCEED_MAX_CASCADE);
1655
1574
thr->run_node = node;
1656
1575
thr->prev_node = node;
1658
1577
row_upd_step(thr);
1660
/* The recursive call for cascading update/delete happens
1661
in above row_upd_step(), reset the counter once we come
1662
out of the recursive call, so it does not accumulate for
1663
different row deletes */
1664
thr->fk_cascade_depth = 0;
1666
1579
err = trx->error_state;
1668
1581
/* Note that the cascade node is a subnode of another InnoDB
1730
1643
/*********************************************************************//**
1644
Calculates the key number used inside MySQL for an Innobase index. We have
1645
to take into account if we generated a default clustered index for the table
1646
@return the key number used inside MySQL */
1649
row_get_mysql_key_number_for_index(
1650
/*===============================*/
1651
const dict_index_t* index) /*!< in: index */
1653
const dict_index_t* ind;
1659
ind = dict_table_get_first_index(index->table);
1661
while (index != ind) {
1662
ind = dict_table_get_next_index(ind);
1666
if (row_table_got_default_clust_index(index->table)) {
1674
/*********************************************************************//**
1731
1675
Locks the data dictionary in shared mode from modifications, for performing
1732
1676
foreign key check, rollback, or other operation invisible to MySQL. */
1846
1792
trx->op_info = "creating table";
1794
if (row_mysql_is_system_table(table->name)) {
1797
"InnoDB: Error: trying to create a MySQL system"
1798
" table %s of type InnoDB.\n"
1799
"InnoDB: MySQL system tables must be"
1800
" of the MyISAM type!\n",
1805
/* Check that no reserved column names are used. */
1806
for (i = 0; i < dict_table_get_n_user_cols(table); i++) {
1807
if (dict_col_name_is_reserved(
1808
dict_table_get_col_name(table, i))) {
1848
1814
trx_start_if_not_started(trx);
1850
1816
/* The table name is prefixed with the database name and a '/'.
1913
1879
thr = pars_complete_graph_for_exec(node, trx, heap);
1915
ut_a(thr == que_fork_start_command(static_cast<que_fork_t *>(que_node_get_parent(thr))));
1881
ut_a(thr == que_fork_start_command(que_node_get_parent(thr)));
1916
1882
que_run_threads(thr);
1918
1884
err = trx->error_state;
1886
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
1887
trx->error_state = DB_SUCCESS;
1888
trx_general_rollback_for_mysql(trx, FALSE, NULL);
1923
1892
case DB_OUT_OF_FILE_SPACE:
1924
trx->error_state = DB_SUCCESS;
1925
trx_general_rollback_for_mysql(trx, NULL);
1927
1893
ut_print_timestamp(stderr);
1928
1894
fputs(" InnoDB: Warning: cannot create table ",
1940
1906
case DB_DUPLICATE_KEY:
1907
ut_print_timestamp(stderr);
1908
fputs(" InnoDB: Error: table ", stderr);
1909
ut_print_name(stderr, trx, TRUE, table->name);
1910
fputs(" already exists in InnoDB internal\n"
1911
"InnoDB: data dictionary. Have you deleted"
1913
"InnoDB: and not used DROP TABLE?"
1914
" Have you used DROP DATABASE\n"
1915
"InnoDB: for InnoDB tables in"
1916
" MySQL version <= 3.23.43?\n"
1917
"InnoDB: See the Restrictions section"
1918
" of the InnoDB manual.\n"
1919
"InnoDB: You can drop the orphaned table"
1920
" inside InnoDB by\n"
1921
"InnoDB: creating an InnoDB table with"
1922
" the same name in another\n"
1923
"InnoDB: database and copying the .frm file"
1924
" to the current database.\n"
1925
"InnoDB: Then MySQL thinks the table exists,"
1926
" and DROP TABLE will\n"
1927
"InnoDB: succeed.\n"
1928
"InnoDB: You can look for further help from\n"
1929
"InnoDB: " REFMAN "innodb-troubleshooting.html\n",
1942
1932
/* We may also get err == DB_ERROR if the .ibd file for the
1943
1933
table already exists */
1945
trx->error_state = DB_SUCCESS;
1946
trx_general_rollback_for_mysql(trx, NULL);
1947
dict_mem_table_free(table);
2124
2110
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
2126
err = dict_create_foreign_constraints(trx, sql_string, sql_length,
2112
err = dict_create_foreign_constraints(trx, sql_string, name,
2128
2114
if (err == DB_SUCCESS) {
2129
2115
/* Check that also referencing constraints are ok */
2130
err = dict_load_foreigns(name, FALSE, TRUE);
2116
err = dict_load_foreigns(name, TRUE);
2133
2119
if (err != DB_SUCCESS) {
2831
2817
dict_index_t* index;
2833
dict_hdr_get_new_id(NULL, NULL, &space);
2835
/* Lock all index trees for this table. We must
2836
do so after dict_hdr_get_new_id() to preserve
2838
dict_table_x_lock_indexes(table);
2840
if (space == ULINT_UNDEFINED
2841
|| fil_create_new_single_table_tablespace(
2842
space, table->name, FALSE, flags,
2821
if (fil_create_new_single_table_tablespace(
2822
&space, table->name, FALSE, flags,
2843
2823
FIL_IBD_FILE_INITIAL_SIZE) != DB_SUCCESS) {
2844
dict_table_x_unlock_indexes(table);
2845
2824
ut_print_timestamp(stderr);
2846
2825
fprintf(stderr,
2847
2826
" InnoDB: TRUNCATE TABLE %s failed to"
2870
2849
FIL_IBD_FILE_INITIAL_SIZE, &mtr);
2871
2850
mtr_commit(&mtr);
2874
/* Lock all index trees for this table, as we will
2875
truncate the table/index and possibly change their metadata.
2876
All DML/DDL are blocked by table level lock, with
2877
a few exceptions such as queries into information schema
2878
about the table, MySQL could try to access index stats
2879
for this kind of query, we need to use index locks to
2881
dict_table_x_lock_indexes(table);
2884
2854
/* scan SYS_INDEXES for all indexes of the table */
2955
2925
mem_heap_free(heap);
2957
/* Done with index truncation, release index tree locks,
2958
subsequent work relates to table level metadata change */
2959
dict_table_x_unlock_indexes(table);
2961
dict_hdr_get_new_id(&new_id, NULL, NULL);
2927
new_id = dict_hdr_get_new_id(DICT_HDR_TABLE_ID);
2963
2929
info = pars_info_create();
2965
2931
pars_info_add_int4_literal(info, "space", (lint) table->space);
2966
pars_info_add_ull_literal(info, "old_id", table->id);
2967
pars_info_add_ull_literal(info, "new_id", new_id);
2932
pars_info_add_dulint_literal(info, "old_id", table->id);
2933
pars_info_add_dulint_literal(info, "new_id", new_id);
2969
2935
err = que_eval_sql(info,
2970
2936
"PROCEDURE RENUMBER_TABLESPACE_PROC () IS\n"
2997
2963
dict_table_change_id_in_cache(table, new_id);
3001
MySQL calls ha_innobase::reset_auto_increment() which does
2966
/* MySQL calls ha_innobase::reset_auto_increment() which does
3004
2968
dict_table_autoinc_lock(table);
3005
2969
dict_table_autoinc_initialize(table, 1);
3006
2970
dict_table_autoinc_unlock(table);
3007
dict_update_statistics(table, FALSE /* update even if stats are
2971
dict_update_statistics(table);
3010
2973
trx_commit_for_mysql(trx);
3214
3177
if (table->n_foreign_key_checks_running > 0) {
3216
const char* i_table_name = table->name;
3179
const char* table_name = table->name;
3219
added = row_add_table_to_background_drop_list(i_table_name);
3182
added = row_add_table_to_background_drop_list(table_name);
3222
3185
ut_print_timestamp(stderr);
3223
3186
fputs(" InnoDB: You are trying to drop table ",
3225
ut_print_name(stderr, trx, TRUE, i_table_name);
3188
ut_print_name(stderr, trx, TRUE, table_name);
3227
3190
"InnoDB: though there is a"
3228
3191
" foreign key check running on it.\n"
3341
3310
space_id = table->space;
3343
3312
if (table->dir_path_of_temp_table != NULL) {
3344
3314
name_or_path = mem_heap_strdup(
3345
3315
heap, table->dir_path_of_temp_table);
3348
3318
name_or_path = name;
3349
is_temp = (table->flags >> DICT_TF2_SHIFT)
3350
& DICT_TF2_TEMPORARY;
3353
3321
dict_table_remove_from_cache(table);
3355
if (dict_load_table(name, TRUE) != NULL) {
3323
if (dict_load_table(name) != NULL) {
3356
3324
ut_print_timestamp(stderr);
3357
3325
fputs(" InnoDB: Error: not able to remove table ",
3399
3367
mem_heap_free(heap);
3402
case DB_TOO_MANY_CONCURRENT_TRXS:
3403
/* Cannot even find a free slot for the
3404
the undo log. We can directly exit here
3405
and return the DB_TOO_MANY_CONCURRENT_TRXS
3409
case DB_OUT_OF_FILE_SPACE:
3410
err = DB_MUST_GET_MORE_FILE_SPACE;
3412
row_mysql_handle_errors(&err, trx, NULL, NULL);
3414
/* Fall through to raise error */
3417
/* No other possible error returns */
3423
3371
if (locked_dictionary) {
3433
3381
return((int) err);
3436
/*********************************************************************//**
3437
Drop all temporary tables during crash recovery. */
3440
row_mysql_drop_temp_tables(void)
3441
/*============================*/
3448
trx = trx_allocate_for_background();
3449
trx->op_info = "dropping temporary tables";
3450
row_mysql_lock_data_dictionary(trx);
3452
heap = mem_heap_create(200);
3456
btr_pcur_open_at_index_side(
3458
dict_table_get_first_index(dict_sys->sys_tables),
3459
BTR_SEARCH_LEAF, &pcur, TRUE, &mtr);
3465
const char* table_name;
3466
dict_table_t* table;
3468
btr_pcur_move_to_next_user_rec(&pcur, &mtr);
3470
if (!btr_pcur_is_on_user_rec(&pcur)) {
3474
rec = btr_pcur_get_rec(&pcur);
3475
field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
3476
if (len != 4 || !(mach_read_from_4(field) & 0x80000000UL)) {
3480
/* Because this is not a ROW_FORMAT=REDUNDANT table,
3481
the is_temp flag is valid. Examine it. */
3483
field = rec_get_nth_field_old(rec, 7/*MIX_LEN*/, &len);
3485
|| !(mach_read_from_4(field) & DICT_TF2_TEMPORARY)) {
3489
/* This is a temporary table. */
3490
field = rec_get_nth_field_old(rec, 0/*NAME*/, &len);
3491
if (len == UNIV_SQL_NULL || len == 0) {
3492
/* Corrupted SYS_TABLES.NAME */
3496
table_name = mem_heap_strdupl(heap, (const char*) field, len);
3498
btr_pcur_store_position(&pcur, &mtr);
3499
btr_pcur_commit_specify_mtr(&pcur, &mtr);
3501
table = dict_load_table(table_name, TRUE);
3504
row_drop_table_for_mysql(table_name, trx, FALSE);
3505
trx_commit_for_mysql(trx);
3509
btr_pcur_restore_position(BTR_SEARCH_LEAF,
3513
btr_pcur_close(&pcur);
3515
mem_heap_free(heap);
3516
row_mysql_unlock_data_dictionary(trx);
3517
trx_free_for_background(trx);
3520
3384
/*******************************************************************//**
3521
3385
Drop all foreign keys in a database, see Bug#18942.
3522
3386
Called at the end of row_drop_database_for_mysql().
4066
3936
constraint is not broken, and calculates the number of index entries
4067
3937
in the read view of the current transaction.
4068
3938
@return TRUE if ok */
4071
row_check_index_for_mysql(
4072
/*======================*/
4073
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct
4075
const dict_index_t* index, /*!< in: index */
4076
ulint* n_rows) /*!< out: number of entries
4077
seen in the consistent read */
3941
row_scan_and_check_index(
3942
/*=====================*/
3943
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in MySQL */
3944
dict_index_t* index, /*!< in: index */
3945
ulint* n_rows) /*!< out: number of entries seen in the
3946
current consistent read */
4079
3948
dtuple_t* prev_entry = NULL;
4080
3949
ulint matched_fields;
4098
buf = static_cast<byte *>(mem_alloc(UNIV_PAGE_SIZE));
3967
if (!row_merge_is_index_usable(prebuilt->trx, index)) {
3968
/* A newly created index may lack some delete-marked
3969
records that may exist in the read view of
3970
prebuilt->trx. Thus, such indexes must not be
3971
accessed by consistent read. */
3975
buf = mem_alloc(UNIV_PAGE_SIZE);
4099
3976
heap = mem_heap_create(100);
3978
/* Make a dummy template in prebuilt, which we will use
3979
in scanning the index entries */
3981
prebuilt->index = index;
3982
/* row_merge_is_index_usable() was already checked above. */
3983
prebuilt->index_usable = TRUE;
3984
prebuilt->sql_stat_start = TRUE;
3985
prebuilt->template_type = ROW_MYSQL_DUMMY_TEMPLATE;
3986
prebuilt->n_template = 0;
3987
prebuilt->need_to_access_clustered = FALSE;
3989
dtuple_set_n_fields(prebuilt->search_tuple, 0);
3991
prebuilt->select_lock_type = LOCK_NONE;
4103
3994
ret = row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, 0);
4218
4109
/*********************************************************************//**
4110
Checks a table for corruption.
4111
@return DB_ERROR or DB_SUCCESS */
4114
row_check_table_for_mysql(
4115
/*======================*/
4116
row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
4119
dict_table_t* table = prebuilt->table;
4120
dict_index_t* index;
4122
ulint n_rows_in_table = ULINT_UNDEFINED;
4123
ulint ret = DB_SUCCESS;
4124
ulint old_isolation_level;
4126
if (table->ibd_file_missing) {
4127
ut_print_timestamp(stderr);
4128
fprintf(stderr, " InnoDB: Error:\n"
4129
"InnoDB: MySQL is trying to use a table handle"
4130
" but the .ibd file for\n"
4131
"InnoDB: table %s does not exist.\n"
4132
"InnoDB: Have you deleted the .ibd file"
4133
" from the database directory under\n"
4134
"InnoDB: the MySQL datadir, or have you"
4135
" used DISCARD TABLESPACE?\n"
4136
"InnoDB: Look from\n"
4137
"InnoDB: " REFMAN "innodb-troubleshooting.html\n"
4138
"InnoDB: how you can resolve the problem.\n",
4143
prebuilt->trx->op_info = "checking table";
4145
old_isolation_level = prebuilt->trx->isolation_level;
4147
/* We must run the index record counts at an isolation level
4148
>= READ COMMITTED, because a dirty read can see a wrong number
4149
of records in some index; to play safe, we use always
4150
REPEATABLE READ here */
4152
prebuilt->trx->isolation_level = TRX_ISO_REPEATABLE_READ;
4154
/* Enlarge the fatal lock wait timeout during CHECK TABLE. */
4155
mutex_enter(&kernel_mutex);
4156
srv_fatal_semaphore_wait_threshold += 7200; /* 2 hours */
4157
mutex_exit(&kernel_mutex);
4159
index = dict_table_get_first_index(table);
4161
while (index != NULL) {
4162
/* fputs("Validating index ", stderr);
4163
ut_print_name(stderr, trx, FALSE, index->name);
4164
putc('\n', stderr); */
4166
if (!btr_validate_index(index, prebuilt->trx)) {
4169
if (!row_scan_and_check_index(prebuilt,index, &n_rows)){
4173
if (trx_is_interrupted(prebuilt->trx)) {
4177
/* fprintf(stderr, "%lu entries in index %s\n", n_rows,
4180
if (index == dict_table_get_first_index(table)) {
4181
n_rows_in_table = n_rows;
4182
} else if (n_rows != n_rows_in_table) {
4186
fputs("Error: ", stderr);
4187
dict_index_name_print(stderr,
4188
prebuilt->trx, index);
4190
" contains %lu entries,"
4193
(ulong) n_rows_in_table);
4197
index = dict_table_get_next_index(index);
4200
/* Restore the original isolation level */
4201
prebuilt->trx->isolation_level = old_isolation_level;
4203
/* We validate also the whole adaptive hash index for all tables
4204
at every CHECK TABLE */
4206
if (!btr_search_validate()) {
4211
/* Restore the fatal lock wait timeout after CHECK TABLE. */
4212
mutex_enter(&kernel_mutex);
4213
srv_fatal_semaphore_wait_threshold -= 7200; /* 2 hours */
4214
mutex_exit(&kernel_mutex);
4216
prebuilt->trx->op_info = "";
4221
/*********************************************************************//**
4219
4222
Determines if a table is a magic monitor table.
4220
4223
@return TRUE if monitor table */