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