You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5468 lines
147 KiB

8 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
12 years ago
10 years ago
10 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*****************************************************************************
  2. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file row/row0mysql.cc
  15. Interface between Innobase row operations and MySQL.
  16. Contains also create table and other data dictionary operations.
  17. Created 9/17/2000 Heikki Tuuri
  18. *******************************************************/
  19. #include "row0mysql.h"
  20. #ifdef UNIV_NONINL
  21. #include "row0mysql.ic"
  22. #endif
  23. #include <debug_sync.h>
  24. #include <my_dbug.h>
  25. #include <sql_const.h>
  26. #include "row0ins.h"
  27. #include "row0merge.h"
  28. #include "row0sel.h"
  29. #include "row0upd.h"
  30. #include "row0row.h"
  31. #include "que0que.h"
  32. #include "pars0pars.h"
  33. #include "dict0dict.h"
  34. #include "dict0crea.h"
  35. #include "dict0load.h"
  36. #include "dict0boot.h"
  37. #include "dict0stats.h"
  38. #include "dict0stats_bg.h"
  39. #include "trx0roll.h"
  40. #include "trx0purge.h"
  41. #include "trx0rec.h"
  42. #include "trx0undo.h"
  43. #include "lock0lock.h"
  44. #include "rem0cmp.h"
  45. #include "log0log.h"
  46. #include "btr0sea.h"
  47. #include "fil0fil.h"
  48. #include "srv0srv.h"
  49. #include "ibuf0ibuf.h"
  50. #include "fts0fts.h"
  51. #include "fts0types.h"
  52. #include "srv0start.h"
  53. #include "row0import.h"
  54. #include "m_string.h"
  55. #include "my_sys.h"
  56. #include "ha_prototypes.h"
  57. #include <algorithm>
  58. /** Provide optional 4.x backwards compatibility for 5.0 and above */
  59. UNIV_INTERN ibool row_rollback_on_timeout = FALSE;
  60. /** Chain node of the list of tables to drop in the background. */
  61. struct row_mysql_drop_t{
  62. char* table_name; /*!< table name */
  63. UT_LIST_NODE_T(row_mysql_drop_t)row_mysql_drop_list;
  64. /*!< list chain node */
  65. };
  66. #ifdef UNIV_PFS_MUTEX
  67. /* Key to register drop list mutex with performance schema */
  68. UNIV_INTERN mysql_pfs_key_t row_drop_list_mutex_key;
  69. #endif /* UNIV_PFS_MUTEX */
  70. /** @brief List of tables we should drop in background.
  71. ALTER TABLE in MySQL requires that the table handler can drop the
  72. table in background when there are no queries to it any
  73. more. Protected by row_drop_list_mutex. */
  74. static UT_LIST_BASE_NODE_T(row_mysql_drop_t) row_mysql_drop_list;
  75. /** Mutex protecting the background table drop list. */
  76. static ib_mutex_t row_drop_list_mutex;
  77. /** Flag: has row_mysql_drop_list been initialized? */
  78. static ibool row_mysql_drop_list_inited = FALSE;
  79. /** Magic table names for invoking various monitor threads */
  80. /* @{ */
  81. static const char S_innodb_monitor[] = "innodb_monitor";
  82. static const char S_innodb_lock_monitor[] = "innodb_lock_monitor";
  83. static const char S_innodb_tablespace_monitor[] = "innodb_tablespace_monitor";
  84. static const char S_innodb_table_monitor[] = "innodb_table_monitor";
  85. #ifdef UNIV_MEM_DEBUG
  86. static const char S_innodb_mem_validate[] = "innodb_mem_validate";
  87. #endif /* UNIV_MEM_DEBUG */
  88. /* @} */
  89. /** Evaluates to true if str1 equals str2_onstack, used for comparing
  90. the magic table names.
  91. @param str1 in: string to compare
  92. @param str1_len in: length of str1, in bytes, including terminating NUL
  93. @param str2_onstack in: char[] array containing a NUL terminated string
  94. @return TRUE if str1 equals str2_onstack */
  95. #define STR_EQ(str1, str1_len, str2_onstack) \
  96. ((str1_len) == sizeof(str2_onstack) \
  97. && memcmp(str1, str2_onstack, sizeof(str2_onstack)) == 0)
  98. /*******************************************************************//**
  99. Determine if the given name is a name reserved for MySQL system tables.
  100. @return TRUE if name is a MySQL system table name */
  101. static
  102. ibool
  103. row_mysql_is_system_table(
  104. /*======================*/
  105. const char* name)
  106. {
  107. if (strncmp(name, "mysql/", 6) != 0) {
  108. return(FALSE);
  109. }
  110. return(0 == strcmp(name + 6, "host")
  111. || 0 == strcmp(name + 6, "user")
  112. || 0 == strcmp(name + 6, "db"));
  113. }
  114. /*********************************************************************//**
  115. If a table is not yet in the drop list, adds the table to the list of tables
  116. which the master thread drops in background. We need this on Unix because in
  117. ALTER TABLE MySQL may call drop table even if the table has running queries on
  118. it. Also, if there are running foreign key checks on the table, we drop the
  119. table lazily.
  120. @return TRUE if the table was not yet in the drop list, and was added there */
  121. static
  122. ibool
  123. row_add_table_to_background_drop_list(
  124. /*==================================*/
  125. const char* name); /*!< in: table name */
  126. /*******************************************************************//**
  127. Delays an INSERT, DELETE or UPDATE operation if the purge is lagging. */
  128. static
  129. void
  130. row_mysql_delay_if_needed(void)
  131. /*===========================*/
  132. {
  133. if (srv_dml_needed_delay) {
  134. os_thread_sleep(srv_dml_needed_delay);
  135. }
  136. }
  137. /*******************************************************************//**
  138. Frees the blob heap in prebuilt when no longer needed. */
  139. UNIV_INTERN
  140. void
  141. row_mysql_prebuilt_free_blob_heap(
  142. /*==============================*/
  143. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct of a
  144. ha_innobase:: table handle */
  145. {
  146. mem_heap_free(prebuilt->blob_heap);
  147. prebuilt->blob_heap = NULL;
  148. }
  149. /*******************************************************************//**
  150. Stores a >= 5.0.3 format true VARCHAR length to dest, in the MySQL row
  151. format.
  152. @return pointer to the data, we skip the 1 or 2 bytes at the start
  153. that are used to store the len */
  154. UNIV_INTERN
  155. byte*
  156. row_mysql_store_true_var_len(
  157. /*=========================*/
  158. byte* dest, /*!< in: where to store */
  159. ulint len, /*!< in: length, must fit in two bytes */
  160. ulint lenlen) /*!< in: storage length of len: either 1 or 2 bytes */
  161. {
  162. if (lenlen == 2) {
  163. ut_a(len < 256 * 256);
  164. mach_write_to_2_little_endian(dest, len);
  165. return(dest + 2);
  166. }
  167. ut_a(lenlen == 1);
  168. ut_a(len < 256);
  169. mach_write_to_1(dest, len);
  170. return(dest + 1);
  171. }
  172. /*******************************************************************//**
  173. Reads a >= 5.0.3 format true VARCHAR length, in the MySQL row format, and
  174. returns a pointer to the data.
  175. @return pointer to the data, we skip the 1 or 2 bytes at the start
  176. that are used to store the len */
  177. UNIV_INTERN
  178. const byte*
  179. row_mysql_read_true_varchar(
  180. /*========================*/
  181. ulint* len, /*!< out: variable-length field length */
  182. const byte* field, /*!< in: field in the MySQL format */
  183. ulint lenlen) /*!< in: storage length of len: either 1
  184. or 2 bytes */
  185. {
  186. if (lenlen == 2) {
  187. *len = mach_read_from_2_little_endian(field);
  188. return(field + 2);
  189. }
  190. ut_a(lenlen == 1);
  191. *len = mach_read_from_1(field);
  192. return(field + 1);
  193. }
  194. /*******************************************************************//**
  195. Stores a reference to a BLOB in the MySQL format. */
  196. UNIV_INTERN
  197. void
  198. row_mysql_store_blob_ref(
  199. /*=====================*/
  200. byte* dest, /*!< in: where to store */
  201. ulint col_len,/*!< in: dest buffer size: determines into
  202. how many bytes the BLOB length is stored,
  203. the space for the length may vary from 1
  204. to 4 bytes */
  205. const void* data, /*!< in: BLOB data; if the value to store
  206. is SQL NULL this should be NULL pointer */
  207. ulint len) /*!< in: BLOB length; if the value to store
  208. is SQL NULL this should be 0; remember
  209. also to set the NULL bit in the MySQL record
  210. header! */
  211. {
  212. /* MySQL might assume the field is set to zero except the length and
  213. the pointer fields */
  214. memset(dest, '\0', col_len);
  215. /* In dest there are 1 - 4 bytes reserved for the BLOB length,
  216. and after that 8 bytes reserved for the pointer to the data.
  217. In 32-bit architectures we only use the first 4 bytes of the pointer
  218. slot. */
  219. ut_a(col_len - 8 > 1 || len < 256);
  220. ut_a(col_len - 8 > 2 || len < 256 * 256);
  221. ut_a(col_len - 8 > 3 || len < 256 * 256 * 256);
  222. mach_write_to_n_little_endian(dest, col_len - 8, len);
  223. memcpy(dest + col_len - 8, &data, sizeof data);
  224. }
  225. /*******************************************************************//**
  226. Reads a reference to a BLOB in the MySQL format.
  227. @return pointer to BLOB data */
  228. UNIV_INTERN
  229. const byte*
  230. row_mysql_read_blob_ref(
  231. /*====================*/
  232. ulint* len, /*!< out: BLOB length */
  233. const byte* ref, /*!< in: BLOB reference in the
  234. MySQL format */
  235. ulint col_len) /*!< in: BLOB reference length
  236. (not BLOB length) */
  237. {
  238. byte* data;
  239. *len = mach_read_from_n_little_endian(ref, col_len - 8);
  240. memcpy(&data, ref + col_len - 8, sizeof data);
  241. return(data);
  242. }
  243. /**************************************************************//**
  244. Pad a column with spaces. */
  245. UNIV_INTERN
  246. void
  247. row_mysql_pad_col(
  248. /*==============*/
  249. ulint mbminlen, /*!< in: minimum size of a character,
  250. in bytes */
  251. byte* pad, /*!< out: padded buffer */
  252. ulint len) /*!< in: number of bytes to pad */
  253. {
  254. const byte* pad_end;
  255. switch (UNIV_EXPECT(mbminlen, 1)) {
  256. default:
  257. ut_error;
  258. case 1:
  259. /* space=0x20 */
  260. memset(pad, 0x20, len);
  261. break;
  262. case 2:
  263. /* space=0x0020 */
  264. pad_end = pad + len;
  265. ut_a(!(len % 2));
  266. while (pad < pad_end) {
  267. *pad++ = 0x00;
  268. *pad++ = 0x20;
  269. };
  270. break;
  271. case 4:
  272. /* space=0x00000020 */
  273. pad_end = pad + len;
  274. ut_a(!(len % 4));
  275. while (pad < pad_end) {
  276. *pad++ = 0x00;
  277. *pad++ = 0x00;
  278. *pad++ = 0x00;
  279. *pad++ = 0x20;
  280. }
  281. break;
  282. }
  283. }
  284. /**************************************************************//**
  285. Stores a non-SQL-NULL field given in the MySQL format in the InnoDB format.
  286. The counterpart of this function is row_sel_field_store_in_mysql_format() in
  287. row0sel.cc.
  288. @return up to which byte we used buf in the conversion */
  289. UNIV_INTERN
  290. byte*
  291. row_mysql_store_col_in_innobase_format(
  292. /*===================================*/
  293. dfield_t* dfield, /*!< in/out: dfield where dtype
  294. information must be already set when
  295. this function is called! */
  296. byte* buf, /*!< in/out: buffer for a converted
  297. integer value; this must be at least
  298. col_len long then! NOTE that dfield
  299. may also get a pointer to 'buf',
  300. therefore do not discard this as long
  301. as dfield is used! */
  302. ibool row_format_col, /*!< TRUE if the mysql_data is from
  303. a MySQL row, FALSE if from a MySQL
  304. key value;
  305. in MySQL, a true VARCHAR storage
  306. format differs in a row and in a
  307. key value: in a key value the length
  308. is always stored in 2 bytes! */
  309. const byte* mysql_data, /*!< in: MySQL column value, not
  310. SQL NULL; NOTE that dfield may also
  311. get a pointer to mysql_data,
  312. therefore do not discard this as long
  313. as dfield is used! */
  314. ulint col_len, /*!< in: MySQL column length; NOTE that
  315. this is the storage length of the
  316. column in the MySQL format row, not
  317. necessarily the length of the actual
  318. payload data; if the column is a true
  319. VARCHAR then this is irrelevant */
  320. ulint comp) /*!< in: nonzero=compact format */
  321. {
  322. const byte* ptr = mysql_data;
  323. const dtype_t* dtype;
  324. ulint type;
  325. ulint lenlen;
  326. dtype = dfield_get_type(dfield);
  327. type = dtype->mtype;
  328. if (type == DATA_INT) {
  329. /* Store integer data in Innobase in a big-endian format,
  330. sign bit negated if the data is a signed integer. In MySQL,
  331. integers are stored in a little-endian format. */
  332. byte* p = buf + col_len;
  333. for (;;) {
  334. p--;
  335. *p = *mysql_data;
  336. if (p == buf) {
  337. break;
  338. }
  339. mysql_data++;
  340. }
  341. if (!(dtype->prtype & DATA_UNSIGNED)) {
  342. *buf ^= 128;
  343. }
  344. ptr = buf;
  345. buf += col_len;
  346. } else if ((type == DATA_VARCHAR
  347. || type == DATA_VARMYSQL
  348. || type == DATA_BINARY)) {
  349. if (dtype_get_mysql_type(dtype) == DATA_MYSQL_TRUE_VARCHAR) {
  350. /* The length of the actual data is stored to 1 or 2
  351. bytes at the start of the field */
  352. if (row_format_col) {
  353. if (dtype->prtype & DATA_LONG_TRUE_VARCHAR) {
  354. lenlen = 2;
  355. } else {
  356. lenlen = 1;
  357. }
  358. } else {
  359. /* In a MySQL key value, lenlen is always 2 */
  360. lenlen = 2;
  361. }
  362. ptr = row_mysql_read_true_varchar(&col_len, mysql_data,
  363. lenlen);
  364. } else {
  365. /* Remove trailing spaces from old style VARCHAR
  366. columns. */
  367. /* Handle Unicode strings differently. */
  368. ulint mbminlen = dtype_get_mbminlen(dtype);
  369. ptr = mysql_data;
  370. switch (mbminlen) {
  371. default:
  372. ut_error;
  373. case 4:
  374. /* space=0x00000020 */
  375. /* Trim "half-chars", just in case. */
  376. col_len &= ~3;
  377. while (col_len >= 4
  378. && ptr[col_len - 4] == 0x00
  379. && ptr[col_len - 3] == 0x00
  380. && ptr[col_len - 2] == 0x00
  381. && ptr[col_len - 1] == 0x20) {
  382. col_len -= 4;
  383. }
  384. break;
  385. case 2:
  386. /* space=0x0020 */
  387. /* Trim "half-chars", just in case. */
  388. col_len &= ~1;
  389. while (col_len >= 2 && ptr[col_len - 2] == 0x00
  390. && ptr[col_len - 1] == 0x20) {
  391. col_len -= 2;
  392. }
  393. break;
  394. case 1:
  395. /* space=0x20 */
  396. while (col_len > 0
  397. && ptr[col_len - 1] == 0x20) {
  398. col_len--;
  399. }
  400. }
  401. }
  402. } else if (comp && type == DATA_MYSQL
  403. && dtype_get_mbminlen(dtype) == 1
  404. && dtype_get_mbmaxlen(dtype) > 1) {
  405. /* In some cases we strip trailing spaces from UTF-8 and other
  406. multibyte charsets, from FIXED-length CHAR columns, to save
  407. space. UTF-8 would otherwise normally use 3 * the string length
  408. bytes to store an ASCII string! */
  409. /* We assume that this CHAR field is encoded in a
  410. variable-length character set where spaces have
  411. 1:1 correspondence to 0x20 bytes, such as UTF-8.
  412. Consider a CHAR(n) field, a field of n characters.
  413. It will contain between n * mbminlen and n * mbmaxlen bytes.
  414. We will try to truncate it to n bytes by stripping
  415. space padding. If the field contains single-byte
  416. characters only, it will be truncated to n characters.
  417. Consider a CHAR(5) field containing the string ".a "
  418. where "." denotes a 3-byte character represented by
  419. the bytes "$%&". After our stripping, the string will
  420. be stored as "$%&a " (5 bytes). The string ".abc "
  421. will be stored as "$%&abc" (6 bytes).
  422. The space padding will be restored in row0sel.cc, function
  423. row_sel_field_store_in_mysql_format(). */
  424. ulint n_chars;
  425. ut_a(!(dtype_get_len(dtype) % dtype_get_mbmaxlen(dtype)));
  426. n_chars = dtype_get_len(dtype) / dtype_get_mbmaxlen(dtype);
  427. /* Strip space padding. */
  428. while (col_len > n_chars && ptr[col_len - 1] == 0x20) {
  429. col_len--;
  430. }
  431. } else if (type == DATA_BLOB && row_format_col) {
  432. ptr = row_mysql_read_blob_ref(&col_len, mysql_data, col_len);
  433. }
  434. dfield_set_data(dfield, ptr, col_len);
  435. return(buf);
  436. }
  437. /**************************************************************//**
  438. Convert a row in the MySQL format to a row in the Innobase format. Note that
  439. the function to convert a MySQL format key value to an InnoDB dtuple is
  440. row_sel_convert_mysql_key_to_innobase() in row0sel.cc. */
  441. static
  442. void
  443. row_mysql_convert_row_to_innobase(
  444. /*==============================*/
  445. dtuple_t* row, /*!< in/out: Innobase row where the
  446. field type information is already
  447. copied there! */
  448. row_prebuilt_t* prebuilt, /*!< in: prebuilt struct where template
  449. must be of type ROW_MYSQL_WHOLE_ROW */
  450. byte* mysql_rec) /*!< in: row in the MySQL format;
  451. NOTE: do not discard as long as
  452. row is used, as row may contain
  453. pointers to this record! */
  454. {
  455. const mysql_row_templ_t*templ;
  456. dfield_t* dfield;
  457. ulint i;
  458. ut_ad(prebuilt->template_type == ROW_MYSQL_WHOLE_ROW);
  459. ut_ad(prebuilt->mysql_template);
  460. for (i = 0; i < prebuilt->n_template; i++) {
  461. templ = prebuilt->mysql_template + i;
  462. dfield = dtuple_get_nth_field(row, i);
  463. if (templ->mysql_null_bit_mask != 0) {
  464. /* Column may be SQL NULL */
  465. if (mysql_rec[templ->mysql_null_byte_offset]
  466. & (byte) (templ->mysql_null_bit_mask)) {
  467. /* It is SQL NULL */
  468. dfield_set_null(dfield);
  469. goto next_column;
  470. }
  471. }
  472. row_mysql_store_col_in_innobase_format(
  473. dfield,
  474. prebuilt->ins_upd_rec_buff + templ->mysql_col_offset,
  475. TRUE, /* MySQL row format data */
  476. mysql_rec + templ->mysql_col_offset,
  477. templ->mysql_col_len,
  478. dict_table_is_comp(prebuilt->table));
  479. next_column:
  480. ;
  481. }
  482. /* If there is a FTS doc id column and it is not user supplied (
  483. generated by server) then assign it a new doc id. */
  484. if (prebuilt->table->fts) {
  485. ut_a(prebuilt->table->fts->doc_col != ULINT_UNDEFINED);
  486. fts_create_doc_id(prebuilt->table, row, prebuilt->heap);
  487. }
  488. }
  489. /****************************************************************//**
  490. Handles user errors and lock waits detected by the database engine.
  491. @return true if it was a lock wait and we should continue running the
  492. query thread and in that case the thr is ALREADY in the running state. */
  493. UNIV_INTERN
  494. bool
  495. row_mysql_handle_errors(
  496. /*====================*/
  497. dberr_t* new_err,/*!< out: possible new error encountered in
  498. lock wait, or if no new error, the value
  499. of trx->error_state at the entry of this
  500. function */
  501. trx_t* trx, /*!< in: transaction */
  502. que_thr_t* thr, /*!< in: query thread, or NULL */
  503. trx_savept_t* savept) /*!< in: savepoint, or NULL */
  504. {
  505. dberr_t err;
  506. handle_new_error:
  507. err = trx->error_state;
  508. ut_a(err != DB_SUCCESS);
  509. trx->error_state = DB_SUCCESS;
  510. switch (err) {
  511. case DB_LOCK_WAIT_TIMEOUT:
  512. if (row_rollback_on_timeout) {
  513. trx_rollback_to_savepoint(trx, NULL);
  514. break;
  515. }
  516. /* fall through */
  517. case DB_DUPLICATE_KEY:
  518. case DB_FOREIGN_DUPLICATE_KEY:
  519. case DB_TOO_BIG_RECORD:
  520. case DB_TOO_BIG_FOR_REDO:
  521. case DB_UNDO_RECORD_TOO_BIG:
  522. case DB_ROW_IS_REFERENCED:
  523. case DB_NO_REFERENCED_ROW:
  524. case DB_CANNOT_ADD_CONSTRAINT:
  525. case DB_TOO_MANY_CONCURRENT_TRXS:
  526. case DB_OUT_OF_FILE_SPACE:
  527. case DB_READ_ONLY:
  528. case DB_FTS_INVALID_DOCID:
  529. case DB_INTERRUPTED:
  530. case DB_DICT_CHANGED:
  531. if (savept) {
  532. /* Roll back the latest, possibly incomplete insertion
  533. or update */
  534. trx_rollback_to_savepoint(trx, savept);
  535. }
  536. /* MySQL will roll back the latest SQL statement */
  537. break;
  538. case DB_LOCK_WAIT:
  539. lock_wait_suspend_thread(thr);
  540. if (trx->error_state != DB_SUCCESS) {
  541. que_thr_stop_for_mysql(thr);
  542. goto handle_new_error;
  543. }
  544. *new_err = err;
  545. return(true);
  546. case DB_DEADLOCK:
  547. case DB_LOCK_TABLE_FULL:
  548. /* Roll back the whole transaction; this resolution was added
  549. to version 3.23.43 */
  550. trx_rollback_to_savepoint(trx, NULL);
  551. break;
  552. case DB_MUST_GET_MORE_FILE_SPACE:
  553. fputs("InnoDB: The database cannot continue"
  554. " operation because of\n"
  555. "InnoDB: lack of space. You must add"
  556. " a new data file to\n"
  557. "InnoDB: my.cnf and restart the database.\n", stderr);
  558. ut_ad(0);
  559. exit(1);
  560. case DB_CORRUPTION:
  561. fputs("InnoDB: We detected index corruption"
  562. " in an InnoDB type table.\n"
  563. "InnoDB: You have to dump + drop + reimport"
  564. " the table or, in\n"
  565. "InnoDB: a case of widespread corruption,"
  566. " dump all InnoDB\n"
  567. "InnoDB: tables and recreate the"
  568. " whole InnoDB tablespace.\n"
  569. "InnoDB: If the mysqld server crashes"
  570. " after the startup or when\n"
  571. "InnoDB: you dump the tables, look at\n"
  572. "InnoDB: " REFMAN "forcing-innodb-recovery.html"
  573. " for help.\n", stderr);
  574. break;
  575. case DB_FOREIGN_EXCEED_MAX_CASCADE:
  576. fprintf(stderr, "InnoDB: Cannot delete/update rows with"
  577. " cascading foreign key constraints that exceed max"
  578. " depth of %lu\n"
  579. "Please drop excessive foreign constraints"
  580. " and try again\n", (ulong) DICT_FK_MAX_RECURSIVE_LOAD);
  581. break;
  582. default:
  583. fprintf(stderr, "InnoDB: unknown error code %lu\n",
  584. (ulong) err);
  585. ut_error;
  586. }
  587. if (trx->error_state != DB_SUCCESS) {
  588. *new_err = trx->error_state;
  589. } else {
  590. *new_err = err;
  591. }
  592. trx->error_state = DB_SUCCESS;
  593. return(false);
  594. }
  595. /********************************************************************//**
  596. Create a prebuilt struct for a MySQL table handle.
  597. @return own: a prebuilt struct */
  598. UNIV_INTERN
  599. row_prebuilt_t*
  600. row_create_prebuilt(
  601. /*================*/
  602. dict_table_t* table, /*!< in: Innobase table handle */
  603. ulint mysql_row_len) /*!< in: length in bytes of a row in
  604. the MySQL format */
  605. {
  606. row_prebuilt_t* prebuilt;
  607. mem_heap_t* heap;
  608. dict_index_t* clust_index;
  609. dict_index_t* temp_index;
  610. dtuple_t* ref;
  611. ulint ref_len;
  612. uint srch_key_len = 0;
  613. ulint search_tuple_n_fields;
  614. search_tuple_n_fields = 2 * dict_table_get_n_cols(table);
  615. clust_index = dict_table_get_first_index(table);
  616. /* Make sure that search_tuple is long enough for clustered index */
  617. ut_a(2 * dict_table_get_n_cols(table) >= clust_index->n_fields);
  618. ref_len = dict_index_get_n_unique(clust_index);
  619. /* Maximum size of the buffer needed for conversion of INTs from
  620. little endian format to big endian format in an index. An index
  621. can have maximum 16 columns (MAX_REF_PARTS) in it. Therfore
  622. Max size for PK: 16 * 8 bytes (BIGINT's size) = 128 bytes
  623. Max size Secondary index: 16 * 8 bytes + PK = 256 bytes. */
  624. #define MAX_SRCH_KEY_VAL_BUFFER 2* (8 * MAX_REF_PARTS)
  625. #define PREBUILT_HEAP_INITIAL_SIZE \
  626. ( \
  627. sizeof(*prebuilt) \
  628. /* allocd in this function */ \
  629. + DTUPLE_EST_ALLOC(search_tuple_n_fields) \
  630. + DTUPLE_EST_ALLOC(ref_len) \
  631. /* allocd in row_prebuild_sel_graph() */ \
  632. + sizeof(sel_node_t) \
  633. + sizeof(que_fork_t) \
  634. + sizeof(que_thr_t) \
  635. /* allocd in row_get_prebuilt_update_vector() */ \
  636. + sizeof(upd_node_t) \
  637. + sizeof(upd_t) \
  638. + sizeof(upd_field_t) \
  639. * dict_table_get_n_cols(table) \
  640. + sizeof(que_fork_t) \
  641. + sizeof(que_thr_t) \
  642. /* allocd in row_get_prebuilt_insert_row() */ \
  643. + sizeof(ins_node_t) \
  644. /* mysql_row_len could be huge and we are not \
  645. sure if this prebuilt instance is going to be \
  646. used in inserts */ \
  647. + (mysql_row_len < 256 ? mysql_row_len : 0) \
  648. + DTUPLE_EST_ALLOC(dict_table_get_n_cols(table)) \
  649. + sizeof(que_fork_t) \
  650. + sizeof(que_thr_t) \
  651. )
  652. /* Calculate size of key buffer used to store search key in
  653. InnoDB format. MySQL stores INTs in little endian format and
  654. InnoDB stores INTs in big endian format with the sign bit
  655. flipped. All other field types are stored/compared the same
  656. in MySQL and InnoDB, so we must create a buffer containing
  657. the INT key parts in InnoDB format.We need two such buffers
  658. since both start and end keys are used in records_in_range(). */
  659. for (temp_index = dict_table_get_first_index(table); temp_index;
  660. temp_index = dict_table_get_next_index(temp_index)) {
  661. DBUG_EXECUTE_IF("innodb_srch_key_buffer_max_value",
  662. ut_a(temp_index->n_user_defined_cols
  663. == MAX_REF_PARTS););
  664. uint temp_len = 0;
  665. for (uint i = 0; i < temp_index->n_uniq; i++) {
  666. if (temp_index->fields[i].col->mtype == DATA_INT) {
  667. temp_len +=
  668. temp_index->fields[i].fixed_len;
  669. }
  670. }
  671. srch_key_len = max(srch_key_len,temp_len);
  672. }
  673. ut_a(srch_key_len <= MAX_SRCH_KEY_VAL_BUFFER);
  674. DBUG_EXECUTE_IF("innodb_srch_key_buffer_max_value",
  675. ut_a(srch_key_len == MAX_SRCH_KEY_VAL_BUFFER););
  676. /* We allocate enough space for the objects that are likely to
  677. be created later in order to minimize the number of malloc()
  678. calls */
  679. heap = mem_heap_create(PREBUILT_HEAP_INITIAL_SIZE + 2 * srch_key_len);
  680. prebuilt = static_cast<row_prebuilt_t*>(
  681. mem_heap_zalloc(heap, sizeof(*prebuilt)));
  682. prebuilt->magic_n = ROW_PREBUILT_ALLOCATED;
  683. prebuilt->magic_n2 = ROW_PREBUILT_ALLOCATED;
  684. prebuilt->table = table;
  685. prebuilt->sql_stat_start = TRUE;
  686. prebuilt->heap = heap;
  687. prebuilt->srch_key_val_len = srch_key_len;
  688. if (prebuilt->srch_key_val_len) {
  689. prebuilt->srch_key_val1 = static_cast<byte*>(
  690. mem_heap_alloc(prebuilt->heap,
  691. 2 * prebuilt->srch_key_val_len));
  692. prebuilt->srch_key_val2 = prebuilt->srch_key_val1 +
  693. prebuilt->srch_key_val_len;
  694. } else {
  695. prebuilt->srch_key_val1 = NULL;
  696. prebuilt->srch_key_val2 = NULL;
  697. }
  698. btr_pcur_reset(&prebuilt->pcur);
  699. btr_pcur_reset(&prebuilt->clust_pcur);
  700. prebuilt->select_lock_type = LOCK_NONE;
  701. prebuilt->stored_select_lock_type = LOCK_NONE_UNSET;
  702. prebuilt->search_tuple = dtuple_create(heap, search_tuple_n_fields);
  703. ref = dtuple_create(heap, ref_len);
  704. dict_index_copy_types(ref, clust_index, ref_len);
  705. prebuilt->clust_ref = ref;
  706. prebuilt->autoinc_error = DB_SUCCESS;
  707. prebuilt->autoinc_offset = 0;
  708. /* Default to 1, we will set the actual value later in
  709. ha_innobase::get_auto_increment(). */
  710. prebuilt->autoinc_increment = 1;
  711. prebuilt->autoinc_last_value = 0;
  712. /* During UPDATE and DELETE we need the doc id. */
  713. prebuilt->fts_doc_id = 0;
  714. prebuilt->mysql_row_len = mysql_row_len;
  715. return(prebuilt);
  716. }
  717. /********************************************************************//**
  718. Free a prebuilt struct for a MySQL table handle. */
  719. UNIV_INTERN
  720. void
  721. row_prebuilt_free(
  722. /*==============*/
  723. row_prebuilt_t* prebuilt, /*!< in, own: prebuilt struct */
  724. ibool dict_locked) /*!< in: TRUE=data dictionary locked */
  725. {
  726. ulint i;
  727. if (UNIV_UNLIKELY
  728. (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED
  729. || prebuilt->magic_n2 != ROW_PREBUILT_ALLOCATED)) {
  730. fprintf(stderr,
  731. "InnoDB: Error: trying to free a corrupt\n"
  732. "InnoDB: table handle. Magic n %lu,"
  733. " magic n2 %lu, table name ",
  734. (ulong) prebuilt->magic_n,
  735. (ulong) prebuilt->magic_n2);
  736. ut_print_name(stderr, NULL, TRUE, prebuilt->table->name);
  737. putc('\n', stderr);
  738. mem_analyze_corruption(prebuilt);
  739. ut_error;
  740. }
  741. prebuilt->magic_n = ROW_PREBUILT_FREED;
  742. prebuilt->magic_n2 = ROW_PREBUILT_FREED;
  743. btr_pcur_reset(&prebuilt->pcur);
  744. btr_pcur_reset(&prebuilt->clust_pcur);
  745. if (prebuilt->mysql_template) {
  746. mem_free(prebuilt->mysql_template);
  747. }
  748. if (prebuilt->ins_graph) {
  749. que_graph_free_recursive(prebuilt->ins_graph);
  750. }
  751. if (prebuilt->sel_graph) {
  752. que_graph_free_recursive(prebuilt->sel_graph);
  753. }
  754. if (prebuilt->upd_graph) {
  755. que_graph_free_recursive(prebuilt->upd_graph);
  756. }
  757. if (prebuilt->blob_heap) {
  758. mem_heap_free(prebuilt->blob_heap);
  759. }
  760. if (prebuilt->old_vers_heap) {
  761. mem_heap_free(prebuilt->old_vers_heap);
  762. }
  763. if (prebuilt->fetch_cache[0] != NULL) {
  764. byte* base = prebuilt->fetch_cache[0] - 4;
  765. byte* ptr = base;
  766. for (i = 0; i < MYSQL_FETCH_CACHE_SIZE; i++) {
  767. byte* row;
  768. ulint magic1;
  769. ulint magic2;
  770. magic1 = mach_read_from_4(ptr);
  771. ptr += 4;
  772. row = ptr;
  773. ptr += prebuilt->mysql_row_len;
  774. magic2 = mach_read_from_4(ptr);
  775. ptr += 4;
  776. if (ROW_PREBUILT_FETCH_MAGIC_N != magic1
  777. || row != prebuilt->fetch_cache[i]
  778. || ROW_PREBUILT_FETCH_MAGIC_N != magic2) {
  779. fputs("InnoDB: Error: trying to free"
  780. " a corrupt fetch buffer.\n", stderr);
  781. mem_analyze_corruption(base);
  782. ut_error;
  783. }
  784. }
  785. mem_free(base);
  786. }
  787. dict_table_close(prebuilt->table, dict_locked, TRUE);
  788. mem_heap_free(prebuilt->heap);
  789. }
  790. /*********************************************************************//**
  791. Updates the transaction pointers in query graphs stored in the prebuilt
  792. struct. */
  793. UNIV_INTERN
  794. void
  795. row_update_prebuilt_trx(
  796. /*====================*/
  797. row_prebuilt_t* prebuilt, /*!< in/out: prebuilt struct
  798. in MySQL handle */
  799. trx_t* trx) /*!< in: transaction handle */
  800. {
  801. if (trx->magic_n != TRX_MAGIC_N) {
  802. fprintf(stderr,
  803. "InnoDB: Error: trying to use a corrupt\n"
  804. "InnoDB: trx handle. Magic n %lu\n",
  805. (ulong) trx->magic_n);
  806. mem_analyze_corruption(trx);
  807. ut_error;
  808. }
  809. if (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED) {
  810. fprintf(stderr,
  811. "InnoDB: Error: trying to use a corrupt\n"
  812. "InnoDB: table handle. Magic n %lu, table name ",
  813. (ulong) prebuilt->magic_n);
  814. ut_print_name(stderr, trx, TRUE, prebuilt->table->name);
  815. putc('\n', stderr);
  816. mem_analyze_corruption(prebuilt);
  817. ut_error;
  818. }
  819. prebuilt->trx = trx;
  820. if (prebuilt->ins_graph) {
  821. prebuilt->ins_graph->trx = trx;
  822. }
  823. if (prebuilt->upd_graph) {
  824. prebuilt->upd_graph->trx = trx;
  825. }
  826. if (prebuilt->sel_graph) {
  827. prebuilt->sel_graph->trx = trx;
  828. }
  829. }
  830. /*********************************************************************//**
  831. Gets pointer to a prebuilt dtuple used in insertions. If the insert graph
  832. has not yet been built in the prebuilt struct, then this function first
  833. builds it.
  834. @return prebuilt dtuple; the column type information is also set in it */
  835. static
  836. dtuple_t*
  837. row_get_prebuilt_insert_row(
  838. /*========================*/
  839. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  840. handle */
  841. {
  842. dict_table_t* table = prebuilt->table;
  843. ut_ad(prebuilt && table && prebuilt->trx);
  844. if (prebuilt->ins_node != 0) {
  845. /* Check if indexes have been dropped or added and we
  846. may need to rebuild the row insert template. */
  847. if (prebuilt->trx_id == table->def_trx_id
  848. && UT_LIST_GET_LEN(prebuilt->ins_node->entry_list)
  849. == UT_LIST_GET_LEN(table->indexes)) {
  850. return(prebuilt->ins_node->row);
  851. }
  852. ut_ad(prebuilt->trx_id < table->def_trx_id);
  853. que_graph_free_recursive(prebuilt->ins_graph);
  854. prebuilt->ins_graph = 0;
  855. }
  856. /* Create an insert node and query graph to the prebuilt struct */
  857. ins_node_t* node;
  858. node = ins_node_create(INS_DIRECT, table, prebuilt->heap);
  859. prebuilt->ins_node = node;
  860. if (prebuilt->ins_upd_rec_buff == 0) {
  861. prebuilt->ins_upd_rec_buff = static_cast<byte*>(
  862. mem_heap_alloc(
  863. prebuilt->heap,
  864. prebuilt->mysql_row_len));
  865. }
  866. dtuple_t* row;
  867. row = dtuple_create(prebuilt->heap, dict_table_get_n_cols(table));
  868. dict_table_copy_types(row, table);
  869. ins_node_set_new_row(node, row);
  870. prebuilt->ins_graph = static_cast<que_fork_t*>(
  871. que_node_get_parent(
  872. pars_complete_graph_for_exec(
  873. node,
  874. prebuilt->trx, prebuilt->heap)));
  875. prebuilt->ins_graph->state = QUE_FORK_ACTIVE;
  876. prebuilt->trx_id = table->def_trx_id;
  877. return(prebuilt->ins_node->row);
  878. }
  879. /*********************************************************************//**
  880. Updates the table modification counter and calculates new estimates
  881. for table and index statistics if necessary. */
  882. UNIV_INLINE
  883. void
  884. row_update_statistics_if_needed(
  885. /*============================*/
  886. dict_table_t* table) /*!< in: table */
  887. {
  888. ib_uint64_t counter;
  889. ib_uint64_t n_rows;
  890. if (!table->stat_initialized) {
  891. DBUG_EXECUTE_IF(
  892. "test_upd_stats_if_needed_not_inited",
  893. fprintf(stderr, "test_upd_stats_if_needed_not_inited "
  894. "was executed\n");
  895. );
  896. return;
  897. }
  898. counter = table->stat_modified_counter++;
  899. n_rows = dict_table_get_n_rows(table);
  900. if (dict_stats_is_persistent_enabled(table)) {
  901. if (counter > n_rows / 10 /* 10% */
  902. && dict_stats_auto_recalc_is_enabled(table)) {
  903. dict_stats_recalc_pool_add(table);
  904. table->stat_modified_counter = 0;
  905. }
  906. return;
  907. }
  908. /* Calculate new statistics if 1 / 16 of table has been modified
  909. since the last time a statistics batch was run.
  910. We calculate statistics at most every 16th round, since we may have
  911. a counter table which is very small and updated very often. */
  912. if (counter > 16 + n_rows / 16 /* 6.25% */) {
  913. ut_ad(!mutex_own(&dict_sys->mutex));
  914. /* this will reset table->stat_modified_counter to 0 */
  915. dict_stats_update(table, DICT_STATS_RECALC_TRANSIENT);
  916. }
  917. }
  918. /*********************************************************************//**
  919. Sets an AUTO_INC type lock on the table mentioned in prebuilt. The
  920. AUTO_INC lock gives exclusive access to the auto-inc counter of the
  921. table. The lock is reserved only for the duration of an SQL statement.
  922. It is not compatible with another AUTO_INC or exclusive lock on the
  923. table.
  924. @return error code or DB_SUCCESS */
  925. UNIV_INTERN
  926. dberr_t
  927. row_lock_table_autoinc_for_mysql(
  928. /*=============================*/
  929. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in the MySQL
  930. table handle */
  931. {
  932. trx_t* trx = prebuilt->trx;
  933. ins_node_t* node = prebuilt->ins_node;
  934. const dict_table_t* table = prebuilt->table;
  935. que_thr_t* thr;
  936. dberr_t err;
  937. ibool was_lock_wait;
  938. ut_ad(trx);
  939. /* If we already hold an AUTOINC lock on the table then do nothing.
  940. Note: We peek at the value of the current owner without acquiring
  941. the lock mutex. **/
  942. if (trx == table->autoinc_trx) {
  943. return(DB_SUCCESS);
  944. }
  945. trx->op_info = "setting auto-inc lock";
  946. row_get_prebuilt_insert_row(prebuilt);
  947. node = prebuilt->ins_node;
  948. /* We use the insert query graph as the dummy graph needed
  949. in the lock module call */
  950. thr = que_fork_get_first_thr(prebuilt->ins_graph);
  951. que_thr_move_to_run_state_for_mysql(thr, trx);
  952. run_again:
  953. thr->run_node = node;
  954. thr->prev_node = node;
  955. /* It may be that the current session has not yet started
  956. its transaction, or it has been committed: */
  957. trx_start_if_not_started_xa(trx);
  958. err = lock_table(0, prebuilt->table, LOCK_AUTO_INC, thr);
  959. trx->error_state = err;
  960. if (err != DB_SUCCESS) {
  961. que_thr_stop_for_mysql(thr);
  962. was_lock_wait = row_mysql_handle_errors(&err, trx, thr, NULL);
  963. if (was_lock_wait) {
  964. goto run_again;
  965. }
  966. trx->op_info = "";
  967. return(err);
  968. }
  969. que_thr_stop_for_mysql_no_error(thr, trx);
  970. trx->op_info = "";
  971. return(err);
  972. }
  973. /*********************************************************************//**
  974. Sets a table lock on the table mentioned in prebuilt.
  975. @return error code or DB_SUCCESS */
  976. UNIV_INTERN
  977. dberr_t
  978. row_lock_table_for_mysql(
  979. /*=====================*/
  980. row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in the MySQL
  981. table handle */
  982. dict_table_t* table, /*!< in: table to lock, or NULL
  983. if prebuilt->table should be
  984. locked as
  985. prebuilt->select_lock_type */
  986. ulint mode) /*!< in: lock mode of table
  987. (ignored if table==NULL) */
  988. {
  989. trx_t* trx = prebuilt->trx;
  990. que_thr_t* thr;
  991. dberr_t err;
  992. ibool was_lock_wait;
  993. ut_ad(trx);
  994. trx->op_info = "setting table lock";
  995. if (prebuilt->sel_graph == NULL) {
  996. /* Build a dummy select query graph */
  997. row_prebuild_sel_graph(prebuilt);
  998. }
  999. /* We use the select query graph as the dummy graph needed
  1000. in the lock module call */
  1001. thr = que_fork_get_first_thr(prebuilt->sel_graph);
  1002. que_thr_move_to_run_state_for_mysql(thr, trx);
  1003. run_again:
  1004. thr->run_node = thr;
  1005. thr->prev_node = thr->common.parent;
  1006. /* It may be that the current session has not yet started
  1007. its transaction, or it has been committed: */
  1008. trx_start_if_not_started_xa(trx);
  1009. if (table) {
  1010. err = lock_table(
  1011. 0, table,
  1012. static_cast<enum lock_mode>(mode), thr);
  1013. } else {
  1014. err = lock_table(
  1015. 0, prebuilt->table,
  1016. static_cast<enum lock_mode>(
  1017. prebuilt->select_lock_type),
  1018. thr);
  1019. }
  1020. trx->error_state = err;
  1021. if (err != DB_SUCCESS) {
  1022. que_thr_stop_for_mysql(thr);
  1023. was_lock_wait = row_mysql_handle_errors(&err, trx, thr, NULL);
  1024. if (was_lock_wait) {
  1025. goto run_again;
  1026. }
  1027. trx->op_info = "";
  1028. return(err);
  1029. }
  1030. que_thr_stop_for_mysql_no_error(thr, trx);
  1031. trx->op_info = "";
  1032. return(err);
  1033. }
  1034. /*********************************************************************//**
  1035. Does an insert for MySQL.
  1036. @return error code or DB_SUCCESS */
  1037. UNIV_INTERN
  1038. dberr_t
  1039. row_insert_for_mysql(
  1040. /*=================*/
  1041. byte* mysql_rec, /*!< in: row in the MySQL format */
  1042. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  1043. handle */
  1044. {
  1045. trx_savept_t savept;
  1046. que_thr_t* thr;
  1047. dberr_t err;
  1048. ibool was_lock_wait;
  1049. trx_t* trx = prebuilt->trx;
  1050. ins_node_t* node = prebuilt->ins_node;
  1051. dict_table_t* table = prebuilt->table;
  1052. ut_ad(trx);
  1053. if (dict_table_is_discarded(prebuilt->table)) {
  1054. ib_logf(IB_LOG_LEVEL_ERROR,
  1055. "The table %s doesn't have a corresponding "
  1056. "tablespace, it was discarded.",
  1057. prebuilt->table->name);
  1058. return(DB_TABLESPACE_DELETED);
  1059. } else if (prebuilt->table->ibd_file_missing) {
  1060. ib_logf(IB_LOG_LEVEL_ERROR,
  1061. ".ibd file is missing for table %s",
  1062. prebuilt->table->name);
  1063. return(DB_TABLESPACE_NOT_FOUND);
  1064. } else if (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED) {
  1065. fprintf(stderr,
  1066. "InnoDB: Error: trying to free a corrupt\n"
  1067. "InnoDB: table handle. Magic n %lu, table name ",
  1068. (ulong) prebuilt->magic_n);
  1069. ut_print_name(stderr, trx, TRUE, prebuilt->table->name);
  1070. putc('\n', stderr);
  1071. mem_analyze_corruption(prebuilt);
  1072. ut_error;
  1073. } else if (srv_force_recovery) {
  1074. fputs("InnoDB: innodb_force_recovery is on: we do not allow\n"
  1075. "InnoDB: database modifications by the user. Shut down\n"
  1076. "InnoDB: mysqld and edit my.cnf so that"
  1077. "InnoDB: innodb_force_... is removed.\n",
  1078. stderr);
  1079. return(DB_READ_ONLY);
  1080. }
  1081. trx->op_info = "inserting";
  1082. row_mysql_delay_if_needed();
  1083. trx_start_if_not_started_xa(trx);
  1084. row_get_prebuilt_insert_row(prebuilt);
  1085. node = prebuilt->ins_node;
  1086. row_mysql_convert_row_to_innobase(node->row, prebuilt, mysql_rec);
  1087. savept = trx_savept_take(trx);
  1088. thr = que_fork_get_first_thr(prebuilt->ins_graph);
  1089. if (prebuilt->sql_stat_start) {
  1090. node->state = INS_NODE_SET_IX_LOCK;
  1091. prebuilt->sql_stat_start = FALSE;
  1092. } else {
  1093. node->state = INS_NODE_ALLOC_ROW_ID;
  1094. }
  1095. que_thr_move_to_run_state_for_mysql(thr, trx);
  1096. run_again:
  1097. thr->run_node = node;
  1098. thr->prev_node = node;
  1099. row_ins_step(thr);
  1100. DEBUG_SYNC_C("ib_after_row_insert_step");
  1101. err = trx->error_state;
  1102. if (err != DB_SUCCESS) {
  1103. error_exit:
  1104. que_thr_stop_for_mysql(thr);
  1105. /* FIXME: What's this ? */
  1106. thr->lock_state = QUE_THR_LOCK_ROW;
  1107. was_lock_wait = row_mysql_handle_errors(
  1108. &err, trx, thr, &savept);
  1109. thr->lock_state = QUE_THR_LOCK_NOLOCK;
  1110. if (was_lock_wait) {
  1111. ut_ad(node->state == INS_NODE_INSERT_ENTRIES
  1112. || node->state == INS_NODE_ALLOC_ROW_ID);
  1113. goto run_again;
  1114. }
  1115. trx->op_info = "";
  1116. return(err);
  1117. }
  1118. if (dict_table_has_fts_index(table)) {
  1119. doc_id_t doc_id;
  1120. /* Extract the doc id from the hidden FTS column */
  1121. doc_id = fts_get_doc_id_from_row(table, node->row);
  1122. if (doc_id <= 0) {
  1123. fprintf(stderr,
  1124. "InnoDB: FTS Doc ID must be large than 0 \n");
  1125. err = DB_FTS_INVALID_DOCID;
  1126. trx->error_state = DB_FTS_INVALID_DOCID;
  1127. goto error_exit;
  1128. }
  1129. if (!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) {
  1130. doc_id_t next_doc_id
  1131. = table->fts->cache->next_doc_id;
  1132. if (doc_id < next_doc_id) {
  1133. fprintf(stderr,
  1134. "InnoDB: FTS Doc ID must be large than"
  1135. " " UINT64PF " for table",
  1136. next_doc_id - 1);
  1137. ut_print_name(stderr, trx, TRUE, table->name);
  1138. putc('\n', stderr);
  1139. err = DB_FTS_INVALID_DOCID;
  1140. trx->error_state = DB_FTS_INVALID_DOCID;
  1141. goto error_exit;
  1142. }
  1143. /* Difference between Doc IDs are restricted within
  1144. 4 bytes integer. See fts_get_encoded_len(). Consecutive
  1145. doc_ids difference should not exceed
  1146. FTS_DOC_ID_MAX_STEP value. */
  1147. if (next_doc_id > 1
  1148. && doc_id - next_doc_id >= FTS_DOC_ID_MAX_STEP) {
  1149. fprintf(stderr,
  1150. "InnoDB: Doc ID " UINT64PF " is too"
  1151. " big. Its difference with largest"
  1152. " used Doc ID " UINT64PF " cannot"
  1153. " exceed or equal to %d\n",
  1154. doc_id, next_doc_id - 1,
  1155. FTS_DOC_ID_MAX_STEP);
  1156. err = DB_FTS_INVALID_DOCID;
  1157. trx->error_state = DB_FTS_INVALID_DOCID;
  1158. goto error_exit;
  1159. }
  1160. }
  1161. /* Pass NULL for the columns affected, since an INSERT affects
  1162. all FTS indexes. */
  1163. fts_trx_add_op(trx, table, doc_id, FTS_INSERT, NULL);
  1164. }
  1165. que_thr_stop_for_mysql_no_error(thr, trx);
  1166. srv_stats.n_rows_inserted.add((size_t)trx->id, 1);
  1167. /* Not protected by dict_table_stats_lock() for performance
  1168. reasons, we would rather get garbage in stat_n_rows (which is
  1169. just an estimate anyway) than protecting the following code
  1170. with a latch. */
  1171. dict_table_n_rows_inc(table);
  1172. row_update_statistics_if_needed(table);
  1173. trx->op_info = "";
  1174. return(err);
  1175. }
  1176. /*********************************************************************//**
  1177. Builds a dummy query graph used in selects. */
  1178. UNIV_INTERN
  1179. void
  1180. row_prebuild_sel_graph(
  1181. /*===================*/
  1182. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  1183. handle */
  1184. {
  1185. sel_node_t* node;
  1186. ut_ad(prebuilt && prebuilt->trx);
  1187. if (prebuilt->sel_graph == NULL) {
  1188. node = sel_node_create(prebuilt->heap);
  1189. prebuilt->sel_graph = static_cast<que_fork_t*>(
  1190. que_node_get_parent(
  1191. pars_complete_graph_for_exec(
  1192. static_cast<sel_node_t*>(node),
  1193. prebuilt->trx, prebuilt->heap)));
  1194. prebuilt->sel_graph->state = QUE_FORK_ACTIVE;
  1195. }
  1196. }
  1197. /*********************************************************************//**
  1198. Creates an query graph node of 'update' type to be used in the MySQL
  1199. interface.
  1200. @return own: update node */
  1201. UNIV_INTERN
  1202. upd_node_t*
  1203. row_create_update_node_for_mysql(
  1204. /*=============================*/
  1205. dict_table_t* table, /*!< in: table to update */
  1206. mem_heap_t* heap) /*!< in: mem heap from which allocated */
  1207. {
  1208. upd_node_t* node;
  1209. node = upd_node_create(heap);
  1210. node->in_mysql_interface = TRUE;
  1211. node->is_delete = FALSE;
  1212. node->searched_update = FALSE;
  1213. node->select = NULL;
  1214. node->pcur = btr_pcur_create_for_mysql();
  1215. node->table = table;
  1216. node->update = upd_create(dict_table_get_n_cols(table), heap);
  1217. node->update_n_fields = dict_table_get_n_cols(table);
  1218. UT_LIST_INIT(node->columns);
  1219. node->has_clust_rec_x_lock = TRUE;
  1220. node->cmpl_info = 0;
  1221. node->table_sym = NULL;
  1222. node->col_assign_list = NULL;
  1223. return(node);
  1224. }
  1225. /*********************************************************************//**
  1226. Gets pointer to a prebuilt update vector used in updates. If the update
  1227. graph has not yet been built in the prebuilt struct, then this function
  1228. first builds it.
  1229. @return prebuilt update vector */
  1230. UNIV_INTERN
  1231. upd_t*
  1232. row_get_prebuilt_update_vector(
  1233. /*===========================*/
  1234. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  1235. handle */
  1236. {
  1237. dict_table_t* table = prebuilt->table;
  1238. upd_node_t* node;
  1239. ut_ad(prebuilt && table && prebuilt->trx);
  1240. if (prebuilt->upd_node == NULL) {
  1241. /* Not called before for this handle: create an update node
  1242. and query graph to the prebuilt struct */
  1243. node = row_create_update_node_for_mysql(table, prebuilt->heap);
  1244. prebuilt->upd_node = node;
  1245. prebuilt->upd_graph = static_cast<que_fork_t*>(
  1246. que_node_get_parent(
  1247. pars_complete_graph_for_exec(
  1248. static_cast<upd_node_t*>(node),
  1249. prebuilt->trx, prebuilt->heap)));
  1250. prebuilt->upd_graph->state = QUE_FORK_ACTIVE;
  1251. }
  1252. return(prebuilt->upd_node->update);
  1253. }
  1254. /********************************************************************
  1255. Handle an update of a column that has an FTS index. */
  1256. static
  1257. void
  1258. row_fts_do_update(
  1259. /*==============*/
  1260. trx_t* trx, /* in: transaction */
  1261. dict_table_t* table, /* in: Table with FTS index */
  1262. doc_id_t old_doc_id, /* in: old document id */
  1263. doc_id_t new_doc_id) /* in: new document id */
  1264. {
  1265. if (trx->fts_next_doc_id) {
  1266. fts_trx_add_op(trx, table, old_doc_id, FTS_DELETE, NULL);
  1267. fts_trx_add_op(trx, table, new_doc_id, FTS_INSERT, NULL);
  1268. }
  1269. }
  1270. /************************************************************************
  1271. Handles FTS matters for an update or a delete.
  1272. NOTE: should not be called if the table does not have an FTS index. .*/
  1273. static
  1274. dberr_t
  1275. row_fts_update_or_delete(
  1276. /*=====================*/
  1277. row_prebuilt_t* prebuilt) /* in: prebuilt struct in MySQL
  1278. handle */
  1279. {
  1280. trx_t* trx = prebuilt->trx;
  1281. dict_table_t* table = prebuilt->table;
  1282. upd_node_t* node = prebuilt->upd_node;
  1283. doc_id_t old_doc_id = prebuilt->fts_doc_id;
  1284. ut_a(dict_table_has_fts_index(prebuilt->table));
  1285. /* Deletes are simple; get them out of the way first. */
  1286. if (node->is_delete) {
  1287. /* A delete affects all FTS indexes, so we pass NULL */
  1288. fts_trx_add_op(trx, table, old_doc_id, FTS_DELETE, NULL);
  1289. } else {
  1290. doc_id_t new_doc_id;
  1291. new_doc_id = fts_read_doc_id((byte*) &trx->fts_next_doc_id);
  1292. if (new_doc_id == 0) {
  1293. fprintf(stderr, " InnoDB FTS: Doc ID cannot be 0 \n");
  1294. return(DB_FTS_INVALID_DOCID);
  1295. }
  1296. row_fts_do_update(trx, table, old_doc_id, new_doc_id);
  1297. }
  1298. return(DB_SUCCESS);
  1299. }
  1300. /*********************************************************************//**
  1301. Initialize the Doc ID system for FK table with FTS index */
  1302. static
  1303. void
  1304. init_fts_doc_id_for_ref(
  1305. /*====================*/
  1306. dict_table_t* table, /*!< in: table */
  1307. ulint* depth) /*!< in: recusive call depth */
  1308. {
  1309. dict_foreign_t* foreign;
  1310. table->fk_max_recusive_level = 0;
  1311. (*depth)++;
  1312. /* Limit on tables involved in cascading delete/update */
  1313. if (*depth > FK_MAX_CASCADE_DEL) {
  1314. return;
  1315. }
  1316. /* Loop through this table's referenced list and also
  1317. recursively traverse each table's foreign table list */
  1318. for (dict_foreign_set::iterator it = table->referenced_set.begin();
  1319. it != table->referenced_set.end();
  1320. ++it) {
  1321. foreign = *it;
  1322. if (foreign->foreign_table == NULL) {
  1323. break;
  1324. }
  1325. if (foreign->foreign_table->fts != NULL) {
  1326. fts_init_doc_id(foreign->foreign_table);
  1327. }
  1328. if (!foreign->foreign_table->referenced_set.empty()
  1329. && foreign->foreign_table != table) {
  1330. init_fts_doc_id_for_ref(
  1331. foreign->foreign_table, depth);
  1332. }
  1333. }
  1334. }
  1335. /*********************************************************************//**
  1336. Does an update or delete of a row for MySQL.
  1337. @return error code or DB_SUCCESS */
  1338. UNIV_INTERN
  1339. dberr_t
  1340. row_update_for_mysql(
  1341. /*=================*/
  1342. byte* mysql_rec, /*!< in: the row to be updated, in
  1343. the MySQL format */
  1344. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  1345. handle */
  1346. {
  1347. trx_savept_t savept;
  1348. dberr_t err;
  1349. que_thr_t* thr;
  1350. ibool was_lock_wait;
  1351. dict_index_t* clust_index;
  1352. /* ulint ref_len; */
  1353. upd_node_t* node;
  1354. dict_table_t* table = prebuilt->table;
  1355. trx_t* trx = prebuilt->trx;
  1356. ulint fk_depth = 0;
  1357. ut_ad(prebuilt != NULL);
  1358. ut_ad(trx != NULL);
  1359. UT_NOT_USED(mysql_rec);
  1360. if (prebuilt->table->ibd_file_missing) {
  1361. ut_print_timestamp(stderr);
  1362. fprintf(stderr, " InnoDB: Error:\n"
  1363. "InnoDB: MySQL is trying to use a table handle"
  1364. " but the .ibd file for\n"
  1365. "InnoDB: table %s does not exist.\n"
  1366. "InnoDB: Have you deleted the .ibd file"
  1367. " from the database directory under\n"
  1368. "InnoDB: the MySQL datadir, or have you"
  1369. " used DISCARD TABLESPACE?\n"
  1370. "InnoDB: Look from\n"
  1371. "InnoDB: " REFMAN "innodb-troubleshooting.html\n"
  1372. "InnoDB: how you can resolve the problem.\n",
  1373. prebuilt->table->name);
  1374. return(DB_ERROR);
  1375. }
  1376. if (UNIV_UNLIKELY(prebuilt->magic_n != ROW_PREBUILT_ALLOCATED)) {
  1377. fprintf(stderr,
  1378. "InnoDB: Error: trying to free a corrupt\n"
  1379. "InnoDB: table handle. Magic n %lu, table name ",
  1380. (ulong) prebuilt->magic_n);
  1381. ut_print_name(stderr, trx, TRUE, prebuilt->table->name);
  1382. putc('\n', stderr);
  1383. mem_analyze_corruption(prebuilt);
  1384. ut_error;
  1385. }
  1386. if (UNIV_UNLIKELY(srv_force_recovery)) {
  1387. fputs("InnoDB: innodb_force_recovery is on: we do not allow\n"
  1388. "InnoDB: database modifications by the user. Shut down\n"
  1389. "InnoDB: mysqld and edit my.cnf so that"
  1390. "InnoDB: innodb_force_... is removed.\n",
  1391. stderr);
  1392. return(DB_READ_ONLY);
  1393. }
  1394. DEBUG_SYNC_C("innodb_row_update_for_mysql_begin");
  1395. trx->op_info = "updating or deleting";
  1396. row_mysql_delay_if_needed();
  1397. trx_start_if_not_started_xa(trx);
  1398. if (dict_table_is_referenced_by_foreign_key(table)) {
  1399. /* Share lock the data dictionary to prevent any
  1400. table dictionary (for foreign constraint) change.
  1401. This is similar to row_ins_check_foreign_constraint
  1402. check protect by the dictionary lock as well.
  1403. In the future, this can be removed once the Foreign
  1404. key MDL is implemented */
  1405. row_mysql_freeze_data_dictionary(trx);
  1406. init_fts_doc_id_for_ref(table, &fk_depth);
  1407. row_mysql_unfreeze_data_dictionary(trx);
  1408. }
  1409. node = prebuilt->upd_node;
  1410. clust_index = dict_table_get_first_index(table);
  1411. if (prebuilt->pcur.btr_cur.index == clust_index) {
  1412. btr_pcur_copy_stored_position(node->pcur, &prebuilt->pcur);
  1413. } else {
  1414. btr_pcur_copy_stored_position(node->pcur,
  1415. &prebuilt->clust_pcur);
  1416. }
  1417. ut_a(node->pcur->rel_pos == BTR_PCUR_ON);
  1418. /* MySQL seems to call rnd_pos before updating each row it
  1419. has cached: we can get the correct cursor position from
  1420. prebuilt->pcur; NOTE that we cannot build the row reference
  1421. from mysql_rec if the clustered index was automatically
  1422. generated for the table: MySQL does not know anything about
  1423. the row id used as the clustered index key */
  1424. savept = trx_savept_take(trx);
  1425. thr = que_fork_get_first_thr(prebuilt->upd_graph);
  1426. node->state = UPD_NODE_UPDATE_CLUSTERED;
  1427. ut_ad(!prebuilt->sql_stat_start);
  1428. que_thr_move_to_run_state_for_mysql(thr, trx);
  1429. run_again:
  1430. thr->run_node = node;
  1431. thr->prev_node = node;
  1432. thr->fk_cascade_depth = 0;
  1433. row_upd_step(thr);
  1434. err = trx->error_state;
  1435. /* Reset fk_cascade_depth back to 0 */
  1436. thr->fk_cascade_depth = 0;
  1437. if (err != DB_SUCCESS) {
  1438. que_thr_stop_for_mysql(thr);
  1439. if (err == DB_RECORD_NOT_FOUND) {
  1440. trx->error_state = DB_SUCCESS;
  1441. trx->op_info = "";
  1442. return(err);
  1443. }
  1444. thr->lock_state= QUE_THR_LOCK_ROW;
  1445. DEBUG_SYNC(trx->mysql_thd, "row_update_for_mysql_error");
  1446. was_lock_wait = row_mysql_handle_errors(&err, trx, thr,
  1447. &savept);
  1448. thr->lock_state= QUE_THR_LOCK_NOLOCK;
  1449. if (was_lock_wait) {
  1450. goto run_again;
  1451. }
  1452. trx->op_info = "";
  1453. return(err);
  1454. }
  1455. que_thr_stop_for_mysql_no_error(thr, trx);
  1456. if (dict_table_has_fts_index(table)
  1457. && trx->fts_next_doc_id != UINT64_UNDEFINED) {
  1458. err = row_fts_update_or_delete(prebuilt);
  1459. if (err != DB_SUCCESS) {
  1460. trx->op_info = "";
  1461. return(err);
  1462. }
  1463. }
  1464. if (node->is_delete) {
  1465. /* Not protected by dict_table_stats_lock() for performance
  1466. reasons, we would rather get garbage in stat_n_rows (which is
  1467. just an estimate anyway) than protecting the following code
  1468. with a latch. */
  1469. dict_table_n_rows_dec(prebuilt->table);
  1470. srv_stats.n_rows_deleted.add((size_t)trx->id, 1);
  1471. } else {
  1472. srv_stats.n_rows_updated.add((size_t)trx->id, 1);
  1473. }
  1474. /* We update table statistics only if it is a DELETE or UPDATE
  1475. that changes indexed columns, UPDATEs that change only non-indexed
  1476. columns would not affect statistics. */
  1477. if (node->is_delete || !(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
  1478. row_update_statistics_if_needed(prebuilt->table);
  1479. }
  1480. trx->op_info = "";
  1481. return(err);
  1482. }
  1483. /*********************************************************************//**
  1484. This can only be used when srv_locks_unsafe_for_binlog is TRUE or this
  1485. session is using a READ COMMITTED or READ UNCOMMITTED isolation level.
  1486. Before calling this function row_search_for_mysql() must have
  1487. initialized prebuilt->new_rec_locks to store the information which new
  1488. record locks really were set. This function removes a newly set
  1489. clustered index record lock under prebuilt->pcur or
  1490. prebuilt->clust_pcur. Thus, this implements a 'mini-rollback' that
  1491. releases the latest clustered index record lock we set.
  1492. @return error code or DB_SUCCESS */
  1493. UNIV_INTERN
  1494. void
  1495. row_unlock_for_mysql(
  1496. /*=================*/
  1497. row_prebuilt_t* prebuilt, /*!< in/out: prebuilt struct in MySQL
  1498. handle */
  1499. ibool has_latches_on_recs)/*!< in: TRUE if called so
  1500. that we have the latches on
  1501. the records under pcur and
  1502. clust_pcur, and we do not need
  1503. to reposition the cursors. */
  1504. {
  1505. btr_pcur_t* pcur = &prebuilt->pcur;
  1506. btr_pcur_t* clust_pcur = &prebuilt->clust_pcur;
  1507. trx_t* trx = prebuilt->trx;
  1508. ut_ad(prebuilt != NULL);
  1509. ut_ad(trx != NULL);
  1510. if (UNIV_UNLIKELY
  1511. (!srv_locks_unsafe_for_binlog
  1512. && trx->isolation_level > TRX_ISO_READ_COMMITTED)) {
  1513. fprintf(stderr,
  1514. "InnoDB: Error: calling row_unlock_for_mysql though\n"
  1515. "InnoDB: innodb_locks_unsafe_for_binlog is FALSE and\n"
  1516. "InnoDB: this session is not using"
  1517. " READ COMMITTED isolation level.\n");
  1518. return;
  1519. }
  1520. trx->op_info = "unlock_row";
  1521. if (prebuilt->new_rec_locks >= 1) {
  1522. const rec_t* rec;
  1523. dict_index_t* index;
  1524. trx_id_t rec_trx_id;
  1525. mtr_t mtr;
  1526. mtr_start(&mtr);
  1527. /* Restore the cursor position and find the record */
  1528. if (!has_latches_on_recs) {
  1529. btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, &mtr);
  1530. }
  1531. rec = btr_pcur_get_rec(pcur);
  1532. index = btr_pcur_get_btr_cur(pcur)->index;
  1533. if (prebuilt->new_rec_locks >= 2) {
  1534. /* Restore the cursor position and find the record
  1535. in the clustered index. */
  1536. if (!has_latches_on_recs) {
  1537. btr_pcur_restore_position(BTR_SEARCH_LEAF,
  1538. clust_pcur, &mtr);
  1539. }
  1540. rec = btr_pcur_get_rec(clust_pcur);
  1541. index = btr_pcur_get_btr_cur(clust_pcur)->index;
  1542. }
  1543. if (!dict_index_is_clust(index)) {
  1544. /* This is not a clustered index record. We
  1545. do not know how to unlock the record. */
  1546. goto no_unlock;
  1547. }
  1548. /* If the record has been modified by this
  1549. transaction, do not unlock it. */
  1550. if (index->trx_id_offset) {
  1551. rec_trx_id = trx_read_trx_id(rec
  1552. + index->trx_id_offset);
  1553. } else {
  1554. mem_heap_t* heap = NULL;
  1555. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  1556. ulint* offsets = offsets_;
  1557. rec_offs_init(offsets_);
  1558. offsets = rec_get_offsets(rec, index, offsets,
  1559. ULINT_UNDEFINED, &heap);
  1560. rec_trx_id = row_get_rec_trx_id(rec, index, offsets);
  1561. if (UNIV_LIKELY_NULL(heap)) {
  1562. mem_heap_free(heap);
  1563. }
  1564. }
  1565. if (rec_trx_id != trx->id) {
  1566. /* We did not update the record: unlock it */
  1567. rec = btr_pcur_get_rec(pcur);
  1568. lock_rec_unlock(
  1569. trx,
  1570. btr_pcur_get_block(pcur),
  1571. rec,
  1572. static_cast<enum lock_mode>(
  1573. prebuilt->select_lock_type));
  1574. if (prebuilt->new_rec_locks >= 2) {
  1575. rec = btr_pcur_get_rec(clust_pcur);
  1576. lock_rec_unlock(
  1577. trx,
  1578. btr_pcur_get_block(clust_pcur),
  1579. rec,
  1580. static_cast<enum lock_mode>(
  1581. prebuilt->select_lock_type));
  1582. }
  1583. }
  1584. no_unlock:
  1585. mtr_commit(&mtr);
  1586. }
  1587. trx->op_info = "";
  1588. }
  1589. /**********************************************************************//**
  1590. Does a cascaded delete or set null in a foreign key operation.
  1591. @return error code or DB_SUCCESS */
  1592. UNIV_INTERN
  1593. dberr_t
  1594. row_update_cascade_for_mysql(
  1595. /*=========================*/
  1596. que_thr_t* thr, /*!< in: query thread */
  1597. upd_node_t* node, /*!< in: update node used in the cascade
  1598. or set null operation */
  1599. dict_table_t* table) /*!< in: table where we do the operation */
  1600. {
  1601. dberr_t err;
  1602. trx_t* trx;
  1603. trx = thr_get_trx(thr);
  1604. /* Increment fk_cascade_depth to record the recursive call depth on
  1605. a single update/delete that affects multiple tables chained
  1606. together with foreign key relations. */
  1607. thr->fk_cascade_depth++;
  1608. if (thr->fk_cascade_depth > FK_MAX_CASCADE_DEL) {
  1609. return(DB_FOREIGN_EXCEED_MAX_CASCADE);
  1610. }
  1611. run_again:
  1612. thr->run_node = node;
  1613. thr->prev_node = node;
  1614. DEBUG_SYNC_C("foreign_constraint_update_cascade");
  1615. row_upd_step(thr);
  1616. /* The recursive call for cascading update/delete happens
  1617. in above row_upd_step(), reset the counter once we come
  1618. out of the recursive call, so it does not accumulate for
  1619. different row deletes */
  1620. thr->fk_cascade_depth = 0;
  1621. err = trx->error_state;
  1622. /* Note that the cascade node is a subnode of another InnoDB
  1623. query graph node. We do a normal lock wait in this node, but
  1624. all errors are handled by the parent node. */
  1625. if (err == DB_LOCK_WAIT) {
  1626. /* Handle lock wait here */
  1627. que_thr_stop_for_mysql(thr);
  1628. lock_wait_suspend_thread(thr);
  1629. /* Note that a lock wait may also end in a lock wait timeout,
  1630. or this transaction is picked as a victim in selective
  1631. deadlock resolution */
  1632. if (trx->error_state != DB_SUCCESS) {
  1633. return(trx->error_state);
  1634. }
  1635. /* Retry operation after a normal lock wait */
  1636. goto run_again;
  1637. }
  1638. if (err != DB_SUCCESS) {
  1639. return(err);
  1640. }
  1641. if (node->is_delete) {
  1642. /* Not protected by dict_table_stats_lock() for performance
  1643. reasons, we would rather get garbage in stat_n_rows (which is
  1644. just an estimate anyway) than protecting the following code
  1645. with a latch. */
  1646. dict_table_n_rows_dec(table);
  1647. srv_stats.n_rows_deleted.add((size_t)trx->id, 1);
  1648. } else {
  1649. srv_stats.n_rows_updated.add((size_t)trx->id, 1);
  1650. }
  1651. row_update_statistics_if_needed(table);
  1652. return(err);
  1653. }
  1654. /*********************************************************************//**
  1655. Checks if a table is such that we automatically created a clustered
  1656. index on it (on row id).
  1657. @return TRUE if the clustered index was generated automatically */
  1658. UNIV_INTERN
  1659. ibool
  1660. row_table_got_default_clust_index(
  1661. /*==============================*/
  1662. const dict_table_t* table) /*!< in: table */
  1663. {
  1664. const dict_index_t* clust_index;
  1665. clust_index = dict_table_get_first_index(table);
  1666. return(dict_index_get_nth_col(clust_index, 0)->mtype == DATA_SYS);
  1667. }
  1668. /*********************************************************************//**
  1669. Locks the data dictionary in shared mode from modifications, for performing
  1670. foreign key check, rollback, or other operation invisible to MySQL. */
  1671. UNIV_INTERN
  1672. void
  1673. row_mysql_freeze_data_dictionary_func(
  1674. /*==================================*/
  1675. trx_t* trx, /*!< in/out: transaction */
  1676. const char* file, /*!< in: file name */
  1677. ulint line) /*!< in: line number */
  1678. {
  1679. ut_a(trx->dict_operation_lock_mode == 0);
  1680. rw_lock_s_lock_inline(&dict_operation_lock, 0, file, line);
  1681. trx->dict_operation_lock_mode = RW_S_LATCH;
  1682. }
  1683. /*********************************************************************//**
  1684. Unlocks the data dictionary shared lock. */
  1685. UNIV_INTERN
  1686. void
  1687. row_mysql_unfreeze_data_dictionary(
  1688. /*===============================*/
  1689. trx_t* trx) /*!< in/out: transaction */
  1690. {
  1691. ut_ad(lock_trx_has_sys_table_locks(trx) == NULL);
  1692. ut_a(trx->dict_operation_lock_mode == RW_S_LATCH);
  1693. rw_lock_s_unlock(&dict_operation_lock);
  1694. trx->dict_operation_lock_mode = 0;
  1695. }
  1696. /*********************************************************************//**
  1697. Locks the data dictionary exclusively for performing a table create or other
  1698. data dictionary modification operation. */
  1699. UNIV_INTERN
  1700. void
  1701. row_mysql_lock_data_dictionary_func(
  1702. /*================================*/
  1703. trx_t* trx, /*!< in/out: transaction */
  1704. const char* file, /*!< in: file name */
  1705. ulint line) /*!< in: line number */
  1706. {
  1707. ut_a(trx->dict_operation_lock_mode == 0
  1708. || trx->dict_operation_lock_mode == RW_X_LATCH);
  1709. /* Serialize data dictionary operations with dictionary mutex:
  1710. no deadlocks or lock waits can occur then in these operations */
  1711. rw_lock_x_lock_inline(&dict_operation_lock, 0, file, line);
  1712. trx->dict_operation_lock_mode = RW_X_LATCH;
  1713. mutex_enter(&(dict_sys->mutex));
  1714. }
  1715. /*********************************************************************//**
  1716. Unlocks the data dictionary exclusive lock. */
  1717. UNIV_INTERN
  1718. void
  1719. row_mysql_unlock_data_dictionary(
  1720. /*=============================*/
  1721. trx_t* trx) /*!< in/out: transaction */
  1722. {
  1723. ut_ad(lock_trx_has_sys_table_locks(trx) == NULL);
  1724. ut_a(trx->dict_operation_lock_mode == RW_X_LATCH);
  1725. /* Serialize data dictionary operations with dictionary mutex:
  1726. no deadlocks can occur then in these operations */
  1727. mutex_exit(&(dict_sys->mutex));
  1728. rw_lock_x_unlock(&dict_operation_lock);
  1729. trx->dict_operation_lock_mode = 0;
  1730. }
  1731. /*********************************************************************//**
  1732. Creates a table for MySQL. If the name of the table ends in
  1733. one of "innodb_monitor", "innodb_lock_monitor", "innodb_tablespace_monitor",
  1734. "innodb_table_monitor", then this will also start the printing of monitor
  1735. output by the master thread. If the table name ends in "innodb_mem_validate",
  1736. InnoDB will try to invoke mem_validate(). On failure the transaction will
  1737. be rolled back and the 'table' object will be freed.
  1738. @return error code or DB_SUCCESS */
  1739. UNIV_INTERN
  1740. dberr_t
  1741. row_create_table_for_mysql(
  1742. /*=======================*/
  1743. dict_table_t* table, /*!< in, own: table definition
  1744. (will be freed, or on DB_SUCCESS
  1745. added to the data dictionary cache) */
  1746. trx_t* trx, /*!< in/out: transaction */
  1747. bool commit) /*!< in: if true, commit the transaction */
  1748. {
  1749. tab_node_t* node;
  1750. mem_heap_t* heap;
  1751. que_thr_t* thr;
  1752. const char* table_name;
  1753. ulint table_name_len;
  1754. dberr_t err;
  1755. #ifdef UNIV_SYNC_DEBUG
  1756. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1757. #endif /* UNIV_SYNC_DEBUG */
  1758. ut_ad(mutex_own(&(dict_sys->mutex)));
  1759. ut_ad(trx->dict_operation_lock_mode == RW_X_LATCH);
  1760. DBUG_EXECUTE_IF(
  1761. "ib_create_table_fail_at_start_of_row_create_table_for_mysql",
  1762. goto err_exit;
  1763. );
  1764. trx->op_info = "creating table";
  1765. if (row_mysql_is_system_table(table->name)) {
  1766. fprintf(stderr,
  1767. "InnoDB: Error: trying to create a MySQL system"
  1768. " table %s of type InnoDB.\n"
  1769. "InnoDB: MySQL system tables must be"
  1770. " of the MyISAM type!\n",
  1771. table->name);
  1772. #ifndef DBUG_OFF
  1773. err_exit:
  1774. #endif /* !DBUG_OFF */
  1775. dict_mem_table_free(table);
  1776. if (commit) {
  1777. trx_commit_for_mysql(trx);
  1778. }
  1779. trx->op_info = "";
  1780. return(DB_ERROR);
  1781. }
  1782. trx_start_if_not_started_xa(trx);
  1783. /* The table name is prefixed with the database name and a '/'.
  1784. Certain table names starting with 'innodb_' have their special
  1785. meaning regardless of the database name. Thus, we need to
  1786. ignore the database name prefix in the comparisons. */
  1787. table_name = dict_remove_db_name(table->name);
  1788. table_name_len = strlen(table_name) + 1;
  1789. if (STR_EQ(table_name, table_name_len, S_innodb_monitor)) {
  1790. /* Table equals "innodb_monitor":
  1791. start monitor prints */
  1792. srv_print_innodb_monitor = TRUE;
  1793. /* The lock timeout monitor thread also takes care
  1794. of InnoDB monitor prints */
  1795. os_event_set(lock_sys->timeout_event);
  1796. } else if (STR_EQ(table_name, table_name_len,
  1797. S_innodb_lock_monitor)) {
  1798. srv_print_innodb_monitor = TRUE;
  1799. srv_print_innodb_lock_monitor = TRUE;
  1800. os_event_set(lock_sys->timeout_event);
  1801. } else if (STR_EQ(table_name, table_name_len,
  1802. S_innodb_tablespace_monitor)) {
  1803. srv_print_innodb_tablespace_monitor = TRUE;
  1804. os_event_set(lock_sys->timeout_event);
  1805. } else if (STR_EQ(table_name, table_name_len,
  1806. S_innodb_table_monitor)) {
  1807. srv_print_innodb_table_monitor = TRUE;
  1808. os_event_set(lock_sys->timeout_event);
  1809. #ifdef UNIV_MEM_DEBUG
  1810. } else if (STR_EQ(table_name, table_name_len,
  1811. S_innodb_mem_validate)) {
  1812. /* We define here a debugging feature intended for
  1813. developers */
  1814. fputs("Validating InnoDB memory:\n"
  1815. "to use this feature you must compile InnoDB with\n"
  1816. "UNIV_MEM_DEBUG defined in univ.i and"
  1817. " the server must be\n"
  1818. "quiet because allocation from a mem heap"
  1819. " is not protected\n"
  1820. "by any semaphore.\n", stderr);
  1821. ut_a(mem_validate());
  1822. fputs("Memory validated\n", stderr);
  1823. #endif /* UNIV_MEM_DEBUG */
  1824. }
  1825. heap = mem_heap_create(512);
  1826. switch (trx_get_dict_operation(trx)) {
  1827. case TRX_DICT_OP_NONE:
  1828. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  1829. case TRX_DICT_OP_TABLE:
  1830. break;
  1831. case TRX_DICT_OP_INDEX:
  1832. /* If the transaction was previously flagged as
  1833. TRX_DICT_OP_INDEX, we should be creating auxiliary
  1834. tables for full-text indexes. */
  1835. ut_ad(strstr(table->name, "/FTS_") != NULL);
  1836. }
  1837. node = tab_create_graph_create(table, heap, commit);
  1838. thr = pars_complete_graph_for_exec(node, trx, heap);
  1839. ut_a(thr == que_fork_start_command(
  1840. static_cast<que_fork_t*>(que_node_get_parent(thr))));
  1841. que_run_threads(thr);
  1842. err = trx->error_state;
  1843. if (table->space != TRX_SYS_SPACE) {
  1844. ut_a(DICT_TF2_FLAG_IS_SET(table, DICT_TF2_USE_TABLESPACE));
  1845. /* Update SYS_TABLESPACES and SYS_DATAFILES if a new
  1846. tablespace was created. */
  1847. if (err == DB_SUCCESS) {
  1848. char* path;
  1849. path = fil_space_get_first_path(table->space);
  1850. err = dict_create_add_tablespace_to_dictionary(
  1851. table->space, table->name,
  1852. fil_space_get_flags(table->space),
  1853. path, trx, commit);
  1854. mem_free(path);
  1855. }
  1856. if (err != DB_SUCCESS) {
  1857. /* We must delete the link file. */
  1858. fil_delete_link_file(table->name);
  1859. }
  1860. }
  1861. switch (err) {
  1862. case DB_SUCCESS:
  1863. break;
  1864. case DB_OUT_OF_FILE_SPACE:
  1865. trx->error_state = DB_SUCCESS;
  1866. trx_rollback_to_savepoint(trx, NULL);
  1867. ut_print_timestamp(stderr);
  1868. fputs(" InnoDB: Warning: cannot create table ",
  1869. stderr);
  1870. ut_print_name(stderr, trx, TRUE, table->name);
  1871. fputs(" because tablespace full\n", stderr);
  1872. if (dict_table_open_on_name(table->name, TRUE, FALSE,
  1873. DICT_ERR_IGNORE_NONE)) {
  1874. /* Make things easy for the drop table code. */
  1875. if (table->can_be_evicted) {
  1876. dict_table_move_from_lru_to_non_lru(table);
  1877. }
  1878. dict_table_close(table, TRUE, FALSE);
  1879. row_drop_table_for_mysql(table->name, trx, FALSE);
  1880. if (commit) {
  1881. trx_commit_for_mysql(trx);
  1882. }
  1883. } else {
  1884. dict_mem_table_free(table);
  1885. }
  1886. break;
  1887. case DB_TOO_MANY_CONCURRENT_TRXS:
  1888. /* We already have .ibd file here. it should be deleted. */
  1889. if (table->space
  1890. && fil_delete_tablespace(
  1891. table->space,
  1892. BUF_REMOVE_FLUSH_NO_WRITE)
  1893. != DB_SUCCESS) {
  1894. ut_print_timestamp(stderr);
  1895. fprintf(stderr,
  1896. " InnoDB: Error: not able to"
  1897. " delete tablespace %lu of table ",
  1898. (ulong) table->space);
  1899. ut_print_name(stderr, trx, TRUE, table->name);
  1900. fputs("!\n", stderr);
  1901. }
  1902. /* fall through */
  1903. case DB_DUPLICATE_KEY:
  1904. case DB_TABLESPACE_EXISTS:
  1905. default:
  1906. trx->error_state = DB_SUCCESS;
  1907. trx_rollback_to_savepoint(trx, NULL);
  1908. dict_mem_table_free(table);
  1909. break;
  1910. }
  1911. que_graph_free((que_t*) que_node_get_parent(thr));
  1912. trx->op_info = "";
  1913. return(err);
  1914. }
  1915. /*********************************************************************//**
  1916. Does an index creation operation for MySQL. TODO: currently failure
  1917. to create an index results in dropping the whole table! This is no problem
  1918. currently as all indexes must be created at the same time as the table.
  1919. @return error number or DB_SUCCESS */
  1920. UNIV_INTERN
  1921. dberr_t
  1922. row_create_index_for_mysql(
  1923. /*=======================*/
  1924. dict_index_t* index, /*!< in, own: index definition
  1925. (will be freed) */
  1926. trx_t* trx, /*!< in: transaction handle */
  1927. const ulint* field_lengths) /*!< in: if not NULL, must contain
  1928. dict_index_get_n_fields(index)
  1929. actual field lengths for the
  1930. index columns, which are
  1931. then checked for not being too
  1932. large. */
  1933. {
  1934. ind_node_t* node;
  1935. mem_heap_t* heap;
  1936. que_thr_t* thr;
  1937. dberr_t err;
  1938. ulint i;
  1939. ulint len;
  1940. char* table_name;
  1941. char* index_name;
  1942. dict_table_t* table;
  1943. ibool is_fts;
  1944. #ifdef UNIV_SYNC_DEBUG
  1945. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1946. #endif /* UNIV_SYNC_DEBUG */
  1947. ut_ad(mutex_own(&(dict_sys->mutex)));
  1948. trx->op_info = "creating index";
  1949. /* Copy the table name because we may want to drop the
  1950. table later, after the index object is freed (inside
  1951. que_run_threads()) and thus index->table_name is not available. */
  1952. table_name = mem_strdup(index->table_name);
  1953. index_name = mem_strdup(index->name);
  1954. is_fts = (index->type == DICT_FTS);
  1955. table = dict_table_open_on_name(table_name, TRUE, TRUE,
  1956. DICT_ERR_IGNORE_NONE);
  1957. trx_start_if_not_started_xa(trx);
  1958. for (i = 0; i < index->n_def; i++) {
  1959. /* Check that prefix_len and actual length
  1960. < DICT_MAX_INDEX_COL_LEN */
  1961. len = dict_index_get_nth_field(index, i)->prefix_len;
  1962. if (field_lengths && field_lengths[i]) {
  1963. len = ut_max(len, field_lengths[i]);
  1964. }
  1965. DBUG_EXECUTE_IF(
  1966. "ib_create_table_fail_at_create_index",
  1967. len = DICT_MAX_FIELD_LEN_BY_FORMAT(table) + 1;
  1968. );
  1969. /* Column or prefix length exceeds maximum column length */
  1970. if (len > (ulint) DICT_MAX_FIELD_LEN_BY_FORMAT(table)) {
  1971. err = DB_TOO_BIG_INDEX_COL;
  1972. dict_mem_index_free(index);
  1973. goto error_handling;
  1974. }
  1975. }
  1976. heap = mem_heap_create(512);
  1977. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  1978. /* Note that the space id where we store the index is inherited from
  1979. the table in dict_build_index_def_step() in dict0crea.cc. */
  1980. node = ind_create_graph_create(index, heap, true);
  1981. thr = pars_complete_graph_for_exec(node, trx, heap);
  1982. ut_a(thr == que_fork_start_command(
  1983. static_cast<que_fork_t*>(que_node_get_parent(thr))));
  1984. que_run_threads(thr);
  1985. err = trx->error_state;
  1986. que_graph_free((que_t*) que_node_get_parent(thr));
  1987. /* Create the index specific FTS auxiliary tables. */
  1988. if (err == DB_SUCCESS && is_fts) {
  1989. dict_index_t* idx;
  1990. idx = dict_table_get_index_on_name(table, index_name);
  1991. ut_ad(idx);
  1992. err = fts_create_index_tables(trx, idx);
  1993. }
  1994. error_handling:
  1995. dict_table_close(table, TRUE, FALSE);
  1996. if (err != DB_SUCCESS) {
  1997. /* We have special error handling here */
  1998. trx->error_state = DB_SUCCESS;
  1999. trx_rollback_to_savepoint(trx, NULL);
  2000. row_drop_table_for_mysql(table_name, trx, FALSE);
  2001. trx_commit_for_mysql(trx);
  2002. trx->error_state = DB_SUCCESS;
  2003. }
  2004. trx->op_info = "";
  2005. mem_free(table_name);
  2006. mem_free(index_name);
  2007. return(err);
  2008. }
  2009. /*********************************************************************//**
  2010. Scans a table create SQL string and adds to the data dictionary
  2011. the foreign key constraints declared in the string. This function
  2012. should be called after the indexes for a table have been created.
  2013. Each foreign key constraint must be accompanied with indexes in
  2014. both participating tables. The indexes are allowed to contain more
  2015. fields than mentioned in the constraint. Check also that foreign key
  2016. constraints which reference this table are ok.
  2017. @return error code or DB_SUCCESS */
  2018. UNIV_INTERN
  2019. dberr_t
  2020. row_table_add_foreign_constraints(
  2021. /*==============================*/
  2022. trx_t* trx, /*!< in: transaction */
  2023. const char* sql_string, /*!< in: table create statement where
  2024. foreign keys are declared like:
  2025. FOREIGN KEY (a, b) REFERENCES table2(c, d),
  2026. table2 can be written also with the
  2027. database name before it: test.table2 */
  2028. size_t sql_length, /*!< in: length of sql_string */
  2029. const char* name, /*!< in: table full name in the
  2030. normalized form
  2031. database_name/table_name */
  2032. ibool reject_fks) /*!< in: if TRUE, fail with error
  2033. code DB_CANNOT_ADD_CONSTRAINT if
  2034. any foreign keys are found. */
  2035. {
  2036. dberr_t err;
  2037. ut_ad(mutex_own(&(dict_sys->mutex)));
  2038. #ifdef UNIV_SYNC_DEBUG
  2039. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  2040. #endif /* UNIV_SYNC_DEBUG */
  2041. ut_a(sql_string);
  2042. trx->op_info = "adding foreign keys";
  2043. trx_start_if_not_started_xa(trx);
  2044. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  2045. err = dict_create_foreign_constraints(trx, sql_string, sql_length,
  2046. name, reject_fks);
  2047. DBUG_EXECUTE_IF("ib_table_add_foreign_fail",
  2048. err = DB_DUPLICATE_KEY;);
  2049. DEBUG_SYNC_C("table_add_foreign_constraints");
  2050. if (err == DB_SUCCESS) {
  2051. /* Check that also referencing constraints are ok */
  2052. err = dict_load_foreigns(name, NULL, false, true,
  2053. DICT_ERR_IGNORE_NONE);
  2054. }
  2055. if (err != DB_SUCCESS) {
  2056. /* We have special error handling here */
  2057. trx->error_state = DB_SUCCESS;
  2058. trx_rollback_to_savepoint(trx, NULL);
  2059. row_drop_table_for_mysql(name, trx, FALSE);
  2060. trx_commit_for_mysql(trx);
  2061. trx->error_state = DB_SUCCESS;
  2062. }
  2063. return(err);
  2064. }
  2065. /*********************************************************************//**
  2066. Drops a table for MySQL as a background operation. MySQL relies on Unix
  2067. in ALTER TABLE to the fact that the table handler does not remove the
  2068. table before all handles to it has been removed. Furhermore, the MySQL's
  2069. call to drop table must be non-blocking. Therefore we do the drop table
  2070. as a background operation, which is taken care of by the master thread
  2071. in srv0srv.cc.
  2072. @return error code or DB_SUCCESS */
  2073. static
  2074. dberr_t
  2075. row_drop_table_for_mysql_in_background(
  2076. /*===================================*/
  2077. const char* name) /*!< in: table name */
  2078. {
  2079. dberr_t error;
  2080. trx_t* trx;
  2081. trx = trx_allocate_for_background();
  2082. /* If the original transaction was dropping a table referenced by
  2083. foreign keys, we must set the following to be able to drop the
  2084. table: */
  2085. trx->check_foreigns = FALSE;
  2086. /* fputs("InnoDB: Error: Dropping table ", stderr);
  2087. ut_print_name(stderr, trx, TRUE, name);
  2088. fputs(" in background drop list\n", stderr); */
  2089. /* Try to drop the table in InnoDB */
  2090. error = row_drop_table_for_mysql(name, trx, FALSE);
  2091. /* Flush the log to reduce probability that the .frm files and
  2092. the InnoDB data dictionary get out-of-sync if the user runs
  2093. with innodb_flush_log_at_trx_commit = 0 */
  2094. log_buffer_flush_to_disk();
  2095. trx_commit_for_mysql(trx);
  2096. trx_free_for_background(trx);
  2097. return(error);
  2098. }
  2099. /*********************************************************************//**
  2100. The master thread in srv0srv.cc calls this regularly to drop tables which
  2101. we must drop in background after queries to them have ended. Such lazy
  2102. dropping of tables is needed in ALTER TABLE on Unix.
  2103. @return how many tables dropped + remaining tables in list */
  2104. UNIV_INTERN
  2105. ulint
  2106. row_drop_tables_for_mysql_in_background(void)
  2107. /*=========================================*/
  2108. {
  2109. row_mysql_drop_t* drop;
  2110. dict_table_t* table;
  2111. ulint n_tables;
  2112. ulint n_tables_dropped = 0;
  2113. loop:
  2114. mutex_enter(&row_drop_list_mutex);
  2115. ut_a(row_mysql_drop_list_inited);
  2116. drop = UT_LIST_GET_FIRST(row_mysql_drop_list);
  2117. n_tables = UT_LIST_GET_LEN(row_mysql_drop_list);
  2118. mutex_exit(&row_drop_list_mutex);
  2119. if (drop == NULL) {
  2120. /* All tables dropped */
  2121. return(n_tables + n_tables_dropped);
  2122. }
  2123. DBUG_EXECUTE_IF("row_drop_tables_in_background_sleep",
  2124. os_thread_sleep(5000000);
  2125. );
  2126. table = dict_table_open_on_name(drop->table_name, FALSE, FALSE,
  2127. DICT_ERR_IGNORE_NONE);
  2128. if (table == NULL) {
  2129. /* If for some reason the table has already been dropped
  2130. through some other mechanism, do not try to drop it */
  2131. goto already_dropped;
  2132. }
  2133. if (!table->to_be_dropped) {
  2134. /* There is a scenario: the old table is dropped
  2135. just after it's added into drop list, and new
  2136. table with the same name is created, then we try
  2137. to drop the new table in background. */
  2138. dict_table_close(table, FALSE, FALSE);
  2139. goto already_dropped;
  2140. }
  2141. ut_a(!table->can_be_evicted);
  2142. dict_table_close(table, FALSE, FALSE);
  2143. if (DB_SUCCESS != row_drop_table_for_mysql_in_background(
  2144. drop->table_name)) {
  2145. /* If the DROP fails for some table, we return, and let the
  2146. main thread retry later */
  2147. return(n_tables + n_tables_dropped);
  2148. }
  2149. n_tables_dropped++;
  2150. already_dropped:
  2151. mutex_enter(&row_drop_list_mutex);
  2152. UT_LIST_REMOVE(row_mysql_drop_list, row_mysql_drop_list, drop);
  2153. MONITOR_DEC(MONITOR_BACKGROUND_DROP_TABLE);
  2154. ut_print_timestamp(stderr);
  2155. fputs(" InnoDB: Dropped table ", stderr);
  2156. ut_print_name(stderr, NULL, TRUE, drop->table_name);
  2157. fputs(" in background drop queue.\n", stderr);
  2158. mem_free(drop->table_name);
  2159. mem_free(drop);
  2160. mutex_exit(&row_drop_list_mutex);
  2161. goto loop;
  2162. }
  2163. /*********************************************************************//**
  2164. Get the background drop list length. NOTE: the caller must own the
  2165. drop list mutex!
  2166. @return how many tables in list */
  2167. UNIV_INTERN
  2168. ulint
  2169. row_get_background_drop_list_len_low(void)
  2170. /*======================================*/
  2171. {
  2172. ulint len;
  2173. mutex_enter(&row_drop_list_mutex);
  2174. ut_a(row_mysql_drop_list_inited);
  2175. len = UT_LIST_GET_LEN(row_mysql_drop_list);
  2176. mutex_exit(&row_drop_list_mutex);
  2177. return(len);
  2178. }
  2179. /*********************************************************************//**
  2180. If a table is not yet in the drop list, adds the table to the list of tables
  2181. which the master thread drops in background. We need this on Unix because in
  2182. ALTER TABLE MySQL may call drop table even if the table has running queries on
  2183. it. Also, if there are running foreign key checks on the table, we drop the
  2184. table lazily.
  2185. @return TRUE if the table was not yet in the drop list, and was added there */
  2186. static
  2187. ibool
  2188. row_add_table_to_background_drop_list(
  2189. /*==================================*/
  2190. const char* name) /*!< in: table name */
  2191. {
  2192. row_mysql_drop_t* drop;
  2193. mutex_enter(&row_drop_list_mutex);
  2194. ut_a(row_mysql_drop_list_inited);
  2195. /* Look if the table already is in the drop list */
  2196. for (drop = UT_LIST_GET_FIRST(row_mysql_drop_list);
  2197. drop != NULL;
  2198. drop = UT_LIST_GET_NEXT(row_mysql_drop_list, drop)) {
  2199. if (strcmp(drop->table_name, name) == 0) {
  2200. /* Already in the list */
  2201. mutex_exit(&row_drop_list_mutex);
  2202. return(FALSE);
  2203. }
  2204. }
  2205. drop = static_cast<row_mysql_drop_t*>(
  2206. mem_alloc(sizeof(row_mysql_drop_t)));
  2207. drop->table_name = mem_strdup(name);
  2208. UT_LIST_ADD_LAST(row_mysql_drop_list, row_mysql_drop_list, drop);
  2209. MONITOR_INC(MONITOR_BACKGROUND_DROP_TABLE);
  2210. /* fputs("InnoDB: Adding table ", stderr);
  2211. ut_print_name(stderr, trx, TRUE, drop->table_name);
  2212. fputs(" to background drop list\n", stderr); */
  2213. mutex_exit(&row_drop_list_mutex);
  2214. return(TRUE);
  2215. }
  2216. /*********************************************************************//**
  2217. Reassigns the table identifier of a table.
  2218. @return error code or DB_SUCCESS */
  2219. UNIV_INTERN
  2220. dberr_t
  2221. row_mysql_table_id_reassign(
  2222. /*========================*/
  2223. dict_table_t* table, /*!< in/out: table */
  2224. trx_t* trx, /*!< in/out: transaction */
  2225. table_id_t* new_id) /*!< out: new table id */
  2226. {
  2227. dberr_t err;
  2228. pars_info_t* info = pars_info_create();
  2229. dict_hdr_get_new_id(new_id, NULL, NULL);
  2230. /* Remove all locks except the table-level S and X locks. */
  2231. lock_remove_all_on_table(table, FALSE);
  2232. pars_info_add_ull_literal(info, "old_id", table->id);
  2233. pars_info_add_ull_literal(info, "new_id", *new_id);
  2234. err = que_eval_sql(
  2235. info,
  2236. "PROCEDURE RENUMBER_TABLE_PROC () IS\n"
  2237. "BEGIN\n"
  2238. "UPDATE SYS_TABLES SET ID = :new_id\n"
  2239. " WHERE ID = :old_id;\n"
  2240. "UPDATE SYS_COLUMNS SET TABLE_ID = :new_id\n"
  2241. " WHERE TABLE_ID = :old_id;\n"
  2242. "UPDATE SYS_INDEXES SET TABLE_ID = :new_id\n"
  2243. " WHERE TABLE_ID = :old_id;\n"
  2244. "END;\n", FALSE, trx);
  2245. return(err);
  2246. }
  2247. /*********************************************************************//**
  2248. Setup the pre-requisites for DISCARD TABLESPACE. It will start the transaction,
  2249. acquire the data dictionary lock in X mode and open the table.
  2250. @return table instance or 0 if not found. */
  2251. static
  2252. dict_table_t*
  2253. row_discard_tablespace_begin(
  2254. /*=========================*/
  2255. const char* name, /*!< in: table name */
  2256. trx_t* trx) /*!< in: transaction handle */
  2257. {
  2258. trx->op_info = "discarding tablespace";
  2259. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  2260. trx_start_if_not_started_xa(trx);
  2261. /* Serialize data dictionary operations with dictionary mutex:
  2262. this is to avoid deadlocks during data dictionary operations */
  2263. row_mysql_lock_data_dictionary(trx);
  2264. dict_table_t* table;
  2265. table = dict_table_open_on_name(
  2266. name, TRUE, FALSE, DICT_ERR_IGNORE_NONE);
  2267. if (table) {
  2268. dict_stats_wait_bg_to_stop_using_table(table, trx);
  2269. ut_a(table->space != TRX_SYS_SPACE);
  2270. ut_a(table->n_foreign_key_checks_running == 0);
  2271. }
  2272. return(table);
  2273. }
  2274. /*********************************************************************//**
  2275. Do the foreign key constraint checks.
  2276. @return DB_SUCCESS or error code. */
  2277. static
  2278. dberr_t
  2279. row_discard_tablespace_foreign_key_checks(
  2280. /*======================================*/
  2281. const trx_t* trx, /*!< in: transaction handle */
  2282. const dict_table_t* table) /*!< in: table to be discarded */
  2283. {
  2284. if (srv_read_only_mode || !trx->check_foreigns) {
  2285. return(DB_SUCCESS);
  2286. }
  2287. /* Check if the table is referenced by foreign key constraints from
  2288. some other table (not the table itself) */
  2289. dict_foreign_set::iterator it
  2290. = std::find_if(table->referenced_set.begin(),
  2291. table->referenced_set.end(),
  2292. dict_foreign_different_tables());
  2293. if (it == table->referenced_set.end()) {
  2294. return(DB_SUCCESS);
  2295. }
  2296. const dict_foreign_t* foreign = *it;
  2297. FILE* ef = dict_foreign_err_file;
  2298. ut_ad(foreign->foreign_table != table);
  2299. ut_ad(foreign->referenced_table == table);
  2300. /* We only allow discarding a referenced table if
  2301. FOREIGN_KEY_CHECKS is set to 0 */
  2302. mutex_enter(&dict_foreign_err_mutex);
  2303. rewind(ef);
  2304. ut_print_timestamp(ef);
  2305. fputs(" Cannot DISCARD table ", ef);
  2306. ut_print_name(stderr, trx, TRUE, table->name);
  2307. fputs("\n"
  2308. "because it is referenced by ", ef);
  2309. ut_print_name(stderr, trx, TRUE, foreign->foreign_table_name);
  2310. putc('\n', ef);
  2311. mutex_exit(&dict_foreign_err_mutex);
  2312. return(DB_CANNOT_DROP_CONSTRAINT);
  2313. }
  2314. /*********************************************************************//**
  2315. Cleanup after the DISCARD TABLESPACE operation.
  2316. @return error code. */
  2317. static
  2318. dberr_t
  2319. row_discard_tablespace_end(
  2320. /*=======================*/
  2321. trx_t* trx, /*!< in/out: transaction handle */
  2322. dict_table_t* table, /*!< in/out: table to be discarded */
  2323. dberr_t err) /*!< in: error code */
  2324. {
  2325. if (table != 0) {
  2326. dict_table_close(table, TRUE, FALSE);
  2327. }
  2328. DBUG_EXECUTE_IF("ib_discard_before_commit_crash",
  2329. log_make_checkpoint_at(LSN_MAX, TRUE);
  2330. DBUG_SUICIDE(););
  2331. trx_commit_for_mysql(trx);
  2332. DBUG_EXECUTE_IF("ib_discard_after_commit_crash",
  2333. log_make_checkpoint_at(LSN_MAX, TRUE);
  2334. DBUG_SUICIDE(););
  2335. row_mysql_unlock_data_dictionary(trx);
  2336. trx->op_info = "";
  2337. return(err);
  2338. }
  2339. /*********************************************************************//**
  2340. Do the DISCARD TABLESPACE operation.
  2341. @return DB_SUCCESS or error code. */
  2342. static
  2343. dberr_t
  2344. row_discard_tablespace(
  2345. /*===================*/
  2346. trx_t* trx, /*!< in/out: transaction handle */
  2347. dict_table_t* table) /*!< in/out: table to be discarded */
  2348. {
  2349. dberr_t err;
  2350. /* How do we prevent crashes caused by ongoing operations on
  2351. the table? Old operations could try to access non-existent
  2352. pages. MySQL will block all DML on the table using MDL and a
  2353. DISCARD will not start unless all existing operations on the
  2354. table to be discarded are completed.
  2355. 1) Acquire the data dictionary latch in X mode. To prevent any
  2356. internal operations that MySQL is not aware off and also for
  2357. the internal SQL parser.
  2358. 2) Purge and rollback: we assign a new table id for the
  2359. table. Since purge and rollback look for the table based on
  2360. the table id, they see the table as 'dropped' and discard
  2361. their operations.
  2362. 3) Insert buffer: we remove all entries for the tablespace in
  2363. the insert buffer tree.
  2364. 4) FOREIGN KEY operations: if table->n_foreign_key_checks_running > 0,
  2365. we do not allow the discard. */
  2366. /* Play safe and remove all insert buffer entries, though we should
  2367. have removed them already when DISCARD TABLESPACE was called */
  2368. ibuf_delete_for_discarded_space(table->space);
  2369. table_id_t new_id;
  2370. /* Set the TABLESPACE DISCARD flag in the table definition on disk. */
  2371. err = row_import_update_discarded_flag(trx, table->id, true, true);
  2372. if (err != DB_SUCCESS) {
  2373. return(err);
  2374. }
  2375. /* Update the index root pages in the system tables, on disk */
  2376. err = row_import_update_index_root(trx, table, true, true);
  2377. if (err != DB_SUCCESS) {
  2378. return(err);
  2379. }
  2380. /* Drop all the FTS auxiliary tables. */
  2381. if (dict_table_has_fts_index(table)
  2382. || DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) {
  2383. fts_drop_tables(trx, table);
  2384. }
  2385. /* Assign a new space ID to the table definition so that purge
  2386. can ignore the changes. Update the system table on disk. */
  2387. err = row_mysql_table_id_reassign(table, trx, &new_id);
  2388. if (err != DB_SUCCESS) {
  2389. return(err);
  2390. }
  2391. /* Discard the physical file that is used for the tablespace. */
  2392. err = fil_discard_tablespace(table->space);
  2393. switch(err) {
  2394. case DB_SUCCESS:
  2395. case DB_IO_ERROR:
  2396. case DB_TABLESPACE_NOT_FOUND:
  2397. /* All persistent operations successful, update the
  2398. data dictionary memory cache. */
  2399. table->ibd_file_missing = TRUE;
  2400. table->flags2 |= DICT_TF2_DISCARDED;
  2401. dict_table_change_id_in_cache(table, new_id);
  2402. /* Reset the root page numbers. */
  2403. for (dict_index_t* index = UT_LIST_GET_FIRST(table->indexes);
  2404. index != 0;
  2405. index = UT_LIST_GET_NEXT(indexes, index)) {
  2406. index->page = FIL_NULL;
  2407. index->space = FIL_NULL;
  2408. }
  2409. /* If the tablespace did not already exist or we couldn't
  2410. write to it, we treat that as a successful DISCARD. It is
  2411. unusable anyway. */
  2412. err = DB_SUCCESS;
  2413. break;
  2414. default:
  2415. /* We need to rollback the disk changes, something failed. */
  2416. trx->error_state = DB_SUCCESS;
  2417. trx_rollback_to_savepoint(trx, NULL);
  2418. trx->error_state = DB_SUCCESS;
  2419. }
  2420. return(err);
  2421. }
  2422. /*********************************************************************//**
  2423. Discards the tablespace of a table which stored in an .ibd file. Discarding
  2424. means that this function renames the .ibd file and assigns a new table id for
  2425. the table. Also the flag table->ibd_file_missing is set to TRUE.
  2426. @return error code or DB_SUCCESS */
  2427. UNIV_INTERN
  2428. dberr_t
  2429. row_discard_tablespace_for_mysql(
  2430. /*=============================*/
  2431. const char* name, /*!< in: table name */
  2432. trx_t* trx) /*!< in: transaction handle */
  2433. {
  2434. dberr_t err;
  2435. dict_table_t* table;
  2436. /* Open the table and start the transaction if not started. */
  2437. table = row_discard_tablespace_begin(name, trx);
  2438. if (table == 0) {
  2439. err = DB_TABLE_NOT_FOUND;
  2440. } else if (table->space == TRX_SYS_SPACE) {
  2441. char table_name[MAX_FULL_NAME_LEN + 1];
  2442. innobase_format_name(
  2443. table_name, sizeof(table_name), table->name, FALSE);
  2444. ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_ERROR,
  2445. ER_TABLE_IN_SYSTEM_TABLESPACE, table_name);
  2446. err = DB_ERROR;
  2447. } else if (table->n_foreign_key_checks_running > 0) {
  2448. char table_name[MAX_FULL_NAME_LEN + 1];
  2449. innobase_format_name(
  2450. table_name, sizeof(table_name), table->name, FALSE);
  2451. ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_ERROR,
  2452. ER_DISCARD_FK_CHECKS_RUNNING, table_name);
  2453. err = DB_ERROR;
  2454. } else {
  2455. /* Do foreign key constraint checks. */
  2456. err = row_discard_tablespace_foreign_key_checks(trx, table);
  2457. if (err == DB_SUCCESS) {
  2458. err = row_discard_tablespace(trx, table);
  2459. }
  2460. }
  2461. return(row_discard_tablespace_end(trx, table, err));
  2462. }
  2463. /*********************************************************************//**
  2464. Sets an exclusive lock on a table.
  2465. @return error code or DB_SUCCESS */
  2466. UNIV_INTERN
  2467. dberr_t
  2468. row_mysql_lock_table(
  2469. /*=================*/
  2470. trx_t* trx, /*!< in/out: transaction */
  2471. dict_table_t* table, /*!< in: table to lock */
  2472. enum lock_mode mode, /*!< in: LOCK_X or LOCK_S */
  2473. const char* op_info) /*!< in: string for trx->op_info */
  2474. {
  2475. mem_heap_t* heap;
  2476. que_thr_t* thr;
  2477. dberr_t err;
  2478. sel_node_t* node;
  2479. ut_ad(trx);
  2480. ut_ad(mode == LOCK_X || mode == LOCK_S);
  2481. heap = mem_heap_create(512);
  2482. trx->op_info = op_info;
  2483. node = sel_node_create(heap);
  2484. thr = pars_complete_graph_for_exec(node, trx, heap);
  2485. thr->graph->state = QUE_FORK_ACTIVE;
  2486. /* We use the select query graph as the dummy graph needed
  2487. in the lock module call */
  2488. thr = que_fork_get_first_thr(
  2489. static_cast<que_fork_t*>(que_node_get_parent(thr)));
  2490. que_thr_move_to_run_state_for_mysql(thr, trx);
  2491. run_again:
  2492. thr->run_node = thr;
  2493. thr->prev_node = thr->common.parent;
  2494. err = lock_table(0, table, mode, thr);
  2495. trx->error_state = err;
  2496. if (err == DB_SUCCESS) {
  2497. que_thr_stop_for_mysql_no_error(thr, trx);
  2498. } else {
  2499. que_thr_stop_for_mysql(thr);
  2500. if (err != DB_QUE_THR_SUSPENDED) {
  2501. ibool was_lock_wait;
  2502. was_lock_wait = row_mysql_handle_errors(
  2503. &err, trx, thr, NULL);
  2504. if (was_lock_wait) {
  2505. goto run_again;
  2506. }
  2507. } else {
  2508. que_thr_t* run_thr;
  2509. que_node_t* parent;
  2510. parent = que_node_get_parent(thr);
  2511. run_thr = que_fork_start_command(
  2512. static_cast<que_fork_t*>(parent));
  2513. ut_a(run_thr == thr);
  2514. /* There was a lock wait but the thread was not
  2515. in a ready to run or running state. */
  2516. trx->error_state = DB_LOCK_WAIT;
  2517. goto run_again;
  2518. }
  2519. }
  2520. que_graph_free(thr->graph);
  2521. trx->op_info = "";
  2522. return(err);
  2523. }
  2524. /*********************************************************************//**
  2525. Truncates a table for MySQL.
  2526. @return error code or DB_SUCCESS */
  2527. UNIV_INTERN
  2528. dberr_t
  2529. row_truncate_table_for_mysql(
  2530. /*=========================*/
  2531. dict_table_t* table, /*!< in: table handle */
  2532. trx_t* trx) /*!< in: transaction handle */
  2533. {
  2534. dberr_t err;
  2535. mem_heap_t* heap;
  2536. byte* buf;
  2537. dtuple_t* tuple;
  2538. dfield_t* dfield;
  2539. dict_index_t* sys_index;
  2540. btr_pcur_t pcur;
  2541. mtr_t mtr;
  2542. table_id_t new_id;
  2543. ulint recreate_space = 0;
  2544. pars_info_t* info = NULL;
  2545. ibool has_internal_doc_id;
  2546. ulint old_space = table->space;
  2547. /* How do we prevent crashes caused by ongoing operations on
  2548. the table? Old operations could try to access non-existent
  2549. pages.
  2550. 1) SQL queries, INSERT, SELECT, ...: we must get an exclusive
  2551. InnoDB table lock on the table before we can do TRUNCATE
  2552. TABLE. Then there are no running queries on the table.
  2553. 2) Purge and rollback: we assign a new table id for the
  2554. table. Since purge and rollback look for the table based on
  2555. the table id, they see the table as 'dropped' and discard
  2556. their operations.
  2557. 3) Insert buffer: TRUNCATE TABLE is analogous to DROP TABLE,
  2558. so we do not have to remove insert buffer records, as the
  2559. insert buffer works at a low level. If a freed page is later
  2560. reallocated, the allocator will remove the ibuf entries for
  2561. it.
  2562. When we truncate *.ibd files by recreating them (analogous to
  2563. DISCARD TABLESPACE), we remove all entries for the table in the
  2564. insert buffer tree. This is not strictly necessary, because
  2565. in 6) we will assign a new tablespace identifier, but we can
  2566. free up some space in the system tablespace.
  2567. 4) Linear readahead and random readahead: we use the same
  2568. method as in 3) to discard ongoing operations. (This is only
  2569. relevant for TRUNCATE TABLE by DISCARD TABLESPACE.)
  2570. 5) FOREIGN KEY operations: if
  2571. table->n_foreign_key_checks_running > 0, we do not allow the
  2572. TRUNCATE. We also reserve the data dictionary latch.
  2573. 6) Crash recovery: To prevent the application of pre-truncation
  2574. redo log records on the truncated tablespace, we will assign
  2575. a new tablespace identifier to the truncated tablespace. */
  2576. ut_ad(table);
  2577. if (dict_table_is_discarded(table)) {
  2578. return(DB_TABLESPACE_DELETED);
  2579. } else if (table->ibd_file_missing) {
  2580. return(DB_TABLESPACE_NOT_FOUND);
  2581. }
  2582. trx_start_for_ddl(trx, TRX_DICT_OP_TABLE);
  2583. trx->op_info = "truncating table";
  2584. /* Serialize data dictionary operations with dictionary mutex:
  2585. no deadlocks can occur then in these operations */
  2586. ut_a(trx->dict_operation_lock_mode == 0);
  2587. /* Prevent foreign key checks etc. while we are truncating the
  2588. table */
  2589. row_mysql_lock_data_dictionary(trx);
  2590. ut_ad(mutex_own(&(dict_sys->mutex)));
  2591. #ifdef UNIV_SYNC_DEBUG
  2592. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  2593. #endif /* UNIV_SYNC_DEBUG */
  2594. dict_stats_wait_bg_to_stop_using_table(table, trx);
  2595. /* Check if the table is referenced by foreign key constraints from
  2596. some other table (not the table itself) */
  2597. dict_foreign_set::iterator it
  2598. = std::find_if(table->referenced_set.begin(),
  2599. table->referenced_set.end(),
  2600. dict_foreign_different_tables());
  2601. if (!srv_read_only_mode
  2602. && it != table->referenced_set.end()
  2603. && trx->check_foreigns) {
  2604. FILE* ef = dict_foreign_err_file;
  2605. dict_foreign_t* foreign = *it;
  2606. /* We only allow truncating a referenced table if
  2607. FOREIGN_KEY_CHECKS is set to 0 */
  2608. mutex_enter(&dict_foreign_err_mutex);
  2609. rewind(ef);
  2610. ut_print_timestamp(ef);
  2611. fputs(" Cannot truncate table ", ef);
  2612. ut_print_name(ef, trx, TRUE, table->name);
  2613. fputs(" by DROP+CREATE\n"
  2614. "InnoDB: because it is referenced by ", ef);
  2615. ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
  2616. putc('\n', ef);
  2617. mutex_exit(&dict_foreign_err_mutex);
  2618. err = DB_ERROR;
  2619. goto funct_exit;
  2620. }
  2621. /* TODO: could we replace the counter n_foreign_key_checks_running
  2622. with lock checks on the table? Acquire here an exclusive lock on the
  2623. table, and rewrite lock0lock.cc and the lock wait in srv0srv.cc so that
  2624. they can cope with the table having been truncated here? Foreign key
  2625. checks take an IS or IX lock on the table. */
  2626. if (table->n_foreign_key_checks_running > 0) {
  2627. ut_print_timestamp(stderr);
  2628. fputs(" InnoDB: Cannot truncate table ", stderr);
  2629. ut_print_name(stderr, trx, TRUE, table->name);
  2630. fputs(" by DROP+CREATE\n"
  2631. "InnoDB: because there is a foreign key check"
  2632. " running on it.\n",
  2633. stderr);
  2634. err = DB_ERROR;
  2635. goto funct_exit;
  2636. }
  2637. /* Check if memcached plugin is running on this table. if is, we don't
  2638. allow truncate this table. */
  2639. if (table->memcached_sync_count != 0) {
  2640. ut_print_timestamp(stderr);
  2641. fputs(" InnoDB: Cannot truncate table ", stderr);
  2642. ut_print_name(stderr, trx, TRUE, table->name);
  2643. fputs(" by DROP+CREATE\n"
  2644. "InnoDB: because there are memcached operations"
  2645. " running on it.\n",
  2646. stderr);
  2647. err = DB_ERROR;
  2648. goto funct_exit;
  2649. } else {
  2650. /* We need to set this counter to -1 for blocking
  2651. memcached operations. */
  2652. table->memcached_sync_count = DICT_TABLE_IN_DDL;
  2653. }
  2654. /* Remove all locks except the table-level X lock. */
  2655. lock_remove_all_on_table(table, FALSE);
  2656. /* Ensure that the table will be dropped by
  2657. trx_rollback_active() in case of a crash. */
  2658. trx->table_id = table->id;
  2659. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  2660. /* Assign an undo segment for the transaction, so that the
  2661. transaction will be recovered after a crash. */
  2662. mutex_enter(&trx->undo_mutex);
  2663. err = trx_undo_assign_undo(trx, TRX_UNDO_UPDATE);
  2664. mutex_exit(&trx->undo_mutex);
  2665. if (err != DB_SUCCESS) {
  2666. goto funct_exit;
  2667. }
  2668. if (table->space && !DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY)) {
  2669. /* Discard and create the single-table tablespace. */
  2670. ulint space = table->space;
  2671. ulint flags = fil_space_get_flags(space);
  2672. dict_get_and_save_data_dir_path(table, true);
  2673. if (flags != ULINT_UNDEFINED
  2674. && fil_discard_tablespace(space) == DB_SUCCESS) {
  2675. dict_index_t* index;
  2676. dict_hdr_get_new_id(NULL, NULL, &space);
  2677. /* Lock all index trees for this table. We must
  2678. do so after dict_hdr_get_new_id() to preserve
  2679. the latch order */
  2680. dict_table_x_lock_indexes(table);
  2681. if (space == ULINT_UNDEFINED
  2682. || fil_create_new_single_table_tablespace(
  2683. space, table->name,
  2684. table->data_dir_path,
  2685. flags, table->flags2,
  2686. FIL_IBD_FILE_INITIAL_SIZE)
  2687. != DB_SUCCESS) {
  2688. dict_table_x_unlock_indexes(table);
  2689. ib_logf(IB_LOG_LEVEL_ERROR,
  2690. "TRUNCATE TABLE %s failed to "
  2691. "create a new tablespace",
  2692. table->name);
  2693. table->ibd_file_missing = 1;
  2694. err = DB_ERROR;
  2695. goto funct_exit;
  2696. }
  2697. recreate_space = space;
  2698. /* Replace the space_id in the data dictionary cache.
  2699. The persisent data dictionary (SYS_TABLES.SPACE
  2700. and SYS_INDEXES.SPACE) are updated later in this
  2701. function. */
  2702. table->space = space;
  2703. index = dict_table_get_first_index(table);
  2704. do {
  2705. index->space = space;
  2706. index = dict_table_get_next_index(index);
  2707. } while (index);
  2708. mtr_start(&mtr);
  2709. fsp_header_init(space,
  2710. FIL_IBD_FILE_INITIAL_SIZE, &mtr);
  2711. mtr_commit(&mtr);
  2712. }
  2713. } else {
  2714. /* Lock all index trees for this table, as we will
  2715. truncate the table/index and possibly change their metadata.
  2716. All DML/DDL are blocked by table level lock, with
  2717. a few exceptions such as queries into information schema
  2718. about the table, MySQL could try to access index stats
  2719. for this kind of query, we need to use index locks to
  2720. sync up */
  2721. dict_table_x_lock_indexes(table);
  2722. }
  2723. /* scan SYS_INDEXES for all indexes of the table */
  2724. heap = mem_heap_create(800);
  2725. tuple = dtuple_create(heap, 1);
  2726. dfield = dtuple_get_nth_field(tuple, 0);
  2727. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  2728. mach_write_to_8(buf, table->id);
  2729. dfield_set_data(dfield, buf, 8);
  2730. sys_index = dict_table_get_first_index(dict_sys->sys_indexes);
  2731. dict_index_copy_types(tuple, sys_index, 1);
  2732. mtr_start(&mtr);
  2733. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  2734. BTR_MODIFY_LEAF, &pcur, &mtr);
  2735. for (;;) {
  2736. rec_t* rec;
  2737. const byte* field;
  2738. ulint len;
  2739. ulint root_page_no;
  2740. if (!btr_pcur_is_on_user_rec(&pcur)) {
  2741. /* The end of SYS_INDEXES has been reached. */
  2742. break;
  2743. }
  2744. rec = btr_pcur_get_rec(&pcur);
  2745. field = rec_get_nth_field_old(
  2746. rec, DICT_FLD__SYS_INDEXES__TABLE_ID, &len);
  2747. ut_ad(len == 8);
  2748. if (memcmp(buf, field, len) != 0) {
  2749. /* End of indexes for the table (TABLE_ID mismatch). */
  2750. break;
  2751. }
  2752. if (rec_get_deleted_flag(rec, FALSE)) {
  2753. /* The index has been dropped. */
  2754. goto next_rec;
  2755. }
  2756. /* This call may commit and restart mtr
  2757. and reposition pcur. */
  2758. root_page_no = dict_truncate_index_tree(table, recreate_space,
  2759. &pcur, &mtr);
  2760. rec = btr_pcur_get_rec(&pcur);
  2761. if (root_page_no != FIL_NULL) {
  2762. page_rec_write_field(
  2763. rec, DICT_FLD__SYS_INDEXES__PAGE_NO,
  2764. root_page_no, &mtr);
  2765. /* We will need to commit and restart the
  2766. mini-transaction in order to avoid deadlocks.
  2767. The dict_truncate_index_tree() call has allocated
  2768. a page in this mini-transaction, and the rest of
  2769. this loop could latch another index page. */
  2770. mtr_commit(&mtr);
  2771. mtr_start(&mtr);
  2772. btr_pcur_restore_position(BTR_MODIFY_LEAF,
  2773. &pcur, &mtr);
  2774. }
  2775. next_rec:
  2776. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  2777. }
  2778. btr_pcur_close(&pcur);
  2779. mtr_commit(&mtr);
  2780. mem_heap_free(heap);
  2781. /* Done with index truncation, release index tree locks,
  2782. subsequent work relates to table level metadata change */
  2783. dict_table_x_unlock_indexes(table);
  2784. dict_hdr_get_new_id(&new_id, NULL, NULL);
  2785. /* Create new FTS auxiliary tables with the new_id, and
  2786. drop the old index later, only if everything runs successful. */
  2787. has_internal_doc_id = dict_table_has_fts_index(table)
  2788. || DICT_TF2_FLAG_IS_SET(
  2789. table, DICT_TF2_FTS_HAS_DOC_ID);
  2790. if (has_internal_doc_id) {
  2791. dict_table_t fts_table;
  2792. ulint i;
  2793. fts_table.name = table->name;
  2794. fts_table.id = new_id;
  2795. fts_table.flags2 = table->flags2;
  2796. err = fts_create_common_tables(
  2797. trx, &fts_table, table->name, TRUE);
  2798. for (i = 0;
  2799. i < ib_vector_size(table->fts->indexes)
  2800. && err == DB_SUCCESS;
  2801. i++) {
  2802. dict_index_t* fts_index;
  2803. fts_index = static_cast<dict_index_t*>(
  2804. ib_vector_getp(table->fts->indexes, i));
  2805. err = fts_create_index_tables_low(
  2806. trx, fts_index, table->name, new_id);
  2807. }
  2808. if (err != DB_SUCCESS) {
  2809. trx->error_state = DB_SUCCESS;
  2810. trx_rollback_to_savepoint(trx, NULL);
  2811. trx->error_state = DB_SUCCESS;
  2812. ut_print_timestamp(stderr);
  2813. fputs(" InnoDB: Unable to truncate FTS index for"
  2814. " table", stderr);
  2815. ut_print_name(stderr, trx, TRUE, table->name);
  2816. fputs("\n", stderr);
  2817. goto funct_exit;
  2818. } else {
  2819. ut_ad(trx->state != TRX_STATE_NOT_STARTED);
  2820. }
  2821. }
  2822. info = pars_info_create();
  2823. pars_info_add_int4_literal(info, "new_space", (lint) table->space);
  2824. pars_info_add_ull_literal(info, "old_id", table->id);
  2825. pars_info_add_ull_literal(info, "new_id", new_id);
  2826. err = que_eval_sql(info,
  2827. "PROCEDURE RENUMBER_TABLE_ID_PROC () IS\n"
  2828. "BEGIN\n"
  2829. "UPDATE SYS_TABLES"
  2830. " SET ID = :new_id, SPACE = :new_space\n"
  2831. " WHERE ID = :old_id;\n"
  2832. "UPDATE SYS_COLUMNS SET TABLE_ID = :new_id\n"
  2833. " WHERE TABLE_ID = :old_id;\n"
  2834. "UPDATE SYS_INDEXES"
  2835. " SET TABLE_ID = :new_id, SPACE = :new_space\n"
  2836. " WHERE TABLE_ID = :old_id;\n"
  2837. "END;\n"
  2838. , FALSE, trx);
  2839. if (err == DB_SUCCESS && old_space != table->space) {
  2840. info = pars_info_create();
  2841. pars_info_add_int4_literal(info, "old_space", (lint) old_space);
  2842. pars_info_add_int4_literal(
  2843. info, "new_space", (lint) table->space);
  2844. err = que_eval_sql(info,
  2845. "PROCEDURE RENUMBER_TABLESPACE_PROC () IS\n"
  2846. "BEGIN\n"
  2847. "UPDATE SYS_TABLESPACES"
  2848. " SET SPACE = :new_space\n"
  2849. " WHERE SPACE = :old_space;\n"
  2850. "UPDATE SYS_DATAFILES"
  2851. " SET SPACE = :new_space"
  2852. " WHERE SPACE = :old_space;\n"
  2853. "END;\n"
  2854. , FALSE, trx);
  2855. }
  2856. DBUG_EXECUTE_IF("ib_ddl_crash_before_fts_truncate", err = DB_ERROR;);
  2857. if (err != DB_SUCCESS) {
  2858. trx->error_state = DB_SUCCESS;
  2859. trx_rollback_to_savepoint(trx, NULL);
  2860. trx->error_state = DB_SUCCESS;
  2861. /* Update system table failed. Table in memory metadata
  2862. could be in an inconsistent state, mark the in-memory
  2863. table->corrupted to be true. In the long run, this should
  2864. be fixed by atomic truncate table */
  2865. table->corrupted = true;
  2866. ut_print_timestamp(stderr);
  2867. fputs(" InnoDB: Unable to assign a new identifier to table ",
  2868. stderr);
  2869. ut_print_name(stderr, trx, TRUE, table->name);
  2870. fputs("\n"
  2871. "InnoDB: after truncating it. Background processes"
  2872. " may corrupt the table!\n", stderr);
  2873. /* Failed to update the table id, so drop the new
  2874. FTS auxiliary tables */
  2875. if (has_internal_doc_id) {
  2876. ut_ad(trx->state == TRX_STATE_NOT_STARTED);
  2877. table_id_t id = table->id;
  2878. table->id = new_id;
  2879. fts_drop_tables(trx, table);
  2880. table->id = id;
  2881. ut_ad(trx->state != TRX_STATE_NOT_STARTED);
  2882. }
  2883. err = DB_ERROR;
  2884. } else {
  2885. /* Drop the old FTS index */
  2886. if (has_internal_doc_id) {
  2887. ut_ad(trx->state != TRX_STATE_NOT_STARTED);
  2888. fts_drop_tables(trx, table);
  2889. ut_ad(trx->state != TRX_STATE_NOT_STARTED);
  2890. }
  2891. DBUG_EXECUTE_IF("ib_truncate_crash_after_fts_drop",
  2892. DBUG_SUICIDE(););
  2893. dict_table_change_id_in_cache(table, new_id);
  2894. /* Reset the Doc ID in cache to 0 */
  2895. if (has_internal_doc_id && table->fts->cache) {
  2896. table->fts->fts_status |= TABLE_DICT_LOCKED;
  2897. fts_update_next_doc_id(trx, table, NULL, 0);
  2898. fts_cache_clear(table->fts->cache);
  2899. fts_cache_init(table->fts->cache);
  2900. table->fts->fts_status &= ~TABLE_DICT_LOCKED;
  2901. }
  2902. }
  2903. /* Reset auto-increment. */
  2904. dict_table_autoinc_lock(table);
  2905. dict_table_autoinc_initialize(table, 1);
  2906. dict_table_autoinc_unlock(table);
  2907. trx_commit_for_mysql(trx);
  2908. funct_exit:
  2909. if (table->memcached_sync_count == DICT_TABLE_IN_DDL) {
  2910. /* We need to set the memcached sync back to 0, unblock
  2911. memcached operationse. */
  2912. table->memcached_sync_count = 0;
  2913. }
  2914. row_mysql_unlock_data_dictionary(trx);
  2915. dict_stats_update(table, DICT_STATS_EMPTY_TABLE);
  2916. trx->op_info = "";
  2917. srv_wake_master_thread();
  2918. return(err);
  2919. }
  2920. /*********************************************************************//**
  2921. Drops a table for MySQL. If the name of the dropped table ends in
  2922. one of "innodb_monitor", "innodb_lock_monitor", "innodb_tablespace_monitor",
  2923. "innodb_table_monitor", then this will also stop the printing of monitor
  2924. output by the master thread. If the data dictionary was not already locked
  2925. by the transaction, the transaction will be committed. Otherwise, the
  2926. data dictionary will remain locked.
  2927. @return error code or DB_SUCCESS */
  2928. UNIV_INTERN
  2929. dberr_t
  2930. row_drop_table_for_mysql(
  2931. /*=====================*/
  2932. const char* name, /*!< in: table name */
  2933. trx_t* trx, /*!< in: transaction handle */
  2934. bool drop_db,/*!< in: true=dropping whole database */
  2935. bool nonatomic)
  2936. /*!< in: whether it is permitted
  2937. to release and reacquire dict_operation_lock */
  2938. {
  2939. dberr_t err;
  2940. dict_foreign_t* foreign;
  2941. dict_table_t* table;
  2942. ibool print_msg;
  2943. ulint space_id;
  2944. char* filepath = NULL;
  2945. const char* tablename_minus_db;
  2946. char* tablename = NULL;
  2947. bool ibd_file_missing;
  2948. ulint namelen;
  2949. bool locked_dictionary = false;
  2950. pars_info_t* info = NULL;
  2951. mem_heap_t* heap = NULL;
  2952. DBUG_ENTER("row_drop_table_for_mysql");
  2953. DBUG_PRINT("row_drop_table_for_mysql", ("table: %s", name));
  2954. ut_a(name != NULL);
  2955. /* The table name is prefixed with the database name and a '/'.
  2956. Certain table names starting with 'innodb_' have their special
  2957. meaning regardless of the database name. Thus, we need to
  2958. ignore the database name prefix in the comparisons. */
  2959. tablename_minus_db = strchr(name, '/');
  2960. if (tablename_minus_db) {
  2961. tablename_minus_db++;
  2962. } else {
  2963. /* Ancillary FTS tables don't have '/' characters. */
  2964. tablename_minus_db = name;
  2965. }
  2966. namelen = strlen(tablename_minus_db) + 1;
  2967. if (namelen == sizeof S_innodb_monitor
  2968. && !memcmp(tablename_minus_db, S_innodb_monitor,
  2969. sizeof S_innodb_monitor)) {
  2970. /* Table name equals "innodb_monitor":
  2971. stop monitor prints */
  2972. srv_print_innodb_monitor = FALSE;
  2973. srv_print_innodb_lock_monitor = FALSE;
  2974. } else if (namelen == sizeof S_innodb_lock_monitor
  2975. && !memcmp(tablename_minus_db, S_innodb_lock_monitor,
  2976. sizeof S_innodb_lock_monitor)) {
  2977. srv_print_innodb_monitor = FALSE;
  2978. srv_print_innodb_lock_monitor = FALSE;
  2979. } else if (namelen == sizeof S_innodb_tablespace_monitor
  2980. && !memcmp(tablename_minus_db, S_innodb_tablespace_monitor,
  2981. sizeof S_innodb_tablespace_monitor)) {
  2982. srv_print_innodb_tablespace_monitor = FALSE;
  2983. } else if (namelen == sizeof S_innodb_table_monitor
  2984. && !memcmp(tablename_minus_db, S_innodb_table_monitor,
  2985. sizeof S_innodb_table_monitor)) {
  2986. srv_print_innodb_table_monitor = FALSE;
  2987. }
  2988. /* Serialize data dictionary operations with dictionary mutex:
  2989. no deadlocks can occur then in these operations */
  2990. trx->op_info = "dropping table";
  2991. /* This function is called recursively via fts_drop_tables(). */
  2992. if (trx->state == TRX_STATE_NOT_STARTED) {
  2993. trx_start_for_ddl(trx, TRX_DICT_OP_TABLE);
  2994. }
  2995. if (trx->dict_operation_lock_mode != RW_X_LATCH) {
  2996. /* Prevent foreign key checks etc. while we are dropping the
  2997. table */
  2998. row_mysql_lock_data_dictionary(trx);
  2999. locked_dictionary = true;
  3000. nonatomic = true;
  3001. }
  3002. ut_ad(mutex_own(&(dict_sys->mutex)));
  3003. #ifdef UNIV_SYNC_DEBUG
  3004. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  3005. #endif /* UNIV_SYNC_DEBUG */
  3006. table = dict_table_open_on_name(
  3007. name, TRUE, FALSE,
  3008. static_cast<dict_err_ignore_t>(
  3009. DICT_ERR_IGNORE_INDEX_ROOT | DICT_ERR_IGNORE_CORRUPT));
  3010. if (!table) {
  3011. err = DB_TABLE_NOT_FOUND;
  3012. ut_print_timestamp(stderr);
  3013. fputs(" InnoDB: Error: table ", stderr);
  3014. ut_print_name(stderr, trx, TRUE, name);
  3015. fputs(" does not exist in the InnoDB internal\n"
  3016. "InnoDB: data dictionary though MySQL is"
  3017. " trying to drop it.\n"
  3018. "InnoDB: Have you copied the .frm file"
  3019. " of the table to the\n"
  3020. "InnoDB: MySQL database directory"
  3021. " from another database?\n"
  3022. "InnoDB: You can look for further help from\n"
  3023. "InnoDB: " REFMAN "innodb-troubleshooting.html\n",
  3024. stderr);
  3025. goto funct_exit;
  3026. }
  3027. /* Turn on this drop bit before we could release the dictionary
  3028. latch */
  3029. table->to_be_dropped = true;
  3030. if (nonatomic) {
  3031. /* This trx did not acquire any locks on dictionary
  3032. table records yet. Thus it is safe to release and
  3033. reacquire the data dictionary latches. */
  3034. if (table->fts) {
  3035. ut_ad(!table->fts->add_wq);
  3036. ut_ad(lock_trx_has_sys_table_locks(trx) == 0);
  3037. for (;;) {
  3038. bool retry = false;
  3039. if (dict_fts_index_syncing(table)) {
  3040. retry = true;
  3041. }
  3042. if (!retry) {
  3043. break;
  3044. }
  3045. DICT_BG_YIELD(trx);
  3046. }
  3047. row_mysql_unlock_data_dictionary(trx);
  3048. fts_optimize_remove_table(table);
  3049. row_mysql_lock_data_dictionary(trx);
  3050. }
  3051. /* Do not bother to deal with persistent stats for temp
  3052. tables since we know temp tables do not use persistent
  3053. stats. */
  3054. if (!dict_table_is_temporary(table)) {
  3055. dict_stats_wait_bg_to_stop_using_table(
  3056. table, trx);
  3057. }
  3058. }
  3059. /* make sure background stats thread is not running on the table */
  3060. ut_ad(!(table->stats_bg_flag & BG_STAT_IN_PROGRESS));
  3061. /* Delete the link file if used. */
  3062. if (DICT_TF_HAS_DATA_DIR(table->flags)) {
  3063. fil_delete_link_file(name);
  3064. }
  3065. if (!dict_table_is_temporary(table)) {
  3066. dict_stats_recalc_pool_del(table);
  3067. /* Remove stats for this table and all of its indexes from the
  3068. persistent storage if it exists and if there are stats for this
  3069. table in there. This function creates its own trx and commits
  3070. it. */
  3071. char errstr[1024];
  3072. err = dict_stats_drop_table(name, errstr, sizeof(errstr));
  3073. if (err != DB_SUCCESS) {
  3074. ib_logf(IB_LOG_LEVEL_WARN, "%s", errstr);
  3075. }
  3076. }
  3077. /* Move the table the the non-LRU list so that it isn't
  3078. considered for eviction. */
  3079. if (table->can_be_evicted) {
  3080. dict_table_move_from_lru_to_non_lru(table);
  3081. }
  3082. dict_table_close(table, TRUE, FALSE);
  3083. /* Check if the table is referenced by foreign key constraints from
  3084. some other table (not the table itself) */
  3085. if (!srv_read_only_mode && trx->check_foreigns) {
  3086. for (dict_foreign_set::iterator it
  3087. = table->referenced_set.begin();
  3088. it != table->referenced_set.end();
  3089. ++it) {
  3090. foreign = *it;
  3091. const bool ref_ok = drop_db
  3092. && dict_tables_have_same_db(
  3093. name,
  3094. foreign->foreign_table_name_lookup);
  3095. if (foreign->foreign_table != table && !ref_ok) {
  3096. FILE* ef = dict_foreign_err_file;
  3097. /* We only allow dropping a referenced table
  3098. if FOREIGN_KEY_CHECKS is set to 0 */
  3099. err = DB_CANNOT_DROP_CONSTRAINT;
  3100. mutex_enter(&dict_foreign_err_mutex);
  3101. rewind(ef);
  3102. ut_print_timestamp(ef);
  3103. fputs(" Cannot drop table ", ef);
  3104. ut_print_name(ef, trx, TRUE, name);
  3105. fputs("\n"
  3106. "because it is referenced by ", ef);
  3107. ut_print_name(ef, trx, TRUE,
  3108. foreign->foreign_table_name);
  3109. putc('\n', ef);
  3110. mutex_exit(&dict_foreign_err_mutex);
  3111. goto funct_exit;
  3112. }
  3113. }
  3114. }
  3115. DBUG_EXECUTE_IF("row_drop_table_add_to_background",
  3116. row_add_table_to_background_drop_list(table->name);
  3117. err = DB_SUCCESS;
  3118. goto funct_exit;
  3119. );
  3120. /* TODO: could we replace the counter n_foreign_key_checks_running
  3121. with lock checks on the table? Acquire here an exclusive lock on the
  3122. table, and rewrite lock0lock.cc and the lock wait in srv0srv.cc so that
  3123. they can cope with the table having been dropped here? Foreign key
  3124. checks take an IS or IX lock on the table. */
  3125. if (table->n_foreign_key_checks_running > 0) {
  3126. const char* save_tablename = table->name;
  3127. ibool added;
  3128. added = row_add_table_to_background_drop_list(save_tablename);
  3129. if (added) {
  3130. ut_print_timestamp(stderr);
  3131. fputs(" InnoDB: You are trying to drop table ",
  3132. stderr);
  3133. ut_print_name(stderr, trx, TRUE, save_tablename);
  3134. fputs("\n"
  3135. "InnoDB: though there is a"
  3136. " foreign key check running on it.\n"
  3137. "InnoDB: Adding the table to"
  3138. " the background drop queue.\n",
  3139. stderr);
  3140. /* We return DB_SUCCESS to MySQL though the drop will
  3141. happen lazily later */
  3142. err = DB_SUCCESS;
  3143. } else {
  3144. /* The table is already in the background drop list */
  3145. err = DB_ERROR;
  3146. }
  3147. goto funct_exit;
  3148. }
  3149. /* Remove all locks that are on the table or its records, if there
  3150. are no refernces to the table but it has record locks, we release
  3151. the record locks unconditionally. One use case is:
  3152. CREATE TABLE t2 (PRIMARY KEY (a)) SELECT * FROM t1;
  3153. If after the user transaction has done the SELECT and there is a
  3154. problem in completing the CREATE TABLE operation, MySQL will drop
  3155. the table. InnoDB will create a new background transaction to do the
  3156. actual drop, the trx instance that is passed to this function. To
  3157. preserve existing behaviour we remove the locks but ideally we
  3158. shouldn't have to. There should never be record locks on a table
  3159. that is going to be dropped. */
  3160. if (table->n_ref_count == 0) {
  3161. lock_remove_all_on_table(table, TRUE);
  3162. ut_a(table->n_rec_locks == 0);
  3163. } else if (table->n_ref_count > 0 || table->n_rec_locks > 0) {
  3164. ibool added;
  3165. added = row_add_table_to_background_drop_list(table->name);
  3166. if (added) {
  3167. ut_print_timestamp(stderr);
  3168. fputs(" InnoDB: Warning: MySQL is"
  3169. " trying to drop table ", stderr);
  3170. ut_print_name(stderr, trx, TRUE, table->name);
  3171. fputs("\n"
  3172. "InnoDB: though there are still"
  3173. " open handles to it.\n"
  3174. "InnoDB: Adding the table to the"
  3175. " background drop queue.\n",
  3176. stderr);
  3177. /* We return DB_SUCCESS to MySQL though the drop will
  3178. happen lazily later */
  3179. err = DB_SUCCESS;
  3180. } else {
  3181. /* The table is already in the background drop list */
  3182. err = DB_ERROR;
  3183. }
  3184. goto funct_exit;
  3185. }
  3186. /* The "to_be_dropped" marks table that is to be dropped, but
  3187. has not been dropped, instead, was put in the background drop
  3188. list due to being used by concurrent DML operations. Clear it
  3189. here since there are no longer any concurrent activities on it,
  3190. and it is free to be dropped */
  3191. table->to_be_dropped = false;
  3192. /* If we get this far then the table to be dropped must not have
  3193. any table or record locks on it. */
  3194. ut_a(!lock_table_has_locks(table));
  3195. switch (trx_get_dict_operation(trx)) {
  3196. case TRX_DICT_OP_NONE:
  3197. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  3198. trx->table_id = table->id;
  3199. case TRX_DICT_OP_TABLE:
  3200. break;
  3201. case TRX_DICT_OP_INDEX:
  3202. /* If the transaction was previously flagged as
  3203. TRX_DICT_OP_INDEX, we should be dropping auxiliary
  3204. tables for full-text indexes. */
  3205. ut_ad(strstr(table->name, "/FTS_") != NULL);
  3206. }
  3207. /* Mark all indexes unavailable in the data dictionary cache
  3208. before starting to drop the table. */
  3209. unsigned* page_no;
  3210. unsigned* page_nos;
  3211. heap = mem_heap_create(
  3212. 200 + UT_LIST_GET_LEN(table->indexes) * sizeof *page_nos);
  3213. tablename = mem_heap_strdup(heap, name);
  3214. page_no = page_nos = static_cast<unsigned*>(
  3215. mem_heap_alloc(
  3216. heap,
  3217. UT_LIST_GET_LEN(table->indexes) * sizeof *page_no));
  3218. for (dict_index_t* index = dict_table_get_first_index(table);
  3219. index != NULL;
  3220. index = dict_table_get_next_index(index)) {
  3221. rw_lock_x_lock(dict_index_get_lock(index));
  3222. /* Save the page numbers so that we can restore them
  3223. if the operation fails. */
  3224. *page_no++ = index->page;
  3225. /* Mark the index unusable. */
  3226. index->page = FIL_NULL;
  3227. rw_lock_x_unlock(dict_index_get_lock(index));
  3228. }
  3229. /* We use the private SQL parser of Innobase to generate the
  3230. query graphs needed in deleting the dictionary data from system
  3231. tables in Innobase. Deleting a row from SYS_INDEXES table also
  3232. frees the file segments of the B-tree associated with the index. */
  3233. info = pars_info_create();
  3234. pars_info_add_str_literal(info, "table_name", name);
  3235. err = que_eval_sql(info,
  3236. "PROCEDURE DROP_TABLE_PROC () IS\n"
  3237. "sys_foreign_id CHAR;\n"
  3238. "table_id CHAR;\n"
  3239. "index_id CHAR;\n"
  3240. "foreign_id CHAR;\n"
  3241. "space_id INT;\n"
  3242. "found INT;\n"
  3243. "DECLARE CURSOR cur_fk IS\n"
  3244. "SELECT ID FROM SYS_FOREIGN\n"
  3245. "WHERE FOR_NAME = :table_name\n"
  3246. "AND TO_BINARY(FOR_NAME)\n"
  3247. " = TO_BINARY(:table_name)\n"
  3248. "LOCK IN SHARE MODE;\n"
  3249. "DECLARE CURSOR cur_idx IS\n"
  3250. "SELECT ID FROM SYS_INDEXES\n"
  3251. "WHERE TABLE_ID = table_id\n"
  3252. "LOCK IN SHARE MODE;\n"
  3253. "BEGIN\n"
  3254. "SELECT ID INTO table_id\n"
  3255. "FROM SYS_TABLES\n"
  3256. "WHERE NAME = :table_name\n"
  3257. "LOCK IN SHARE MODE;\n"
  3258. "IF (SQL % NOTFOUND) THEN\n"
  3259. " RETURN;\n"
  3260. "END IF;\n"
  3261. "SELECT SPACE INTO space_id\n"
  3262. "FROM SYS_TABLES\n"
  3263. "WHERE NAME = :table_name;\n"
  3264. "IF (SQL % NOTFOUND) THEN\n"
  3265. " RETURN;\n"
  3266. "END IF;\n"
  3267. "found := 1;\n"
  3268. "SELECT ID INTO sys_foreign_id\n"
  3269. "FROM SYS_TABLES\n"
  3270. "WHERE NAME = 'SYS_FOREIGN'\n"
  3271. "LOCK IN SHARE MODE;\n"
  3272. "IF (SQL % NOTFOUND) THEN\n"
  3273. " found := 0;\n"
  3274. "END IF;\n"
  3275. "IF (:table_name = 'SYS_FOREIGN') THEN\n"
  3276. " found := 0;\n"
  3277. "END IF;\n"
  3278. "IF (:table_name = 'SYS_FOREIGN_COLS') THEN\n"
  3279. " found := 0;\n"
  3280. "END IF;\n"
  3281. "OPEN cur_fk;\n"
  3282. "WHILE found = 1 LOOP\n"
  3283. " FETCH cur_fk INTO foreign_id;\n"
  3284. " IF (SQL % NOTFOUND) THEN\n"
  3285. " found := 0;\n"
  3286. " ELSE\n"
  3287. " DELETE FROM SYS_FOREIGN_COLS\n"
  3288. " WHERE ID = foreign_id;\n"
  3289. " DELETE FROM SYS_FOREIGN\n"
  3290. " WHERE ID = foreign_id;\n"
  3291. " END IF;\n"
  3292. "END LOOP;\n"
  3293. "CLOSE cur_fk;\n"
  3294. "found := 1;\n"
  3295. "OPEN cur_idx;\n"
  3296. "WHILE found = 1 LOOP\n"
  3297. " FETCH cur_idx INTO index_id;\n"
  3298. " IF (SQL % NOTFOUND) THEN\n"
  3299. " found := 0;\n"
  3300. " ELSE\n"
  3301. " DELETE FROM SYS_FIELDS\n"
  3302. " WHERE INDEX_ID = index_id;\n"
  3303. " DELETE FROM SYS_INDEXES\n"
  3304. " WHERE ID = index_id\n"
  3305. " AND TABLE_ID = table_id;\n"
  3306. " END IF;\n"
  3307. "END LOOP;\n"
  3308. "CLOSE cur_idx;\n"
  3309. "DELETE FROM SYS_TABLESPACES\n"
  3310. "WHERE SPACE = space_id;\n"
  3311. "DELETE FROM SYS_DATAFILES\n"
  3312. "WHERE SPACE = space_id;\n"
  3313. "DELETE FROM SYS_COLUMNS\n"
  3314. "WHERE TABLE_ID = table_id;\n"
  3315. "DELETE FROM SYS_TABLES\n"
  3316. "WHERE NAME = :table_name;\n"
  3317. "END;\n"
  3318. , FALSE, trx);
  3319. switch (err) {
  3320. ibool is_temp;
  3321. case DB_SUCCESS:
  3322. /* Clone the name, in case it has been allocated
  3323. from table->heap, which will be freed by
  3324. dict_table_remove_from_cache(table) below. */
  3325. space_id = table->space;
  3326. ibd_file_missing = table->ibd_file_missing;
  3327. is_temp = DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY);
  3328. /* If there is a temp path then the temp flag is set.
  3329. However, during recovery or reloading the table object
  3330. after eviction from data dictionary cache, we might
  3331. have a temp flag but not know the temp path */
  3332. ut_a(table->dir_path_of_temp_table == NULL || is_temp);
  3333. if (dict_table_is_discarded(table)
  3334. || table->ibd_file_missing) {
  3335. /* Do not attempt to drop known-to-be-missing
  3336. tablespaces. */
  3337. space_id = 0;
  3338. }
  3339. /* We do not allow temporary tables with a remote path. */
  3340. ut_a(!(is_temp && DICT_TF_HAS_DATA_DIR(table->flags)));
  3341. if (space_id && DICT_TF_HAS_DATA_DIR(table->flags)) {
  3342. dict_get_and_save_data_dir_path(table, true);
  3343. ut_a(table->data_dir_path);
  3344. filepath = os_file_make_remote_pathname(
  3345. table->data_dir_path, table->name, "ibd");
  3346. } else if (table->dir_path_of_temp_table) {
  3347. filepath = fil_make_ibd_name(
  3348. table->dir_path_of_temp_table, true);
  3349. } else {
  3350. filepath = fil_make_ibd_name(tablename, false);
  3351. }
  3352. if (dict_table_has_fts_index(table)
  3353. || DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) {
  3354. ut_ad(table->n_ref_count == 0);
  3355. ut_ad(trx->state != TRX_STATE_NOT_STARTED);
  3356. err = fts_drop_tables(trx, table);
  3357. if (err != DB_SUCCESS) {
  3358. ut_print_timestamp(stderr);
  3359. fprintf(stderr," InnoDB: Error: (%s) not "
  3360. "able to remove ancillary FTS tables "
  3361. "for table ", ut_strerr(err));
  3362. ut_print_name(stderr, trx, TRUE, tablename);
  3363. fputs("\n", stderr);
  3364. goto funct_exit;
  3365. }
  3366. }
  3367. /* The table->fts flag can be set on the table for which
  3368. the cluster index is being rebuilt. Such table might not have
  3369. DICT_TF2_FTS flag set. So keep this out of above
  3370. dict_table_has_fts_index condition */
  3371. if (table->fts) {
  3372. /* Need to set TABLE_DICT_LOCKED bit, since
  3373. fts_que_graph_free_check_lock would try to acquire
  3374. dict mutex lock */
  3375. table->fts->fts_status |= TABLE_DICT_LOCKED;
  3376. fts_free(table);
  3377. }
  3378. dict_table_remove_from_cache(table);
  3379. if (dict_load_table(tablename, TRUE,
  3380. DICT_ERR_IGNORE_NONE) != NULL) {
  3381. ut_print_timestamp(stderr);
  3382. fputs(" InnoDB: Error: not able to remove table ",
  3383. stderr);
  3384. ut_print_name(stderr, trx, TRUE, tablename);
  3385. fputs(" from the dictionary cache!\n", stderr);
  3386. err = DB_ERROR;
  3387. }
  3388. /* Do not drop possible .ibd tablespace if something went
  3389. wrong: we do not want to delete valuable data of the user */
  3390. /* Don't spam the log if we can't find the tablespace of
  3391. a temp table or if the tablesace has been discarded. */
  3392. print_msg = !(is_temp || ibd_file_missing);
  3393. if (err == DB_SUCCESS && space_id > TRX_SYS_SPACE) {
  3394. if (!is_temp
  3395. && !fil_space_for_table_exists_in_mem(
  3396. space_id, tablename, FALSE,
  3397. print_msg, false, NULL, 0)) {
  3398. /* This might happen if we are dropping a
  3399. discarded tablespace */
  3400. err = DB_SUCCESS;
  3401. if (print_msg) {
  3402. char msg_tablename[MAX_FULL_NAME_LEN + 1];
  3403. innobase_format_name(
  3404. msg_tablename, sizeof(tablename),
  3405. tablename, FALSE);
  3406. ib_logf(IB_LOG_LEVEL_INFO,
  3407. "Removed the table %s from "
  3408. "InnoDB's data dictionary",
  3409. msg_tablename);
  3410. }
  3411. /* Force a delete of any discarded
  3412. or temporary files. */
  3413. fil_delete_file(filepath);
  3414. } else if (fil_delete_tablespace(
  3415. space_id,
  3416. BUF_REMOVE_FLUSH_NO_WRITE)
  3417. != DB_SUCCESS) {
  3418. fprintf(stderr,
  3419. "InnoDB: We removed now the InnoDB"
  3420. " internal data dictionary entry\n"
  3421. "InnoDB: of table ");
  3422. ut_print_name(stderr, trx, TRUE, tablename);
  3423. fprintf(stderr, ".\n");
  3424. ut_print_timestamp(stderr);
  3425. fprintf(stderr,
  3426. " InnoDB: Error: not able to"
  3427. " delete tablespace %lu of table ",
  3428. (ulong) space_id);
  3429. ut_print_name(stderr, trx, TRUE, tablename);
  3430. fputs("!\n", stderr);
  3431. err = DB_ERROR;
  3432. }
  3433. }
  3434. break;
  3435. case DB_OUT_OF_FILE_SPACE:
  3436. err = DB_MUST_GET_MORE_FILE_SPACE;
  3437. row_mysql_handle_errors(&err, trx, NULL, NULL);
  3438. /* raise error */
  3439. ut_error;
  3440. break;
  3441. case DB_TOO_MANY_CONCURRENT_TRXS:
  3442. /* Cannot even find a free slot for the
  3443. the undo log. We can directly exit here
  3444. and return the DB_TOO_MANY_CONCURRENT_TRXS
  3445. error. */
  3446. default:
  3447. /* This is some error we do not expect. Print
  3448. the error number and rollback transaction */
  3449. ut_print_timestamp(stderr);
  3450. fprintf(stderr, "InnoDB: unknown error code %lu"
  3451. " while dropping table:", (ulong) err);
  3452. ut_print_name(stderr, trx, TRUE, tablename);
  3453. fprintf(stderr, ".\n");
  3454. trx->error_state = DB_SUCCESS;
  3455. trx_rollback_to_savepoint(trx, NULL);
  3456. trx->error_state = DB_SUCCESS;
  3457. /* Mark all indexes available in the data dictionary
  3458. cache again. */
  3459. page_no = page_nos;
  3460. for (dict_index_t* index = dict_table_get_first_index(table);
  3461. index != NULL;
  3462. index = dict_table_get_next_index(index)) {
  3463. rw_lock_x_lock(dict_index_get_lock(index));
  3464. ut_a(index->page == FIL_NULL);
  3465. index->page = *page_no++;
  3466. rw_lock_x_unlock(dict_index_get_lock(index));
  3467. }
  3468. }
  3469. funct_exit:
  3470. if (heap) {
  3471. mem_heap_free(heap);
  3472. }
  3473. if (filepath) {
  3474. mem_free(filepath);
  3475. }
  3476. if (locked_dictionary) {
  3477. trx_commit_for_mysql(trx);
  3478. row_mysql_unlock_data_dictionary(trx);
  3479. }
  3480. trx->op_info = "";
  3481. srv_wake_master_thread();
  3482. DBUG_RETURN(err);
  3483. }
  3484. /*********************************************************************//**
  3485. Drop all temporary tables during crash recovery. */
  3486. UNIV_INTERN
  3487. void
  3488. row_mysql_drop_temp_tables(void)
  3489. /*============================*/
  3490. {
  3491. trx_t* trx;
  3492. btr_pcur_t pcur;
  3493. mtr_t mtr;
  3494. mem_heap_t* heap;
  3495. trx = trx_allocate_for_background();
  3496. trx->op_info = "dropping temporary tables";
  3497. row_mysql_lock_data_dictionary(trx);
  3498. heap = mem_heap_create(200);
  3499. mtr_start(&mtr);
  3500. btr_pcur_open_at_index_side(
  3501. true,
  3502. dict_table_get_first_index(dict_sys->sys_tables),
  3503. BTR_SEARCH_LEAF, &pcur, true, 0, &mtr);
  3504. for (;;) {
  3505. const rec_t* rec;
  3506. const byte* field;
  3507. ulint len;
  3508. const char* table_name;
  3509. dict_table_t* table;
  3510. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  3511. if (!btr_pcur_is_on_user_rec(&pcur)) {
  3512. break;
  3513. }
  3514. /* The high order bit of N_COLS is set unless
  3515. ROW_FORMAT=REDUNDANT. */
  3516. rec = btr_pcur_get_rec(&pcur);
  3517. field = rec_get_nth_field_old(
  3518. rec, DICT_FLD__SYS_TABLES__NAME, &len);
  3519. field = rec_get_nth_field_old(
  3520. rec, DICT_FLD__SYS_TABLES__N_COLS, &len);
  3521. if (len != 4
  3522. || !(mach_read_from_4(field) & DICT_N_COLS_COMPACT)) {
  3523. continue;
  3524. }
  3525. /* Older versions of InnoDB, which only supported tables
  3526. in ROW_FORMAT=REDUNDANT could write garbage to
  3527. SYS_TABLES.MIX_LEN, where we now store the is_temp flag.
  3528. Above, we assumed is_temp=0 if ROW_FORMAT=REDUNDANT. */
  3529. field = rec_get_nth_field_old(
  3530. rec, DICT_FLD__SYS_TABLES__MIX_LEN, &len);
  3531. if (len != 4
  3532. || !(mach_read_from_4(field) & DICT_TF2_TEMPORARY)) {
  3533. continue;
  3534. }
  3535. /* This is a temporary table. */
  3536. field = rec_get_nth_field_old(
  3537. rec, DICT_FLD__SYS_TABLES__NAME, &len);
  3538. if (len == UNIV_SQL_NULL || len == 0) {
  3539. /* Corrupted SYS_TABLES.NAME */
  3540. continue;
  3541. }
  3542. table_name = mem_heap_strdupl(heap, (const char*) field, len);
  3543. btr_pcur_store_position(&pcur, &mtr);
  3544. btr_pcur_commit_specify_mtr(&pcur, &mtr);
  3545. table = dict_load_table(table_name, TRUE, DICT_ERR_IGNORE_NONE);
  3546. if (table) {
  3547. row_drop_table_for_mysql(table_name, trx, FALSE);
  3548. trx_commit_for_mysql(trx);
  3549. }
  3550. mtr_start(&mtr);
  3551. btr_pcur_restore_position(BTR_SEARCH_LEAF,
  3552. &pcur, &mtr);
  3553. }
  3554. btr_pcur_close(&pcur);
  3555. mtr_commit(&mtr);
  3556. mem_heap_free(heap);
  3557. row_mysql_unlock_data_dictionary(trx);
  3558. trx_free_for_background(trx);
  3559. }
  3560. /*******************************************************************//**
  3561. Drop all foreign keys in a database, see Bug#18942.
  3562. Called at the end of row_drop_database_for_mysql().
  3563. @return error code or DB_SUCCESS */
  3564. static MY_ATTRIBUTE((nonnull, warn_unused_result))
  3565. dberr_t
  3566. drop_all_foreign_keys_in_db(
  3567. /*========================*/
  3568. const char* name, /*!< in: database name which ends to '/' */
  3569. trx_t* trx) /*!< in: transaction handle */
  3570. {
  3571. pars_info_t* pinfo;
  3572. dberr_t err;
  3573. ut_a(name[strlen(name) - 1] == '/');
  3574. pinfo = pars_info_create();
  3575. pars_info_add_str_literal(pinfo, "dbname", name);
  3576. /** true if for_name is not prefixed with dbname */
  3577. #define TABLE_NOT_IN_THIS_DB \
  3578. "SUBSTR(for_name, 0, LENGTH(:dbname)) <> :dbname"
  3579. err = que_eval_sql(pinfo,
  3580. "PROCEDURE DROP_ALL_FOREIGN_KEYS_PROC () IS\n"
  3581. "foreign_id CHAR;\n"
  3582. "for_name CHAR;\n"
  3583. "found INT;\n"
  3584. "DECLARE CURSOR cur IS\n"
  3585. "SELECT ID, FOR_NAME FROM SYS_FOREIGN\n"
  3586. "WHERE FOR_NAME >= :dbname\n"
  3587. "LOCK IN SHARE MODE\n"
  3588. "ORDER BY FOR_NAME;\n"
  3589. "BEGIN\n"
  3590. "found := 1;\n"
  3591. "OPEN cur;\n"
  3592. "WHILE found = 1 LOOP\n"
  3593. " FETCH cur INTO foreign_id, for_name;\n"
  3594. " IF (SQL % NOTFOUND) THEN\n"
  3595. " found := 0;\n"
  3596. " ELSIF (" TABLE_NOT_IN_THIS_DB ") THEN\n"
  3597. " found := 0;\n"
  3598. " ELSIF (1=1) THEN\n"
  3599. " DELETE FROM SYS_FOREIGN_COLS\n"
  3600. " WHERE ID = foreign_id;\n"
  3601. " DELETE FROM SYS_FOREIGN\n"
  3602. " WHERE ID = foreign_id;\n"
  3603. " END IF;\n"
  3604. "END LOOP;\n"
  3605. "CLOSE cur;\n"
  3606. "COMMIT WORK;\n"
  3607. "END;\n",
  3608. FALSE, /* do not reserve dict mutex,
  3609. we are already holding it */
  3610. trx);
  3611. return(err);
  3612. }
  3613. /*********************************************************************//**
  3614. Drops a database for MySQL.
  3615. @return error code or DB_SUCCESS */
  3616. UNIV_INTERN
  3617. dberr_t
  3618. row_drop_database_for_mysql(
  3619. /*========================*/
  3620. const char* name, /*!< in: database name which ends to '/' */
  3621. trx_t* trx) /*!< in: transaction handle */
  3622. {
  3623. dict_table_t* table;
  3624. char* table_name;
  3625. dberr_t err = DB_SUCCESS;
  3626. ulint namelen = strlen(name);
  3627. ut_a(name != NULL);
  3628. ut_a(name[namelen - 1] == '/');
  3629. trx->op_info = "dropping database";
  3630. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  3631. trx_start_if_not_started_xa(trx);
  3632. loop:
  3633. row_mysql_lock_data_dictionary(trx);
  3634. while ((table_name = dict_get_first_table_name_in_db(name))) {
  3635. /* Drop parent table if it is a fts aux table, to
  3636. avoid accessing dropped fts aux tables in information
  3637. scheam when parent table still exists.
  3638. Note: Drop parent table will drop fts aux tables. */
  3639. char* parent_table_name;
  3640. parent_table_name = fts_get_parent_table_name(
  3641. table_name, strlen(table_name));
  3642. if (parent_table_name != NULL) {
  3643. mem_free(table_name);
  3644. table_name = parent_table_name;
  3645. }
  3646. ut_a(memcmp(table_name, name, namelen) == 0);
  3647. table = dict_table_open_on_name(
  3648. table_name, TRUE, FALSE, static_cast<dict_err_ignore_t>(
  3649. DICT_ERR_IGNORE_INDEX_ROOT
  3650. | DICT_ERR_IGNORE_CORRUPT));
  3651. if (!table) {
  3652. ib_logf(IB_LOG_LEVEL_ERROR,
  3653. "Cannot load table %s from InnoDB internal "
  3654. "data dictionary during drop database",
  3655. table_name);
  3656. mem_free(table_name);
  3657. err = DB_TABLE_NOT_FOUND;
  3658. break;
  3659. }
  3660. if (!row_is_mysql_tmp_table_name(table->name)) {
  3661. /* There could be orphan temp tables left from
  3662. interrupted alter table. Leave them, and handle
  3663. the rest.*/
  3664. if (table->can_be_evicted) {
  3665. ib_logf(IB_LOG_LEVEL_WARN,
  3666. "Orphan table encountered during "
  3667. "DROP DATABASE. This is possible if "
  3668. "'%s.frm' was lost.", table->name);
  3669. }
  3670. if (table->ibd_file_missing) {
  3671. ib_logf(IB_LOG_LEVEL_WARN,
  3672. "Missing %s.ibd file for table %s.",
  3673. table->name, table->name);
  3674. }
  3675. }
  3676. dict_table_close(table, TRUE, FALSE);
  3677. /* The dict_table_t object must not be accessed before
  3678. dict_table_open() or after dict_table_close(). But this is OK
  3679. if we are holding, the dict_sys->mutex. */
  3680. ut_ad(mutex_own(&dict_sys->mutex));
  3681. /* Wait until MySQL does not have any queries running on
  3682. the table */
  3683. if (table->n_ref_count > 0) {
  3684. row_mysql_unlock_data_dictionary(trx);
  3685. ut_print_timestamp(stderr);
  3686. fputs(" InnoDB: Warning: MySQL is trying to"
  3687. " drop database ", stderr);
  3688. ut_print_name(stderr, trx, TRUE, name);
  3689. fputs("\n"
  3690. "InnoDB: though there are still"
  3691. " open handles to table ", stderr);
  3692. ut_print_name(stderr, trx, TRUE, table_name);
  3693. fputs(".\n", stderr);
  3694. os_thread_sleep(1000000);
  3695. mem_free(table_name);
  3696. goto loop;
  3697. }
  3698. err = row_drop_table_for_mysql(table_name, trx, TRUE);
  3699. trx_commit_for_mysql(trx);
  3700. if (err != DB_SUCCESS) {
  3701. fputs("InnoDB: DROP DATABASE ", stderr);
  3702. ut_print_name(stderr, trx, TRUE, name);
  3703. fprintf(stderr, " failed with error (%s) for table ",
  3704. ut_strerr(err));
  3705. ut_print_name(stderr, trx, TRUE, table_name);
  3706. putc('\n', stderr);
  3707. mem_free(table_name);
  3708. break;
  3709. }
  3710. mem_free(table_name);
  3711. }
  3712. if (err == DB_SUCCESS) {
  3713. /* after dropping all tables try to drop all leftover
  3714. foreign keys in case orphaned ones exist */
  3715. err = drop_all_foreign_keys_in_db(name, trx);
  3716. if (err != DB_SUCCESS) {
  3717. fputs("InnoDB: DROP DATABASE ", stderr);
  3718. ut_print_name(stderr, trx, TRUE, name);
  3719. fprintf(stderr, " failed with error %d while "
  3720. "dropping all foreign keys", err);
  3721. }
  3722. }
  3723. trx_commit_for_mysql(trx);
  3724. row_mysql_unlock_data_dictionary(trx);
  3725. trx->op_info = "";
  3726. return(err);
  3727. }
  3728. /*********************************************************************//**
  3729. Checks if a table name contains the string "/#sql" which denotes temporary
  3730. tables in MySQL.
  3731. @return true if temporary table */
  3732. UNIV_INTERN MY_ATTRIBUTE((warn_unused_result))
  3733. bool
  3734. row_is_mysql_tmp_table_name(
  3735. /*========================*/
  3736. const char* name) /*!< in: table name in the form
  3737. 'database/tablename' */
  3738. {
  3739. return(strstr(name, "/#sql") != NULL);
  3740. /* return(strstr(name, "/@0023sql") != NULL); */
  3741. }
  3742. /****************************************************************//**
  3743. Delete a single constraint.
  3744. @return error code or DB_SUCCESS */
  3745. static MY_ATTRIBUTE((nonnull, warn_unused_result))
  3746. dberr_t
  3747. row_delete_constraint_low(
  3748. /*======================*/
  3749. const char* id, /*!< in: constraint id */
  3750. trx_t* trx) /*!< in: transaction handle */
  3751. {
  3752. pars_info_t* info = pars_info_create();
  3753. pars_info_add_str_literal(info, "id", id);
  3754. return(que_eval_sql(info,
  3755. "PROCEDURE DELETE_CONSTRAINT () IS\n"
  3756. "BEGIN\n"
  3757. "DELETE FROM SYS_FOREIGN_COLS WHERE ID = :id;\n"
  3758. "DELETE FROM SYS_FOREIGN WHERE ID = :id;\n"
  3759. "END;\n"
  3760. , FALSE, trx));
  3761. }
  3762. /****************************************************************//**
  3763. Delete a single constraint.
  3764. @return error code or DB_SUCCESS */
  3765. static MY_ATTRIBUTE((nonnull, warn_unused_result))
  3766. dberr_t
  3767. row_delete_constraint(
  3768. /*==================*/
  3769. const char* id, /*!< in: constraint id */
  3770. const char* database_name, /*!< in: database name, with the
  3771. trailing '/' */
  3772. mem_heap_t* heap, /*!< in: memory heap */
  3773. trx_t* trx) /*!< in: transaction handle */
  3774. {
  3775. dberr_t err;
  3776. /* New format constraints have ids <databasename>/<constraintname>. */
  3777. err = row_delete_constraint_low(
  3778. mem_heap_strcat(heap, database_name, id), trx);
  3779. if ((err == DB_SUCCESS) && !strchr(id, '/')) {
  3780. /* Old format < 4.0.18 constraints have constraint ids
  3781. NUMBER_NUMBER. We only try deleting them if the
  3782. constraint name does not contain a '/' character, otherwise
  3783. deleting a new format constraint named 'foo/bar' from
  3784. database 'baz' would remove constraint 'bar' from database
  3785. 'foo', if it existed. */
  3786. err = row_delete_constraint_low(id, trx);
  3787. }
  3788. return(err);
  3789. }
  3790. /*********************************************************************//**
  3791. Renames a table for MySQL.
  3792. @return error code or DB_SUCCESS */
  3793. UNIV_INTERN
  3794. dberr_t
  3795. row_rename_table_for_mysql(
  3796. /*=======================*/
  3797. const char* old_name, /*!< in: old table name */
  3798. const char* new_name, /*!< in: new table name */
  3799. trx_t* trx, /*!< in/out: transaction */
  3800. bool commit) /*!< in: whether to commit trx */
  3801. {
  3802. dict_table_t* table = NULL;
  3803. ibool dict_locked = FALSE;
  3804. dberr_t err = DB_ERROR;
  3805. mem_heap_t* heap = NULL;
  3806. const char** constraints_to_drop = NULL;
  3807. ulint n_constraints_to_drop = 0;
  3808. ibool old_is_tmp, new_is_tmp;
  3809. pars_info_t* info = NULL;
  3810. int retry;
  3811. bool aux_fts_rename = false;
  3812. ut_a(old_name != NULL);
  3813. ut_a(new_name != NULL);
  3814. ut_ad(trx->state == TRX_STATE_ACTIVE);
  3815. if (srv_force_recovery) {
  3816. fputs("InnoDB: innodb_force_recovery is on: we do not allow\n"
  3817. "InnoDB: database modifications by the user. Shut down\n"
  3818. "InnoDB: mysqld and edit my.cnf so that"
  3819. "InnoDB: innodb_force_... is removed.\n",
  3820. stderr);
  3821. err = DB_READ_ONLY;
  3822. goto funct_exit;
  3823. } else if (row_mysql_is_system_table(new_name)) {
  3824. fprintf(stderr,
  3825. "InnoDB: Error: trying to create a MySQL"
  3826. " system table %s of type InnoDB.\n"
  3827. "InnoDB: MySQL system tables must be"
  3828. " of the MyISAM type!\n",
  3829. new_name);
  3830. goto funct_exit;
  3831. }
  3832. trx->op_info = "renaming table";
  3833. old_is_tmp = row_is_mysql_tmp_table_name(old_name);
  3834. new_is_tmp = row_is_mysql_tmp_table_name(new_name);
  3835. dict_locked = trx->dict_operation_lock_mode == RW_X_LATCH;
  3836. table = dict_table_open_on_name(old_name, dict_locked, FALSE,
  3837. DICT_ERR_IGNORE_NONE);
  3838. if (!table) {
  3839. err = DB_TABLE_NOT_FOUND;
  3840. ut_print_timestamp(stderr);
  3841. fputs(" InnoDB: Error: table ", stderr);
  3842. ut_print_name(stderr, trx, TRUE, old_name);
  3843. fputs(" does not exist in the InnoDB internal\n"
  3844. "InnoDB: data dictionary though MySQL is"
  3845. " trying to rename the table.\n"
  3846. "InnoDB: Have you copied the .frm file"
  3847. " of the table to the\n"
  3848. "InnoDB: MySQL database directory"
  3849. " from another database?\n"
  3850. "InnoDB: You can look for further help from\n"
  3851. "InnoDB: " REFMAN "innodb-troubleshooting.html\n",
  3852. stderr);
  3853. goto funct_exit;
  3854. } else if (table->ibd_file_missing
  3855. && !dict_table_is_discarded(table)) {
  3856. err = DB_TABLE_NOT_FOUND;
  3857. ib_logf(IB_LOG_LEVEL_ERROR,
  3858. "Table %s does not have an .ibd file in the database "
  3859. "directory. See " REFMAN "innodb-troubleshooting.html",
  3860. old_name);
  3861. goto funct_exit;
  3862. } else if (new_is_tmp) {
  3863. /* MySQL is doing an ALTER TABLE command and it renames the
  3864. original table to a temporary table name. We want to preserve
  3865. the original foreign key constraint definitions despite the
  3866. name change. An exception is those constraints for which
  3867. the ALTER TABLE contained DROP FOREIGN KEY <foreign key id>.*/
  3868. heap = mem_heap_create(100);
  3869. err = dict_foreign_parse_drop_constraints(
  3870. heap, trx, table, &n_constraints_to_drop,
  3871. &constraints_to_drop);
  3872. if (err != DB_SUCCESS) {
  3873. goto funct_exit;
  3874. }
  3875. }
  3876. /* Is a foreign key check running on this table? */
  3877. for (retry = 0; retry < 100
  3878. && table->n_foreign_key_checks_running > 0; ++retry) {
  3879. row_mysql_unlock_data_dictionary(trx);
  3880. os_thread_yield();
  3881. row_mysql_lock_data_dictionary(trx);
  3882. }
  3883. if (table->n_foreign_key_checks_running > 0) {
  3884. ut_print_timestamp(stderr);
  3885. fputs(" InnoDB: Error: in ALTER TABLE ", stderr);
  3886. ut_print_name(stderr, trx, TRUE, old_name);
  3887. fprintf(stderr, "\n"
  3888. "InnoDB: a FOREIGN KEY check is running.\n"
  3889. "InnoDB: Cannot rename table.\n");
  3890. err = DB_TABLE_IN_FK_CHECK;
  3891. goto funct_exit;
  3892. }
  3893. /* We use the private SQL parser of Innobase to generate the query
  3894. graphs needed in updating the dictionary data from system tables. */
  3895. info = pars_info_create();
  3896. pars_info_add_str_literal(info, "new_table_name", new_name);
  3897. pars_info_add_str_literal(info, "old_table_name", old_name);
  3898. err = que_eval_sql(info,
  3899. "PROCEDURE RENAME_TABLE () IS\n"
  3900. "BEGIN\n"
  3901. "UPDATE SYS_TABLES"
  3902. " SET NAME = :new_table_name\n"
  3903. " WHERE NAME = :old_table_name;\n"
  3904. "END;\n"
  3905. , FALSE, trx);
  3906. /* SYS_TABLESPACES and SYS_DATAFILES track non-system tablespaces
  3907. which have space IDs > 0. */
  3908. if (err == DB_SUCCESS
  3909. && table->space != TRX_SYS_SPACE
  3910. && !table->ibd_file_missing) {
  3911. /* Make a new pathname to update SYS_DATAFILES. */
  3912. char* new_path = row_make_new_pathname(table, new_name);
  3913. info = pars_info_create();
  3914. pars_info_add_str_literal(info, "new_table_name", new_name);
  3915. pars_info_add_str_literal(info, "new_path_name", new_path);
  3916. pars_info_add_int4_literal(info, "space_id", table->space);
  3917. err = que_eval_sql(info,
  3918. "PROCEDURE RENAME_SPACE () IS\n"
  3919. "BEGIN\n"
  3920. "UPDATE SYS_TABLESPACES"
  3921. " SET NAME = :new_table_name\n"
  3922. " WHERE SPACE = :space_id;\n"
  3923. "UPDATE SYS_DATAFILES"
  3924. " SET PATH = :new_path_name\n"
  3925. " WHERE SPACE = :space_id;\n"
  3926. "END;\n"
  3927. , FALSE, trx);
  3928. mem_free(new_path);
  3929. }
  3930. if (err != DB_SUCCESS) {
  3931. goto end;
  3932. }
  3933. if (!new_is_tmp) {
  3934. /* Rename all constraints. */
  3935. char new_table_name[MAX_TABLE_NAME_LEN] = "";
  3936. char old_table_utf8[MAX_TABLE_NAME_LEN] = "";
  3937. uint errors = 0;
  3938. strncpy(old_table_utf8, old_name, MAX_TABLE_NAME_LEN);
  3939. innobase_convert_to_system_charset(
  3940. strchr(old_table_utf8, '/') + 1,
  3941. strchr(old_name, '/') +1,
  3942. MAX_TABLE_NAME_LEN, &errors);
  3943. if (errors) {
  3944. /* Table name could not be converted from charset
  3945. my_charset_filename to UTF-8. This means that the
  3946. table name is already in UTF-8 (#mysql#50). */
  3947. strncpy(old_table_utf8, old_name, MAX_TABLE_NAME_LEN);
  3948. }
  3949. info = pars_info_create();
  3950. pars_info_add_str_literal(info, "new_table_name", new_name);
  3951. pars_info_add_str_literal(info, "old_table_name", old_name);
  3952. pars_info_add_str_literal(info, "old_table_name_utf8",
  3953. old_table_utf8);
  3954. strncpy(new_table_name, new_name, MAX_TABLE_NAME_LEN);
  3955. innobase_convert_to_system_charset(
  3956. strchr(new_table_name, '/') + 1,
  3957. strchr(new_name, '/') +1,
  3958. MAX_TABLE_NAME_LEN, &errors);
  3959. if (errors) {
  3960. /* Table name could not be converted from charset
  3961. my_charset_filename to UTF-8. This means that the
  3962. table name is already in UTF-8 (#mysql#50). */
  3963. strncpy(new_table_name, new_name, MAX_TABLE_NAME_LEN);
  3964. }
  3965. pars_info_add_str_literal(info, "new_table_utf8", new_table_name);
  3966. err = que_eval_sql(
  3967. info,
  3968. "PROCEDURE RENAME_CONSTRAINT_IDS () IS\n"
  3969. "gen_constr_prefix CHAR;\n"
  3970. "new_db_name CHAR;\n"
  3971. "foreign_id CHAR;\n"
  3972. "new_foreign_id CHAR;\n"
  3973. "old_db_name_len INT;\n"
  3974. "old_t_name_len INT;\n"
  3975. "new_db_name_len INT;\n"
  3976. "id_len INT;\n"
  3977. "offset INT;\n"
  3978. "found INT;\n"
  3979. "BEGIN\n"
  3980. "found := 1;\n"
  3981. "old_db_name_len := INSTR(:old_table_name, '/')-1;\n"
  3982. "new_db_name_len := INSTR(:new_table_name, '/')-1;\n"
  3983. "new_db_name := SUBSTR(:new_table_name, 0,\n"
  3984. " new_db_name_len);\n"
  3985. "old_t_name_len := LENGTH(:old_table_name);\n"
  3986. "gen_constr_prefix := CONCAT(:old_table_name_utf8,\n"
  3987. " '_ibfk_');\n"
  3988. "WHILE found = 1 LOOP\n"
  3989. " SELECT ID INTO foreign_id\n"
  3990. " FROM SYS_FOREIGN\n"
  3991. " WHERE FOR_NAME = :old_table_name\n"
  3992. " AND TO_BINARY(FOR_NAME)\n"
  3993. " = TO_BINARY(:old_table_name)\n"
  3994. " LOCK IN SHARE MODE;\n"
  3995. " IF (SQL % NOTFOUND) THEN\n"
  3996. " found := 0;\n"
  3997. " ELSE\n"
  3998. " UPDATE SYS_FOREIGN\n"
  3999. " SET FOR_NAME = :new_table_name\n"
  4000. " WHERE ID = foreign_id;\n"
  4001. " id_len := LENGTH(foreign_id);\n"
  4002. " IF (INSTR(foreign_id, '/') > 0) THEN\n"
  4003. " IF (INSTR(foreign_id,\n"
  4004. " gen_constr_prefix) > 0)\n"
  4005. " THEN\n"
  4006. " offset := INSTR(foreign_id, '_ibfk_') - 1;\n"
  4007. " new_foreign_id :=\n"
  4008. " CONCAT(:new_table_utf8,\n"
  4009. " SUBSTR(foreign_id, offset,\n"
  4010. " id_len - offset));\n"
  4011. " ELSE\n"
  4012. " new_foreign_id :=\n"
  4013. " CONCAT(new_db_name,\n"
  4014. " SUBSTR(foreign_id,\n"
  4015. " old_db_name_len,\n"
  4016. " id_len - old_db_name_len));\n"
  4017. " END IF;\n"
  4018. " UPDATE SYS_FOREIGN\n"
  4019. " SET ID = new_foreign_id\n"
  4020. " WHERE ID = foreign_id;\n"
  4021. " UPDATE SYS_FOREIGN_COLS\n"
  4022. " SET ID = new_foreign_id\n"
  4023. " WHERE ID = foreign_id;\n"
  4024. " END IF;\n"
  4025. " END IF;\n"
  4026. "END LOOP;\n"
  4027. "UPDATE SYS_FOREIGN SET REF_NAME = :new_table_name\n"
  4028. "WHERE REF_NAME = :old_table_name\n"
  4029. " AND TO_BINARY(REF_NAME)\n"
  4030. " = TO_BINARY(:old_table_name);\n"
  4031. "END;\n"
  4032. , FALSE, trx);
  4033. } else if (n_constraints_to_drop > 0) {
  4034. /* Drop some constraints of tmp tables. */
  4035. ulint db_name_len = dict_get_db_name_len(old_name) + 1;
  4036. char* db_name = mem_heap_strdupl(heap, old_name,
  4037. db_name_len);
  4038. ulint i;
  4039. for (i = 0; i < n_constraints_to_drop; i++) {
  4040. err = row_delete_constraint(constraints_to_drop[i],
  4041. db_name, heap, trx);
  4042. if (err != DB_SUCCESS) {
  4043. break;
  4044. }
  4045. }
  4046. }
  4047. if (dict_table_has_fts_index(table)
  4048. && !dict_tables_have_same_db(old_name, new_name)) {
  4049. err = fts_rename_aux_tables(table, new_name, trx);
  4050. if (err != DB_TABLE_NOT_FOUND) {
  4051. aux_fts_rename = true;
  4052. }
  4053. }
  4054. end:
  4055. if (err != DB_SUCCESS) {
  4056. if (err == DB_DUPLICATE_KEY) {
  4057. ut_print_timestamp(stderr);
  4058. fputs(" InnoDB: Error; possible reasons:\n"
  4059. "InnoDB: 1) Table rename would cause"
  4060. " two FOREIGN KEY constraints\n"
  4061. "InnoDB: to have the same internal name"
  4062. " in case-insensitive comparison.\n"
  4063. "InnoDB: 2) table ", stderr);
  4064. ut_print_name(stderr, trx, TRUE, new_name);
  4065. fputs(" exists in the InnoDB internal data\n"
  4066. "InnoDB: dictionary though MySQL is"
  4067. " trying to rename table ", stderr);
  4068. ut_print_name(stderr, trx, TRUE, old_name);
  4069. fputs(" to it.\n"
  4070. "InnoDB: Have you deleted the .frm file"
  4071. " and not used DROP TABLE?\n"
  4072. "InnoDB: You can look for further help from\n"
  4073. "InnoDB: " REFMAN "innodb-troubleshooting.html\n"
  4074. "InnoDB: If table ", stderr);
  4075. ut_print_name(stderr, trx, TRUE, new_name);
  4076. fputs(" is a temporary table #sql..., then"
  4077. " it can be that\n"
  4078. "InnoDB: there are still queries running"
  4079. " on the table, and it will be\n"
  4080. "InnoDB: dropped automatically when"
  4081. " the queries end.\n"
  4082. "InnoDB: You can drop the orphaned table"
  4083. " inside InnoDB by\n"
  4084. "InnoDB: creating an InnoDB table with"
  4085. " the same name in another\n"
  4086. "InnoDB: database and copying the .frm file"
  4087. " to the current database.\n"
  4088. "InnoDB: Then MySQL thinks the table exists,"
  4089. " and DROP TABLE will\n"
  4090. "InnoDB: succeed.\n", stderr);
  4091. }
  4092. trx->error_state = DB_SUCCESS;
  4093. trx_rollback_to_savepoint(trx, NULL);
  4094. trx->error_state = DB_SUCCESS;
  4095. } else {
  4096. /* The following call will also rename the .ibd data file if
  4097. the table is stored in a single-table tablespace */
  4098. err = dict_table_rename_in_cache(
  4099. table, new_name, !new_is_tmp);
  4100. if (err != DB_SUCCESS) {
  4101. trx->error_state = DB_SUCCESS;
  4102. trx_rollback_to_savepoint(trx, NULL);
  4103. trx->error_state = DB_SUCCESS;
  4104. goto funct_exit;
  4105. }
  4106. /* We only want to switch off some of the type checking in
  4107. an ALTER, not in a RENAME. */
  4108. err = dict_load_foreigns(
  4109. new_name, NULL,
  4110. false, !old_is_tmp || trx->check_foreigns,
  4111. DICT_ERR_IGNORE_NONE);
  4112. if (err != DB_SUCCESS) {
  4113. ut_print_timestamp(stderr);
  4114. if (old_is_tmp) {
  4115. fputs(" InnoDB: Error: in ALTER TABLE ",
  4116. stderr);
  4117. ut_print_name(stderr, trx, TRUE, new_name);
  4118. fputs("\n"
  4119. "InnoDB: has or is referenced"
  4120. " in foreign key constraints\n"
  4121. "InnoDB: which are not compatible"
  4122. " with the new table definition.\n",
  4123. stderr);
  4124. } else {
  4125. fputs(" InnoDB: Error: in RENAME TABLE"
  4126. " table ",
  4127. stderr);
  4128. ut_print_name(stderr, trx, TRUE, new_name);
  4129. fputs("\n"
  4130. "InnoDB: is referenced in"
  4131. " foreign key constraints\n"
  4132. "InnoDB: which are not compatible"
  4133. " with the new table definition.\n",
  4134. stderr);
  4135. }
  4136. ut_a(DB_SUCCESS == dict_table_rename_in_cache(
  4137. table, old_name, FALSE));
  4138. trx->error_state = DB_SUCCESS;
  4139. trx_rollback_to_savepoint(trx, NULL);
  4140. trx->error_state = DB_SUCCESS;
  4141. }
  4142. }
  4143. funct_exit:
  4144. if (aux_fts_rename && err != DB_SUCCESS
  4145. && table != NULL && (table->space != 0)) {
  4146. char* orig_name = table->name;
  4147. trx_t* trx_bg = trx_allocate_for_background();
  4148. /* If the first fts_rename fails, the trx would
  4149. be rolled back and committed, we can't use it any more,
  4150. so we have to start a new background trx here. */
  4151. ut_a(trx_state_eq(trx_bg, TRX_STATE_NOT_STARTED));
  4152. trx_bg->op_info = "Revert the failing rename "
  4153. "for fts aux tables";
  4154. trx_bg->dict_operation_lock_mode = RW_X_LATCH;
  4155. trx_start_for_ddl(trx_bg, TRX_DICT_OP_TABLE);
  4156. /* If rename fails and table has its own tablespace,
  4157. we need to call fts_rename_aux_tables again to
  4158. revert the ibd file rename, which is not under the
  4159. control of trx. Also notice the parent table name
  4160. in cache is not changed yet. If the reverting fails,
  4161. the ibd data may be left in the new database, which
  4162. can be fixed only manually. */
  4163. table->name = const_cast<char*>(new_name);
  4164. fts_rename_aux_tables(table, old_name, trx_bg);
  4165. table->name = orig_name;
  4166. trx_bg->dict_operation_lock_mode = 0;
  4167. trx_commit_for_mysql(trx_bg);
  4168. trx_free_for_background(trx_bg);
  4169. }
  4170. if (table != NULL) {
  4171. dict_table_close(table, dict_locked, FALSE);
  4172. }
  4173. if (commit) {
  4174. trx_commit_for_mysql(trx);
  4175. }
  4176. if (UNIV_LIKELY_NULL(heap)) {
  4177. mem_heap_free(heap);
  4178. }
  4179. trx->op_info = "";
  4180. return(err);
  4181. }
  4182. /*********************************************************************//**
  4183. Checks that the index contains entries in an ascending order, unique
  4184. constraint is not broken, and calculates the number of index entries
  4185. in the read view of the current transaction.
  4186. @return true if ok */
  4187. UNIV_INTERN
  4188. bool
  4189. row_check_index_for_mysql(
  4190. /*======================*/
  4191. row_prebuilt_t* prebuilt, /*!< in: prebuilt struct
  4192. in MySQL handle */
  4193. const dict_index_t* index, /*!< in: index */
  4194. ulint* n_rows) /*!< out: number of entries
  4195. seen in the consistent read */
  4196. {
  4197. dtuple_t* prev_entry = NULL;
  4198. ulint matched_fields;
  4199. ulint matched_bytes;
  4200. byte* buf;
  4201. ulint ret;
  4202. rec_t* rec;
  4203. bool is_ok = true;
  4204. int cmp;
  4205. ibool contains_null;
  4206. ulint i;
  4207. ulint cnt;
  4208. mem_heap_t* heap = NULL;
  4209. ulint n_ext;
  4210. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  4211. ulint* offsets;
  4212. rec_offs_init(offsets_);
  4213. *n_rows = 0;
  4214. if (dict_index_is_clust(index)) {
  4215. /* The clustered index of a table is always available.
  4216. During online ALTER TABLE that rebuilds the table, the
  4217. clustered index in the old table will have
  4218. index->online_log pointing to the new table. All
  4219. indexes of the old table will remain valid and the new
  4220. table will be unaccessible to MySQL until the
  4221. completion of the ALTER TABLE. */
  4222. } else if (dict_index_is_online_ddl(index)
  4223. || (index->type & DICT_FTS)) {
  4224. /* Full Text index are implemented by auxiliary tables,
  4225. not the B-tree. We also skip secondary indexes that are
  4226. being created online. */
  4227. return(true);
  4228. }
  4229. buf = static_cast<byte*>(mem_alloc(UNIV_PAGE_SIZE));
  4230. heap = mem_heap_create(100);
  4231. cnt = 1000;
  4232. ret = row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, 0);
  4233. loop:
  4234. /* Check thd->killed every 1,000 scanned rows */
  4235. if (--cnt == 0) {
  4236. if (trx_is_interrupted(prebuilt->trx)) {
  4237. goto func_exit;
  4238. }
  4239. cnt = 1000;
  4240. }
  4241. switch (ret) {
  4242. case DB_SUCCESS:
  4243. break;
  4244. default:
  4245. ut_print_timestamp(stderr);
  4246. fputs(" InnoDB: Warning: CHECK TABLE on ", stderr);
  4247. dict_index_name_print(stderr, prebuilt->trx, index);
  4248. fprintf(stderr, " returned %lu\n", ret);
  4249. /* fall through (this error is ignored by CHECK TABLE) */
  4250. case DB_END_OF_INDEX:
  4251. func_exit:
  4252. mem_free(buf);
  4253. mem_heap_free(heap);
  4254. return(is_ok);
  4255. }
  4256. *n_rows = *n_rows + 1;
  4257. /* row_search... returns the index record in buf, record origin offset
  4258. within buf stored in the first 4 bytes, because we have built a dummy
  4259. template */
  4260. rec = buf + mach_read_from_4(buf);
  4261. offsets = rec_get_offsets(rec, index, offsets_,
  4262. ULINT_UNDEFINED, &heap);
  4263. if (prev_entry != NULL) {
  4264. matched_fields = 0;
  4265. matched_bytes = 0;
  4266. cmp = cmp_dtuple_rec_with_match(prev_entry, rec, offsets,
  4267. &matched_fields,
  4268. &matched_bytes);
  4269. contains_null = FALSE;
  4270. /* In a unique secondary index we allow equal key values if
  4271. they contain SQL NULLs */
  4272. for (i = 0;
  4273. i < dict_index_get_n_ordering_defined_by_user(index);
  4274. i++) {
  4275. if (UNIV_SQL_NULL == dfield_get_len(
  4276. dtuple_get_nth_field(prev_entry, i))) {
  4277. contains_null = TRUE;
  4278. break;
  4279. }
  4280. }
  4281. if (cmp > 0) {
  4282. fputs("InnoDB: index records in a wrong order in ",
  4283. stderr);
  4284. not_ok:
  4285. dict_index_name_print(stderr,
  4286. prebuilt->trx, index);
  4287. fputs("\n"
  4288. "InnoDB: prev record ", stderr);
  4289. dtuple_print(stderr, prev_entry);
  4290. fputs("\n"
  4291. "InnoDB: record ", stderr);
  4292. rec_print_new(stderr, rec, offsets);
  4293. putc('\n', stderr);
  4294. is_ok = false;
  4295. } else if (dict_index_is_unique(index)
  4296. && !contains_null
  4297. && matched_fields
  4298. >= dict_index_get_n_ordering_defined_by_user(
  4299. index)) {
  4300. fputs("InnoDB: duplicate key in ", stderr);
  4301. goto not_ok;
  4302. }
  4303. }
  4304. {
  4305. mem_heap_t* tmp_heap = NULL;
  4306. /* Empty the heap on each round. But preserve offsets[]
  4307. for the row_rec_to_index_entry() call, by copying them
  4308. into a separate memory heap when needed. */
  4309. if (UNIV_UNLIKELY(offsets != offsets_)) {
  4310. ulint size = rec_offs_get_n_alloc(offsets)
  4311. * sizeof *offsets;
  4312. tmp_heap = mem_heap_create(size);
  4313. offsets = static_cast<ulint*>(
  4314. mem_heap_dup(tmp_heap, offsets, size));
  4315. }
  4316. mem_heap_empty(heap);
  4317. prev_entry = row_rec_to_index_entry(
  4318. rec, index, offsets, &n_ext, heap);
  4319. if (UNIV_LIKELY_NULL(tmp_heap)) {
  4320. mem_heap_free(tmp_heap);
  4321. }
  4322. }
  4323. ret = row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, ROW_SEL_NEXT);
  4324. goto loop;
  4325. }
  4326. /*********************************************************************//**
  4327. Determines if a table is a magic monitor table.
  4328. @return true if monitor table */
  4329. UNIV_INTERN
  4330. bool
  4331. row_is_magic_monitor_table(
  4332. /*=======================*/
  4333. const char* table_name) /*!< in: name of the table, in the
  4334. form database/table_name */
  4335. {
  4336. const char* name; /* table_name without database/ */
  4337. ulint len;
  4338. name = dict_remove_db_name(table_name);
  4339. len = strlen(name) + 1;
  4340. return(STR_EQ(name, len, S_innodb_monitor)
  4341. || STR_EQ(name, len, S_innodb_lock_monitor)
  4342. || STR_EQ(name, len, S_innodb_tablespace_monitor)
  4343. || STR_EQ(name, len, S_innodb_table_monitor)
  4344. #ifdef UNIV_MEM_DEBUG
  4345. || STR_EQ(name, len, S_innodb_mem_validate)
  4346. #endif /* UNIV_MEM_DEBUG */
  4347. );
  4348. }
  4349. /*********************************************************************//**
  4350. Initialize this module */
  4351. UNIV_INTERN
  4352. void
  4353. row_mysql_init(void)
  4354. /*================*/
  4355. {
  4356. mutex_create(
  4357. row_drop_list_mutex_key,
  4358. &row_drop_list_mutex, SYNC_NO_ORDER_CHECK);
  4359. UT_LIST_INIT(row_mysql_drop_list);
  4360. row_mysql_drop_list_inited = TRUE;
  4361. }
  4362. /*********************************************************************//**
  4363. Close this module */
  4364. UNIV_INTERN
  4365. void
  4366. row_mysql_close(void)
  4367. /*================*/
  4368. {
  4369. ut_a(UT_LIST_GET_LEN(row_mysql_drop_list) == 0);
  4370. mutex_free(&row_drop_list_mutex);
  4371. row_mysql_drop_list_inited = FALSE;
  4372. }