1 /*****************************************************************************
3 Copyright (c) 1996, 2013, Oracle and/or its affiliates. All Rights Reserved.
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17 *****************************************************************************/
19 /**************************************************//**
20 @file include/dict0mem.h
21 Data dictionary memory object creation
23 Created 1/8/1996 Heikki Tuuri
24 *******************************************************/
30 #include "dict0types.h"
31 #include "data0type.h"
32 #include "data0data.h"
34 #include "rem0types.h"
35 #include "btr0types.h"
41 #include "lock0types.h"
42 #include "hash0hash.h"
43 #include "que0types.h"
45 /* Type flags of an index: OR'ing of the flags is allowed to define a
46 combination of types */
47 #define DICT_CLUSTERED 1 /* clustered index */
48 #define DICT_UNIQUE 2 /* unique index */
49 #define DICT_UNIVERSAL 4 /* index which can contain records from any
51 #define DICT_IBUF 8 /* insert buffer tree */
53 /* Types for a table object */
54 #define DICT_TABLE_ORDINARY 1
55 #if 0 /* not implemented */
56 #define DICT_TABLE_CLUSTER_MEMBER 2
57 #define DICT_TABLE_CLUSTER 3 /* this means that the table is
58 really a cluster definition */
62 #define DICT_TF_COMPACT 1 /* compact page format */
64 /**************************************************************************
65 Creates a table memory object. */
68 dict_mem_table_create(
69 /*==================*/
70 /* out, own: table object */
71 const char* name
, /* in: table name */
72 ulint space
, /* in: space where the clustered index
73 of the table is placed; this parameter
74 is ignored if the table is made
75 a member of a cluster */
76 ulint n_cols
, /* in: number of columns */
77 ulint flags
); /* in: table flags */
78 /********************************************************************
79 Free a table memory object. */
84 dict_table_t
* table
); /* in: table */
85 /**************************************************************************
86 Adds a column definition to a table. */
89 dict_mem_table_add_col(
90 /*===================*/
91 dict_table_t
* table
, /* in: table */
92 mem_heap_t
* heap
, /* in: temporary memory heap, or NULL */
93 const char* name
, /* in: column name, or NULL */
94 ulint mtype
, /* in: main datatype */
95 ulint prtype
, /* in: precise type */
96 ulint len
); /* in: precision */
97 /**************************************************************************
98 Creates an index memory object. */
101 dict_mem_index_create(
102 /*==================*/
103 /* out, own: index object */
104 const char* table_name
, /* in: table name */
105 const char* index_name
, /* in: index name */
106 ulint space
, /* in: space where the index tree is
107 placed, ignored if the index is of
108 the clustered type */
109 ulint type
, /* in: DICT_UNIQUE,
110 DICT_CLUSTERED, ... ORed */
111 ulint n_fields
); /* in: number of fields */
112 /**************************************************************************
113 Adds a field definition to an index. NOTE: does not take a copy
114 of the column name if the field is a column. The memory occupied
115 by the column name may be released only after publishing the index. */
118 dict_mem_index_add_field(
119 /*=====================*/
120 dict_index_t
* index
, /* in: index */
121 const char* name
, /* in: column name */
122 ulint prefix_len
); /* in: 0 or the column prefix length
123 in a MySQL index like
124 INDEX (textcol(25)) */
125 /**************************************************************************
126 Frees an index memory object. */
131 dict_index_t
* index
); /* in: index */
132 /**************************************************************************
133 Creates and initializes a foreign constraint memory object. */
136 dict_mem_foreign_create(void);
137 /*=========================*/
138 /* out, own: foreign constraint struct */
140 /* Data structure for a column in a table */
141 struct dict_col_struct
{
142 /*----------------------*/
143 /* The following are copied from dtype_t,
144 so that all bit-fields can be packed tightly. */
145 unsigned mtype
:8; /* main data type */
146 unsigned prtype
:24; /* precise type; MySQL data
147 type, charset code, flags to
148 indicate nullability,
149 signedness, whether this is a
150 binary string, whether this is
151 a true VARCHAR where MySQL
152 uses 2 bytes to store the length */
154 /* the remaining fields do not affect alphabetical ordering: */
156 unsigned len
:16; /* length; for MySQL data this
157 is field->pack_length(),
158 except that for a >= 5.0.3
159 type true VARCHAR this is the
160 maximum byte length of the
161 string data (in addition to
162 the string, MySQL uses 1 or 2
163 bytes to store the string length) */
165 unsigned mbminlen
:2; /* minimum length of a
166 character, in bytes */
167 unsigned mbmaxlen
:3; /* maximum length of a
168 character, in bytes */
169 /*----------------------*/
170 /* End of definitions copied from dtype_t */
172 unsigned ind
:10; /* table column position
174 unsigned ord_part
:1; /* nonzero if this column
175 appears in the ordering fields
179 /* DICT_MAX_INDEX_COL_LEN is measured in bytes and is the maximum
180 indexed column length (or indexed prefix length). It is set to 3*256,
181 so that one can create a column prefix index on 256 characters of a
182 TEXT or VARCHAR column also in the UTF-8 charset. In that charset,
183 a character may take at most 3 bytes.
184 This constant MUST NOT BE CHANGED, or the compatibility of InnoDB data
185 files would be at risk! */
187 #define DICT_MAX_INDEX_COL_LEN 768
189 /* Data structure for a field in an index */
190 struct dict_field_struct
{
191 dict_col_t
* col
; /* pointer to the table column */
192 const char* name
; /* name of the column */
193 unsigned prefix_len
:10; /* 0 or the length of the column
194 prefix in bytes in a MySQL index of
195 type, e.g., INDEX (textcol(25));
197 DICT_MAX_INDEX_COL_LEN; NOTE that
198 in the UTF-8 charset, MySQL sets this
199 to 3 * the prefix len in UTF-8 chars */
200 unsigned fixed_len
:10; /* 0 or the fixed length of the
201 column if smaller than
202 DICT_MAX_INDEX_COL_LEN */
205 /* Data structure for an index */
206 struct dict_index_struct
{
207 dulint id
; /* id of the index */
208 mem_heap_t
* heap
; /* memory heap */
209 ulint type
; /* index type */
210 const char* name
; /* index name */
211 const char* table_name
; /* table name */
212 dict_table_t
* table
; /* back pointer to table */
214 /* space where the index tree is placed */
215 unsigned page
:32;/* index tree root page number */
216 #define MAX_KEY_LENGTH_BITS 12
217 unsigned trx_id_offset
:MAX_KEY_LENGTH_BITS
;
218 /* position of the trx id column
219 in a clustered index record, if the fields
220 before it are known to be of a fixed size,
222 #if (1<<MAX_KEY_LENGTH_BITS) < MAX_KEY_LENGTH
223 # error (1<<MAX_KEY_LENGTH_BITS) < MAX_KEY_LENGTH
225 unsigned n_user_defined_cols
:10;
226 /* number of columns the user defined to
227 be in the index: in the internal
228 representation we add more columns */
229 unsigned n_uniq
:10;/* number of fields from the beginning
230 which are enough to determine an index
232 unsigned n_def
:10;/* number of fields defined so far */
233 unsigned n_fields
:10;/* number of fields in the index */
234 unsigned n_nullable
:10;/* number of nullable fields */
235 unsigned cached
:1;/* TRUE if the index object is in the
237 dict_field_t
* fields
; /* array of field descriptions */
238 UT_LIST_NODE_T(dict_index_t
)
239 indexes
;/* list of indexes of the table */
240 btr_search_t
* search_info
; /* info used in optimistic searches */
241 /*----------------------*/
242 ib_longlong
* stat_n_diff_key_vals
;
243 /* approximate number of different key values
244 for this index, for each n-column prefix
245 where n <= dict_get_n_unique(index); we
246 periodically calculate new estimates */
247 ib_longlong
* stat_n_non_null_key_vals
;
248 /* approximate number of non-null key values
249 for this index, for each column where
250 n < dict_get_n_unique(index); This
251 is used when innodb_stats_method is
253 ulint stat_index_size
;
254 /* approximate index size in database pages */
255 ulint stat_n_leaf_pages
;
256 /* approximate number of leaf pages in the
258 rw_lock_t lock
; /* read-write lock protecting the upper levels
261 ulint magic_n
;/* magic number */
262 # define DICT_INDEX_MAGIC_N 76789786
266 /* Data structure for a foreign key constraint; an example:
267 FOREIGN KEY (A, B) REFERENCES TABLE2 (C, D) */
269 struct dict_foreign_struct
{
270 mem_heap_t
* heap
; /* this object is allocated from
272 char* id
; /* id of the constraint as a
273 null-terminated string */
274 unsigned n_fields
:10; /* number of indexes' first fields
275 for which the the foreign key
276 constraint is defined: we allow the
277 indexes to contain more fields than
278 mentioned in the constraint, as long
279 as the first fields are as mentioned */
280 unsigned type
:6; /* 0 or DICT_FOREIGN_ON_DELETE_CASCADE
281 or DICT_FOREIGN_ON_DELETE_SET_NULL */
282 char* foreign_table_name
;/* foreign table name */
283 dict_table_t
* foreign_table
; /* table where the foreign key is */
284 const char** foreign_col_names
;/* names of the columns in the
286 char* referenced_table_name
;/* referenced table name */
287 dict_table_t
* referenced_table
;/* table where the referenced key
289 const char** referenced_col_names
;/* names of the referenced
290 columns in the referenced table */
291 dict_index_t
* foreign_index
; /* foreign index; we require that
292 both tables contain explicitly defined
293 indexes for the constraint: InnoDB
294 does not generate new indexes
296 dict_index_t
* referenced_index
;/* referenced index */
297 UT_LIST_NODE_T(dict_foreign_t
)
298 foreign_list
; /* list node for foreign keys of the
300 UT_LIST_NODE_T(dict_foreign_t
)
301 referenced_list
;/* list node for referenced keys of the
305 /* The flags for ON_UPDATE and ON_DELETE can be ORed; the default is that
306 a foreign key constraint is enforced, therefore RESTRICT just means no flag */
307 #define DICT_FOREIGN_ON_DELETE_CASCADE 1
308 #define DICT_FOREIGN_ON_DELETE_SET_NULL 2
309 #define DICT_FOREIGN_ON_UPDATE_CASCADE 4
310 #define DICT_FOREIGN_ON_UPDATE_SET_NULL 8
311 #define DICT_FOREIGN_ON_DELETE_NO_ACTION 16
312 #define DICT_FOREIGN_ON_UPDATE_NO_ACTION 32
314 /** Tables could be chained together with Foreign key constraint. When
315 first load the parent table, we would load all of its descedents.
316 This could result in rescursive calls and out of stack error eventually.
317 DICT_FK_MAX_RECURSIVE_LOAD defines the maximum number of recursive loads,
318 when exceeded, the child table will not be loaded. It will be loaded when
319 the foreign constraint check needs to be run. */
320 #define DICT_FK_MAX_RECURSIVE_LOAD 20
322 /** Similarly, when tables are chained together with foreign key constraints
323 with on cascading delete/update clause, delete from parent table could
324 result in recursive cascading calls. This defines the maximum number of
325 such cascading deletes/updates allowed. When exceeded, the delete from
326 parent table will fail, and user has to drop excessive foreign constraint
328 #define FK_MAX_CASCADE_DEL 300
330 /* Data structure for a database table */
331 struct dict_table_struct
{
332 dulint id
; /* id of the table */
333 mem_heap_t
* heap
; /* memory heap */
334 const char* name
; /* table name */
335 const char* dir_path_of_temp_table
;/* NULL or the directory path
336 where a TEMPORARY table that was explicitly
337 created by a user should be placed if
338 innodb_file_per_table is defined in my.cnf;
339 in Unix this is usually /tmp/..., in Windows
342 /* space where the clustered index of the
344 unsigned ibd_file_missing
:1;
345 /* TRUE if this is in a single-table
346 tablespace and the .ibd file is missing; then
347 we must return in ha_innodb.cc an error if the
348 user tries to query such an orphaned table */
349 unsigned tablespace_discarded
:1;
350 /* this flag is set TRUE when the user
351 calls DISCARD TABLESPACE on this
352 table, and reset to FALSE in IMPORT
354 unsigned cached
:1;/* TRUE if the table object has been added
355 to the dictionary cache */
356 unsigned flags
:8;/* DICT_TF_COMPACT, ... */
357 unsigned n_def
:10;/* number of columns defined so far */
358 unsigned n_cols
:10;/* number of columns */
359 dict_col_t
* cols
; /* array of column descriptions */
360 const char* col_names
;
361 /* Column names packed in a character string
362 "name1\0name2\0...nameN\0". Until
363 the string contains n_cols, it will be
364 allocated from a temporary heap. The final
365 string will be allocated from table->heap. */
366 hash_node_t name_hash
; /* hash chain node */
367 hash_node_t id_hash
; /* hash chain node */
368 UT_LIST_BASE_NODE_T(dict_index_t
)
369 indexes
; /* list of indexes of the table */
370 UT_LIST_BASE_NODE_T(dict_foreign_t
)
371 foreign_list
;/* list of foreign key constraints
372 in the table; these refer to columns
374 UT_LIST_BASE_NODE_T(dict_foreign_t
)
375 referenced_list
;/* list of foreign key constraints
376 which refer to this table */
377 UT_LIST_NODE_T(dict_table_t
)
378 table_LRU
; /* node of the LRU list of tables */
379 ulint n_mysql_handles_opened
;
380 /* count of how many handles MySQL has opened
381 to this table; dropping of the table is
382 NOT allowed until this count gets to zero;
383 MySQL does NOT itself check the number of
384 open handles at drop */
385 unsigned fk_max_recusive_level
:8;
386 /*!< maximum recursive level we support when
387 loading tables chained together with FK
388 constraints. If exceeds this level, we will
389 stop loading child table into memory along with
391 ulint n_foreign_key_checks_running
;
392 /* count of how many foreign key check
393 operations are currently being performed
394 on the table: we cannot drop the table while
395 there are foreign key checks running on
397 lock_t
* auto_inc_lock
;/* a buffer for an auto-inc lock
398 for this table: we allocate the memory here
399 so that individual transactions can get it
400 and release it without a need to allocate
401 space from the lock heap of the trx:
402 otherwise the lock heap would grow rapidly
403 if we do a large insert from a select */
404 dulint query_cache_inv_trx_id
;
405 /* transactions whose trx id < than this
406 number are not allowed to store to the MySQL
407 query cache or retrieve from it; when a trx
408 with undo logs commits, it sets this to the
409 value of the trx id counter for the tables it
411 UT_LIST_BASE_NODE_T(lock_t
)
412 locks
; /* list of locks on the table */
414 /*----------------------*/
415 ibool does_not_fit_in_memory
;
416 /* this field is used to specify in simulations
417 tables which are so big that disk should be
418 accessed: disk access is simulated by
419 putting the thread to sleep for a while;
420 NOTE that this flag is not stored to the data
421 dictionary on disk, and the database will
422 forget about value TRUE if it has to reload
423 the table definition from disk */
424 #endif /* UNIV_DEBUG */
425 /*----------------------*/
427 /* flag: TRUE if the maximum length of
428 a single row exceeds BIG_ROW_SIZE;
429 initialized in dict_table_add_to_cache() */
430 unsigned stat_initialized
:1; /* TRUE if statistics have
431 been calculated the first time
432 after database startup or table creation */
433 ib_longlong stat_n_rows
;
434 /* approximate number of rows in the table;
435 we periodically calculate new estimates */
436 ulint stat_clustered_index_size
;
437 /* approximate clustered index size in
439 ulint stat_sum_of_other_index_sizes
;
440 /* other indexes in database pages */
441 ulint stat_modified_counter
;
442 /* when a row is inserted, updated, or deleted,
443 we add 1 to this number; we calculate new
444 estimates for the stat_... values for the
445 table and the indexes at an interval of 2 GB
446 or when about 1 / 16 of table has been
447 modified; also when the estimate operation is
448 called for MySQL SHOW TABLE STATUS; the
449 counter is reset to zero at statistics
450 calculation; this counter is not protected by
451 any latch, because this is only used for
453 /*----------------------*/
454 mutex_t autoinc_mutex
;
455 /* mutex protecting the autoincrement
457 ib_ulonglong autoinc
;/* autoinc counter value to give to the
459 ulong n_waiting_or_granted_auto_inc_locks
;
460 /* This counter is used to track the number
461 of granted and pending autoinc locks on this
462 table. This value is set after acquiring the
463 kernel mutex but we peek the contents to
464 determine whether other transactions have
465 acquired the AUTOINC lock or not. Of course
466 only one transaction can be granted the
467 lock but there can be multiple waiters. */
468 /*----------------------*/
471 ulint magic_n
;/* magic number */
472 # define DICT_TABLE_MAGIC_N 76333786
473 #endif /* UNIV_DEBUG */
477 #include "dict0mem.ic"