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.

1342 lines
48 KiB

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