MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_partition_admin.cc
1 /* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "sql_parse.h" // check_access,
17  // check_merge_table_access
18  // check_one_table_access
19 #include "sql_table.h" // mysql_alter_table, etc.
20 #include "sql_cmd.h" // Sql_cmd
21 #include "sql_alter.h" // Sql_cmd_alter_table
22 #include "sql_partition.h" // struct partition_info, etc.
23 #include "sql_base.h" // open_and_lock_tables, etc
24 #include "debug_sync.h" // DEBUG_SYNC
25 #include "sql_truncate.h" // mysql_truncate_table,
26  // Sql_cmd_truncate_table
27 #include "sql_admin.h" // Sql_cmd_Analyze/Check/.._table
28 #include "sql_partition_admin.h" // Alter_table_*_partition
29 #ifdef WITH_PARTITION_STORAGE_ENGINE
30 #include "ha_partition.h" // ha_partition
31 #endif
32 #include "sql_base.h" // open_and_lock_tables
33 
34 #ifndef WITH_PARTITION_STORAGE_ENGINE
35 
37 {
38  DBUG_ENTER("Sql_cmd_partition_unsupported::execute");
39  /* error, partitioning support not compiled in... */
40  my_error(ER_FEATURE_DISABLED, MYF(0), "partitioning",
41  "--with-plugin-partition");
42  DBUG_RETURN(TRUE);
43 }
44 
45 #else
46 
48 {
49  /* Moved from mysql_execute_command */
50  LEX *lex= thd->lex;
51  /* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
52  SELECT_LEX *select_lex= &lex->select_lex;
53  /* first table of first SELECT_LEX */
54  TABLE_LIST *first_table= (TABLE_LIST*) select_lex->table_list.first;
55  /*
56  Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
57  so we have to use a copy of this structure to make execution
58  prepared statement- safe. A shallow copy is enough as no memory
59  referenced from this structure will be modified.
60  @todo move these into constructor...
61  */
62  HA_CREATE_INFO create_info(lex->create_info);
63  Alter_info alter_info(lex->alter_info, thd->mem_root);
64  ulong priv_needed= ALTER_ACL | DROP_ACL | INSERT_ACL | CREATE_ACL;
65 
66  DBUG_ENTER("Sql_cmd_alter_table_exchange_partition::execute");
67 
68  if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
69  DBUG_RETURN(TRUE);
70 
71  /* Must be set in the parser */
72  DBUG_ASSERT(select_lex->db);
73  /* also check the table to be exchanged with the partition */
74  DBUG_ASSERT(alter_info.flags & Alter_info::ALTER_EXCHANGE_PARTITION);
75 
76  if (check_access(thd, priv_needed, first_table->db,
77  &first_table->grant.privilege,
78  &first_table->grant.m_internal,
79  0, 0) ||
80  check_access(thd, priv_needed, first_table->next_local->db,
81  &first_table->next_local->grant.privilege,
82  &first_table->next_local->grant.m_internal,
83  0, 0))
84  DBUG_RETURN(TRUE);
85 
86  if (check_grant(thd, priv_needed, first_table, FALSE, UINT_MAX, FALSE))
87  DBUG_RETURN(TRUE);
88 
89  /* Not allowed with EXCHANGE PARTITION */
90  DBUG_ASSERT(!create_info.data_file_name && !create_info.index_file_name);
91 
92  thd->enable_slow_log= opt_log_slow_admin_statements;
93  DBUG_RETURN(exchange_partition(thd, first_table, &alter_info));
94 }
95 
96 
104 static bool check_exchange_partition(TABLE *table, TABLE *part_table)
105 {
106  DBUG_ENTER("check_exchange_partition");
107 
108  /* Both tables must exist */
109  if (!part_table || !table)
110  {
111  my_error(ER_CHECK_NO_SUCH_TABLE, MYF(0));
112  DBUG_RETURN(TRUE);
113  }
114 
115  /* The first table must be partitioned, and the second must not */
116  if (!part_table->part_info)
117  {
118  my_error(ER_PARTITION_MGMT_ON_NONPARTITIONED, MYF(0));
119  DBUG_RETURN(TRUE);
120  }
121  if (table->part_info)
122  {
123  my_error(ER_PARTITION_EXCHANGE_PART_TABLE, MYF(0),
124  table->s->table_name.str);
125  DBUG_RETURN(TRUE);
126  }
127 
128  if (part_table->file->ht != partition_hton)
129  {
130  /*
131  Only allowed on partitioned tables throught the generic ha_partition
132  handler, i.e not yet for native partitioning (NDB).
133  */
134  my_error(ER_PARTITION_MGMT_ON_NONPARTITIONED, MYF(0));
135  DBUG_RETURN(TRUE);
136  }
137 
138  if (table->file->ht != part_table->part_info->default_engine_type)
139  {
140  my_error(ER_MIX_HANDLER_ERROR, MYF(0));
141  DBUG_RETURN(TRUE);
142  }
143 
144  /* Verify that table is not tmp table, partitioned tables cannot be tmp. */
145  if (table->s->tmp_table != NO_TMP_TABLE)
146  {
147  my_error(ER_PARTITION_EXCHANGE_TEMP_TABLE, MYF(0),
148  table->s->table_name.str);
149  DBUG_RETURN(TRUE);
150  }
151 
152  /* The table cannot have foreign keys constraints or be referenced */
153  if(!table->file->can_switch_engines())
154  {
155  my_error(ER_PARTITION_EXCHANGE_FOREIGN_KEY, MYF(0),
156  table->s->table_name.str);
157  DBUG_RETURN(TRUE);
158  }
159  DBUG_RETURN(FALSE);
160 }
161 
162 
172 static bool compare_table_with_partition(THD *thd, TABLE *table,
173  TABLE *part_table,
174  partition_element *part_elem)
175 {
176  HA_CREATE_INFO table_create_info, part_create_info;
177  Alter_info part_alter_info;
178  Alter_table_ctx part_alter_ctx; // Not used
179  DBUG_ENTER("compare_table_with_partition");
180 
181  bool metadata_equal= false;
182  memset(&part_create_info, 0, sizeof(HA_CREATE_INFO));
183  memset(&table_create_info, 0, sizeof(HA_CREATE_INFO));
184 
185  update_create_info_from_table(&table_create_info, table);
186  /* get the current auto_increment value */
187  table->file->update_create_info(&table_create_info);
188  /* mark all columns used, since they are used when preparing the new table */
189  part_table->use_all_columns();
190  table->use_all_columns();
191  if (mysql_prepare_alter_table(thd, part_table, &part_create_info,
192  &part_alter_info, &part_alter_ctx))
193  {
194  my_error(ER_TABLES_DIFFERENT_METADATA, MYF(0));
195  DBUG_RETURN(TRUE);
196  }
197  /* db_type is not set in prepare_alter_table */
198  part_create_info.db_type= part_table->part_info->default_engine_type;
199  /*
200  Since we exchange the partition with the table, allow exchanging
201  auto_increment value as well.
202  */
203  part_create_info.auto_increment_value=
204  table_create_info.auto_increment_value;
205 
206  /* Check compatible row_types and set create_info accordingly. */
207  {
208  enum row_type part_row_type= part_table->file->get_row_type();
209  enum row_type table_row_type= table->file->get_row_type();
210  if (part_row_type != table_row_type)
211  {
212  my_error(ER_PARTITION_EXCHANGE_DIFFERENT_OPTION, MYF(0),
213  "ROW_FORMAT");
214  DBUG_RETURN(true);
215  }
216  part_create_info.row_type= table->s->row_type;
217  }
218 
219  /*
220  NOTE: ha_blackhole does not support check_if_compatible_data,
221  so this always fail for blackhole tables.
222  ha_myisam compares pointers to verify that DATA/INDEX DIRECTORY is
223  the same, so any table using data/index_file_name will fail.
224  */
225  if (mysql_compare_tables(table, &part_alter_info, &part_create_info,
226  &metadata_equal))
227  {
228  my_error(ER_TABLES_DIFFERENT_METADATA, MYF(0));
229  DBUG_RETURN(TRUE);
230  }
231 
232  DEBUG_SYNC(thd, "swap_partition_after_compare_tables");
233  if (!metadata_equal)
234  {
235  my_error(ER_TABLES_DIFFERENT_METADATA, MYF(0));
236  DBUG_RETURN(TRUE);
237  }
238  DBUG_ASSERT(table->s->db_create_options ==
239  part_table->s->db_create_options);
240  DBUG_ASSERT(table->s->db_options_in_use ==
241  part_table->s->db_options_in_use);
242 
243  if (table_create_info.avg_row_length != part_create_info.avg_row_length)
244  {
245  my_error(ER_PARTITION_EXCHANGE_DIFFERENT_OPTION, MYF(0),
246  "AVG_ROW_LENGTH");
247  DBUG_RETURN(TRUE);
248  }
249 
250  if (table_create_info.table_options != part_create_info.table_options)
251  {
252  my_error(ER_PARTITION_EXCHANGE_DIFFERENT_OPTION, MYF(0),
253  "TABLE OPTION");
254  DBUG_RETURN(TRUE);
255  }
256 
257  if (table->s->table_charset != part_table->s->table_charset)
258  {
259  my_error(ER_PARTITION_EXCHANGE_DIFFERENT_OPTION, MYF(0),
260  "CHARACTER SET");
261  DBUG_RETURN(TRUE);
262  }
263 
264  /*
265  NOTE: We do not support update of frm-file, i.e. change
266  max/min_rows, data/index_file_name etc.
267  The workaround is to use REORGANIZE PARTITION to rewrite
268  the frm file and then use EXCHANGE PARTITION when they are the same.
269  */
270  if (compare_partition_options(&table_create_info, part_elem))
271  DBUG_RETURN(TRUE);
272 
273  DBUG_RETURN(FALSE);
274 }
275 
276 
315 static bool exchange_name_with_ddl_log(THD *thd,
316  const char *name,
317  const char *from_name,
318  const char *tmp_name,
319  handlerton *ht)
320 {
321  DDL_LOG_ENTRY exchange_entry;
322  DDL_LOG_MEMORY_ENTRY *log_entry= NULL;
323  DDL_LOG_MEMORY_ENTRY *exec_log_entry= NULL;
324  bool error= TRUE;
325  bool error_set= FALSE;
326  handler *file= NULL;
327  DBUG_ENTER("exchange_name_with_ddl_log");
328 
329  if (!(file= get_new_handler(NULL, thd->mem_root, ht)))
330  {
331  mem_alloc_error(sizeof(handler));
332  DBUG_RETURN(TRUE);
333  }
334 
335  /* prepare the action entry */
336  exchange_entry.entry_type= DDL_LOG_ENTRY_CODE;
337  exchange_entry.action_type= DDL_LOG_EXCHANGE_ACTION;
338  exchange_entry.next_entry= 0;
339  exchange_entry.name= name;
340  exchange_entry.from_name= from_name;
341  exchange_entry.tmp_name= tmp_name;
342  exchange_entry.handler_name= ha_resolve_storage_engine_name(ht);
343  exchange_entry.phase= EXCH_PHASE_NAME_TO_TEMP;
344 
345  mysql_mutex_lock(&LOCK_gdl);
346  /*
347  write to the ddl log what to do by:
348  1) write the action entry (i.e. which names to be exchanged)
349  2) write the execution entry with a link to the action entry
350  */
351  DBUG_EXECUTE_IF("exchange_partition_fail_1", goto err_no_action_written;);
352  DBUG_EXECUTE_IF("exchange_partition_abort_1", DBUG_SUICIDE(););
353  if (write_ddl_log_entry(&exchange_entry, &log_entry))
354  goto err_no_action_written;
355 
356  DBUG_EXECUTE_IF("exchange_partition_fail_2", goto err_no_execute_written;);
357  DBUG_EXECUTE_IF("exchange_partition_abort_2", DBUG_SUICIDE(););
358  if (write_execute_ddl_log_entry(log_entry->entry_pos, FALSE, &exec_log_entry))
359  goto err_no_execute_written;
360  /* ddl_log is written and synced */
361 
362  mysql_mutex_unlock(&LOCK_gdl);
363  /*
364  Execute the name exchange.
365  Do one rename, increase the phase, update the action entry and sync.
366  In case of errors in the ddl_log we must fail and let the ddl_log try
367  to revert the changes, since otherwise it could revert the command after
368  we sent OK to the client.
369  */
370  /* call rename table from table to tmp-name */
371  DBUG_EXECUTE_IF("exchange_partition_fail_3",
372  my_error(ER_ERROR_ON_RENAME, MYF(0),
373  name, tmp_name, 0, "n/a");
374  error_set= TRUE;
375  goto err_rename;);
376  DBUG_EXECUTE_IF("exchange_partition_abort_3", DBUG_SUICIDE(););
377  if (file->ha_rename_table(name, tmp_name))
378  {
379  char errbuf[MYSYS_STRERROR_SIZE];
380  my_error(ER_ERROR_ON_RENAME, MYF(0), name, tmp_name,
381  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
382  error_set= TRUE;
383  goto err_rename;
384  }
385  DBUG_EXECUTE_IF("exchange_partition_fail_4", goto err_rename;);
386  DBUG_EXECUTE_IF("exchange_partition_abort_4", DBUG_SUICIDE(););
387  if (deactivate_ddl_log_entry(log_entry->entry_pos))
388  goto err_rename;
389 
390  /* call rename table from partition to table */
391  DBUG_EXECUTE_IF("exchange_partition_fail_5",
392  my_error(ER_ERROR_ON_RENAME, MYF(0),
393  from_name, name, 0, "n/a");
394  error_set= TRUE;
395  goto err_rename;);
396  DBUG_EXECUTE_IF("exchange_partition_abort_5", DBUG_SUICIDE(););
397  if (file->ha_rename_table(from_name, name))
398  {
399  char errbuf[MYSYS_STRERROR_SIZE];
400  my_error(ER_ERROR_ON_RENAME, MYF(0), from_name, name,
401  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
402  error_set= TRUE;
403  goto err_rename;
404  }
405  DBUG_EXECUTE_IF("exchange_partition_fail_6", goto err_rename;);
406  DBUG_EXECUTE_IF("exchange_partition_abort_6", DBUG_SUICIDE(););
407  if (deactivate_ddl_log_entry(log_entry->entry_pos))
408  goto err_rename;
409 
410  /* call rename table from tmp-nam to partition */
411  DBUG_EXECUTE_IF("exchange_partition_fail_7",
412  my_error(ER_ERROR_ON_RENAME, MYF(0),
413  tmp_name, from_name, 0, "n/a");
414  error_set= TRUE;
415  goto err_rename;);
416  DBUG_EXECUTE_IF("exchange_partition_abort_7", DBUG_SUICIDE(););
417  if (file->ha_rename_table(tmp_name, from_name))
418  {
419  char errbuf[MYSYS_STRERROR_SIZE];
420  my_error(ER_ERROR_ON_RENAME, MYF(0), tmp_name, from_name,
421  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
422  error_set= TRUE;
423  goto err_rename;
424  }
425  DBUG_EXECUTE_IF("exchange_partition_fail_8", goto err_rename;);
426  DBUG_EXECUTE_IF("exchange_partition_abort_8", DBUG_SUICIDE(););
427  if (deactivate_ddl_log_entry(log_entry->entry_pos))
428  goto err_rename;
429 
430  /* The exchange is complete and ddl_log is deactivated */
431  DBUG_EXECUTE_IF("exchange_partition_fail_9", goto err_rename;);
432  DBUG_EXECUTE_IF("exchange_partition_abort_9", DBUG_SUICIDE(););
433  /* all OK */
434  error= FALSE;
435  delete file;
436  DBUG_RETURN(error);
437 err_rename:
438  /*
439  Nothing to do if any of these commands fails :( the commands itselfs
440  will log to the error log about the failures...
441  */
442  /* execute the ddl log entry to revert the renames */
443  (void) execute_ddl_log_entry(current_thd, log_entry->entry_pos);
444  mysql_mutex_lock(&LOCK_gdl);
445  /* mark the execute log entry done */
446  (void) write_execute_ddl_log_entry(0, TRUE, &exec_log_entry);
447  /* release the execute log entry */
448  (void) release_ddl_log_memory_entry(exec_log_entry);
449 err_no_execute_written:
450  /* release the action log entry */
451  (void) release_ddl_log_memory_entry(log_entry);
452 err_no_action_written:
453  mysql_mutex_unlock(&LOCK_gdl);
454  delete file;
455  if (!error_set)
456  my_error(ER_DDL_LOG_ERROR, MYF(0));
457  DBUG_RETURN(error);
458 }
459 
460 
483 bool Sql_cmd_alter_table_exchange_partition::
484  exchange_partition(THD *thd, TABLE_LIST *table_list, Alter_info *alter_info)
485 {
486  TABLE *part_table, *swap_table;
487  TABLE_LIST *swap_table_list;
488  handlerton *table_hton;
489  partition_element *part_elem;
490  char *partition_name;
491  char temp_name[FN_REFLEN+1];
492  char part_file_name[FN_REFLEN+1];
493  char swap_file_name[FN_REFLEN+1];
494  char temp_file_name[FN_REFLEN+1];
495  uint swap_part_id;
496  uint part_file_name_len;
497  Alter_table_prelocking_strategy alter_prelocking_strategy;
498  MDL_ticket *swap_table_mdl_ticket= NULL;
499  MDL_ticket *part_table_mdl_ticket= NULL;
500  uint table_counter;
501  bool error= TRUE;
502  DBUG_ENTER("mysql_exchange_partition");
503  DBUG_ASSERT(alter_info->flags & Alter_info::ALTER_EXCHANGE_PARTITION);
504 
505  /* Don't allow to exchange with log table */
506  swap_table_list= table_list->next_local;
507  if (check_if_log_table(swap_table_list->db_length, swap_table_list->db,
508  swap_table_list->table_name_length,
509  swap_table_list->table_name, 0))
510  {
511  my_error(ER_WRONG_USAGE, MYF(0), "PARTITION", "log table");
512  DBUG_RETURN(TRUE);
513  }
514 
515  /*
516  Currently no MDL lock that allows both read and write and is upgradeable
517  to exclusive, so leave the lock type to TL_WRITE_ALLOW_READ also on the
518  partitioned table.
519 
520  TODO: add MDL lock that allows both read and write and is upgradable to
521  exclusive lock. This would allow to continue using the partitioned table
522  also with update/insert/delete while the verification of the swap table
523  is running.
524  */
525 
526  /*
527  NOTE: It is not possible to exchange a crashed partition/table since
528  we need some info from the engine, which we can only access after open,
529  to be able to verify the structure/metadata.
530  */
531  table_list->mdl_request.set_type(MDL_SHARED_NO_WRITE);
532  if (open_tables(thd, &table_list, &table_counter, 0,
533  &alter_prelocking_strategy))
534  DBUG_RETURN(true);
535 
536  part_table= table_list->table;
537  swap_table= swap_table_list->table;
538 
539  if (check_exchange_partition(swap_table, part_table))
540  DBUG_RETURN(TRUE);
541 
542  /* set lock pruning on first table */
543  partition_name= alter_info->partition_names.head();
544  if (table_list->table->part_info->
545  set_named_partition_bitmap(partition_name, strlen(partition_name)))
546  DBUG_RETURN(true);
547 
548  if (lock_tables(thd, table_list, table_counter, 0))
549  DBUG_RETURN(true);
550 
551 
552  table_hton= swap_table->file->ht;
553 
554  THD_STAGE_INFO(thd, stage_verifying_table);
555 
556  /* Will append the partition name later in part_info->get_part_elem() */
557  part_file_name_len= build_table_filename(part_file_name,
558  sizeof(part_file_name),
559  table_list->db,
560  table_list->table_name,
561  "", 0);
562  build_table_filename(swap_file_name,
563  sizeof(swap_file_name),
564  swap_table_list->db,
565  swap_table_list->table_name,
566  "", 0);
567  /* create a unique temp name #sqlx-nnnn_nnnn, x for eXchange */
568  my_snprintf(temp_name, sizeof(temp_name), "%sx-%lx_%lx",
569  tmp_file_prefix, current_pid, thd->thread_id);
570  if (lower_case_table_names)
571  my_casedn_str(files_charset_info, temp_name);
572  build_table_filename(temp_file_name, sizeof(temp_file_name),
573  table_list->next_local->db,
574  temp_name, "", FN_IS_TMP);
575 
576  if (!(part_elem= part_table->part_info->get_part_elem(partition_name,
577  part_file_name +
578  part_file_name_len,
579  &swap_part_id)))
580  {
581  my_error(ER_UNKNOWN_PARTITION, MYF(0), partition_name,
582  part_table->alias);
583  DBUG_RETURN(TRUE);
584  }
585 
586  if (swap_part_id == NOT_A_PARTITION_ID)
587  {
588  DBUG_ASSERT(part_table->part_info->is_sub_partitioned());
589  my_error(ER_PARTITION_INSTEAD_OF_SUBPARTITION, MYF(0));
590  DBUG_RETURN(TRUE);
591  }
592 
593  if (compare_table_with_partition(thd, swap_table, part_table, part_elem))
594  DBUG_RETURN(TRUE);
595 
596  /* Table and partition has same structure/options, OK to exchange */
597 
598  thd_proc_info(thd, "verifying data with partition");
599 
600  if (verify_data_with_partition(swap_table, part_table, swap_part_id))
601  DBUG_RETURN(TRUE);
602 
603  /*
604  Get exclusive mdl lock on both tables, alway the non partitioned table
605  first. Remember the tickets for downgrading locks later.
606  */
607  swap_table_mdl_ticket= swap_table->mdl_ticket;
608  part_table_mdl_ticket= part_table->mdl_ticket;
609 
610  /*
611  No need to set used_partitions to only propagate
612  HA_EXTRA_PREPARE_FOR_RENAME to one part since no built in engine uses
613  that flag. And the action would probably be to force close all other
614  instances which is what we are doing any way.
615  */
616  if (wait_while_table_is_used(thd, swap_table, HA_EXTRA_PREPARE_FOR_RENAME) ||
617  wait_while_table_is_used(thd, part_table, HA_EXTRA_PREPARE_FOR_RENAME))
618  goto err;
619 
620  DEBUG_SYNC(thd, "swap_partition_after_wait");
621 
622  close_all_tables_for_name(thd, swap_table->s, false, NULL);
623  close_all_tables_for_name(thd, part_table->s, false, NULL);
624 
625  DEBUG_SYNC(thd, "swap_partition_before_rename");
626 
627  if (exchange_name_with_ddl_log(thd, swap_file_name, part_file_name,
628  temp_file_name, table_hton))
629  goto err;
630 
631  /*
632  Reopen tables under LOCK TABLES. Ignore the return value for now. It's
633  better to keep master/slave in consistent state. Alternative would be to
634  try to revert the exchange operation and issue error.
635  */
636  (void) thd->locked_tables_list.reopen_tables(thd);
637 
638  if ((error= write_bin_log(thd, TRUE, thd->query(), thd->query_length())))
639  {
640  /*
641  The error is reported in write_bin_log().
642  We try to revert to make it easier to keep the master/slave in sync.
643  */
644  (void) exchange_name_with_ddl_log(thd, part_file_name, swap_file_name,
645  temp_file_name, table_hton);
646  }
647 
648 err:
649  if (thd->locked_tables_mode)
650  {
651  if (swap_table_mdl_ticket)
652  swap_table_mdl_ticket->downgrade_lock(MDL_SHARED_NO_READ_WRITE);
653  if (part_table_mdl_ticket)
654  part_table_mdl_ticket->downgrade_lock(MDL_SHARED_NO_READ_WRITE);
655  }
656 
657  if (!error)
658  my_ok(thd);
659 
660  // For query cache
661  table_list->table= NULL;
662  table_list->next_local->table= NULL;
663  query_cache_invalidate3(thd, table_list, FALSE);
664 
665  DBUG_RETURN(error);
666 }
667 
668 
670 {
671  bool res;
672  DBUG_ENTER("Sql_cmd_alter_table_analyze_partition::execute");
673 
674  /*
675  Flag that it is an ALTER command which administrates partitions, used
676  by ha_partition
677  */
678  thd->lex->alter_info.flags|= Alter_info::ALTER_ADMIN_PARTITION;
679 
681 
682  DBUG_RETURN(res);
683 }
684 
685 
687 {
688  bool res;
689  DBUG_ENTER("Sql_cmd_alter_table_check_partition::execute");
690 
691  /*
692  Flag that it is an ALTER command which administrates partitions, used
693  by ha_partition
694  */
695  thd->lex->alter_info.flags|= Alter_info::ALTER_ADMIN_PARTITION;
696 
698 
699  DBUG_RETURN(res);
700 }
701 
702 
704 {
705  bool res;
706  DBUG_ENTER("Alter_table_optimize_partition_statement::execute");
707 
708  /*
709  Flag that it is an ALTER command which administrates partitions, used
710  by ha_partition
711  */
712  thd->lex->alter_info.flags|= Alter_info::ALTER_ADMIN_PARTITION;
713 
715 
716  DBUG_RETURN(res);
717 }
718 
719 
721 {
722  bool res;
723  DBUG_ENTER("Sql_cmd_alter_table_repair_partition::execute");
724 
725  /*
726  Flag that it is an ALTER command which administrates partitions, used
727  by ha_partition
728  */
729  thd->lex->alter_info.flags|= Alter_info::ALTER_ADMIN_PARTITION;
730 
732 
733  DBUG_RETURN(res);
734 }
735 
736 
738 {
739  int error;
740  ha_partition *partition;
741  ulong timeout= thd->variables.lock_wait_timeout;
742  TABLE_LIST *first_table= thd->lex->select_lex.table_list.first;
743  Alter_info *alter_info= &thd->lex->alter_info;
744  uint table_counter, i;
745  List<String> partition_names_list;
746  bool binlog_stmt;
747  DBUG_ENTER("Sql_cmd_alter_table_truncate_partition::execute");
748 
749  /*
750  Flag that it is an ALTER command which administrates partitions, used
751  by ha_partition.
752  */
753  thd->lex->alter_info.flags|= Alter_info::ALTER_ADMIN_PARTITION |
754  Alter_info::ALTER_TRUNCATE_PARTITION;
755 
756  /* Fix the lock types (not the same as ordinary ALTER TABLE). */
757  first_table->lock_type= TL_WRITE;
758  first_table->mdl_request.set_type(MDL_EXCLUSIVE);
759 
760  /*
761  Check table permissions and open it with a exclusive lock.
762  Ensure it is a partitioned table and finally, upcast the
763  handler and invoke the partition truncate method. Lastly,
764  write the statement to the binary log if necessary.
765  */
766 
767  if (check_one_table_access(thd, DROP_ACL, first_table))
768  DBUG_RETURN(TRUE);
769 
770  if (open_tables(thd, &first_table, &table_counter, 0))
771  DBUG_RETURN(true);
772 
773  /*
774  TODO: Add support for TRUNCATE PARTITION for NDB and other
775  engines supporting native partitioning.
776  */
777 
778  if (!first_table->table || first_table->view ||
779  first_table->table->s->db_type() != partition_hton)
780  {
781  my_error(ER_PARTITION_MGMT_ON_NONPARTITIONED, MYF(0));
782  DBUG_RETURN(TRUE);
783  }
784 
785 
786  /*
787  Prune all, but named partitions,
788  to avoid excessive calls to external_lock().
789  */
790  List_iterator<char> partition_names_it(alter_info->partition_names);
791  uint num_names= alter_info->partition_names.elements;
792  for (i= 0; i < num_names; i++)
793  {
794  char *partition_name= partition_names_it++;
795  String *str_partition_name= new (thd->mem_root)
796  String(partition_name, system_charset_info);
797  if (!str_partition_name)
798  DBUG_RETURN(true);
799  partition_names_list.push_back(str_partition_name);
800  }
801  first_table->partition_names= &partition_names_list;
802  if (first_table->table->part_info->set_partition_bitmaps(first_table))
803  DBUG_RETURN(true);
804 
805  if (lock_tables(thd, first_table, table_counter, 0))
806  DBUG_RETURN(true);
807 
808  /*
809  Under locked table modes this might still not be an exclusive
810  lock. Hence, upgrade the lock since the handler truncate method
811  mandates an exclusive metadata lock.
812  */
813  MDL_ticket *ticket= first_table->table->mdl_ticket;
814  if (thd->mdl_context.upgrade_shared_lock(ticket, MDL_EXCLUSIVE, timeout))
815  DBUG_RETURN(TRUE);
816 
817  tdc_remove_table(thd, TDC_RT_REMOVE_NOT_OWN, first_table->db,
818  first_table->table_name, FALSE);
819 
820  partition= (ha_partition*) first_table->table->file;
821  /* Invoke the handler method responsible for truncating the partition. */
822  if ((error= partition->truncate_partition(alter_info, &binlog_stmt)))
823  partition->print_error(error, MYF(0));
824 
825  /*
826  All effects of a truncate operation are committed even if the
827  operation fails. Thus, the query must be written to the binary
828  log. The exception is a unimplemented truncate method or failure
829  before any call to handler::truncate() is done.
830  Also, it is logged in statement format, regardless of the binlog format.
831  */
832  if (error != HA_ERR_WRONG_COMMAND && binlog_stmt)
833  error|= write_bin_log(thd, !error, thd->query(), thd->query_length());
834 
835  /*
836  A locked table ticket was upgraded to a exclusive lock. After the
837  the query has been written to the binary log, downgrade the lock
838  to a shared one.
839  */
840  if (thd->locked_tables_mode)
841  ticket->downgrade_lock(MDL_SHARED_NO_READ_WRITE);
842 
843  if (! error)
844  my_ok(thd);
845 
846  // Invalidate query cache
847  DBUG_ASSERT(!first_table->next_local);
848  query_cache_invalidate3(thd, first_table, FALSE);
849 
850  DBUG_RETURN(error);
851 }
852 
853 #endif /* WITH_PARTITION_STORAGE_ENGINE */