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.

1347 lines
48 KiB

11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2015, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2012, Facebook Inc.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /**************************************************//**
  15. @file include/dict0mem.h
  16. Data dictionary memory object creation
  17. Created 1/8/1996 Heikki Tuuri
  18. *******************************************************/
  19. #ifndef dict0mem_h
  20. #define dict0mem_h
  21. #include "univ.i"
  22. #include "dict0types.h"
  23. #include "data0type.h"
  24. #include "mem0mem.h"
  25. #include "row0types.h"
  26. #include "rem0types.h"
  27. #include "btr0types.h"
  28. #ifndef UNIV_HOTBACKUP
  29. # include "lock0types.h"
  30. # include "que0types.h"
  31. # include "sync0rw.h"
  32. #endif /* !UNIV_HOTBACKUP */
  33. #include "ut0mem.h"
  34. #include "ut0lst.h"
  35. #include "ut0rnd.h"
  36. #include "ut0byte.h"
  37. #include "hash0hash.h"
  38. #include "trx0types.h"
  39. #include "fts0fts.h"
  40. #include "os0once.h"
  41. #include <set>
  42. #include <algorithm>
  43. #include <iterator>
  44. #include <ostream>
  45. /* Forward declaration. */
  46. struct ib_rbt_t;
  47. /** Type flags of an index: OR'ing of the flags is allowed to define a
  48. combination of types */
  49. /* @{ */
  50. #define DICT_CLUSTERED 1 /*!< clustered index */
  51. #define DICT_UNIQUE 2 /*!< unique index */
  52. #define DICT_UNIVERSAL 4 /*!< index which can contain records from any
  53. other index */
  54. #define DICT_IBUF 8 /*!< insert buffer tree */
  55. #define DICT_CORRUPT 16 /*!< bit to store the corrupted flag
  56. in SYS_INDEXES.TYPE */
  57. #define DICT_FTS 32 /* FTS index; can't be combined with the
  58. other flags */
  59. #define DICT_IT_BITS 6 /*!< number of bits used for
  60. SYS_INDEXES.TYPE */
  61. /* @} */
  62. #if 0 /* not implemented, retained for history */
  63. /** Types for a table object */
  64. #define DICT_TABLE_ORDINARY 1 /*!< ordinary table */
  65. #define DICT_TABLE_CLUSTER_MEMBER 2
  66. #define DICT_TABLE_CLUSTER 3 /* this means that the table is
  67. really a cluster definition */
  68. #endif
  69. /* Table and tablespace flags are generally not used for the Antelope file
  70. format except for the low order bit, which is used differently depending on
  71. where the flags are stored.
  72. ==================== Low order flags bit =========================
  73. | REDUNDANT | COMPACT | COMPRESSED and DYNAMIC
  74. SYS_TABLES.TYPE | 1 | 1 | 1
  75. dict_table_t::flags | 0 | 1 | 1
  76. FSP_SPACE_FLAGS | 0 | 0 | 1
  77. fil_space_t::flags | 0 | 0 | 1
  78. Before the 5.1 plugin, SYS_TABLES.TYPE was always DICT_TABLE_ORDINARY (1)
  79. and the tablespace flags field was always 0. In the 5.1 plugin, these fields
  80. were repurposed to identify compressed and dynamic row formats.
  81. The following types and constants describe the flags found in dict_table_t
  82. and SYS_TABLES.TYPE. Similar flags found in fil_space_t and FSP_SPACE_FLAGS
  83. are described in fsp0fsp.h. */
  84. /* @{ */
  85. /** dict_table_t::flags bit 0 is equal to 0 if the row format = Redundant */
  86. #define DICT_TF_REDUNDANT 0 /*!< Redundant row format. */
  87. /** dict_table_t::flags bit 0 is equal to 1 if the row format = Compact */
  88. #define DICT_TF_COMPACT 1 /*!< Compact row format. */
  89. /** This bitmask is used in SYS_TABLES.N_COLS to set and test whether
  90. the Compact page format is used, i.e ROW_FORMAT != REDUNDANT */
  91. #define DICT_N_COLS_COMPACT 0x80000000UL
  92. /** Width of the COMPACT flag */
  93. #define DICT_TF_WIDTH_COMPACT 1
  94. /** Width of the ZIP_SSIZE flag */
  95. #define DICT_TF_WIDTH_ZIP_SSIZE 4
  96. /** Width of the ATOMIC_BLOBS flag. The Antelope file formats broke up
  97. BLOB and TEXT fields, storing the first 768 bytes in the clustered index.
  98. Brracuda row formats store the whole blob or text field off-page atomically.
  99. Secondary indexes are created from this external data using row_ext_t
  100. to cache the BLOB prefixes. */
  101. #define DICT_TF_WIDTH_ATOMIC_BLOBS 1
  102. /** If a table is created with the MYSQL option DATA DIRECTORY and
  103. innodb-file-per-table, an older engine will not be able to find that table.
  104. This flag prevents older engines from attempting to open the table and
  105. allows InnoDB to update_create_info() accordingly. */
  106. #define DICT_TF_WIDTH_DATA_DIR 1
  107. /** Width of all the currently known table flags */
  108. #define DICT_TF_BITS (DICT_TF_WIDTH_COMPACT \
  109. + DICT_TF_WIDTH_ZIP_SSIZE \
  110. + DICT_TF_WIDTH_ATOMIC_BLOBS \
  111. + DICT_TF_WIDTH_DATA_DIR)
  112. /** A mask of all the known/used bits in table flags */
  113. #define DICT_TF_BIT_MASK (~(~0 << DICT_TF_BITS))
  114. /** Zero relative shift position of the COMPACT field */
  115. #define DICT_TF_POS_COMPACT 0
  116. /** Zero relative shift position of the ZIP_SSIZE field */
  117. #define DICT_TF_POS_ZIP_SSIZE (DICT_TF_POS_COMPACT \
  118. + DICT_TF_WIDTH_COMPACT)
  119. /** Zero relative shift position of the ATOMIC_BLOBS field */
  120. #define DICT_TF_POS_ATOMIC_BLOBS (DICT_TF_POS_ZIP_SSIZE \
  121. + DICT_TF_WIDTH_ZIP_SSIZE)
  122. /** Zero relative shift position of the DATA_DIR field */
  123. #define DICT_TF_POS_DATA_DIR (DICT_TF_POS_ATOMIC_BLOBS \
  124. + DICT_TF_WIDTH_ATOMIC_BLOBS)
  125. /** Zero relative shift position of the start of the UNUSED bits */
  126. #define DICT_TF_POS_UNUSED (DICT_TF_POS_DATA_DIR \
  127. + DICT_TF_WIDTH_DATA_DIR)
  128. /** Bit mask of the COMPACT field */
  129. #define DICT_TF_MASK_COMPACT \
  130. ((~(~0 << DICT_TF_WIDTH_COMPACT)) \
  131. << DICT_TF_POS_COMPACT)
  132. /** Bit mask of the ZIP_SSIZE field */
  133. #define DICT_TF_MASK_ZIP_SSIZE \
  134. ((~(~0 << DICT_TF_WIDTH_ZIP_SSIZE)) \
  135. << DICT_TF_POS_ZIP_SSIZE)
  136. /** Bit mask of the ATOMIC_BLOBS field */
  137. #define DICT_TF_MASK_ATOMIC_BLOBS \
  138. ((~(~0 << DICT_TF_WIDTH_ATOMIC_BLOBS)) \
  139. << DICT_TF_POS_ATOMIC_BLOBS)
  140. /** Bit mask of the DATA_DIR field */
  141. #define DICT_TF_MASK_DATA_DIR \
  142. ((~(~0 << DICT_TF_WIDTH_DATA_DIR)) \
  143. << DICT_TF_POS_DATA_DIR)
  144. /** Return the value of the COMPACT field */
  145. #define DICT_TF_GET_COMPACT(flags) \
  146. ((flags & DICT_TF_MASK_COMPACT) \
  147. >> DICT_TF_POS_COMPACT)
  148. /** Return the value of the ZIP_SSIZE field */
  149. #define DICT_TF_GET_ZIP_SSIZE(flags) \
  150. ((flags & DICT_TF_MASK_ZIP_SSIZE) \
  151. >> DICT_TF_POS_ZIP_SSIZE)
  152. /** Return the value of the ATOMIC_BLOBS field */
  153. #define DICT_TF_HAS_ATOMIC_BLOBS(flags) \
  154. ((flags & DICT_TF_MASK_ATOMIC_BLOBS) \
  155. >> DICT_TF_POS_ATOMIC_BLOBS)
  156. /** Return the value of the ATOMIC_BLOBS field */
  157. #define DICT_TF_HAS_DATA_DIR(flags) \
  158. ((flags & DICT_TF_MASK_DATA_DIR) \
  159. >> DICT_TF_POS_DATA_DIR)
  160. /** Return the contents of the UNUSED bits */
  161. #define DICT_TF_GET_UNUSED(flags) \
  162. (flags >> DICT_TF_POS_UNUSED)
  163. /* @} */
  164. /** @brief Table Flags set number 2.
  165. These flags will be stored in SYS_TABLES.MIX_LEN. All unused flags
  166. will be written as 0. The column may contain garbage for tables
  167. created with old versions of InnoDB that only implemented
  168. ROW_FORMAT=REDUNDANT. InnoDB engines do not check these flags
  169. for unknown bits in order to protect backward incompatibility. */
  170. /* @{ */
  171. /** Total number of bits in table->flags2. */
  172. #define DICT_TF2_BITS 7
  173. #define DICT_TF2_BIT_MASK ~(~0 << DICT_TF2_BITS)
  174. /** TEMPORARY; TRUE for tables from CREATE TEMPORARY TABLE. */
  175. #define DICT_TF2_TEMPORARY 1
  176. /** The table has an internal defined DOC ID column */
  177. #define DICT_TF2_FTS_HAS_DOC_ID 2
  178. /** The table has an FTS index */
  179. #define DICT_TF2_FTS 4
  180. /** Need to add Doc ID column for FTS index build.
  181. This is a transient bit for index build */
  182. #define DICT_TF2_FTS_ADD_DOC_ID 8
  183. /** This bit is used during table creation to indicate that it will
  184. use its own tablespace instead of the system tablespace. */
  185. #define DICT_TF2_USE_TABLESPACE 16
  186. /** Set when we discard/detach the tablespace */
  187. #define DICT_TF2_DISCARDED 32
  188. /** This bit is set if all aux table names (both common tables and
  189. index tables) of a FTS table are in HEX format. */
  190. #define DICT_TF2_FTS_AUX_HEX_NAME 64
  191. /* @} */
  192. #define DICT_TF2_FLAG_SET(table, flag) \
  193. (table->flags2 |= (flag))
  194. #define DICT_TF2_FLAG_IS_SET(table, flag) \
  195. (table->flags2 & (flag))
  196. #define DICT_TF2_FLAG_UNSET(table, flag) \
  197. (table->flags2 &= ~(flag))
  198. /** Tables could be chained together with Foreign key constraint. When
  199. first load the parent table, we would load all of its descedents.
  200. This could result in rescursive calls and out of stack error eventually.
  201. DICT_FK_MAX_RECURSIVE_LOAD defines the maximum number of recursive loads,
  202. when exceeded, the child table will not be loaded. It will be loaded when
  203. the foreign constraint check needs to be run. */
  204. #define DICT_FK_MAX_RECURSIVE_LOAD 20
  205. /** Similarly, when tables are chained together with foreign key constraints
  206. with on cascading delete/update clause, delete from parent table could
  207. result in recursive cascading calls. This defines the maximum number of
  208. such cascading deletes/updates allowed. When exceeded, the delete from
  209. parent table will fail, and user has to drop excessive foreign constraint
  210. before proceeds. */
  211. #define FK_MAX_CASCADE_DEL 255
  212. /**********************************************************************//**
  213. Creates a table memory object.
  214. @return own: table object */
  215. UNIV_INTERN
  216. dict_table_t*
  217. dict_mem_table_create(
  218. /*==================*/
  219. const char* name, /*!< in: table name */
  220. ulint space, /*!< in: space where the clustered index
  221. of the table is placed */
  222. ulint n_cols, /*!< in: number of columns */
  223. ulint flags, /*!< in: table flags */
  224. ulint flags2); /*!< in: table flags2 */
  225. /**********************************************************************//**
  226. Determines if a table belongs to a system database
  227. @return */
  228. UNIV_INTERN
  229. bool
  230. dict_mem_table_is_system(
  231. /*==================*/
  232. char *name); /*!< in: table name */
  233. /****************************************************************//**
  234. Free a table memory object. */
  235. UNIV_INTERN
  236. void
  237. dict_mem_table_free(
  238. /*================*/
  239. dict_table_t* table); /*!< in: table */
  240. /**********************************************************************//**
  241. Adds a column definition to a table. */
  242. UNIV_INTERN
  243. void
  244. dict_mem_table_add_col(
  245. /*===================*/
  246. dict_table_t* table, /*!< in: table */
  247. mem_heap_t* heap, /*!< in: temporary memory heap, or NULL */
  248. const char* name, /*!< in: column name, or NULL */
  249. ulint mtype, /*!< in: main datatype */
  250. ulint prtype, /*!< in: precise type */
  251. ulint len) /*!< in: precision */
  252. __attribute__((nonnull(1)));
  253. /**********************************************************************//**
  254. Renames a column of a table in the data dictionary cache. */
  255. UNIV_INTERN
  256. void
  257. dict_mem_table_col_rename(
  258. /*======================*/
  259. dict_table_t* table, /*!< in/out: table */
  260. unsigned nth_col,/*!< in: column index */
  261. const char* from, /*!< in: old column name */
  262. const char* to) /*!< in: new column name */
  263. __attribute__((nonnull));
  264. /**********************************************************************//**
  265. This function populates a dict_col_t memory structure with
  266. supplied information. */
  267. UNIV_INTERN
  268. void
  269. dict_mem_fill_column_struct(
  270. /*========================*/
  271. dict_col_t* column, /*!< out: column struct to be
  272. filled */
  273. ulint col_pos, /*!< in: column position */
  274. ulint mtype, /*!< in: main data type */
  275. ulint prtype, /*!< in: precise type */
  276. ulint col_len); /*!< in: column length */
  277. /**********************************************************************//**
  278. This function poplulates a dict_index_t index memory structure with
  279. supplied information. */
  280. UNIV_INLINE
  281. void
  282. dict_mem_fill_index_struct(
  283. /*=======================*/
  284. dict_index_t* index, /*!< out: index to be filled */
  285. mem_heap_t* heap, /*!< in: memory heap */
  286. const char* table_name, /*!< in: table name */
  287. const char* index_name, /*!< in: index name */
  288. ulint space, /*!< in: space where the index tree is
  289. placed, ignored if the index is of
  290. the clustered type */
  291. ulint type, /*!< in: DICT_UNIQUE,
  292. DICT_CLUSTERED, ... ORed */
  293. ulint n_fields); /*!< in: number of fields */
  294. /**********************************************************************//**
  295. Creates an index memory object.
  296. @return own: index object */
  297. UNIV_INTERN
  298. dict_index_t*
  299. dict_mem_index_create(
  300. /*==================*/
  301. const char* table_name, /*!< in: table name */
  302. const char* index_name, /*!< in: index name */
  303. ulint space, /*!< in: space where the index tree is
  304. placed, ignored if the index is of
  305. the clustered type */
  306. ulint type, /*!< in: DICT_UNIQUE,
  307. DICT_CLUSTERED, ... ORed */
  308. ulint n_fields); /*!< in: number of fields */
  309. /**********************************************************************//**
  310. Adds a field definition to an index. NOTE: does not take a copy
  311. of the column name if the field is a column. The memory occupied
  312. by the column name may be released only after publishing the index. */
  313. UNIV_INTERN
  314. void
  315. dict_mem_index_add_field(
  316. /*=====================*/
  317. dict_index_t* index, /*!< in: index */
  318. const char* name, /*!< in: column name */
  319. ulint prefix_len); /*!< in: 0 or the column prefix length
  320. in a MySQL index like
  321. INDEX (textcol(25)) */
  322. /**********************************************************************//**
  323. Frees an index memory object. */
  324. UNIV_INTERN
  325. void
  326. dict_mem_index_free(
  327. /*================*/
  328. dict_index_t* index); /*!< in: index */
  329. /**********************************************************************//**
  330. Creates and initializes a foreign constraint memory object.
  331. @return own: foreign constraint struct */
  332. UNIV_INTERN
  333. dict_foreign_t*
  334. dict_mem_foreign_create(void);
  335. /*=========================*/
  336. /**********************************************************************//**
  337. Sets the foreign_table_name_lookup pointer based on the value of
  338. lower_case_table_names. If that is 0 or 1, foreign_table_name_lookup
  339. will point to foreign_table_name. If 2, then another string is
  340. allocated from the heap and set to lower case. */
  341. UNIV_INTERN
  342. void
  343. dict_mem_foreign_table_name_lookup_set(
  344. /*===================================*/
  345. dict_foreign_t* foreign, /*!< in/out: foreign struct */
  346. ibool do_alloc); /*!< in: is an alloc needed */
  347. /**********************************************************************//**
  348. Sets the referenced_table_name_lookup pointer based on the value of
  349. lower_case_table_names. If that is 0 or 1, referenced_table_name_lookup
  350. will point to referenced_table_name. If 2, then another string is
  351. allocated from the heap and set to lower case. */
  352. UNIV_INTERN
  353. void
  354. dict_mem_referenced_table_name_lookup_set(
  355. /*======================================*/
  356. dict_foreign_t* foreign, /*!< in/out: foreign struct */
  357. ibool do_alloc); /*!< in: is an alloc needed */
  358. /** Create a temporary tablename like "#sql-ibtid-inc where
  359. tid = the Table ID
  360. inc = a randomly initialized number that is incremented for each file
  361. The table ID is a 64 bit integer, can use up to 20 digits, and is
  362. initialized at bootstrap. The second number is 32 bits, can use up to 10
  363. digits, and is initialized at startup to a randomly distributed number.
  364. It is hoped that the combination of these two numbers will provide a
  365. reasonably unique temporary file name.
  366. @param[in] heap A memory heap
  367. @param[in] dbtab Table name in the form database/table name
  368. @param[in] id Table id
  369. @return A unique temporary tablename suitable for InnoDB use */
  370. UNIV_INTERN
  371. char*
  372. dict_mem_create_temporary_tablename(
  373. mem_heap_t* heap,
  374. const char* dbtab,
  375. table_id_t id);
  376. /** Initialize dict memory variables */
  377. void
  378. dict_mem_init(void);
  379. /** Data structure for a column in a table */
  380. struct dict_col_t{
  381. /*----------------------*/
  382. /** The following are copied from dtype_t,
  383. so that all bit-fields can be packed tightly. */
  384. /* @{ */
  385. unsigned prtype:32; /*!< precise type; MySQL data
  386. type, charset code, flags to
  387. indicate nullability,
  388. signedness, whether this is a
  389. binary string, whether this is
  390. a true VARCHAR where MySQL
  391. uses 2 bytes to store the length */
  392. unsigned mtype:8; /*!< main data type */
  393. /* the remaining fields do not affect alphabetical ordering: */
  394. unsigned len:16; /*!< length; for MySQL data this
  395. is field->pack_length(),
  396. except that for a >= 5.0.3
  397. type true VARCHAR this is the
  398. maximum byte length of the
  399. string data (in addition to
  400. the string, MySQL uses 1 or 2
  401. bytes to store the string length) */
  402. unsigned mbminmaxlen:5; /*!< minimum and maximum length of a
  403. character, in bytes;
  404. DATA_MBMINMAXLEN(mbminlen,mbmaxlen);
  405. mbminlen=DATA_MBMINLEN(mbminmaxlen);
  406. mbmaxlen=DATA_MBMINLEN(mbminmaxlen) */
  407. /*----------------------*/
  408. /* End of definitions copied from dtype_t */
  409. /* @} */
  410. unsigned ind:10; /*!< table column position
  411. (starting from 0) */
  412. unsigned ord_part:1; /*!< nonzero if this column
  413. appears in the ordering fields
  414. of an index */
  415. unsigned max_prefix:12; /*!< maximum index prefix length on
  416. this column. Our current max limit is
  417. 3072 for Barracuda table */
  418. };
  419. /** @brief DICT_ANTELOPE_MAX_INDEX_COL_LEN is measured in bytes and
  420. is the maximum indexed column length (or indexed prefix length) in
  421. ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT. Also, in any format,
  422. any fixed-length field that is longer than this will be encoded as
  423. a variable-length field.
  424. It is set to 3*256, so that one can create a column prefix index on
  425. 256 characters of a TEXT or VARCHAR column also in the UTF-8
  426. charset. In that charset, a character may take at most 3 bytes. This
  427. constant MUST NOT BE CHANGED, or the compatibility of InnoDB data
  428. files would be at risk! */
  429. #define DICT_ANTELOPE_MAX_INDEX_COL_LEN REC_ANTELOPE_MAX_INDEX_COL_LEN
  430. /** Find out maximum indexed column length by its table format.
  431. For ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT, the maximum
  432. field length is REC_ANTELOPE_MAX_INDEX_COL_LEN - 1 (767). For
  433. Barracuda row formats COMPRESSED and DYNAMIC, the length could
  434. be REC_VERSION_56_MAX_INDEX_COL_LEN (3072) bytes */
  435. #define DICT_MAX_FIELD_LEN_BY_FORMAT(table) \
  436. ((dict_table_get_format(table) < UNIV_FORMAT_B) \
  437. ? (REC_ANTELOPE_MAX_INDEX_COL_LEN - 1) \
  438. : REC_VERSION_56_MAX_INDEX_COL_LEN)
  439. #define DICT_MAX_FIELD_LEN_BY_FORMAT_FLAG(flags) \
  440. ((DICT_TF_HAS_ATOMIC_BLOBS(flags) < UNIV_FORMAT_B) \
  441. ? (REC_ANTELOPE_MAX_INDEX_COL_LEN - 1) \
  442. : REC_VERSION_56_MAX_INDEX_COL_LEN)
  443. /** Defines the maximum fixed length column size */
  444. #define DICT_MAX_FIXED_COL_LEN DICT_ANTELOPE_MAX_INDEX_COL_LEN
  445. /** Data structure for a field in an index */
  446. struct dict_field_t{
  447. dict_col_t* col; /*!< pointer to the table column */
  448. const char* name; /*!< name of the column */
  449. unsigned prefix_len:12; /*!< 0 or the length of the column
  450. prefix in bytes in a MySQL index of
  451. type, e.g., INDEX (textcol(25));
  452. must be smaller than
  453. DICT_MAX_FIELD_LEN_BY_FORMAT;
  454. NOTE that in the UTF-8 charset, MySQL
  455. sets this to (mbmaxlen * the prefix len)
  456. in UTF-8 chars */
  457. unsigned fixed_len:10; /*!< 0 or the fixed length of the
  458. column if smaller than
  459. DICT_ANTELOPE_MAX_INDEX_COL_LEN */
  460. };
  461. /**********************************************************************//**
  462. PADDING HEURISTIC BASED ON LINEAR INCREASE OF PADDING TO AVOID
  463. COMPRESSION FAILURES
  464. (Note: this is relevant only for compressed indexes)
  465. GOAL: Avoid compression failures by maintaining information about the
  466. compressibility of data. If data is not very compressible then leave
  467. some extra space 'padding' in the uncompressed page making it more
  468. likely that compression of less than fully packed uncompressed page will
  469. succeed.
  470. This padding heuristic works by increasing the pad linearly until the
  471. desired failure rate is reached. A "round" is a fixed number of
  472. compression operations.
  473. After each round, the compression failure rate for that round is
  474. computed. If the failure rate is too high, then padding is incremented
  475. by a fixed value, otherwise it's left intact.
  476. If the compression failure is lower than the desired rate for a fixed
  477. number of consecutive rounds, then the padding is decreased by a fixed
  478. value. This is done to prevent overshooting the padding value,
  479. and to accommodate the possible change in data compressibility. */
  480. /** Number of zip ops in one round. */
  481. #define ZIP_PAD_ROUND_LEN (128)
  482. /** Number of successful rounds after which the padding is decreased */
  483. #define ZIP_PAD_SUCCESSFUL_ROUND_LIMIT (5)
  484. /** Amount by which padding is increased. */
  485. #define ZIP_PAD_INCR (128)
  486. /** Percentage of compression failures that are allowed in a single
  487. round */
  488. extern ulong zip_failure_threshold_pct;
  489. /** Maximum percentage of a page that can be allowed as a pad to avoid
  490. compression failures */
  491. extern ulong zip_pad_max;
  492. /** Data structure to hold information about how much space in
  493. an uncompressed page should be left as padding to avoid compression
  494. failures. This estimate is based on a self-adapting heuristic. */
  495. struct zip_pad_info_t {
  496. os_fast_mutex_t*
  497. mutex; /*!< mutex protecting the info */
  498. ulint pad; /*!< number of bytes used as pad */
  499. ulint success;/*!< successful compression ops during
  500. current round */
  501. ulint failure;/*!< failed compression ops during
  502. current round */
  503. ulint n_rounds;/*!< number of currently successful
  504. rounds */
  505. volatile os_once::state_t
  506. mutex_created;
  507. /*!< Creation state of mutex member */
  508. };
  509. /** Data structure for an index. Most fields will be
  510. initialized to 0, NULL or FALSE in dict_mem_index_create(). */
  511. struct dict_index_t{
  512. index_id_t id; /*!< id of the index */
  513. mem_heap_t* heap; /*!< memory heap */
  514. const char* name; /*!< index name */
  515. const char* table_name;/*!< table name */
  516. dict_table_t* table; /*!< back pointer to table */
  517. #ifndef UNIV_HOTBACKUP
  518. unsigned space:32;
  519. /*!< space where the index tree is placed */
  520. unsigned page:32;/*!< index tree root page number */
  521. #endif /* !UNIV_HOTBACKUP */
  522. unsigned type:DICT_IT_BITS;
  523. /*!< index type (DICT_CLUSTERED, DICT_UNIQUE,
  524. DICT_UNIVERSAL, DICT_IBUF, DICT_CORRUPT) */
  525. #define MAX_KEY_LENGTH_BITS 12
  526. unsigned trx_id_offset:MAX_KEY_LENGTH_BITS;
  527. /*!< position of the trx id column
  528. in a clustered index record, if the fields
  529. before it are known to be of a fixed size,
  530. 0 otherwise */
  531. #if (1<<MAX_KEY_LENGTH_BITS) < MAX_KEY_LENGTH
  532. # error (1<<MAX_KEY_LENGTH_BITS) < MAX_KEY_LENGTH
  533. #endif
  534. unsigned n_user_defined_cols:10;
  535. /*!< number of columns the user defined to
  536. be in the index: in the internal
  537. representation we add more columns */
  538. unsigned n_uniq:10;/*!< number of fields from the beginning
  539. which are enough to determine an index
  540. entry uniquely */
  541. unsigned n_def:10;/*!< number of fields defined so far */
  542. unsigned n_fields:10;/*!< number of fields in the index */
  543. unsigned n_nullable:10;/*!< number of nullable fields */
  544. unsigned cached:1;/*!< TRUE if the index object is in the
  545. dictionary cache */
  546. unsigned to_be_dropped:1;
  547. /*!< TRUE if the index is to be dropped;
  548. protected by dict_operation_lock */
  549. unsigned online_status:2;
  550. /*!< enum online_index_status.
  551. Transitions from ONLINE_INDEX_COMPLETE (to
  552. ONLINE_INDEX_CREATION) are protected
  553. by dict_operation_lock and
  554. dict_sys->mutex. Other changes are
  555. protected by index->lock. */
  556. dict_field_t* fields; /*!< array of field descriptions */
  557. #ifndef UNIV_HOTBACKUP
  558. UT_LIST_NODE_T(dict_index_t)
  559. indexes;/*!< list of indexes of the table */
  560. btr_search_t* search_info;
  561. /*!< info used in optimistic searches */
  562. row_log_t* online_log;
  563. /*!< the log of modifications
  564. during online index creation;
  565. valid when online_status is
  566. ONLINE_INDEX_CREATION */
  567. /*----------------------*/
  568. /** Statistics for query optimization */
  569. /* @{ */
  570. ib_uint64_t* stat_n_diff_key_vals;
  571. /*!< approximate number of different
  572. key values for this index, for each
  573. n-column prefix where 1 <= n <=
  574. dict_get_n_unique(index) (the array is
  575. indexed from 0 to n_uniq-1); we
  576. periodically calculate new
  577. estimates */
  578. ib_uint64_t* stat_n_sample_sizes;
  579. /*!< number of pages that were sampled
  580. to calculate each of stat_n_diff_key_vals[],
  581. e.g. stat_n_sample_sizes[3] pages were sampled
  582. to get the number stat_n_diff_key_vals[3]. */
  583. ib_uint64_t* stat_n_non_null_key_vals;
  584. /* approximate number of non-null key values
  585. for this index, for each column where
  586. 1 <= n <= dict_get_n_unique(index) (the array
  587. is indexed from 0 to n_uniq-1); This
  588. is used when innodb_stats_method is
  589. "nulls_ignored". */
  590. ulint stat_index_size;
  591. /*!< approximate index size in
  592. database pages */
  593. ulint stat_n_leaf_pages;
  594. /*!< approximate number of leaf pages in the
  595. index tree */
  596. bool stats_error_printed;
  597. /*!< has persistent statistics error printed
  598. for this index ? */
  599. /* @} */
  600. rw_lock_t lock; /*!< read-write lock protecting the
  601. upper levels of the index tree */
  602. trx_id_t trx_id; /*!< id of the transaction that created this
  603. index, or 0 if the index existed
  604. when InnoDB was started up */
  605. zip_pad_info_t zip_pad;/*!< Information about state of
  606. compression failures and successes */
  607. #endif /* !UNIV_HOTBACKUP */
  608. #ifdef UNIV_BLOB_DEBUG
  609. ib_mutex_t blobs_mutex;
  610. /*!< mutex protecting blobs */
  611. ib_rbt_t* blobs; /*!< map of (page_no,heap_no,field_no)
  612. to first_blob_page_no; protected by
  613. blobs_mutex; @see btr_blob_dbg_t */
  614. #endif /* UNIV_BLOB_DEBUG */
  615. #ifdef UNIV_DEBUG
  616. ulint magic_n;/*!< magic number */
  617. /** Value of dict_index_t::magic_n */
  618. # define DICT_INDEX_MAGIC_N 76789786
  619. #endif
  620. };
  621. /** The status of online index creation */
  622. enum online_index_status {
  623. /** the index is complete and ready for access */
  624. ONLINE_INDEX_COMPLETE = 0,
  625. /** the index is being created, online
  626. (allowing concurrent modifications) */
  627. ONLINE_INDEX_CREATION,
  628. /** secondary index creation was aborted and the index
  629. should be dropped as soon as index->table->n_ref_count reaches 0,
  630. or online table rebuild was aborted and the clustered index
  631. of the original table should soon be restored to
  632. ONLINE_INDEX_COMPLETE */
  633. ONLINE_INDEX_ABORTED,
  634. /** the online index creation was aborted, the index was
  635. dropped from the data dictionary and the tablespace, and it
  636. should be dropped from the data dictionary cache as soon as
  637. index->table->n_ref_count reaches 0. */
  638. ONLINE_INDEX_ABORTED_DROPPED
  639. };
  640. /** Data structure for a foreign key constraint; an example:
  641. FOREIGN KEY (A, B) REFERENCES TABLE2 (C, D). Most fields will be
  642. initialized to 0, NULL or FALSE in dict_mem_foreign_create(). */
  643. struct dict_foreign_t{
  644. mem_heap_t* heap; /*!< this object is allocated from
  645. this memory heap */
  646. char* id; /*!< id of the constraint as a
  647. null-terminated string */
  648. unsigned n_fields:10; /*!< number of indexes' first fields
  649. for which the foreign key
  650. constraint is defined: we allow the
  651. indexes to contain more fields than
  652. mentioned in the constraint, as long
  653. as the first fields are as mentioned */
  654. unsigned type:6; /*!< 0 or DICT_FOREIGN_ON_DELETE_CASCADE
  655. or DICT_FOREIGN_ON_DELETE_SET_NULL */
  656. char* foreign_table_name;/*!< foreign table name */
  657. char* foreign_table_name_lookup;
  658. /*!< foreign table name used for dict lookup */
  659. dict_table_t* foreign_table; /*!< table where the foreign key is */
  660. const char** foreign_col_names;/*!< names of the columns in the
  661. foreign key */
  662. char* referenced_table_name;/*!< referenced table name */
  663. char* referenced_table_name_lookup;
  664. /*!< referenced table name for dict lookup*/
  665. dict_table_t* referenced_table;/*!< table where the referenced key
  666. is */
  667. const char** referenced_col_names;/*!< names of the referenced
  668. columns in the referenced table */
  669. dict_index_t* foreign_index; /*!< foreign index; we require that
  670. both tables contain explicitly defined
  671. indexes for the constraint: InnoDB
  672. does not generate new indexes
  673. implicitly */
  674. dict_index_t* referenced_index;/*!< referenced index */
  675. };
  676. std::ostream&
  677. operator<< (std::ostream& out, const dict_foreign_t& foreign);
  678. struct dict_foreign_print {
  679. dict_foreign_print(std::ostream& out)
  680. : m_out(out)
  681. {}
  682. void operator()(const dict_foreign_t* foreign) {
  683. m_out << *foreign;
  684. }
  685. private:
  686. std::ostream& m_out;
  687. };
  688. /** Compare two dict_foreign_t objects using their ids. Used in the ordering
  689. of dict_table_t::foreign_set and dict_table_t::referenced_set. It returns
  690. true if the first argument is considered to go before the second in the
  691. strict weak ordering it defines, and false otherwise. */
  692. struct dict_foreign_compare {
  693. bool operator()(
  694. const dict_foreign_t* lhs,
  695. const dict_foreign_t* rhs) const
  696. {
  697. return(ut_strcmp(lhs->id, rhs->id) < 0);
  698. }
  699. };
  700. /** A function object to find a foreign key with the given index as the
  701. referenced index. Return the foreign key with matching criteria or NULL */
  702. struct dict_foreign_with_index {
  703. dict_foreign_with_index(const dict_index_t* index)
  704. : m_index(index)
  705. {}
  706. bool operator()(const dict_foreign_t* foreign) const
  707. {
  708. return(foreign->referenced_index == m_index);
  709. }
  710. const dict_index_t* m_index;
  711. };
  712. /* A function object to check if the foreign constraint is between different
  713. tables. Returns true if foreign key constraint is between different tables,
  714. false otherwise. */
  715. struct dict_foreign_different_tables {
  716. bool operator()(const dict_foreign_t* foreign) const
  717. {
  718. return(foreign->foreign_table != foreign->referenced_table);
  719. }
  720. };
  721. /** A function object to check if the foreign key constraint has the same
  722. name as given. If the full name of the foreign key constraint doesn't match,
  723. then, check if removing the database name from the foreign key constraint
  724. matches. Return true if it matches, false otherwise. */
  725. struct dict_foreign_matches_id {
  726. dict_foreign_matches_id(const char* id)
  727. : m_id(id)
  728. {}
  729. bool operator()(const dict_foreign_t* foreign) const
  730. {
  731. if (0 == innobase_strcasecmp(foreign->id, m_id)) {
  732. return(true);
  733. }
  734. if (const char* pos = strchr(foreign->id, '/')) {
  735. if (0 == innobase_strcasecmp(m_id, pos + 1)) {
  736. return(true);
  737. }
  738. }
  739. return(false);
  740. }
  741. const char* m_id;
  742. };
  743. typedef std::set<dict_foreign_t*, dict_foreign_compare> dict_foreign_set;
  744. std::ostream&
  745. operator<< (std::ostream& out, const dict_foreign_set& fk_set);
  746. /** Function object to check if a foreign key object is there
  747. in the given foreign key set or not. It returns true if the
  748. foreign key is not found, false otherwise */
  749. struct dict_foreign_not_exists {
  750. dict_foreign_not_exists(const dict_foreign_set& obj_)
  751. : m_foreigns(obj_)
  752. {}
  753. /* Return true if the given foreign key is not found */
  754. bool operator()(dict_foreign_t* const & foreign) const {
  755. return(m_foreigns.find(foreign) == m_foreigns.end());
  756. }
  757. private:
  758. const dict_foreign_set& m_foreigns;
  759. };
  760. /** Validate the search order in the foreign key set.
  761. @param[in] fk_set the foreign key set to be validated
  762. @return true if search order is fine in the set, false otherwise. */
  763. bool
  764. dict_foreign_set_validate(
  765. const dict_foreign_set& fk_set);
  766. /** Validate the search order in the foreign key sets of the table
  767. (foreign_set and referenced_set).
  768. @param[in] table table whose foreign key sets are to be validated
  769. @return true if foreign key sets are fine, false otherwise. */
  770. bool
  771. dict_foreign_set_validate(
  772. const dict_table_t& table);
  773. /*********************************************************************//**
  774. Frees a foreign key struct. */
  775. inline
  776. void
  777. dict_foreign_free(
  778. /*==============*/
  779. dict_foreign_t* foreign) /*!< in, own: foreign key struct */
  780. {
  781. mem_heap_free(foreign->heap);
  782. }
  783. /** The destructor will free all the foreign key constraints in the set
  784. by calling dict_foreign_free() on each of the foreign key constraints.
  785. This is used to free the allocated memory when a local set goes out
  786. of scope. */
  787. struct dict_foreign_set_free {
  788. dict_foreign_set_free(const dict_foreign_set& foreign_set)
  789. : m_foreign_set(foreign_set)
  790. {}
  791. ~dict_foreign_set_free()
  792. {
  793. std::for_each(m_foreign_set.begin(),
  794. m_foreign_set.end(),
  795. dict_foreign_free);
  796. }
  797. const dict_foreign_set& m_foreign_set;
  798. };
  799. /** The flags for ON_UPDATE and ON_DELETE can be ORed; the default is that
  800. a foreign key constraint is enforced, therefore RESTRICT just means no flag */
  801. /* @{ */
  802. #define DICT_FOREIGN_ON_DELETE_CASCADE 1 /*!< ON DELETE CASCADE */
  803. #define DICT_FOREIGN_ON_DELETE_SET_NULL 2 /*!< ON UPDATE SET NULL */
  804. #define DICT_FOREIGN_ON_UPDATE_CASCADE 4 /*!< ON DELETE CASCADE */
  805. #define DICT_FOREIGN_ON_UPDATE_SET_NULL 8 /*!< ON UPDATE SET NULL */
  806. #define DICT_FOREIGN_ON_DELETE_NO_ACTION 16 /*!< ON DELETE NO ACTION */
  807. #define DICT_FOREIGN_ON_UPDATE_NO_ACTION 32 /*!< ON UPDATE NO ACTION */
  808. /* @} */
  809. /* This flag is for sync SQL DDL and memcached DML.
  810. if table->memcached_sync_count == DICT_TABLE_IN_DDL means there's DDL running on
  811. the table, DML from memcached will be blocked. */
  812. #define DICT_TABLE_IN_DDL -1
  813. /** Data structure for a database table. Most fields will be
  814. initialized to 0, NULL or FALSE in dict_mem_table_create(). */
  815. struct dict_table_t{
  816. table_id_t id; /*!< id of the table */
  817. mem_heap_t* heap; /*!< memory heap */
  818. char* name; /*!< table name */
  819. const char* dir_path_of_temp_table;/*!< NULL or the directory path
  820. where a TEMPORARY table that was explicitly
  821. created by a user should be placed if
  822. innodb_file_per_table is defined in my.cnf;
  823. in Unix this is usually /tmp/..., in Windows
  824. temp\... */
  825. char* data_dir_path; /*!< NULL or the directory path
  826. specified by DATA DIRECTORY */
  827. unsigned space:32;
  828. /*!< space where the clustered index of the
  829. table is placed */
  830. unsigned flags:DICT_TF_BITS; /*!< DICT_TF_... */
  831. unsigned flags2:DICT_TF2_BITS; /*!< DICT_TF2_... */
  832. unsigned ibd_file_missing:1;
  833. /*!< TRUE if this is in a single-table
  834. tablespace and the .ibd file is missing; then
  835. we must return in ha_innodb.cc an error if the
  836. user tries to query such an orphaned table */
  837. unsigned cached:1;/*!< TRUE if the table object has been added
  838. to the dictionary cache */
  839. unsigned to_be_dropped:1;
  840. /*!< TRUE if the table is to be dropped, but
  841. not yet actually dropped (could in the bk
  842. drop list); It is turned on at the beginning
  843. of row_drop_table_for_mysql() and turned off
  844. just before we start to update system tables
  845. for the drop. It is protected by
  846. dict_operation_lock */
  847. unsigned n_def:10;/*!< number of columns defined so far */
  848. unsigned n_cols:10;/*!< number of columns */
  849. unsigned can_be_evicted:1;
  850. /*!< TRUE if it's not an InnoDB system table
  851. or a table that has no FK relationships */
  852. unsigned corrupted:1;
  853. /*!< TRUE if table is corrupted */
  854. unsigned drop_aborted:1;
  855. /*!< TRUE if some indexes should be dropped
  856. after ONLINE_INDEX_ABORTED
  857. or ONLINE_INDEX_ABORTED_DROPPED */
  858. dict_col_t* cols; /*!< array of column descriptions */
  859. const char* col_names;
  860. /*!< Column names packed in a character string
  861. "name1\0name2\0...nameN\0". Until
  862. the string contains n_cols, it will be
  863. allocated from a temporary heap. The final
  864. string will be allocated from table->heap. */
  865. bool is_system_db;
  866. /*!< True if the table belongs to a system
  867. database (mysql, information_schema or
  868. performance_schema) */
  869. #ifndef UNIV_HOTBACKUP
  870. hash_node_t name_hash; /*!< hash chain node */
  871. hash_node_t id_hash; /*!< hash chain node */
  872. UT_LIST_BASE_NODE_T(dict_index_t)
  873. indexes; /*!< list of indexes of the table */
  874. dict_foreign_set foreign_set;
  875. /*!< set of foreign key constraints
  876. in the table; these refer to columns
  877. in other tables */
  878. dict_foreign_set referenced_set;
  879. /*!< list of foreign key constraints
  880. which refer to this table */
  881. UT_LIST_NODE_T(dict_table_t)
  882. table_LRU; /*!< node of the LRU list of tables */
  883. unsigned fk_max_recusive_level:8;
  884. /*!< maximum recursive level we support when
  885. loading tables chained together with FK
  886. constraints. If exceeds this level, we will
  887. stop loading child table into memory along with
  888. its parent table */
  889. ulint n_foreign_key_checks_running;
  890. /*!< count of how many foreign key check
  891. operations are currently being performed
  892. on the table: we cannot drop the table while
  893. there are foreign key checks running on
  894. it! */
  895. trx_id_t def_trx_id;
  896. /*!< transaction id that last touched
  897. the table definition, either when
  898. loading the definition or CREATE
  899. TABLE, or ALTER TABLE (prepare,
  900. commit, and rollback phases) */
  901. trx_id_t query_cache_inv_trx_id;
  902. /*!< transactions whose trx id is
  903. smaller than this number are not
  904. allowed to store to the MySQL query
  905. cache or retrieve from it; when a trx
  906. with undo logs commits, it sets this
  907. to the value of the trx id counter for
  908. the tables it had an IX lock on */
  909. #ifdef UNIV_DEBUG
  910. /*----------------------*/
  911. ibool does_not_fit_in_memory;
  912. /*!< this field is used to specify in
  913. simulations tables which are so big
  914. that disk should be accessed: disk
  915. access is simulated by putting the
  916. thread to sleep for a while; NOTE that
  917. this flag is not stored to the data
  918. dictionary on disk, and the database
  919. will forget about value TRUE if it has
  920. to reload the table definition from
  921. disk */
  922. #endif /* UNIV_DEBUG */
  923. /*----------------------*/
  924. unsigned big_rows:1;
  925. /*!< flag: TRUE if the maximum length of
  926. a single row exceeds BIG_ROW_SIZE;
  927. initialized in dict_table_add_to_cache() */
  928. /** Statistics for query optimization */
  929. /* @{ */
  930. volatile os_once::state_t stats_latch_created;
  931. /*!< Creation state of 'stats_latch'. */
  932. rw_lock_t* stats_latch; /*!< this latch protects:
  933. dict_table_t::stat_initialized
  934. dict_table_t::stat_n_rows (*)
  935. dict_table_t::stat_clustered_index_size
  936. dict_table_t::stat_sum_of_other_index_sizes
  937. dict_table_t::stat_modified_counter (*)
  938. dict_table_t::indexes*::stat_n_diff_key_vals[]
  939. dict_table_t::indexes*::stat_index_size
  940. dict_table_t::indexes*::stat_n_leaf_pages
  941. (*) those are not always protected for
  942. performance reasons */
  943. unsigned stat_initialized:1; /*!< TRUE if statistics have
  944. been calculated the first time
  945. after database startup or table creation */
  946. #define DICT_TABLE_IN_USED -1
  947. lint memcached_sync_count;
  948. /*!< count of how many handles are opened
  949. to this table from memcached; DDL on the
  950. table is NOT allowed until this count
  951. goes to zero. If it's -1, means there's DDL
  952. on the table, DML from memcached will be
  953. blocked. */
  954. ib_time_t stats_last_recalc;
  955. /*!< Timestamp of last recalc of the stats */
  956. ib_uint32_t stat_persistent;
  957. /*!< The two bits below are set in the
  958. ::stat_persistent member and have the following
  959. meaning:
  960. 1. _ON=0, _OFF=0, no explicit persistent stats
  961. setting for this table, the value of the global
  962. srv_stats_persistent is used to determine
  963. whether the table has persistent stats enabled
  964. or not
  965. 2. _ON=0, _OFF=1, persistent stats are
  966. explicitly disabled for this table, regardless
  967. of the value of the global srv_stats_persistent
  968. 3. _ON=1, _OFF=0, persistent stats are
  969. explicitly enabled for this table, regardless
  970. of the value of the global srv_stats_persistent
  971. 4. _ON=1, _OFF=1, not allowed, we assert if
  972. this ever happens. */
  973. #define DICT_STATS_PERSISTENT_ON (1 << 1)
  974. #define DICT_STATS_PERSISTENT_OFF (1 << 2)
  975. ib_uint32_t stats_auto_recalc;
  976. /*!< The two bits below are set in the
  977. ::stats_auto_recalc member and have
  978. the following meaning:
  979. 1. _ON=0, _OFF=0, no explicit auto recalc
  980. setting for this table, the value of the global
  981. srv_stats_persistent_auto_recalc is used to
  982. determine whether the table has auto recalc
  983. enabled or not
  984. 2. _ON=0, _OFF=1, auto recalc is explicitly
  985. disabled for this table, regardless of the
  986. value of the global
  987. srv_stats_persistent_auto_recalc
  988. 3. _ON=1, _OFF=0, auto recalc is explicitly
  989. enabled for this table, regardless of the
  990. value of the global
  991. srv_stats_persistent_auto_recalc
  992. 4. _ON=1, _OFF=1, not allowed, we assert if
  993. this ever happens. */
  994. #define DICT_STATS_AUTO_RECALC_ON (1 << 1)
  995. #define DICT_STATS_AUTO_RECALC_OFF (1 << 2)
  996. ulint stats_sample_pages;
  997. /*!< the number of pages to sample for this
  998. table during persistent stats estimation;
  999. if this is 0, then the value of the global
  1000. srv_stats_persistent_sample_pages will be
  1001. used instead. */
  1002. ib_uint64_t stat_n_rows;
  1003. /*!< approximate number of rows in the table;
  1004. we periodically calculate new estimates */
  1005. ulint stat_clustered_index_size;
  1006. /*!< approximate clustered index size in
  1007. database pages */
  1008. ulint stat_sum_of_other_index_sizes;
  1009. /*!< other indexes in database pages */
  1010. ib_uint64_t stat_modified_counter;
  1011. /*!< when a row is inserted, updated,
  1012. or deleted,
  1013. we add 1 to this number; we calculate new
  1014. estimates for the stat_... values for the
  1015. table and the indexes when about 1 / 16 of
  1016. table has been modified;
  1017. also when the estimate operation is
  1018. called for MySQL SHOW TABLE STATUS; the
  1019. counter is reset to zero at statistics
  1020. calculation; this counter is not protected by
  1021. any latch, because this is only used for
  1022. heuristics */
  1023. #define BG_STAT_NONE 0
  1024. #define BG_STAT_IN_PROGRESS (1 << 0)
  1025. /*!< BG_STAT_IN_PROGRESS is set in
  1026. stats_bg_flag when the background
  1027. stats code is working on this table. The DROP
  1028. TABLE code waits for this to be cleared
  1029. before proceeding. */
  1030. #define BG_STAT_SHOULD_QUIT (1 << 1)
  1031. /*!< BG_STAT_SHOULD_QUIT is set in
  1032. stats_bg_flag when DROP TABLE starts
  1033. waiting on BG_STAT_IN_PROGRESS to be cleared,
  1034. the background stats thread will detect this
  1035. and will eventually quit sooner */
  1036. byte stats_bg_flag;
  1037. /*!< see BG_STAT_* above.
  1038. Writes are covered by dict_sys->mutex.
  1039. Dirty reads are possible. */
  1040. bool stats_error_printed;
  1041. /*!< Has persistent stats error beein
  1042. already printed for this table ? */
  1043. /* @} */
  1044. /*----------------------*/
  1045. /**!< The following fields are used by the
  1046. AUTOINC code. The actual collection of
  1047. tables locked during AUTOINC read/write is
  1048. kept in trx_t. In order to quickly determine
  1049. whether a transaction has locked the AUTOINC
  1050. lock we keep a pointer to the transaction
  1051. here in the autoinc_trx variable. This is to
  1052. avoid acquiring the lock_sys_t::mutex and
  1053. scanning the vector in trx_t.
  1054. When an AUTOINC lock has to wait, the
  1055. corresponding lock instance is created on
  1056. the trx lock heap rather than use the
  1057. pre-allocated instance in autoinc_lock below.*/
  1058. /* @{ */
  1059. lock_t* autoinc_lock;
  1060. /*!< a buffer for an AUTOINC lock
  1061. for this table: we allocate the memory here
  1062. so that individual transactions can get it
  1063. and release it without a need to allocate
  1064. space from the lock heap of the trx:
  1065. otherwise the lock heap would grow rapidly
  1066. if we do a large insert from a select */
  1067. ib_mutex_t* autoinc_mutex;
  1068. /*!< mutex protecting the autoincrement
  1069. counter */
  1070. /** Creation state of autoinc_mutex member */
  1071. volatile os_once::state_t
  1072. autoinc_mutex_created;
  1073. ib_uint64_t autoinc;/*!< autoinc counter value to give to the
  1074. next inserted row */
  1075. ulong n_waiting_or_granted_auto_inc_locks;
  1076. /*!< This counter is used to track the number
  1077. of granted and pending autoinc locks on this
  1078. table. This value is set after acquiring the
  1079. lock_sys_t::mutex but we peek the contents to
  1080. determine whether other transactions have
  1081. acquired the AUTOINC lock or not. Of course
  1082. only one transaction can be granted the
  1083. lock but there can be multiple waiters. */
  1084. const trx_t* autoinc_trx;
  1085. /*!< The transaction that currently holds the
  1086. the AUTOINC lock on this table.
  1087. Protected by lock_sys->mutex. */
  1088. fts_t* fts; /* FTS specific state variables */
  1089. /* @} */
  1090. /*----------------------*/
  1091. ib_quiesce_t quiesce;/*!< Quiescing states, protected by the
  1092. dict_index_t::lock. ie. we can only change
  1093. the state if we acquire all the latches
  1094. (dict_index_t::lock) in X mode of this table's
  1095. indexes. */
  1096. /*----------------------*/
  1097. ulint n_rec_locks;
  1098. /*!< Count of the number of record locks on
  1099. this table. We use this to determine whether
  1100. we can evict the table from the dictionary
  1101. cache. It is protected by lock_sys->mutex. */
  1102. ulint n_ref_count;
  1103. /*!< count of how many handles are opened
  1104. to this table; dropping of the table is
  1105. NOT allowed until this count gets to zero;
  1106. MySQL does NOT itself check the number of
  1107. open handles at drop */
  1108. UT_LIST_BASE_NODE_T(lock_t)
  1109. locks; /*!< list of locks on the table; protected
  1110. by lock_sys->mutex */
  1111. #endif /* !UNIV_HOTBACKUP */
  1112. #ifdef UNIV_DEBUG
  1113. ulint magic_n;/*!< magic number */
  1114. /** Value of dict_table_t::magic_n */
  1115. # define DICT_TABLE_MAGIC_N 76333786
  1116. #endif /* UNIV_DEBUG */
  1117. };
  1118. /** A function object to add the foreign key constraint to the referenced set
  1119. of the referenced table, if it exists in the dictionary cache. */
  1120. struct dict_foreign_add_to_referenced_table {
  1121. void operator()(dict_foreign_t* foreign) const
  1122. {
  1123. if (dict_table_t* table = foreign->referenced_table) {
  1124. std::pair<dict_foreign_set::iterator, bool> ret
  1125. = table->referenced_set.insert(foreign);
  1126. ut_a(ret.second);
  1127. }
  1128. }
  1129. };
  1130. /** Destroy the autoinc latch of the given table.
  1131. This function is only called from either single threaded environment
  1132. or from a thread that has not shared the table object with other threads.
  1133. @param[in,out] table table whose stats latch to destroy */
  1134. inline
  1135. void
  1136. dict_table_autoinc_destroy(
  1137. dict_table_t* table)
  1138. {
  1139. if (table->autoinc_mutex_created == os_once::DONE
  1140. && table->autoinc_mutex != NULL) {
  1141. mutex_free(table->autoinc_mutex);
  1142. delete table->autoinc_mutex;
  1143. }
  1144. }
  1145. /** Allocate and init the autoinc latch of a given table.
  1146. This function must not be called concurrently on the same table object.
  1147. @param[in,out] table_void table whose autoinc latch to create */
  1148. void
  1149. dict_table_autoinc_alloc(
  1150. void* table_void);
  1151. /** Allocate and init the zip_pad_mutex of a given index.
  1152. This function must not be called concurrently on the same index object.
  1153. @param[in,out] index_void index whose zip_pad_mutex to create */
  1154. void
  1155. dict_index_zip_pad_alloc(
  1156. void* index_void);
  1157. /** Request for lazy creation of the autoinc latch of a given table.
  1158. This function is only called from either single threaded environment
  1159. or from a thread that has not shared the table object with other threads.
  1160. @param[in,out] table table whose autoinc latch is to be created. */
  1161. inline
  1162. void
  1163. dict_table_autoinc_create_lazy(
  1164. dict_table_t* table)
  1165. {
  1166. #ifdef HAVE_ATOMIC_BUILTINS
  1167. table->autoinc_mutex = NULL;
  1168. table->autoinc_mutex_created = os_once::NEVER_DONE;
  1169. #else /* HAVE_ATOMIC_BUILTINS */
  1170. dict_table_autoinc_alloc(table);
  1171. table->autoinc_mutex_created = os_once::DONE;
  1172. #endif /* HAVE_ATOMIC_BUILTINS */
  1173. }
  1174. /** Request a lazy creation of dict_index_t::zip_pad::mutex.
  1175. This function is only called from either single threaded environment
  1176. or from a thread that has not shared the table object with other threads.
  1177. @param[in,out] index index whose zip_pad mutex is to be created */
  1178. inline
  1179. void
  1180. dict_index_zip_pad_mutex_create_lazy(
  1181. dict_index_t* index)
  1182. {
  1183. #ifdef HAVE_ATOMIC_BUILTINS
  1184. index->zip_pad.mutex = NULL;
  1185. index->zip_pad.mutex_created = os_once::NEVER_DONE;
  1186. #else /* HAVE_ATOMIC_BUILTINS */
  1187. dict_index_zip_pad_alloc(index);
  1188. index->zip_pad.mutex_created = os_once::DONE;
  1189. #endif /* HAVE_ATOMIC_BUILTINS */
  1190. }
  1191. /** Destroy the zip_pad_mutex of the given index.
  1192. This function is only called from either single threaded environment
  1193. or from a thread that has not shared the table object with other threads.
  1194. @param[in,out] table table whose stats latch to destroy */
  1195. inline
  1196. void
  1197. dict_index_zip_pad_mutex_destroy(
  1198. dict_index_t* index)
  1199. {
  1200. if (index->zip_pad.mutex_created == os_once::DONE
  1201. && index->zip_pad.mutex != NULL) {
  1202. os_fast_mutex_free(index->zip_pad.mutex);
  1203. delete index->zip_pad.mutex;
  1204. }
  1205. }
  1206. /** Release the zip_pad_mutex of a given index.
  1207. @param[in,out] index index whose zip_pad_mutex is to be released */
  1208. inline
  1209. void
  1210. dict_index_zip_pad_unlock(
  1211. dict_index_t* index)
  1212. {
  1213. os_fast_mutex_unlock(index->zip_pad.mutex);
  1214. }
  1215. #ifdef UNIV_DEBUG
  1216. /** Check if the current thread owns the autoinc_mutex of a given table.
  1217. @param[in] table the autoinc_mutex belongs to this table
  1218. @return true, if the current thread owns the autoinc_mutex, false otherwise.*/
  1219. inline
  1220. bool
  1221. dict_table_autoinc_own(
  1222. const dict_table_t* table)
  1223. {
  1224. return(mutex_own(table->autoinc_mutex));
  1225. }
  1226. #endif /* UNIV_DEBUG */
  1227. #ifndef UNIV_NONINL
  1228. #include "dict0mem.ic"
  1229. #endif
  1230. #endif