-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
table.h
4516 lines (3889 loc) · 156 KB
/
table.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef TABLE_INCLUDED
#define TABLE_INCLUDED
/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <string>
#include "field_types.h"
#include "lex_string.h"
#include "libbinlogevents/include/table_id.h" // Table_id
#include "m_ctype.h"
#include "map_helpers.h"
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "my_table_map.h"
#include "mysql/components/services/bits/mysql_mutex_bits.h"
#include "mysql/components/services/bits/psi_table_bits.h"
#include "sql/dd/types/foreign_key.h" // dd::Foreign_key::enum_rule
#include "sql/enum_query_type.h" // enum_query_type
#include "sql/key.h"
#include "sql/key_spec.h"
#include "sql/mdl.h" // MDL_wait_for_subgraph
#include "sql/mem_root_array.h"
#include "sql/opt_costmodel.h" // Cost_model_table
#include "sql/partition_info.h"
#include "sql/record_buffer.h" // Record_buffer
#include "sql/sql_bitmap.h" // Bitmap
#include "sql/sql_const.h"
#include "sql/sql_list.h"
#include "sql/sql_plist.h"
#include "sql/sql_plugin_ref.h"
#include "sql/sql_sort.h" // Sort_result
#include "thr_lock.h"
#include "typelib.h"
class Field;
class Field_longlong;
namespace histograms {
class Histogram;
}
class ACL_internal_schema_access;
class ACL_internal_table_access;
class COND_EQUAL;
class Field_json;
/* Structs that defines the TABLE */
class File_parser;
class Value_generator;
class GRANT_TABLE;
class Handler_share;
class Index_hint;
class Item;
class Item_field;
class Json_diff_vector;
class Json_seekable_path;
class Json_wrapper;
class Opt_hints_qb;
class Opt_hints_table;
class Query_result_union;
class Query_block;
class Query_expression;
class Security_context;
class SortingIterator;
class String;
class THD;
class Table_cache_element;
class Table_ref;
class Table_trigger_dispatcher;
class Temp_table_param;
class handler;
class partition_info;
enum enum_stats_auto_recalc : int;
enum Value_generator_source : short;
enum row_type : int;
struct AccessPath;
struct HA_CREATE_INFO;
struct LEX;
struct NESTED_JOIN;
struct Partial_update_info;
struct TABLE;
struct TABLE_SHARE;
struct handlerton;
struct Name_resolution_context;
using plan_idx = int;
namespace dd {
class Table;
class View;
enum class enum_table_type;
} // namespace dd
class Common_table_expr;
class Sql_table_check_constraint;
using Sql_table_check_constraint_list =
Mem_root_array<Sql_table_check_constraint>;
class Sql_check_constraint_share;
using Sql_check_constraint_share_list =
Mem_root_array<Sql_check_constraint_share>;
typedef Mem_root_array_YY<LEX_CSTRING> Create_col_name_list;
typedef int64 query_id_t;
enum class enum_json_diff_operation;
bool assert_ref_count_is_locked(const TABLE_SHARE *);
bool assert_invalid_dict_is_locked(const TABLE *);
bool assert_invalid_stats_is_locked(const TABLE *);
#define store_record(A, B) \
memcpy((A)->B, (A)->record[0], (size_t)(A)->s->reclength)
#define restore_record(A, B) \
memcpy((A)->record[0], (A)->B, (size_t)(A)->s->reclength)
#define cmp_record(A, B) \
memcmp((A)->record[0], (A)->B, (size_t)(A)->s->reclength)
#define tmp_file_prefix "#sql" /**< Prefix for tmp tables */
#define tmp_file_prefix_length 4
#define TMP_TABLE_KEY_EXTRA 8
#define PLACEHOLDER_TABLE_ROW_ESTIMATE 2
/**
Enumerate possible types of a table from re-execution
standpoint.
Table_ref class has a member of this type.
At prepared statement prepare, this member is assigned a value
as of the current state of the database. Before (re-)execution
of a prepared statement, we check that the value recorded at
prepare matches the type of the object we obtained from the
table definition cache.
@sa check_and_update_table_version()
@sa Execute_observer
@sa Prepared_statement::reprepare()
*/
enum enum_table_ref_type {
/** Initial value set by the parser */
TABLE_REF_NULL = 0,
TABLE_REF_VIEW,
TABLE_REF_BASE_TABLE,
TABLE_REF_I_S_TABLE,
TABLE_REF_TMP_TABLE
};
/**
Enumerate possible status of a identifier name while determining
its validity
*/
enum class Ident_name_check { OK, WRONG, TOO_LONG };
/*************************************************************************/
/**
Object_creation_ctx -- interface for creation context of database objects
(views, stored routines, events, triggers). Creation context -- is a set
of attributes, that should be fixed at the creation time and then be used
each time the object is parsed or executed.
*/
class Object_creation_ctx {
public:
Object_creation_ctx *set_n_backup(THD *thd);
void restore_env(THD *thd, Object_creation_ctx *backup_ctx);
protected:
Object_creation_ctx() = default;
virtual Object_creation_ctx *create_backup_ctx(THD *thd) const = 0;
virtual void delete_backup_ctx() = 0;
virtual void change_env(THD *thd) const = 0;
public:
virtual ~Object_creation_ctx() = default;
};
/*************************************************************************/
/**
Default_object_creation_ctx -- default implementation of
Object_creation_ctx.
*/
class Default_object_creation_ctx : public Object_creation_ctx {
public:
const CHARSET_INFO *get_client_cs() { return m_client_cs; }
const CHARSET_INFO *get_connection_cl() { return m_connection_cl; }
protected:
Default_object_creation_ctx(THD *thd);
Default_object_creation_ctx(const CHARSET_INFO *client_cs,
const CHARSET_INFO *connection_cl);
protected:
Object_creation_ctx *create_backup_ctx(THD *thd) const override;
void delete_backup_ctx() override;
void change_env(THD *thd) const override;
protected:
/**
client_cs stores the value of character_set_client session variable.
The only character set attribute is used.
Client character set is included into query context, because we save
query in the original character set, which is client character set. So,
in order to parse the query properly we have to switch client character
set on parsing.
*/
const CHARSET_INFO *m_client_cs;
/**
connection_cl stores the value of collation_connection session
variable. Both character set and collation attributes are used.
Connection collation is included into query context, because it defines
the character set and collation of text literals in internal
representation of query (item-objects).
*/
const CHARSET_INFO *m_connection_cl;
};
/**
View_creation_ctx -- creation context of view objects.
*/
class View_creation_ctx : public Default_object_creation_ctx {
public:
static View_creation_ctx *create(THD *thd);
static View_creation_ctx *create(THD *thd, Table_ref *view);
private:
View_creation_ctx(THD *thd) : Default_object_creation_ctx(thd) {}
};
/*************************************************************************/
/** Order clause list element */
class Item_rollup_group_item;
struct ORDER {
/// @returns true if item pointer is same as original
bool is_item_original() const { return item[0] == item_initial; }
ORDER *next{nullptr};
/**
The initial ordering expression. Usually substituted during resolving
and must not be used during optimization and execution.
*/
Item *item_initial{nullptr}; /* Storage for initial item */
public:
/**
Points at the item in the select fields. Note that this means that
after resolving, it points into a slice (see JOIN::ref_items),
even though the item is not of type Item_ref!
*/
Item **item{&item_initial};
Item_rollup_group_item *rollup_item{nullptr};
enum_order direction{
ORDER_NOT_RELEVANT}; /* Requested direction of ordering */
bool in_field_list{false}; /* true if in select field list */
/**
Tells whether this ORDER element was referenced with an alias or with an
expression, in the query:
SELECT a AS foo GROUP BY foo: true.
SELECT a AS foo GROUP BY a: false.
*/
bool used_alias{false};
/**
When GROUP BY is implemented with a temporary table (i.e. the table takes
care to store only unique group rows, table->group != nullptr), each GROUP
BY expression is stored in a column of the table, which is
'field_in_tmp_table'.
Such field may point into table->record[0] (if we only use it to get its
value from a tmp table's row), or into 'buff' (if we use it to do index
lookup into the tmp table).
*/
Field *field_in_tmp_table{nullptr};
char *buff{nullptr}; /* If tmp-table group */
table_map used{0}, depend_map{0};
bool is_explicit{false}; /* Whether ASC/DESC is explicitly specified */
};
/**
State information for internal tables grants.
This structure is part of the Table_ref, and is updated
during the ACL check process.
@sa GRANT_INFO
*/
struct GRANT_INTERNAL_INFO {
/** True if the internal lookup by schema name was done. */
bool m_schema_lookup_done{false};
/** Cached internal schema access. */
const ACL_internal_schema_access *m_schema_access{nullptr};
/** True if the internal lookup by table name was done. */
bool m_table_lookup_done{false};
/** Cached internal table access. */
const ACL_internal_table_access *m_table_access{nullptr};
};
/**
@brief The current state of the privilege checking process for the current
user, SQL statement and SQL object.
@details The privilege checking process is divided into phases depending on
the level of the privilege to be checked and the type of object to be
accessed. Due to the mentioned scattering of privilege checking
functionality, it is necessary to keep track of the state of the process.
A GRANT_INFO also serves as a cache of the privilege hash tables. Relevant
members are grant_table and version.
*/
struct GRANT_INFO {
GRANT_INFO();
/**
@brief A copy of the privilege information regarding the current host,
database, object and user.
@details The version of this copy is found in GRANT_INFO::version.
*/
GRANT_TABLE *grant_table{nullptr};
/**
@brief Used for cache invalidation when caching privilege information.
@details The privilege information is stored on disk, with dedicated
caches residing in memory: table-level and column-level privileges,
respectively, have their own dedicated caches.
The GRANT_INFO works as a level 1 cache with this member updated to the
current value of the global variable @c grant_version (@c static variable
in sql_acl.cc). It is updated Whenever the GRANT_INFO is refreshed from
the level 2 cache. The level 2 cache is the @c column_priv_hash structure
(@c static variable in sql_acl.cc)
@see grant_version
*/
uint version{0};
/**
@brief The set of privileges that the current user has fulfilled for a
certain host, database, and object.
@details This field is continually updated throughout the access checking
process. In each step the "wanted privilege" is checked against the
fulfilled privileges. When/if the intersection of these sets is empty,
access is granted.
The set is implemented as a bitmap, with the bits defined in sql_acl.h.
*/
ulong privilege{0};
/** The grant state for internal tables. */
GRANT_INTERNAL_INFO m_internal;
};
enum tmp_table_type {
NO_TMP_TABLE,
NON_TRANSACTIONAL_TMP_TABLE,
TRANSACTIONAL_TMP_TABLE,
INTERNAL_TMP_TABLE,
SYSTEM_TMP_TABLE
};
/**
Category of table found in the table share.
*/
enum enum_table_category {
/**
Unknown value.
*/
TABLE_UNKNOWN_CATEGORY = 0,
/**
Temporary table.
The table is visible only in the session.
Therefore,
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
do not apply to this table.
Note that LOCK TABLE t FOR READ/WRITE
can be used on temporary tables.
Temporary tables are not part of the table cache.
2016-06-14 Contrary to what's written in these comments, the truth is:
- tables created by CREATE TEMPORARY TABLE have TABLE_CATEGORY_USER
- tables created by create_tmp_table() (internal ones) have
TABLE_CATEGORY_TEMPORARY.
ha_innodb.cc relies on this observation (so: grep it). If you clean this
up, you may also want to look at 'no_tmp_table'; its enum values' meanings
have degraded over time: INTERNAL_TMP_TABLE is not used for some internal
tmp tables (derived tables). Unification of both enums would be
great. Whatever the result, we need to be able to distinguish the two
types of temporary tables above, as usage patterns are more restricted for
the second type, and allow more optimizations.
*/
TABLE_CATEGORY_TEMPORARY = 1,
/**
User table.
These tables do honor:
- LOCK TABLE t FOR READ/WRITE
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
User tables are cached in the table cache.
*/
TABLE_CATEGORY_USER = 2,
/**
System table, maintained by the server.
These tables do honor:
- LOCK TABLE t FOR READ/WRITE
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
Typically, writes to system tables are performed by
the server implementation, not explicitly be a user.
System tables are cached in the table cache.
*/
TABLE_CATEGORY_SYSTEM = 3,
/**
Information schema tables.
These tables are an interface provided by the system
to inspect the system metadata.
These tables do *not* honor:
- LOCK TABLE t FOR READ/WRITE
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
as there is no point in locking explicitly
an INFORMATION_SCHEMA table.
Nothing is directly written to information schema tables.
Note that this value is not used currently,
since information schema tables are not shared,
but implemented as session specific temporary tables.
*/
/*
TODO: Fixing the performance issues of I_S will lead
to I_S tables in the table cache, which should use
this table type.
*/
TABLE_CATEGORY_INFORMATION = 4,
/**
Log tables.
These tables are an interface provided by the system
to inspect the system logs.
These tables do *not* honor:
- LOCK TABLE t FOR READ/WRITE
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
as there is no point in locking explicitly
a LOG table.
An example of LOG tables are:
- mysql.slow_log
- mysql.general_log,
which *are* updated even when there is either
a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
User queries do not write directly to these tables
(there are exceptions for log tables).
The server implementation perform writes.
Log tables are cached in the table cache.
*/
TABLE_CATEGORY_LOG = 5,
/**
Performance schema tables.
These tables are an interface provided by the system
to inspect the system performance data.
These tables do *not* honor:
- LOCK TABLE t FOR READ/WRITE
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
as there is no point in locking explicitly
a PERFORMANCE_SCHEMA table.
An example of PERFORMANCE_SCHEMA tables are:
- performance_schema.*
which *are* updated (but not using the handler interface)
even when there is either
a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
User queries do not write directly to these tables
(there are exceptions for SETUP_* tables).
The server implementation perform writes.
Performance tables are cached in the table cache.
*/
TABLE_CATEGORY_PERFORMANCE = 6,
/**
Replication Information Tables.
These tables are used to store replication information.
These tables do *not* honor:
- LOCK TABLE t FOR READ/WRITE
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
as there is no point in locking explicitly
a Replication Information table.
An example of replication tables are:
- mysql.slave_master_info
- mysql.slave_relay_log_info,
which *are* updated even when there is either
a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
User queries do not write directly to these tables.
Replication tables are cached in the table cache.
*/
TABLE_CATEGORY_RPL_INFO = 7,
/**
Gtid Table.
The table is used to store gtids.
The table does *not* honor:
- LOCK TABLE t FOR READ/WRITE
- FLUSH TABLES WITH READ LOCK
- SET GLOBAL READ_ONLY = ON
as there is no point in locking explicitly
a Gtid table.
An example of gtid_executed table is:
- mysql.gtid_executed,
which is updated even when there is either
a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
Gtid table is cached in the table cache.
*/
TABLE_CATEGORY_GTID = 8,
/**
A data dictionary table.
Table's with this category will skip checking the
TABLE_SHARE versions because these table structures
are fixed upon server bootstrap.
*/
TABLE_CATEGORY_DICTIONARY = 9,
/**
A ACL metadata table.
For table in this category we will skip row locks when SQL statement
reads them.
*/
TABLE_CATEGORY_ACL_TABLE = 10
};
typedef enum enum_table_category TABLE_CATEGORY;
extern ulong refresh_version;
struct TABLE_FIELD_TYPE {
LEX_CSTRING name;
LEX_CSTRING type;
LEX_CSTRING cset;
};
struct TABLE_FIELD_DEF {
uint count;
const TABLE_FIELD_TYPE *field;
};
class Table_check_intact {
protected:
bool has_keys;
virtual void report_error(uint code, const char *fmt, ...) = 0;
public:
Table_check_intact() : has_keys(false) {}
virtual ~Table_check_intact() = default;
/**
Checks whether a table is intact. Should be done *just* after the table has
been opened.
@param[in] thd Thread handle
@param[in] table The table to check
@param[in] table_def Expected structure of the table (column name
and type)
@retval false OK
@retval true There was an error.
*/
bool check(THD *thd, TABLE *table, const TABLE_FIELD_DEF *table_def);
};
/**
Class representing the fact that some thread waits for table
share to be flushed. Is used to represent information about
such waits in MDL deadlock detector.
*/
class Wait_for_flush : public MDL_wait_for_subgraph {
MDL_context *m_ctx;
TABLE_SHARE *m_share;
uint m_deadlock_weight;
public:
Wait_for_flush(MDL_context *ctx_arg, TABLE_SHARE *share_arg,
uint deadlock_weight_arg)
: m_ctx(ctx_arg),
m_share(share_arg),
m_deadlock_weight(deadlock_weight_arg) {}
MDL_context *get_ctx() const { return m_ctx; }
bool accept_visitor(MDL_wait_for_graph_visitor *dvisitor) override;
uint get_deadlock_weight() const override;
/**
Pointers for participating in the list of waiters for table share.
*/
Wait_for_flush *next_in_share;
Wait_for_flush **prev_in_share;
};
typedef I_P_List<
Wait_for_flush,
I_P_List_adapter<Wait_for_flush, &Wait_for_flush::next_in_share,
&Wait_for_flush::prev_in_share>>
Wait_for_flush_list;
typedef struct Table_share_foreign_key_info {
LEX_CSTRING referenced_table_db;
LEX_CSTRING referenced_table_name;
/**
Name of unique key matching FK in parent table, "" if there is no
unique key.
*/
LEX_CSTRING unique_constraint_name;
dd::Foreign_key::enum_rule update_rule, delete_rule;
uint columns;
/**
Arrays with names of referencing columns of the FK.
*/
LEX_CSTRING *column_name;
} TABLE_SHARE_FOREIGN_KEY_INFO;
typedef struct Table_share_foreign_key_parent_info {
LEX_CSTRING referencing_table_db;
LEX_CSTRING referencing_table_name;
dd::Foreign_key::enum_rule update_rule, delete_rule;
} TABLE_SHARE_FOREIGN_KEY_PARENT_INFO;
/**
Definition of name for generated keys, owned by TABLE_SHARE
*/
struct Key_name {
char name[NAME_CHAR_LEN];
};
/**
This structure is shared between different table objects. There is one
instance of table share per one table in the database.
*/
struct TABLE_SHARE {
TABLE_SHARE() = default;
/**
Create a new TABLE_SHARE with the given version number.
@param version the version of the TABLE_SHARE
@param secondary set to true if the TABLE_SHARE represents a table
in a secondary storage engine
*/
TABLE_SHARE(unsigned long version, bool secondary)
: m_version(version), m_secondary_engine(secondary) {}
/*
A map of [uint, Histogram] values, where the key is the field index. The
map is populated with any histogram statistics when it is loaded/created.
*/
malloc_unordered_map<uint, const histograms::Histogram *> *m_histograms{
nullptr};
/**
Find the histogram for the given field index.
@param field_index the index of the field we want to find a histogram for
@retval nullptr if no histogram is found
@retval a pointer to a histogram if one is found
*/
const histograms::Histogram *find_histogram(uint field_index) const;
/** Category of this table. */
TABLE_CATEGORY table_category{TABLE_UNKNOWN_CATEGORY};
MEM_ROOT mem_root;
/**
Used to allocate new handler for internal temporary table when the
size limitation of the primary storage engine is exceeded.
*/
MEM_ROOT *alloc_for_tmp_file_handler{nullptr};
TYPELIB keynames; /* Pointers to keynames */
TYPELIB *intervals{nullptr}; /* pointer to interval info */
mysql_mutex_t LOCK_ha_data; /* To protect access to ha_data */
TABLE_SHARE *next{nullptr}, **prev{nullptr}; /* Link to unused shares */
/**
Array of table_cache_instances pointers to elements of table caches
respresenting this table in each of Table_cache instances.
Allocated along with the share itself in alloc_table_share().
Each element of the array is protected by Table_cache::m_lock in the
corresponding Table_cache. False sharing should not be a problem in
this case as elements of this array are supposed to be updated rarely.
*/
Table_cache_element **cache_element{nullptr};
/* The following is copied to each TABLE on OPEN */
Field **field{nullptr};
Field **found_next_number_field{nullptr};
KEY *key_info{nullptr}; /* data of keys defined for the table */
uint *blob_field{nullptr}; /* Index to blobs in Field array */
uchar *default_values{nullptr}; /* row with default values */
LEX_STRING comment{nullptr, 0}; /* Comment about table */
LEX_STRING compress{nullptr, 0}; /* Compression algorithm */
LEX_STRING encrypt_type{nullptr, 0}; /* encryption algorithm */
/** Secondary storage engine. */
LEX_CSTRING secondary_engine{nullptr, 0};
const CHARSET_INFO *table_charset{
nullptr}; /* Default charset of string fields */
MY_BITMAP all_set;
/*
Key which is used for looking-up table in table cache and in the list
of thread's temporary tables. Has the form of:
"database_name\0table_name\0" + optional part for temporary tables.
Note that all three 'table_cache_key', 'db' and 'table_name' members
must be set (and be non-zero) for tables in table cache. They also
should correspond to each other.
To ensure this one can use set_table_cache() methods.
*/
LEX_CSTRING table_cache_key{nullptr, 0};
LEX_CSTRING db{nullptr, 0}; /* Pointer to db */
LEX_CSTRING table_name{nullptr, 0}; /* Table name (for open) */
LEX_STRING path{nullptr, 0}; /* Path to .frm file (from datadir) */
LEX_CSTRING normalized_path{nullptr, 0}; /* unpack_filename(path) */
LEX_STRING connect_string{nullptr, 0};
LEX_CSTRING engine_attribute = EMPTY_CSTR;
LEX_CSTRING secondary_engine_attribute = EMPTY_CSTR;
/**
The set of indexes that are not disabled for this table. I.e. it excludes
indexes disabled by `ALTER TABLE ... DISABLE KEYS`, however it does
include invisible indexes. The data dictionary populates this bitmap.
*/
Key_map keys_in_use;
/// The set of visible and enabled indexes for this table.
Key_map visible_indexes;
Key_map keys_for_keyread;
ha_rows min_rows{0}, max_rows{0}; /* create information */
ulong avg_row_length{0}; /* create information */
ulong mysql_version{0}; /* 0 if .frm is created before 5.0 */
ulong reclength{0}; /* Recordlength */
ulong stored_rec_length{0}; /* Stored record length
(no generated-only generated fields) */
ulonglong autoextend_size{0};
plugin_ref db_plugin{nullptr}; /* storage engine plugin */
inline handlerton *db_type() const /* table_type for handler */
{
// assert(db_plugin);
return db_plugin ? plugin_data<handlerton *>(db_plugin) : NULL;
}
/**
Value of ROW_FORMAT option for the table as provided by user.
Can be different from the real row format used by the storage
engine. ROW_TYPE_DEFAULT value indicates that no explicit
ROW_FORMAT was specified for the table. @sa real_row_type.
*/
enum row_type row_type = {}; // Zero-initialized to ROW_TYPE_DEFAULT
/** Real row format used for the table by the storage engine. */
enum row_type real_row_type = {}; // Zero-initialized to ROW_TYPE_DEFAULT
tmp_table_type tmp_table{NO_TMP_TABLE};
/**
Only for internal temporary tables.
Count of TABLEs (having this TABLE_SHARE) which have a "handler"
(table->file!=nullptr) which is open (ha_open() has been called).
*/
uint tmp_handler_count{0};
/**
Only for internal temporary tables.
Count of TABLEs (having this TABLE_SHARE) which have opened this table.
*/
uint tmp_open_count{0};
// Can only be 1,2,4,8 or 16, but use uint32_t since that how it is
// represented in InnoDB
std::uint32_t key_block_size{0}; /* create key_block_size, if used */
uint stats_sample_pages{0}; /* number of pages to sample during
stats estimation, if used, otherwise 0. */
enum_stats_auto_recalc
stats_auto_recalc{}; /* Automatic recalc of stats.
Zero-initialized to HA_STATS_AUTO_RECALC_DEFAULT
*/
uint null_bytes{0}, last_null_bit_pos{0};
uint fields{0}; /* Number of fields */
uint rec_buff_length{0}; /* Size of table->record[] buffer */
uint keys{0}; /* Number of keys defined for the table*/
uint key_parts{0}; /* Number of key parts of all keys
defined for the table
*/
uint max_key_length{0}; /* Length of the longest key */
uint max_unique_length{0}; /* Length of the longest unique key */
uint total_key_length{0};
/**
Whether this is a temporary table that already has a UNIQUE index (removing
duplicate rows on insert), so that the optimizer does not need to run
DISTINCT itself.
*/
bool is_distinct{false};
uint null_fields{0}; /* number of null fields */
uint blob_fields{0}; /* number of blob fields */
uint varchar_fields{0}; /* number of varchar fields */
/**
For materialized derived tables; @see add_derived_key().
'first' means: having the lowest position in key_info.
*/
uint first_unused_tmp_key{0};
/**
For materialized derived tables: allocated size of key_info array.
*/
uint max_tmp_keys{0};
/**
For materialized derived tables: allocated size of base_key_parts array of
all TABLE objects. Used for generated keys.
*/
uint max_tmp_key_parts{0};
/**
Array of names for generated keys, used for materialized derived tables.
Shared among all TABLE objects referring to this table share.
*/
Key_name *key_names{nullptr};
/**
Records per key array, used for materialized derived tables.
This is a contiguous array, with size given by max_tmp_key_parts.
The array is shared with all TABLE objects referring to this table share.
*/
ulong *base_rec_per_key{nullptr};
/**
Records per key array, float rep., used for materialized derived tables.
This is a contiguous array, with size given by max_tmp_key_parts.
The array is shared with all TABLE objects referring to this table share.
*/
rec_per_key_t *base_rec_per_key_float{nullptr};
/**
Bitmap with flags representing some of table options/attributes.
@sa HA_OPTION_PACK_RECORD, HA_OPTION_PACK_KEYS, ...
@note This is basically copy of HA_CREATE_INFO::table_options bitmap
at the time of table opening/usage.
*/
uint db_create_options{0};
/**
Bitmap with flags representing some of table options/attributes which
are in use by storage engine.
@note db_options_in_use is normally copy of db_create_options but can
be overridden by SE. E.g. MyISAM does this at handler::open() and
handler::info() time.
*/
uint db_options_in_use{0};
uint rowid_field_offset{0}; /* Field_nr +1 to rowid field */
/* Primary key index number, used in TABLE::key_info[] */
uint primary_key{0};
uint next_number_index{0}; /* autoincrement key number */
uint next_number_key_offset{0}; /* autoinc keypart offset in a key */
uint next_number_keypart{0}; /* autoinc keypart number in a key */
bool error{false}; /* error during open_table_def() */
uint column_bitmap_size{0};
/// Number of generated fields
uint vfields{0};
/// Number of fields having the default value generated
uint gen_def_field_count{0};
bool system{false}; /* Set if system table (one record) */
bool db_low_byte_first{false}; /* Portable row format */
bool crashed{false};
bool is_view{false};
bool m_open_in_progress{false}; /* True: alloc'ed, false: def opened */
Table_id table_map_id; /* for row-based replication */
/*
Cache for row-based replication table share checks that does not
need to be repeated. Possible values are: -1 when cache value is
not calculated yet, 0 when table *shall not* be replicated, 1 when
table *may* be replicated.
*/
int cached_row_logging_check{0};
/*
Storage media to use for this table (unless another storage
media has been specified on an individual column - in versions
where that is supported)
*/
ha_storage_media default_storage_media{HA_SM_DEFAULT};
/* Name of the tablespace used for this table */
const char *tablespace{nullptr};
/**
Partition meta data. Allocated from TABLE_SHARE::mem_root,
created when reading from the dd tables,
used as template for each TABLE instance.
The reason for having it on the TABLE_SHARE is to be able to reuse the
partition_elements containing partition names, values etc. instead of
allocating them for each TABLE instance.
TODO: Currently it is filled in and then only used for generating
the partition_info_str. The plan is to clone/copy/reference each
TABLE::part_info instance from it.
What is missing before it can be completed:
1) The partition expression, currently created only during parsing which
also needs the current TABLE instance as context for name resolution etc.
2) The partition values, currently the DD stores them as text so it needs
to be converted to field images (which is now done by first parsing the
value text into an Item, then saving the Item result/value into a field
and then finally copy the field image).
*/
partition_info *m_part_info{nullptr};
// TODO: Remove these four variables:
/**
Filled in when reading from frm.
This can simply be removed when removing the .frm support,
since it is already stored in the new DD.
*/
bool auto_partitioned{false};
/**
Storing the full partitioning clause (PARTITION BY ...) which is used
when creating new partition_info object for each new TABLE object by
parsing this string.
These two will be needed until the missing parts above is fixed.
*/
char *partition_info_str{nullptr};
uint partition_info_str_len{0};
/**
Cache the checked structure of this table.
The pointer data is used to describe the structure that
a instance of the table must have. Each element of the
array specifies a field that must exist on the table.
The pointer is cached in order to perform the check only
once -- when the table is loaded from the disk.
*/
const TABLE_FIELD_DEF *table_field_def_cache{nullptr};
/** Main handler's share */
Handler_share *ha_share{nullptr};
/** Instrumentation for this table share. */
PSI_table_share *m_psi{nullptr};
/**
List of tickets representing threads waiting for the share to be flushed.
*/
Wait_for_flush_list m_flush_tickets;
/**