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.

4500 lines
121 KiB

17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
17 years ago
17 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
  1. /*****************************************************************************
  2. Copyright (c) 2000, 2013, 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.c
  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 "ha_prototypes.h"
  24. #include "row0ins.h"
  25. #include "row0merge.h"
  26. #include "row0sel.h"
  27. #include "row0upd.h"
  28. #include "row0row.h"
  29. #include "que0que.h"
  30. #include "pars0pars.h"
  31. #include "dict0dict.h"
  32. #include "dict0crea.h"
  33. #include "dict0load.h"
  34. #include "dict0boot.h"
  35. #include "trx0roll.h"
  36. #include "trx0purge.h"
  37. #include "trx0rec.h"
  38. #include "trx0undo.h"
  39. #include "lock0lock.h"
  40. #include "rem0cmp.h"
  41. #include "log0log.h"
  42. #include "btr0sea.h"
  43. #include "fil0fil.h"
  44. #include "ibuf0ibuf.h"
  45. #include "ha_prototypes.h"
  46. #include "m_string.h"
  47. #include "my_sys.h"
  48. #include "ha_prototypes.h"
  49. /** Provide optional 4.x backwards compatibility for 5.0 and above */
  50. UNIV_INTERN ibool row_rollback_on_timeout = FALSE;
  51. /** Chain node of the list of tables to drop in the background. */
  52. typedef struct row_mysql_drop_struct row_mysql_drop_t;
  53. /** Chain node of the list of tables to drop in the background. */
  54. struct row_mysql_drop_struct{
  55. char* table_name; /*!< table name */
  56. UT_LIST_NODE_T(row_mysql_drop_t)row_mysql_drop_list;
  57. /*!< list chain node */
  58. };
  59. /** @brief List of tables we should drop in background.
  60. ALTER TABLE in MySQL requires that the table handler can drop the
  61. table in background when there are no queries to it any
  62. more. Protected by kernel_mutex. */
  63. static UT_LIST_BASE_NODE_T(row_mysql_drop_t) row_mysql_drop_list;
  64. /** Flag: has row_mysql_drop_list been initialized? */
  65. static ibool row_mysql_drop_list_inited = FALSE;
  66. /** Magic table names for invoking various monitor threads */
  67. /* @{ */
  68. static const char S_innodb_monitor[] = "innodb_monitor";
  69. static const char S_innodb_lock_monitor[] = "innodb_lock_monitor";
  70. static const char S_innodb_tablespace_monitor[] = "innodb_tablespace_monitor";
  71. static const char S_innodb_table_monitor[] = "innodb_table_monitor";
  72. static const char S_innodb_mem_validate[] = "innodb_mem_validate";
  73. /* @} */
  74. /** Evaluates to true if str1 equals str2_onstack, used for comparing
  75. the magic table names.
  76. @param str1 in: string to compare
  77. @param str1_len in: length of str1, in bytes, including terminating NUL
  78. @param str2_onstack in: char[] array containing a NUL terminated string
  79. @return TRUE if str1 equals str2_onstack */
  80. #define STR_EQ(str1, str1_len, str2_onstack) \
  81. ((str1_len) == sizeof(str2_onstack) \
  82. && memcmp(str1, str2_onstack, sizeof(str2_onstack)) == 0)
  83. /*******************************************************************//**
  84. Determine if the given name is a name reserved for MySQL system tables.
  85. @return TRUE if name is a MySQL system table name */
  86. static
  87. ibool
  88. row_mysql_is_system_table(
  89. /*======================*/
  90. const char* name)
  91. {
  92. if (strncmp(name, "mysql/", 6) != 0) {
  93. return(FALSE);
  94. }
  95. return(0 == strcmp(name + 6, "host")
  96. || 0 == strcmp(name + 6, "user")
  97. || 0 == strcmp(name + 6, "db"));
  98. }
  99. /*********************************************************************//**
  100. If a table is not yet in the drop list, adds the table to the list of tables
  101. which the master thread drops in background. We need this on Unix because in
  102. ALTER TABLE MySQL may call drop table even if the table has running queries on
  103. it. Also, if there are running foreign key checks on the table, we drop the
  104. table lazily.
  105. @return TRUE if the table was not yet in the drop list, and was added there */
  106. static
  107. ibool
  108. row_add_table_to_background_drop_list(
  109. /*==================================*/
  110. const char* name); /*!< in: table name */
  111. /*******************************************************************//**
  112. Delays an INSERT, DELETE or UPDATE operation if the purge is lagging. */
  113. static
  114. void
  115. row_mysql_delay_if_needed(void)
  116. /*===========================*/
  117. {
  118. if (srv_dml_needed_delay) {
  119. os_thread_sleep(srv_dml_needed_delay);
  120. }
  121. }
  122. /*******************************************************************//**
  123. Frees the blob heap in prebuilt when no longer needed. */
  124. UNIV_INTERN
  125. void
  126. row_mysql_prebuilt_free_blob_heap(
  127. /*==============================*/
  128. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct of a
  129. ha_innobase:: table handle */
  130. {
  131. mem_heap_free(prebuilt->blob_heap);
  132. prebuilt->blob_heap = NULL;
  133. }
  134. /*******************************************************************//**
  135. Stores a >= 5.0.3 format true VARCHAR length to dest, in the MySQL row
  136. format.
  137. @return pointer to the data, we skip the 1 or 2 bytes at the start
  138. that are used to store the len */
  139. UNIV_INTERN
  140. byte*
  141. row_mysql_store_true_var_len(
  142. /*=========================*/
  143. byte* dest, /*!< in: where to store */
  144. ulint len, /*!< in: length, must fit in two bytes */
  145. ulint lenlen) /*!< in: storage length of len: either 1 or 2 bytes */
  146. {
  147. if (lenlen == 2) {
  148. ut_a(len < 256 * 256);
  149. mach_write_to_2_little_endian(dest, len);
  150. return(dest + 2);
  151. }
  152. ut_a(lenlen == 1);
  153. ut_a(len < 256);
  154. mach_write_to_1(dest, len);
  155. return(dest + 1);
  156. }
  157. /*******************************************************************//**
  158. Reads a >= 5.0.3 format true VARCHAR length, in the MySQL row format, and
  159. returns a pointer to the data.
  160. @return pointer to the data, we skip the 1 or 2 bytes at the start
  161. that are used to store the len */
  162. UNIV_INTERN
  163. const byte*
  164. row_mysql_read_true_varchar(
  165. /*========================*/
  166. ulint* len, /*!< out: variable-length field length */
  167. const byte* field, /*!< in: field in the MySQL format */
  168. ulint lenlen) /*!< in: storage length of len: either 1
  169. or 2 bytes */
  170. {
  171. if (lenlen == 2) {
  172. *len = mach_read_from_2_little_endian(field);
  173. return(field + 2);
  174. }
  175. ut_a(lenlen == 1);
  176. *len = mach_read_from_1(field);
  177. return(field + 1);
  178. }
  179. /*******************************************************************//**
  180. Stores a reference to a BLOB in the MySQL format. */
  181. UNIV_INTERN
  182. void
  183. row_mysql_store_blob_ref(
  184. /*=====================*/
  185. byte* dest, /*!< in: where to store */
  186. ulint col_len,/*!< in: dest buffer size: determines into
  187. how many bytes the BLOB length is stored,
  188. the space for the length may vary from 1
  189. to 4 bytes */
  190. const void* data, /*!< in: BLOB data; if the value to store
  191. is SQL NULL this should be NULL pointer */
  192. ulint len) /*!< in: BLOB length; if the value to store
  193. is SQL NULL this should be 0; remember
  194. also to set the NULL bit in the MySQL record
  195. header! */
  196. {
  197. /* MySQL might assume the field is set to zero except the length and
  198. the pointer fields */
  199. memset(dest, '\0', col_len);
  200. /* In dest there are 1 - 4 bytes reserved for the BLOB length,
  201. and after that 8 bytes reserved for the pointer to the data.
  202. In 32-bit architectures we only use the first 4 bytes of the pointer
  203. slot. */
  204. ut_a(col_len - 8 > 1 || len < 256);
  205. ut_a(col_len - 8 > 2 || len < 256 * 256);
  206. ut_a(col_len - 8 > 3 || len < 256 * 256 * 256);
  207. mach_write_to_n_little_endian(dest, col_len - 8, len);
  208. memcpy(dest + col_len - 8, &data, sizeof data);
  209. }
  210. /*******************************************************************//**
  211. Reads a reference to a BLOB in the MySQL format.
  212. @return pointer to BLOB data */
  213. UNIV_INTERN
  214. const byte*
  215. row_mysql_read_blob_ref(
  216. /*====================*/
  217. ulint* len, /*!< out: BLOB length */
  218. const byte* ref, /*!< in: BLOB reference in the
  219. MySQL format */
  220. ulint col_len) /*!< in: BLOB reference length
  221. (not BLOB length) */
  222. {
  223. byte* data;
  224. *len = mach_read_from_n_little_endian(ref, col_len - 8);
  225. memcpy(&data, ref + col_len - 8, sizeof data);
  226. return(data);
  227. }
  228. /**************************************************************//**
  229. Pad a column with spaces. */
  230. UNIV_INTERN
  231. void
  232. row_mysql_pad_col(
  233. /*==============*/
  234. ulint mbminlen, /*!< in: minimum size of a character,
  235. in bytes */
  236. byte* pad, /*!< out: padded buffer */
  237. ulint len) /*!< in: number of bytes to pad */
  238. {
  239. const byte* pad_end;
  240. switch (UNIV_EXPECT(mbminlen, 1)) {
  241. default:
  242. ut_error;
  243. case 1:
  244. /* space=0x20 */
  245. memset(pad, 0x20, len);
  246. break;
  247. case 2:
  248. /* space=0x0020 */
  249. pad_end = pad + len;
  250. ut_a(!(len % 2));
  251. while (pad < pad_end) {
  252. *pad++ = 0x00;
  253. *pad++ = 0x20;
  254. };
  255. break;
  256. case 4:
  257. /* space=0x00000020 */
  258. pad_end = pad + len;
  259. ut_a(!(len % 4));
  260. while (pad < pad_end) {
  261. *pad++ = 0x00;
  262. *pad++ = 0x00;
  263. *pad++ = 0x00;
  264. *pad++ = 0x20;
  265. }
  266. break;
  267. }
  268. }
  269. /**************************************************************//**
  270. Stores a non-SQL-NULL field given in the MySQL format in the InnoDB format.
  271. The counterpart of this function is row_sel_field_store_in_mysql_format() in
  272. row0sel.c.
  273. @return up to which byte we used buf in the conversion */
  274. UNIV_INTERN
  275. byte*
  276. row_mysql_store_col_in_innobase_format(
  277. /*===================================*/
  278. dfield_t* dfield, /*!< in/out: dfield where dtype
  279. information must be already set when
  280. this function is called! */
  281. byte* buf, /*!< in/out: buffer for a converted
  282. integer value; this must be at least
  283. col_len long then! */
  284. ibool row_format_col, /*!< TRUE if the mysql_data is from
  285. a MySQL row, FALSE if from a MySQL
  286. key value;
  287. in MySQL, a true VARCHAR storage
  288. format differs in a row and in a
  289. key value: in a key value the length
  290. is always stored in 2 bytes! */
  291. const byte* mysql_data, /*!< in: MySQL column value, not
  292. SQL NULL; NOTE that dfield may also
  293. get a pointer to mysql_data,
  294. therefore do not discard this as long
  295. as dfield is used! */
  296. ulint col_len, /*!< in: MySQL column length; NOTE that
  297. this is the storage length of the
  298. column in the MySQL format row, not
  299. necessarily the length of the actual
  300. payload data; if the column is a true
  301. VARCHAR then this is irrelevant */
  302. ulint comp) /*!< in: nonzero=compact format */
  303. {
  304. const byte* ptr = mysql_data;
  305. const dtype_t* dtype;
  306. ulint type;
  307. ulint lenlen;
  308. dtype = dfield_get_type(dfield);
  309. type = dtype->mtype;
  310. if (type == DATA_INT) {
  311. /* Store integer data in Innobase in a big-endian format,
  312. sign bit negated if the data is a signed integer. In MySQL,
  313. integers are stored in a little-endian format. */
  314. byte* p = buf + col_len;
  315. for (;;) {
  316. p--;
  317. *p = *mysql_data;
  318. if (p == buf) {
  319. break;
  320. }
  321. mysql_data++;
  322. }
  323. if (!(dtype->prtype & DATA_UNSIGNED)) {
  324. *buf ^= 128;
  325. }
  326. ptr = buf;
  327. buf += col_len;
  328. } else if ((type == DATA_VARCHAR
  329. || type == DATA_VARMYSQL
  330. || type == DATA_BINARY)) {
  331. if (dtype_get_mysql_type(dtype) == DATA_MYSQL_TRUE_VARCHAR) {
  332. /* The length of the actual data is stored to 1 or 2
  333. bytes at the start of the field */
  334. if (row_format_col) {
  335. if (dtype->prtype & DATA_LONG_TRUE_VARCHAR) {
  336. lenlen = 2;
  337. } else {
  338. lenlen = 1;
  339. }
  340. } else {
  341. /* In a MySQL key value, lenlen is always 2 */
  342. lenlen = 2;
  343. }
  344. ptr = row_mysql_read_true_varchar(&col_len, mysql_data,
  345. lenlen);
  346. } else {
  347. /* Remove trailing spaces from old style VARCHAR
  348. columns. */
  349. /* Handle Unicode strings differently. */
  350. ulint mbminlen = dtype_get_mbminlen(dtype);
  351. ptr = mysql_data;
  352. switch (mbminlen) {
  353. default:
  354. ut_error;
  355. case 4:
  356. /* space=0x00000020 */
  357. /* Trim "half-chars", just in case. */
  358. col_len &= ~3;
  359. while (col_len >= 4
  360. && ptr[col_len - 4] == 0x00
  361. && ptr[col_len - 3] == 0x00
  362. && ptr[col_len - 2] == 0x00
  363. && ptr[col_len - 1] == 0x20) {
  364. col_len -= 4;
  365. }
  366. break;
  367. case 2:
  368. /* space=0x0020 */
  369. /* Trim "half-chars", just in case. */
  370. col_len &= ~1;
  371. while (col_len >= 2 && ptr[col_len - 2] == 0x00
  372. && ptr[col_len - 1] == 0x20) {
  373. col_len -= 2;
  374. }
  375. break;
  376. case 1:
  377. /* space=0x20 */
  378. while (col_len > 0
  379. && ptr[col_len - 1] == 0x20) {
  380. col_len--;
  381. }
  382. }
  383. }
  384. } else if (comp && type == DATA_MYSQL
  385. && dtype_get_mbminlen(dtype) == 1
  386. && dtype_get_mbmaxlen(dtype) > 1) {
  387. /* In some cases we strip trailing spaces from UTF-8 and other
  388. multibyte charsets, from FIXED-length CHAR columns, to save
  389. space. UTF-8 would otherwise normally use 3 * the string length
  390. bytes to store an ASCII string! */
  391. /* We assume that this CHAR field is encoded in a
  392. variable-length character set where spaces have
  393. 1:1 correspondence to 0x20 bytes, such as UTF-8.
  394. Consider a CHAR(n) field, a field of n characters.
  395. It will contain between n * mbminlen and n * mbmaxlen bytes.
  396. We will try to truncate it to n bytes by stripping
  397. space padding. If the field contains single-byte
  398. characters only, it will be truncated to n characters.
  399. Consider a CHAR(5) field containing the string ".a "
  400. where "." denotes a 3-byte character represented by
  401. the bytes "$%&". After our stripping, the string will
  402. be stored as "$%&a " (5 bytes). The string ".abc "
  403. will be stored as "$%&abc" (6 bytes).
  404. The space padding will be restored in row0sel.c, function
  405. row_sel_field_store_in_mysql_format(). */
  406. ulint n_chars;
  407. ut_a(!(dtype_get_len(dtype) % dtype_get_mbmaxlen(dtype)));
  408. n_chars = dtype_get_len(dtype) / dtype_get_mbmaxlen(dtype);
  409. /* Strip space padding. */
  410. while (col_len > n_chars && ptr[col_len - 1] == 0x20) {
  411. col_len--;
  412. }
  413. } else if (type == DATA_BLOB && row_format_col) {
  414. ptr = row_mysql_read_blob_ref(&col_len, mysql_data, col_len);
  415. }
  416. dfield_set_data(dfield, ptr, col_len);
  417. return(buf);
  418. }
  419. /**************************************************************//**
  420. Convert a row in the MySQL format to a row in the Innobase format. Note that
  421. the function to convert a MySQL format key value to an InnoDB dtuple is
  422. row_sel_convert_mysql_key_to_innobase() in row0sel.c. */
  423. static
  424. void
  425. row_mysql_convert_row_to_innobase(
  426. /*==============================*/
  427. dtuple_t* row, /*!< in/out: Innobase row where the
  428. field type information is already
  429. copied there! */
  430. row_prebuilt_t* prebuilt, /*!< in: prebuilt struct where template
  431. must be of type ROW_MYSQL_WHOLE_ROW */
  432. byte* mysql_rec) /*!< in: row in the MySQL format;
  433. NOTE: do not discard as long as
  434. row is used, as row may contain
  435. pointers to this record! */
  436. {
  437. const mysql_row_templ_t*templ;
  438. dfield_t* dfield;
  439. ulint i;
  440. ut_ad(prebuilt->template_type == ROW_MYSQL_WHOLE_ROW);
  441. ut_ad(prebuilt->mysql_template);
  442. for (i = 0; i < prebuilt->n_template; i++) {
  443. templ = prebuilt->mysql_template + i;
  444. dfield = dtuple_get_nth_field(row, i);
  445. if (templ->mysql_null_bit_mask != 0) {
  446. /* Column may be SQL NULL */
  447. if (mysql_rec[templ->mysql_null_byte_offset]
  448. & (byte) (templ->mysql_null_bit_mask)) {
  449. /* It is SQL NULL */
  450. dfield_set_null(dfield);
  451. goto next_column;
  452. }
  453. }
  454. row_mysql_store_col_in_innobase_format(
  455. dfield,
  456. prebuilt->ins_upd_rec_buff + templ->mysql_col_offset,
  457. TRUE, /* MySQL row format data */
  458. mysql_rec + templ->mysql_col_offset,
  459. templ->mysql_col_len,
  460. dict_table_is_comp(prebuilt->table));
  461. next_column:
  462. ;
  463. }
  464. }
  465. /****************************************************************//**
  466. Handles user errors and lock waits detected by the database engine.
  467. @return TRUE if it was a lock wait and we should continue running the
  468. query thread and in that case the thr is ALREADY in the running state. */
  469. UNIV_INTERN
  470. ibool
  471. row_mysql_handle_errors(
  472. /*====================*/
  473. ulint* new_err,/*!< out: possible new error encountered in
  474. lock wait, or if no new error, the value
  475. of trx->error_state at the entry of this
  476. function */
  477. trx_t* trx, /*!< in: transaction */
  478. que_thr_t* thr, /*!< in: query thread */
  479. trx_savept_t* savept) /*!< in: savepoint or NULL */
  480. {
  481. ulint err;
  482. handle_new_error:
  483. err = trx->error_state;
  484. ut_a(err != DB_SUCCESS);
  485. trx->error_state = DB_SUCCESS;
  486. switch (err) {
  487. case DB_LOCK_WAIT_TIMEOUT:
  488. if (row_rollback_on_timeout) {
  489. trx_general_rollback_for_mysql(trx, NULL);
  490. break;
  491. }
  492. /* fall through */
  493. case DB_DUPLICATE_KEY:
  494. case DB_FOREIGN_DUPLICATE_KEY:
  495. case DB_TOO_BIG_RECORD:
  496. case DB_UNDO_RECORD_TOO_BIG:
  497. case DB_ROW_IS_REFERENCED:
  498. case DB_NO_REFERENCED_ROW:
  499. case DB_CANNOT_ADD_CONSTRAINT:
  500. case DB_TOO_MANY_CONCURRENT_TRXS:
  501. case DB_OUT_OF_FILE_SPACE:
  502. case DB_INTERRUPTED:
  503. if (savept) {
  504. /* Roll back the latest, possibly incomplete
  505. insertion or update */
  506. trx_general_rollback_for_mysql(trx, savept);
  507. }
  508. /* MySQL will roll back the latest SQL statement */
  509. break;
  510. case DB_LOCK_WAIT:
  511. srv_suspend_mysql_thread(thr);
  512. if (trx->error_state != DB_SUCCESS) {
  513. que_thr_stop_for_mysql(thr);
  514. goto handle_new_error;
  515. }
  516. *new_err = err;
  517. return(TRUE);
  518. case DB_DEADLOCK:
  519. case DB_LOCK_TABLE_FULL:
  520. /* Roll back the whole transaction; this resolution was added
  521. to version 3.23.43 */
  522. trx_general_rollback_for_mysql(trx, NULL);
  523. break;
  524. case DB_MUST_GET_MORE_FILE_SPACE:
  525. fputs("InnoDB: The database cannot continue"
  526. " operation because of\n"
  527. "InnoDB: lack of space. You must add"
  528. " a new data file to\n"
  529. "InnoDB: my.cnf and restart the database.\n", stderr);
  530. exit(1);
  531. case DB_CORRUPTION:
  532. fputs("InnoDB: We detected index corruption"
  533. " in an InnoDB type table.\n"
  534. "InnoDB: You have to dump + drop + reimport"
  535. " the table or, in\n"
  536. "InnoDB: a case of widespread corruption,"
  537. " dump all InnoDB\n"
  538. "InnoDB: tables and recreate the"
  539. " whole InnoDB tablespace.\n"
  540. "InnoDB: If the mysqld server crashes"
  541. " after the startup or when\n"
  542. "InnoDB: you dump the tables, look at\n"
  543. "InnoDB: " REFMAN "forcing-innodb-recovery.html"
  544. " for help.\n", stderr);
  545. break;
  546. case DB_FOREIGN_EXCEED_MAX_CASCADE:
  547. fprintf(stderr, "InnoDB: Cannot delete/update rows with"
  548. " cascading foreign key constraints that exceed max"
  549. " depth of %lu\n"
  550. "Please drop excessive foreign constraints"
  551. " and try again\n", (ulong) DICT_FK_MAX_RECURSIVE_LOAD);
  552. break;
  553. default:
  554. fprintf(stderr, "InnoDB: unknown error code %lu\n",
  555. (ulong) err);
  556. ut_error;
  557. }
  558. if (trx->error_state != DB_SUCCESS) {
  559. *new_err = trx->error_state;
  560. } else {
  561. *new_err = err;
  562. }
  563. trx->error_state = DB_SUCCESS;
  564. return(FALSE);
  565. }
  566. /********************************************************************//**
  567. Create a prebuilt struct for a MySQL table handle.
  568. @return own: a prebuilt struct */
  569. UNIV_INTERN
  570. row_prebuilt_t*
  571. row_create_prebuilt(
  572. /*================*/
  573. dict_table_t* table, /*!< in: Innobase table handle */
  574. ulint mysql_row_len) /*!< in: length in bytes of a row in
  575. the MySQL format */
  576. {
  577. row_prebuilt_t* prebuilt;
  578. mem_heap_t* heap;
  579. dict_index_t* clust_index;
  580. dtuple_t* ref;
  581. ulint ref_len;
  582. ulint search_tuple_n_fields;
  583. search_tuple_n_fields = 2 * dict_table_get_n_cols(table);
  584. clust_index = dict_table_get_first_index(table);
  585. /* Make sure that search_tuple is long enough for clustered index */
  586. ut_a(2 * dict_table_get_n_cols(table) >= clust_index->n_fields);
  587. ref_len = dict_index_get_n_unique(clust_index);
  588. #define PREBUILT_HEAP_INITIAL_SIZE \
  589. ( \
  590. sizeof(*prebuilt) \
  591. /* allocd in this function */ \
  592. + DTUPLE_EST_ALLOC(search_tuple_n_fields) \
  593. + DTUPLE_EST_ALLOC(ref_len) \
  594. /* allocd in row_prebuild_sel_graph() */ \
  595. + sizeof(sel_node_t) \
  596. + sizeof(que_fork_t) \
  597. + sizeof(que_thr_t) \
  598. /* allocd in row_get_prebuilt_update_vector() */ \
  599. + sizeof(upd_node_t) \
  600. + sizeof(upd_t) \
  601. + sizeof(upd_field_t) \
  602. * dict_table_get_n_cols(table) \
  603. + sizeof(que_fork_t) \
  604. + sizeof(que_thr_t) \
  605. /* allocd in row_get_prebuilt_insert_row() */ \
  606. + sizeof(ins_node_t) \
  607. /* mysql_row_len could be huge and we are not \
  608. sure if this prebuilt instance is going to be \
  609. used in inserts */ \
  610. + (mysql_row_len < 256 ? mysql_row_len : 0) \
  611. + DTUPLE_EST_ALLOC(dict_table_get_n_cols(table)) \
  612. + sizeof(que_fork_t) \
  613. + sizeof(que_thr_t) \
  614. )
  615. /* We allocate enough space for the objects that are likely to
  616. be created later in order to minimize the number of malloc()
  617. calls */
  618. heap = mem_heap_create(PREBUILT_HEAP_INITIAL_SIZE);
  619. prebuilt = mem_heap_zalloc(heap, sizeof(*prebuilt));
  620. prebuilt->magic_n = ROW_PREBUILT_ALLOCATED;
  621. prebuilt->magic_n2 = ROW_PREBUILT_ALLOCATED;
  622. prebuilt->table = table;
  623. prebuilt->sql_stat_start = TRUE;
  624. prebuilt->heap = heap;
  625. btr_pcur_reset(&prebuilt->pcur);
  626. btr_pcur_reset(&prebuilt->clust_pcur);
  627. prebuilt->select_lock_type = LOCK_NONE;
  628. prebuilt->stored_select_lock_type = 99999999;
  629. UNIV_MEM_INVALID(&prebuilt->stored_select_lock_type,
  630. sizeof prebuilt->stored_select_lock_type);
  631. prebuilt->search_tuple = dtuple_create(heap, search_tuple_n_fields);
  632. ref = dtuple_create(heap, ref_len);
  633. dict_index_copy_types(ref, clust_index, ref_len);
  634. prebuilt->clust_ref = ref;
  635. prebuilt->autoinc_error = 0;
  636. prebuilt->autoinc_offset = 0;
  637. /* Default to 1, we will set the actual value later in
  638. ha_innobase::get_auto_increment(). */
  639. prebuilt->autoinc_increment = 1;
  640. prebuilt->autoinc_last_value = 0;
  641. prebuilt->mysql_row_len = mysql_row_len;
  642. return(prebuilt);
  643. }
  644. /********************************************************************//**
  645. Free a prebuilt struct for a MySQL table handle. */
  646. UNIV_INTERN
  647. void
  648. row_prebuilt_free(
  649. /*==============*/
  650. row_prebuilt_t* prebuilt, /*!< in, own: prebuilt struct */
  651. ibool dict_locked) /*!< in: TRUE=data dictionary locked */
  652. {
  653. ulint i;
  654. if (UNIV_UNLIKELY
  655. (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED
  656. || prebuilt->magic_n2 != ROW_PREBUILT_ALLOCATED)) {
  657. fprintf(stderr,
  658. "InnoDB: Error: trying to free a corrupt\n"
  659. "InnoDB: table handle. Magic n %lu,"
  660. " magic n2 %lu, table name ",
  661. (ulong) prebuilt->magic_n,
  662. (ulong) prebuilt->magic_n2);
  663. ut_print_name(stderr, NULL, TRUE, prebuilt->table->name);
  664. putc('\n', stderr);
  665. mem_analyze_corruption(prebuilt);
  666. ut_error;
  667. }
  668. prebuilt->magic_n = ROW_PREBUILT_FREED;
  669. prebuilt->magic_n2 = ROW_PREBUILT_FREED;
  670. btr_pcur_reset(&prebuilt->pcur);
  671. btr_pcur_reset(&prebuilt->clust_pcur);
  672. if (prebuilt->mysql_template) {
  673. mem_free(prebuilt->mysql_template);
  674. }
  675. if (prebuilt->ins_graph) {
  676. que_graph_free_recursive(prebuilt->ins_graph);
  677. }
  678. if (prebuilt->sel_graph) {
  679. que_graph_free_recursive(prebuilt->sel_graph);
  680. }
  681. if (prebuilt->upd_graph) {
  682. que_graph_free_recursive(prebuilt->upd_graph);
  683. }
  684. if (prebuilt->blob_heap) {
  685. mem_heap_free(prebuilt->blob_heap);
  686. }
  687. if (prebuilt->old_vers_heap) {
  688. mem_heap_free(prebuilt->old_vers_heap);
  689. }
  690. for (i = 0; i < MYSQL_FETCH_CACHE_SIZE; i++) {
  691. if (prebuilt->fetch_cache[i] != NULL) {
  692. if ((ROW_PREBUILT_FETCH_MAGIC_N != mach_read_from_4(
  693. (prebuilt->fetch_cache[i]) - 4))
  694. || (ROW_PREBUILT_FETCH_MAGIC_N != mach_read_from_4(
  695. (prebuilt->fetch_cache[i])
  696. + prebuilt->mysql_row_len))) {
  697. fputs("InnoDB: Error: trying to free"
  698. " a corrupt fetch buffer.\n", stderr);
  699. mem_analyze_corruption(
  700. prebuilt->fetch_cache[i]);
  701. ut_error;
  702. }
  703. mem_free((prebuilt->fetch_cache[i]) - 4);
  704. }
  705. }
  706. dict_table_decrement_handle_count(prebuilt->table, dict_locked);
  707. mem_heap_free(prebuilt->heap);
  708. }
  709. /*********************************************************************//**
  710. Updates the transaction pointers in query graphs stored in the prebuilt
  711. struct. */
  712. UNIV_INTERN
  713. void
  714. row_update_prebuilt_trx(
  715. /*====================*/
  716. row_prebuilt_t* prebuilt, /*!< in/out: prebuilt struct
  717. in MySQL handle */
  718. trx_t* trx) /*!< in: transaction handle */
  719. {
  720. if (trx->magic_n != TRX_MAGIC_N) {
  721. fprintf(stderr,
  722. "InnoDB: Error: trying to use a corrupt\n"
  723. "InnoDB: trx handle. Magic n %lu\n",
  724. (ulong) trx->magic_n);
  725. mem_analyze_corruption(trx);
  726. ut_error;
  727. }
  728. if (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED) {
  729. fprintf(stderr,
  730. "InnoDB: Error: trying to use a corrupt\n"
  731. "InnoDB: table handle. Magic n %lu, table name ",
  732. (ulong) prebuilt->magic_n);
  733. ut_print_name(stderr, trx, TRUE, prebuilt->table->name);
  734. putc('\n', stderr);
  735. mem_analyze_corruption(prebuilt);
  736. ut_error;
  737. }
  738. prebuilt->trx = trx;
  739. if (prebuilt->ins_graph) {
  740. prebuilt->ins_graph->trx = trx;
  741. }
  742. if (prebuilt->upd_graph) {
  743. prebuilt->upd_graph->trx = trx;
  744. }
  745. if (prebuilt->sel_graph) {
  746. prebuilt->sel_graph->trx = trx;
  747. }
  748. }
  749. /*********************************************************************//**
  750. Gets pointer to a prebuilt dtuple used in insertions. If the insert graph
  751. has not yet been built in the prebuilt struct, then this function first
  752. builds it.
  753. @return prebuilt dtuple; the column type information is also set in it */
  754. static
  755. dtuple_t*
  756. row_get_prebuilt_insert_row(
  757. /*========================*/
  758. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  759. handle */
  760. {
  761. ins_node_t* node;
  762. dtuple_t* row;
  763. dict_table_t* table = prebuilt->table;
  764. ut_ad(prebuilt && table && prebuilt->trx);
  765. if (prebuilt->ins_node == NULL) {
  766. /* Not called before for this handle: create an insert node
  767. and query graph to the prebuilt struct */
  768. node = ins_node_create(INS_DIRECT, table, prebuilt->heap);
  769. prebuilt->ins_node = node;
  770. if (prebuilt->ins_upd_rec_buff == NULL) {
  771. prebuilt->ins_upd_rec_buff = mem_heap_alloc(
  772. prebuilt->heap, prebuilt->mysql_row_len);
  773. }
  774. row = dtuple_create(prebuilt->heap,
  775. dict_table_get_n_cols(table));
  776. dict_table_copy_types(row, table);
  777. ins_node_set_new_row(node, row);
  778. prebuilt->ins_graph = que_node_get_parent(
  779. pars_complete_graph_for_exec(node,
  780. prebuilt->trx,
  781. prebuilt->heap));
  782. prebuilt->ins_graph->state = QUE_FORK_ACTIVE;
  783. }
  784. return(prebuilt->ins_node->row);
  785. }
  786. /*********************************************************************//**
  787. Updates the table modification counter and calculates new estimates
  788. for table and index statistics if necessary. */
  789. UNIV_INLINE
  790. void
  791. row_update_statistics_if_needed(
  792. /*============================*/
  793. dict_table_t* table) /*!< in: table */
  794. {
  795. ulint counter;
  796. counter = table->stat_modified_counter;
  797. table->stat_modified_counter = counter + 1;
  798. if (!srv_stats_auto_update)
  799. return;
  800. if (DICT_TABLE_CHANGED_TOO_MUCH(table)) {
  801. dict_update_statistics(
  802. table,
  803. FALSE, /* update even if stats are initialized */
  804. TRUE,
  805. TRUE /* only update if stats changed too much */);
  806. }
  807. }
  808. /*********************************************************************//**
  809. Unlocks AUTO_INC type locks that were possibly reserved by a trx. This
  810. function should be called at the the end of an SQL statement, by the
  811. connection thread that owns the transaction (trx->mysql_thd). */
  812. UNIV_INTERN
  813. void
  814. row_unlock_table_autoinc_for_mysql(
  815. /*===============================*/
  816. trx_t* trx) /*!< in/out: transaction */
  817. {
  818. if (lock_trx_holds_autoinc_locks(trx)) {
  819. mutex_enter(&kernel_mutex);
  820. lock_release_autoinc_locks(trx);
  821. mutex_exit(&kernel_mutex);
  822. }
  823. }
  824. /*********************************************************************//**
  825. Sets an AUTO_INC type lock on the table mentioned in prebuilt. The
  826. AUTO_INC lock gives exclusive access to the auto-inc counter of the
  827. table. The lock is reserved only for the duration of an SQL statement.
  828. It is not compatible with another AUTO_INC or exclusive lock on the
  829. table.
  830. @return error code or DB_SUCCESS */
  831. UNIV_INTERN
  832. int
  833. row_lock_table_autoinc_for_mysql(
  834. /*=============================*/
  835. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in the MySQL
  836. table handle */
  837. {
  838. trx_t* trx = prebuilt->trx;
  839. ins_node_t* node = prebuilt->ins_node;
  840. const dict_table_t* table = prebuilt->table;
  841. que_thr_t* thr;
  842. ulint err;
  843. ibool was_lock_wait;
  844. ut_ad(trx);
  845. /* If we already hold an AUTOINC lock on the table then do nothing.
  846. Note: We peek at the value of the current owner without acquiring
  847. the kernel mutex. **/
  848. if (trx == table->autoinc_trx) {
  849. return(DB_SUCCESS);
  850. }
  851. trx->op_info = "setting auto-inc lock";
  852. if (node == NULL) {
  853. row_get_prebuilt_insert_row(prebuilt);
  854. node = prebuilt->ins_node;
  855. }
  856. /* We use the insert query graph as the dummy graph needed
  857. in the lock module call */
  858. thr = que_fork_get_first_thr(prebuilt->ins_graph);
  859. que_thr_move_to_run_state_for_mysql(thr, trx);
  860. run_again:
  861. thr->run_node = node;
  862. thr->prev_node = node;
  863. /* It may be that the current session has not yet started
  864. its transaction, or it has been committed: */
  865. trx_start_if_not_started(trx);
  866. err = lock_table(0, prebuilt->table, LOCK_AUTO_INC, thr);
  867. trx->error_state = err;
  868. if (err != DB_SUCCESS) {
  869. que_thr_stop_for_mysql(thr);
  870. was_lock_wait = row_mysql_handle_errors(&err, trx, thr, NULL);
  871. if (was_lock_wait) {
  872. goto run_again;
  873. }
  874. trx->op_info = "";
  875. return((int) err);
  876. }
  877. que_thr_stop_for_mysql_no_error(thr, trx);
  878. trx->op_info = "";
  879. return((int) err);
  880. }
  881. /*********************************************************************//**
  882. Sets a table lock on the table mentioned in prebuilt.
  883. @return error code or DB_SUCCESS */
  884. UNIV_INTERN
  885. int
  886. row_lock_table_for_mysql(
  887. /*=====================*/
  888. row_prebuilt_t* prebuilt, /*!< in: prebuilt struct in the MySQL
  889. table handle */
  890. dict_table_t* table, /*!< in: table to lock, or NULL
  891. if prebuilt->table should be
  892. locked as
  893. prebuilt->select_lock_type */
  894. ulint mode) /*!< in: lock mode of table
  895. (ignored if table==NULL) */
  896. {
  897. trx_t* trx = prebuilt->trx;
  898. que_thr_t* thr;
  899. ulint err;
  900. ibool was_lock_wait;
  901. ut_ad(trx);
  902. trx->op_info = "setting table lock";
  903. if (prebuilt->sel_graph == NULL) {
  904. /* Build a dummy select query graph */
  905. row_prebuild_sel_graph(prebuilt);
  906. }
  907. /* We use the select query graph as the dummy graph needed
  908. in the lock module call */
  909. thr = que_fork_get_first_thr(prebuilt->sel_graph);
  910. que_thr_move_to_run_state_for_mysql(thr, trx);
  911. run_again:
  912. thr->run_node = thr;
  913. thr->prev_node = thr->common.parent;
  914. /* It may be that the current session has not yet started
  915. its transaction, or it has been committed: */
  916. trx_start_if_not_started(trx);
  917. if (table) {
  918. err = lock_table(0, table, mode, thr);
  919. } else {
  920. err = lock_table(0, prebuilt->table,
  921. prebuilt->select_lock_type, thr);
  922. }
  923. trx->error_state = err;
  924. if (err != DB_SUCCESS) {
  925. que_thr_stop_for_mysql(thr);
  926. was_lock_wait = row_mysql_handle_errors(&err, trx, thr, NULL);
  927. if (was_lock_wait) {
  928. goto run_again;
  929. }
  930. trx->op_info = "";
  931. return((int) err);
  932. }
  933. que_thr_stop_for_mysql_no_error(thr, trx);
  934. trx->op_info = "";
  935. return((int) err);
  936. }
  937. /*********************************************************************//**
  938. Does an insert for MySQL.
  939. @return error code or DB_SUCCESS */
  940. UNIV_INTERN
  941. int
  942. row_insert_for_mysql(
  943. /*=================*/
  944. byte* mysql_rec, /*!< in: row in the MySQL format */
  945. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  946. handle */
  947. {
  948. trx_savept_t savept;
  949. que_thr_t* thr;
  950. ulint err;
  951. ibool was_lock_wait;
  952. trx_t* trx = prebuilt->trx;
  953. ins_node_t* node = prebuilt->ins_node;
  954. ut_ad(trx);
  955. if (prebuilt->table->ibd_file_missing) {
  956. ut_print_timestamp(stderr);
  957. fprintf(stderr, " InnoDB: Error:\n"
  958. "InnoDB: MySQL is trying to use a table handle"
  959. " but the .ibd file for\n"
  960. "InnoDB: table %s does not exist.\n"
  961. "InnoDB: Have you deleted the .ibd file"
  962. " from the database directory under\n"
  963. "InnoDB: the MySQL datadir, or have you"
  964. " used DISCARD TABLESPACE?\n"
  965. "InnoDB: Look from\n"
  966. "InnoDB: " REFMAN "innodb-troubleshooting.html\n"
  967. "InnoDB: how you can resolve the problem.\n",
  968. prebuilt->table->name);
  969. return(DB_ERROR);
  970. }
  971. if (UNIV_UNLIKELY(prebuilt->magic_n != ROW_PREBUILT_ALLOCATED)) {
  972. fprintf(stderr,
  973. "InnoDB: Error: trying to free a corrupt\n"
  974. "InnoDB: table handle. Magic n %lu, table name ",
  975. (ulong) prebuilt->magic_n);
  976. ut_print_name(stderr, trx, TRUE, prebuilt->table->name);
  977. putc('\n', stderr);
  978. mem_analyze_corruption(prebuilt);
  979. ut_error;
  980. }
  981. if (UNIV_UNLIKELY(srv_created_new_raw || srv_force_recovery)) {
  982. fputs("InnoDB: A new raw disk partition was initialized or\n"
  983. "InnoDB: innodb_force_recovery is on: we do not allow\n"
  984. "InnoDB: database modifications by the user. Shut down\n"
  985. "InnoDB: mysqld and edit my.cnf so that"
  986. " newraw is replaced\n"
  987. "InnoDB: with raw, and innodb_force_... is removed.\n",
  988. stderr);
  989. return(DB_ERROR);
  990. }
  991. trx->op_info = "inserting";
  992. row_mysql_delay_if_needed();
  993. trx_start_if_not_started(trx);
  994. if (node == NULL) {
  995. row_get_prebuilt_insert_row(prebuilt);
  996. node = prebuilt->ins_node;
  997. }
  998. row_mysql_convert_row_to_innobase(node->row, prebuilt, mysql_rec);
  999. savept = trx_savept_take(trx);
  1000. thr = que_fork_get_first_thr(prebuilt->ins_graph);
  1001. if (!prebuilt->mysql_has_locked && !(prebuilt->table->flags & (DICT_TF2_TEMPORARY << DICT_TF2_SHIFT))) {
  1002. fprintf(stderr, "InnoDB: Error: row_insert_for_mysql is called without ha_innobase::external_lock()\n");
  1003. if (trx->mysql_thd != NULL) {
  1004. innobase_mysql_print_thd(stderr, trx->mysql_thd, 600);
  1005. }
  1006. }
  1007. if (prebuilt->sql_stat_start) {
  1008. node->state = INS_NODE_SET_IX_LOCK;
  1009. prebuilt->sql_stat_start = FALSE;
  1010. } else {
  1011. node->state = INS_NODE_ALLOC_ROW_ID;
  1012. }
  1013. que_thr_move_to_run_state_for_mysql(thr, trx);
  1014. run_again:
  1015. thr->run_node = node;
  1016. thr->prev_node = node;
  1017. row_ins_step(thr);
  1018. err = trx->error_state;
  1019. if (err != DB_SUCCESS) {
  1020. que_thr_stop_for_mysql(thr);
  1021. /* TODO: what is this? */ thr->lock_state= QUE_THR_LOCK_ROW;
  1022. was_lock_wait = row_mysql_handle_errors(&err, trx, thr,
  1023. &savept);
  1024. thr->lock_state= QUE_THR_LOCK_NOLOCK;
  1025. if (was_lock_wait) {
  1026. goto run_again;
  1027. }
  1028. trx->op_info = "";
  1029. return((int) err);
  1030. }
  1031. que_thr_stop_for_mysql_no_error(thr, trx);
  1032. if (UNIV_LIKELY(!(trx->fake_changes))) {
  1033. prebuilt->table->stat_n_rows++;
  1034. if (prebuilt->table->stat_n_rows == 0) {
  1035. /* Avoid wrap-over */
  1036. prebuilt->table->stat_n_rows--;
  1037. }
  1038. srv_n_rows_inserted++;
  1039. row_update_statistics_if_needed(prebuilt->table);
  1040. }
  1041. trx->op_info = "";
  1042. return((int) err);
  1043. }
  1044. /*********************************************************************//**
  1045. Builds a dummy query graph used in selects. */
  1046. UNIV_INTERN
  1047. void
  1048. row_prebuild_sel_graph(
  1049. /*===================*/
  1050. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  1051. handle */
  1052. {
  1053. sel_node_t* node;
  1054. ut_ad(prebuilt && prebuilt->trx);
  1055. if (prebuilt->sel_graph == NULL) {
  1056. node = sel_node_create(prebuilt->heap);
  1057. prebuilt->sel_graph = que_node_get_parent(
  1058. pars_complete_graph_for_exec(node,
  1059. prebuilt->trx,
  1060. prebuilt->heap));
  1061. prebuilt->sel_graph->state = QUE_FORK_ACTIVE;
  1062. }
  1063. }
  1064. /*********************************************************************//**
  1065. Creates an query graph node of 'update' type to be used in the MySQL
  1066. interface.
  1067. @return own: update node */
  1068. UNIV_INTERN
  1069. upd_node_t*
  1070. row_create_update_node_for_mysql(
  1071. /*=============================*/
  1072. dict_table_t* table, /*!< in: table to update */
  1073. mem_heap_t* heap) /*!< in: mem heap from which allocated */
  1074. {
  1075. upd_node_t* node;
  1076. node = upd_node_create(heap);
  1077. node->in_mysql_interface = TRUE;
  1078. node->is_delete = FALSE;
  1079. node->searched_update = FALSE;
  1080. node->select = NULL;
  1081. node->pcur = btr_pcur_create_for_mysql();
  1082. node->table = table;
  1083. node->update = upd_create(dict_table_get_n_cols(table), heap);
  1084. node->update_n_fields = dict_table_get_n_cols(table);
  1085. UT_LIST_INIT(node->columns);
  1086. node->has_clust_rec_x_lock = TRUE;
  1087. node->cmpl_info = 0;
  1088. node->table_sym = NULL;
  1089. node->col_assign_list = NULL;
  1090. return(node);
  1091. }
  1092. /*********************************************************************//**
  1093. Gets pointer to a prebuilt update vector used in updates. If the update
  1094. graph has not yet been built in the prebuilt struct, then this function
  1095. first builds it.
  1096. @return prebuilt update vector */
  1097. UNIV_INTERN
  1098. upd_t*
  1099. row_get_prebuilt_update_vector(
  1100. /*===========================*/
  1101. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  1102. handle */
  1103. {
  1104. dict_table_t* table = prebuilt->table;
  1105. upd_node_t* node;
  1106. ut_ad(prebuilt && table && prebuilt->trx);
  1107. if (prebuilt->upd_node == NULL) {
  1108. /* Not called before for this handle: create an update node
  1109. and query graph to the prebuilt struct */
  1110. node = row_create_update_node_for_mysql(table, prebuilt->heap);
  1111. prebuilt->upd_node = node;
  1112. prebuilt->upd_graph = que_node_get_parent(
  1113. pars_complete_graph_for_exec(node,
  1114. prebuilt->trx,
  1115. prebuilt->heap));
  1116. prebuilt->upd_graph->state = QUE_FORK_ACTIVE;
  1117. }
  1118. return(prebuilt->upd_node->update);
  1119. }
  1120. /*********************************************************************//**
  1121. Does an update or delete of a row for MySQL.
  1122. @return error code or DB_SUCCESS */
  1123. UNIV_INTERN
  1124. int
  1125. row_update_for_mysql(
  1126. /*=================*/
  1127. byte* mysql_rec, /*!< in: the row to be updated, in
  1128. the MySQL format */
  1129. row_prebuilt_t* prebuilt) /*!< in: prebuilt struct in MySQL
  1130. handle */
  1131. {
  1132. trx_savept_t savept;
  1133. ulint err;
  1134. que_thr_t* thr;
  1135. ibool was_lock_wait;
  1136. dict_index_t* clust_index;
  1137. /* ulint ref_len; */
  1138. upd_node_t* node;
  1139. dict_table_t* table = prebuilt->table;
  1140. trx_t* trx = prebuilt->trx;
  1141. ut_ad(prebuilt && trx);
  1142. UT_NOT_USED(mysql_rec);
  1143. if (prebuilt->table->ibd_file_missing) {
  1144. ut_print_timestamp(stderr);
  1145. fprintf(stderr, " InnoDB: Error:\n"
  1146. "InnoDB: MySQL is trying to use a table handle"
  1147. " but the .ibd file for\n"
  1148. "InnoDB: table %s does not exist.\n"
  1149. "InnoDB: Have you deleted the .ibd file"
  1150. " from the database directory under\n"
  1151. "InnoDB: the MySQL datadir, or have you"
  1152. " used DISCARD TABLESPACE?\n"
  1153. "InnoDB: Look from\n"
  1154. "InnoDB: " REFMAN "innodb-troubleshooting.html\n"
  1155. "InnoDB: how you can resolve the problem.\n",
  1156. prebuilt->table->name);
  1157. return(DB_ERROR);
  1158. }
  1159. if (UNIV_UNLIKELY(prebuilt->magic_n != ROW_PREBUILT_ALLOCATED)) {
  1160. fprintf(stderr,
  1161. "InnoDB: Error: trying to free a corrupt\n"
  1162. "InnoDB: table handle. Magic n %lu, table name ",
  1163. (ulong) prebuilt->magic_n);
  1164. ut_print_name(stderr, trx, TRUE, prebuilt->table->name);
  1165. putc('\n', stderr);
  1166. mem_analyze_corruption(prebuilt);
  1167. ut_error;
  1168. }
  1169. if (UNIV_UNLIKELY(srv_created_new_raw || srv_force_recovery)) {
  1170. fputs("InnoDB: A new raw disk partition was initialized or\n"
  1171. "InnoDB: innodb_force_recovery is on: we do not allow\n"
  1172. "InnoDB: database modifications by the user. Shut down\n"
  1173. "InnoDB: mysqld and edit my.cnf so that newraw"
  1174. " is replaced\n"
  1175. "InnoDB: with raw, and innodb_force_... is removed.\n",
  1176. stderr);
  1177. return(DB_ERROR);
  1178. }
  1179. DEBUG_SYNC_C("innodb_row_update_for_mysql_begin");
  1180. trx->op_info = "updating or deleting";
  1181. row_mysql_delay_if_needed();
  1182. trx_start_if_not_started(trx);
  1183. node = prebuilt->upd_node;
  1184. clust_index = dict_table_get_first_index(table);
  1185. if (prebuilt->pcur.btr_cur.index == clust_index) {
  1186. btr_pcur_copy_stored_position(node->pcur, &prebuilt->pcur);
  1187. } else {
  1188. btr_pcur_copy_stored_position(node->pcur,
  1189. &prebuilt->clust_pcur);
  1190. }
  1191. ut_a(node->pcur->rel_pos == BTR_PCUR_ON);
  1192. /* MySQL seems to call rnd_pos before updating each row it
  1193. has cached: we can get the correct cursor position from
  1194. prebuilt->pcur; NOTE that we cannot build the row reference
  1195. from mysql_rec if the clustered index was automatically
  1196. generated for the table: MySQL does not know anything about
  1197. the row id used as the clustered index key */
  1198. savept = trx_savept_take(trx);
  1199. thr = que_fork_get_first_thr(prebuilt->upd_graph);
  1200. node->state = UPD_NODE_UPDATE_CLUSTERED;
  1201. ut_ad(!prebuilt->sql_stat_start);
  1202. que_thr_move_to_run_state_for_mysql(thr, trx);
  1203. run_again:
  1204. thr->run_node = node;
  1205. thr->prev_node = node;
  1206. thr->fk_cascade_depth = 0;
  1207. row_upd_step(thr);
  1208. err = trx->error_state;
  1209. /* Reset fk_cascade_depth back to 0 */
  1210. thr->fk_cascade_depth = 0;
  1211. if (err != DB_SUCCESS) {
  1212. que_thr_stop_for_mysql(thr);
  1213. if (err == DB_RECORD_NOT_FOUND) {
  1214. trx->error_state = DB_SUCCESS;
  1215. trx->op_info = "";
  1216. return((int) err);
  1217. }
  1218. thr->lock_state= QUE_THR_LOCK_ROW;
  1219. was_lock_wait = row_mysql_handle_errors(&err, trx, thr,
  1220. &savept);
  1221. thr->lock_state= QUE_THR_LOCK_NOLOCK;
  1222. if (was_lock_wait) {
  1223. goto run_again;
  1224. }
  1225. trx->op_info = "";
  1226. return((int) err);
  1227. }
  1228. que_thr_stop_for_mysql_no_error(thr, trx);
  1229. if (UNIV_UNLIKELY(trx->fake_changes)) {
  1230. trx->op_info = "";
  1231. return((int) err);
  1232. }
  1233. if (node->is_delete) {
  1234. if (prebuilt->table->stat_n_rows > 0) {
  1235. prebuilt->table->stat_n_rows--;
  1236. }
  1237. srv_n_rows_deleted++;
  1238. } else {
  1239. srv_n_rows_updated++;
  1240. }
  1241. /* We update table statistics only if it is a DELETE or UPDATE
  1242. that changes indexed columns, UPDATEs that change only non-indexed
  1243. columns would not affect statistics. */
  1244. if (node->is_delete || !(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
  1245. row_update_statistics_if_needed(prebuilt->table);
  1246. }
  1247. trx->op_info = "";
  1248. return((int) err);
  1249. }
  1250. /*********************************************************************//**
  1251. This can only be used when srv_locks_unsafe_for_binlog is TRUE or this
  1252. session is using a READ COMMITTED or READ UNCOMMITTED isolation level.
  1253. Before calling this function row_search_for_mysql() must have
  1254. initialized prebuilt->new_rec_locks to store the information which new
  1255. record locks really were set. This function removes a newly set
  1256. clustered index record lock under prebuilt->pcur or
  1257. prebuilt->clust_pcur. Thus, this implements a 'mini-rollback' that
  1258. releases the latest clustered index record lock we set.
  1259. @return error code or DB_SUCCESS */
  1260. UNIV_INTERN
  1261. int
  1262. row_unlock_for_mysql(
  1263. /*=================*/
  1264. row_prebuilt_t* prebuilt, /*!< in/out: prebuilt struct in MySQL
  1265. handle */
  1266. ibool has_latches_on_recs)/*!< in: TRUE if called so
  1267. that we have the latches on
  1268. the records under pcur and
  1269. clust_pcur, and we do not need
  1270. to reposition the cursors. */
  1271. {
  1272. btr_pcur_t* pcur = &prebuilt->pcur;
  1273. btr_pcur_t* clust_pcur = &prebuilt->clust_pcur;
  1274. trx_t* trx = prebuilt->trx;
  1275. ut_ad(prebuilt && trx);
  1276. if (UNIV_UNLIKELY
  1277. (!srv_locks_unsafe_for_binlog
  1278. && trx->isolation_level > TRX_ISO_READ_COMMITTED)) {
  1279. fprintf(stderr,
  1280. "InnoDB: Error: calling row_unlock_for_mysql though\n"
  1281. "InnoDB: innodb_locks_unsafe_for_binlog is FALSE and\n"
  1282. "InnoDB: this session is not using"
  1283. " READ COMMITTED isolation level.\n");
  1284. return(DB_SUCCESS);
  1285. }
  1286. trx->op_info = "unlock_row";
  1287. if (prebuilt->new_rec_locks >= 1) {
  1288. const rec_t* rec;
  1289. dict_index_t* index;
  1290. trx_id_t rec_trx_id;
  1291. mtr_t mtr;
  1292. mtr_start(&mtr);
  1293. /* Restore the cursor position and find the record */
  1294. if (!has_latches_on_recs) {
  1295. btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, &mtr);
  1296. }
  1297. rec = btr_pcur_get_rec(pcur);
  1298. index = btr_pcur_get_btr_cur(pcur)->index;
  1299. if (prebuilt->new_rec_locks >= 2) {
  1300. /* Restore the cursor position and find the record
  1301. in the clustered index. */
  1302. if (!has_latches_on_recs) {
  1303. btr_pcur_restore_position(BTR_SEARCH_LEAF,
  1304. clust_pcur, &mtr);
  1305. }
  1306. rec = btr_pcur_get_rec(clust_pcur);
  1307. index = btr_pcur_get_btr_cur(clust_pcur)->index;
  1308. }
  1309. if (UNIV_UNLIKELY(!dict_index_is_clust(index))) {
  1310. /* This is not a clustered index record. We
  1311. do not know how to unlock the record. */
  1312. goto no_unlock;
  1313. }
  1314. /* If the record has been modified by this
  1315. transaction, do not unlock it. */
  1316. if (index->trx_id_offset) {
  1317. rec_trx_id = trx_read_trx_id(rec
  1318. + index->trx_id_offset);
  1319. } else {
  1320. mem_heap_t* heap = NULL;
  1321. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  1322. ulint* offsets = offsets_;
  1323. rec_offs_init(offsets_);
  1324. offsets = rec_get_offsets(rec, index, offsets,
  1325. ULINT_UNDEFINED, &heap);
  1326. rec_trx_id = row_get_rec_trx_id(rec, index, offsets);
  1327. if (UNIV_LIKELY_NULL(heap)) {
  1328. mem_heap_free(heap);
  1329. }
  1330. }
  1331. if (rec_trx_id != trx->id) {
  1332. /* We did not update the record: unlock it */
  1333. rec = btr_pcur_get_rec(pcur);
  1334. index = btr_pcur_get_btr_cur(pcur)->index;
  1335. lock_rec_unlock(trx, btr_pcur_get_block(pcur),
  1336. rec, prebuilt->select_lock_type);
  1337. if (prebuilt->new_rec_locks >= 2) {
  1338. rec = btr_pcur_get_rec(clust_pcur);
  1339. index = btr_pcur_get_btr_cur(clust_pcur)->index;
  1340. lock_rec_unlock(trx,
  1341. btr_pcur_get_block(clust_pcur),
  1342. rec,
  1343. prebuilt->select_lock_type);
  1344. }
  1345. }
  1346. no_unlock:
  1347. mtr_commit(&mtr);
  1348. }
  1349. trx->op_info = "";
  1350. return(DB_SUCCESS);
  1351. }
  1352. /**********************************************************************//**
  1353. Does a cascaded delete or set null in a foreign key operation.
  1354. @return error code or DB_SUCCESS */
  1355. UNIV_INTERN
  1356. ulint
  1357. row_update_cascade_for_mysql(
  1358. /*=========================*/
  1359. que_thr_t* thr, /*!< in: query thread */
  1360. upd_node_t* node, /*!< in: update node used in the cascade
  1361. or set null operation */
  1362. dict_table_t* table) /*!< in: table where we do the operation */
  1363. {
  1364. ulint err;
  1365. trx_t* trx;
  1366. trx = thr_get_trx(thr);
  1367. /* Increment fk_cascade_depth to record the recursive call depth on
  1368. a single update/delete that affects multiple tables chained
  1369. together with foreign key relations. */
  1370. thr->fk_cascade_depth++;
  1371. if (thr->fk_cascade_depth > FK_MAX_CASCADE_DEL) {
  1372. return (DB_FOREIGN_EXCEED_MAX_CASCADE);
  1373. }
  1374. run_again:
  1375. thr->run_node = node;
  1376. thr->prev_node = node;
  1377. row_upd_step(thr);
  1378. /* The recursive call for cascading update/delete happens
  1379. in above row_upd_step(), reset the counter once we come
  1380. out of the recursive call, so it does not accumulate for
  1381. different row deletes */
  1382. thr->fk_cascade_depth = 0;
  1383. err = trx->error_state;
  1384. /* Note that the cascade node is a subnode of another InnoDB
  1385. query graph node. We do a normal lock wait in this node, but
  1386. all errors are handled by the parent node. */
  1387. if (err == DB_LOCK_WAIT) {
  1388. /* Handle lock wait here */
  1389. que_thr_stop_for_mysql(thr);
  1390. srv_suspend_mysql_thread(thr);
  1391. /* Note that a lock wait may also end in a lock wait timeout,
  1392. or this transaction is picked as a victim in selective
  1393. deadlock resolution */
  1394. if (trx->error_state != DB_SUCCESS) {
  1395. return(trx->error_state);
  1396. }
  1397. /* Retry operation after a normal lock wait */
  1398. goto run_again;
  1399. }
  1400. if (err != DB_SUCCESS) {
  1401. return(err);
  1402. }
  1403. if (UNIV_UNLIKELY((trx->fake_changes))) {
  1404. return(err);
  1405. }
  1406. if (node->is_delete) {
  1407. if (table->stat_n_rows > 0) {
  1408. table->stat_n_rows--;
  1409. }
  1410. srv_n_rows_deleted++;
  1411. } else {
  1412. srv_n_rows_updated++;
  1413. }
  1414. row_update_statistics_if_needed(table);
  1415. return(err);
  1416. }
  1417. /*********************************************************************//**
  1418. Checks if a table is such that we automatically created a clustered
  1419. index on it (on row id).
  1420. @return TRUE if the clustered index was generated automatically */
  1421. UNIV_INTERN
  1422. ibool
  1423. row_table_got_default_clust_index(
  1424. /*==============================*/
  1425. const dict_table_t* table) /*!< in: table */
  1426. {
  1427. const dict_index_t* clust_index;
  1428. clust_index = dict_table_get_first_index(table);
  1429. return(dict_index_get_nth_col(clust_index, 0)->mtype == DATA_SYS);
  1430. }
  1431. /*********************************************************************//**
  1432. Locks the data dictionary in shared mode from modifications, for performing
  1433. foreign key check, rollback, or other operation invisible to MySQL. */
  1434. UNIV_INTERN
  1435. void
  1436. row_mysql_freeze_data_dictionary_func(
  1437. /*==================================*/
  1438. trx_t* trx, /*!< in/out: transaction */
  1439. const char* file, /*!< in: file name */
  1440. ulint line) /*!< in: line number */
  1441. {
  1442. ut_a(trx->dict_operation_lock_mode == 0);
  1443. rw_lock_s_lock_inline(&dict_operation_lock, 0, file, line);
  1444. trx->dict_operation_lock_mode = RW_S_LATCH;
  1445. }
  1446. /*********************************************************************//**
  1447. Unlocks the data dictionary shared lock. */
  1448. UNIV_INTERN
  1449. void
  1450. row_mysql_unfreeze_data_dictionary(
  1451. /*===============================*/
  1452. trx_t* trx) /*!< in/out: transaction */
  1453. {
  1454. ut_a(trx->dict_operation_lock_mode == RW_S_LATCH);
  1455. rw_lock_s_unlock(&dict_operation_lock);
  1456. trx->dict_operation_lock_mode = 0;
  1457. }
  1458. /*********************************************************************//**
  1459. Locks the data dictionary exclusively for performing a table create or other
  1460. data dictionary modification operation. */
  1461. UNIV_INTERN
  1462. void
  1463. row_mysql_lock_data_dictionary_func(
  1464. /*================================*/
  1465. trx_t* trx, /*!< in/out: transaction */
  1466. const char* file, /*!< in: file name */
  1467. ulint line) /*!< in: line number */
  1468. {
  1469. ut_a(trx->dict_operation_lock_mode == 0
  1470. || trx->dict_operation_lock_mode == RW_X_LATCH);
  1471. /* Serialize data dictionary operations with dictionary mutex:
  1472. no deadlocks or lock waits can occur then in these operations */
  1473. rw_lock_x_lock_inline(&dict_operation_lock, 0, file, line);
  1474. trx->dict_operation_lock_mode = RW_X_LATCH;
  1475. mutex_enter(&(dict_sys->mutex));
  1476. }
  1477. /*********************************************************************//**
  1478. Unlocks the data dictionary exclusive lock. */
  1479. UNIV_INTERN
  1480. void
  1481. row_mysql_unlock_data_dictionary(
  1482. /*=============================*/
  1483. trx_t* trx) /*!< in/out: transaction */
  1484. {
  1485. ut_a(trx->dict_operation_lock_mode == RW_X_LATCH);
  1486. /* Serialize data dictionary operations with dictionary mutex:
  1487. no deadlocks can occur then in these operations */
  1488. mutex_exit(&(dict_sys->mutex));
  1489. rw_lock_x_unlock(&dict_operation_lock);
  1490. trx->dict_operation_lock_mode = 0;
  1491. }
  1492. /*********************************************************************//**
  1493. Creates a table for MySQL. If the name of the table ends in
  1494. one of "innodb_monitor", "innodb_lock_monitor", "innodb_tablespace_monitor",
  1495. "innodb_table_monitor", then this will also start the printing of monitor
  1496. output by the master thread. If the table name ends in "innodb_mem_validate",
  1497. InnoDB will try to invoke mem_validate(). On failure the transaction will
  1498. be rolled back and the 'table' object will be freed.
  1499. @return error code or DB_SUCCESS */
  1500. UNIV_INTERN
  1501. int
  1502. row_create_table_for_mysql(
  1503. /*=======================*/
  1504. dict_table_t* table, /*!< in, own: table definition
  1505. (will be freed) */
  1506. trx_t* trx) /*!< in: transaction handle */
  1507. {
  1508. tab_node_t* node;
  1509. mem_heap_t* heap;
  1510. que_thr_t* thr;
  1511. const char* table_name;
  1512. ulint table_name_len;
  1513. ulint err;
  1514. #ifdef UNIV_SYNC_DEBUG
  1515. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1516. #endif /* UNIV_SYNC_DEBUG */
  1517. ut_ad(mutex_own(&(dict_sys->mutex)));
  1518. ut_ad(trx->dict_operation_lock_mode == RW_X_LATCH);
  1519. if (srv_created_new_raw) {
  1520. fputs("InnoDB: A new raw disk partition was initialized:\n"
  1521. "InnoDB: we do not allow database modifications"
  1522. " by the user.\n"
  1523. "InnoDB: Shut down mysqld and edit my.cnf so that newraw"
  1524. " is replaced with raw.\n", stderr);
  1525. err_exit:
  1526. dict_mem_table_free(table);
  1527. trx_commit_for_mysql(trx);
  1528. return(DB_ERROR);
  1529. }
  1530. trx->op_info = "creating table";
  1531. if (row_mysql_is_system_table(table->name)) {
  1532. fprintf(stderr,
  1533. "InnoDB: Error: trying to create a MySQL system"
  1534. " table %s of type InnoDB.\n"
  1535. "InnoDB: MySQL system tables must be"
  1536. " of the MyISAM type!\n",
  1537. table->name);
  1538. goto err_exit;
  1539. }
  1540. trx_start_if_not_started(trx);
  1541. /* The table name is prefixed with the database name and a '/'.
  1542. Certain table names starting with 'innodb_' have their special
  1543. meaning regardless of the database name. Thus, we need to
  1544. ignore the database name prefix in the comparisons. */
  1545. table_name = strchr(table->name, '/');
  1546. ut_a(table_name);
  1547. table_name++;
  1548. table_name_len = strlen(table_name) + 1;
  1549. if (STR_EQ(table_name, table_name_len, S_innodb_monitor)) {
  1550. /* Table equals "innodb_monitor":
  1551. start monitor prints */
  1552. srv_print_innodb_monitor = TRUE;
  1553. /* The lock timeout monitor thread also takes care
  1554. of InnoDB monitor prints */
  1555. os_event_set(srv_lock_timeout_thread_event);
  1556. } else if (STR_EQ(table_name, table_name_len,
  1557. S_innodb_lock_monitor)) {
  1558. srv_print_innodb_monitor = TRUE;
  1559. srv_print_innodb_lock_monitor = TRUE;
  1560. os_event_set(srv_lock_timeout_thread_event);
  1561. } else if (STR_EQ(table_name, table_name_len,
  1562. S_innodb_tablespace_monitor)) {
  1563. srv_print_innodb_tablespace_monitor = TRUE;
  1564. os_event_set(srv_lock_timeout_thread_event);
  1565. } else if (STR_EQ(table_name, table_name_len,
  1566. S_innodb_table_monitor)) {
  1567. srv_print_innodb_table_monitor = TRUE;
  1568. os_event_set(srv_lock_timeout_thread_event);
  1569. } else if (STR_EQ(table_name, table_name_len,
  1570. S_innodb_mem_validate)) {
  1571. /* We define here a debugging feature intended for
  1572. developers */
  1573. fputs("Validating InnoDB memory:\n"
  1574. "to use this feature you must compile InnoDB with\n"
  1575. "UNIV_MEM_DEBUG defined in univ.i and"
  1576. " the server must be\n"
  1577. "quiet because allocation from a mem heap"
  1578. " is not protected\n"
  1579. "by any semaphore.\n", stderr);
  1580. #ifdef UNIV_MEM_DEBUG
  1581. ut_a(mem_validate());
  1582. fputs("Memory validated\n", stderr);
  1583. #else /* UNIV_MEM_DEBUG */
  1584. fputs("Memory NOT validated (recompile with UNIV_MEM_DEBUG)\n",
  1585. stderr);
  1586. #endif /* UNIV_MEM_DEBUG */
  1587. }
  1588. heap = mem_heap_create(512);
  1589. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  1590. node = tab_create_graph_create(table, heap);
  1591. thr = pars_complete_graph_for_exec(node, trx, heap);
  1592. ut_a(thr == que_fork_start_command(que_node_get_parent(thr)));
  1593. que_run_threads(thr);
  1594. err = trx->error_state;
  1595. switch (err) {
  1596. case DB_SUCCESS:
  1597. break;
  1598. case DB_OUT_OF_FILE_SPACE:
  1599. trx->error_state = DB_SUCCESS;
  1600. trx_general_rollback_for_mysql(trx, NULL);
  1601. ut_print_timestamp(stderr);
  1602. fputs(" InnoDB: Warning: cannot create table ",
  1603. stderr);
  1604. ut_print_name(stderr, trx, TRUE, table->name);
  1605. fputs(" because tablespace full\n", stderr);
  1606. if (dict_table_get_low(table->name)) {
  1607. row_drop_table_for_mysql(table->name, trx, FALSE);
  1608. trx_commit_for_mysql(trx);
  1609. } else {
  1610. dict_mem_table_free(table);
  1611. }
  1612. break;
  1613. case DB_TOO_MANY_CONCURRENT_TRXS:
  1614. /* We already have .ibd file here. it should be deleted. */
  1615. if (table->space && !fil_delete_tablespace(table->space,
  1616. FALSE)) {
  1617. ut_print_timestamp(stderr);
  1618. fprintf(stderr,
  1619. " InnoDB: Error: not able to"
  1620. " delete tablespace %lu of table ",
  1621. (ulong) table->space);
  1622. ut_print_name(stderr, trx, TRUE, table->name);
  1623. fputs("!\n", stderr);
  1624. }
  1625. /* fall through */
  1626. case DB_DUPLICATE_KEY:
  1627. default:
  1628. /* We may also get err == DB_ERROR if the .ibd file for the
  1629. table already exists */
  1630. trx->error_state = DB_SUCCESS;
  1631. trx_general_rollback_for_mysql(trx, NULL);
  1632. dict_mem_table_free(table);
  1633. break;
  1634. }
  1635. que_graph_free((que_t*) que_node_get_parent(thr));
  1636. trx->op_info = "";
  1637. return((int) err);
  1638. }
  1639. /*********************************************************************//**
  1640. Does an index creation operation for MySQL. TODO: currently failure
  1641. to create an index results in dropping the whole table! This is no problem
  1642. currently as all indexes must be created at the same time as the table.
  1643. @return error number or DB_SUCCESS */
  1644. UNIV_INTERN
  1645. int
  1646. row_create_index_for_mysql(
  1647. /*=======================*/
  1648. dict_index_t* index, /*!< in, own: index definition
  1649. (will be freed) */
  1650. trx_t* trx, /*!< in: transaction handle */
  1651. const ulint* field_lengths) /*!< in: if not NULL, must contain
  1652. dict_index_get_n_fields(index)
  1653. actual field lengths for the
  1654. index columns, which are
  1655. then checked for not being too
  1656. large. */
  1657. {
  1658. ind_node_t* node;
  1659. mem_heap_t* heap;
  1660. que_thr_t* thr;
  1661. ulint err;
  1662. ulint i;
  1663. ulint len;
  1664. char* table_name;
  1665. dict_table_t* table;
  1666. #ifdef UNIV_SYNC_DEBUG
  1667. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1668. #endif /* UNIV_SYNC_DEBUG */
  1669. ut_ad(mutex_own(&(dict_sys->mutex)));
  1670. trx->op_info = "creating index";
  1671. /* Copy the table name because we may want to drop the
  1672. table later, after the index object is freed (inside
  1673. que_run_threads()) and thus index->table_name is not available. */
  1674. table_name = mem_strdup(index->table_name);
  1675. table = dict_table_get_low(table_name);
  1676. trx_start_if_not_started(trx);
  1677. for (i = 0; i < index->n_def; i++) {
  1678. /* Check that prefix_len and actual length
  1679. < DICT_MAX_INDEX_COL_LEN */
  1680. len = dict_index_get_nth_field(index, i)->prefix_len;
  1681. if (field_lengths && field_lengths[i]) {
  1682. len = ut_max(len, field_lengths[i]);
  1683. }
  1684. /* Column or prefix length exceeds maximum column length */
  1685. if (len > (ulint) DICT_MAX_FIELD_LEN_BY_FORMAT(table)) {
  1686. err = DB_TOO_BIG_INDEX_COL;
  1687. dict_mem_index_free(index);
  1688. goto error_handling;
  1689. }
  1690. }
  1691. heap = mem_heap_create(512);
  1692. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  1693. /* Note that the space id where we store the index is inherited from
  1694. the table in dict_build_index_def_step() in dict0crea.c. */
  1695. node = ind_create_graph_create(index, heap);
  1696. thr = pars_complete_graph_for_exec(node, trx, heap);
  1697. ut_a(thr == que_fork_start_command(que_node_get_parent(thr)));
  1698. que_run_threads(thr);
  1699. err = trx->error_state;
  1700. que_graph_free((que_t*) que_node_get_parent(thr));
  1701. error_handling:
  1702. if (err != DB_SUCCESS) {
  1703. /* We have special error handling here */
  1704. trx->error_state = DB_SUCCESS;
  1705. trx_general_rollback_for_mysql(trx, NULL);
  1706. row_drop_table_for_mysql(table_name, trx, FALSE);
  1707. trx_commit_for_mysql(trx);
  1708. trx->error_state = DB_SUCCESS;
  1709. }
  1710. trx->op_info = "";
  1711. mem_free(table_name);
  1712. return((int) err);
  1713. }
  1714. /*********************************************************************//**
  1715. */
  1716. UNIV_INTERN
  1717. int
  1718. row_insert_stats_for_mysql(
  1719. /*=======================*/
  1720. dict_index_t* index,
  1721. trx_t* trx)
  1722. {
  1723. ind_node_t* node;
  1724. mem_heap_t* heap;
  1725. que_thr_t* thr;
  1726. ulint err;
  1727. //ut_ad(trx->mysql_thread_id == os_thread_get_curr_id());
  1728. trx->op_info = "try to insert rows to SYS_STATS";
  1729. trx_start_if_not_started(trx);
  1730. trx->error_state = DB_SUCCESS;
  1731. heap = mem_heap_create(512);
  1732. node = ind_insert_stats_graph_create(index, heap);
  1733. thr = pars_complete_graph_for_exec(node, trx, heap);
  1734. ut_a(thr == que_fork_start_command(que_node_get_parent(thr)));
  1735. que_run_threads(thr);
  1736. err = trx->error_state;
  1737. que_graph_free((que_t*) que_node_get_parent(thr));
  1738. trx->op_info = "";
  1739. return((int) err);
  1740. }
  1741. /*********************************************************************//**
  1742. */
  1743. UNIV_INTERN
  1744. int
  1745. row_delete_stats_for_mysql(
  1746. /*=============================*/
  1747. dict_index_t* index,
  1748. trx_t* trx)
  1749. {
  1750. pars_info_t* info = pars_info_create();
  1751. trx->op_info = "delete rows from SYS_STATS";
  1752. trx_start_if_not_started(trx);
  1753. trx->error_state = DB_SUCCESS;
  1754. pars_info_add_ull_literal(info, "indexid", index->id);
  1755. return((int) que_eval_sql(info,
  1756. "PROCEDURE DELETE_STATISTICS_PROC () IS\n"
  1757. "BEGIN\n"
  1758. "DELETE FROM SYS_STATS WHERE INDEX_ID = :indexid;\n"
  1759. "END;\n"
  1760. , TRUE, trx));
  1761. }
  1762. /*********************************************************************//**
  1763. Scans a table create SQL string and adds to the data dictionary
  1764. the foreign key constraints declared in the string. This function
  1765. should be called after the indexes for a table have been created.
  1766. Each foreign key constraint must be accompanied with indexes in
  1767. both participating tables. The indexes are allowed to contain more
  1768. fields than mentioned in the constraint. Check also that foreign key
  1769. constraints which reference this table are ok.
  1770. @return error code or DB_SUCCESS */
  1771. UNIV_INTERN
  1772. int
  1773. row_table_add_foreign_constraints(
  1774. /*==============================*/
  1775. trx_t* trx, /*!< in: transaction */
  1776. const char* sql_string, /*!< in: table create statement where
  1777. foreign keys are declared like:
  1778. FOREIGN KEY (a, b) REFERENCES table2(c, d),
  1779. table2 can be written also with the
  1780. database name before it: test.table2 */
  1781. size_t sql_length, /*!< in: length of sql_string */
  1782. const char* name, /*!< in: table full name in the
  1783. normalized form
  1784. database_name/table_name */
  1785. ibool reject_fks) /*!< in: if TRUE, fail with error
  1786. code DB_CANNOT_ADD_CONSTRAINT if
  1787. any foreign keys are found. */
  1788. {
  1789. ulint err;
  1790. ut_ad(mutex_own(&(dict_sys->mutex)));
  1791. #ifdef UNIV_SYNC_DEBUG
  1792. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1793. #endif /* UNIV_SYNC_DEBUG */
  1794. ut_a(sql_string);
  1795. trx->op_info = "adding foreign keys";
  1796. trx_start_if_not_started(trx);
  1797. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  1798. err = dict_create_foreign_constraints(trx, sql_string, sql_length,
  1799. name, reject_fks);
  1800. if (err == DB_SUCCESS) {
  1801. /* Check that also referencing constraints are ok */
  1802. err = dict_load_foreigns(name, FALSE, TRUE);
  1803. }
  1804. if (err != DB_SUCCESS) {
  1805. /* We have special error handling here */
  1806. trx->error_state = DB_SUCCESS;
  1807. trx_general_rollback_for_mysql(trx, NULL);
  1808. row_drop_table_for_mysql(name, trx, FALSE);
  1809. trx_commit_for_mysql(trx);
  1810. trx->error_state = DB_SUCCESS;
  1811. }
  1812. return((int) err);
  1813. }
  1814. /*********************************************************************//**
  1815. Drops a table for MySQL as a background operation. MySQL relies on Unix
  1816. in ALTER TABLE to the fact that the table handler does not remove the
  1817. table before all handles to it has been removed. Furhermore, the MySQL's
  1818. call to drop table must be non-blocking. Therefore we do the drop table
  1819. as a background operation, which is taken care of by the master thread
  1820. in srv0srv.c.
  1821. @return error code or DB_SUCCESS */
  1822. static
  1823. int
  1824. row_drop_table_for_mysql_in_background(
  1825. /*===================================*/
  1826. const char* name) /*!< in: table name */
  1827. {
  1828. ulint error;
  1829. trx_t* trx;
  1830. trx = trx_allocate_for_background();
  1831. /* If the original transaction was dropping a table referenced by
  1832. foreign keys, we must set the following to be able to drop the
  1833. table: */
  1834. trx->check_foreigns = FALSE;
  1835. /* fputs("InnoDB: Error: Dropping table ", stderr);
  1836. ut_print_name(stderr, trx, TRUE, name);
  1837. fputs(" in background drop list\n", stderr); */
  1838. /* Try to drop the table in InnoDB */
  1839. error = row_drop_table_for_mysql(name, trx, FALSE);
  1840. /* Flush the log to reduce probability that the .frm files and
  1841. the InnoDB data dictionary get out-of-sync if the user runs
  1842. with innodb_flush_log_at_trx_commit = 0 */
  1843. log_buffer_flush_to_disk();
  1844. trx_commit_for_mysql(trx);
  1845. trx_free_for_background(trx);
  1846. return((int) error);
  1847. }
  1848. /*********************************************************************//**
  1849. The master thread in srv0srv.c calls this regularly to drop tables which
  1850. we must drop in background after queries to them have ended. Such lazy
  1851. dropping of tables is needed in ALTER TABLE on Unix.
  1852. @return how many tables dropped + remaining tables in list */
  1853. UNIV_INTERN
  1854. ulint
  1855. row_drop_tables_for_mysql_in_background(void)
  1856. /*=========================================*/
  1857. {
  1858. row_mysql_drop_t* drop;
  1859. dict_table_t* table;
  1860. ulint n_tables;
  1861. ulint n_tables_dropped = 0;
  1862. loop:
  1863. mutex_enter(&kernel_mutex);
  1864. if (!row_mysql_drop_list_inited) {
  1865. UT_LIST_INIT(row_mysql_drop_list);
  1866. row_mysql_drop_list_inited = TRUE;
  1867. }
  1868. drop = UT_LIST_GET_FIRST(row_mysql_drop_list);
  1869. n_tables = UT_LIST_GET_LEN(row_mysql_drop_list);
  1870. mutex_exit(&kernel_mutex);
  1871. if (drop == NULL) {
  1872. /* All tables dropped */
  1873. return(n_tables + n_tables_dropped);
  1874. }
  1875. mutex_enter(&(dict_sys->mutex));
  1876. table = dict_table_get_low(drop->table_name);
  1877. mutex_exit(&(dict_sys->mutex));
  1878. if (table == NULL) {
  1879. /* If for some reason the table has already been dropped
  1880. through some other mechanism, do not try to drop it */
  1881. goto already_dropped;
  1882. }
  1883. if (DB_SUCCESS != row_drop_table_for_mysql_in_background(
  1884. drop->table_name)) {
  1885. /* If the DROP fails for some table, we return, and let the
  1886. main thread retry later */
  1887. return(n_tables + n_tables_dropped);
  1888. }
  1889. n_tables_dropped++;
  1890. already_dropped:
  1891. mutex_enter(&kernel_mutex);
  1892. UT_LIST_REMOVE(row_mysql_drop_list, row_mysql_drop_list, drop);
  1893. ut_print_timestamp(stderr);
  1894. fputs(" InnoDB: Dropped table ", stderr);
  1895. ut_print_name(stderr, NULL, TRUE, drop->table_name);
  1896. fputs(" in background drop queue.\n", stderr);
  1897. mem_free(drop->table_name);
  1898. mem_free(drop);
  1899. mutex_exit(&kernel_mutex);
  1900. goto loop;
  1901. }
  1902. /*********************************************************************//**
  1903. Get the background drop list length. NOTE: the caller must own the kernel
  1904. mutex!
  1905. @return how many tables in list */
  1906. UNIV_INTERN
  1907. ulint
  1908. row_get_background_drop_list_len_low(void)
  1909. /*======================================*/
  1910. {
  1911. ut_ad(mutex_own(&kernel_mutex));
  1912. if (!row_mysql_drop_list_inited) {
  1913. UT_LIST_INIT(row_mysql_drop_list);
  1914. row_mysql_drop_list_inited = TRUE;
  1915. }
  1916. return(UT_LIST_GET_LEN(row_mysql_drop_list));
  1917. }
  1918. /*********************************************************************//**
  1919. If a table is not yet in the drop list, adds the table to the list of tables
  1920. which the master thread drops in background. We need this on Unix because in
  1921. ALTER TABLE MySQL may call drop table even if the table has running queries on
  1922. it. Also, if there are running foreign key checks on the table, we drop the
  1923. table lazily.
  1924. @return TRUE if the table was not yet in the drop list, and was added there */
  1925. static
  1926. ibool
  1927. row_add_table_to_background_drop_list(
  1928. /*==================================*/
  1929. const char* name) /*!< in: table name */
  1930. {
  1931. row_mysql_drop_t* drop;
  1932. mutex_enter(&kernel_mutex);
  1933. if (!row_mysql_drop_list_inited) {
  1934. UT_LIST_INIT(row_mysql_drop_list);
  1935. row_mysql_drop_list_inited = TRUE;
  1936. }
  1937. /* Look if the table already is in the drop list */
  1938. drop = UT_LIST_GET_FIRST(row_mysql_drop_list);
  1939. while (drop != NULL) {
  1940. if (strcmp(drop->table_name, name) == 0) {
  1941. /* Already in the list */
  1942. mutex_exit(&kernel_mutex);
  1943. return(FALSE);
  1944. }
  1945. drop = UT_LIST_GET_NEXT(row_mysql_drop_list, drop);
  1946. }
  1947. drop = mem_alloc(sizeof(row_mysql_drop_t));
  1948. drop->table_name = mem_strdup(name);
  1949. UT_LIST_ADD_LAST(row_mysql_drop_list, row_mysql_drop_list, drop);
  1950. /* fputs("InnoDB: Adding table ", stderr);
  1951. ut_print_name(stderr, trx, TRUE, drop->table_name);
  1952. fputs(" to background drop list\n", stderr); */
  1953. mutex_exit(&kernel_mutex);
  1954. return(TRUE);
  1955. }
  1956. /*********************************************************************//**
  1957. Discards the tablespace of a table which stored in an .ibd file. Discarding
  1958. means that this function deletes the .ibd file and assigns a new table id for
  1959. the table. Also the flag table->ibd_file_missing is set TRUE.
  1960. @return error code or DB_SUCCESS */
  1961. UNIV_INTERN
  1962. int
  1963. row_discard_tablespace_for_mysql(
  1964. /*=============================*/
  1965. const char* name, /*!< in: table name */
  1966. trx_t* trx) /*!< in: transaction handle */
  1967. {
  1968. dict_foreign_t* foreign;
  1969. table_id_t new_id;
  1970. dict_table_t* table;
  1971. ibool success;
  1972. ulint err;
  1973. pars_info_t* info = NULL;
  1974. /* How do we prevent crashes caused by ongoing operations on
  1975. the table? Old operations could try to access non-existent
  1976. pages.
  1977. 1) SQL queries, INSERT, SELECT, ...: we must get an exclusive
  1978. MySQL table lock on the table before we can do DISCARD
  1979. TABLESPACE. Then there are no running queries on the table.
  1980. 2) Purge and rollback: we assign a new table id for the
  1981. table. Since purge and rollback look for the table based on
  1982. the table id, they see the table as 'dropped' and discard
  1983. their operations.
  1984. 3) Insert buffer: we remove all entries for the tablespace in
  1985. the insert buffer tree; as long as the tablespace mem object
  1986. does not exist, ongoing insert buffer page merges are
  1987. discarded in buf0rea.c. If we recreate the tablespace mem
  1988. object with IMPORT TABLESPACE later, then the tablespace will
  1989. have the same id, but the tablespace_version field in the mem
  1990. object is different, and ongoing old insert buffer page merges
  1991. get discarded.
  1992. 4) Linear readahead and random readahead: we use the same
  1993. method as in 3) to discard ongoing operations.
  1994. 5) FOREIGN KEY operations: if
  1995. table->n_foreign_key_checks_running > 0, we do not allow the
  1996. discard. We also reserve the data dictionary latch. */
  1997. trx->op_info = "discarding tablespace";
  1998. trx_start_if_not_started(trx);
  1999. /* Serialize data dictionary operations with dictionary mutex:
  2000. no deadlocks can occur then in these operations */
  2001. row_mysql_lock_data_dictionary(trx);
  2002. table = dict_table_get_low(name);
  2003. if (!table) {
  2004. err = DB_TABLE_NOT_FOUND;
  2005. goto funct_exit;
  2006. }
  2007. if (table->space == 0) {
  2008. ut_print_timestamp(stderr);
  2009. fputs(" InnoDB: Error: table ", stderr);
  2010. ut_print_name(stderr, trx, TRUE, name);
  2011. fputs("\n"
  2012. "InnoDB: is in the system tablespace 0"
  2013. " which cannot be discarded\n", stderr);
  2014. err = DB_ERROR;
  2015. goto funct_exit;
  2016. }
  2017. if (table->n_foreign_key_checks_running > 0) {
  2018. ut_print_timestamp(stderr);
  2019. fputs(" InnoDB: You are trying to DISCARD table ", stderr);
  2020. ut_print_name(stderr, trx, TRUE, table->name);
  2021. fputs("\n"
  2022. "InnoDB: though there is a foreign key check"
  2023. " running on it.\n"
  2024. "InnoDB: Cannot discard the table.\n",
  2025. stderr);
  2026. err = DB_ERROR;
  2027. goto funct_exit;
  2028. }
  2029. /* Check if the table is referenced by foreign key constraints from
  2030. some other table (not the table itself) */
  2031. foreign = UT_LIST_GET_FIRST(table->referenced_list);
  2032. while (foreign && foreign->foreign_table == table) {
  2033. foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
  2034. }
  2035. if (foreign && trx->check_foreigns) {
  2036. FILE* ef = dict_foreign_err_file;
  2037. /* We only allow discarding a referenced table if
  2038. FOREIGN_KEY_CHECKS is set to 0 */
  2039. err = DB_CANNOT_DROP_CONSTRAINT;
  2040. mutex_enter(&dict_foreign_err_mutex);
  2041. rewind(ef);
  2042. ut_print_timestamp(ef);
  2043. fputs(" Cannot DISCARD table ", ef);
  2044. ut_print_name(stderr, trx, TRUE, name);
  2045. fputs("\n"
  2046. "because it is referenced by ", ef);
  2047. ut_print_name(stderr, trx, TRUE, foreign->foreign_table_name);
  2048. putc('\n', ef);
  2049. mutex_exit(&dict_foreign_err_mutex);
  2050. goto funct_exit;
  2051. }
  2052. dict_hdr_get_new_id(&new_id, NULL, NULL);
  2053. /* Remove all locks except the table-level S and X locks. */
  2054. lock_remove_all_on_table(table, FALSE);
  2055. info = pars_info_create();
  2056. pars_info_add_str_literal(info, "table_name", name);
  2057. pars_info_add_ull_literal(info, "new_id", new_id);
  2058. err = que_eval_sql(info,
  2059. "PROCEDURE DISCARD_TABLESPACE_PROC () IS\n"
  2060. "old_id CHAR;\n"
  2061. "BEGIN\n"
  2062. "SELECT ID INTO old_id\n"
  2063. "FROM SYS_TABLES\n"
  2064. "WHERE NAME = :table_name\n"
  2065. "LOCK IN SHARE MODE;\n"
  2066. "IF (SQL % NOTFOUND) THEN\n"
  2067. " COMMIT WORK;\n"
  2068. " RETURN;\n"
  2069. "END IF;\n"
  2070. "UPDATE SYS_TABLES SET ID = :new_id\n"
  2071. " WHERE ID = old_id;\n"
  2072. "UPDATE SYS_COLUMNS SET TABLE_ID = :new_id\n"
  2073. " WHERE TABLE_ID = old_id;\n"
  2074. "UPDATE SYS_INDEXES SET TABLE_ID = :new_id\n"
  2075. " WHERE TABLE_ID = old_id;\n"
  2076. "COMMIT WORK;\n"
  2077. "END;\n"
  2078. , FALSE, trx);
  2079. if (err != DB_SUCCESS) {
  2080. trx->error_state = DB_SUCCESS;
  2081. trx_general_rollback_for_mysql(trx, NULL);
  2082. trx->error_state = DB_SUCCESS;
  2083. } else {
  2084. dict_table_change_id_in_cache(table, new_id);
  2085. success = fil_discard_tablespace(table->space);
  2086. if (!success) {
  2087. trx->error_state = DB_SUCCESS;
  2088. trx_general_rollback_for_mysql(trx, NULL);
  2089. trx->error_state = DB_SUCCESS;
  2090. err = DB_ERROR;
  2091. } else {
  2092. dict_index_t* index;
  2093. /* Set the flag which tells that now it is legal to
  2094. IMPORT a tablespace for this table */
  2095. table->tablespace_discarded = TRUE;
  2096. table->ibd_file_missing = TRUE;
  2097. /* check adaptive hash entries */
  2098. index = dict_table_get_first_index(table);
  2099. while (index) {
  2100. ulint ref_count = btr_search_info_get_ref_count(index->search_info, index->id);
  2101. if (ref_count) {
  2102. fprintf(stderr, "InnoDB: Warning:"
  2103. " hash index ref_count (%lu) is not zero"
  2104. " after fil_discard_tablespace().\n"
  2105. "index: \"%s\""
  2106. " table: \"%s\"\n",
  2107. ref_count,
  2108. index->name,
  2109. table->name);
  2110. }
  2111. index = dict_table_get_next_index(index);
  2112. }
  2113. }
  2114. }
  2115. funct_exit:
  2116. trx_commit_for_mysql(trx);
  2117. row_mysql_unlock_data_dictionary(trx);
  2118. trx->op_info = "";
  2119. return((int) err);
  2120. }
  2121. /*****************************************************************//**
  2122. Imports a tablespace. The space id in the .ibd file must match the space id
  2123. of the table in the data dictionary.
  2124. @return error code or DB_SUCCESS */
  2125. UNIV_INTERN
  2126. int
  2127. row_import_tablespace_for_mysql(
  2128. /*============================*/
  2129. const char* name, /*!< in: table name */
  2130. trx_t* trx) /*!< in: transaction handle */
  2131. {
  2132. dict_table_t* table;
  2133. ibool success;
  2134. ib_uint64_t current_lsn;
  2135. ulint err = DB_SUCCESS;
  2136. trx_start_if_not_started(trx);
  2137. trx->op_info = "importing tablespace";
  2138. current_lsn = log_get_lsn();
  2139. /* Enlarge the fatal lock wait timeout during import. */
  2140. mutex_enter(&kernel_mutex);
  2141. srv_fatal_semaphore_wait_threshold += 7200; /* 2 hours */
  2142. mutex_exit(&kernel_mutex);
  2143. /* It is possible, though very improbable, that the lsn's in the
  2144. tablespace to be imported have risen above the current system lsn, if
  2145. a lengthy purge, ibuf merge, or rollback was performed on a backup
  2146. taken with ibbackup. If that is the case, reset page lsn's in the
  2147. file. We assume that mysqld was shut down after it performed these
  2148. cleanup operations on the .ibd file, so that it stamped the latest lsn
  2149. to the FIL_PAGE_FILE_FLUSH_LSN in the first page of the .ibd file.
  2150. TODO: reset also the trx id's in clustered index records and write
  2151. a new space id to each data page. That would allow us to import clean
  2152. .ibd files from another MySQL installation. */
  2153. success = fil_reset_too_high_lsns(name, current_lsn);
  2154. if (!success) {
  2155. ut_print_timestamp(stderr);
  2156. fputs(" InnoDB: Error: cannot reset lsn's in table ", stderr);
  2157. ut_print_name(stderr, trx, TRUE, name);
  2158. fputs("\n"
  2159. "InnoDB: in ALTER TABLE ... IMPORT TABLESPACE\n",
  2160. stderr);
  2161. err = DB_ERROR;
  2162. row_mysql_lock_data_dictionary(trx);
  2163. goto funct_exit;
  2164. }
  2165. /* Serialize data dictionary operations with dictionary mutex:
  2166. no deadlocks can occur then in these operations */
  2167. row_mysql_lock_data_dictionary(trx);
  2168. table = dict_table_get_low(name);
  2169. if (!table) {
  2170. ut_print_timestamp(stderr);
  2171. fputs(" InnoDB: table ", stderr);
  2172. ut_print_name(stderr, trx, TRUE, name);
  2173. fputs("\n"
  2174. "InnoDB: does not exist in the InnoDB data dictionary\n"
  2175. "InnoDB: in ALTER TABLE ... IMPORT TABLESPACE\n",
  2176. stderr);
  2177. err = DB_TABLE_NOT_FOUND;
  2178. goto funct_exit;
  2179. }
  2180. if (table->space == 0) {
  2181. ut_print_timestamp(stderr);
  2182. fputs(" InnoDB: Error: table ", stderr);
  2183. ut_print_name(stderr, trx, TRUE, name);
  2184. fputs("\n"
  2185. "InnoDB: is in the system tablespace 0"
  2186. " which cannot be imported\n", stderr);
  2187. err = DB_ERROR;
  2188. goto funct_exit;
  2189. }
  2190. if (!table->tablespace_discarded) {
  2191. ut_print_timestamp(stderr);
  2192. fputs(" InnoDB: Error: you are trying to"
  2193. " IMPORT a tablespace\n"
  2194. "InnoDB: ", stderr);
  2195. ut_print_name(stderr, trx, TRUE, name);
  2196. fputs(", though you have not called DISCARD on it yet\n"
  2197. "InnoDB: during the lifetime of the mysqld process!\n",
  2198. stderr);
  2199. err = DB_ERROR;
  2200. goto funct_exit;
  2201. }
  2202. /* Play safe and remove all insert buffer entries, though we should
  2203. have removed them already when DISCARD TABLESPACE was called */
  2204. ibuf_delete_for_discarded_space(table->space);
  2205. success = fil_open_single_table_tablespace(
  2206. TRUE, table->space,
  2207. table->flags == DICT_TF_COMPACT ? 0 : table->flags,
  2208. table->name, trx);
  2209. if (success) {
  2210. table->ibd_file_missing = FALSE;
  2211. table->tablespace_discarded = FALSE;
  2212. } else {
  2213. if (table->ibd_file_missing) {
  2214. ut_print_timestamp(stderr);
  2215. fputs(" InnoDB: cannot find or open in the"
  2216. " database directory the .ibd file of\n"
  2217. "InnoDB: table ", stderr);
  2218. ut_print_name(stderr, trx, TRUE, name);
  2219. fputs("\n"
  2220. "InnoDB: in ALTER TABLE ... IMPORT TABLESPACE\n",
  2221. stderr);
  2222. }
  2223. err = DB_ERROR;
  2224. }
  2225. funct_exit:
  2226. trx_commit_for_mysql(trx);
  2227. row_mysql_unlock_data_dictionary(trx);
  2228. trx->op_info = "";
  2229. /* Restore the fatal semaphore wait timeout */
  2230. mutex_enter(&kernel_mutex);
  2231. srv_fatal_semaphore_wait_threshold -= 7200; /* 2 hours */
  2232. mutex_exit(&kernel_mutex);
  2233. return((int) err);
  2234. }
  2235. /*********************************************************************//**
  2236. Truncates a table for MySQL.
  2237. @return error code or DB_SUCCESS */
  2238. UNIV_INTERN
  2239. int
  2240. row_truncate_table_for_mysql(
  2241. /*=========================*/
  2242. dict_table_t* table, /*!< in: table handle */
  2243. trx_t* trx) /*!< in: transaction handle */
  2244. {
  2245. dict_foreign_t* foreign;
  2246. ulint err;
  2247. mem_heap_t* heap;
  2248. byte* buf;
  2249. dtuple_t* tuple;
  2250. dfield_t* dfield;
  2251. dict_index_t* sys_index;
  2252. btr_pcur_t pcur;
  2253. mtr_t mtr;
  2254. table_id_t new_id;
  2255. ulint recreate_space = 0;
  2256. pars_info_t* info = NULL;
  2257. /* How do we prevent crashes caused by ongoing operations on
  2258. the table? Old operations could try to access non-existent
  2259. pages.
  2260. 1) SQL queries, INSERT, SELECT, ...: we must get an exclusive
  2261. MySQL table lock on the table before we can do TRUNCATE
  2262. TABLE. Then there are no running queries on the table. This is
  2263. guaranteed, because in ha_innobase::store_lock(), we do not
  2264. weaken the TL_WRITE lock requested by MySQL when executing
  2265. SQLCOM_TRUNCATE.
  2266. 2) Purge and rollback: we assign a new table id for the
  2267. table. Since purge and rollback look for the table based on
  2268. the table id, they see the table as 'dropped' and discard
  2269. their operations.
  2270. 3) Insert buffer: TRUNCATE TABLE is analogous to DROP TABLE,
  2271. so we do not have to remove insert buffer records, as the
  2272. insert buffer works at a low level. If a freed page is later
  2273. reallocated, the allocator will remove the ibuf entries for
  2274. it.
  2275. When we truncate *.ibd files by recreating them (analogous to
  2276. DISCARD TABLESPACE), we remove all entries for the table in the
  2277. insert buffer tree. This is not strictly necessary, because
  2278. in 6) we will assign a new tablespace identifier, but we can
  2279. free up some space in the system tablespace.
  2280. 4) Linear readahead and random readahead: we use the same
  2281. method as in 3) to discard ongoing operations. (This is only
  2282. relevant for TRUNCATE TABLE by DISCARD TABLESPACE.)
  2283. 5) FOREIGN KEY operations: if
  2284. table->n_foreign_key_checks_running > 0, we do not allow the
  2285. TRUNCATE. We also reserve the data dictionary latch.
  2286. 6) Crash recovery: To prevent the application of pre-truncation
  2287. redo log records on the truncated tablespace, we will assign
  2288. a new tablespace identifier to the truncated tablespace. */
  2289. ut_ad(table);
  2290. if (srv_created_new_raw) {
  2291. fputs("InnoDB: A new raw disk partition was initialized:\n"
  2292. "InnoDB: we do not allow database modifications"
  2293. " by the user.\n"
  2294. "InnoDB: Shut down mysqld and edit my.cnf so that newraw"
  2295. " is replaced with raw.\n", stderr);
  2296. return(DB_ERROR);
  2297. }
  2298. trx->op_info = "truncating table";
  2299. trx_start_if_not_started(trx);
  2300. /* Serialize data dictionary operations with dictionary mutex:
  2301. no deadlocks can occur then in these operations */
  2302. ut_a(trx->dict_operation_lock_mode == 0);
  2303. /* Prevent foreign key checks etc. while we are truncating the
  2304. table */
  2305. row_mysql_lock_data_dictionary(trx);
  2306. ut_ad(mutex_own(&(dict_sys->mutex)));
  2307. #ifdef UNIV_SYNC_DEBUG
  2308. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  2309. #endif /* UNIV_SYNC_DEBUG */
  2310. /* Check if the table is referenced by foreign key constraints from
  2311. some other table (not the table itself) */
  2312. foreign = UT_LIST_GET_FIRST(table->referenced_list);
  2313. while (foreign && foreign->foreign_table == table) {
  2314. foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
  2315. }
  2316. if (foreign && trx->check_foreigns) {
  2317. FILE* ef = dict_foreign_err_file;
  2318. /* We only allow truncating a referenced table if
  2319. FOREIGN_KEY_CHECKS is set to 0 */
  2320. mutex_enter(&dict_foreign_err_mutex);
  2321. rewind(ef);
  2322. ut_print_timestamp(ef);
  2323. fputs(" Cannot truncate table ", ef);
  2324. ut_print_name(ef, trx, TRUE, table->name);
  2325. fputs(" by DROP+CREATE\n"
  2326. "InnoDB: because it is referenced by ", ef);
  2327. ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
  2328. putc('\n', ef);
  2329. mutex_exit(&dict_foreign_err_mutex);
  2330. err = DB_ERROR;
  2331. goto funct_exit;
  2332. }
  2333. /* TODO: could we replace the counter n_foreign_key_checks_running
  2334. with lock checks on the table? Acquire here an exclusive lock on the
  2335. table, and rewrite lock0lock.c and the lock wait in srv0srv.c so that
  2336. they can cope with the table having been truncated here? Foreign key
  2337. checks take an IS or IX lock on the table. */
  2338. if (table->n_foreign_key_checks_running > 0) {
  2339. ut_print_timestamp(stderr);
  2340. fputs(" InnoDB: Cannot truncate table ", stderr);
  2341. ut_print_name(stderr, trx, TRUE, table->name);
  2342. fputs(" by DROP+CREATE\n"
  2343. "InnoDB: because there is a foreign key check"
  2344. " running on it.\n",
  2345. stderr);
  2346. err = DB_ERROR;
  2347. goto funct_exit;
  2348. }
  2349. /* Remove all locks except the table-level S and X locks. */
  2350. lock_remove_all_on_table(table, FALSE);
  2351. trx->table_id = table->id;
  2352. if (table->space && !table->dir_path_of_temp_table) {
  2353. /* Discard and create the single-table tablespace. */
  2354. ulint space = table->space;
  2355. ulint flags = fil_space_get_flags(space);
  2356. if (flags != ULINT_UNDEFINED
  2357. && fil_discard_tablespace(space)) {
  2358. dict_index_t* index;
  2359. dict_hdr_get_new_id(NULL, NULL, &space);
  2360. /* Lock all index trees for this table. We must
  2361. do so after dict_hdr_get_new_id() to preserve
  2362. the latch order */
  2363. dict_table_x_lock_indexes(table);
  2364. if (space == ULINT_UNDEFINED
  2365. || fil_create_new_single_table_tablespace(
  2366. space, table->name, FALSE, flags,
  2367. FIL_IBD_FILE_INITIAL_SIZE) != DB_SUCCESS) {
  2368. dict_table_x_unlock_indexes(table);
  2369. ut_print_timestamp(stderr);
  2370. fprintf(stderr,
  2371. " InnoDB: TRUNCATE TABLE %s failed to"
  2372. " create a new tablespace\n",
  2373. table->name);
  2374. table->ibd_file_missing = 1;
  2375. err = DB_ERROR;
  2376. goto funct_exit;
  2377. }
  2378. recreate_space = space;
  2379. /* Replace the space_id in the data dictionary cache.
  2380. The persisent data dictionary (SYS_TABLES.SPACE
  2381. and SYS_INDEXES.SPACE) are updated later in this
  2382. function. */
  2383. table->space = space;
  2384. index = dict_table_get_first_index(table);
  2385. do {
  2386. ulint ref_count = btr_search_info_get_ref_count(index->search_info, index->id);
  2387. /* check adaptive hash entries */
  2388. if (ref_count) {
  2389. fprintf(stderr, "InnoDB: Warning:"
  2390. " hash index ref_count (%lu) is not zero"
  2391. " after fil_discard_tablespace().\n"
  2392. "index: \"%s\""
  2393. " table: \"%s\"\n",
  2394. ref_count,
  2395. index->name,
  2396. table->name);
  2397. }
  2398. index->space = space;
  2399. index = dict_table_get_next_index(index);
  2400. } while (index);
  2401. mtr_start(&mtr);
  2402. fsp_header_init(space,
  2403. FIL_IBD_FILE_INITIAL_SIZE, &mtr);
  2404. mtr_commit(&mtr);
  2405. }
  2406. } else {
  2407. /* Lock all index trees for this table, as we will
  2408. truncate the table/index and possibly change their metadata.
  2409. All DML/DDL are blocked by table level lock, with
  2410. a few exceptions such as queries into information schema
  2411. about the table, MySQL could try to access index stats
  2412. for this kind of query, we need to use index locks to
  2413. sync up */
  2414. dict_table_x_lock_indexes(table);
  2415. }
  2416. /* scan SYS_INDEXES for all indexes of the table */
  2417. heap = mem_heap_create(800);
  2418. tuple = dtuple_create(heap, 1);
  2419. dfield = dtuple_get_nth_field(tuple, 0);
  2420. buf = mem_heap_alloc(heap, 8);
  2421. mach_write_to_8(buf, table->id);
  2422. dfield_set_data(dfield, buf, 8);
  2423. sys_index = dict_table_get_first_index(dict_sys->sys_indexes);
  2424. dict_index_copy_types(tuple, sys_index, 1);
  2425. mtr_start(&mtr);
  2426. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  2427. BTR_MODIFY_LEAF, &pcur, &mtr);
  2428. for (;;) {
  2429. rec_t* rec;
  2430. const byte* field;
  2431. ulint len;
  2432. ulint root_page_no;
  2433. if (!btr_pcur_is_on_user_rec(&pcur)) {
  2434. /* The end of SYS_INDEXES has been reached. */
  2435. break;
  2436. }
  2437. rec = btr_pcur_get_rec(&pcur);
  2438. field = rec_get_nth_field_old(rec, 0, &len);
  2439. ut_ad(len == 8);
  2440. if (memcmp(buf, field, len) != 0) {
  2441. /* End of indexes for the table (TABLE_ID mismatch). */
  2442. break;
  2443. }
  2444. if (rec_get_deleted_flag(rec, FALSE)) {
  2445. /* The index has been dropped. */
  2446. goto next_rec;
  2447. }
  2448. /* This call may commit and restart mtr
  2449. and reposition pcur. */
  2450. root_page_no = dict_truncate_index_tree(table, recreate_space,
  2451. &pcur, &mtr);
  2452. rec = btr_pcur_get_rec(&pcur);
  2453. if (root_page_no != FIL_NULL) {
  2454. page_rec_write_field(
  2455. rec, DICT_SYS_INDEXES_PAGE_NO_FIELD,
  2456. root_page_no, &mtr);
  2457. /* We will need to commit and restart the
  2458. mini-transaction in order to avoid deadlocks.
  2459. The dict_truncate_index_tree() call has allocated
  2460. a page in this mini-transaction, and the rest of
  2461. this loop could latch another index page. */
  2462. mtr_commit(&mtr);
  2463. mtr_start(&mtr);
  2464. btr_pcur_restore_position(BTR_MODIFY_LEAF,
  2465. &pcur, &mtr);
  2466. }
  2467. next_rec:
  2468. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  2469. }
  2470. btr_pcur_close(&pcur);
  2471. mtr_commit(&mtr);
  2472. mem_heap_free(heap);
  2473. /* Done with index truncation, release index tree locks,
  2474. subsequent work relates to table level metadata change */
  2475. dict_table_x_unlock_indexes(table);
  2476. dict_hdr_get_new_id(&new_id, NULL, NULL);
  2477. info = pars_info_create();
  2478. pars_info_add_int4_literal(info, "space", (lint) table->space);
  2479. pars_info_add_ull_literal(info, "old_id", table->id);
  2480. pars_info_add_ull_literal(info, "new_id", new_id);
  2481. err = que_eval_sql(info,
  2482. "PROCEDURE RENUMBER_TABLESPACE_PROC () IS\n"
  2483. "BEGIN\n"
  2484. "UPDATE SYS_TABLES"
  2485. " SET ID = :new_id, SPACE = :space\n"
  2486. " WHERE ID = :old_id;\n"
  2487. "UPDATE SYS_COLUMNS SET TABLE_ID = :new_id\n"
  2488. " WHERE TABLE_ID = :old_id;\n"
  2489. "UPDATE SYS_INDEXES"
  2490. " SET TABLE_ID = :new_id, SPACE = :space\n"
  2491. " WHERE TABLE_ID = :old_id;\n"
  2492. "COMMIT WORK;\n"
  2493. "END;\n"
  2494. , FALSE, trx);
  2495. if (err != DB_SUCCESS) {
  2496. trx->error_state = DB_SUCCESS;
  2497. trx_general_rollback_for_mysql(trx, NULL);
  2498. trx->error_state = DB_SUCCESS;
  2499. ut_print_timestamp(stderr);
  2500. fputs(" InnoDB: Unable to assign a new identifier to table ",
  2501. stderr);
  2502. ut_print_name(stderr, trx, TRUE, table->name);
  2503. fputs("\n"
  2504. "InnoDB: after truncating it. Background processes"
  2505. " may corrupt the table!\n", stderr);
  2506. err = DB_ERROR;
  2507. } else {
  2508. dict_table_change_id_in_cache(table, new_id);
  2509. }
  2510. /* Reset auto-increment. */
  2511. dict_table_autoinc_lock(table);
  2512. dict_table_autoinc_initialize(table, 1);
  2513. dict_table_autoinc_unlock(table);
  2514. dict_update_statistics(
  2515. table,
  2516. FALSE, /* update even if stats are initialized */
  2517. TRUE,
  2518. FALSE /* update even if not changed too much */);
  2519. trx_commit_for_mysql(trx);
  2520. funct_exit:
  2521. row_mysql_unlock_data_dictionary(trx);
  2522. trx->op_info = "";
  2523. srv_wake_master_thread();
  2524. return((int) err);
  2525. }
  2526. /*********************************************************************//**
  2527. Drops a table for MySQL. If the name of the dropped table ends in
  2528. one of "innodb_monitor", "innodb_lock_monitor", "innodb_tablespace_monitor",
  2529. "innodb_table_monitor", then this will also stop the printing of monitor
  2530. output by the master thread. If the data dictionary was not already locked
  2531. by the transaction, the transaction will be committed. Otherwise, the
  2532. data dictionary will remain locked.
  2533. @return error code or DB_SUCCESS */
  2534. UNIV_INTERN
  2535. int
  2536. row_drop_table_for_mysql(
  2537. /*=====================*/
  2538. const char* name, /*!< in: table name */
  2539. trx_t* trx, /*!< in: transaction handle */
  2540. ibool drop_db)/*!< in: TRUE=dropping whole database */
  2541. {
  2542. dict_foreign_t* foreign;
  2543. dict_table_t* table;
  2544. dict_index_t* index;
  2545. ulint space_id;
  2546. ulint err;
  2547. const char* table_name;
  2548. ulint namelen;
  2549. ibool locked_dictionary = FALSE;
  2550. pars_info_t* info = NULL;
  2551. ut_a(name != NULL);
  2552. if (srv_created_new_raw) {
  2553. fputs("InnoDB: A new raw disk partition was initialized:\n"
  2554. "InnoDB: we do not allow database modifications"
  2555. " by the user.\n"
  2556. "InnoDB: Shut down mysqld and edit my.cnf so that newraw"
  2557. " is replaced with raw.\n", stderr);
  2558. return(DB_ERROR);
  2559. }
  2560. trx->op_info = "dropping table";
  2561. trx_start_if_not_started(trx);
  2562. /* The table name is prefixed with the database name and a '/'.
  2563. Certain table names starting with 'innodb_' have their special
  2564. meaning regardless of the database name. Thus, we need to
  2565. ignore the database name prefix in the comparisons. */
  2566. table_name = strchr(name, '/');
  2567. ut_a(table_name);
  2568. table_name++;
  2569. namelen = strlen(table_name) + 1;
  2570. if (namelen == sizeof S_innodb_monitor
  2571. && !memcmp(table_name, S_innodb_monitor,
  2572. sizeof S_innodb_monitor)) {
  2573. /* Table name equals "innodb_monitor":
  2574. stop monitor prints */
  2575. srv_print_innodb_monitor = FALSE;
  2576. srv_print_innodb_lock_monitor = FALSE;
  2577. } else if (namelen == sizeof S_innodb_lock_monitor
  2578. && !memcmp(table_name, S_innodb_lock_monitor,
  2579. sizeof S_innodb_lock_monitor)) {
  2580. srv_print_innodb_monitor = FALSE;
  2581. srv_print_innodb_lock_monitor = FALSE;
  2582. } else if (namelen == sizeof S_innodb_tablespace_monitor
  2583. && !memcmp(table_name, S_innodb_tablespace_monitor,
  2584. sizeof S_innodb_tablespace_monitor)) {
  2585. srv_print_innodb_tablespace_monitor = FALSE;
  2586. } else if (namelen == sizeof S_innodb_table_monitor
  2587. && !memcmp(table_name, S_innodb_table_monitor,
  2588. sizeof S_innodb_table_monitor)) {
  2589. srv_print_innodb_table_monitor = FALSE;
  2590. }
  2591. /* Serialize data dictionary operations with dictionary mutex:
  2592. no deadlocks can occur then in these operations */
  2593. if (trx->dict_operation_lock_mode != RW_X_LATCH) {
  2594. /* Prevent foreign key checks etc. while we are dropping the
  2595. table */
  2596. row_mysql_lock_data_dictionary(trx);
  2597. locked_dictionary = TRUE;
  2598. }
  2599. ut_ad(mutex_own(&(dict_sys->mutex)));
  2600. #ifdef UNIV_SYNC_DEBUG
  2601. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  2602. #endif /* UNIV_SYNC_DEBUG */
  2603. table = dict_table_get_low_ignore_err(
  2604. name, DICT_ERR_IGNORE_INDEX_ROOT | DICT_ERR_IGNORE_CORRUPT);
  2605. if (!table) {
  2606. err = DB_TABLE_NOT_FOUND;
  2607. ut_print_timestamp(stderr);
  2608. fputs(" InnoDB: Error: table ", stderr);
  2609. ut_print_name(stderr, trx, TRUE, name);
  2610. fputs(" does not exist in the InnoDB internal\n"
  2611. "InnoDB: data dictionary though MySQL is"
  2612. " trying to drop it.\n"
  2613. "InnoDB: Have you copied the .frm file"
  2614. " of the table to the\n"
  2615. "InnoDB: MySQL database directory"
  2616. " from another database?\n"
  2617. "InnoDB: You can look for further help from\n"
  2618. "InnoDB: " REFMAN "innodb-troubleshooting.html\n",
  2619. stderr);
  2620. goto funct_exit;
  2621. }
  2622. /* Check if the table is referenced by foreign key constraints from
  2623. some other table (not the table itself) */
  2624. foreign = UT_LIST_GET_FIRST(table->referenced_list);
  2625. while (foreign && foreign->foreign_table == table) {
  2626. check_next_foreign:
  2627. foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
  2628. }
  2629. if (foreign && trx->check_foreigns
  2630. && !(drop_db && dict_tables_have_same_db(
  2631. name, foreign->foreign_table_name_lookup))) {
  2632. FILE* ef = dict_foreign_err_file;
  2633. /* We only allow dropping a referenced table if
  2634. FOREIGN_KEY_CHECKS is set to 0 */
  2635. err = DB_CANNOT_DROP_CONSTRAINT;
  2636. mutex_enter(&dict_foreign_err_mutex);
  2637. rewind(ef);
  2638. ut_print_timestamp(ef);
  2639. fputs(" Cannot drop table ", ef);
  2640. ut_print_name(ef, trx, TRUE, name);
  2641. fputs("\n"
  2642. "because it is referenced by ", ef);
  2643. ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
  2644. putc('\n', ef);
  2645. mutex_exit(&dict_foreign_err_mutex);
  2646. goto funct_exit;
  2647. }
  2648. if (foreign && trx->check_foreigns) {
  2649. goto check_next_foreign;
  2650. }
  2651. if (table->n_mysql_handles_opened > 0) {
  2652. ibool added;
  2653. added = row_add_table_to_background_drop_list(table->name);
  2654. if (added) {
  2655. ut_print_timestamp(stderr);
  2656. fputs(" InnoDB: Warning: MySQL is"
  2657. " trying to drop table ", stderr);
  2658. ut_print_name(stderr, trx, TRUE, table->name);
  2659. fputs("\n"
  2660. "InnoDB: though there are still"
  2661. " open handles to it.\n"
  2662. "InnoDB: Adding the table to the"
  2663. " background drop queue.\n",
  2664. stderr);
  2665. /* We return DB_SUCCESS to MySQL though the drop will
  2666. happen lazily later */
  2667. err = DB_SUCCESS;
  2668. } else {
  2669. /* The table is already in the background drop list */
  2670. err = DB_ERROR;
  2671. }
  2672. goto funct_exit;
  2673. }
  2674. /* TODO: could we replace the counter n_foreign_key_checks_running
  2675. with lock checks on the table? Acquire here an exclusive lock on the
  2676. table, and rewrite lock0lock.c and the lock wait in srv0srv.c so that
  2677. they can cope with the table having been dropped here? Foreign key
  2678. checks take an IS or IX lock on the table. */
  2679. if (table->n_foreign_key_checks_running > 0) {
  2680. const char* table_name = table->name;
  2681. ibool added;
  2682. added = row_add_table_to_background_drop_list(table_name);
  2683. if (added) {
  2684. ut_print_timestamp(stderr);
  2685. fputs(" InnoDB: You are trying to drop table ",
  2686. stderr);
  2687. ut_print_name(stderr, trx, TRUE, table_name);
  2688. fputs("\n"
  2689. "InnoDB: though there is a"
  2690. " foreign key check running on it.\n"
  2691. "InnoDB: Adding the table to"
  2692. " the background drop queue.\n",
  2693. stderr);
  2694. /* We return DB_SUCCESS to MySQL though the drop will
  2695. happen lazily later */
  2696. err = DB_SUCCESS;
  2697. } else {
  2698. /* The table is already in the background drop list */
  2699. err = DB_ERROR;
  2700. }
  2701. goto funct_exit;
  2702. }
  2703. /* Remove all locks there are on the table or its records */
  2704. lock_remove_all_on_table(table, TRUE);
  2705. trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
  2706. trx->table_id = table->id;
  2707. /* Mark all indexes unavailable in the data dictionary cache
  2708. before starting to drop the table. */
  2709. for (index = dict_table_get_first_index(table);
  2710. index != NULL;
  2711. index = dict_table_get_next_index(index)) {
  2712. rw_lock_x_lock(dict_index_get_lock(index));
  2713. ut_ad(!index->to_be_dropped);
  2714. index->to_be_dropped = TRUE;
  2715. rw_lock_x_unlock(dict_index_get_lock(index));
  2716. }
  2717. /* We use the private SQL parser of Innobase to generate the
  2718. query graphs needed in deleting the dictionary data from system
  2719. tables in Innobase. Deleting a row from SYS_INDEXES table also
  2720. frees the file segments of the B-tree associated with the index. */
  2721. info = pars_info_create();
  2722. pars_info_add_str_literal(info, "table_name", name);
  2723. err = que_eval_sql(info,
  2724. "PROCEDURE DROP_TABLE_PROC () IS\n"
  2725. "sys_foreign_id CHAR;\n"
  2726. "table_id CHAR;\n"
  2727. "index_id CHAR;\n"
  2728. "foreign_id CHAR;\n"
  2729. "found INT;\n"
  2730. "DECLARE CURSOR cur_fk IS\n"
  2731. "SELECT ID FROM SYS_FOREIGN\n"
  2732. "WHERE FOR_NAME = :table_name\n"
  2733. "AND TO_BINARY(FOR_NAME)\n"
  2734. " = TO_BINARY(:table_name)\n"
  2735. "LOCK IN SHARE MODE;\n"
  2736. "DECLARE CURSOR cur_idx IS\n"
  2737. "SELECT ID FROM SYS_INDEXES\n"
  2738. "WHERE TABLE_ID = table_id\n"
  2739. "LOCK IN SHARE MODE;\n"
  2740. "BEGIN\n"
  2741. "SELECT ID INTO table_id\n"
  2742. "FROM SYS_TABLES\n"
  2743. "WHERE NAME = :table_name\n"
  2744. "LOCK IN SHARE MODE;\n"
  2745. "IF (SQL % NOTFOUND) THEN\n"
  2746. " RETURN;\n"
  2747. "END IF;\n"
  2748. "found := 1;\n"
  2749. "SELECT ID INTO sys_foreign_id\n"
  2750. "FROM SYS_TABLES\n"
  2751. "WHERE NAME = 'SYS_FOREIGN'\n"
  2752. "LOCK IN SHARE MODE;\n"
  2753. "IF (SQL % NOTFOUND) THEN\n"
  2754. " found := 0;\n"
  2755. "END IF;\n"
  2756. "IF (:table_name = 'SYS_FOREIGN') THEN\n"
  2757. " found := 0;\n"
  2758. "END IF;\n"
  2759. "IF (:table_name = 'SYS_FOREIGN_COLS') THEN\n"
  2760. " found := 0;\n"
  2761. "END IF;\n"
  2762. "OPEN cur_fk;\n"
  2763. "WHILE found = 1 LOOP\n"
  2764. " FETCH cur_fk INTO foreign_id;\n"
  2765. " IF (SQL % NOTFOUND) THEN\n"
  2766. " found := 0;\n"
  2767. " ELSE\n"
  2768. " DELETE FROM SYS_FOREIGN_COLS\n"
  2769. " WHERE ID = foreign_id;\n"
  2770. " DELETE FROM SYS_FOREIGN\n"
  2771. " WHERE ID = foreign_id;\n"
  2772. " END IF;\n"
  2773. "END LOOP;\n"
  2774. "CLOSE cur_fk;\n"
  2775. "found := 1;\n"
  2776. "OPEN cur_idx;\n"
  2777. "WHILE found = 1 LOOP\n"
  2778. " FETCH cur_idx INTO index_id;\n"
  2779. " IF (SQL % NOTFOUND) THEN\n"
  2780. " found := 0;\n"
  2781. " ELSE\n"
  2782. " DELETE FROM SYS_STATS\n"
  2783. " WHERE INDEX_ID = index_id;\n"
  2784. " DELETE FROM SYS_FIELDS\n"
  2785. " WHERE INDEX_ID = index_id;\n"
  2786. " DELETE FROM SYS_INDEXES\n"
  2787. " WHERE ID = index_id\n"
  2788. " AND TABLE_ID = table_id;\n"
  2789. " END IF;\n"
  2790. "END LOOP;\n"
  2791. "CLOSE cur_idx;\n"
  2792. "DELETE FROM SYS_COLUMNS\n"
  2793. "WHERE TABLE_ID = table_id;\n"
  2794. "DELETE FROM SYS_TABLES\n"
  2795. "WHERE ID = table_id;\n"
  2796. "END;\n"
  2797. , FALSE, trx);
  2798. switch (err) {
  2799. ibool is_temp;
  2800. const char* name_or_path;
  2801. mem_heap_t* heap;
  2802. case DB_SUCCESS:
  2803. heap = mem_heap_create(200);
  2804. /* Clone the name, in case it has been allocated
  2805. from table->heap, which will be freed by
  2806. dict_table_remove_from_cache(table) below. */
  2807. name = mem_heap_strdup(heap, name);
  2808. space_id = table->space;
  2809. if (table->dir_path_of_temp_table != NULL) {
  2810. name_or_path = mem_heap_strdup(
  2811. heap, table->dir_path_of_temp_table);
  2812. is_temp = TRUE;
  2813. } else {
  2814. name_or_path = name;
  2815. is_temp = (table->flags >> DICT_TF2_SHIFT)
  2816. & DICT_TF2_TEMPORARY;
  2817. }
  2818. dict_table_remove_from_cache(table);
  2819. if (dict_load_table(name, TRUE, DICT_ERR_IGNORE_NONE) != NULL) {
  2820. ut_print_timestamp(stderr);
  2821. fputs(" InnoDB: Error: not able to remove table ",
  2822. stderr);
  2823. ut_print_name(stderr, trx, TRUE, name);
  2824. fputs(" from the dictionary cache!\n", stderr);
  2825. err = DB_ERROR;
  2826. }
  2827. /* Do not drop possible .ibd tablespace if something went
  2828. wrong: we do not want to delete valuable data of the user */
  2829. if (err == DB_SUCCESS && !trx_sys_sys_space(space_id)) {
  2830. if (!fil_space_for_table_exists_in_mem(space_id,
  2831. name_or_path,
  2832. is_temp, FALSE,
  2833. !is_temp)) {
  2834. err = DB_SUCCESS;
  2835. fprintf(stderr,
  2836. "InnoDB: We removed now the InnoDB"
  2837. " internal data dictionary entry\n"
  2838. "InnoDB: of table ");
  2839. ut_print_name(stderr, trx, TRUE, name);
  2840. fprintf(stderr, ".\n");
  2841. } else if (!fil_delete_tablespace(space_id, FALSE)) {
  2842. fprintf(stderr,
  2843. "InnoDB: We removed now the InnoDB"
  2844. " internal data dictionary entry\n"
  2845. "InnoDB: of table ");
  2846. ut_print_name(stderr, trx, TRUE, name);
  2847. fprintf(stderr, ".\n");
  2848. ut_print_timestamp(stderr);
  2849. fprintf(stderr,
  2850. " InnoDB: Error: not able to"
  2851. " delete tablespace %lu of table ",
  2852. (ulong) space_id);
  2853. ut_print_name(stderr, trx, TRUE, name);
  2854. fputs("!\n", stderr);
  2855. err = DB_ERROR;
  2856. }
  2857. }
  2858. mem_heap_free(heap);
  2859. break;
  2860. case DB_TOO_MANY_CONCURRENT_TRXS:
  2861. /* Cannot even find a free slot for the
  2862. the undo log. We can directly exit here
  2863. and return the DB_TOO_MANY_CONCURRENT_TRXS
  2864. error. */
  2865. /* Mark all indexes available in the data dictionary
  2866. cache again. */
  2867. for (index = dict_table_get_first_index(table);
  2868. index != NULL;
  2869. index = dict_table_get_next_index(index)) {
  2870. rw_lock_x_lock(dict_index_get_lock(index));
  2871. index->to_be_dropped = FALSE;
  2872. rw_lock_x_unlock(dict_index_get_lock(index));
  2873. }
  2874. break;
  2875. case DB_OUT_OF_FILE_SPACE:
  2876. err = DB_MUST_GET_MORE_FILE_SPACE;
  2877. row_mysql_handle_errors(&err, trx, NULL, NULL);
  2878. /* Fall through to raise error */
  2879. default:
  2880. /* No other possible error returns */
  2881. ut_error;
  2882. }
  2883. funct_exit:
  2884. if (locked_dictionary) {
  2885. trx_commit_for_mysql(trx);
  2886. row_mysql_unlock_data_dictionary(trx);
  2887. }
  2888. trx->op_info = "";
  2889. srv_wake_master_thread();
  2890. return((int) err);
  2891. }
  2892. /*********************************************************************//**
  2893. Drop all temporary tables during crash recovery. */
  2894. UNIV_INTERN
  2895. void
  2896. row_mysql_drop_temp_tables(void)
  2897. /*============================*/
  2898. {
  2899. trx_t* trx;
  2900. btr_pcur_t pcur;
  2901. mtr_t mtr;
  2902. mem_heap_t* heap;
  2903. trx = trx_allocate_for_background();
  2904. trx->op_info = "dropping temporary tables";
  2905. row_mysql_lock_data_dictionary(trx);
  2906. heap = mem_heap_create(200);
  2907. mtr_start(&mtr);
  2908. btr_pcur_open_at_index_side(
  2909. TRUE,
  2910. dict_table_get_first_index(dict_sys->sys_tables),
  2911. BTR_SEARCH_LEAF, &pcur, TRUE, &mtr);
  2912. for (;;) {
  2913. const rec_t* rec;
  2914. const byte* field;
  2915. ulint len;
  2916. const char* table_name;
  2917. dict_table_t* table;
  2918. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  2919. if (!btr_pcur_is_on_user_rec(&pcur)) {
  2920. break;
  2921. }
  2922. rec = btr_pcur_get_rec(&pcur);
  2923. field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
  2924. if (len != 4 || !(mach_read_from_4(field) & 0x80000000UL)) {
  2925. continue;
  2926. }
  2927. /* Because this is not a ROW_FORMAT=REDUNDANT table,
  2928. the is_temp flag is valid. Examine it. */
  2929. field = rec_get_nth_field_old(rec, 7/*MIX_LEN*/, &len);
  2930. if (len != 4
  2931. || !(mach_read_from_4(field) & DICT_TF2_TEMPORARY)) {
  2932. continue;
  2933. }
  2934. /* This is a temporary table. */
  2935. field = rec_get_nth_field_old(rec, 0/*NAME*/, &len);
  2936. if (len == UNIV_SQL_NULL || len == 0) {
  2937. /* Corrupted SYS_TABLES.NAME */
  2938. continue;
  2939. }
  2940. table_name = mem_heap_strdupl(heap, (const char*) field, len);
  2941. btr_pcur_store_position(&pcur, &mtr);
  2942. btr_pcur_commit_specify_mtr(&pcur, &mtr);
  2943. table = dict_table_get_low(table_name);
  2944. if (table) {
  2945. row_drop_table_for_mysql(table_name, trx, FALSE);
  2946. trx_commit_for_mysql(trx);
  2947. }
  2948. mtr_start(&mtr);
  2949. btr_pcur_restore_position(BTR_SEARCH_LEAF,
  2950. &pcur, &mtr);
  2951. }
  2952. btr_pcur_close(&pcur);
  2953. mtr_commit(&mtr);
  2954. mem_heap_free(heap);
  2955. row_mysql_unlock_data_dictionary(trx);
  2956. trx_free_for_background(trx);
  2957. }
  2958. /*******************************************************************//**
  2959. Drop all foreign keys in a database, see Bug#18942.
  2960. Called at the end of row_drop_database_for_mysql().
  2961. @return error code or DB_SUCCESS */
  2962. static
  2963. ulint
  2964. drop_all_foreign_keys_in_db(
  2965. /*========================*/
  2966. const char* name, /*!< in: database name which ends to '/' */
  2967. trx_t* trx) /*!< in: transaction handle */
  2968. {
  2969. pars_info_t* pinfo;
  2970. ulint err;
  2971. ut_a(name[strlen(name) - 1] == '/');
  2972. pinfo = pars_info_create();
  2973. pars_info_add_str_literal(pinfo, "dbname", name);
  2974. /** true if for_name is not prefixed with dbname */
  2975. #define TABLE_NOT_IN_THIS_DB \
  2976. "SUBSTR(for_name, 0, LENGTH(:dbname)) <> :dbname"
  2977. err = que_eval_sql(pinfo,
  2978. "PROCEDURE DROP_ALL_FOREIGN_KEYS_PROC () IS\n"
  2979. "foreign_id CHAR;\n"
  2980. "for_name CHAR;\n"
  2981. "found INT;\n"
  2982. "DECLARE CURSOR cur IS\n"
  2983. "SELECT ID, FOR_NAME FROM SYS_FOREIGN\n"
  2984. "WHERE FOR_NAME >= :dbname\n"
  2985. "LOCK IN SHARE MODE\n"
  2986. "ORDER BY FOR_NAME;\n"
  2987. "BEGIN\n"
  2988. "found := 1;\n"
  2989. "OPEN cur;\n"
  2990. "WHILE found = 1 LOOP\n"
  2991. " FETCH cur INTO foreign_id, for_name;\n"
  2992. " IF (SQL % NOTFOUND) THEN\n"
  2993. " found := 0;\n"
  2994. " ELSIF (" TABLE_NOT_IN_THIS_DB ") THEN\n"
  2995. " found := 0;\n"
  2996. " ELSIF (1=1) THEN\n"
  2997. " DELETE FROM SYS_FOREIGN_COLS\n"
  2998. " WHERE ID = foreign_id;\n"
  2999. " DELETE FROM SYS_FOREIGN\n"
  3000. " WHERE ID = foreign_id;\n"
  3001. " END IF;\n"
  3002. "END LOOP;\n"
  3003. "CLOSE cur;\n"
  3004. "COMMIT WORK;\n"
  3005. "END;\n",
  3006. FALSE, /* do not reserve dict mutex,
  3007. we are already holding it */
  3008. trx);
  3009. return(err);
  3010. }
  3011. /*********************************************************************//**
  3012. Drops a database for MySQL.
  3013. @return error code or DB_SUCCESS */
  3014. UNIV_INTERN
  3015. int
  3016. row_drop_database_for_mysql(
  3017. /*========================*/
  3018. const char* name, /*!< in: database name which ends to '/' */
  3019. trx_t* trx) /*!< in: transaction handle */
  3020. {
  3021. dict_table_t* table;
  3022. char* table_name;
  3023. int err = DB_SUCCESS;
  3024. ulint namelen = strlen(name);
  3025. ut_a(name != NULL);
  3026. ut_a(name[namelen - 1] == '/');
  3027. trx->op_info = "dropping database";
  3028. trx_start_if_not_started(trx);
  3029. loop:
  3030. row_mysql_lock_data_dictionary(trx);
  3031. while ((table_name = dict_get_first_table_name_in_db(name))) {
  3032. ut_a(memcmp(table_name, name, namelen) == 0);
  3033. table = dict_table_get_low(table_name);
  3034. ut_a(table);
  3035. /* Wait until MySQL does not have any queries running on
  3036. the table */
  3037. if (table->n_mysql_handles_opened > 0) {
  3038. row_mysql_unlock_data_dictionary(trx);
  3039. ut_print_timestamp(stderr);
  3040. fputs(" InnoDB: Warning: MySQL is trying to"
  3041. " drop database ", stderr);
  3042. ut_print_name(stderr, trx, TRUE, name);
  3043. fputs("\n"
  3044. "InnoDB: though there are still"
  3045. " open handles to table ", stderr);
  3046. ut_print_name(stderr, trx, TRUE, table_name);
  3047. fputs(".\n", stderr);
  3048. os_thread_sleep(1000000);
  3049. mem_free(table_name);
  3050. goto loop;
  3051. }
  3052. err = row_drop_table_for_mysql(table_name, trx, TRUE);
  3053. trx_commit_for_mysql(trx);
  3054. if (err != DB_SUCCESS) {
  3055. fputs("InnoDB: DROP DATABASE ", stderr);
  3056. ut_print_name(stderr, trx, TRUE, name);
  3057. fprintf(stderr, " failed with error %lu for table ",
  3058. (ulint) err);
  3059. ut_print_name(stderr, trx, TRUE, table_name);
  3060. putc('\n', stderr);
  3061. mem_free(table_name);
  3062. break;
  3063. }
  3064. mem_free(table_name);
  3065. }
  3066. if (err == DB_SUCCESS) {
  3067. /* after dropping all tables try to drop all leftover
  3068. foreign keys in case orphaned ones exist */
  3069. err = (int) drop_all_foreign_keys_in_db(name, trx);
  3070. if (err != DB_SUCCESS) {
  3071. fputs("InnoDB: DROP DATABASE ", stderr);
  3072. ut_print_name(stderr, trx, TRUE, name);
  3073. fprintf(stderr, " failed with error %d while "
  3074. "dropping all foreign keys", err);
  3075. }
  3076. }
  3077. trx_commit_for_mysql(trx);
  3078. row_mysql_unlock_data_dictionary(trx);
  3079. trx->op_info = "";
  3080. return(err);
  3081. }
  3082. /*********************************************************************//**
  3083. Checks if a table name contains the string "/#sql" which denotes temporary
  3084. tables in MySQL.
  3085. @return TRUE if temporary table */
  3086. static
  3087. ibool
  3088. row_is_mysql_tmp_table_name(
  3089. /*========================*/
  3090. const char* name) /*!< in: table name in the form
  3091. 'database/tablename' */
  3092. {
  3093. return(strstr(name, "/#sql") != NULL);
  3094. /* return(strstr(name, "/@0023sql") != NULL); */
  3095. }
  3096. /****************************************************************//**
  3097. Delete a single constraint.
  3098. @return error code or DB_SUCCESS */
  3099. static
  3100. int
  3101. row_delete_constraint_low(
  3102. /*======================*/
  3103. const char* id, /*!< in: constraint id */
  3104. trx_t* trx) /*!< in: transaction handle */
  3105. {
  3106. pars_info_t* info = pars_info_create();
  3107. pars_info_add_str_literal(info, "id", id);
  3108. return((int) que_eval_sql(info,
  3109. "PROCEDURE DELETE_CONSTRAINT () IS\n"
  3110. "BEGIN\n"
  3111. "DELETE FROM SYS_FOREIGN_COLS WHERE ID = :id;\n"
  3112. "DELETE FROM SYS_FOREIGN WHERE ID = :id;\n"
  3113. "END;\n"
  3114. , FALSE, trx));
  3115. }
  3116. /****************************************************************//**
  3117. Delete a single constraint.
  3118. @return error code or DB_SUCCESS */
  3119. static
  3120. int
  3121. row_delete_constraint(
  3122. /*==================*/
  3123. const char* id, /*!< in: constraint id */
  3124. const char* database_name, /*!< in: database name, with the
  3125. trailing '/' */
  3126. mem_heap_t* heap, /*!< in: memory heap */
  3127. trx_t* trx) /*!< in: transaction handle */
  3128. {
  3129. ulint err;
  3130. /* New format constraints have ids <databasename>/<constraintname>. */
  3131. err = row_delete_constraint_low(
  3132. mem_heap_strcat(heap, database_name, id), trx);
  3133. if ((err == DB_SUCCESS) && !strchr(id, '/')) {
  3134. /* Old format < 4.0.18 constraints have constraint ids
  3135. NUMBER_NUMBER. We only try deleting them if the
  3136. constraint name does not contain a '/' character, otherwise
  3137. deleting a new format constraint named 'foo/bar' from
  3138. database 'baz' would remove constraint 'bar' from database
  3139. 'foo', if it existed. */
  3140. err = row_delete_constraint_low(id, trx);
  3141. }
  3142. return((int) err);
  3143. }
  3144. /*********************************************************************//**
  3145. Renames a table for MySQL.
  3146. @return error code or DB_SUCCESS */
  3147. UNIV_INTERN
  3148. ulint
  3149. row_rename_table_for_mysql(
  3150. /*=======================*/
  3151. const char* old_name, /*!< in: old table name */
  3152. const char* new_name, /*!< in: new table name */
  3153. trx_t* trx, /*!< in: transaction handle */
  3154. ibool commit) /*!< in: if TRUE then commit trx */
  3155. {
  3156. dict_table_t* table;
  3157. ulint err = DB_ERROR;
  3158. mem_heap_t* heap = NULL;
  3159. const char** constraints_to_drop = NULL;
  3160. ulint n_constraints_to_drop = 0;
  3161. ibool old_is_tmp, new_is_tmp;
  3162. pars_info_t* info = NULL;
  3163. int retry;
  3164. ut_a(old_name != NULL);
  3165. ut_a(new_name != NULL);
  3166. ut_ad(trx->state == TRX_ACTIVE);
  3167. if (srv_created_new_raw || srv_force_recovery) {
  3168. fputs("InnoDB: A new raw disk partition was initialized or\n"
  3169. "InnoDB: innodb_force_recovery is on: we do not allow\n"
  3170. "InnoDB: database modifications by the user. Shut down\n"
  3171. "InnoDB: mysqld and edit my.cnf so that newraw"
  3172. " is replaced\n"
  3173. "InnoDB: with raw, and innodb_force_... is removed.\n",
  3174. stderr);
  3175. goto funct_exit;
  3176. } else if (row_mysql_is_system_table(new_name)) {
  3177. fprintf(stderr,
  3178. "InnoDB: Error: trying to create a MySQL"
  3179. " system table %s of type InnoDB.\n"
  3180. "InnoDB: MySQL system tables must be"
  3181. " of the MyISAM type!\n",
  3182. new_name);
  3183. goto funct_exit;
  3184. }
  3185. trx->op_info = "renaming table";
  3186. old_is_tmp = row_is_mysql_tmp_table_name(old_name);
  3187. new_is_tmp = row_is_mysql_tmp_table_name(new_name);
  3188. table = dict_table_get_low(old_name);
  3189. if (!table) {
  3190. err = DB_TABLE_NOT_FOUND;
  3191. ut_print_timestamp(stderr);
  3192. fputs(" InnoDB: Error: table ", stderr);
  3193. ut_print_name(stderr, trx, TRUE, old_name);
  3194. fputs(" does not exist in the InnoDB internal\n"
  3195. "InnoDB: data dictionary though MySQL is"
  3196. " trying to rename the table.\n"
  3197. "InnoDB: Have you copied the .frm file"
  3198. " of the table to the\n"
  3199. "InnoDB: MySQL database directory"
  3200. " from another database?\n"
  3201. "InnoDB: You can look for further help from\n"
  3202. "InnoDB: " REFMAN "innodb-troubleshooting.html\n",
  3203. stderr);
  3204. goto funct_exit;
  3205. } else if (table->ibd_file_missing) {
  3206. err = DB_TABLE_NOT_FOUND;
  3207. ut_print_timestamp(stderr);
  3208. fputs(" InnoDB: Error: table ", stderr);
  3209. ut_print_name(stderr, trx, TRUE, old_name);
  3210. fputs(" does not have an .ibd file"
  3211. " in the database directory.\n"
  3212. "InnoDB: You can look for further help from\n"
  3213. "InnoDB: " REFMAN "innodb-troubleshooting.html\n",
  3214. stderr);
  3215. goto funct_exit;
  3216. } else if (new_is_tmp) {
  3217. /* MySQL is doing an ALTER TABLE command and it renames the
  3218. original table to a temporary table name. We want to preserve
  3219. the original foreign key constraint definitions despite the
  3220. name change. An exception is those constraints for which
  3221. the ALTER TABLE contained DROP FOREIGN KEY <foreign key id>.*/
  3222. heap = mem_heap_create(100);
  3223. err = dict_foreign_parse_drop_constraints(
  3224. heap, trx, table, &n_constraints_to_drop,
  3225. &constraints_to_drop);
  3226. if (err != DB_SUCCESS) {
  3227. goto funct_exit;
  3228. }
  3229. }
  3230. /* Is a foreign key check running on this table? */
  3231. for (retry = 0; retry < 100
  3232. && table->n_foreign_key_checks_running > 0; ++retry) {
  3233. row_mysql_unlock_data_dictionary(trx);
  3234. os_thread_yield();
  3235. row_mysql_lock_data_dictionary(trx);
  3236. }
  3237. if (table->n_foreign_key_checks_running > 0) {
  3238. ut_print_timestamp(stderr);
  3239. fputs(" InnoDB: Error: in ALTER TABLE ", stderr);
  3240. ut_print_name(stderr, trx, TRUE, old_name);
  3241. fprintf(stderr, "\n"
  3242. "InnoDB: a FOREIGN KEY check is running.\n"
  3243. "InnoDB: Cannot rename table.\n");
  3244. err = DB_TABLE_IN_FK_CHECK;
  3245. goto funct_exit;
  3246. }
  3247. /* We use the private SQL parser of Innobase to generate the query
  3248. graphs needed in updating the dictionary data from system tables. */
  3249. info = pars_info_create();
  3250. pars_info_add_str_literal(info, "new_table_name", new_name);
  3251. pars_info_add_str_literal(info, "old_table_name", old_name);
  3252. err = que_eval_sql(info,
  3253. "PROCEDURE RENAME_TABLE () IS\n"
  3254. "BEGIN\n"
  3255. "UPDATE SYS_TABLES SET NAME = :new_table_name\n"
  3256. " WHERE NAME = :old_table_name;\n"
  3257. "END;\n"
  3258. , FALSE, trx);
  3259. if (err != DB_SUCCESS) {
  3260. goto end;
  3261. } else if (!new_is_tmp) {
  3262. /* Rename all constraints. */
  3263. char new_table_name[MAX_TABLE_NAME_LEN] = "";
  3264. uint errors = 0;
  3265. info = pars_info_create();
  3266. pars_info_add_str_literal(info, "new_table_name", new_name);
  3267. pars_info_add_str_literal(info, "old_table_name", old_name);
  3268. strncpy(new_table_name, new_name, MAX_TABLE_NAME_LEN);
  3269. innobase_convert_to_system_charset(
  3270. strchr(new_table_name, '/') + 1,
  3271. strchr(new_name, '/') +1,
  3272. MAX_TABLE_NAME_LEN, &errors);
  3273. if (errors) {
  3274. /* Table name could not be converted from charset
  3275. my_charset_filename to UTF-8. This means that the
  3276. table name is already in UTF-8 (#mysql#50). */
  3277. strncpy(new_table_name, new_name, MAX_TABLE_NAME_LEN);
  3278. }
  3279. pars_info_add_str_literal(info, "new_table_utf8", new_table_name);
  3280. err = que_eval_sql(
  3281. info,
  3282. "PROCEDURE RENAME_CONSTRAINT_IDS () IS\n"
  3283. "gen_constr_prefix CHAR;\n"
  3284. "new_db_name CHAR;\n"
  3285. "foreign_id CHAR;\n"
  3286. "new_foreign_id CHAR;\n"
  3287. "old_db_name_len INT;\n"
  3288. "old_t_name_len INT;\n"
  3289. "new_db_name_len INT;\n"
  3290. "id_len INT;\n"
  3291. "offset INT;\n"
  3292. "found INT;\n"
  3293. "BEGIN\n"
  3294. "found := 1;\n"
  3295. "old_db_name_len := INSTR(:old_table_name, '/')-1;\n"
  3296. "new_db_name_len := INSTR(:new_table_name, '/')-1;\n"
  3297. "new_db_name := SUBSTR(:new_table_name, 0,\n"
  3298. " new_db_name_len);\n"
  3299. "old_t_name_len := LENGTH(:old_table_name);\n"
  3300. "WHILE found = 1 LOOP\n"
  3301. " SELECT ID INTO foreign_id\n"
  3302. " FROM SYS_FOREIGN\n"
  3303. " WHERE FOR_NAME = :old_table_name\n"
  3304. " AND TO_BINARY(FOR_NAME)\n"
  3305. " = TO_BINARY(:old_table_name)\n"
  3306. " LOCK IN SHARE MODE;\n"
  3307. " IF (SQL % NOTFOUND) THEN\n"
  3308. " found := 0;\n"
  3309. " ELSE\n"
  3310. " UPDATE SYS_FOREIGN\n"
  3311. " SET FOR_NAME = :new_table_name\n"
  3312. " WHERE ID = foreign_id;\n"
  3313. " id_len := LENGTH(foreign_id);\n"
  3314. " IF (INSTR(foreign_id, '/') > 0) THEN\n"
  3315. " IF (INSTR(foreign_id,\n"
  3316. " '_ibfk_') > 0)\n"
  3317. " THEN\n"
  3318. " offset := INSTR(foreign_id, '_ibfk_') - 1;\n"
  3319. " new_foreign_id :=\n"
  3320. " CONCAT(:new_table_utf8,\n"
  3321. " SUBSTR(foreign_id, offset,\n"
  3322. " id_len - offset));\n"
  3323. " ELSE\n"
  3324. " new_foreign_id :=\n"
  3325. " CONCAT(new_db_name,\n"
  3326. " SUBSTR(foreign_id,\n"
  3327. " old_db_name_len,\n"
  3328. " id_len - old_db_name_len));\n"
  3329. " END IF;\n"
  3330. " UPDATE SYS_FOREIGN\n"
  3331. " SET ID = new_foreign_id\n"
  3332. " WHERE ID = foreign_id;\n"
  3333. " UPDATE SYS_FOREIGN_COLS\n"
  3334. " SET ID = new_foreign_id\n"
  3335. " WHERE ID = foreign_id;\n"
  3336. " END IF;\n"
  3337. " END IF;\n"
  3338. "END LOOP;\n"
  3339. "UPDATE SYS_FOREIGN SET REF_NAME = :new_table_name\n"
  3340. "WHERE REF_NAME = :old_table_name\n"
  3341. " AND TO_BINARY(REF_NAME)\n"
  3342. " = TO_BINARY(:old_table_name);\n"
  3343. "END;\n"
  3344. , FALSE, trx);
  3345. } else if (n_constraints_to_drop > 0) {
  3346. /* Drop some constraints of tmp tables. */
  3347. ulint db_name_len = dict_get_db_name_len(old_name) + 1;
  3348. char* db_name = mem_heap_strdupl(heap, old_name,
  3349. db_name_len);
  3350. ulint i;
  3351. for (i = 0; i < n_constraints_to_drop; i++) {
  3352. err = row_delete_constraint(constraints_to_drop[i],
  3353. db_name, heap, trx);
  3354. if (err != DB_SUCCESS) {
  3355. break;
  3356. }
  3357. }
  3358. }
  3359. end:
  3360. if (err != DB_SUCCESS) {
  3361. if (err == DB_DUPLICATE_KEY) {
  3362. ut_print_timestamp(stderr);
  3363. fputs(" InnoDB: Error; possible reasons:\n"
  3364. "InnoDB: 1) Table rename would cause"
  3365. " two FOREIGN KEY constraints\n"
  3366. "InnoDB: to have the same internal name"
  3367. " in case-insensitive comparison.\n"
  3368. "InnoDB: 2) table ", stderr);
  3369. ut_print_name(stderr, trx, TRUE, new_name);
  3370. fputs(" exists in the InnoDB internal data\n"
  3371. "InnoDB: dictionary though MySQL is"
  3372. " trying to rename table ", stderr);
  3373. ut_print_name(stderr, trx, TRUE, old_name);
  3374. fputs(" to it.\n"
  3375. "InnoDB: Have you deleted the .frm file"
  3376. " and not used DROP TABLE?\n"
  3377. "InnoDB: You can look for further help from\n"
  3378. "InnoDB: " REFMAN "innodb-troubleshooting.html\n"
  3379. "InnoDB: If table ", stderr);
  3380. ut_print_name(stderr, trx, TRUE, new_name);
  3381. fputs(" is a temporary table #sql..., then"
  3382. " it can be that\n"
  3383. "InnoDB: there are still queries running"
  3384. " on the table, and it will be\n"
  3385. "InnoDB: dropped automatically when"
  3386. " the queries end.\n"
  3387. "InnoDB: You can drop the orphaned table"
  3388. " inside InnoDB by\n"
  3389. "InnoDB: creating an InnoDB table with"
  3390. " the same name in another\n"
  3391. "InnoDB: database and copying the .frm file"
  3392. " to the current database.\n"
  3393. "InnoDB: Then MySQL thinks the table exists,"
  3394. " and DROP TABLE will\n"
  3395. "InnoDB: succeed.\n", stderr);
  3396. }
  3397. trx->error_state = DB_SUCCESS;
  3398. trx_general_rollback_for_mysql(trx, NULL);
  3399. trx->error_state = DB_SUCCESS;
  3400. } else {
  3401. /* The following call will also rename the .ibd data file if
  3402. the table is stored in a single-table tablespace */
  3403. if (!dict_table_rename_in_cache(table, new_name,
  3404. !new_is_tmp)) {
  3405. trx->error_state = DB_SUCCESS;
  3406. trx_general_rollback_for_mysql(trx, NULL);
  3407. trx->error_state = DB_SUCCESS;
  3408. err = DB_ERROR;
  3409. goto funct_exit;
  3410. }
  3411. /* We only want to switch off some of the type checking in
  3412. an ALTER, not in a RENAME. */
  3413. err = dict_load_foreigns(
  3414. new_name, FALSE, !old_is_tmp || trx->check_foreigns);
  3415. if (err != DB_SUCCESS) {
  3416. ut_print_timestamp(stderr);
  3417. if (old_is_tmp) {
  3418. fputs(" InnoDB: Error: in ALTER TABLE ",
  3419. stderr);
  3420. ut_print_name(stderr, trx, TRUE, new_name);
  3421. fputs("\n"
  3422. "InnoDB: has or is referenced"
  3423. " in foreign key constraints\n"
  3424. "InnoDB: which are not compatible"
  3425. " with the new table definition.\n",
  3426. stderr);
  3427. } else {
  3428. fputs(" InnoDB: Error: in RENAME TABLE"
  3429. " table ",
  3430. stderr);
  3431. ut_print_name(stderr, trx, TRUE, new_name);
  3432. fputs("\n"
  3433. "InnoDB: is referenced in"
  3434. " foreign key constraints\n"
  3435. "InnoDB: which are not compatible"
  3436. " with the new table definition.\n",
  3437. stderr);
  3438. }
  3439. ut_a(dict_table_rename_in_cache(table,
  3440. old_name, FALSE));
  3441. trx->error_state = DB_SUCCESS;
  3442. trx_general_rollback_for_mysql(trx, NULL);
  3443. trx->error_state = DB_SUCCESS;
  3444. } else {
  3445. if (old_is_tmp && !new_is_tmp) {
  3446. /* After ALTER TABLE the table statistics
  3447. needs to be rebuilt. It will be rebuilt
  3448. when the table is loaded again. */
  3449. table->stat_initialized = FALSE;
  3450. }
  3451. }
  3452. }
  3453. funct_exit:
  3454. if (commit) {
  3455. trx_commit_for_mysql(trx);
  3456. }
  3457. if (UNIV_LIKELY_NULL(heap)) {
  3458. mem_heap_free(heap);
  3459. }
  3460. trx->op_info = "";
  3461. return(err);
  3462. }
  3463. /*********************************************************************//**
  3464. Checks that the index contains entries in an ascending order, unique
  3465. constraint is not broken, and calculates the number of index entries
  3466. in the read view of the current transaction.
  3467. @return TRUE if ok */
  3468. UNIV_INTERN
  3469. ibool
  3470. row_check_index_for_mysql(
  3471. /*======================*/
  3472. row_prebuilt_t* prebuilt, /*!< in: prebuilt struct
  3473. in MySQL handle */
  3474. const dict_index_t* index, /*!< in: index */
  3475. ulint* n_rows) /*!< out: number of entries
  3476. seen in the consistent read */
  3477. {
  3478. dtuple_t* prev_entry = NULL;
  3479. ulint matched_fields;
  3480. ulint matched_bytes;
  3481. byte* buf;
  3482. ulint ret;
  3483. rec_t* rec;
  3484. ibool is_ok = TRUE;
  3485. int cmp;
  3486. ibool contains_null;
  3487. ulint i;
  3488. ulint cnt;
  3489. mem_heap_t* heap = NULL;
  3490. ulint n_ext;
  3491. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  3492. ulint* offsets;
  3493. rec_offs_init(offsets_);
  3494. *n_rows = 0;
  3495. buf = mem_alloc(UNIV_PAGE_SIZE);
  3496. heap = mem_heap_create(100);
  3497. cnt = 1000;
  3498. ret = row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, 0);
  3499. loop:
  3500. /* Check thd->killed every 1,000 scanned rows */
  3501. if (--cnt == 0) {
  3502. if (trx_is_interrupted(prebuilt->trx)) {
  3503. goto func_exit;
  3504. }
  3505. cnt = 1000;
  3506. }
  3507. switch (ret) {
  3508. case DB_SUCCESS:
  3509. break;
  3510. default:
  3511. ut_print_timestamp(stderr);
  3512. fputs(" InnoDB: Warning: CHECK TABLE on ", stderr);
  3513. dict_index_name_print(stderr, prebuilt->trx, index);
  3514. fprintf(stderr, " returned %lu\n", ret);
  3515. /* fall through (this error is ignored by CHECK TABLE) */
  3516. case DB_END_OF_INDEX:
  3517. func_exit:
  3518. mem_free(buf);
  3519. mem_heap_free(heap);
  3520. return(is_ok);
  3521. }
  3522. *n_rows = *n_rows + 1;
  3523. /* row_search... returns the index record in buf, record origin offset
  3524. within buf stored in the first 4 bytes, because we have built a dummy
  3525. template */
  3526. rec = buf + mach_read_from_4(buf);
  3527. offsets = rec_get_offsets(rec, index, offsets_,
  3528. ULINT_UNDEFINED, &heap);
  3529. if (prev_entry != NULL) {
  3530. matched_fields = 0;
  3531. matched_bytes = 0;
  3532. cmp = cmp_dtuple_rec_with_match(prev_entry, rec, offsets,
  3533. &matched_fields,
  3534. &matched_bytes);
  3535. contains_null = FALSE;
  3536. /* In a unique secondary index we allow equal key values if
  3537. they contain SQL NULLs */
  3538. for (i = 0;
  3539. i < dict_index_get_n_ordering_defined_by_user(index);
  3540. i++) {
  3541. if (UNIV_SQL_NULL == dfield_get_len(
  3542. dtuple_get_nth_field(prev_entry, i))) {
  3543. contains_null = TRUE;
  3544. }
  3545. }
  3546. if (cmp > 0) {
  3547. fputs("InnoDB: index records in a wrong order in ",
  3548. stderr);
  3549. not_ok:
  3550. dict_index_name_print(stderr,
  3551. prebuilt->trx, index);
  3552. fputs("\n"
  3553. "InnoDB: prev record ", stderr);
  3554. dtuple_print(stderr, prev_entry);
  3555. fputs("\n"
  3556. "InnoDB: record ", stderr);
  3557. rec_print_new(stderr, rec, offsets);
  3558. putc('\n', stderr);
  3559. is_ok = FALSE;
  3560. } else if (dict_index_is_unique(index)
  3561. && !contains_null
  3562. && matched_fields
  3563. >= dict_index_get_n_ordering_defined_by_user(
  3564. index)) {
  3565. fputs("InnoDB: duplicate key in ", stderr);
  3566. goto not_ok;
  3567. }
  3568. }
  3569. {
  3570. mem_heap_t* tmp_heap = NULL;
  3571. /* Empty the heap on each round. But preserve offsets[]
  3572. for the row_rec_to_index_entry() call, by copying them
  3573. into a separate memory heap when needed. */
  3574. if (UNIV_UNLIKELY(offsets != offsets_)) {
  3575. ulint size = rec_offs_get_n_alloc(offsets)
  3576. * sizeof *offsets;
  3577. tmp_heap = mem_heap_create(size);
  3578. offsets = mem_heap_dup(tmp_heap, offsets, size);
  3579. }
  3580. mem_heap_empty(heap);
  3581. prev_entry = row_rec_to_index_entry(ROW_COPY_DATA, rec,
  3582. index, offsets,
  3583. &n_ext, heap);
  3584. if (UNIV_LIKELY_NULL(tmp_heap)) {
  3585. mem_heap_free(tmp_heap);
  3586. }
  3587. }
  3588. ret = row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, ROW_SEL_NEXT);
  3589. goto loop;
  3590. }
  3591. /*********************************************************************//**
  3592. Determines if a table is a magic monitor table.
  3593. @return TRUE if monitor table */
  3594. UNIV_INTERN
  3595. ibool
  3596. row_is_magic_monitor_table(
  3597. /*=======================*/
  3598. const char* table_name) /*!< in: name of the table, in the
  3599. form database/table_name */
  3600. {
  3601. const char* name; /* table_name without database/ */
  3602. ulint len;
  3603. name = strchr(table_name, '/');
  3604. ut_a(name != NULL);
  3605. name++;
  3606. len = strlen(name) + 1;
  3607. if (STR_EQ(name, len, S_innodb_monitor)
  3608. || STR_EQ(name, len, S_innodb_lock_monitor)
  3609. || STR_EQ(name, len, S_innodb_tablespace_monitor)
  3610. || STR_EQ(name, len, S_innodb_table_monitor)
  3611. || STR_EQ(name, len, S_innodb_mem_validate)) {
  3612. return(TRUE);
  3613. }
  3614. return(FALSE);
  3615. }