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