58 #include "my_global.h"
61 #include "sql_insert.h"
62 #include "sql_update.h"
64 #include "sql_cache.h"
69 #include "sql_table.h"
71 #include "sql_trigger.h"
75 #include "sql_parse.h"
77 #include "transaction.h"
78 #include "sql_audit.h"
80 #include "opt_explain.h"
81 #include "delayable_insert_operation.h"
83 #include "sql_optimizer.h"
84 #include "global_threads.h"
85 #ifdef WITH_PARTITION_STORAGE_ENGINE
86 #include "sql_partition.h"
87 #include "partition_info.h"
92 #ifndef EMBEDDED_LIBRARY
93 static bool delayed_get_table(THD *thd,
MDL_request *grl_protection_request,
99 static void end_delayed_insert(THD *thd);
100 pthread_handler_t handle_delayed_insert(
void *arg);
101 static void unlink_blobs(
register TABLE *
table);
137 tables|= item->used_tables();
143 tables|= item->used_tables();
147 tables&= ~PSEUDO_TABLE_BITS;
158 if (view->check_single_table(&tbl, tables, view) || tbl == 0)
161 view->table= tbl->table;
167 my_error(ER_VIEW_MULTIUPDATE, MYF(0),
168 view->view_db.str, view->view_name.str);
188 static int check_insert_fields(THD *thd,
TABLE_LIST *table_list,
191 bool fields_and_values_from_different_maps,
196 if (!table_list->updatable)
198 my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias,
"INSERT");
202 if (fields.elements == 0 && values.elements != 0)
206 my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0),
207 table_list->view_db.str, table_list->view_name.str);
210 if (values.elements != table->s->fields)
212 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
215 #ifndef NO_EMBEDDED_ACCESS_CHECKS
217 field_it.set(table_list);
218 if (check_grant_all_columns(thd, INSERT_ACL, &field_it))
225 bitmap_set_all(table->write_set);
229 SELECT_LEX *select_lex= &thd->lex->select_lex;
234 if (fields.elements != values.elements)
236 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
241 select_lex->no_wrap_view_item= TRUE;
244 ctx_state.save_state(context, table_list);
250 table_list->next_local= 0;
251 context->resolve_in_table_list_only(table_list);
252 res= setup_fields(thd,
Ref_ptr_array(), fields, MARK_COLUMNS_WRITE, 0, 0);
255 ctx_state.restore_state(context, table_list);
256 thd->lex->select_lex.no_wrap_view_item= FALSE;
263 if (check_view_single_update(fields,
264 fields_and_values_from_different_maps ?
268 table= table_list->table;
271 if (check_unique && thd->dup_field)
273 my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), thd->dup_field->field_name);
278 #ifndef NO_EMBEDDED_ACCESS_CHECKS
282 if (check_key_in_view(thd, table_list) ||
284 check_view_insertability(thd, table_list)))
286 my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias,
"INSERT");
308 static int check_update_fields(THD *thd,
TABLE_LIST *insert_table_list,
311 bool fields_and_values_from_different_maps,
316 update_fields, MARK_COLUMNS_WRITE, 0, 0))
320 check_view_single_update(update_fields,
321 fields_and_values_from_different_maps ?
323 insert_table_list, map))
344 bool validate_default_values_of_unset_fields(THD *thd,
TABLE *table)
347 DBUG_ENTER(
"validate_default_values_of_unset_fields");
349 for (
Field **field= table->field; *field; field++)
351 if (!bitmap_is_set(write_set, (*field)->field_index) &&
352 !((*field)->flags & NO_DEFAULT_VALUE_FLAG))
354 if ((*field)->validate_stored_val(thd) && thd->is_error())
376 void prepare_triggers_for_insert_stmt(
TABLE *table)
380 if (table->triggers->has_triggers(TRG_EVENT_DELETE,
388 (void) table->file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
390 if (table->triggers->has_triggers(TRG_EVENT_UPDATE,
398 (void) table->file->extra(HA_EXTRA_UPDATE_CANNOT_BATCH);
401 table->mark_columns_needed_for_insert();
414 void upgrade_lock_type(THD *thd, thr_lock_type *lock_type,
415 enum_duplicates duplic)
417 if (duplic == DUP_UPDATE ||
418 (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT))
420 *lock_type= TL_WRITE_DEFAULT;
424 if (*lock_type == TL_WRITE_DELAYED)
449 if (specialflag & SPECIAL_NO_NEW_FUNC ||
450 thd->variables.max_insert_delayed_threads == 0 ||
451 thd->locked_tables_mode > LTM_LOCK_TABLES ||
452 thd->lex->uses_stored_routines() || thd->lex->describe)
454 *lock_type= TL_WRITE;
457 if (thd->slave_thread)
460 *lock_type= (duplic == DUP_UPDATE || duplic == DUP_REPLACE) ?
461 TL_WRITE : TL_WRITE_CONCURRENT_INSERT;
465 bool log_on= (thd->variables.option_bits & OPTION_BIN_LOG);
466 if (global_system_variables.
binlog_format == BINLOG_FORMAT_STMT &&
467 log_on && mysql_bin_log.is_open())
492 *lock_type= TL_WRITE;
523 bool open_and_lock_for_insert_delayed(THD *thd,
TABLE_LIST *table_list)
526 DBUG_ENTER(
"open_and_lock_for_insert_delayed");
528 #ifndef EMBEDDED_LIBRARY
530 if (thd->tx_read_only)
532 my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0));
544 if (thd->global_read_lock.can_acquire_protection())
547 protection_request.
init(MDL_key::GLOBAL,
"",
"", MDL_INTENTION_EXCLUSIVE,
550 if (thd->mdl_context.acquire_lock(&protection_request,
551 thd->variables.lock_wait_timeout))
554 if (thd->mdl_context.acquire_lock(&table_list->mdl_request,
555 thd->variables.lock_wait_timeout))
563 if (delayed_get_table(thd, &protection_request, table_list))
565 else if (table_list->table)
573 end_delayed_insert(thd);
582 if (!table_list->
derived && !table_list->view)
583 table_list->updatable= 1;
600 table_list->mdl_request.
ticket= NULL;
602 if (error || table_list->table)
614 table_list->lock_type= TL_WRITE;
631 create_insert_stmt_from_insert_delayed(THD *thd,
String *
buf)
634 if (buf->append(thd->query()) ||
635 buf->replace(thd->lex->keyword_delayed_begin_offset,
636 thd->lex->keyword_delayed_end_offset -
637 thd->lex->keyword_delayed_begin_offset, 0))
651 bool mysql_insert(THD *thd,
TABLE_LIST *table_list,
656 enum_duplicates duplic,
661 bool transactional_table, joins_freed= FALSE;
663 bool was_insert_delayed= (table_list->lock_type == TL_WRITE_DELAYED);
664 bool is_locked=
false;
677 const bool manage_defaults=
678 fields.elements != 0 ||
679 values_list.head()->elements == 0;
680 COPY_INFO info(COPY_INFO::INSERT_OPERATION,
685 COPY_INFO update(COPY_INFO::UPDATE_OPERATION, &update_fields, &update_values);
688 #ifndef EMBEDDED_LIBRARY
689 char *
query= thd->query();
695 bool log_on= (thd->variables.option_bits & OPTION_BIN_LOG);
697 Item *unused_conds= 0;
698 #ifdef WITH_PARTITION_STORAGE_ENGINE
699 uint num_partitions= 0;
701 partition_info::PRUNE_NO;
703 bool prune_needs_default_values;
705 DBUG_ENTER(
"mysql_insert");
711 upgrade_lock_type(thd, &table_list->lock_type, duplic);
719 if (table_list->lock_type == TL_WRITE_DELAYED &&
720 thd->locked_tables_mode &&
722 table_list->table_name))
724 my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
725 table_list->table_name);
729 if (table_list->lock_type == TL_WRITE_DELAYED)
731 if (open_and_lock_for_insert_delayed(thd, table_list))
741 const thr_lock_type lock_type= table_list->lock_type;
743 THD_STAGE_INFO(thd, stage_init);
744 thd->lex->used_tables=0;
748 const uint value_count= values->elements;
750 if (mysql_prepare_insert(thd, table_list, table, fields, values,
751 update_fields, update_values, duplic, &unused_conds,
753 (fields.elements || !value_count ||
754 table_list->view != 0),
755 !ignore && thd->is_strict_mode()))
756 goto exit_without_my_ok;
759 table= table_list->table;
762 if (info.add_function_default_columns(table, table->write_set))
763 goto exit_without_my_ok;
764 if (duplic == DUP_UPDATE &&
765 update.add_function_default_columns(table, table->write_set))
766 goto exit_without_my_ok;
768 context= &thd->lex->select_lex.context;
774 DBUG_ASSERT(!table_list->next_local);
775 DBUG_ASSERT(!context->table_list->next_local);
776 DBUG_ASSERT(!context->first_name_resolution_table->next_name_resolution_table);
779 ctx_state.save_state(context, table_list);
785 table_list->next_local= 0;
786 context->resolve_in_table_list_only(table_list);
788 #ifdef WITH_PARTITION_STORAGE_ENGINE
789 if (!is_locked && table->part_info)
791 if (table->part_info->can_prune_insert(thd,
796 !
test(values->elements),
797 &can_prune_partitions,
798 &prune_needs_default_values,
800 goto exit_without_my_ok;
802 if (can_prune_partitions != partition_info::PRUNE_NO)
804 num_partitions= table->part_info->lock_partitions.n_bits;
817 if (table->part_info->set_used_partition(fields,
820 prune_needs_default_values,
822 can_prune_partitions= partition_info::PRUNE_NO;
827 while ((values= its++))
830 if (values->elements != value_count)
832 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
833 goto exit_without_my_ok;
835 if (setup_fields(thd,
Ref_ptr_array(), *values, MARK_COLUMNS_READ, 0, 0))
836 goto exit_without_my_ok;
838 #ifdef WITH_PARTITION_STORAGE_ENGINE
847 if (can_prune_partitions == partition_info::PRUNE_YES)
849 if (table->part_info->set_used_partition(fields,
852 prune_needs_default_values,
854 can_prune_partitions= partition_info::PRUNE_NO;
855 if (!(counter % num_partitions))
862 if (bitmap_is_set_all(&used_partitions))
863 can_prune_partitions= partition_info::PRUNE_NO;
868 table->auto_increment_field_not_null=
false;
872 ctx_state.restore_state(context, table_list);
874 if (thd->lex->describe)
881 err= explain_no_table(thd,
"No tables used");
882 goto exit_without_my_ok;
885 #ifdef WITH_PARTITION_STORAGE_ENGINE
886 if (can_prune_partitions != partition_info::PRUNE_NO)
900 bitmap_intersect(&table->part_info->read_partitions,
902 bitmap_intersect(&table->part_info->lock_partitions,
909 lock_tables(thd, table_list, thd->lex->table_count, 0))
917 thd->count_cuted_fields= ((values_list.elements == 1 &&
919 CHECK_FIELD_ERROR_FOR_NULL :
921 thd->cuted_fields = 0L;
922 table->next_number_field=table->found_next_number_field;
924 #ifdef HAVE_REPLICATION
925 if (thd->slave_thread)
927 DBUG_ASSERT(active_mi != NULL);
928 if(info.get_duplicate_handling() == DUP_UPDATE &&
929 table->next_number_field != NULL &&
930 rpl_master_has_bug(active_mi->rli, 24432, TRUE, NULL, NULL))
931 goto exit_without_my_ok;
936 THD_STAGE_INFO(thd, stage_update);
937 if (duplic == DUP_REPLACE &&
938 (!table->triggers || !table->triggers->has_delete_triggers()))
939 table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
940 if (duplic == DUP_UPDATE)
941 table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
954 #ifndef EMBEDDED_LIBRARY
955 if (lock_type != TL_WRITE_DELAYED)
958 if (duplic != DUP_ERROR || ignore)
959 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
967 if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
971 thd->abort_on_warning= (!ignore && thd->is_strict_mode());
973 prepare_triggers_for_insert_stmt(table);
976 if (table_list->prepare_where(thd, 0, TRUE) ||
977 table_list->prepare_check_option(thd))
980 while ((values= its++))
982 if (fields.elements || !value_count)
984 restore_record(table,s->default_values);
990 if (validate_default_values_of_unset_fields(thd, table))
996 if (fill_record_n_invoke_before_triggers(thd, fields, *values, 0,
1000 if (values_list.elements != 1 && ! thd->is_error())
1002 info.stats.records++;
1016 if (thd->lex->used_tables)
1017 restore_record(table,s->default_values);
1027 table->record[0][0]= share->default_values[0];
1030 if (share->null_bytes > 1 && share->last_null_bit_pos)
1032 table->record[0][share->null_bytes - 1]=
1033 share->default_values[share->null_bytes - 1];
1036 if (fill_record_n_invoke_before_triggers(thd, table->field, *values, 0,
1040 if (values_list.elements != 1 && ! thd->is_error())
1042 info.stats.records++;
1050 if ((res= table_list->view_check_option(thd,
1051 (values_list.elements == 1 ?
1056 else if (res == VIEW_CHECK_ERROR)
1061 #ifndef EMBEDDED_LIBRARY
1062 if (lock_type == TL_WRITE_DELAYED)
1064 LEX_STRING const st_query = { query, thd->query_length() };
1065 DEBUG_SYNC(thd,
"before_write_delayed");
1066 error= write_delayed(thd, table, st_query, log_on, &info);
1067 DEBUG_SYNC(thd,
"after_write_delayed");
1072 error= write_record(thd, table, &info, &update);
1075 thd->get_stmt_da()->inc_current_row_for_warning();
1085 #ifndef EMBEDDED_LIBRARY
1086 if (lock_type == TL_WRITE_DELAYED)
1090 info.stats.copied=values_list.elements;
1091 end_delayed_insert(thd);
1101 table->file->ha_release_auto_increment();
1102 if (thd->locked_tables_mode <= LTM_LOCK_TABLES &&
1108 if (duplic != DUP_ERROR || ignore)
1109 table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1111 transactional_table= table->file->has_transactions();
1113 if ((changed= (info.stats.copied || info.stats.deleted || info.stats.updated)))
1120 query_cache_invalidate3(thd, table_list, 1);
1124 thd->transaction.stmt.cannot_safely_rollback() ||
1127 if (mysql_bin_log.is_open())
1145 errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED);
1159 DBUG_ASSERT(thd->killed != THD::KILL_BAD_DATA || error > 0);
1160 if (was_insert_delayed && table_list->lock_type == TL_WRITE)
1164 if (create_insert_stmt_from_insert_delayed(thd, &log_query))
1166 sql_print_error(
"Event Error: An error occurred while creating query string"
1167 "for INSERT DELAYED stmt, before writing it into binary log.");
1171 else if (thd->binlog_query(THD::ROW_QUERY_TYPE,
1172 log_query.c_ptr(), log_query.length(),
1173 transactional_table, FALSE, FALSE,
1177 else if (thd->binlog_query(THD::ROW_QUERY_TYPE,
1178 thd->query(), thd->query_length(),
1179 transactional_table, FALSE, FALSE,
1184 DBUG_ASSERT(transactional_table || !changed ||
1185 thd->transaction.stmt.cannot_safely_rollback());
1187 THD_STAGE_INFO(thd, stage_end);
1198 id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
1199 thd->first_successful_insert_id_in_cur_stmt :
1200 (thd->arg_of_last_insert_id_function ?
1201 thd->first_successful_insert_id_in_prev_stmt :
1202 ((table->next_number_field && info.stats.copied) ?
1203 table->next_number_field->val_int() : 0));
1204 table->next_number_field=0;
1205 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1206 table->auto_increment_field_not_null= FALSE;
1207 if (duplic == DUP_REPLACE &&
1208 (!table->triggers || !table->triggers->has_delete_triggers()))
1209 table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1212 goto exit_without_my_ok;
1213 if (values_list.elements == 1 && (!(thd->variables.option_bits & OPTION_WARNINGS) ||
1214 !thd->cuted_fields))
1216 my_ok(thd, info.stats.copied + info.stats.deleted +
1217 ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
1218 info.stats.touched : info.stats.updated),
1224 ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
1225 info.stats.touched : info.stats.updated);
1227 my_snprintf(buff,
sizeof(buff),
1228 ER(ER_INSERT_INFO), (
long) info.stats.records,
1229 (lock_type == TL_WRITE_DELAYED) ? (
long) 0 :
1230 (
long) (info.stats.records - info.stats.copied),
1231 (
long) thd->get_stmt_da()->current_statement_warn_count());
1233 my_snprintf(buff,
sizeof(buff),
1234 ER(ER_INSERT_INFO), (
long) info.stats.records,
1235 (
long) (info.stats.deleted + updated),
1236 (
long) thd->get_stmt_da()->current_statement_warn_count());
1237 my_ok(thd, info.stats.copied + info.stats.deleted + updated,
id, buff);
1239 thd->abort_on_warning= 0;
1243 #ifndef EMBEDDED_LIBRARY
1244 if (lock_type == TL_WRITE_DELAYED)
1245 end_delayed_insert(thd);
1249 thd->abort_on_warning= 0;
1276 static bool check_view_insertability(THD * thd,
TABLE_LIST *view)
1278 uint num= view->view->select_lex.item_list.elements;
1279 TABLE *table= view->table;
1281 *trans_end= trans_start + num;
1283 uint used_fields_buff_size= bitmap_buffer_size(table->s->fields);
1284 uint32 *used_fields_buff= (uint32*)thd->alloc(used_fields_buff_size);
1286 enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
1287 DBUG_ENTER(
"check_key_in_view");
1289 if (!used_fields_buff)
1292 DBUG_ASSERT(view->table != 0 && view->field_translation != 0);
1294 (void) bitmap_init(&used_fields, used_fields_buff, table->s->fields, 0);
1295 bitmap_clear_all(&used_fields);
1297 view->contain_auto_increment= 0;
1302 thd->mark_used_columns= MARK_COLUMNS_NONE;
1304 for (trans= trans_start; trans != trans_end; trans++)
1306 if (!trans->item->fixed && trans->item->fix_fields(thd, &trans->item))
1308 thd->mark_used_columns= save_mark_used_columns;
1313 if (!(field= trans->item->field_for_view_update()))
1315 thd->mark_used_columns= save_mark_used_columns;
1318 if (field->field->unireg_check == Field::NEXT_NUMBER)
1319 view->contain_auto_increment= 1;
1327 thd->mark_used_columns= save_mark_used_columns;
1329 for (trans= trans_start; trans != trans_end; trans++)
1334 if (field->field->table == table &&
1335 bitmap_fast_test_and_set(&used_fields, field->field->field_index))
1359 static bool mysql_prepare_insert_check_table(THD *thd,
TABLE_LIST *table_list,
1363 bool insert_into_view= (table_list->view != 0);
1364 DBUG_ENTER(
"mysql_prepare_insert_check_table");
1366 if (!table_list->updatable)
1368 my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias,
"INSERT");
1379 &thd->lex->select_lex.top_join_list,
1381 &thd->lex->select_lex.leaf_tables,
1382 select_insert, INSERT_ACL, SELECT_ACL))
1385 if (insert_into_view && !fields.elements)
1387 thd->lex->empty_field_list_on_rset= 1;
1388 if (!table_list->table)
1390 my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0),
1391 table_list->view_db.str, table_list->view_name.str);
1394 DBUG_RETURN(insert_view_fields(thd, &fields, table_list));
1409 static void prepare_for_positional_update(
TABLE *table,
TABLE_LIST *tables)
1413 if(table->reginfo.lock_type != TL_WRITE_DELAYED)
1418 DBUG_ASSERT(tables->view);
1422 prepare_for_positional_update(tbl->table, tbl);
1459 bool mysql_prepare_insert(THD *thd,
TABLE_LIST *table_list,
1462 enum_duplicates duplic,
1463 Item **where,
bool select_insert,
1464 bool check_fields,
bool abort_on_warning)
1466 SELECT_LEX *select_lex= &thd->lex->select_lex;
1469 bool insert_into_view= (table_list->view != 0);
1472 DBUG_ENTER(
"mysql_prepare_insert");
1473 DBUG_PRINT(
"enter", (
"table_list 0x%lx, table 0x%lx, view %d",
1474 (ulong)table_list, (ulong)table,
1475 (
int)insert_into_view));
1477 DBUG_ASSERT (!select_insert || !values);
1486 for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit();
1488 un= un->next_unit())
1490 for (SELECT_LEX *sl= un->first_select();
1492 sl= sl->next_select())
1494 sl->context.outer_context= 0;
1499 if (duplic == DUP_UPDATE)
1502 if (table_list->set_insert_values(thd->mem_root))
1506 if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert))
1514 DBUG_ASSERT (!select_lex->group_list.elements);
1517 ctx_state.save_state(context, table_list);
1523 table_list->next_local= 0;
1524 context->resolve_in_table_list_only(table_list);
1527 *values, MARK_COLUMNS_READ, 0, 0) ||
1528 check_insert_fields(thd, context->table_list, fields, *values,
1529 !insert_into_view, 0, &map));
1531 if (!res && check_fields)
1533 bool saved_abort_on_warning= thd->abort_on_warning;
1534 thd->abort_on_warning= abort_on_warning;
1535 res= check_that_all_fields_are_given_values(thd,
1537 context->table_list->table,
1538 context->table_list);
1539 thd->abort_on_warning= saved_abort_on_warning;
1544 update_values, MARK_COLUMNS_READ, 0, 0);
1546 if (!res && duplic == DUP_UPDATE)
1548 select_lex->no_wrap_view_item= TRUE;
1549 res= check_update_fields(thd, context->table_list, update_fields,
1550 update_values,
false, &map);
1551 select_lex->no_wrap_view_item= FALSE;
1555 ctx_state.restore_state(context, table_list);
1562 table= table_list->table;
1566 Item *fake_conds= 0;
1568 if ((duplicate=
unique_table(thd, table_list, table_list->next_global, 1)))
1570 update_non_unique_table_error(table_list,
"INSERT", duplicate);
1573 select_lex->fix_prepare_information(thd, &fake_conds, &fake_conds);
1574 select_lex->first_execution= 0;
1580 if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
1581 prepare_for_positional_update(table, table_list);
1588 static int last_uniq_key(
TABLE *table,uint keynr)
1604 if (table->file->
ha_table_flags() & HA_DUPLICATE_KEY_NOT_IN_ORDER)
1607 while (++keynr < table->s->keys)
1608 if (table->key_info[keynr].
flags & HA_NOSAME)
1645 int error, trg_error= 0;
1647 MY_BITMAP *save_read_set, *save_write_set;
1649 ulonglong insert_id_for_cur_row= 0;
1650 DBUG_ENTER(
"write_record");
1653 save_read_set= table->read_set;
1654 save_write_set= table->write_set;
1658 const enum_duplicates duplicate_handling= info->get_duplicate_handling();
1659 const bool ignore_errors= info->get_ignore_errors();
1661 if (duplicate_handling == DUP_REPLACE || duplicate_handling == DUP_UPDATE)
1663 DBUG_ASSERT(duplicate_handling != DUP_UPDATE || update != NULL);
1664 while ((error=table->file->ha_write_row(table->record[0])))
1678 bool is_duplicate_key_error;
1682 if (!is_duplicate_key_error)
1690 goto ok_or_after_trg_err;
1693 if ((
int) (key_nr = table->file->
get_dup_key(error)) < 0)
1695 error= HA_ERR_FOUND_DUPP_KEY;
1698 DEBUG_SYNC(thd,
"write_row_replace");
1701 table->use_all_columns();
1707 if (duplicate_handling == DUP_REPLACE &&
1708 table->next_number_field &&
1709 key_nr == table->s->next_number_index &&
1710 (insert_id_for_cur_row > 0))
1714 if (table->file->
ha_rnd_pos(table->record[1],table->file->dup_ref))
1719 if (table->file->extra(HA_EXTRA_FLUSH_CACHE))
1727 if (!(key=(
char*) my_safe_alloca(table->s->max_unique_length,
1734 key_copy((uchar*) key,table->record[0],table->key_info+key_nr,0);
1736 (uchar*) key, HA_WHOLE_KEY,
1737 HA_READ_KEY_EXACT))))
1740 if (duplicate_handling == DUP_UPDATE)
1748 DBUG_ASSERT(table->insert_values != NULL);
1749 store_record(table,insert_values);
1750 restore_record(table,
record[1]);
1751 DBUG_ASSERT(update->get_changed_columns()->elements ==
1753 if (fill_record_n_invoke_before_triggers(thd,
1754 *update->get_changed_columns(),
1759 goto before_trg_err;
1761 bool insert_id_consumed=
false;
1763 table->auto_increment_field_not_null &&
1765 (insert_id_for_cur_row > 0))
1768 const ulonglong auto_incr_val= table->next_number_field->val_int();
1769 if (auto_incr_val == insert_id_for_cur_row)
1772 insert_id_consumed=
true;
1775 in_range(auto_incr_val))
1784 my_error(ER_AUTO_INCREMENT_CONFLICT, MYF(0));
1785 goto before_trg_err;
1789 if (!insert_id_consumed)
1790 table->file->restore_auto_increment(prev_insert_id);
1795 table->pos_in_table_list->belong_to_view;
1796 if (inserted_view != NULL)
1798 res= inserted_view->view_check_option(thd, ignore_errors);
1799 if (res == VIEW_CHECK_SKIP)
1800 goto ok_or_after_trg_err;
1801 if (res == VIEW_CHECK_ERROR)
1802 goto before_trg_err;
1806 info->stats.touched++;
1807 if (!records_are_comparable(table) || compare_records(table))
1812 if ((error=table->file->ha_update_row(table->record[1],
1813 table->record[0])) &&
1814 error != HA_ERR_RECORD_IS_THE_SAME)
1816 if (ignore_errors &&
1819 goto ok_or_after_trg_err;
1824 if (error != HA_ERR_RECORD_IS_THE_SAME)
1836 trg_error= (table->triggers &&
1838 TRG_ACTION_AFTER, TRUE));
1842 goto ok_or_after_trg_err;
1860 if (last_uniq_key(table,key_nr) &&
1861 !table->file->referenced_by_foreign_key() &&
1862 (!table->triggers || !table->triggers->has_delete_triggers()))
1864 if ((error=table->file->ha_update_row(table->record[1],
1865 table->record[0])) &&
1866 error != HA_ERR_RECORD_IS_THE_SAME)
1868 if (error != HA_ERR_RECORD_IS_THE_SAME)
1877 goto after_trg_n_copied_inc;
1881 if (table->triggers &&
1883 TRG_ACTION_BEFORE, TRUE))
1884 goto before_trg_err;
1885 if ((error=table->file->ha_delete_row(table->record[1])))
1888 if (!table->file->has_transactions())
1889 thd->transaction.stmt.mark_modified_non_trans_table();
1890 if (table->triggers &&
1892 TRG_ACTION_AFTER, TRUE))
1895 goto ok_or_after_trg_err;
1917 if (table->read_set != save_read_set ||
1918 table->write_set != save_write_set)
1919 table->column_bitmaps_set(save_read_set, save_write_set);
1921 else if ((error=table->file->ha_write_row(table->record[0])))
1923 DEBUG_SYNC(thd,
"write_row_noreplace");
1924 if (!ignore_errors ||
1927 table->file->restore_auto_increment(prev_insert_id);
1928 goto ok_or_after_trg_err;
1931 after_trg_n_copied_inc:
1934 trg_error= (table->triggers &&
1936 TRG_ACTION_AFTER, TRUE));
1938 ok_or_after_trg_err:
1940 my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
1941 if (!table->file->has_transactions())
1942 thd->transaction.stmt.mark_modified_non_trans_table();
1943 DBUG_RETURN(trg_error);
1946 info->last_errno= error;
1948 if (thd->lex->current_select)
1949 thd->lex->current_select->no_error= 0;
1953 table->file->restore_auto_increment(prev_insert_id);
1955 my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
1956 table->column_bitmaps_set(save_read_set, save_write_set);
1965 int check_that_all_fields_are_given_values(THD *thd,
TABLE *
entry,
1971 for (
Field **field=entry->field ; *field ; field++)
1973 if (!bitmap_is_set(write_set, (*field)->field_index) &&
1974 ((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
1975 ((*field)->real_type() != MYSQL_TYPE_ENUM))
1980 table_list= table_list->top_table();
1981 view=
test(table_list->view);
1985 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1986 ER_NO_DEFAULT_FOR_VIEW_FIELD,
1987 ER(ER_NO_DEFAULT_FOR_VIEW_FIELD),
1988 table_list->view_db.str,
1989 table_list->view_name.str);
1993 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1994 ER_NO_DEFAULT_FOR_FIELD,
1995 ER(ER_NO_DEFAULT_FOR_FIELD),
1996 (*field)->field_name);
2001 return thd->abort_on_warning ? err : 0;
2009 #ifndef EMBEDDED_LIBRARY
2023 enum_duplicates dup;
2025 sql_mode_t sql_mode;
2026 bool auto_increment_field_not_null;
2027 bool query_start_used, ignore, log_query, binlog_rows_query_log_events;
2028 bool stmt_depends_on_first_successful_insert_id_in_prev_stmt;
2030 ulonglong first_successful_insert_id_in_prev_stmt;
2031 ulonglong forced_insert_id;
2032 ulong auto_increment_increment;
2033 ulong auto_increment_offset;
2047 dup(insert_operation->get_duplicate_handling()),
2048 ignore(insert_operation->get_ignore_errors()),
2049 log_query(log_query_arg),
2050 binlog_rows_query_log_events(false),
2051 forced_insert_id(0),
2055 memset(&write_set, 0,
sizeof(write_set));
2080 bitmap_free(&write_set);
2088 if (!(
record= (
char*) my_malloc(client_table->s->reclength, MYF(MY_WME))))
2091 memcpy(
record, client_table->record[0], client_table->s->reclength);
2092 start_time= thd->start_time.tv_sec;
2093 query_start_used= thd->query_start_used;
2101 stmt_depends_on_first_successful_insert_id_in_prev_stmt=
2102 thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt;
2103 first_successful_insert_id_in_prev_stmt=
2104 thd->first_successful_insert_id_in_prev_stmt;
2111 if (thd->time_zone_used)
2113 time_zone= thd->variables.time_zone;
2120 auto_increment_increment= thd->variables.auto_increment_increment;
2121 auto_increment_offset= thd->variables.auto_increment_offset;
2122 sql_mode= thd->variables.sql_mode;
2123 auto_increment_field_not_null= client_table->auto_increment_field_not_null;
2124 binlog_rows_query_log_events= thd->variables.binlog_rows_query_log_events;
2128 thd->auto_inc_intervals_forced.get_next();
2129 if (forced_auto_inc != NULL)
2131 forced_insert_id= forced_auto_inc->minimum();
2132 DBUG_PRINT(
"delayed", (
"transmitting auto_inc: %lu",
2133 (ulong) forced_insert_id));
2140 my_bitmap_map *bitmaps=
2142 my_malloc(bitmap_buffer_size(client_table->write_set->n_bits), MYF(0));
2144 if (bitmaps == NULL)
2147 bitmap_init(&write_set, bitmaps, client_table->write_set->n_bits,
false);
2148 bitmap_union(&write_set, client_table->write_set);
2163 uint locks_in_memory;
2164 thr_lock_type delayed_lock;
2170 volatile uint tables_in_use,stacked_inserts;
2171 volatile bool status;
2199 :locks_in_memory(0), table(0),tables_in_use(0),stacked_inserts(0),
2202 DBUG_ENTER(
"Delayed_insert constructor");
2203 thd.security_ctx->user=(
char*) delayed_user;
2204 thd.security_ctx->set_host(my_localhost);
2205 strmake(thd.security_ctx->priv_user, thd.security_ctx->user,
2207 thd.current_tablenr=0;
2208 thd.set_command(COM_DELAYED_INSERT);
2209 thd.lex->current_select= 0;
2210 thd.lex->sql_command= SQLCOM_INSERT;
2217 thd.variables.lock_wait_timeout= LONG_TIMEOUT;
2219 memset(&thd.net, 0,
sizeof(thd.net));
2220 memset(&table_list, 0,
sizeof(table_list));
2221 thd.system_thread= SYSTEM_THREAD_DELAYED_INSERT;
2222 thd.security_ctx->host_or_ip=
"";
2227 delayed_insert_threads++;
2228 delayed_lock= global_system_variables.low_priority_updates ?
2229 TL_WRITE_LOW_PRIORITY : TL_WRITE;
2237 while ((row=rows.get()))
2241 close_thread_tables(&thd);
2242 thd.mdl_context.release_transactional_locks();
2244 thd.release_resources();
2249 remove_global_thread(&thd);
2250 my_free(table_list.table_name);
2251 thd.security_ctx->set_host(
"");
2252 thd.security_ctx->user= 0;
2253 delayed_insert_threads--;
2265 if (!--locks_in_memory)
2268 if (thd.killed && ! stacked_inserts && ! tables_in_use)
2277 inline uint lock_count() {
return locks_in_memory; }
2281 bool handle_inserts(
void);
2296 THD_STAGE_INFO(thd, stage_waiting_for_delay_list);
2302 if (!strcmp(table_list->db, di->table_list.db) &&
2303 !strcmp(table_list->table_name, di->table_list.table_name))
2364 bool delayed_get_table(THD *thd,
MDL_request *grl_protection_request,
2369 DBUG_ENTER(
"delayed_get_table");
2372 DBUG_ASSERT(table_list->db);
2375 if (!(di= find_handler(thd, table_list)))
2381 if (delayed_insert_threads >= thd->variables.max_insert_delayed_threads)
2383 THD_STAGE_INFO(thd, stage_creating_delayed_handler);
2389 if (! (di= find_handler(thd, table_list)))
2393 di->table_list= *table_list;
2395 di->thd.set_db(table_list->db, (uint) strlen(table_list->db));
2396 di->table_list.alias= di->table_list.table_name=
2397 my_strdup(table_list->table_name, MYF(MY_WME | ME_FATALERROR));
2398 di->table_list.db= di->thd.db;
2399 di->thd.set_query(di->table_list.table_name, 0, system_charset_info);
2400 if (di->thd.db == NULL || di->thd.query() == NULL)
2408 MDL_INTENTION_EXCLUSIVE, MDL_STATEMENT);
2410 init_mdl_requests(&di->table_list);
2411 di->table_list.mdl_request.
ticket= table_list->mdl_request.
ticket;
2416 &di->thd.real_id, &connection_attrib,
2417 handle_delayed_insert, (
void*) di)))
2420 (
"Can't create thread to handle delayed insert (error %d)",
2425 my_error(ER_CANT_CREATE_THREAD, MYF(ME_FATALERROR), error);
2435 THD_STAGE_INFO(thd, stage_waiting_for_handler_open);
2437 (!di->thd.killed && !di->table && !thd->killed))
2442 THD_STAGE_INFO(thd, stage_got_old_table);
2450 if (di->thd.is_error())
2459 if (di->thd.get_stmt_da()->sql_errno() == ER_SERVER_SHUTDOWN)
2460 my_message(ER_QUERY_INTERRUPTED, ER(ER_QUERY_INTERRUPTED), MYF(0));
2462 my_message(di->thd.get_stmt_da()->sql_errno(),
2463 di->thd.get_stmt_da()->message(),
2479 if (table_list->table)
2481 DBUG_ASSERT(! thd->is_error());
2486 DBUG_RETURN((table_list->table == NULL));
2490 DBUG_RETURN(thd->is_error());
2512 my_ptrdiff_t adjust_ptrs;
2513 Field **field,**org_field, *found_next_number_field;
2517 DBUG_ENTER(
"Delayed_insert::get_local_table");
2524 THD_STAGE_INFO(client_thd, stage_waiting_for_handler_lock);
2526 while (!thd.killed && !thd.lock && ! client_thd->killed)
2530 THD_STAGE_INFO(client_thd, stage_got_handler_lock);
2531 if (client_thd->killed)
2548 if (!thd.is_error() ||
2549 thd.get_stmt_da()->sql_errno() == ER_SERVER_SHUTDOWN)
2550 my_message(ER_QUERY_INTERRUPTED, ER(ER_QUERY_INTERRUPTED), MYF(0));
2552 my_message(thd.get_stmt_da()->sql_errno(),
2553 thd.get_stmt_da()->message(), MYF(0));
2566 THD_STAGE_INFO(client_thd, stage_allocating_local_table);
2567 copy= (
TABLE*) client_thd->alloc(
sizeof(*copy)+
2568 (share->fields+1)*
sizeof(
Field**)+
2570 share->column_bitmap_size*2);
2578 field= copy->field= (
Field**) (copy + 1);
2579 bitmap= (uchar*) (field + share->fields + 1);
2580 copy->record[0]= (bitmap + share->column_bitmap_size * 2);
2581 memcpy((
char*) copy->record[0], (
char*) table->record[0], share->reclength);
2589 adjust_ptrs= PTR_BYTE_DIFF(copy->record[0], table->record[0]);
2590 found_next_number_field= table->found_next_number_field;
2591 for (org_field= table->field; *org_field; org_field++, field++)
2593 if (!(*field= (*org_field)->new_field(client_thd->mem_root, copy, 1)))
2595 (*field)->orig_table= copy;
2596 (*field)->move_field_offset(adjust_ptrs);
2597 if (*org_field == found_next_number_field)
2598 (*field)->table->found_next_number_field= *field;
2605 if ((*org_field)->has_insert_default_function() ||
2606 (*org_field)->has_update_default_function())
2607 (*field)->unireg_check= (*org_field)->unireg_check;
2612 copy->in_use= client_thd;
2614 copy->lock_count= 0;
2617 bitmap_init(©->def_read_set,
2618 reinterpret_cast<my_bitmap_map*>(bitmap),
2619 table->def_read_set.n_bits,
2621 bitmap_init(©->def_write_set,
2622 reinterpret_cast<my_bitmap_map*>
2623 (bitmap + share->column_bitmap_size),
2624 table->def_write_set.n_bits,
2626 copy->tmp_set.bitmap= 0;
2627 copy->read_set= ©->def_read_set;
2628 copy->write_set= ©->def_write_set;
2654 static bool write_delayed(THD *thd,
TABLE *table,
LEX_STRING query,
bool log_on,
2659 DBUG_ENTER(
"write_delayed");
2660 DBUG_PRINT(
"enter", (
"query = '%s' length %lu", query.str,
2661 (ulong) query.length));
2663 THD_STAGE_INFO(thd, stage_waiting_for_handler_insert);
2665 while (di->stacked_inserts >= delayed_queue_size && !thd->killed)
2667 THD_STAGE_INFO(thd, stage_storing_row_into_queue);
2681 if (!(str= my_strndup(query.str, query.length, MYF(MY_WME))))
2692 di->stacked_inserts++;
2694 if (table->s->blob_fields)
2695 unlink_blobs(table);
2698 thread_safe_increment(delayed_rows_in_use,&LOCK_delayed_status);
2713 static void end_delayed_insert(THD *thd)
2715 DBUG_ENTER(
"end_delayed_insert");
2718 DBUG_PRINT(
"info",(
"tables in use: %d",di->tables_in_use));
2719 if (!--di->tables_in_use || di->thd.killed)
2731 void kill_delayed_threads(
void)
2739 di->thd.killed= THD::KILL_CONNECTION;
2740 if (di->thd.mysys_var)
2743 if (di->thd.mysys_var->current_cond)
2749 if (&di->mutex != di->thd.mysys_var->current_mutex)
2752 if (&di->mutex != di->thd.mysys_var->current_mutex)
2775 virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
2777 bool *need_prelocking);
2778 virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
2779 TABLE_LIST *table_list,
bool *need_prelocking);
2780 virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
2781 TABLE_LIST *table_list,
bool *need_prelocking);
2785 bool Delayed_prelocking_strategy::
2786 handle_table(THD *thd, Query_tables_list *prelocking_ctx,
2787 TABLE_LIST *table_list,
bool *need_prelocking)
2789 DBUG_ASSERT(table_list->lock_type == TL_WRITE_DELAYED);
2791 if (!(table_list->table->file->
ha_table_flags() & HA_CAN_INSERT_DELAYED))
2793 my_error(ER_DELAYED_NOT_SUPPORTED, MYF(0), table_list->table_name);
2800 bool Delayed_prelocking_strategy::
2801 handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
2803 bool *need_prelocking)
2811 bool Delayed_prelocking_strategy::
2812 handle_view(THD *thd, Query_tables_list *prelocking_ctx,
2813 TABLE_LIST *table_list,
bool *need_prelocking)
2831 DBUG_ENTER(
"Delayed_insert::open_and_lock_table");
2840 MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK,
2841 &prelocking_strategy)))
2847 if (table->triggers)
2856 table->copy_blobs= 1;
2865 pthread_handler_t handle_delayed_insert(
void *arg)
2870 pthread_detach_this_thread();
2873 thd->thread_id= thd->variables.pseudo_thread_id= thread_id++;
2874 thd->set_current_time();
2875 add_global_thread(thd);
2876 thd->killed=abort_loop ? THD::KILL_CONNECTION : THD::NOT_KILLED;
2890 if (my_thread_init())
2893 thd->get_stmt_da()->set_error_status(ER_OUT_OF_RESOURCES);
2898 DBUG_ENTER(
"handle_delayed_insert");
2899 thd->thread_stack= (
char*) &thd;
2900 if (init_thr_lock() || thd->store_globals())
2903 thd->get_stmt_da()->set_error_status(ER_OUT_OF_RESOURCES);
2909 thd->lex->sql_command= SQLCOM_INSERT;
2915 thd->set_current_stmt_binlog_format_row_if_mixed();
2924 thd->mdl_context.clone_ticket(&di->table_list.mdl_request))
2926 thd->mdl_context.release_transactional_locks();
2936 di->table_list.mdl_request.
ticket= NULL;
2959 lock_count=di->lock_count();
2962 if (!lock_count && !di->tables_in_use && !di->stacked_inserts)
2967 if (!thd->killed && !di->status && !di->stacked_inserts)
2970 set_timespec(abstime, delayed_insert_timeout);
2973 di->thd.mysys_var->current_mutex= &di->mutex;
2974 di->thd.mysys_var->current_cond= &di->cond;
2975 THD_STAGE_INFO(&(di->thd), stage_waiting_for_insert);
2977 DBUG_PRINT(
"info",(
"Waiting for someone to insert rows"));
2978 while (!thd->killed && !di->status)
2981 mysql_audit_release(thd);
2982 #if defined(HAVE_BROKEN_COND_TIMEDWAIT)
2987 if (error && error != EINTR && error != ETIMEDOUT)
2989 fprintf(stderr,
"Got error %d from mysql_cond_timedwait\n", error);
2990 DBUG_PRINT(
"error", (
"Got error %d from mysql_cond_timedwait",
2995 if (error == ETIMEDOUT || error == ETIME)
2996 thd->killed= THD::KILL_CONNECTION;
3001 di->thd.mysys_var->current_mutex= 0;
3002 di->thd.mysys_var->current_cond= 0;
3007 if (di->tables_in_use && ! thd->lock && !thd->killed)
3022 thd->killed= THD::KILL_CONNECTION;
3026 if (di->stacked_inserts)
3028 if (di->handle_inserts())
3031 thd->killed= THD::KILL_CONNECTION;
3035 if (!di->stacked_inserts && !di->tables_in_use && thd->lock)
3048 di->table->file->ha_release_auto_increment();
3049 mysql_unlock_tables(thd, lock);
3050 trans_commit_stmt(thd);
3052 mysql_audit_release(thd);
3055 if (di->tables_in_use)
3063 close_thread_tables(thd);
3064 thd->mdl_context.release_transactional_locks();
3066 thd->killed= THD::KILL_CONNECTION;
3089 static void unlink_blobs(
register TABLE *table)
3091 for (
Field **ptr=table->field ; *ptr ; ptr++)
3093 if ((*ptr)->flags & BLOB_FLAG)
3100 static void free_delayed_insert_blobs(
register TABLE *table)
3102 for (
Field **ptr=table->field ; *ptr ; ptr++)
3104 if ((*ptr)->flags & BLOB_FLAG)
3115 bool Delayed_insert::handle_inserts(
void)
3119 bool has_trans = TRUE;
3120 bool using_ignore= 0, using_opt_replace= 0,
3121 using_bin_log= mysql_bin_log.is_open();
3123 DBUG_ENTER(
"Delayed_insert::handle_inserts");
3128 table->next_number_field=table->found_next_number_field;
3130 THD_STAGE_INFO(&thd, stage_upgrading_lock);
3131 if (thr_upgrade_write_delay_lock(*thd.lock->locks, delayed_lock,
3132 thd.variables.lock_wait_timeout))
3139 my_error(ER_DELAYED_CANT_CHANGE_LOCK,MYF(ME_FATALERROR),
3140 table->s->table_name.str);
3144 THD_STAGE_INFO(&thd, stage_insert);
3145 max_rows= delayed_insert_limit;
3148 thd.killed= THD::KILL_CONNECTION;
3149 max_rows= ULONG_MAX;
3158 table->file->extra(HA_EXTRA_WRITE_CACHE);
3161 bitmap_set_all(table->read_set);
3162 while ((row=rows.get()))
3166 memcpy(table->record[0],row->record,table->s->reclength);
3168 thd.start_time.tv_sec= row->start_time;
3169 thd.start_time.tv_usec= 0;
3170 thd.query_start_used=row->query_start_used;
3177 bitmap_clear_all(table->write_set);
3178 bitmap_union(table->write_set, &row->write_set);
3185 bool log_query= (row->log_query && row->query.str != NULL);
3186 DBUG_PRINT(
"delayed", (
"query: '%s' length: %lu", row->query.str ?
3187 row->query.str :
"[NULL]",
3188 (ulong) row->query.length));
3195 DBUG_ASSERT(!(mysql_bin_log.is_open() &&
3196 !thd.is_current_stmt_binlog_format_row()));
3198 if (mysql_bin_log.is_open())
3201 if (thd.binlog_flush_pending_rows_event(TRUE, FALSE))
3207 thd.set_query(row->query.str, row->query.length);
3208 thd.variables.binlog_rows_query_log_events= row->binlog_rows_query_log_events;
3219 table->file->ha_release_auto_increment();
3220 thd.auto_inc_intervals_in_cur_stmt_for_binlog.empty();
3222 thd.first_successful_insert_id_in_prev_stmt=
3223 row->first_successful_insert_id_in_prev_stmt;
3224 thd.stmt_depends_on_first_successful_insert_id_in_prev_stmt=
3225 row->stmt_depends_on_first_successful_insert_id_in_prev_stmt;
3226 table->auto_increment_field_not_null= row->auto_increment_field_not_null;
3229 thd.variables.auto_increment_increment= row->auto_increment_increment;
3230 thd.variables.auto_increment_offset= row->auto_increment_offset;
3231 thd.variables.sql_mode= row->sql_mode;
3234 if (row->forced_insert_id)
3236 DBUG_PRINT(
"delayed", (
"received auto_inc: %lu",
3237 (ulong) row->forced_insert_id));
3238 thd.force_one_auto_inc_interval(row->forced_insert_id);
3243 const enum_duplicates duplicate_handling=
d_info.get_duplicate_handling();
3244 if (
d_info.get_ignore_errors() || duplicate_handling != DUP_ERROR)
3246 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
3249 if (duplicate_handling == DUP_REPLACE &&
3250 (!table->triggers ||
3251 !table->triggers->has_delete_triggers()))
3253 table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
3254 using_opt_replace= 1;
3256 if (duplicate_handling == DUP_UPDATE)
3257 table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
3259 if (write_record(&thd, table, &
d_info, NULL))
3261 d_info.stats.error_count++;
3262 thread_safe_increment(delayed_insert_errors,&LOCK_delayed_status);
3269 table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
3271 if (using_opt_replace)
3273 using_opt_replace= 0;
3274 table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
3277 if (table->s->blob_fields)
3278 free_delayed_insert_blobs(table);
3279 thread_safe_decrement(delayed_rows_in_use,&LOCK_delayed_status);
3280 thread_safe_increment(delayed_insert_writes,&LOCK_delayed_status);
3287 table->auto_increment_field_not_null= FALSE;
3289 if (log_query && mysql_bin_log.is_open())
3290 thd.set_query(NULL, 0);
3298 if (group_count++ >= max_rows && (row= rows.head()) &&
3299 (!(row->log_query & using_bin_log)))
3302 if (stacked_inserts || tables_in_use)
3306 THD_STAGE_INFO(&thd, stage_reschedule);
3308 if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
3312 sql_print_error(
"%s", thd.get_stmt_da()->message());
3313 DBUG_PRINT(
"error", (
"HA_EXTRA_NO_CACHE failed in loop"));
3316 query_cache_invalidate3(&thd, table, 1);
3317 if (thr_reschedule_write_lock(*thd.lock->locks,
3318 thd.variables.lock_wait_timeout))
3321 my_error(ER_DELAYED_CANT_CHANGE_LOCK,MYF(ME_FATALERROR),
3322 table->s->table_name.str);
3326 table->file->extra(HA_EXTRA_WRITE_CACHE);
3328 THD_STAGE_INFO(&thd, stage_insert);
3348 has_trans= thd.lex->sql_command == SQLCOM_CREATE_TABLE ||
3349 table->file->has_transactions();
3350 if (mysql_bin_log.is_open() &&
3351 thd.binlog_flush_pending_rows_event(TRUE, has_trans))
3354 if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
3357 sql_print_error(
"%s", thd.get_stmt_da()->message());
3358 DBUG_PRINT(
"error", (
"HA_EXTRA_NO_CACHE failed after loop"));
3361 query_cache_invalidate3(&thd, table, 1);
3362 DBUG_EXECUTE_IF(
"after_handle_inserts",
3366 "signal inserts_handled";
3367 DBUG_ASSERT(opt_debug_sync_timeout > 0);
3368 DBUG_ASSERT(!debug_sync_set_action(&thd,
3369 STRING_WITH_LEN(act)));
3378 while ((row=rows.get()))
3380 if (table->s->blob_fields)
3382 memcpy(table->record[0],row->record,table->s->reclength);
3383 free_delayed_insert_blobs(table);
3390 DBUG_PRINT(
"error", (
"dropped %lu rows after an error", max_rows));
3391 for (; max_rows > 0; max_rows--)
3392 thread_safe_increment(delayed_insert_errors, &LOCK_delayed_status);
3393 thread_safe_increment(delayed_insert_errors, &LOCK_delayed_status);
3416 bool mysql_insert_select_prepare(THD *thd)
3419 SELECT_LEX *select_lex= &lex->select_lex;
3421 DBUG_ENTER(
"mysql_insert_select_prepare");
3428 if (mysql_prepare_insert(thd, lex->query_tables,
3429 lex->query_tables->table, lex->field_list, 0,
3430 lex->update_list, lex->value_list,
3432 &select_lex->where, TRUE, FALSE, FALSE))
3439 DBUG_ASSERT(select_lex->leaf_tables != 0);
3440 lex->leaf_tables_insert= select_lex->leaf_tables;
3442 for (first_select_leaf_table= select_lex->leaf_tables->next_leaf;
3443 first_select_leaf_table &&
3444 first_select_leaf_table->belong_to_view &&
3445 first_select_leaf_table->belong_to_view ==
3446 lex->leaf_tables_insert->belong_to_view;
3447 first_select_leaf_table= first_select_leaf_table->next_leaf)
3449 select_lex->leaf_tables= first_select_leaf_table;
3455 select_insert::prepare(
List<Item> &values, SELECT_LEX_UNIT *u)
3460 SELECT_LEX *lex_current_select_save= lex->current_select;
3461 DBUG_ENTER(
"select_insert::prepare");
3463 const enum_duplicates duplicate_handling= info.get_duplicate_handling();
3464 const bool ignore_errors= info.get_ignore_errors();
3473 lex->current_select= &lex->select_lex;
3476 lex->current_select->no_error= FALSE;
3477 res= (setup_fields(thd,
Ref_ptr_array(), values, MARK_COLUMNS_READ, 0, 0) ||
3478 check_insert_fields(thd, table_list, *fields, values,
3479 !insert_into_view, 1, &map));
3481 if (!res && fields->elements)
3483 bool saved_abort_on_warning= thd->abort_on_warning;
3485 thd->abort_on_warning= !ignore_errors && thd->is_strict_mode();
3487 res= check_that_all_fields_are_given_values(thd, table_list->table,
3489 thd->abort_on_warning= saved_abort_on_warning;
3492 if (duplicate_handling == DUP_UPDATE && !res)
3498 ctx_state.save_state(context, table_list);
3501 table_list->next_local= 0;
3502 context->resolve_in_table_list_only(table_list);
3504 lex->select_lex.no_wrap_view_item= TRUE;
3506 check_update_fields(thd, context->table_list,
3507 *update.get_changed_columns(),
3516 lex->select_lex.no_wrap_view_item= FALSE;
3524 DBUG_ASSERT (!table_list->next_name_resolution_table);
3525 if (lex->select_lex.group_list.elements == 0 &&
3526 !lex->select_lex.with_sum_func)
3534 table_list->next_name_resolution_table=
3535 ctx_state.get_first_name_resolution_table();
3538 MARK_COLUMNS_READ, 0, 0);
3550 while ((item= li++))
3552 item->
transform(&Item::update_value_transformer,
3553 (uchar*)lex->current_select);
3558 ctx_state.restore_state(context, table_list);
3561 lex->current_select= lex_current_select_save;
3568 table= table_list->table;
3572 if ((duplicate_handling == DUP_UPDATE) &&
3580 if (
unique_table(thd, table_list, table_list->next_global, 0))
3583 lex->current_select->options|= OPTION_BUFFER_RESULT;
3584 lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
3586 restore_record(table,s->default_values);
3587 table->next_number_field=table->found_next_number_field;
3589 #ifdef HAVE_REPLICATION
3590 if (thd->slave_thread)
3592 DBUG_ASSERT(active_mi != NULL);
3593 if (duplicate_handling == DUP_UPDATE &&
3594 table->next_number_field != NULL &&
3595 rpl_master_has_bug(active_mi->rli, 24432, TRUE, NULL, NULL))
3600 thd->cuted_fields=0;
3601 if (ignore_errors || duplicate_handling != DUP_ERROR)
3602 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
3603 if (duplicate_handling == DUP_REPLACE &&
3604 (!table->triggers || !table->triggers->has_delete_triggers()))
3605 table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
3606 if (duplicate_handling == DUP_UPDATE)
3607 table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
3608 thd->abort_on_warning= (!ignore_errors && thd->is_strict_mode());
3609 res= (table_list->prepare_where(thd, 0, TRUE) ||
3610 table_list->prepare_check_option(thd));
3613 prepare_triggers_for_insert_stmt(table);
3635 int select_insert::prepare2(
void)
3637 DBUG_ENTER(
"select_insert::prepare2");
3638 if (thd->locked_tables_mode <= LTM_LOCK_TABLES &&
3639 !thd->lex->describe)
3641 DBUG_ASSERT(!bulk_insert_started);
3644 bulk_insert_started=
true;
3650 void select_insert::cleanup()
3656 select_insert::~select_insert()
3658 DBUG_ENTER(
"~select_insert");
3661 table->next_number_field=0;
3662 table->auto_increment_field_not_null= FALSE;
3665 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
3666 thd->abort_on_warning= 0;
3671 bool select_insert::send_data(
List<Item> &values)
3673 DBUG_ENTER(
"select_insert::send_data");
3676 if (unit->offset_limit_cnt)
3678 unit->offset_limit_cnt--;
3682 thd->count_cuted_fields= CHECK_FIELD_WARN;
3683 store_values(values);
3684 thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
3685 if (thd->is_error())
3687 table->auto_increment_field_not_null= FALSE;
3692 switch (table_list->view_check_option(thd, info.get_ignore_errors())) {
3693 case VIEW_CHECK_SKIP:
3695 case VIEW_CHECK_ERROR:
3703 error= write_record(thd, table, &info, &update);
3704 table->auto_increment_field_not_null= FALSE;
3708 if (table->triggers || info.get_duplicate_handling() == DUP_UPDATE)
3718 restore_record(table, s->default_values);
3720 if (table->next_number_field)
3726 if (thd->first_successful_insert_id_in_cur_stmt == 0)
3727 autoinc_value_of_last_inserted_row=
3728 table->next_number_field->val_int();
3733 table->next_number_field->reset();
3740 void select_insert::store_values(
List<Item> &values)
3742 const bool ignore_err=
true;
3743 if (fields->elements)
3745 restore_record(table, s->default_values);
3746 if (!validate_default_values_of_unset_fields(thd, table))
3747 fill_record_n_invoke_before_triggers(thd, *fields, values, ignore_err,
3748 table->triggers, TRG_EVENT_INSERT);
3751 fill_record_n_invoke_before_triggers(thd, table->field, values, ignore_err,
3752 table->triggers, TRG_EVENT_INSERT);
3755 void select_insert::send_error(uint errcode,
const char *err)
3757 DBUG_ENTER(
"select_insert::send_error");
3759 my_message(errcode, err, MYF(0));
3765 bool select_insert::send_eof()
3768 bool const trans_table= table->file->has_transactions();
3769 ulonglong
id, row_count;
3771 THD::killed_state killed_status= thd->killed;
3772 DBUG_ENTER(
"select_insert::send_eof");
3773 DBUG_PRINT(
"enter", (
"trans_table=%d, table_type='%s'",
3776 error= (bulk_insert_started ?
3778 if (!error && thd->is_error())
3779 error= thd->get_stmt_da()->sql_errno();
3781 table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
3782 table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
3791 query_cache_invalidate3(thd, table, 1);
3794 DBUG_ASSERT(trans_table || !changed ||
3795 thd->transaction.stmt.cannot_safely_rollback());
3803 if (mysql_bin_log.is_open() &&
3804 (!error || thd->transaction.stmt.cannot_safely_rollback()))
3810 errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
3811 if (thd->binlog_query(THD::ROW_QUERY_TYPE,
3812 thd->query(), thd->query_length(),
3813 trans_table, FALSE, FALSE, errcode))
3815 table->file->ha_release_auto_increment();
3819 table->file->ha_release_auto_increment();
3827 if (info.get_ignore_errors())
3828 my_snprintf(buff,
sizeof(buff),
3829 ER(ER_INSERT_INFO), (long) info.stats.
records,
3831 (
long) thd->get_stmt_da()->current_statement_warn_count());
3833 my_snprintf(buff,
sizeof(buff),
3834 ER(ER_INSERT_INFO), (
long) info.stats.
records,
3836 (
long) thd->get_stmt_da()->current_statement_warn_count());
3838 ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
3839 info.stats.touched : info.stats.
updated);
3840 id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
3841 thd->first_successful_insert_id_in_cur_stmt :
3842 (thd->arg_of_last_insert_id_function ?
3843 thd->first_successful_insert_id_in_prev_stmt :
3844 (info.stats.
copied ? autoinc_value_of_last_inserted_row : 0));
3845 my_ok(thd, row_count,
id, buff);
3849 void select_insert::abort_result_set() {
3851 DBUG_ENTER(
"select_insert::abort_result_set");
3860 bool changed, transactional_table;
3868 if (bulk_insert_started)
3886 transactional_table= table->file->has_transactions();
3887 if (thd->transaction.stmt.cannot_safely_rollback())
3889 if (mysql_bin_log.is_open())
3891 int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED);
3893 (void) thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(),
3894 thd->query_length(),
3895 transactional_table, FALSE, FALSE, errcode);
3898 query_cache_invalidate3(thd, table, 1);
3900 DBUG_ASSERT(transactional_table || !changed ||
3901 thd->transaction.stmt.cannot_safely_rollback());
3902 table->file->ha_release_auto_increment();
3970 uint select_field_count= items->elements;
3975 DBUG_ENTER(
"create_table_from_items");
3978 tmp_table.s= &share;
3979 init_tmp_table_share(thd, &share,
"", 0,
"",
"");
3981 tmp_table.s->db_create_options=0;
3982 tmp_table.s->db_low_byte_first=
3983 test(create_info->db_type == myisam_hton ||
3984 create_info->db_type == heap_hton);
3985 tmp_table.null_row=tmp_table.maybe_null=0;
3987 if (!thd->variables.explicit_defaults_for_timestamp)
3988 promote_first_timestamp_column(&alter_info->create_list);
3993 Field *field, *def_field;
3994 if (item->type() == Item::FUNC_ITEM)
3995 if (item->result_type() != STRING_RESULT)
3996 field= item->tmp_table_field(&tmp_table);
4000 field= create_tmp_field(thd, &tmp_table, item, item->type(),
4001 (
Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0);
4003 !(cr_field=
new Create_field(field,(item->type() == Item::FIELD_ITEM ?
4008 if (item->maybe_null)
4009 cr_field->flags &= ~NOT_NULL_FLAG;
4010 alter_info->create_list.push_back(cr_field);
4013 DEBUG_SYNC(thd,
"create_table_select_before_create");
4032 if (!mysql_create_table_no_lock(thd, create_table->db,
4033 create_table->table_name,
4034 create_info, alter_info,
4035 select_field_count, NULL))
4037 DEBUG_SYNC(thd,
"create_table_select_before_open");
4039 if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
4048 quick_rm_table(thd, create_info->db_type, create_table->db,
4049 table_case_name(create_info, create_table->table_name),
4053 table= create_table->table;
4068 table= create_table->table;
4091 select_create::prepare(
List<Item> &values, SELECT_LEX_UNIT *u)
4093 DBUG_ENTER(
"select_create::prepare");
4096 DBUG_ASSERT(create_table->table == NULL);
4098 DEBUG_SYNC(thd,
"create_table_select_before_check_if_exists");
4100 if (!(table= create_table_from_items(thd, create_info, create_table,
4101 alter_info, &values)))
4105 if (table->s->fields < values.elements)
4107 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
4111 field= table->field+table->s->fields - values.elements;
4115 DBUG_RETURN(retval);
4128 select_create::prepare2()
4130 DBUG_ENTER(
"select_create::prepare2");
4131 DEBUG_SYNC(thd,
"create_table_select_before_lock");
4154 MY_HOOKS(select_create *x,
TABLE_LIST *create_table_arg,
4157 create_table(create_table_arg),
4158 select_tables(select_tables_arg)
4163 virtual int do_postlock(
TABLE **tables, uint count)
4166 THD *thd=
const_cast<THD*
>(ptr->get_thd());
4167 TABLE_LIST *save_next_global= create_table->next_global;
4169 create_table->next_global= select_tables;
4171 error= thd->decide_logging_format(create_table);
4173 create_table->next_global= save_next_global;
4178 TABLE const *
const table = *tables;
4179 if (thd->is_current_stmt_binlog_format_row() &&
4180 !table->s->tmp_table)
4182 if (
int error= ptr->binlog_show_create_table(tables, count))
4192 MY_HOOKS hooks(
this, create_table, select_tables);
4194 table->reginfo.lock_type=TL_WRITE;
4195 hooks.prelock(&table, 1);
4202 hooks.postlock(&table, 1))
4206 mysql_unlock_tables(thd, extra_lock);
4209 drop_open_table(thd, table, create_table->db, create_table->table_name);
4215 DBUG_ASSERT(m_plock == NULL);
4217 if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
4220 m_plock= &thd->extra_lock;
4222 *m_plock= extra_lock;
4225 for (
Field **f= field ; *f ; f++)
4226 bitmap_set_bit(table->write_set, (*f)->field_index);
4232 table->next_number_field=table->found_next_number_field;
4234 restore_record(table,s->default_values);
4235 thd->cuted_fields=0;
4237 const enum_duplicates duplicate_handling= info.get_duplicate_handling();
4238 const bool ignore_errors= info.get_ignore_errors();
4240 if (ignore_errors || duplicate_handling != DUP_ERROR)
4241 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
4242 if (duplicate_handling == DUP_REPLACE &&
4243 (!table->triggers || !table->triggers->has_delete_triggers()))
4244 table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
4245 if (duplicate_handling == DUP_UPDATE)
4246 table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
4247 if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
4250 bulk_insert_started=
true;
4252 thd->abort_on_warning= (!ignore_errors && thd->is_strict_mode());
4253 if (check_that_all_fields_are_given_values(thd, table, table_list))
4255 table->mark_columns_needed_for_insert();
4256 table->file->extra(HA_EXTRA_WRITE_CACHE);
4261 select_create::binlog_show_create_table(
TABLE **tables, uint count)
4280 DBUG_ASSERT(thd->is_current_stmt_binlog_format_row());
4281 DBUG_ASSERT(tables && *tables && count > 0);
4284 String query(buf,
sizeof(buf), system_charset_info);
4288 memset(&tmp_table_list, 0,
sizeof(tmp_table_list));
4289 tmp_table_list.table = *tables;
4292 result= store_create_info(thd, &tmp_table_list, &query, create_info,
4294 DBUG_ASSERT(result == 0);
4296 if (mysql_bin_log.is_open())
4298 int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED);
4299 result= thd->binlog_query(THD::STMT_QUERY_TYPE,
4300 query.ptr(), query.length(),
4309 void select_create::store_values(
List<Item> &values)
4311 const bool ignore_err=
true;
4312 fill_record_n_invoke_before_triggers(thd, field, values, ignore_err,
4313 table->triggers, TRG_EVENT_INSERT);
4317 void select_create::send_error(uint errcode,
const char *err)
4319 DBUG_ENTER(
"select_create::send_error");
4322 (
"Current statement %s row-based",
4323 thd->is_current_stmt_binlog_format_row() ?
"is" :
"is NOT"));
4325 (
"Current table (at 0x%lu) %s a temporary (or non-existant) table",
4327 table && !table->s->tmp_table ?
"is NOT" :
"is"));
4339 tmp_disable_binlog(thd);
4340 select_insert::send_error(errcode, err);
4341 reenable_binlog(thd);
4347 bool select_create::send_eof()
4354 if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
4355 thd->transaction.stmt.mark_created_temp_table();
4357 bool tmp=select_insert::send_eof();
4367 if (!table->s->tmp_table)
4369 trans_commit_stmt(thd);
4370 trans_commit_implicit(thd);
4373 table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
4374 table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
4377 mysql_unlock_tables(thd, *m_plock);
4386 void select_create::abort_result_set()
4388 DBUG_ENTER(
"select_create::abort_result_set");
4405 tmp_disable_binlog(thd);
4406 select_insert::abort_result_set();
4407 thd->transaction.stmt.reset_unsafe_rollback_flags();
4408 reenable_binlog(thd);
4410 (void) thd->binlog_flush_pending_rows_event(TRUE, TRUE);
4414 mysql_unlock_tables(thd, *m_plock);
4421 table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
4422 table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
4423 table->auto_increment_field_not_null= FALSE;
4424 drop_open_table(thd, table, create_table->db, create_table->table_name);