/
ha_innopart.cc
4291 lines (3686 loc) · 114 KB
/
ha_innopart.cc
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
/*****************************************************************************
Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2017, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
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 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 Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/** @file ha_innopart.cc
Code for native partitioning in InnoDB.
Created Nov 22, 2013 Mattias Jonsson */
#include "univ.i"
/* Include necessary SQL headers */
#include <debug_sync.h>
#include <log.h>
#include <strfunc.h>
#include <sql_acl.h>
#include <sql_class.h>
#include <sql_show.h>
#include <sql_table.h>
#include <my_check_opt.h>
/* Include necessary InnoDB headers */
#include "btr0sea.h"
#include "dict0dict.h"
#include "dict0stats.h"
#include "lock0lock.h"
#include "row0import.h"
#include "row0merge.h"
#include "row0mysql.h"
#include "row0quiesce.h"
#include "row0sel.h"
#include "row0ins.h"
#include "row0upd.h"
#include "fsp0sysspace.h"
#include "ut0ut.h"
#include "ha_innodb.h"
#include "ha_innopart.h"
#include "partition_info.h"
#include "key.h"
#define INSIDE_HA_INNOPART_CC
/* To be backwards compatible we also fold partition separator on windows. */
#ifdef _WIN32
static const char* part_sep = "#p#";
static const char* sub_sep = "#sp#";
#else
static const char* part_sep = "#P#";
static const char* sub_sep = "#SP#";
#endif /* _WIN32 */
/* Partition separator for *nix platforms */
const char* part_sep_nix = "#P#";
const char* sub_sep_nix = "#SP#";
extern char* innobase_file_format_max;
Ha_innopart_share::Ha_innopart_share(
TABLE_SHARE* table_share)
:
Partition_share(),
m_table_parts(),
m_index_mapping(),
m_tot_parts(),
m_index_count(),
m_ref_count(),
m_table_share(table_share)
{}
Ha_innopart_share::~Ha_innopart_share()
{
ut_ad(m_ref_count == 0);
if (m_table_parts != NULL) {
ut_free(m_table_parts);
m_table_parts = NULL;
}
if (m_index_mapping != NULL) {
ut_free(m_index_mapping);
m_index_mapping = NULL;
}
}
/** Fold to lower case if windows or lower_case_table_names == 1.
@param[in,out] s String to fold.*/
void
Ha_innopart_share::partition_name_casedn_str(
char* s)
{
#ifdef _WIN32
innobase_casedn_str(s);
#endif
}
/** Translate and append partition name.
@param[out] to String to write in filesystem charset
@param[in] from Name in system charset
@param[in] sep Separator
@param[in] len Max length of to buffer
@return length of written string. */
size_t
Ha_innopart_share::append_sep_and_name(
char* to,
const char* from,
const char* sep,
size_t len)
{
size_t ret;
size_t sep_len = strlen(sep);
ut_ad(len > sep_len + strlen(from));
ut_ad(to != NULL);
ut_ad(from != NULL);
ut_ad(from[0] != '\0');
memcpy(to, sep, sep_len);
ret = tablename_to_filename(from, to + sep_len,
len - sep_len);
/* Don't convert to lower case for nix style name. */
if (strcmp(sep, part_sep_nix) != 0
&& strcmp(sep, sub_sep_nix) != 0) {
partition_name_casedn_str(to);
}
return(ret + sep_len);
}
/** Copy a cached MySQL row.
If requested, also avoids overwriting non-read columns.
@param[out] buf Row in MySQL format.
@param[in] cached_row Which row to copy. */
inline
void
ha_innopart::copy_cached_row(
uchar* buf,
const uchar* cached_row)
{
if (m_prebuilt->keep_other_fields_on_keyread) {
row_sel_copy_cached_fields_for_mysql(buf, cached_row,
m_prebuilt);
} else {
memcpy(buf, cached_row, m_rec_length);
}
}
/** Open one partition.
@param[in] part_id Partition id to open.
@param[in] partition_name Name of internal innodb table to open.
@return false on success else true. */
bool
Ha_innopart_share::open_one_table_part(
uint part_id,
const char* partition_name)
{
char norm_name[FN_REFLEN];
normalize_table_name(norm_name, partition_name);
m_table_parts[part_id] =
ha_innobase::open_dict_table(partition_name, norm_name,
TRUE, DICT_ERR_IGNORE_NONE);
if (m_table_parts[part_id] == NULL) {
return(true);
}
dict_table_t *ib_table = m_table_parts[part_id];
if ((!DICT_TF2_FLAG_IS_SET(ib_table, DICT_TF2_FTS_HAS_DOC_ID)
&& m_table_share->fields
!= (dict_table_get_n_user_cols(ib_table)
+ dict_table_get_n_v_cols(ib_table)))
|| (DICT_TF2_FLAG_IS_SET(ib_table, DICT_TF2_FTS_HAS_DOC_ID)
&& (m_table_share->fields
!= dict_table_get_n_user_cols(ib_table)
+ dict_table_get_n_v_cols(ib_table) - 1))) {
ib::warn() << "Partition `" << get_partition_name(part_id)
<< "` contains " << dict_table_get_n_user_cols(ib_table)
<< " user defined columns in InnoDB, but "
<< m_table_share->fields
<< " columns in MySQL. Please check"
" INFORMATION_SCHEMA.INNODB_SYS_COLUMNS and " REFMAN
"innodb-troubleshooting.html for how to resolve the"
" issue.";
/* Mark this partition as corrupted, so the drop table
or force recovery can still use it, but not others.
TODO: persist table->corrupted so it will be retained on
restart and out-of-bounds operations will see it. */
ib_table->corrupted = true;
dict_table_close(ib_table, FALSE, FALSE);
}
/* TODO: To save memory, compare with first partition and reuse
the column names etc. in the internal InnoDB meta-data cache. */
return(false);
}
/** Set up the virtual column template for partition table, and points
all m_table_parts[]->vc_templ to it.
@param[in] table MySQL TABLE object
@param[in] ib_table InnoDB dict_table_t
@param[in] table_name Table name (db/table_name) */
void
Ha_innopart_share::set_v_templ(
TABLE* table,
dict_table_t* ib_table,
const char* name)
{
ut_ad(mutex_own(&dict_sys->mutex));
if (ib_table->n_v_cols > 0) {
for (ulint i = 0; i < m_tot_parts; i++) {
if (m_table_parts[i]->vc_templ == NULL) {
m_table_parts[i]->vc_templ
= UT_NEW_NOKEY(dict_vcol_templ_t());
m_table_parts[i]->vc_templ->vtempl = NULL;
} else if (m_table_parts[i]->get_ref_count() == 1) {
/* Clean and refresh the template */
dict_free_vc_templ(m_table_parts[i]->vc_templ);
m_table_parts[i]->vc_templ->vtempl = NULL;
}
if (m_table_parts[i]->vc_templ->vtempl == NULL) {
innobase_build_v_templ(
table, ib_table,
m_table_parts[i]->vc_templ,
NULL, true, name);
}
}
}
}
/** Initialize the share with table and indexes per partition.
@param[in] part_info Partition info (partition names to use).
@param[in] table_name Table name (db/table_name).
@return false on success else true. */
bool
Ha_innopart_share::open_table_parts(
partition_info* part_info,
const char* table_name)
{
size_t table_name_len;
size_t len;
uint ib_num_index;
uint mysql_num_index;
char partition_name[FN_REFLEN];
bool index_loaded = true;
#ifndef DBUG_OFF
if (m_table_share->tmp_table == NO_TMP_TABLE) {
mysql_mutex_assert_owner(&m_table_share->LOCK_ha_data);
}
#endif /* DBUG_OFF */
m_ref_count++;
if (m_table_parts != NULL) {
ut_ad(m_ref_count > 1);
ut_ad(m_tot_parts > 0);
/* Increment dict_table_t reference count for all partitions */
mutex_enter(&dict_sys->mutex);
for (uint i = 0; i < m_tot_parts; i++) {
dict_table_t* table = m_table_parts[i];
table->acquire();
ut_ad(table->get_ref_count() >= m_ref_count);
}
mutex_exit(&dict_sys->mutex);
return(false);
}
ut_ad(m_ref_count == 1);
m_tot_parts = part_info->get_tot_partitions();
size_t table_parts_size = sizeof(dict_table_t*) * m_tot_parts;
m_table_parts = static_cast<dict_table_t**>(
ut_zalloc(table_parts_size, mem_key_partitioning));
if (m_table_parts == NULL) {
m_ref_count--;
return(true);
}
/* Set up the array over all table partitions. */
table_name_len = strlen(table_name);
memcpy(partition_name, table_name, table_name_len);
List_iterator<partition_element>
part_it(part_info->partitions);
partition_element* part_elem;
uint i = 0;
while ((part_elem = part_it++)) {
len = append_sep_and_name(
partition_name + table_name_len,
part_elem->partition_name,
part_sep_nix,
FN_REFLEN - table_name_len);
if (part_info->is_sub_partitioned()) {
List_iterator<partition_element>
sub_it(part_elem->subpartitions);
partition_element* sub_elem;
while ((sub_elem = sub_it++)) {
append_sep_and_name(
partition_name
+ table_name_len + len,
sub_elem->partition_name,
sub_sep_nix,
FN_REFLEN - table_name_len - len);
if (open_one_table_part(i, partition_name)) {
goto err;
}
i++;
}
} else {
if (open_one_table_part(i, partition_name)) {
goto err;
}
i++;
}
}
ut_ad(i == m_tot_parts);
/* Create the mapping of mysql index number to innodb indexes. */
ib_num_index = (uint) UT_LIST_GET_LEN(m_table_parts[0]->indexes);
mysql_num_index = part_info->table->s->keys;
/* If there exists inconsistency between MySQL and InnoDB dictionary
(metadata) information, the number of index defined in MySQL
could exceed that in InnoDB, do not build index translation
table in such case. */
if (ib_num_index < mysql_num_index) {
ut_ad(0);
goto err;
}
if (mysql_num_index != 0) {
size_t alloc_size = mysql_num_index * m_tot_parts
* sizeof(*m_index_mapping);
m_index_mapping = static_cast<dict_index_t**>(
ut_zalloc(alloc_size, mem_key_partitioning));
if (m_index_mapping == NULL) {
/* Report an error if index_mapping continues to be
NULL and mysql_num_index is a non-zero value. */
ib::error() << "Failed to allocate memory for"
" index translation table. Number of"
" Index:" << mysql_num_index;
goto err;
}
}
/* For each index in the mysql key_info array, fetch its
corresponding InnoDB index pointer into index_mapping
array. */
for (ulint idx = 0; idx < mysql_num_index; idx++) {
for (ulint part = 0; part < m_tot_parts; part++) {
ulint count = part * mysql_num_index + idx;
/* Fetch index pointers into index_mapping according
to mysql index sequence. */
m_index_mapping[count] = dict_table_get_index_on_name(
m_table_parts[part],
part_info->table->key_info[idx].name);
if (m_index_mapping[count] == NULL) {
ib::error() << "Cannot find index `"
<< part_info->table->key_info[idx].name
<< "` in InnoDB index dictionary"
" partition `"
<< get_partition_name(part) << "`.";
index_loaded = false;
break;
}
/* Double check fetched index has the same
column info as those in mysql key_info. */
if (!innobase_match_index_columns(
&part_info->table->key_info[idx],
m_index_mapping[count])) {
ib::error() << "Found index `"
<< part_info->table->key_info[idx].name
<< "` whose column info does not match"
" that of MySQL.";
index_loaded = false;
break;
}
}
}
if (!index_loaded && m_index_mapping != NULL) {
ut_free(m_index_mapping);
m_index_mapping = NULL;
}
/* Successfully built the translation table. */
m_index_count = mysql_num_index;
return(false);
err:
close_table_parts();
return(true);
}
/** Close all partitions. */
void
Ha_innopart_share::close_table_parts()
{
#ifndef DBUG_OFF
if (m_table_share->tmp_table == NO_TMP_TABLE) {
mysql_mutex_assert_owner(&m_table_share->LOCK_ha_data);
}
#endif /* DBUG_OFF */
m_ref_count--;
if (m_ref_count != 0) {
/* Decrement dict_table_t reference count for all partitions */
mutex_enter(&dict_sys->mutex);
for (uint i = 0; i < m_tot_parts; i++) {
dict_table_t* table = m_table_parts[i];
table->release();
ut_ad(table->get_ref_count() >= m_ref_count);
}
mutex_exit(&dict_sys->mutex);
return;
}
/* Last instance closed, close all table partitions and
free the memory. */
mutex_enter(&dict_sys->mutex);
if (m_table_parts != NULL) {
for (uint i = 0; i < m_tot_parts; i++) {
if (m_table_parts[i] != NULL) {
dict_table_close(m_table_parts[i], TRUE, TRUE);
}
}
ut_free(m_table_parts);
m_table_parts = NULL;
}
mutex_exit(&dict_sys->mutex);
if (m_index_mapping != NULL) {
ut_free(m_index_mapping);
m_index_mapping = NULL;
}
m_tot_parts = 0;
m_index_count = 0;
}
/** Get index.
Find the index of the specified partition and key number.
@param[in] part_id Partition number.
@param[in] keynr Key number.
@return Index pointer or NULL. */
inline
dict_index_t*
Ha_innopart_share::get_index(
uint part_id,
uint keynr)
{
ut_a(part_id < m_tot_parts);
ut_ad(keynr < m_index_count || keynr == MAX_KEY);
if (m_index_mapping == NULL
|| keynr >= m_index_count) {
if (keynr == MAX_KEY) {
return(dict_table_get_first_index(
get_table_part(part_id)));
}
return(NULL);
}
return(m_index_mapping[m_index_count * part_id + keynr]);
}
/** Get MySQL key number corresponding to InnoDB index.
Calculates the key number used inside MySQL for an Innobase index. We will
first check the "index translation table" for a match of the index to get
the index number. If there does not exist an "index translation table",
or not able to find the index in the translation table, then we will fall back
to the traditional way of looping through dict_index_t list to find a
match. In this case, we have to take into account if we generated a
default clustered index for the table
@param[in] part_id Partition the index belongs to.
@param[in] index Index to return MySQL key number for.
@return the key number used inside MySQL or UINT_MAX if key is not found. */
inline
uint
Ha_innopart_share::get_mysql_key(
uint part_id,
const dict_index_t* index)
{
ut_ad(index != NULL);
ut_ad(m_index_mapping != NULL);
ut_ad(m_tot_parts);
if (index != NULL && m_index_mapping != NULL) {
uint start;
uint end;
if (part_id < m_tot_parts) {
start = part_id * m_index_count;
end = start + m_index_count;
} else {
start = 0;
end = m_tot_parts * m_index_count;
}
for (uint i = start; i < end; i++) {
if (m_index_mapping[i] == index) {
return(i % m_index_count);
}
}
/* Print an error message if we cannot find the index
in the "index translation table". */
if (index->is_committed()) {
ib::error() << "Cannot find index "
<< index->name
<< " in InnoDB index translation table.";
}
}
return(UINT_MAX);
}
/** Helper function for set bit in bitmap.
@param[in,out] buf Bitmap buffer to update bit in.
@param[in] bit_pos Bit number (index starts at 0). */
static
inline
void
set_bit(
byte* buf,
size_t pos)
{
buf[pos/8] |= (0x1 << (pos & 0x7));
}
/** Helper function for clear bit in bitmap.
@param[in,out] buf Bitmap buffer to update bit in.
@param[in] bit_pos Bit number (index starts at 0). */
static
inline
void
clear_bit(
byte* buf,
size_t pos)
{
buf[pos/8] &= ~(0x1 << (pos & 0x7));
}
/** Helper function for get bit in bitmap.
@param[in,out] buf Bitmap buffer.
@param[in] bit_pos Bit number (index starts at 0).
@return byte set to 0x0 or 0x1.
@retval 0x0 bit not set.
@retval 0x1 bet set. */
static
inline
byte
get_bit(
byte* buf,
size_t pos)
{
return((buf[pos/8] >> (pos & 0x7)) & 0x1);
}
/** Helper class for encapsulating new/altered partitions during
ADD/REORG/... PARTITION. */
class Altered_partitions
{
private:
/** New partitions during ADD/REORG/... PARTITION. */
dict_table_t** m_new_table_parts;
/** Insert nodes per partition. */
ins_node_t** m_ins_nodes;
/** sql_stat_start per partition. */
byte* m_sql_stat_start;
/** Trx id per partition. */
trx_id_t* m_trx_ids;
/** Number of new partitions. */
size_t m_num_new_parts;
/** Only need to create the partitions (no open/lock). */
bool m_only_create;
public:
Altered_partitions(
uint n_partitions,
bool only_create);
~Altered_partitions();
bool
initialize();
bool
only_create() const
{
return(m_only_create);
}
/** Set currently used partition.
@param[in] new_part_id Partition id to set.
@param[in] part InnoDB table to use. */
inline
void
set_part(
ulint new_part_id,
dict_table_t* part)
{
ut_ad(m_new_table_parts[new_part_id] == NULL);
m_new_table_parts[new_part_id] = part;
set_bit(m_sql_stat_start, new_part_id);
}
/** Get lower level InnoDB table for partition.
@param[in] part_id Partition id.
@return Lower level InnoDB table for the partition id. */
inline
dict_table_t*
part(
uint part_id) const
{
ut_ad(part_id < m_num_new_parts);
return(m_new_table_parts[part_id]);
}
/** Set up prebuilt for using a specified partition.
@param[in] prebuilt Prebuilt to update.
@param[in] new_part_id Partition to use. */
inline
void
get_prebuilt(
row_prebuilt_t* prebuilt,
uint new_part_id) const
{
ut_ad(m_new_table_parts[new_part_id]);
prebuilt->table = m_new_table_parts[new_part_id];
prebuilt->ins_node = m_ins_nodes[new_part_id];
prebuilt->trx_id = m_trx_ids[new_part_id];
prebuilt->sql_stat_start = get_bit(m_sql_stat_start,
new_part_id);
}
/** Update cached values for a partition from prebuilt.
@param[in] prebuilt Prebuilt to copy from.
@param[in] new_part_id Partition id to copy. */
inline
void
set_from_prebuilt(
row_prebuilt_t* prebuilt,
uint new_part_id)
{
ut_ad(m_new_table_parts[new_part_id] == prebuilt->table);
m_ins_nodes[new_part_id] = prebuilt->ins_node;
m_trx_ids[new_part_id] = prebuilt->trx_id;
if (prebuilt->sql_stat_start == 0) {
clear_bit(m_sql_stat_start, new_part_id);
}
}
};
Altered_partitions::Altered_partitions(
uint n_partitions,
bool only_create)
:
m_new_table_parts(),
m_ins_nodes(),
m_sql_stat_start(),
m_trx_ids(),
m_num_new_parts(n_partitions),
m_only_create(only_create)
{}
Altered_partitions::~Altered_partitions()
{
if (m_new_table_parts != NULL) {
for (ulint i = 0; i < m_num_new_parts; i++) {
if (m_new_table_parts[i] != NULL) {
dict_table_close(m_new_table_parts[i],
false, true);
}
}
ut_free(m_new_table_parts);
m_new_table_parts = NULL;
}
if (m_ins_nodes != NULL) {
for (ulint i = 0; i < m_num_new_parts; i++) {
if (m_ins_nodes[i] != NULL) {
ins_node_t* ins = m_ins_nodes[i];
ut_ad(ins->select == NULL);
que_graph_free_recursive(ins->select);
ins->select = NULL;
if (ins->entry_sys_heap != NULL) {
mem_heap_free(ins->entry_sys_heap);
ins->entry_sys_heap = NULL;
}
}
}
ut_free(m_ins_nodes);
m_ins_nodes = NULL;
}
if (m_sql_stat_start != NULL) {
ut_free(m_sql_stat_start);
m_sql_stat_start = NULL;
}
if (m_trx_ids != NULL) {
ut_free(m_trx_ids);
m_trx_ids = NULL;
}
}
/** Initialize the object.
@return false on success else true. */
bool
Altered_partitions::initialize()
{
size_t alloc_size = sizeof(*m_new_table_parts) * m_num_new_parts;
m_new_table_parts = static_cast<dict_table_t**>(
ut_zalloc(alloc_size, mem_key_partitioning));
if (m_new_table_parts == NULL) {
return(true);
}
alloc_size = sizeof(*m_ins_nodes) * m_num_new_parts;
m_ins_nodes = static_cast<ins_node_t**>(
ut_zalloc(alloc_size, mem_key_partitioning));
if (m_ins_nodes == NULL) {
ut_free(m_new_table_parts);
m_new_table_parts = NULL;
return(true);
}
alloc_size = sizeof(*m_sql_stat_start)
* UT_BITS_IN_BYTES(m_num_new_parts);
m_sql_stat_start = static_cast<byte*>(
ut_zalloc(alloc_size, mem_key_partitioning));
if (m_sql_stat_start == NULL) {
ut_free(m_new_table_parts);
m_new_table_parts = NULL;
ut_free(m_ins_nodes);
m_ins_nodes = NULL;
return(true);
}
alloc_size = sizeof(*m_trx_ids) * m_num_new_parts;
m_trx_ids = static_cast<trx_id_t*>(
ut_zalloc(alloc_size, mem_key_partitioning));
if (m_trx_ids == NULL) {
ut_free(m_new_table_parts);
m_new_table_parts = NULL;
ut_free(m_ins_nodes);
m_ins_nodes = NULL;
ut_free(m_sql_stat_start);
m_sql_stat_start = NULL;
return(true);
}
return(false);
}
/** Construct ha_innopart handler.
@param[in] hton Handlerton.
@param[in] table_arg MySQL Table.
@return a new ha_innopart handler. */
ha_innopart::ha_innopart(
handlerton* hton,
TABLE_SHARE* table_arg)
:
ha_innobase(hton, table_arg),
Partition_helper(this),
m_ins_node_parts(),
m_upd_node_parts(),
m_blob_heap_parts(),
m_trx_id_parts(),
m_row_read_type_parts(),
m_sql_stat_start_parts(),
m_pcur(),
m_clust_pcur(),
m_new_partitions()
{
m_int_table_flags &= ~(HA_INNOPART_DISABLED_TABLE_FLAGS);
/* INNOBASE_SHARE is not used in ha_innopart.
This also flags for ha_innobase that it is a partitioned table.
And make it impossible to use legacy share functionality. */
m_share = NULL;
}
/** Destruct ha_innopart handler. */
ha_innopart::~ha_innopart()
{}
/** Returned supported alter table flags.
@param[in] flags Flags to support.
@return Supported flags. */
uint
ha_innopart::alter_table_flags(
uint flags)
{
return(HA_PARTITION_FUNCTION_SUPPORTED | HA_FAST_CHANGE_PARTITION);
}
/** Set the autoinc column max value.
This should only be called once from ha_innobase::open().
Therefore there's no need for a covering lock.
@param[in] no_lock Ignored!
@return 0 for success or error code. */
inline
int
ha_innopart::initialize_auto_increment(
bool /* no_lock */)
{
int error = 0;
ulonglong auto_inc = 0;
const Field* field = table->found_next_number_field;
#ifndef DBUG_OFF
if (table_share->tmp_table == NO_TMP_TABLE)
{
mysql_mutex_assert_owner(m_part_share->auto_inc_mutex);
}
#endif
/* Since a table can already be "open" in InnoDB's internal
data dictionary, we only init the autoinc counter once, the
first time the table is loaded. We can safely reuse the
autoinc value from a previous MySQL open. */
if (m_part_share->auto_inc_initialized) {
/* Already initialized, nothing to do. */
return(0);
}
if (field == NULL) {
ib::info() << "Unable to determine the AUTOINC column name";
}
if (srv_force_recovery >= SRV_FORCE_NO_IBUF_MERGE) {
/* If the recovery level is set so high that writes
are disabled we force the AUTOINC counter to 0
value effectively disabling writes to the table.
Secondly, we avoid reading the table in case the read
results in failure due to a corrupted table/index.
We will not return an error to the client, so that the
tables can be dumped with minimal hassle. If an error
were returned in this case, the first attempt to read
the table would fail and subsequent SELECTs would succeed. */
} else if (field == NULL) {
/* This is a far more serious error, best to avoid
opening the table and return failure. */
my_error(ER_AUTOINC_READ_FAILED, MYF(0));
error = HA_ERR_AUTOINC_READ_FAILED;
} else {
ib_uint64_t col_max_value = field->get_max_int_value();
update_thd(ha_thd());
for (uint part = 0; part < m_tot_parts; part++) {
dict_table_t* ib_table
= m_part_share->get_table_part(part);
dict_table_autoinc_lock(ib_table);
ut_ad(ib_table->persistent_autoinc);
ib_uint64_t read_auto_inc
= dict_table_autoinc_read(ib_table);
if (read_auto_inc == 0) {
read_auto_inc = btr_read_autoinc(
dict_table_get_first_index(ib_table));
/* At the this stage we do not know the
increment nor the offset,
so use a default increment of 1. */
read_auto_inc = innobase_next_autoinc(
read_auto_inc, 1, 1, 0, col_max_value);
dict_table_autoinc_initialize(ib_table,
read_auto_inc);
}
set_if_bigger(auto_inc, read_auto_inc);
dict_table_autoinc_unlock(ib_table);
}
}
done:
m_part_share->next_auto_inc_val = auto_inc;
m_part_share->auto_inc_initialized = true;
return(error);
}
/** Opens a partitioned InnoDB table.
Initializes needed data and opens the table which already exists
in an InnoDB database.
@param[in] name Table name (db/tablename)
@param[in] mode Not used
@param[in] test_if_locked Not used
@return 0 or error number. */
int
ha_innopart::open(
const char* name,
int /*mode*/,
uint /*test_if_locked*/)
{
dict_table_t* ib_table;
char norm_name[FN_REFLEN];
THD* thd;
DBUG_ENTER("ha_innopart::open");
ut_ad(table);
if (m_part_info == NULL) {
/* Must be during ::clone()! */
ut_ad(table->part_info != NULL);
m_part_info = table->part_info;
}
thd = ha_thd();
/* Under some cases MySQL seems to call this function while
holding search latch(es). This breaks the latching order as
we acquire dict_sys->mutex below and leads to a deadlock. */
if (thd != NULL) {
innobase_release_temporary_latches(ht, thd);
}
normalize_table_name(norm_name, name);
m_user_thd = NULL;
/* Get the Ha_innopart_share from the TABLE_SHARE. */
lock_shared_ha_data();
m_part_share = static_cast<Ha_innopart_share*>(get_ha_share_ptr());
if (m_part_share == NULL) {
m_part_share = new (std::nothrow)
Ha_innopart_share(table_share);
if (m_part_share == NULL) {
share_error:
unlock_shared_ha_data();
DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
}
set_ha_share_ptr(static_cast<Handler_share*>(m_part_share));
}
if (m_part_share->open_table_parts(m_part_info, name)
|| m_part_share->populate_partition_name_hash(m_part_info)) {
goto share_error;
}
if (m_part_share->auto_inc_mutex == NULL
&& table->found_next_number_field != NULL) {
if (m_part_share->init_auto_inc_mutex(table_share)) {
goto share_error;
}
}
unlock_shared_ha_data();
/* Will be allocated if it is needed in ::update_row(). */
m_upd_buf = NULL;
m_upd_buf_size = 0;
/* Get pointer to a table object in InnoDB dictionary cache. */
ib_table = m_part_share->get_table_part(0);
m_pcur_parts = NULL;
m_clust_pcur_parts = NULL;
m_pcur_map = NULL;
/* TODO: Handle mismatching #P# vs #p# in upgrading to new DD instead!
See bug#58406, The problem exists when moving partitioned tables
between Windows and Unix-like platforms. InnoDB always folds the name
on windows, partitioning never folds partition (and #P# separator).
I.e. non of it follows lower_case_table_names correctly :( */