drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
handler0alter.cc
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (C) 2005, 2010, Innobase Oy. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15St, Fifth Floor, Boston, MA 02110-1301 USA
16
17*****************************************************************************/
18
19/**************************************************/
23
24#include <config.h>
25#include <drizzled/error.h>
26#include <drizzled/charset.h>
27#include <drizzled/field.h>
28#include <drizzled/table.h>
29#include <drizzled/field/varstring.h>
30#include <drizzled/internal/my_sys.h>
31#include <drizzled/key_part_info.h>
32
33#include "log0log.h"
34#include "row0merge.h"
35#include "srv0srv.h"
36#include "trx0trx.h"
37#include "trx0roll.h"
38#include "ha_prototypes.h"
39#include "handler0alter.h"
40
41#include "ha_innodb.h"
42#include "handler0vars.h"
43
44/*************************************************************/
47static
48void
50/*==================*/
51 const dict_col_t* col,
52 const unsigned char* data,
53 ulint len,
54 Field* field)
55{
56 unsigned char* ptr;
57 unsigned char* dest = field->ptr;
58 ulint flen = field->pack_length();
59
60 switch (col->mtype) {
61 case DATA_INT:
62 ut_ad(len == flen);
63
64 /* Convert integer data from Innobase to little-endian
65 format, sign bit restored to normal */
66
67 for (ptr = dest + len; ptr != dest; ) {
68 *--ptr = *data++;
69 }
70
71 if (!(field->flags & UNSIGNED_FLAG)) {
72 ((byte*) dest)[len - 1] ^= 0x80;
73 }
74
75 break;
76
77 case DATA_VARCHAR:
78 case DATA_VARMYSQL:
79 case DATA_BINARY:
80 field->reset();
81
82 if (field->type() == DRIZZLE_TYPE_VARCHAR) {
83 /* This is a >= 5.0.3 type true VARCHAR. Store the
84 length of the data to the first byte or the first
85 two bytes of dest. */
86
88 dest, len, flen - field->key_length());
89 }
90
91 /* Copy the actual data */
92 memcpy(dest, data, len);
93 break;
94
95 case DATA_BLOB:
96 /* Store a pointer to the BLOB buffer to dest: the BLOB was
97 already copied to the buffer in row_sel_store_mysql_rec */
98
99 row_mysql_store_blob_ref(dest, flen, data, len);
100 break;
101
102#ifdef UNIV_DEBUG
103 case DATA_MYSQL:
104 ut_ad(flen >= len);
105 ut_ad(DATA_MBMAXLEN(col->mbminmaxlen)
106 >= DATA_MBMINLEN(col->mbminmaxlen));
107 ut_ad(DATA_MBMAXLEN(col->mbminmaxlen)
108 > DATA_MBMINLEN(col->mbminmaxlen) || flen == len);
109 memcpy(dest, data, len);
110 break;
111
112 default:
113 case DATA_SYS_CHILD:
114 case DATA_SYS:
115 /* These column types should never be shipped to MySQL. */
116 ut_ad(0);
117
118 case DATA_CHAR:
119 case DATA_FIXBINARY:
120 case DATA_FLOAT:
121 case DATA_DOUBLE:
122 case DATA_DECIMAL:
123 /* Above are the valid column types for MySQL data. */
124 ut_ad(flen == len);
125#else /* UNIV_DEBUG */
126 default:
127#endif /* UNIV_DEBUG */
128 memcpy(dest, data, len);
129 }
130}
131
132/*************************************************************/
134UNIV_INTERN
135void
137/*==================*/
138 Table* table,
139 const rec_t* rec,
140 const dict_index_t* index,
141 const ulint* offsets)
143{
144 uint n_fields = table->getShare()->sizeFields();
145 uint i;
146
147 ut_ad(n_fields == dict_table_get_n_user_cols(index->table));
148
149 for (i = 0; i < n_fields; i++) {
150 Field* field = table->getField(i);
151 ulint ipos;
152 ulint ilen;
153 const unsigned char* ifield;
154
155 field->reset();
156
157 ipos = dict_index_get_nth_col_pos(index, i);
158
159 if (UNIV_UNLIKELY(ipos == ULINT_UNDEFINED)) {
160null_field:
161 field->set_null();
162 continue;
163 }
164
165 ifield = rec_get_nth_field(rec, offsets, ipos, &ilen);
166
167 /* Assign the NULL flag */
168 if (ilen == UNIV_SQL_NULL) {
169 ut_ad(field->real_maybe_null());
170 goto null_field;
171 }
172
173 field->set_notnull();
174
177 dict_index_get_nth_field(index, ipos)),
178 ifield, ilen, field);
179 }
180}
181
182/*************************************************************/
184UNIV_INTERN
185void
187/*===============*/
188 Table* table)
189{
190 uint n_fields = table->getShare()->sizeFields();
191 uint i;
192
193 for (i = 0; i < n_fields; i++) {
194 table->getField(i)->set_default();
195 }
196}
197
198#if 0 // This is a part of the fast index code.
199/******************************************************************/
201static
202void
203innobase_convert_tablename(
204/*=======================*/
205 char* s)
206{
207
208 char* slash = strchr(s, '/');
209
210 if (slash) {
211 char* t;
212 /* Temporarily replace the '/' with NUL. */
213 *slash = 0;
214 strncpy(s, s, slash - s + 1);
215
216 t = s + strlen(s);
217 ut_ad(slash >= t);
218 /* Append a '.' after the database name. */
219 *t++ = '.';
220 slash++;
221 /* Convert the table name. */
222 strncpy(t, slash, slash - t + strlen(slash));
223 }
224}
225
226
227/*******************************************************************/
230static
231int
232innobase_check_index_keys(
233/*======================*/
234 const KeyInfo* key_info,
235 ulint num_of_keys,
237 const dict_table_t* table)
238{
239 ulint key_num;
240
241 ut_ad(key_info);
242 ut_ad(num_of_keys);
243
244 for (key_num = 0; key_num < num_of_keys; key_num++) {
245 const KeyInfo& key = key_info[key_num];
246
247 /* Check that the same index name does not appear
248 twice in indexes to be created. */
249
250 for (ulint i = 0; i < key_num; i++) {
251 const KeyInfo& key2 = key_info[i];
252
253 if (0 == strcmp(key.name, key2.name)) {
254 my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0),
255 key.name);
256
257 return(ER_WRONG_NAME_FOR_INDEX);
258 }
259 }
260
261 /* Check that the same index name does not already exist. */
262
263 for (const dict_index_t* index
264 = dict_table_get_first_index(table);
265 index; index = dict_table_get_next_index(index)) {
266
267 if (0 == strcmp(key.name, index->name)) {
268 my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0),
269 key.name);
270
271 return(ER_WRONG_NAME_FOR_INDEX);
272 }
273 }
274
275 /* Check that MySQL does not try to create a column
276 prefix index field on an inappropriate data type and
277 that the same column does not appear twice in the index. */
278
279 for (ulint i = 0; i < key.key_parts; i++) {
280 const KeyPartInfo& key_part1
281 = key.key_part[i];
282 const Field* field
283 = key_part1.field;
284 ibool is_unsigned;
285
287 &is_unsigned, field)) {
288 default:
289 break;
290 case DATA_INT:
291 case DATA_FLOAT:
292 case DATA_DOUBLE:
293 case DATA_DECIMAL:
294 if (field->type() == DRIZZLE_TYPE_VARCHAR) {
295 if (key_part1.length
296 >= field->pack_length()
297 - ((Field_varstring*) field)
298 ->length_bytes) {
299 break;
300 }
301 } else {
302 if (key_part1.length
303 >= field->pack_length()) {
304 break;
305 }
306 }
307
308 my_error(ER_WRONG_KEY_COLUMN, MYF(0),
309 field->field_name);
310 return(ER_WRONG_KEY_COLUMN);
311 }
312
313 for (ulint j = 0; j < i; j++) {
314 const KeyPartInfo& key_part2
315 = key.key_part[j];
316
317 if (strcmp(key_part1.field->field_name,
318 key_part2.field->field_name)) {
319 continue;
320 }
321
322 my_error(ER_WRONG_KEY_COLUMN, MYF(0),
323 key_part1.field->field_name);
324 return(ER_WRONG_KEY_COLUMN);
325 }
326 }
327 }
328
329 return(0);
330}
331
332/*******************************************************************/
334static
335void
336innobase_create_index_field_def(
337/*============================*/
338 KeyPartInfo* key_part,
339 mem_heap_t* heap,
340 merge_index_field_t* index_field)
342{
343 Field* field;
344 ibool is_unsigned;
345 ulint col_type;
346
347 ut_ad(key_part);
348 ut_ad(index_field);
349
350 field = key_part->field;
351 ut_a(field);
352
353 col_type = get_innobase_type_from_mysql_type(&is_unsigned, field);
354
355 if (DATA_BLOB == col_type
356 || (key_part->length < field->pack_length()
357 && field->type() != DRIZZLE_TYPE_VARCHAR)
358 || (field->type() == DRIZZLE_TYPE_VARCHAR
359 && key_part->length < field->pack_length()
360 - ((Field_varstring*)field)->length_bytes)) {
361
362 index_field->prefix_len = key_part->length;
363 } else {
364 index_field->prefix_len = 0;
365 }
366
367 index_field->field_name = mem_heap_strdup(heap, field->field_name);
368
369 return;
370}
371
372/*******************************************************************/
374static
375void
376innobase_create_index_def(
377/*======================*/
378 KeyInfo* key,
379 bool new_primary,
382 bool key_primary,
384 merge_index_def_t* index,
385 mem_heap_t* heap)
387{
388 ulint i;
389 ulint len;
390 ulint n_fields = key->key_parts;
391 char* index_name;
392
393 index->fields = (merge_index_field_t*) mem_heap_alloc(
394 heap, n_fields * sizeof *index->fields);
395
396 index->ind_type = 0;
397 index->n_fields = n_fields;
398 len = strlen(key->name) + 1;
399 index->name = index_name = (char*) mem_heap_alloc(heap,
400 len + !new_primary);
401
402 if (UNIV_LIKELY(!new_primary)) {
403 *index_name++ = TEMP_INDEX_PREFIX;
404 }
405
406 memcpy(index_name, key->name, len);
407
408 if (key->flags & HA_NOSAME) {
409 index->ind_type |= DICT_UNIQUE;
410 }
411
412 if (key_primary) {
413 index->ind_type |= DICT_CLUSTERED;
414 }
415
416 for (i = 0; i < n_fields; i++) {
417 innobase_create_index_field_def(&key->key_part[i], heap,
418 &index->fields[i]);
419 }
420
421 return;
422}
423
424/*******************************************************************/
426static
427void
428innobase_copy_index_field_def(
429/*==========================*/
430 const dict_field_t* field,
431 merge_index_field_t* index_field)
432{
433 assert(field != NULL);
434 assert(index_field != NULL);
435
436 index_field->field_name = field->name;
437 index_field->prefix_len = field->prefix_len;
438
439 return;
440}
441
442/*******************************************************************/
444static
445void
446innobase_copy_index_def(
447/*====================*/
448 const dict_index_t* index,
449 merge_index_def_t* new_index,
450 mem_heap_t* heap)
451{
452 ulint n_fields;
453 ulint i;
454
455 /* Note that we take only those fields that user defined to be
456 in the index. In the internal representation more colums were
457 added and those colums are not copied .*/
458
459 n_fields = index->n_user_defined_cols;
460
462 heap, n_fields * sizeof *new_index->fields);
463
464 /* When adding a PRIMARY KEY, we may convert a previous
465 clustered index to a secondary index (UNIQUE NOT NULL). */
466 new_index->ind_type = index->type & ~DICT_CLUSTERED;
467 new_index->n_fields = n_fields;
468 new_index->name = index->name;
469
470 for (i = 0; i < n_fields; i++) {
471 innobase_copy_index_field_def(&index->fields[i],
472 &new_index->fields[i]);
473 }
474
475 return;
476}
477
478/*******************************************************************/
495
496static
498innobase_create_key_def(
499/*====================*/
500 trx_t* trx,
501 const dict_table_t*table,
502 mem_heap_t* heap,
504 KeyInfo* key_info,
505 ulint& n_keys)
507{
508 ulint i = 0;
509 merge_index_def_t* indexdef;
510 merge_index_def_t* indexdefs;
511 bool new_primary;
512
513 indexdef = indexdefs = (merge_index_def_t*)
514 mem_heap_alloc(heap, sizeof *indexdef
515 * (n_keys + UT_LIST_GET_LEN(table->indexes)));
516
517 /* If there is a primary key, it is always the first index
518 defined for the table. */
519
520 new_primary = !system_charset_info->strcasecmp(key_info->name, "PRIMARY");
521
522 /* If there is a UNIQUE INDEX consisting entirely of NOT NULL
523 columns and if the index does not contain column prefix(es)
524 (only prefix/part of the column is indexed), MySQL will treat the
525 index as a PRIMARY KEY unless the table already has one. */
526
527 if (!new_primary && (key_info->flags & HA_NOSAME)
528 && (!(key_info->flags & HA_KEY_HAS_PART_KEY_SEG))
530 uint key_part = key_info->key_parts;
531
532 new_primary = TRUE;
533
534 while (key_part--) {
535 if (key_info->key_part[key_part].null_bit == 0) {
536 new_primary = FALSE;
537 break;
538 }
539 }
540 }
541
542 if (new_primary) {
543 const dict_index_t* index;
544
545 /* Create the PRIMARY key index definition */
546 innobase_create_index_def(&key_info[i++], TRUE, TRUE,
547 indexdef++, heap);
548
549 row_mysql_lock_data_dictionary(trx);
550
551 index = dict_table_get_first_index(table);
552
553 /* Copy the index definitions of the old table. Skip
554 the old clustered index if it is a generated clustered
555 index or a PRIMARY KEY. If the clustered index is a
556 UNIQUE INDEX, it must be converted to a secondary index. */
557
558 if (dict_index_get_nth_col(index, 0)->mtype == DATA_SYS
559 || !system_charset_info->strcasecmp(index->name, "PRIMARY"))
560 {
561 index = dict_table_get_next_index(index);
562 }
563
564 while (index) {
565 innobase_copy_index_def(index, indexdef++, heap);
566 index = dict_table_get_next_index(index);
567 }
568
570 }
571
572 /* Create definitions for added secondary indexes. */
573
574 while (i < n_keys) {
575 innobase_create_index_def(&key_info[i++], new_primary, FALSE,
576 indexdef++, heap);
577 }
578
579 n_keys = indexdef - indexdefs;
580
581 return(indexdefs);
582}
583
584/*******************************************************************/
587static
588char*
589innobase_create_temporary_tablename(
590/*================================*/
591 mem_heap_t* heap,
592 char id,
593 const char* table_name)
594{
595 char* name;
596 ulint len;
597 static const char suffix[] = "@0023 "; /* "# " */
598
599 len = strlen(table_name);
600
601 name = (char*) mem_heap_alloc(heap, len + sizeof suffix);
602 memcpy(name, table_name, len);
603 memcpy(name + len, suffix, sizeof suffix);
604 name[len + (sizeof suffix - 2)] = id;
605
606 return(name);
607}
608
609
610/*******************************************************************/
613UNIV_INTERN
614int
615ha_innobase::add_index(
616/*===================*/
617 Session *session,
618 Table* i_table,
619 KeyInfo* key_info,
620 uint num_of_keys)
621{
622 dict_index_t** index;
623 dict_table_t* innodb_table;
624 dict_table_t* indexed_table;
625 merge_index_def_t* index_defs;
626 mem_heap_t* heap;
627 trx_t* trx;
628 ulint num_of_idx;
629 ulint num_created = 0;
630 ibool dict_locked = FALSE;
631 ulint new_primary;
632 int error;
633
634 ut_a(i_table);
635 ut_a(key_info);
636 ut_a(num_of_keys);
637
638 if (srv_created_new_raw || srv_force_recovery) {
639 return(HA_ERR_WRONG_COMMAND);
640 }
641
642 update_session(session);
643
644 heap = mem_heap_create(1024);
645
646 /* In case MySQL calls this in the middle of a SELECT query, release
647 possible adaptive hash latch to avoid deadlocks of threads. */
650
651 /* Create a background transaction for the operations on
652 the data dictionary tables. */
655
656 innodb_table = indexed_table
657 = dict_table_get(prebuilt->table->name, FALSE);
658
659 if (UNIV_UNLIKELY(!innodb_table)) {
660 error = HA_ERR_NO_SUCH_TABLE;
661 goto err_exit;
662 }
663
664 /* Check if the index name is reserved. */
665 if (innobase_index_name_is_reserved(trx, key_info, num_of_keys)) {
666 error = -1;
667 } else {
668 /* Check that index keys are sensible */
669 error = innobase_check_index_keys(key_info, num_of_keys,
670 innodb_table);
671 }
672
673 if (UNIV_UNLIKELY(error)) {
674err_exit:
675 mem_heap_free(heap);
679 return(error);
680 }
681
682 /* Create table containing all indexes to be built in this
683 alter table add index so that they are in the correct order
684 in the table. */
685
686 num_of_idx = num_of_keys;
687
688 index_defs = innobase_create_key_def(
689 trx, innodb_table, heap, key_info, num_of_idx);
690
691 new_primary = DICT_CLUSTERED & index_defs[0].ind_type;
692
693 /* Allocate memory for dictionary index definitions */
694
695 index = (dict_index_t**) mem_heap_alloc(
696 heap, num_of_idx * sizeof *index);
697
698 /* Flag this transaction as a dictionary operation, so that
699 the data dictionary will be locked in crash recovery. */
701
702 /* Acquire a lock on the table before creating any indexes. */
703 error = row_merge_lock_table(prebuilt->trx, innodb_table,
704 new_primary ? LOCK_X : LOCK_S);
705
706 if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
707
708 goto error_handling;
709 }
710
711 /* Latch the InnoDB data dictionary exclusively so that no deadlocks
712 or lock waits can happen in it during an index create operation. */
713
714 row_mysql_lock_data_dictionary(trx);
715 dict_locked = TRUE;
716
717 ut_d(dict_table_check_for_dup_indexes(innodb_table, FALSE));
718
719 /* If a new primary key is defined for the table we need
720 to drop the original table and rebuild all indexes. */
721
722 if (UNIV_UNLIKELY(new_primary)) {
723 /* This transaction should be the only one
724 operating on the table. */
725 ut_a(innodb_table->n_mysql_handles_opened == 1);
726
727 char* new_table_name = innobase_create_temporary_tablename(
728 heap, '1', innodb_table->name);
729
730 /* Clone the table. */
732 indexed_table = row_merge_create_temporary_table(
733 new_table_name, index_defs, innodb_table, trx);
734
735 if (!indexed_table) {
736
737 switch (trx->error_state) {
738 case DB_TABLESPACE_ALREADY_EXISTS:
739 case DB_DUPLICATE_KEY:
740 innobase_convert_tablename(new_table_name);
741 my_error(HA_ERR_TABLE_EXIST, MYF(0),
742 new_table_name);
743 error = HA_ERR_TABLE_EXIST;
744 break;
745 default:
747 trx->error_state, innodb_table->flags,
749 }
750
751 ut_d(dict_table_check_for_dup_indexes(innodb_table,
752 FALSE));
754 goto err_exit;
755 }
756
757 trx->table_id = indexed_table->id;
758 }
759
760 /* Create the indexes in SYS_INDEXES and load into dictionary. */
761
762 for (ulint i = 0; i < num_of_idx; i++) {
763
764 index[i] = row_merge_create_index(trx, indexed_table,
765 &index_defs[i]);
766
767 if (!index[i]) {
768 error = trx->error_state;
769 goto error_handling;
770 }
771
772 num_created++;
773 }
774
775 ut_ad(error == DB_SUCCESS);
776
777 /* Commit the data dictionary transaction in order to release
778 the table locks on the system tables. This means that if
779 MySQL crashes while creating a new primary key inside
780 row_merge_build_indexes(), indexed_table will not be dropped
781 by trx_rollback_active(). It will have to be recovered or
782 dropped by the database administrator. */
784
786 dict_locked = FALSE;
787
788 ut_a(trx->n_active_thrs == 0);
789 ut_a(UT_LIST_GET_LEN(trx->signals) == 0);
790
791 if (UNIV_UNLIKELY(new_primary)) {
792 /* A primary key is to be built. Acquire an exclusive
793 table lock also on the table that is being created. */
794 ut_ad(indexed_table != innodb_table);
795
796 error = row_merge_lock_table(prebuilt->trx, indexed_table,
797 LOCK_X);
798
799 if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
800
801 goto error_handling;
802 }
803 }
804
805 /* Read the clustered index of the table and build indexes
806 based on this information using temporary files and merge sort. */
808 innodb_table, indexed_table,
809 index, num_of_idx, i_table);
810
811error_handling:
812
813 /* After an error, remove all those index definitions from the
814 dictionary which were defined. */
815
816 switch (error) {
817 const char* old_name;
818 char* tmp_name;
819 case DB_SUCCESS:
820 ut_a(!dict_locked);
821 row_mysql_lock_data_dictionary(trx);
822 dict_locked = TRUE;
823
824 ut_d(dict_table_check_for_dup_indexes(prebuilt->table, TRUE));
825
826 if (!new_primary) {
827 error = row_merge_rename_indexes(trx, indexed_table);
828
829 if (error != DB_SUCCESS) {
830 row_merge_drop_indexes(trx, indexed_table,
831 index, num_created);
832 }
833
834 goto convert_error;
835 }
836
837 /* If a new primary key was defined for the table and
838 there was no error at this point, we can now rename
839 the old table as a temporary table, rename the new
840 temporary table as the old table and drop the old table. */
841 old_name = innodb_table->name;
842 tmp_name = innobase_create_temporary_tablename(heap, '2',
843 old_name);
844
845 error = row_merge_rename_tables(innodb_table, indexed_table,
846 tmp_name, trx);
847
848 if (error != DB_SUCCESS) {
849
850 row_merge_drop_table(trx, indexed_table);
851
852 switch (error) {
853 case DB_TABLESPACE_ALREADY_EXISTS:
854 case DB_DUPLICATE_KEY:
855 innobase_convert_tablename(tmp_name);
856 my_error(HA_ERR_TABLE_EXIST, MYF(0), tmp_name);
857 error = HA_ERR_TABLE_EXIST;
858 break;
859 default:
860 goto convert_error;
861 }
862 break;
863 }
864
867 prebuilt = row_create_prebuilt(indexed_table);
868
869 indexed_table->n_mysql_handles_opened++;
870
871 error = row_merge_drop_table(trx, innodb_table);
872 innodb_table = indexed_table;
873 goto convert_error;
874
875 case DB_TOO_BIG_RECORD:
876 my_error(HA_ERR_TO_BIG_ROW, MYF(0));
877 goto error;
878 case DB_PRIMARY_KEY_IS_NULL:
879 my_error(ER_PRIMARY_CANT_HAVE_NULL, MYF(0));
880 /* fall through */
881 case DB_DUPLICATE_KEY:
882error:
883 prebuilt->trx->error_info = NULL;
884 /* fall through */
885 default:
886 trx->error_state = DB_SUCCESS;
887
888 if (new_primary) {
889 if (indexed_table != innodb_table) {
890 row_merge_drop_table(trx, indexed_table);
891 }
892 } else {
893 if (!dict_locked) {
894 row_mysql_lock_data_dictionary(trx);
895 dict_locked = TRUE;
896 }
897
898 row_merge_drop_indexes(trx, indexed_table,
899 index, num_created);
900 }
901
902convert_error:
903 if (error == DB_SUCCESS) {
904 /* Build index is successful. We will need to
905 rebuild index translation table. Reset the
906 index entry count in the translation table
907 to zero, so that translation table will be rebuilt */
908 share->idx_trans_tbl.index_count = 0;
909 }
910
911 error = convert_error_code_to_mysql(error,
912 innodb_table->flags,
914 }
915
916 mem_heap_free(heap);
918 if (prebuilt->trx) {
920 }
921
922 if (dict_locked) {
923 ut_d(dict_table_check_for_dup_indexes(innodb_table, FALSE));
925 }
926
928
929 /* There might be work for utility threads.*/
931
932 return(error);
933}
934
935/*******************************************************************/
938UNIV_INTERN
939int
940ha_innobase::prepare_drop_index(
941/*============================*/
942 Session *session,
943 Table* i_table,
944 uint* key_num,
945 uint num_of_keys)
946{
947 trx_t* trx;
948 int err = 0;
949 uint n_key;
950
951 ut_ad(i_table);
952 ut_ad(key_num);
953 ut_ad(num_of_keys);
954 if (srv_created_new_raw || srv_force_recovery) {
955 return(HA_ERR_WRONG_COMMAND);
956 }
957
958 update_session(session);
959
961 trx = prebuilt->trx;
962
963 /* Test and mark all the indexes to be dropped */
964
965 row_mysql_lock_data_dictionary(trx);
966 ut_d(dict_table_check_for_dup_indexes(prebuilt->table, FALSE));
967
968 /* Check that none of the indexes have previously been flagged
969 for deletion. */
970 {
971 const dict_index_t* index
972 = dict_table_get_first_index(prebuilt->table);
973 do {
974 ut_a(!index->to_be_dropped);
975 index = dict_table_get_next_index(index);
976 } while (index);
977 }
978
979 for (n_key = 0; n_key < num_of_keys; n_key++) {
980 const KeyInfo* key;
981 dict_index_t* index;
982
983 key = i_table->key_info + key_num[n_key];
984 index = dict_table_get_index_on_name_and_min_id(
985 prebuilt->table, key->name);
986
987 if (!index) {
988 errmsg_printf(ERRMSG_LVL_ERROR, "InnoDB could not find key n:o %u "
989 "with name %s for table %s",
990 key_num[n_key],
991 key ? key->name : "NULL",
992 prebuilt->table->name);
993
994 err = HA_ERR_KEY_NOT_FOUND;
995 goto func_exit;
996 }
997
998 /* Refuse to drop the clustered index. It would be
999 better to automatically generate a clustered index,
1000 but drizzled::alter_table() will call this method only
1001 after ha_innobase::add_index(). */
1002
1003 if (dict_index_is_clust(index)) {
1004 my_error(ER_REQUIRES_PRIMARY_KEY, MYF(0));
1005 err = -1;
1006 goto func_exit;
1007 }
1008
1009 index->to_be_dropped = TRUE;
1010 }
1011
1012 /* If FOREIGN_KEY_CHECKS = 1 you may not drop an index defined
1013 for a foreign key constraint because InnoDB requires that both
1014 tables contain indexes for the constraint. Such index can
1015 be dropped only if FOREIGN_KEY_CHECKS is set to 0.
1016 Note that CREATE INDEX id ON table does a CREATE INDEX and
1017 DROP INDEX, and we can ignore here foreign keys because a
1018 new index for the foreign key has already been created.
1019
1020 We check for the foreign key constraints after marking the
1021 candidate indexes for deletion, because when we check for an
1022 equivalent foreign index we don't want to select an index that
1023 is later deleted. */
1024
1025 if (trx->check_foreigns
1026 && session_sql_command(user_session) != SQLCOM_CREATE_INDEX) {
1027 dict_index_t* index;
1028
1029 for (index = dict_table_get_first_index(prebuilt->table);
1030 index;
1031 index = dict_table_get_next_index(index)) {
1032 dict_foreign_t* foreign;
1033
1034 if (!index->to_be_dropped) {
1035
1036 continue;
1037 }
1038
1039 /* Check if the index is referenced. */
1040 foreign = dict_table_get_referenced_constraint(
1041 prebuilt->table, index);
1042
1043 if (foreign) {
1044index_needed:
1046 trx,
1047 "Index needed in foreign key "
1048 "constraint");
1049
1050 trx->error_info = index;
1051
1052 err = HA_ERR_DROP_INDEX_FK;
1053 break;
1054 } else {
1055 /* Check if this index references some
1056 other table */
1057 foreign = dict_table_get_foreign_constraint(
1058 prebuilt->table, index);
1059
1060 if (foreign) {
1061 ut_a(foreign->foreign_index == index);
1062
1063 /* Search for an equivalent index that
1064 the foreign key constraint could use
1065 if this index were to be deleted. */
1066 if (!dict_foreign_find_equiv_index(
1067 foreign)) {
1068
1069 goto index_needed;
1070 }
1071 }
1072 }
1073 }
1074 } else if (session_sql_command(user_session) == SQLCOM_CREATE_INDEX) {
1075 /* This is a drop of a foreign key constraint index that
1076 was created by MySQL when the constraint was added. MySQL
1077 does this when the user creates an index explicitly which
1078 can be used in place of the automatically generated index. */
1079
1080 dict_index_t* index;
1081
1082 for (index = dict_table_get_first_index(prebuilt->table);
1083 index;
1084 index = dict_table_get_next_index(index)) {
1085 dict_foreign_t* foreign;
1086
1087 if (!index->to_be_dropped) {
1088
1089 continue;
1090 }
1091
1092 /* Check if this index references some other table */
1093 foreign = dict_table_get_foreign_constraint(
1094 prebuilt->table, index);
1095
1096 if (foreign == NULL) {
1097
1098 continue;
1099 }
1100
1101 ut_a(foreign->foreign_index == index);
1102
1103 /* Search for an equivalent index that the
1104 foreign key constraint could use if this index
1105 were to be deleted. */
1106
1107 if (!dict_foreign_find_equiv_index(foreign)) {
1109 trx,
1110 "Index needed in foreign key "
1111 "constraint");
1112
1113 trx->error_info = foreign->foreign_index;
1114
1115 err = HA_ERR_DROP_INDEX_FK;
1116 break;
1117 }
1118 }
1119 }
1120
1121func_exit:
1122 if (err) {
1123 /* Undo our changes since there was some sort of error. */
1124 dict_index_t* index
1125 = dict_table_get_first_index(prebuilt->table);
1126
1127 do {
1128 index->to_be_dropped = FALSE;
1129 index = dict_table_get_next_index(index);
1130 } while (index);
1131 }
1132
1133 ut_d(dict_table_check_for_dup_indexes(prebuilt->table, FALSE));
1135
1136 return(err);
1137}
1138
1139/*******************************************************************/
1142UNIV_INTERN
1143int
1144ha_innobase::final_drop_index(
1145/*==========================*/
1146 Session *session,
1147 Table* )
1148{
1149 dict_index_t* index;
1150 trx_t* trx;
1151 int err;
1152
1153 if (srv_created_new_raw || srv_force_recovery) {
1154 return(HA_ERR_WRONG_COMMAND);
1155 }
1156
1157 update_session(session);
1158
1161
1162 /* Create a background transaction for the operations on
1163 the data dictionary tables. */
1166
1167 /* Flag this transaction as a dictionary operation, so that
1168 the data dictionary will be locked in crash recovery. */
1170
1171 /* Lock the table exclusively, to ensure that no active
1172 transaction depends on an index that is being dropped. */
1174 row_merge_lock_table(prebuilt->trx, prebuilt->table, LOCK_X),
1175 prebuilt->table->flags, user_session);
1176
1177 row_mysql_lock_data_dictionary(trx);
1178 ut_d(dict_table_check_for_dup_indexes(prebuilt->table, FALSE));
1179
1180 if (UNIV_UNLIKELY(err)) {
1181
1182 /* Unmark the indexes to be dropped. */
1183 for (index = dict_table_get_first_index(prebuilt->table);
1184 index; index = dict_table_get_next_index(index)) {
1185
1186 index->to_be_dropped = FALSE;
1187 }
1188
1189 goto func_exit;
1190 }
1191
1192 /* Drop indexes marked to be dropped */
1193
1194 index = dict_table_get_first_index(prebuilt->table);
1195
1196 while (index) {
1197 dict_index_t* next_index;
1198
1199 next_index = dict_table_get_next_index(index);
1200
1201 if (index->to_be_dropped) {
1202
1203 row_merge_drop_index(index, prebuilt->table, trx);
1204 }
1205
1206 index = next_index;
1207 }
1208
1209 /* Check that all flagged indexes were dropped. */
1210 for (index = dict_table_get_first_index(prebuilt->table);
1211 index; index = dict_table_get_next_index(index)) {
1212 ut_a(!index->to_be_dropped);
1213 }
1214
1215 /* We will need to rebuild index translation table. Set
1216 valid index entry count in the translation table to zero */
1217 share->idx_trans_tbl.index_count = 0;
1218
1219func_exit:
1220 ut_d(dict_table_check_for_dup_indexes(prebuilt->table, FALSE));
1224
1225 /* Flush the log to reduce probability that the .frm files and
1226 the InnoDB data dictionary get out-of-sync if the user runs
1227 with innodb_flush_log_at_trx_commit = 0 */
1228
1230
1231 trx_free_for_mysql(trx);
1232
1233 /* Tell the InnoDB server that there might be work for
1234 utility threads: */
1235
1237
1238 return(err);
1239}
1240#endif
const char * field_name
Definition field.h:104
row_prebuilt_t * prebuilt
Definition ha_innodb.h:82
UNIV_INTERN void update_session(Session *session)
INNOBASE_SHARE * share
Definition ha_innodb.h:89
Session * user_session
Definition ha_innodb.h:85
UNIV_INLINE const dict_col_t * dict_index_get_nth_col(const dict_index_t *index, ulint pos)
UNIV_INLINE ulint dict_index_is_clust(const dict_index_t *index) __attribute__((pure))
UNIV_INLINE const dict_col_t * dict_field_get_col(const dict_field_t *field)
UNIV_INLINE ulint dict_table_get_n_user_cols(const dict_table_t *table)
#define DICT_UNIQUE
Definition dict0mem.h:52
#define DICT_CLUSTERED
Definition dict0mem.h:51
UNIV_INTERN trx_t * innobase_trx_allocate(Session *session)
UNIV_INTERN ulint get_innobase_type_from_mysql_type(ulint *unsigned_flag, const void *f)
UNIV_INTERN int convert_error_code_to_mysql(int error, ulint flags, Session *session)
UNIV_INTERN void innobase_rec_to_mysql(Table *table, const rec_t *rec, const dict_index_t *index, const ulint *offsets)
static void innobase_col_to_mysql(const dict_col_t *col, const unsigned char *data, ulint len, Field *field)
UNIV_INTERN void innobase_rec_reset(Table *table)
UNIV_INTERN void log_buffer_flush_to_disk(void)
Definition log0log.cc:1601
UNIV_INTERN char * mem_heap_strdup(mem_heap_t *heap, const char *str)
Definition mem0mem.cc:104
#define mem_heap_create(N)
Definition mem0mem.h:91
UNIV_INLINE void * mem_heap_alloc(mem_heap_t *heap, ulint n)
#define mem_heap_free(heap)
Definition mem0mem.h:111
UNIV_INTERN dict_index_t * row_merge_create_index(trx_t *trx, dict_table_t *table, const merge_index_def_t *index_def)
struct merge_index_def_struct merge_index_def_t
Definition row0merge.h:64
UNIV_INTERN void row_merge_drop_indexes(trx_t *trx, dict_table_t *table, dict_index_t **index, ulint num_created)
UNIV_INTERN ulint row_merge_rename_tables(dict_table_t *old_table, dict_table_t *new_table, const char *tmp_name, trx_t *trx)
UNIV_INTERN ulint row_merge_build_indexes(trx_t *trx, dict_table_t *old_table, dict_table_t *new_table, dict_index_t **indexes, ulint n_indexes, TABLE *table)
UNIV_INTERN ulint row_merge_lock_table(trx_t *trx, dict_table_t *table, enum lock_mode mode)
struct merge_index_field_struct merge_index_field_t
Definition row0merge.h:51
UNIV_INTERN dict_table_t * row_merge_create_temporary_table(const char *table_name, const merge_index_def_t *index_def, const dict_table_t *table, trx_t *trx)
UNIV_INTERN void row_merge_drop_index(dict_index_t *index, dict_table_t *table, trx_t *trx)
UNIV_INTERN ulint row_merge_drop_table(trx_t *trx, dict_table_t *table)
UNIV_INTERN ulint row_merge_rename_indexes(trx_t *trx, dict_table_t *table)
UNIV_INTERN void row_prebuilt_free(row_prebuilt_t *prebuilt, ibool dict_locked)
Definition row0mysql.cc:712
UNIV_INTERN ibool row_table_got_default_clust_index(const dict_table_t *table)
UNIV_INTERN row_prebuilt_t * row_create_prebuilt(dict_table_t *table)
Definition row0mysql.cc:650
UNIV_INTERN byte * row_mysql_store_true_var_len(byte *dest, ulint len, ulint lenlen)
Definition row0mysql.cc:143
UNIV_INTERN void row_mysql_store_blob_ref(byte *dest, ulint col_len, const void *data, ulint len)
Definition row0mysql.cc:196
UNIV_INTERN void row_mysql_unlock_data_dictionary(trx_t *trx)
UNIV_INTERN void srv_active_wake_master_thread(void)
Definition srv0srv.cc:2617
unsigned mbminmaxlen
Definition dict0mem.h:284
unsigned mtype
Definition dict0mem.h:264
dict_index_t * foreign_index
Definition dict0mem.h:440
unsigned to_be_dropped
Definition dict0mem.h:357
ulint n_mysql_handles_opened
Definition dict0mem.h:519
table_id_t id
Definition dict0mem.h:469
const char * name
Definition row0merge.h:55
merge_index_field_t * fields
Definition row0merge.h:60
const char * field_name
Definition row0merge.h:47
table_id_t table_id
Definition trx0trx.h:556
ulint error_state
Definition trx0trx.h:601
const dict_index_t * error_info
Definition trx0trx.h:606
ulint n_active_thrs
Definition trx0trx.h:619
UNIV_INTERN int trx_general_rollback_for_mysql(trx_t *trx, trx_savept_t *savept)
Definition trx0roll.cc:65
UNIV_INLINE void trx_start_if_not_started(trx_t *trx)
UNIV_INTERN void trx_set_detailed_error(trx_t *trx, const char *msg)
Definition trx0trx.cc:63
UNIV_INTERN void trx_search_latch_release_if_reserved(trx_t *trx)
Definition trx0trx.cc:244
UNIV_INTERN ulint trx_commit_for_mysql(trx_t *trx)
Definition trx0trx.cc:1596
@ TRX_DICT_OP_TABLE
Definition trx0trx.h:370
@ TRX_DICT_OP_INDEX
Definition trx0trx.h:374
UNIV_INTERN void trx_free_for_mysql(trx_t *trx)
Definition trx0trx.cc:342
UNIV_INLINE void trx_set_dict_operation(trx_t *trx, enum trx_dict_op op)
#define ut_ad(EXPR)
Definition ut0dbg.h:127
#define ut_d(EXPR)
Definition ut0dbg.h:129
#define ut_a(EXPR)
Definition ut0dbg.h:105
#define UT_LIST_GET_LEN(BASE)
Definition ut0lst.h:216
#define TEMP_INDEX_PREFIX
Definition ut0ut.h:51