| 1 | /* |
| 2 | Copyright (c) 2000, 2016, Oracle and/or its affiliates. |
| 3 | Copyright (c) 2010, 2018, MariaDB Corporation |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; version 2 of the License. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | /* Insert of records */ |
| 20 | |
| 21 | /* |
| 22 | INSERT DELAYED |
| 23 | |
| 24 | Insert delayed is distinguished from a normal insert by lock_type == |
| 25 | TL_WRITE_DELAYED instead of TL_WRITE. It first tries to open a |
| 26 | "delayed" table (delayed_get_table()), but falls back to |
| 27 | open_and_lock_tables() on error and proceeds as normal insert then. |
| 28 | |
| 29 | Opening a "delayed" table means to find a delayed insert thread that |
| 30 | has the table open already. If this fails, a new thread is created and |
| 31 | waited for to open and lock the table. |
| 32 | |
| 33 | If accessing the thread succeeded, in |
| 34 | Delayed_insert::get_local_table() the table of the thread is copied |
| 35 | for local use. A copy is required because the normal insert logic |
| 36 | works on a target table, but the other threads table object must not |
| 37 | be used. The insert logic uses the record buffer to create a record. |
| 38 | And the delayed insert thread uses the record buffer to pass the |
| 39 | record to the table handler. So there must be different objects. Also |
| 40 | the copied table is not included in the lock, so that the statement |
| 41 | can proceed even if the real table cannot be accessed at this moment. |
| 42 | |
| 43 | Copying a table object is not a trivial operation. Besides the TABLE |
| 44 | object there are the field pointer array, the field objects and the |
| 45 | record buffer. After copying the field objects, their pointers into |
| 46 | the record must be "moved" to point to the new record buffer. |
| 47 | |
| 48 | After this setup the normal insert logic is used. Only that for |
| 49 | delayed inserts write_delayed() is called instead of write_record(). |
| 50 | It inserts the rows into a queue and signals the delayed insert thread |
| 51 | instead of writing directly to the table. |
| 52 | |
| 53 | The delayed insert thread awakes from the signal. It locks the table, |
| 54 | inserts the rows from the queue, unlocks the table, and waits for the |
| 55 | next signal. It does normally live until a FLUSH TABLES or SHUTDOWN. |
| 56 | |
| 57 | */ |
| 58 | |
| 59 | #include "mariadb.h" /* NO_EMBEDDED_ACCESS_CHECKS */ |
| 60 | #include "sql_priv.h" |
| 61 | #include "sql_insert.h" |
| 62 | #include "sql_update.h" // compare_record |
| 63 | #include "sql_base.h" // close_thread_tables |
| 64 | #include "sql_cache.h" // query_cache_* |
| 65 | #include "key.h" // key_copy |
| 66 | #include "lock.h" // mysql_unlock_tables |
| 67 | #include "sp_head.h" |
| 68 | #include "sql_view.h" // check_key_in_view, insert_view_fields |
| 69 | #include "sql_table.h" // mysql_create_table_no_lock |
| 70 | #include "sql_acl.h" // *_ACL, check_grant_all_columns |
| 71 | #include "sql_trigger.h" |
| 72 | #include "sql_select.h" |
| 73 | #include "sql_show.h" |
| 74 | #include "slave.h" |
| 75 | #include "sql_parse.h" // end_active_trans |
| 76 | #include "rpl_mi.h" |
| 77 | #include "transaction.h" |
| 78 | #include "sql_audit.h" |
| 79 | #include "sql_derived.h" // mysql_handle_derived |
| 80 | #include "sql_prepare.h" |
| 81 | #include <my_bit.h> |
| 82 | |
| 83 | #include "debug_sync.h" |
| 84 | |
| 85 | #ifndef EMBEDDED_LIBRARY |
| 86 | static bool delayed_get_table(THD *thd, MDL_request *grl_protection_request, |
| 87 | TABLE_LIST *table_list); |
| 88 | static int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic, |
| 89 | LEX_STRING query, bool ignore, bool log_on); |
| 90 | static void end_delayed_insert(THD *thd); |
| 91 | pthread_handler_t handle_delayed_insert(void *arg); |
| 92 | static void unlink_blobs(TABLE *table); |
| 93 | #endif |
| 94 | static bool check_view_insertability(THD *thd, TABLE_LIST *view); |
| 95 | |
| 96 | /* |
| 97 | Check that insert/update fields are from the same single table of a view. |
| 98 | |
| 99 | @param fields The insert/update fields to be checked. |
| 100 | @param values The insert/update values to be checked, NULL if |
| 101 | checking is not wanted. |
| 102 | @param view The view for insert. |
| 103 | @param map [in/out] The insert table map. |
| 104 | |
| 105 | This function is called in 2 cases: |
| 106 | 1. to check insert fields. In this case *map will be set to 0. |
| 107 | Insert fields are checked to be all from the same single underlying |
| 108 | table of the given view. Otherwise the error is thrown. Found table |
| 109 | map is returned in the map parameter. |
| 110 | 2. to check update fields of the ON DUPLICATE KEY UPDATE clause. |
| 111 | In this case *map contains table_map found on the previous call of |
| 112 | the function to check insert fields. Update fields are checked to be |
| 113 | from the same table as the insert fields. |
| 114 | |
| 115 | @returns false if success. |
| 116 | */ |
| 117 | |
| 118 | static bool check_view_single_update(List<Item> &fields, List<Item> *values, |
| 119 | TABLE_LIST *view, table_map *map, |
| 120 | bool insert) |
| 121 | { |
| 122 | /* it is join view => we need to find the table for update */ |
| 123 | List_iterator_fast<Item> it(fields); |
| 124 | Item *item; |
| 125 | TABLE_LIST *tbl= 0; // reset for call to check_single_table() |
| 126 | table_map tables= 0; |
| 127 | |
| 128 | while ((item= it++)) |
| 129 | tables|= item->used_tables(); |
| 130 | |
| 131 | /* |
| 132 | Check that table is only one |
| 133 | (we can not rely on check_single_table because it skips some |
| 134 | types of tables) |
| 135 | */ |
| 136 | if (my_count_bits(tables) > 1) |
| 137 | goto error; |
| 138 | |
| 139 | if (values) |
| 140 | { |
| 141 | it.init(*values); |
| 142 | while ((item= it++)) |
| 143 | tables|= item->view_used_tables(view); |
| 144 | } |
| 145 | |
| 146 | /* Convert to real table bits */ |
| 147 | tables&= ~PSEUDO_TABLE_BITS; |
| 148 | |
| 149 | /* Check found map against provided map */ |
| 150 | if (*map) |
| 151 | { |
| 152 | if (tables != *map) |
| 153 | goto error; |
| 154 | return FALSE; |
| 155 | } |
| 156 | |
| 157 | if (view->check_single_table(&tbl, tables, view) || tbl == 0) |
| 158 | goto error; |
| 159 | |
| 160 | /* view->table should have been set in mysql_derived_merge_for_insert */ |
| 161 | DBUG_ASSERT(view->table); |
| 162 | |
| 163 | /* |
| 164 | Use buffer for the insert values that was allocated for the merged view. |
| 165 | */ |
| 166 | tbl->table->insert_values= view->table->insert_values; |
| 167 | view->table= tbl->table; |
| 168 | if (!tbl->single_table_updatable()) |
| 169 | { |
| 170 | if (insert) |
| 171 | my_error(ER_NON_INSERTABLE_TABLE, MYF(0), view->alias.str, "INSERT" ); |
| 172 | else |
| 173 | my_error(ER_NON_UPDATABLE_TABLE, MYF(0), view->alias.str, "UPDATE" ); |
| 174 | return TRUE; |
| 175 | } |
| 176 | *map= tables; |
| 177 | |
| 178 | return FALSE; |
| 179 | |
| 180 | error: |
| 181 | my_error(ER_VIEW_MULTIUPDATE, MYF(0), |
| 182 | view->view_db.str, view->view_name.str); |
| 183 | return TRUE; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /* |
| 188 | Check if insert fields are correct. |
| 189 | |
| 190 | @param thd The current thread. |
| 191 | @param table_list The table we are inserting into (may be view) |
| 192 | @param fields The insert fields. |
| 193 | @param values The insert values. |
| 194 | @param check_unique If duplicate values should be rejected. |
| 195 | @param fields_and_values_from_different_maps If 'values' are allowed to |
| 196 | refer to other tables than those of 'fields' |
| 197 | @param map See check_view_single_update |
| 198 | |
| 199 | @returns 0 if success, -1 if error |
| 200 | */ |
| 201 | |
| 202 | static int check_insert_fields(THD *thd, TABLE_LIST *table_list, |
| 203 | List<Item> &fields, List<Item> &values, |
| 204 | bool check_unique, |
| 205 | bool fields_and_values_from_different_maps, |
| 206 | table_map *map) |
| 207 | { |
| 208 | TABLE *table= table_list->table; |
| 209 | DBUG_ENTER("check_insert_fields" ); |
| 210 | |
| 211 | if (!table_list->single_table_updatable()) |
| 212 | { |
| 213 | my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT" ); |
| 214 | DBUG_RETURN(-1); |
| 215 | } |
| 216 | |
| 217 | if (fields.elements == 0 && values.elements != 0) |
| 218 | { |
| 219 | if (!table) |
| 220 | { |
| 221 | my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0), |
| 222 | table_list->view_db.str, table_list->view_name.str); |
| 223 | DBUG_RETURN(-1); |
| 224 | } |
| 225 | if (values.elements != table->s->visible_fields) |
| 226 | { |
| 227 | my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L); |
| 228 | DBUG_RETURN(-1); |
| 229 | } |
| 230 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 231 | Field_iterator_table_ref field_it; |
| 232 | field_it.set(table_list); |
| 233 | if (check_grant_all_columns(thd, INSERT_ACL, &field_it)) |
| 234 | DBUG_RETURN(-1); |
| 235 | #endif |
| 236 | /* |
| 237 | No fields are provided so all fields must be provided in the values. |
| 238 | Thus we set all bits in the write set. |
| 239 | */ |
| 240 | bitmap_set_all(table->write_set); |
| 241 | } |
| 242 | else |
| 243 | { // Part field list |
| 244 | SELECT_LEX *select_lex= &thd->lex->select_lex; |
| 245 | Name_resolution_context *context= &select_lex->context; |
| 246 | Name_resolution_context_state ctx_state; |
| 247 | int res; |
| 248 | |
| 249 | if (fields.elements != values.elements) |
| 250 | { |
| 251 | my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L); |
| 252 | DBUG_RETURN(-1); |
| 253 | } |
| 254 | |
| 255 | thd->dup_field= 0; |
| 256 | select_lex->no_wrap_view_item= TRUE; |
| 257 | |
| 258 | /* Save the state of the current name resolution context. */ |
| 259 | ctx_state.save_state(context, table_list); |
| 260 | |
| 261 | /* |
| 262 | Perform name resolution only in the first table - 'table_list', |
| 263 | which is the table that is inserted into. |
| 264 | */ |
| 265 | table_list->next_local= 0; |
| 266 | context->resolve_in_table_list_only(table_list); |
| 267 | /* 'Unfix' fields to allow correct marking by the setup_fields function. */ |
| 268 | if (table_list->is_view()) |
| 269 | unfix_fields(fields); |
| 270 | |
| 271 | res= setup_fields(thd, Ref_ptr_array(), |
| 272 | fields, MARK_COLUMNS_WRITE, 0, NULL, 0); |
| 273 | |
| 274 | /* Restore the current context. */ |
| 275 | ctx_state.restore_state(context, table_list); |
| 276 | thd->lex->select_lex.no_wrap_view_item= FALSE; |
| 277 | |
| 278 | if (res) |
| 279 | DBUG_RETURN(-1); |
| 280 | |
| 281 | if (table_list->is_view() && table_list->is_merged_derived()) |
| 282 | { |
| 283 | if (check_view_single_update(fields, |
| 284 | fields_and_values_from_different_maps ? |
| 285 | (List<Item>*) 0 : &values, |
| 286 | table_list, map, true)) |
| 287 | DBUG_RETURN(-1); |
| 288 | table= table_list->table; |
| 289 | } |
| 290 | |
| 291 | if (check_unique && thd->dup_field) |
| 292 | { |
| 293 | my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), |
| 294 | thd->dup_field->field_name.str); |
| 295 | DBUG_RETURN(-1); |
| 296 | } |
| 297 | } |
| 298 | // For the values we need select_priv |
| 299 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 300 | table->grant.want_privilege= (SELECT_ACL & ~table->grant.privilege); |
| 301 | #endif |
| 302 | |
| 303 | if (check_key_in_view(thd, table_list) || |
| 304 | (table_list->view && |
| 305 | check_view_insertability(thd, table_list))) |
| 306 | { |
| 307 | my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT" ); |
| 308 | DBUG_RETURN(-1); |
| 309 | } |
| 310 | |
| 311 | DBUG_RETURN(0); |
| 312 | } |
| 313 | |
| 314 | static bool has_no_default_value(THD *thd, Field *field, TABLE_LIST *table_list) |
| 315 | { |
| 316 | if ((field->flags & NO_DEFAULT_VALUE_FLAG) && field->real_type() != MYSQL_TYPE_ENUM) |
| 317 | { |
| 318 | bool view= false; |
| 319 | if (table_list) |
| 320 | { |
| 321 | table_list= table_list->top_table(); |
| 322 | view= table_list->view != NULL; |
| 323 | } |
| 324 | if (view) |
| 325 | { |
| 326 | push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, ER_NO_DEFAULT_FOR_VIEW_FIELD, |
| 327 | ER_THD(thd, ER_NO_DEFAULT_FOR_VIEW_FIELD), |
| 328 | table_list->view_db.str, table_list->view_name.str); |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, ER_NO_DEFAULT_FOR_FIELD, |
| 333 | ER_THD(thd, ER_NO_DEFAULT_FOR_FIELD), |
| 334 | field->field_name.str); |
| 335 | } |
| 336 | return thd->really_abort_on_warning(); |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /** |
| 343 | Check if update fields are correct. |
| 344 | |
| 345 | @param thd The current thread. |
| 346 | @param insert_table_list The table we are inserting into (may be view) |
| 347 | @param update_fields The update fields. |
| 348 | @param update_values The update values. |
| 349 | @param fields_and_values_from_different_maps If 'update_values' are allowed to |
| 350 | refer to other tables than those of 'update_fields' |
| 351 | @param map See check_view_single_update |
| 352 | |
| 353 | @note |
| 354 | If the update fields include an autoinc field, set the |
| 355 | table->next_number_field_updated flag. |
| 356 | |
| 357 | @returns 0 if success, -1 if error |
| 358 | */ |
| 359 | |
| 360 | static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list, |
| 361 | List<Item> &update_fields, |
| 362 | List<Item> &update_values, |
| 363 | bool fields_and_values_from_different_maps, |
| 364 | table_map *map) |
| 365 | { |
| 366 | TABLE *table= insert_table_list->table; |
| 367 | my_bool UNINIT_VAR(autoinc_mark); |
| 368 | |
| 369 | table->next_number_field_updated= FALSE; |
| 370 | |
| 371 | if (table->found_next_number_field) |
| 372 | { |
| 373 | /* |
| 374 | Unmark the auto_increment field so that we can check if this is modified |
| 375 | by update_fields |
| 376 | */ |
| 377 | autoinc_mark= bitmap_test_and_clear(table->write_set, |
| 378 | table->found_next_number_field-> |
| 379 | field_index); |
| 380 | } |
| 381 | |
| 382 | /* Check the fields we are going to modify */ |
| 383 | if (setup_fields(thd, Ref_ptr_array(), |
| 384 | update_fields, MARK_COLUMNS_WRITE, 0, NULL, 0)) |
| 385 | return -1; |
| 386 | |
| 387 | if (insert_table_list->is_view() && |
| 388 | insert_table_list->is_merged_derived() && |
| 389 | check_view_single_update(update_fields, |
| 390 | fields_and_values_from_different_maps ? |
| 391 | (List<Item>*) 0 : &update_values, |
| 392 | insert_table_list, map, false)) |
| 393 | return -1; |
| 394 | |
| 395 | if (table->default_field) |
| 396 | table->mark_default_fields_for_write(FALSE); |
| 397 | |
| 398 | if (table->found_next_number_field) |
| 399 | { |
| 400 | if (bitmap_is_set(table->write_set, |
| 401 | table->found_next_number_field->field_index)) |
| 402 | table->next_number_field_updated= TRUE; |
| 403 | |
| 404 | if (autoinc_mark) |
| 405 | bitmap_set_bit(table->write_set, |
| 406 | table->found_next_number_field->field_index); |
| 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | Upgrade table-level lock of INSERT statement to TL_WRITE if |
| 414 | a more concurrent lock is infeasible for some reason. This is |
| 415 | necessary for engines without internal locking support (MyISAM). |
| 416 | An engine with internal locking implementation might later |
| 417 | downgrade the lock in handler::store_lock() method. |
| 418 | */ |
| 419 | |
| 420 | static |
| 421 | void upgrade_lock_type(THD *thd, thr_lock_type *lock_type, |
| 422 | enum_duplicates duplic) |
| 423 | { |
| 424 | if (duplic == DUP_UPDATE || |
| 425 | (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT)) |
| 426 | { |
| 427 | *lock_type= TL_WRITE_DEFAULT; |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | if (*lock_type == TL_WRITE_DELAYED) |
| 432 | { |
| 433 | /* |
| 434 | We do not use delayed threads if: |
| 435 | - we're running in the safe mode or skip-new mode -- the |
| 436 | feature is disabled in these modes |
| 437 | - we're executing this statement on a replication slave -- |
| 438 | we need to ensure serial execution of queries on the |
| 439 | slave |
| 440 | - it is INSERT .. ON DUPLICATE KEY UPDATE - in this case the |
| 441 | insert cannot be concurrent |
| 442 | - this statement is directly or indirectly invoked from |
| 443 | a stored function or trigger (under pre-locking) - to |
| 444 | avoid deadlocks, since INSERT DELAYED involves a lock |
| 445 | upgrade (TL_WRITE_DELAYED -> TL_WRITE) which we should not |
| 446 | attempt while keeping other table level locks. |
| 447 | - this statement itself may require pre-locking. |
| 448 | We should upgrade the lock even though in most cases |
| 449 | delayed functionality may work. Unfortunately, we can't |
| 450 | easily identify whether the subject table is not used in |
| 451 | the statement indirectly via a stored function or trigger: |
| 452 | if it is used, that will lead to a deadlock between the |
| 453 | client connection and the delayed thread. |
| 454 | */ |
| 455 | if (specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE) || |
| 456 | thd->variables.max_insert_delayed_threads == 0 || |
| 457 | thd->locked_tables_mode > LTM_LOCK_TABLES || |
| 458 | thd->lex->uses_stored_routines() /*|| |
| 459 | thd->lex->describe*/) |
| 460 | { |
| 461 | *lock_type= TL_WRITE; |
| 462 | return; |
| 463 | } |
| 464 | if (thd->slave_thread) |
| 465 | { |
| 466 | /* Try concurrent insert */ |
| 467 | *lock_type= (duplic == DUP_UPDATE || duplic == DUP_REPLACE) ? |
| 468 | TL_WRITE : TL_WRITE_CONCURRENT_INSERT; |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | bool log_on= (thd->variables.option_bits & OPTION_BIN_LOG); |
| 473 | if (global_system_variables.binlog_format == BINLOG_FORMAT_STMT && |
| 474 | log_on && mysql_bin_log.is_open()) |
| 475 | { |
| 476 | /* |
| 477 | Statement-based binary logging does not work in this case, because: |
| 478 | a) two concurrent statements may have their rows intermixed in the |
| 479 | queue, leading to autoincrement replication problems on slave (because |
| 480 | the values generated used for one statement don't depend only on the |
| 481 | value generated for the first row of this statement, so are not |
| 482 | replicable) |
| 483 | b) if first row of the statement has an error the full statement is |
| 484 | not binlogged, while next rows of the statement may be inserted. |
| 485 | c) if first row succeeds, statement is binlogged immediately with a |
| 486 | zero error code (i.e. "no error"), if then second row fails, query |
| 487 | will fail on slave too and slave will stop (wrongly believing that the |
| 488 | master got no error). |
| 489 | So we fallback to non-delayed INSERT. |
| 490 | Note that to be fully correct, we should test the "binlog format which |
| 491 | the delayed thread is going to use for this row". But in the common case |
| 492 | where the global binlog format is not changed and the session binlog |
| 493 | format may be changed, that is equal to the global binlog format. |
| 494 | We test it without mutex for speed reasons (condition rarely true), and |
| 495 | in the common case (global not changed) it is as good as without mutex; |
| 496 | if global value is changed, anyway there is uncertainty as the delayed |
| 497 | thread may be old and use the before-the-change value. |
| 498 | */ |
| 499 | *lock_type= TL_WRITE; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
| 505 | /** |
| 506 | Find or create a delayed insert thread for the first table in |
| 507 | the table list, then open and lock the remaining tables. |
| 508 | If a table can not be used with insert delayed, upgrade the lock |
| 509 | and open and lock all tables using the standard mechanism. |
| 510 | |
| 511 | @param thd thread context |
| 512 | @param table_list list of "descriptors" for tables referenced |
| 513 | directly in statement SQL text. |
| 514 | The first element in the list corresponds to |
| 515 | the destination table for inserts, remaining |
| 516 | tables, if any, are usually tables referenced |
| 517 | by sub-queries in the right part of the |
| 518 | INSERT. |
| 519 | |
| 520 | @return Status of the operation. In case of success 'table' |
| 521 | member of every table_list element points to an instance of |
| 522 | class TABLE. |
| 523 | |
| 524 | @sa open_and_lock_tables for more information about MySQL table |
| 525 | level locking |
| 526 | */ |
| 527 | |
| 528 | static |
| 529 | bool open_and_lock_for_insert_delayed(THD *thd, TABLE_LIST *table_list) |
| 530 | { |
| 531 | MDL_request protection_request; |
| 532 | DBUG_ENTER("open_and_lock_for_insert_delayed" ); |
| 533 | |
| 534 | #ifndef EMBEDDED_LIBRARY |
| 535 | /* INSERT DELAYED is not allowed in a read only transaction. */ |
| 536 | if (thd->tx_read_only) |
| 537 | { |
| 538 | my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0)); |
| 539 | DBUG_RETURN(true); |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | In order for the deadlock detector to be able to find any deadlocks |
| 544 | caused by the handler thread waiting for GRL or this table, we acquire |
| 545 | protection against GRL (global IX metadata lock) and metadata lock on |
| 546 | table to being inserted into inside the connection thread. |
| 547 | If this goes ok, the tickets are cloned and added to the list of granted |
| 548 | locks held by the handler thread. |
| 549 | */ |
| 550 | if (thd->global_read_lock.can_acquire_protection()) |
| 551 | DBUG_RETURN(TRUE); |
| 552 | |
| 553 | protection_request.init(MDL_key::GLOBAL, "" , "" , MDL_INTENTION_EXCLUSIVE, |
| 554 | MDL_STATEMENT); |
| 555 | |
| 556 | if (thd->mdl_context.acquire_lock(&protection_request, |
| 557 | thd->variables.lock_wait_timeout)) |
| 558 | DBUG_RETURN(TRUE); |
| 559 | |
| 560 | if (thd->mdl_context.acquire_lock(&table_list->mdl_request, |
| 561 | thd->variables.lock_wait_timeout)) |
| 562 | /* |
| 563 | If a lock can't be acquired, it makes no sense to try normal insert. |
| 564 | Therefore we just abort the statement. |
| 565 | */ |
| 566 | DBUG_RETURN(TRUE); |
| 567 | |
| 568 | bool error= FALSE; |
| 569 | if (delayed_get_table(thd, &protection_request, table_list)) |
| 570 | error= TRUE; |
| 571 | else if (table_list->table) |
| 572 | { |
| 573 | /* |
| 574 | Open tables used for sub-selects or in stored functions, will also |
| 575 | cache these functions. |
| 576 | */ |
| 577 | if (open_and_lock_tables(thd, table_list->next_global, TRUE, 0)) |
| 578 | { |
| 579 | end_delayed_insert(thd); |
| 580 | error= TRUE; |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | /* |
| 585 | First table was not processed by open_and_lock_tables(), |
| 586 | we need to set updatability flag "by hand". |
| 587 | */ |
| 588 | if (!table_list->derived && !table_list->view) |
| 589 | table_list->updatable= 1; // usual table |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | We can't release protection against GRL and metadata lock on the table |
| 595 | being inserted into here. These locks might be required, for example, |
| 596 | because this INSERT DELAYED calls functions which may try to update |
| 597 | this or another tables (updating the same table is of course illegal, |
| 598 | but such an attempt can be discovered only later during statement |
| 599 | execution). |
| 600 | */ |
| 601 | |
| 602 | /* |
| 603 | Reset the ticket in case we end up having to use normal insert and |
| 604 | therefore will reopen the table and reacquire the metadata lock. |
| 605 | */ |
| 606 | table_list->mdl_request.ticket= NULL; |
| 607 | |
| 608 | if (error || table_list->table) |
| 609 | DBUG_RETURN(error); |
| 610 | #endif |
| 611 | /* |
| 612 | * This is embedded library and we don't have auxiliary |
| 613 | threads OR |
| 614 | * a lock upgrade was requested inside delayed_get_table |
| 615 | because |
| 616 | - there are too many delayed insert threads OR |
| 617 | - the table has triggers. |
| 618 | Use a normal insert. |
| 619 | */ |
| 620 | table_list->lock_type= TL_WRITE; |
| 621 | DBUG_RETURN(open_and_lock_tables(thd, table_list, TRUE, 0)); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | /** |
| 626 | Create a new query string for removing DELAYED keyword for |
| 627 | multi INSERT DEALAYED statement. |
| 628 | |
| 629 | @param[in] thd Thread handler |
| 630 | @param[in] buf Query string |
| 631 | |
| 632 | @return |
| 633 | 0 ok |
| 634 | 1 error |
| 635 | */ |
| 636 | static int |
| 637 | create_insert_stmt_from_insert_delayed(THD *thd, String *buf) |
| 638 | { |
| 639 | /* Make a copy of thd->query() and then remove the "DELAYED" keyword */ |
| 640 | if (buf->append(thd->query()) || |
| 641 | buf->replace(thd->lex->keyword_delayed_begin_offset, |
| 642 | thd->lex->keyword_delayed_end_offset - |
| 643 | thd->lex->keyword_delayed_begin_offset, 0)) |
| 644 | return 1; |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | static void save_insert_query_plan(THD* thd, TABLE_LIST *table_list) |
| 650 | { |
| 651 | Explain_insert* explain= new (thd->mem_root) Explain_insert(thd->mem_root); |
| 652 | explain->table_name.append(table_list->table->alias); |
| 653 | |
| 654 | thd->lex->explain->add_insert_plan(explain); |
| 655 | |
| 656 | /* See Update_plan::updating_a_view for details */ |
| 657 | bool skip= MY_TEST(table_list->view); |
| 658 | |
| 659 | /* Save subquery children */ |
| 660 | for (SELECT_LEX_UNIT *unit= thd->lex->select_lex.first_inner_unit(); |
| 661 | unit; |
| 662 | unit= unit->next_unit()) |
| 663 | { |
| 664 | if (skip) |
| 665 | { |
| 666 | skip= false; |
| 667 | continue; |
| 668 | } |
| 669 | /* |
| 670 | Table elimination doesn't work for INSERTS, but let's still have this |
| 671 | here for consistency |
| 672 | */ |
| 673 | if (!(unit->item && unit->item->eliminated)) |
| 674 | explain->add_child(unit->first_select()->select_number); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | |
| 679 | Field **TABLE::field_to_fill() |
| 680 | { |
| 681 | return triggers && triggers->nullable_fields() ? triggers->nullable_fields() : field; |
| 682 | } |
| 683 | |
| 684 | |
| 685 | /** |
| 686 | INSERT statement implementation |
| 687 | |
| 688 | @note Like implementations of other DDL/DML in MySQL, this function |
| 689 | relies on the caller to close the thread tables. This is done in the |
| 690 | end of dispatch_command(). |
| 691 | */ |
| 692 | |
| 693 | bool mysql_insert(THD *thd,TABLE_LIST *table_list, |
| 694 | List<Item> &fields, |
| 695 | List<List_item> &values_list, |
| 696 | List<Item> &update_fields, |
| 697 | List<Item> &update_values, |
| 698 | enum_duplicates duplic, |
| 699 | bool ignore) |
| 700 | { |
| 701 | bool retval= true; |
| 702 | int error, res; |
| 703 | bool transactional_table, joins_freed= FALSE; |
| 704 | bool changed; |
| 705 | const bool was_insert_delayed= (table_list->lock_type == TL_WRITE_DELAYED); |
| 706 | bool using_bulk_insert= 0; |
| 707 | uint value_count; |
| 708 | ulong counter = 1; |
| 709 | /* counter of iteration in bulk PS operation*/ |
| 710 | ulonglong iteration= 0; |
| 711 | ulonglong id; |
| 712 | COPY_INFO info; |
| 713 | TABLE *table= 0; |
| 714 | List_iterator_fast<List_item> its(values_list); |
| 715 | List_item *values; |
| 716 | Name_resolution_context *context; |
| 717 | Name_resolution_context_state ctx_state; |
| 718 | #ifndef EMBEDDED_LIBRARY |
| 719 | char *query= thd->query(); |
| 720 | /* |
| 721 | log_on is about delayed inserts only. |
| 722 | By default, both logs are enabled (this won't cause problems if the server |
| 723 | runs without --log-bin). |
| 724 | */ |
| 725 | bool log_on= (thd->variables.option_bits & OPTION_BIN_LOG); |
| 726 | #endif |
| 727 | thr_lock_type lock_type; |
| 728 | Item *unused_conds= 0; |
| 729 | DBUG_ENTER("mysql_insert" ); |
| 730 | |
| 731 | create_explain_query(thd->lex, thd->mem_root); |
| 732 | /* |
| 733 | Upgrade lock type if the requested lock is incompatible with |
| 734 | the current connection mode or table operation. |
| 735 | */ |
| 736 | upgrade_lock_type(thd, &table_list->lock_type, duplic); |
| 737 | |
| 738 | /* |
| 739 | We can't write-delayed into a table locked with LOCK TABLES: |
| 740 | this will lead to a deadlock, since the delayed thread will |
| 741 | never be able to get a lock on the table. |
| 742 | */ |
| 743 | if (table_list->lock_type == TL_WRITE_DELAYED && |
| 744 | thd->locked_tables_mode && |
| 745 | find_locked_table(thd->open_tables, table_list->db.str, |
| 746 | table_list->table_name.str)) |
| 747 | { |
| 748 | my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0), |
| 749 | table_list->table_name.str); |
| 750 | DBUG_RETURN(TRUE); |
| 751 | } |
| 752 | |
| 753 | if (table_list->lock_type == TL_WRITE_DELAYED) |
| 754 | { |
| 755 | if (open_and_lock_for_insert_delayed(thd, table_list)) |
| 756 | DBUG_RETURN(TRUE); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | if (open_and_lock_tables(thd, table_list, TRUE, 0)) |
| 761 | DBUG_RETURN(TRUE); |
| 762 | } |
| 763 | |
| 764 | THD_STAGE_INFO(thd, stage_init_update); |
| 765 | lock_type= table_list->lock_type; |
| 766 | thd->lex->used_tables=0; |
| 767 | values= its++; |
| 768 | if (bulk_parameters_set(thd)) |
| 769 | DBUG_RETURN(TRUE); |
| 770 | value_count= values->elements; |
| 771 | |
| 772 | if (mysql_prepare_insert(thd, table_list, table, fields, values, |
| 773 | update_fields, update_values, duplic, &unused_conds, |
| 774 | FALSE)) |
| 775 | goto abort; |
| 776 | |
| 777 | /* mysql_prepare_insert sets table_list->table if it was not set */ |
| 778 | table= table_list->table; |
| 779 | |
| 780 | context= &thd->lex->select_lex.context; |
| 781 | /* |
| 782 | These three asserts test the hypothesis that the resetting of the name |
| 783 | resolution context below is not necessary at all since the list of local |
| 784 | tables for INSERT always consists of one table. |
| 785 | */ |
| 786 | DBUG_ASSERT(!table_list->next_local); |
| 787 | DBUG_ASSERT(!context->table_list->next_local); |
| 788 | DBUG_ASSERT(!context->first_name_resolution_table->next_name_resolution_table); |
| 789 | |
| 790 | /* Save the state of the current name resolution context. */ |
| 791 | ctx_state.save_state(context, table_list); |
| 792 | |
| 793 | /* |
| 794 | Perform name resolution only in the first table - 'table_list', |
| 795 | which is the table that is inserted into. |
| 796 | */ |
| 797 | table_list->next_local= 0; |
| 798 | context->resolve_in_table_list_only(table_list); |
| 799 | switch_to_nullable_trigger_fields(*values, table); |
| 800 | |
| 801 | while ((values= its++)) |
| 802 | { |
| 803 | counter++; |
| 804 | if (values->elements != value_count) |
| 805 | { |
| 806 | my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter); |
| 807 | goto abort; |
| 808 | } |
| 809 | if (setup_fields(thd, Ref_ptr_array(), |
| 810 | *values, MARK_COLUMNS_READ, 0, NULL, 0)) |
| 811 | goto abort; |
| 812 | switch_to_nullable_trigger_fields(*values, table); |
| 813 | } |
| 814 | its.rewind (); |
| 815 | |
| 816 | /* Restore the current context. */ |
| 817 | ctx_state.restore_state(context, table_list); |
| 818 | |
| 819 | if (thd->lex->unit.first_select()->optimize_unflattened_subqueries(false)) |
| 820 | { |
| 821 | goto abort; |
| 822 | } |
| 823 | save_insert_query_plan(thd, table_list); |
| 824 | if (thd->lex->describe) |
| 825 | { |
| 826 | retval= thd->lex->explain->send_explain(thd); |
| 827 | goto abort; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | Fill in the given fields and dump it to the table file |
| 832 | */ |
| 833 | bzero((char*) &info,sizeof(info)); |
| 834 | info.ignore= ignore; |
| 835 | info.handle_duplicates=duplic; |
| 836 | info.update_fields= &update_fields; |
| 837 | info.update_values= &update_values; |
| 838 | info.view= (table_list->view ? table_list : 0); |
| 839 | info.table_list= table_list; |
| 840 | |
| 841 | /* |
| 842 | Count warnings for all inserts. |
| 843 | For single line insert, generate an error if try to set a NOT NULL field |
| 844 | to NULL. |
| 845 | */ |
| 846 | thd->count_cuted_fields= ((values_list.elements == 1 && |
| 847 | !ignore) ? |
| 848 | CHECK_FIELD_ERROR_FOR_NULL : |
| 849 | CHECK_FIELD_WARN); |
| 850 | thd->cuted_fields = 0L; |
| 851 | table->next_number_field=table->found_next_number_field; |
| 852 | |
| 853 | #ifdef HAVE_REPLICATION |
| 854 | if (thd->rgi_slave && |
| 855 | (info.handle_duplicates == DUP_UPDATE) && |
| 856 | (table->next_number_field != NULL) && |
| 857 | rpl_master_has_bug(thd->rgi_slave->rli, 24432, TRUE, NULL, NULL)) |
| 858 | goto abort; |
| 859 | #endif |
| 860 | |
| 861 | error=0; |
| 862 | if (duplic == DUP_REPLACE && |
| 863 | (!table->triggers || !table->triggers->has_delete_triggers())) |
| 864 | table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE); |
| 865 | if (duplic == DUP_UPDATE) |
| 866 | table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE); |
| 867 | /* |
| 868 | let's *try* to start bulk inserts. It won't necessary |
| 869 | start them as values_list.elements should be greater than |
| 870 | some - handler dependent - threshold. |
| 871 | We should not start bulk inserts if this statement uses |
| 872 | functions or invokes triggers since they may access |
| 873 | to the same table and therefore should not see its |
| 874 | inconsistent state created by this optimization. |
| 875 | So we call start_bulk_insert to perform nesessary checks on |
| 876 | values_list.elements, and - if nothing else - to initialize |
| 877 | the code to make the call of end_bulk_insert() below safe. |
| 878 | */ |
| 879 | #ifndef EMBEDDED_LIBRARY |
| 880 | if (lock_type != TL_WRITE_DELAYED) |
| 881 | #endif /* EMBEDDED_LIBRARY */ |
| 882 | { |
| 883 | if (duplic != DUP_ERROR || ignore) |
| 884 | table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); |
| 885 | /** |
| 886 | This is a simple check for the case when the table has a trigger |
| 887 | that reads from it, or when the statement invokes a stored function |
| 888 | that reads from the table being inserted to. |
| 889 | Engines can't handle a bulk insert in parallel with a read form the |
| 890 | same table in the same connection. |
| 891 | */ |
| 892 | if (thd->locked_tables_mode <= LTM_LOCK_TABLES && |
| 893 | values_list.elements > 1) |
| 894 | { |
| 895 | using_bulk_insert= 1; |
| 896 | table->file->ha_start_bulk_insert(values_list.elements); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | thd->abort_on_warning= !ignore && thd->is_strict_mode(); |
| 901 | |
| 902 | table->reset_default_fields(); |
| 903 | table->prepare_triggers_for_insert_stmt_or_event(); |
| 904 | table->mark_columns_needed_for_insert(); |
| 905 | |
| 906 | if (fields.elements || !value_count || table_list->view != 0) |
| 907 | { |
| 908 | if (table->triggers && |
| 909 | table->triggers->has_triggers(TRG_EVENT_INSERT, TRG_ACTION_BEFORE)) |
| 910 | { |
| 911 | /* BEFORE INSERT triggers exist, the check will be done later, per row */ |
| 912 | } |
| 913 | else if (check_that_all_fields_are_given_values(thd, table, table_list)) |
| 914 | { |
| 915 | error= 1; |
| 916 | goto values_loop_end; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | if (table_list->prepare_where(thd, 0, TRUE) || |
| 921 | table_list->prepare_check_option(thd)) |
| 922 | error= 1; |
| 923 | |
| 924 | switch_to_nullable_trigger_fields(fields, table); |
| 925 | switch_to_nullable_trigger_fields(update_fields, table); |
| 926 | switch_to_nullable_trigger_fields(update_values, table); |
| 927 | |
| 928 | if (fields.elements || !value_count) |
| 929 | { |
| 930 | /* |
| 931 | There are possibly some default values: |
| 932 | INSERT INTO t1 (fields) VALUES ... |
| 933 | INSERT INTO t1 VALUES () |
| 934 | */ |
| 935 | if (table->validate_default_values_of_unset_fields(thd)) |
| 936 | { |
| 937 | error= 1; |
| 938 | goto values_loop_end; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | THD_STAGE_INFO(thd, stage_update); |
| 943 | do |
| 944 | { |
| 945 | DBUG_PRINT("info" , ("iteration %llu" , iteration)); |
| 946 | if (iteration && bulk_parameters_set(thd)) |
| 947 | goto abort; |
| 948 | |
| 949 | while ((values= its++)) |
| 950 | { |
| 951 | if (fields.elements || !value_count) |
| 952 | { |
| 953 | /* |
| 954 | There are possibly some default values: |
| 955 | INSERT INTO t1 (fields) VALUES ... |
| 956 | INSERT INTO t1 VALUES () |
| 957 | */ |
| 958 | restore_record(table,s->default_values); // Get empty record |
| 959 | table->reset_default_fields(); |
| 960 | if (unlikely(fill_record_n_invoke_before_triggers(thd, table, fields, |
| 961 | *values, 0, |
| 962 | TRG_EVENT_INSERT))) |
| 963 | { |
| 964 | if (values_list.elements != 1 && ! thd->is_error()) |
| 965 | { |
| 966 | info.records++; |
| 967 | continue; |
| 968 | } |
| 969 | /* |
| 970 | TODO: set thd->abort_on_warning if values_list.elements == 1 |
| 971 | and check that all items return warning in case of problem with |
| 972 | storing field. |
| 973 | */ |
| 974 | error=1; |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | /* |
| 981 | No field list, all fields are set explicitly: |
| 982 | INSERT INTO t1 VALUES (values) |
| 983 | */ |
| 984 | if (thd->lex->used_tables || // Column used in values() |
| 985 | table->s->visible_fields != table->s->fields) |
| 986 | restore_record(table,s->default_values); // Get empty record |
| 987 | else |
| 988 | { |
| 989 | TABLE_SHARE *share= table->s; |
| 990 | |
| 991 | /* |
| 992 | Fix delete marker. No need to restore rest of record since it will |
| 993 | be overwritten by fill_record() anyway (and fill_record() does not |
| 994 | use default values in this case). |
| 995 | */ |
| 996 | table->record[0][0]= share->default_values[0]; |
| 997 | |
| 998 | /* Fix undefined null_bits. */ |
| 999 | if (share->null_bytes > 1 && share->last_null_bit_pos) |
| 1000 | { |
| 1001 | table->record[0][share->null_bytes - 1]= |
| 1002 | share->default_values[share->null_bytes - 1]; |
| 1003 | } |
| 1004 | } |
| 1005 | table->reset_default_fields(); |
| 1006 | if (unlikely(fill_record_n_invoke_before_triggers(thd, table, |
| 1007 | table-> |
| 1008 | field_to_fill(), |
| 1009 | *values, 0, |
| 1010 | TRG_EVENT_INSERT))) |
| 1011 | { |
| 1012 | if (values_list.elements != 1 && ! thd->is_error()) |
| 1013 | { |
| 1014 | info.records++; |
| 1015 | continue; |
| 1016 | } |
| 1017 | error=1; |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | with triggers a field can get a value *conditionally*, so we have to |
| 1024 | repeat has_no_default_value() check for every row |
| 1025 | */ |
| 1026 | if (table->triggers && |
| 1027 | table->triggers->has_triggers(TRG_EVENT_INSERT, TRG_ACTION_BEFORE)) |
| 1028 | { |
| 1029 | for (Field **f=table->field ; *f ; f++) |
| 1030 | { |
| 1031 | if (unlikely(!(*f)->has_explicit_value() && |
| 1032 | has_no_default_value(thd, *f, table_list))) |
| 1033 | { |
| 1034 | error= 1; |
| 1035 | goto values_loop_end; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | if ((res= table_list->view_check_option(thd, |
| 1041 | (values_list.elements == 1 ? |
| 1042 | 0 : |
| 1043 | ignore))) == |
| 1044 | VIEW_CHECK_SKIP) |
| 1045 | continue; |
| 1046 | else if (res == VIEW_CHECK_ERROR) |
| 1047 | { |
| 1048 | error= 1; |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | #ifndef EMBEDDED_LIBRARY |
| 1053 | if (lock_type == TL_WRITE_DELAYED) |
| 1054 | { |
| 1055 | LEX_STRING const st_query = { query, thd->query_length() }; |
| 1056 | DEBUG_SYNC(thd, "before_write_delayed" ); |
| 1057 | error=write_delayed(thd, table, duplic, st_query, ignore, log_on); |
| 1058 | DEBUG_SYNC(thd, "after_write_delayed" ); |
| 1059 | query=0; |
| 1060 | } |
| 1061 | else |
| 1062 | #endif |
| 1063 | error=write_record(thd, table ,&info); |
| 1064 | if (unlikely(error)) |
| 1065 | break; |
| 1066 | thd->get_stmt_da()->inc_current_row_for_warning(); |
| 1067 | } |
| 1068 | its.rewind(); |
| 1069 | iteration++; |
| 1070 | } while (bulk_parameters_iterations(thd)); |
| 1071 | |
| 1072 | values_loop_end: |
| 1073 | free_underlaid_joins(thd, &thd->lex->select_lex); |
| 1074 | joins_freed= TRUE; |
| 1075 | |
| 1076 | /* |
| 1077 | Now all rows are inserted. Time to update logs and sends response to |
| 1078 | user |
| 1079 | */ |
| 1080 | #ifndef EMBEDDED_LIBRARY |
| 1081 | if (unlikely(lock_type == TL_WRITE_DELAYED)) |
| 1082 | { |
| 1083 | if (likely(!error)) |
| 1084 | { |
| 1085 | info.copied=values_list.elements; |
| 1086 | end_delayed_insert(thd); |
| 1087 | } |
| 1088 | } |
| 1089 | else |
| 1090 | #endif |
| 1091 | { |
| 1092 | /* |
| 1093 | Do not do this release if this is a delayed insert, it would steal |
| 1094 | auto_inc values from the delayed_insert thread as they share TABLE. |
| 1095 | */ |
| 1096 | table->file->ha_release_auto_increment(); |
| 1097 | if (using_bulk_insert && unlikely(table->file->ha_end_bulk_insert()) && |
| 1098 | !error) |
| 1099 | { |
| 1100 | table->file->print_error(my_errno,MYF(0)); |
| 1101 | error=1; |
| 1102 | } |
| 1103 | if (duplic != DUP_ERROR || ignore) |
| 1104 | table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); |
| 1105 | |
| 1106 | transactional_table= table->file->has_transactions(); |
| 1107 | |
| 1108 | if (likely(changed= (info.copied || info.deleted || info.updated))) |
| 1109 | { |
| 1110 | /* |
| 1111 | Invalidate the table in the query cache if something changed. |
| 1112 | For the transactional algorithm to work the invalidation must be |
| 1113 | before binlog writing and ha_autocommit_or_rollback |
| 1114 | */ |
| 1115 | query_cache_invalidate3(thd, table_list, 1); |
| 1116 | } |
| 1117 | |
| 1118 | if (thd->transaction.stmt.modified_non_trans_table) |
| 1119 | thd->transaction.all.modified_non_trans_table= TRUE; |
| 1120 | thd->transaction.all.m_unsafe_rollback_flags|= |
| 1121 | (thd->transaction.stmt.m_unsafe_rollback_flags & THD_TRANS::DID_WAIT); |
| 1122 | |
| 1123 | if (error <= 0 || |
| 1124 | thd->transaction.stmt.modified_non_trans_table || |
| 1125 | was_insert_delayed) |
| 1126 | { |
| 1127 | if(WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open()) |
| 1128 | { |
| 1129 | int errcode= 0; |
| 1130 | if (error <= 0) |
| 1131 | { |
| 1132 | /* |
| 1133 | [Guilhem wrote] Temporary errors may have filled |
| 1134 | thd->net.last_error/errno. For example if there has |
| 1135 | been a disk full error when writing the row, and it was |
| 1136 | MyISAM, then thd->net.last_error/errno will be set to |
| 1137 | "disk full"... and the mysql_file_pwrite() will wait until free |
| 1138 | space appears, and so when it finishes then the |
| 1139 | write_row() was entirely successful |
| 1140 | */ |
| 1141 | /* todo: consider removing */ |
| 1142 | thd->clear_error(); |
| 1143 | } |
| 1144 | else |
| 1145 | errcode= query_error_code(thd, thd->killed == NOT_KILLED); |
| 1146 | |
| 1147 | ScopedStatementReplication scoped_stmt_rpl( |
| 1148 | table->versioned(VERS_TRX_ID) ? thd : NULL); |
| 1149 | /* bug#22725: |
| 1150 | |
| 1151 | A query which per-row-loop can not be interrupted with |
| 1152 | KILLED, like INSERT, and that does not invoke stored |
| 1153 | routines can be binlogged with neglecting the KILLED error. |
| 1154 | |
| 1155 | If there was no error (error == zero) until after the end of |
| 1156 | inserting loop the KILLED flag that appeared later can be |
| 1157 | disregarded since previously possible invocation of stored |
| 1158 | routines did not result in any error due to the KILLED. In |
| 1159 | such case the flag is ignored for constructing binlog event. |
| 1160 | */ |
| 1161 | DBUG_ASSERT(thd->killed != KILL_BAD_DATA || error > 0); |
| 1162 | if (was_insert_delayed && table_list->lock_type == TL_WRITE) |
| 1163 | { |
| 1164 | /* Binlog INSERT DELAYED as INSERT without DELAYED. */ |
| 1165 | String log_query; |
| 1166 | if (create_insert_stmt_from_insert_delayed(thd, &log_query)) |
| 1167 | { |
| 1168 | sql_print_error("Event Error: An error occurred while creating query string" |
| 1169 | "for INSERT DELAYED stmt, before writing it into binary log." ); |
| 1170 | |
| 1171 | error= 1; |
| 1172 | } |
| 1173 | else if (thd->binlog_query(THD::ROW_QUERY_TYPE, |
| 1174 | log_query.c_ptr(), log_query.length(), |
| 1175 | transactional_table, FALSE, FALSE, |
| 1176 | errcode)) |
| 1177 | error= 1; |
| 1178 | } |
| 1179 | else if (thd->binlog_query(THD::ROW_QUERY_TYPE, |
| 1180 | thd->query(), thd->query_length(), |
| 1181 | transactional_table, FALSE, FALSE, |
| 1182 | errcode)) |
| 1183 | error= 1; |
| 1184 | } |
| 1185 | } |
| 1186 | DBUG_ASSERT(transactional_table || !changed || |
| 1187 | thd->transaction.stmt.modified_non_trans_table); |
| 1188 | } |
| 1189 | THD_STAGE_INFO(thd, stage_end); |
| 1190 | /* |
| 1191 | We'll report to the client this id: |
| 1192 | - if the table contains an autoincrement column and we successfully |
| 1193 | inserted an autogenerated value, the autogenerated value. |
| 1194 | - if the table contains no autoincrement column and LAST_INSERT_ID(X) was |
| 1195 | called, X. |
| 1196 | - if the table contains an autoincrement column, and some rows were |
| 1197 | inserted, the id of the last "inserted" row (if IGNORE, that value may not |
| 1198 | have been really inserted but ignored). |
| 1199 | */ |
| 1200 | id= (thd->first_successful_insert_id_in_cur_stmt > 0) ? |
| 1201 | thd->first_successful_insert_id_in_cur_stmt : |
| 1202 | (thd->arg_of_last_insert_id_function ? |
| 1203 | thd->first_successful_insert_id_in_prev_stmt : |
| 1204 | ((table->next_number_field && info.copied) ? |
| 1205 | table->next_number_field->val_int() : 0)); |
| 1206 | table->next_number_field=0; |
| 1207 | thd->count_cuted_fields= CHECK_FIELD_IGNORE; |
| 1208 | table->auto_increment_field_not_null= FALSE; |
| 1209 | if (duplic == DUP_REPLACE && |
| 1210 | (!table->triggers || !table->triggers->has_delete_triggers())) |
| 1211 | table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); |
| 1212 | |
| 1213 | if (unlikely(error)) |
| 1214 | goto abort; |
| 1215 | if (thd->lex->analyze_stmt) |
| 1216 | { |
| 1217 | retval= thd->lex->explain->send_explain(thd); |
| 1218 | goto abort; |
| 1219 | } |
| 1220 | if ((iteration * values_list.elements) == 1 && (!(thd->variables.option_bits & OPTION_WARNINGS) || |
| 1221 | !thd->cuted_fields)) |
| 1222 | { |
| 1223 | my_ok(thd, info.copied + info.deleted + |
| 1224 | ((thd->client_capabilities & CLIENT_FOUND_ROWS) ? |
| 1225 | info.touched : info.updated), |
| 1226 | id); |
| 1227 | } |
| 1228 | else |
| 1229 | { |
| 1230 | char buff[160]; |
| 1231 | ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ? |
| 1232 | info.touched : info.updated); |
| 1233 | if (ignore) |
| 1234 | sprintf(buff, ER_THD(thd, ER_INSERT_INFO), (ulong) info.records, |
| 1235 | (lock_type == TL_WRITE_DELAYED) ? (ulong) 0 : |
| 1236 | (ulong) (info.records - info.copied), |
| 1237 | (long) thd->get_stmt_da()->current_statement_warn_count()); |
| 1238 | else |
| 1239 | sprintf(buff, ER_THD(thd, ER_INSERT_INFO), (ulong) info.records, |
| 1240 | (ulong) (info.deleted + updated), |
| 1241 | (long) thd->get_stmt_da()->current_statement_warn_count()); |
| 1242 | ::my_ok(thd, info.copied + info.deleted + updated, id, buff); |
| 1243 | } |
| 1244 | thd->abort_on_warning= 0; |
| 1245 | if (thd->lex->current_select->first_cond_optimization) |
| 1246 | { |
| 1247 | thd->lex->current_select->save_leaf_tables(thd); |
| 1248 | thd->lex->current_select->first_cond_optimization= 0; |
| 1249 | } |
| 1250 | |
| 1251 | DBUG_RETURN(FALSE); |
| 1252 | |
| 1253 | abort: |
| 1254 | #ifndef EMBEDDED_LIBRARY |
| 1255 | if (lock_type == TL_WRITE_DELAYED) |
| 1256 | end_delayed_insert(thd); |
| 1257 | #endif |
| 1258 | if (table != NULL) |
| 1259 | table->file->ha_release_auto_increment(); |
| 1260 | |
| 1261 | if (!joins_freed) |
| 1262 | free_underlaid_joins(thd, &thd->lex->select_lex); |
| 1263 | thd->abort_on_warning= 0; |
| 1264 | DBUG_RETURN(retval); |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | /* |
| 1269 | Additional check for insertability for VIEW |
| 1270 | |
| 1271 | SYNOPSIS |
| 1272 | check_view_insertability() |
| 1273 | thd - thread handler |
| 1274 | view - reference on VIEW |
| 1275 | |
| 1276 | IMPLEMENTATION |
| 1277 | A view is insertable if the folloings are true: |
| 1278 | - All columns in the view are columns from a table |
| 1279 | - All not used columns in table have a default values |
| 1280 | - All field in view are unique (not referring to the same column) |
| 1281 | |
| 1282 | RETURN |
| 1283 | FALSE - OK |
| 1284 | view->contain_auto_increment is 1 if and only if the view contains an |
| 1285 | auto_increment field |
| 1286 | |
| 1287 | TRUE - can't be used for insert |
| 1288 | */ |
| 1289 | |
| 1290 | static bool check_view_insertability(THD * thd, TABLE_LIST *view) |
| 1291 | { |
| 1292 | uint num= view->view->select_lex.item_list.elements; |
| 1293 | TABLE *table= view->table; |
| 1294 | Field_translator *trans_start= view->field_translation, |
| 1295 | *trans_end= trans_start + num; |
| 1296 | Field_translator *trans; |
| 1297 | uint used_fields_buff_size= bitmap_buffer_size(table->s->fields); |
| 1298 | uint32 *used_fields_buff= (uint32*)thd->alloc(used_fields_buff_size); |
| 1299 | MY_BITMAP used_fields; |
| 1300 | enum_column_usage saved_column_usage= thd->column_usage; |
| 1301 | DBUG_ENTER("check_key_in_view" ); |
| 1302 | |
| 1303 | if (!used_fields_buff) |
| 1304 | DBUG_RETURN(TRUE); // EOM |
| 1305 | |
| 1306 | DBUG_ASSERT(view->table != 0 && view->field_translation != 0); |
| 1307 | |
| 1308 | (void) my_bitmap_init(&used_fields, used_fields_buff, table->s->fields, 0); |
| 1309 | bitmap_clear_all(&used_fields); |
| 1310 | |
| 1311 | view->contain_auto_increment= 0; |
| 1312 | /* |
| 1313 | we must not set query_id for fields as they're not |
| 1314 | really used in this context |
| 1315 | */ |
| 1316 | thd->column_usage= COLUMNS_WRITE; |
| 1317 | /* check simplicity and prepare unique test of view */ |
| 1318 | for (trans= trans_start; trans != trans_end; trans++) |
| 1319 | { |
| 1320 | if (!trans->item->fixed && trans->item->fix_fields(thd, &trans->item)) |
| 1321 | { |
| 1322 | thd->column_usage= saved_column_usage; |
| 1323 | DBUG_RETURN(TRUE); |
| 1324 | } |
| 1325 | Item_field *field; |
| 1326 | /* simple SELECT list entry (field without expression) */ |
| 1327 | if (!(field= trans->item->field_for_view_update())) |
| 1328 | { |
| 1329 | thd->column_usage= saved_column_usage; |
| 1330 | DBUG_RETURN(TRUE); |
| 1331 | } |
| 1332 | if (field->field->unireg_check == Field::NEXT_NUMBER) |
| 1333 | view->contain_auto_increment= 1; |
| 1334 | /* prepare unique test */ |
| 1335 | /* |
| 1336 | remove collation (or other transparent for update function) if we have |
| 1337 | it |
| 1338 | */ |
| 1339 | trans->item= field; |
| 1340 | } |
| 1341 | thd->column_usage= saved_column_usage; |
| 1342 | /* unique test */ |
| 1343 | for (trans= trans_start; trans != trans_end; trans++) |
| 1344 | { |
| 1345 | /* Thanks to test above, we know that all columns are of type Item_field */ |
| 1346 | Item_field *field= (Item_field *)trans->item; |
| 1347 | /* check fields belong to table in which we are inserting */ |
| 1348 | if (field->field->table == table && |
| 1349 | bitmap_fast_test_and_set(&used_fields, field->field->field_index)) |
| 1350 | DBUG_RETURN(TRUE); |
| 1351 | } |
| 1352 | |
| 1353 | DBUG_RETURN(FALSE); |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | /* |
| 1358 | Check if table can be updated |
| 1359 | |
| 1360 | SYNOPSIS |
| 1361 | mysql_prepare_insert_check_table() |
| 1362 | thd Thread handle |
| 1363 | table_list Table list |
| 1364 | fields List of fields to be updated |
| 1365 | where Pointer to where clause |
| 1366 | select_insert Check is making for SELECT ... INSERT |
| 1367 | |
| 1368 | RETURN |
| 1369 | FALSE ok |
| 1370 | TRUE ERROR |
| 1371 | */ |
| 1372 | |
| 1373 | static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list, |
| 1374 | List<Item> &fields, |
| 1375 | bool select_insert) |
| 1376 | { |
| 1377 | bool insert_into_view= (table_list->view != 0); |
| 1378 | DBUG_ENTER("mysql_prepare_insert_check_table" ); |
| 1379 | |
| 1380 | if (!table_list->single_table_updatable()) |
| 1381 | { |
| 1382 | my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT" ); |
| 1383 | DBUG_RETURN(TRUE); |
| 1384 | } |
| 1385 | /* |
| 1386 | first table in list is the one we'll INSERT into, requires INSERT_ACL. |
| 1387 | all others require SELECT_ACL only. the ACL requirement below is for |
| 1388 | new leaves only anyway (view-constituents), so check for SELECT rather |
| 1389 | than INSERT. |
| 1390 | */ |
| 1391 | |
| 1392 | if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context, |
| 1393 | &thd->lex->select_lex.top_join_list, |
| 1394 | table_list, |
| 1395 | thd->lex->select_lex.leaf_tables, |
| 1396 | select_insert, INSERT_ACL, SELECT_ACL, |
| 1397 | TRUE)) |
| 1398 | DBUG_RETURN(TRUE); |
| 1399 | |
| 1400 | if (insert_into_view && !fields.elements) |
| 1401 | { |
| 1402 | thd->lex->empty_field_list_on_rset= 1; |
| 1403 | if (!thd->lex->select_lex.leaf_tables.head()->table || |
| 1404 | table_list->is_multitable()) |
| 1405 | { |
| 1406 | my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0), |
| 1407 | table_list->view_db.str, table_list->view_name.str); |
| 1408 | DBUG_RETURN(TRUE); |
| 1409 | } |
| 1410 | DBUG_RETURN(insert_view_fields(thd, &fields, table_list)); |
| 1411 | } |
| 1412 | |
| 1413 | DBUG_RETURN(FALSE); |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | /* |
| 1418 | Get extra info for tables we insert into |
| 1419 | |
| 1420 | @param table table(TABLE object) we insert into, |
| 1421 | might be NULL in case of view |
| 1422 | @param table(TABLE_LIST object) or view we insert into |
| 1423 | */ |
| 1424 | |
| 1425 | static void prepare_for_positional_update(TABLE *table, TABLE_LIST *tables) |
| 1426 | { |
| 1427 | if (table) |
| 1428 | { |
| 1429 | if(table->reginfo.lock_type != TL_WRITE_DELAYED) |
| 1430 | table->prepare_for_position(); |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | DBUG_ASSERT(tables->view); |
| 1435 | List_iterator<TABLE_LIST> it(*tables->view_tables); |
| 1436 | TABLE_LIST *tbl; |
| 1437 | while ((tbl= it++)) |
| 1438 | prepare_for_positional_update(tbl->table, tbl); |
| 1439 | |
| 1440 | return; |
| 1441 | } |
| 1442 | |
| 1443 | |
| 1444 | /* |
| 1445 | Prepare items in INSERT statement |
| 1446 | |
| 1447 | SYNOPSIS |
| 1448 | mysql_prepare_insert() |
| 1449 | thd Thread handler |
| 1450 | table_list Global/local table list |
| 1451 | table Table to insert into (can be NULL if table should |
| 1452 | be taken from table_list->table) |
| 1453 | where Where clause (for insert ... select) |
| 1454 | select_insert TRUE if INSERT ... SELECT statement |
| 1455 | |
| 1456 | TODO (in far future) |
| 1457 | In cases of: |
| 1458 | INSERT INTO t1 SELECT a, sum(a) as sum1 from t2 GROUP BY a |
| 1459 | ON DUPLICATE KEY ... |
| 1460 | we should be able to refer to sum1 in the ON DUPLICATE KEY part |
| 1461 | |
| 1462 | WARNING |
| 1463 | You MUST set table->insert_values to 0 after calling this function |
| 1464 | before releasing the table object. |
| 1465 | |
| 1466 | RETURN VALUE |
| 1467 | FALSE OK |
| 1468 | TRUE error |
| 1469 | */ |
| 1470 | |
| 1471 | bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, |
| 1472 | TABLE *table, List<Item> &fields, List_item *values, |
| 1473 | List<Item> &update_fields, List<Item> &update_values, |
| 1474 | enum_duplicates duplic, COND **where, |
| 1475 | bool select_insert) |
| 1476 | { |
| 1477 | SELECT_LEX *select_lex= &thd->lex->select_lex; |
| 1478 | Name_resolution_context *context= &select_lex->context; |
| 1479 | Name_resolution_context_state ctx_state; |
| 1480 | bool insert_into_view= (table_list->view != 0); |
| 1481 | bool res= 0; |
| 1482 | table_map map= 0; |
| 1483 | DBUG_ENTER("mysql_prepare_insert" ); |
| 1484 | DBUG_PRINT("enter" , ("table_list: %p table: %p view: %d" , |
| 1485 | table_list, table, |
| 1486 | (int)insert_into_view)); |
| 1487 | /* INSERT should have a SELECT or VALUES clause */ |
| 1488 | DBUG_ASSERT (!select_insert || !values); |
| 1489 | |
| 1490 | if (mysql_handle_derived(thd->lex, DT_INIT)) |
| 1491 | DBUG_RETURN(TRUE); |
| 1492 | if (table_list->handle_derived(thd->lex, DT_MERGE_FOR_INSERT)) |
| 1493 | DBUG_RETURN(TRUE); |
| 1494 | if (mysql_handle_list_of_derived(thd->lex, table_list, DT_PREPARE)) |
| 1495 | DBUG_RETURN(TRUE); |
| 1496 | /* |
| 1497 | For subqueries in VALUES() we should not see the table in which we are |
| 1498 | inserting (for INSERT ... SELECT this is done by changing table_list, |
| 1499 | because INSERT ... SELECT share SELECT_LEX it with SELECT. |
| 1500 | */ |
| 1501 | if (!select_insert) |
| 1502 | { |
| 1503 | for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); |
| 1504 | un; |
| 1505 | un= un->next_unit()) |
| 1506 | { |
| 1507 | for (SELECT_LEX *sl= un->first_select(); |
| 1508 | sl; |
| 1509 | sl= sl->next_select()) |
| 1510 | { |
| 1511 | sl->context.outer_context= 0; |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | if (duplic == DUP_UPDATE) |
| 1517 | { |
| 1518 | /* it should be allocated before Item::fix_fields() */ |
| 1519 | if (table_list->set_insert_values(thd->mem_root)) |
| 1520 | DBUG_RETURN(TRUE); |
| 1521 | } |
| 1522 | |
| 1523 | if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert)) |
| 1524 | DBUG_RETURN(TRUE); |
| 1525 | |
| 1526 | /* Prepare the fields in the statement. */ |
| 1527 | if (values) |
| 1528 | { |
| 1529 | /* if we have INSERT ... VALUES () we cannot have a GROUP BY clause */ |
| 1530 | DBUG_ASSERT (!select_lex->group_list.elements); |
| 1531 | |
| 1532 | /* Save the state of the current name resolution context. */ |
| 1533 | ctx_state.save_state(context, table_list); |
| 1534 | |
| 1535 | /* |
| 1536 | Perform name resolution only in the first table - 'table_list', |
| 1537 | which is the table that is inserted into. |
| 1538 | */ |
| 1539 | table_list->next_local= 0; |
| 1540 | context->resolve_in_table_list_only(table_list); |
| 1541 | |
| 1542 | res= (setup_fields(thd, Ref_ptr_array(), |
| 1543 | *values, MARK_COLUMNS_READ, 0, NULL, 0) || |
| 1544 | check_insert_fields(thd, context->table_list, fields, *values, |
| 1545 | !insert_into_view, 0, &map)); |
| 1546 | |
| 1547 | if (!res) |
| 1548 | res= setup_fields(thd, Ref_ptr_array(), |
| 1549 | update_values, MARK_COLUMNS_READ, 0, NULL, 0); |
| 1550 | |
| 1551 | if (!res && duplic == DUP_UPDATE) |
| 1552 | { |
| 1553 | select_lex->no_wrap_view_item= TRUE; |
| 1554 | res= check_update_fields(thd, context->table_list, update_fields, |
| 1555 | update_values, false, &map); |
| 1556 | select_lex->no_wrap_view_item= FALSE; |
| 1557 | } |
| 1558 | |
| 1559 | /* Restore the current context. */ |
| 1560 | ctx_state.restore_state(context, table_list); |
| 1561 | } |
| 1562 | |
| 1563 | if (res) |
| 1564 | DBUG_RETURN(res); |
| 1565 | |
| 1566 | if (!table) |
| 1567 | table= table_list->table; |
| 1568 | |
| 1569 | if (table->versioned(VERS_TIMESTAMP) && duplic == DUP_REPLACE) |
| 1570 | { |
| 1571 | // Additional memory may be required to create historical items. |
| 1572 | if (table_list->set_insert_values(thd->mem_root)) |
| 1573 | DBUG_RETURN(TRUE); |
| 1574 | } |
| 1575 | |
| 1576 | if (!select_insert) |
| 1577 | { |
| 1578 | Item *fake_conds= 0; |
| 1579 | TABLE_LIST *duplicate; |
| 1580 | if ((duplicate= unique_table(thd, table_list, table_list->next_global, |
| 1581 | CHECK_DUP_ALLOW_DIFFERENT_ALIAS))) |
| 1582 | { |
| 1583 | update_non_unique_table_error(table_list, "INSERT" , duplicate); |
| 1584 | DBUG_RETURN(TRUE); |
| 1585 | } |
| 1586 | select_lex->fix_prepare_information(thd, &fake_conds, &fake_conds); |
| 1587 | select_lex->first_execution= 0; |
| 1588 | } |
| 1589 | /* |
| 1590 | Only call prepare_for_posistion() if we are not performing a DELAYED |
| 1591 | operation. It will instead be executed by delayed insert thread. |
| 1592 | */ |
| 1593 | if (duplic == DUP_UPDATE || duplic == DUP_REPLACE) |
| 1594 | prepare_for_positional_update(table, table_list); |
| 1595 | DBUG_RETURN(FALSE); |
| 1596 | } |
| 1597 | |
| 1598 | |
| 1599 | /* Check if there is more uniq keys after field */ |
| 1600 | |
| 1601 | static int last_uniq_key(TABLE *table,uint keynr) |
| 1602 | { |
| 1603 | /* |
| 1604 | When an underlying storage engine informs that the unique key |
| 1605 | conflicts are not reported in the ascending order by setting |
| 1606 | the HA_DUPLICATE_KEY_NOT_IN_ORDER flag, we cannot rely on this |
| 1607 | information to determine the last key conflict. |
| 1608 | |
| 1609 | The information about the last key conflict will be used to |
| 1610 | do a replace of the new row on the conflicting row, rather |
| 1611 | than doing a delete (of old row) + insert (of new row). |
| 1612 | |
| 1613 | Hence check for this flag and disable replacing the last row |
| 1614 | by returning 0 always. Returning 0 will result in doing |
| 1615 | a delete + insert always. |
| 1616 | */ |
| 1617 | if (table->file->ha_table_flags() & HA_DUPLICATE_KEY_NOT_IN_ORDER) |
| 1618 | return 0; |
| 1619 | |
| 1620 | while (++keynr < table->s->keys) |
| 1621 | if (table->key_info[keynr].flags & HA_NOSAME) |
| 1622 | return 0; |
| 1623 | return 1; |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | /* |
| 1628 | Inserts one historical row to a table. |
| 1629 | |
| 1630 | Copies content of the row from table->record[1] to table->record[0], |
| 1631 | sets Sys_end to now() and calls ha_write_row() . |
| 1632 | */ |
| 1633 | |
| 1634 | int vers_insert_history_row(TABLE *table) |
| 1635 | { |
| 1636 | DBUG_ASSERT(table->versioned(VERS_TIMESTAMP)); |
| 1637 | restore_record(table,record[1]); |
| 1638 | |
| 1639 | // Set Sys_end to now() |
| 1640 | table->vers_update_end(); |
| 1641 | |
| 1642 | Field *row_start= table->vers_start_field(); |
| 1643 | Field *row_end= table->vers_end_field(); |
| 1644 | if (row_start->cmp(row_start->ptr, row_end->ptr) >= 0) |
| 1645 | return 0; |
| 1646 | |
| 1647 | return table->file->ha_write_row(table->record[0]); |
| 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | Write a record to table with optional deleting of conflicting records, |
| 1652 | invoke proper triggers if needed. |
| 1653 | |
| 1654 | SYNOPSIS |
| 1655 | write_record() |
| 1656 | thd - thread context |
| 1657 | table - table to which record should be written |
| 1658 | info - COPY_INFO structure describing handling of duplicates |
| 1659 | and which is used for counting number of records inserted |
| 1660 | and deleted. |
| 1661 | |
| 1662 | NOTE |
| 1663 | Once this record will be written to table after insert trigger will |
| 1664 | be invoked. If instead of inserting new record we will update old one |
| 1665 | then both on update triggers will work instead. Similarly both on |
| 1666 | delete triggers will be invoked if we will delete conflicting records. |
| 1667 | |
| 1668 | Sets thd->transaction.stmt.modified_non_trans_table to TRUE if table which is updated didn't have |
| 1669 | transactions. |
| 1670 | |
| 1671 | RETURN VALUE |
| 1672 | 0 - success |
| 1673 | non-0 - error |
| 1674 | */ |
| 1675 | |
| 1676 | |
| 1677 | int write_record(THD *thd, TABLE *table,COPY_INFO *info) |
| 1678 | { |
| 1679 | int error, trg_error= 0; |
| 1680 | char *key=0; |
| 1681 | MY_BITMAP *save_read_set, *save_write_set; |
| 1682 | ulonglong prev_insert_id= table->file->next_insert_id; |
| 1683 | ulonglong insert_id_for_cur_row= 0; |
| 1684 | ulonglong prev_insert_id_for_cur_row= 0; |
| 1685 | DBUG_ENTER("write_record" ); |
| 1686 | |
| 1687 | info->records++; |
| 1688 | save_read_set= table->read_set; |
| 1689 | save_write_set= table->write_set; |
| 1690 | |
| 1691 | if (info->handle_duplicates == DUP_REPLACE || |
| 1692 | info->handle_duplicates == DUP_UPDATE) |
| 1693 | { |
| 1694 | while (unlikely(error=table->file->ha_write_row(table->record[0]))) |
| 1695 | { |
| 1696 | uint key_nr; |
| 1697 | /* |
| 1698 | If we do more than one iteration of this loop, from the second one the |
| 1699 | row will have an explicit value in the autoinc field, which was set at |
| 1700 | the first call of handler::update_auto_increment(). So we must save |
| 1701 | the autogenerated value to avoid thd->insert_id_for_cur_row to become |
| 1702 | 0. |
| 1703 | */ |
| 1704 | if (table->file->insert_id_for_cur_row > 0) |
| 1705 | insert_id_for_cur_row= table->file->insert_id_for_cur_row; |
| 1706 | else |
| 1707 | table->file->insert_id_for_cur_row= insert_id_for_cur_row; |
| 1708 | bool is_duplicate_key_error; |
| 1709 | if (table->file->is_fatal_error(error, HA_CHECK_ALL)) |
| 1710 | goto err; |
| 1711 | is_duplicate_key_error= |
| 1712 | table->file->is_fatal_error(error, HA_CHECK_ALL & ~HA_CHECK_DUP); |
| 1713 | if (!is_duplicate_key_error) |
| 1714 | { |
| 1715 | /* |
| 1716 | We come here when we had an ignorable error which is not a duplicate |
| 1717 | key error. In this we ignore error if ignore flag is set, otherwise |
| 1718 | report error as usual. We will not do any duplicate key processing. |
| 1719 | */ |
| 1720 | if (info->ignore) |
| 1721 | { |
| 1722 | table->file->print_error(error, MYF(ME_JUST_WARNING)); |
| 1723 | goto ok_or_after_trg_err; /* Ignoring a not fatal error, return 0 */ |
| 1724 | } |
| 1725 | goto err; |
| 1726 | } |
| 1727 | if (unlikely((int) (key_nr = table->file->get_dup_key(error)) < 0)) |
| 1728 | { |
| 1729 | error= HA_ERR_FOUND_DUPP_KEY; /* Database can't find key */ |
| 1730 | goto err; |
| 1731 | } |
| 1732 | DEBUG_SYNC(thd, "write_row_replace" ); |
| 1733 | |
| 1734 | /* Read all columns for the row we are going to replace */ |
| 1735 | table->use_all_columns(); |
| 1736 | /* |
| 1737 | Don't allow REPLACE to replace a row when a auto_increment column |
| 1738 | was used. This ensures that we don't get a problem when the |
| 1739 | whole range of the key has been used. |
| 1740 | */ |
| 1741 | if (info->handle_duplicates == DUP_REPLACE && |
| 1742 | table->next_number_field && |
| 1743 | key_nr == table->s->next_number_index && |
| 1744 | (insert_id_for_cur_row > 0)) |
| 1745 | goto err; |
| 1746 | if (table->file->ha_table_flags() & HA_DUPLICATE_POS) |
| 1747 | { |
| 1748 | if (table->file->ha_rnd_pos(table->record[1],table->file->dup_ref)) |
| 1749 | goto err; |
| 1750 | } |
| 1751 | else |
| 1752 | { |
| 1753 | if (table->file->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */ |
| 1754 | { |
| 1755 | error=my_errno; |
| 1756 | goto err; |
| 1757 | } |
| 1758 | |
| 1759 | if (!key) |
| 1760 | { |
| 1761 | if (!(key=(char*) my_safe_alloca(table->s->max_unique_length))) |
| 1762 | { |
| 1763 | error=ENOMEM; |
| 1764 | goto err; |
| 1765 | } |
| 1766 | } |
| 1767 | key_copy((uchar*) key,table->record[0],table->key_info+key_nr,0); |
| 1768 | key_part_map keypart_map= (1 << table->key_info[key_nr].user_defined_key_parts) - 1; |
| 1769 | if ((error= (table->file->ha_index_read_idx_map(table->record[1], |
| 1770 | key_nr, (uchar*) key, |
| 1771 | keypart_map, |
| 1772 | HA_READ_KEY_EXACT)))) |
| 1773 | goto err; |
| 1774 | } |
| 1775 | if (table->vfield) |
| 1776 | { |
| 1777 | /* |
| 1778 | We have not yet called update_virtual_fields(VOL_UPDATE_FOR_READ) |
| 1779 | in handler methods for the just read row in record[1]. |
| 1780 | */ |
| 1781 | table->move_fields(table->field, table->record[1], table->record[0]); |
| 1782 | table->update_virtual_fields(table->file, VCOL_UPDATE_FOR_REPLACE); |
| 1783 | table->move_fields(table->field, table->record[0], table->record[1]); |
| 1784 | } |
| 1785 | if (info->handle_duplicates == DUP_UPDATE) |
| 1786 | { |
| 1787 | int res= 0; |
| 1788 | /* |
| 1789 | We don't check for other UNIQUE keys - the first row |
| 1790 | that matches, is updated. If update causes a conflict again, |
| 1791 | an error is returned |
| 1792 | */ |
| 1793 | DBUG_ASSERT(table->insert_values != NULL); |
| 1794 | store_record(table,insert_values); |
| 1795 | restore_record(table,record[1]); |
| 1796 | table->reset_default_fields(); |
| 1797 | |
| 1798 | /* |
| 1799 | in INSERT ... ON DUPLICATE KEY UPDATE the set of modified fields can |
| 1800 | change per row. Thus, we have to do reset_default_fields() per row. |
| 1801 | Twice (before insert and before update). |
| 1802 | */ |
| 1803 | DBUG_ASSERT(info->update_fields->elements == |
| 1804 | info->update_values->elements); |
| 1805 | if (fill_record_n_invoke_before_triggers(thd, table, |
| 1806 | *info->update_fields, |
| 1807 | *info->update_values, |
| 1808 | info->ignore, |
| 1809 | TRG_EVENT_UPDATE)) |
| 1810 | goto before_trg_err; |
| 1811 | |
| 1812 | bool different_records= (!records_are_comparable(table) || |
| 1813 | compare_record(table)); |
| 1814 | /* |
| 1815 | Default fields must be updated before checking view updateability. |
| 1816 | This branch of INSERT is executed only when a UNIQUE key was violated |
| 1817 | with the ON DUPLICATE KEY UPDATE option. In this case the INSERT |
| 1818 | operation is transformed to an UPDATE, and the default fields must |
| 1819 | be updated as if this is an UPDATE. |
| 1820 | */ |
| 1821 | if (different_records && table->default_field) |
| 1822 | { |
| 1823 | if (table->update_default_fields(1, info->ignore)) |
| 1824 | goto err; |
| 1825 | } |
| 1826 | |
| 1827 | /* CHECK OPTION for VIEW ... ON DUPLICATE KEY UPDATE ... */ |
| 1828 | res= info->table_list->view_check_option(table->in_use, info->ignore); |
| 1829 | if (res == VIEW_CHECK_SKIP) |
| 1830 | goto ok_or_after_trg_err; |
| 1831 | if (res == VIEW_CHECK_ERROR) |
| 1832 | goto before_trg_err; |
| 1833 | |
| 1834 | table->file->restore_auto_increment(prev_insert_id); |
| 1835 | info->touched++; |
| 1836 | if (different_records) |
| 1837 | { |
| 1838 | if (unlikely(error=table->file->ha_update_row(table->record[1], |
| 1839 | table->record[0])) && |
| 1840 | error != HA_ERR_RECORD_IS_THE_SAME) |
| 1841 | { |
| 1842 | if (info->ignore && |
| 1843 | !table->file->is_fatal_error(error, HA_CHECK_ALL)) |
| 1844 | { |
| 1845 | if (!(thd->variables.old_behavior & |
| 1846 | OLD_MODE_NO_DUP_KEY_WARNINGS_WITH_IGNORE)) |
| 1847 | table->file->print_error(error, MYF(ME_JUST_WARNING)); |
| 1848 | goto ok_or_after_trg_err; |
| 1849 | } |
| 1850 | goto err; |
| 1851 | } |
| 1852 | |
| 1853 | if (error != HA_ERR_RECORD_IS_THE_SAME) |
| 1854 | { |
| 1855 | info->updated++; |
| 1856 | if (table->versioned()) |
| 1857 | { |
| 1858 | if (table->versioned(VERS_TIMESTAMP)) |
| 1859 | { |
| 1860 | store_record(table, record[2]); |
| 1861 | if ((error= vers_insert_history_row(table))) |
| 1862 | { |
| 1863 | info->last_errno= error; |
| 1864 | table->file->print_error(error, MYF(0)); |
| 1865 | trg_error= 1; |
| 1866 | restore_record(table, record[2]); |
| 1867 | goto ok_or_after_trg_err; |
| 1868 | } |
| 1869 | restore_record(table, record[2]); |
| 1870 | } |
| 1871 | info->copied++; |
| 1872 | } |
| 1873 | } |
| 1874 | else |
| 1875 | error= 0; |
| 1876 | /* |
| 1877 | If ON DUP KEY UPDATE updates a row instead of inserting |
| 1878 | one, it's like a regular UPDATE statement: it should not |
| 1879 | affect the value of a next SELECT LAST_INSERT_ID() or |
| 1880 | mysql_insert_id(). Except if LAST_INSERT_ID(#) was in the |
| 1881 | INSERT query, which is handled separately by |
| 1882 | THD::arg_of_last_insert_id_function. |
| 1883 | */ |
| 1884 | prev_insert_id_for_cur_row= table->file->insert_id_for_cur_row; |
| 1885 | insert_id_for_cur_row= table->file->insert_id_for_cur_row= 0; |
| 1886 | trg_error= (table->triggers && |
| 1887 | table->triggers->process_triggers(thd, TRG_EVENT_UPDATE, |
| 1888 | TRG_ACTION_AFTER, TRUE)); |
| 1889 | info->copied++; |
| 1890 | } |
| 1891 | |
| 1892 | /* |
| 1893 | Only update next_insert_id if the AUTO_INCREMENT value was explicitly |
| 1894 | updated, so we don't update next_insert_id with the value from the |
| 1895 | row being updated. Otherwise reset next_insert_id to what it was |
| 1896 | before the duplicate key error, since that value is unused. |
| 1897 | */ |
| 1898 | if (table->next_number_field_updated) |
| 1899 | { |
| 1900 | DBUG_ASSERT(table->next_number_field != NULL); |
| 1901 | |
| 1902 | table->file->adjust_next_insert_id_after_explicit_value(table->next_number_field->val_int()); |
| 1903 | } |
| 1904 | else if (prev_insert_id_for_cur_row) |
| 1905 | { |
| 1906 | table->file->restore_auto_increment(prev_insert_id_for_cur_row); |
| 1907 | } |
| 1908 | goto ok_or_after_trg_err; |
| 1909 | } |
| 1910 | else /* DUP_REPLACE */ |
| 1911 | { |
| 1912 | /* |
| 1913 | The manual defines the REPLACE semantics that it is either |
| 1914 | an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in |
| 1915 | InnoDB do not function in the defined way if we allow MySQL |
| 1916 | to convert the latter operation internally to an UPDATE. |
| 1917 | We also should not perform this conversion if we have |
| 1918 | timestamp field with ON UPDATE which is different from DEFAULT. |
| 1919 | Another case when conversion should not be performed is when |
| 1920 | we have ON DELETE trigger on table so user may notice that |
| 1921 | we cheat here. Note that it is ok to do such conversion for |
| 1922 | tables which have ON UPDATE but have no ON DELETE triggers, |
| 1923 | we just should not expose this fact to users by invoking |
| 1924 | ON UPDATE triggers. |
| 1925 | For system versioning wa also use path through delete since we would |
| 1926 | save nothing through this cheating. |
| 1927 | */ |
| 1928 | if (last_uniq_key(table,key_nr) && |
| 1929 | !table->file->referenced_by_foreign_key() && |
| 1930 | (!table->triggers || !table->triggers->has_delete_triggers())) |
| 1931 | { |
| 1932 | if (table->versioned(VERS_TRX_ID)) |
| 1933 | { |
| 1934 | bitmap_set_bit(table->write_set, table->vers_start_field()->field_index); |
| 1935 | table->vers_start_field()->store(0, false); |
| 1936 | } |
| 1937 | if (unlikely(error= table->file->ha_update_row(table->record[1], |
| 1938 | table->record[0])) && |
| 1939 | error != HA_ERR_RECORD_IS_THE_SAME) |
| 1940 | goto err; |
| 1941 | if (likely(!error)) |
| 1942 | { |
| 1943 | info->deleted++; |
| 1944 | if (table->versioned(VERS_TIMESTAMP)) |
| 1945 | { |
| 1946 | store_record(table, record[2]); |
| 1947 | error= vers_insert_history_row(table); |
| 1948 | restore_record(table, record[2]); |
| 1949 | if (unlikely(error)) |
| 1950 | goto err; |
| 1951 | } |
| 1952 | } |
| 1953 | else |
| 1954 | error= 0; // error was HA_ERR_RECORD_IS_THE_SAME |
| 1955 | thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row); |
| 1956 | /* |
| 1957 | Since we pretend that we have done insert we should call |
| 1958 | its after triggers. |
| 1959 | */ |
| 1960 | goto after_trg_n_copied_inc; |
| 1961 | } |
| 1962 | else |
| 1963 | { |
| 1964 | if (table->triggers && |
| 1965 | table->triggers->process_triggers(thd, TRG_EVENT_DELETE, |
| 1966 | TRG_ACTION_BEFORE, TRUE)) |
| 1967 | goto before_trg_err; |
| 1968 | |
| 1969 | if (!table->versioned(VERS_TIMESTAMP)) |
| 1970 | error= table->file->ha_delete_row(table->record[1]); |
| 1971 | else |
| 1972 | { |
| 1973 | DBUG_ASSERT(table->insert_values); |
| 1974 | store_record(table,insert_values); |
| 1975 | restore_record(table,record[1]); |
| 1976 | table->vers_update_end(); |
| 1977 | error= table->file->ha_update_row(table->record[1], |
| 1978 | table->record[0]); |
| 1979 | restore_record(table,insert_values); |
| 1980 | } |
| 1981 | if (unlikely(error)) |
| 1982 | goto err; |
| 1983 | if (!table->versioned(VERS_TIMESTAMP)) |
| 1984 | info->deleted++; |
| 1985 | else |
| 1986 | info->updated++; |
| 1987 | if (!table->file->has_transactions()) |
| 1988 | thd->transaction.stmt.modified_non_trans_table= TRUE; |
| 1989 | if (table->triggers && |
| 1990 | table->triggers->process_triggers(thd, TRG_EVENT_DELETE, |
| 1991 | TRG_ACTION_AFTER, TRUE)) |
| 1992 | { |
| 1993 | trg_error= 1; |
| 1994 | goto ok_or_after_trg_err; |
| 1995 | } |
| 1996 | /* Let us attempt do write_row() once more */ |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | If more than one iteration of the above while loop is done, from |
| 2003 | the second one the row being inserted will have an explicit |
| 2004 | value in the autoinc field, which was set at the first call of |
| 2005 | handler::update_auto_increment(). This value is saved to avoid |
| 2006 | thd->insert_id_for_cur_row becoming 0. Use this saved autoinc |
| 2007 | value. |
| 2008 | */ |
| 2009 | if (table->file->insert_id_for_cur_row == 0) |
| 2010 | table->file->insert_id_for_cur_row= insert_id_for_cur_row; |
| 2011 | |
| 2012 | thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row); |
| 2013 | /* |
| 2014 | Restore column maps if they where replaced during an duplicate key |
| 2015 | problem. |
| 2016 | */ |
| 2017 | if (table->read_set != save_read_set || |
| 2018 | table->write_set != save_write_set) |
| 2019 | table->column_bitmaps_set(save_read_set, save_write_set); |
| 2020 | } |
| 2021 | else if (unlikely((error=table->file->ha_write_row(table->record[0])))) |
| 2022 | { |
| 2023 | DEBUG_SYNC(thd, "write_row_noreplace" ); |
| 2024 | if (!info->ignore || |
| 2025 | table->file->is_fatal_error(error, HA_CHECK_ALL)) |
| 2026 | goto err; |
| 2027 | if (!(thd->variables.old_behavior & |
| 2028 | OLD_MODE_NO_DUP_KEY_WARNINGS_WITH_IGNORE)) |
| 2029 | table->file->print_error(error, MYF(ME_JUST_WARNING)); |
| 2030 | table->file->restore_auto_increment(prev_insert_id); |
| 2031 | goto ok_or_after_trg_err; |
| 2032 | } |
| 2033 | |
| 2034 | after_trg_n_copied_inc: |
| 2035 | info->copied++; |
| 2036 | thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row); |
| 2037 | trg_error= (table->triggers && |
| 2038 | table->triggers->process_triggers(thd, TRG_EVENT_INSERT, |
| 2039 | TRG_ACTION_AFTER, TRUE)); |
| 2040 | |
| 2041 | ok_or_after_trg_err: |
| 2042 | if (key) |
| 2043 | my_safe_afree(key,table->s->max_unique_length); |
| 2044 | if (!table->file->has_transactions()) |
| 2045 | thd->transaction.stmt.modified_non_trans_table= TRUE; |
| 2046 | DBUG_RETURN(trg_error); |
| 2047 | |
| 2048 | err: |
| 2049 | info->last_errno= error; |
| 2050 | table->file->print_error(error,MYF(0)); |
| 2051 | |
| 2052 | before_trg_err: |
| 2053 | table->file->restore_auto_increment(prev_insert_id); |
| 2054 | if (key) |
| 2055 | my_safe_afree(key, table->s->max_unique_length); |
| 2056 | table->column_bitmaps_set(save_read_set, save_write_set); |
| 2057 | DBUG_RETURN(1); |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | /****************************************************************************** |
| 2062 | Check that all fields with arn't null_fields are used |
| 2063 | ******************************************************************************/ |
| 2064 | |
| 2065 | |
| 2066 | int check_that_all_fields_are_given_values(THD *thd, TABLE *entry, TABLE_LIST *table_list) |
| 2067 | { |
| 2068 | int err= 0; |
| 2069 | MY_BITMAP *write_set= entry->write_set; |
| 2070 | |
| 2071 | for (Field **field=entry->field ; *field ; field++) |
| 2072 | { |
| 2073 | if (!bitmap_is_set(write_set, (*field)->field_index) && |
| 2074 | !(*field)->vers_sys_field() && |
| 2075 | has_no_default_value(thd, *field, table_list) && |
| 2076 | ((*field)->real_type() != MYSQL_TYPE_ENUM)) |
| 2077 | err=1; |
| 2078 | } |
| 2079 | return thd->abort_on_warning ? err : 0; |
| 2080 | } |
| 2081 | |
| 2082 | /***************************************************************************** |
| 2083 | Handling of delayed inserts |
| 2084 | A thread is created for each table that one uses with the DELAYED attribute. |
| 2085 | *****************************************************************************/ |
| 2086 | |
| 2087 | #ifndef EMBEDDED_LIBRARY |
| 2088 | |
| 2089 | class delayed_row :public ilink { |
| 2090 | public: |
| 2091 | char *record; |
| 2092 | enum_duplicates dup; |
| 2093 | my_time_t start_time; |
| 2094 | ulong start_time_sec_part; |
| 2095 | sql_mode_t sql_mode; |
| 2096 | bool auto_increment_field_not_null; |
| 2097 | bool ignore, log_query, query_start_sec_part_used; |
| 2098 | bool stmt_depends_on_first_successful_insert_id_in_prev_stmt; |
| 2099 | ulonglong first_successful_insert_id_in_prev_stmt; |
| 2100 | ulonglong forced_insert_id; |
| 2101 | ulong auto_increment_increment; |
| 2102 | ulong auto_increment_offset; |
| 2103 | LEX_STRING query; |
| 2104 | Time_zone *time_zone; |
| 2105 | |
| 2106 | delayed_row(LEX_STRING const query_arg, enum_duplicates dup_arg, |
| 2107 | bool ignore_arg, bool log_query_arg) |
| 2108 | : record(0), dup(dup_arg), ignore(ignore_arg), log_query(log_query_arg), |
| 2109 | forced_insert_id(0), query(query_arg), time_zone(0) |
| 2110 | {} |
| 2111 | ~delayed_row() |
| 2112 | { |
| 2113 | my_free(query.str); |
| 2114 | my_free(record); |
| 2115 | } |
| 2116 | }; |
| 2117 | |
| 2118 | /** |
| 2119 | Delayed_insert - context of a thread responsible for delayed insert |
| 2120 | into one table. When processing delayed inserts, we create an own |
| 2121 | thread for every distinct table. Later on all delayed inserts directed |
| 2122 | into that table are handled by a dedicated thread. |
| 2123 | */ |
| 2124 | |
| 2125 | class Delayed_insert :public ilink { |
| 2126 | uint locks_in_memory; |
| 2127 | thr_lock_type delayed_lock; |
| 2128 | public: |
| 2129 | THD thd; |
| 2130 | TABLE *table; |
| 2131 | mysql_mutex_t mutex; |
| 2132 | mysql_cond_t cond, cond_client; |
| 2133 | uint tables_in_use, stacked_inserts; |
| 2134 | volatile bool status; |
| 2135 | bool retry; |
| 2136 | /** |
| 2137 | When the handler thread starts, it clones a metadata lock ticket |
| 2138 | which protects against GRL and ticket for the table to be inserted. |
| 2139 | This is done to allow the deadlock detector to detect deadlocks |
| 2140 | resulting from these locks. |
| 2141 | Before this is done, the connection thread cannot safely exit |
| 2142 | without causing problems for clone_ticket(). |
| 2143 | Once handler_thread_initialized has been set, it is safe for the |
| 2144 | connection thread to exit. |
| 2145 | Access to handler_thread_initialized is protected by di->mutex. |
| 2146 | */ |
| 2147 | bool handler_thread_initialized; |
| 2148 | COPY_INFO info; |
| 2149 | I_List<delayed_row> rows; |
| 2150 | ulong group_count; |
| 2151 | TABLE_LIST table_list; // Argument |
| 2152 | /** |
| 2153 | Request for IX metadata lock protecting against GRL which is |
| 2154 | passed from connection thread to the handler thread. |
| 2155 | */ |
| 2156 | MDL_request grl_protection; |
| 2157 | |
| 2158 | Delayed_insert(SELECT_LEX *current_select) |
| 2159 | :locks_in_memory(0), thd(next_thread_id()), |
| 2160 | table(0),tables_in_use(0), stacked_inserts(0), |
| 2161 | status(0), retry(0), handler_thread_initialized(FALSE), group_count(0) |
| 2162 | { |
| 2163 | DBUG_ENTER("Delayed_insert constructor" ); |
| 2164 | thd.security_ctx->user=(char*) delayed_user; |
| 2165 | thd.security_ctx->host=(char*) my_localhost; |
| 2166 | strmake_buf(thd.security_ctx->priv_user, thd.security_ctx->user); |
| 2167 | thd.current_tablenr=0; |
| 2168 | thd.set_command(COM_DELAYED_INSERT); |
| 2169 | thd.lex->current_select= current_select; |
| 2170 | thd.lex->sql_command= SQLCOM_INSERT; // For innodb::store_lock() |
| 2171 | /* |
| 2172 | Prevent changes to global.lock_wait_timeout from affecting |
| 2173 | delayed insert threads as any timeouts in delayed inserts |
| 2174 | are not communicated to the client. |
| 2175 | */ |
| 2176 | thd.variables.lock_wait_timeout= LONG_TIMEOUT; |
| 2177 | |
| 2178 | bzero((char*) &thd.net, sizeof(thd.net)); // Safety |
| 2179 | bzero((char*) &table_list, sizeof(table_list)); // Safety |
| 2180 | thd.system_thread= SYSTEM_THREAD_DELAYED_INSERT; |
| 2181 | thd.security_ctx->host_or_ip= "" ; |
| 2182 | bzero((char*) &info,sizeof(info)); |
| 2183 | mysql_mutex_init(key_delayed_insert_mutex, &mutex, MY_MUTEX_INIT_FAST); |
| 2184 | mysql_cond_init(key_delayed_insert_cond, &cond, NULL); |
| 2185 | mysql_cond_init(key_delayed_insert_cond_client, &cond_client, NULL); |
| 2186 | mysql_mutex_lock(&LOCK_thread_count); |
| 2187 | delayed_insert_threads++; |
| 2188 | delayed_lock= global_system_variables.low_priority_updates ? |
| 2189 | TL_WRITE_LOW_PRIORITY : TL_WRITE; |
| 2190 | mysql_mutex_unlock(&LOCK_thread_count); |
| 2191 | DBUG_VOID_RETURN; |
| 2192 | } |
| 2193 | ~Delayed_insert() |
| 2194 | { |
| 2195 | /* The following is not really needed, but just for safety */ |
| 2196 | delayed_row *row; |
| 2197 | while ((row=rows.get())) |
| 2198 | delete row; |
| 2199 | if (table) |
| 2200 | { |
| 2201 | close_thread_tables(&thd); |
| 2202 | thd.mdl_context.release_transactional_locks(); |
| 2203 | } |
| 2204 | mysql_mutex_destroy(&mutex); |
| 2205 | mysql_cond_destroy(&cond); |
| 2206 | mysql_cond_destroy(&cond_client); |
| 2207 | |
| 2208 | /* |
| 2209 | We could use unlink_not_visible_threads() here, but as |
| 2210 | delayed_insert_threads also needs to be protected by |
| 2211 | the LOCK_thread_count mutex, we open code this. |
| 2212 | */ |
| 2213 | mysql_mutex_lock(&LOCK_thread_count); |
| 2214 | thd.unlink(); // Must be unlinked under lock |
| 2215 | delayed_insert_threads--; |
| 2216 | mysql_mutex_unlock(&LOCK_thread_count); |
| 2217 | |
| 2218 | my_free(thd.query()); |
| 2219 | thd.security_ctx->user= 0; |
| 2220 | thd.security_ctx->host= 0; |
| 2221 | } |
| 2222 | |
| 2223 | /* The following is for checking when we can delete ourselves */ |
| 2224 | inline void lock() |
| 2225 | { |
| 2226 | locks_in_memory++; // Assume LOCK_delay_insert |
| 2227 | } |
| 2228 | void unlock() |
| 2229 | { |
| 2230 | mysql_mutex_lock(&LOCK_delayed_insert); |
| 2231 | if (!--locks_in_memory) |
| 2232 | { |
| 2233 | mysql_mutex_lock(&mutex); |
| 2234 | if (thd.killed && ! stacked_inserts && ! tables_in_use) |
| 2235 | { |
| 2236 | mysql_cond_signal(&cond); |
| 2237 | status=1; |
| 2238 | } |
| 2239 | mysql_mutex_unlock(&mutex); |
| 2240 | } |
| 2241 | mysql_mutex_unlock(&LOCK_delayed_insert); |
| 2242 | } |
| 2243 | inline uint lock_count() { return locks_in_memory; } |
| 2244 | |
| 2245 | TABLE* get_local_table(THD* client_thd); |
| 2246 | bool open_and_lock_table(); |
| 2247 | bool handle_inserts(void); |
| 2248 | }; |
| 2249 | |
| 2250 | |
| 2251 | I_List<Delayed_insert> delayed_threads; |
| 2252 | |
| 2253 | |
| 2254 | /** |
| 2255 | Return an instance of delayed insert thread that can handle |
| 2256 | inserts into a given table, if it exists. Otherwise return NULL. |
| 2257 | */ |
| 2258 | |
| 2259 | static |
| 2260 | Delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) |
| 2261 | { |
| 2262 | THD_STAGE_INFO(thd, stage_waiting_for_delay_list); |
| 2263 | mysql_mutex_lock(&LOCK_delayed_insert); // Protect master list |
| 2264 | I_List_iterator<Delayed_insert> it(delayed_threads); |
| 2265 | Delayed_insert *di; |
| 2266 | while ((di= it++)) |
| 2267 | { |
| 2268 | if (!cmp(&table_list->db, &di->table_list.db) && |
| 2269 | !cmp(&table_list->table_name, &di->table_list.table_name)) |
| 2270 | { |
| 2271 | di->lock(); |
| 2272 | break; |
| 2273 | } |
| 2274 | } |
| 2275 | mysql_mutex_unlock(&LOCK_delayed_insert); // For unlink from list |
| 2276 | return di; |
| 2277 | } |
| 2278 | |
| 2279 | |
| 2280 | /** |
| 2281 | Attempt to find or create a delayed insert thread to handle inserts |
| 2282 | into this table. |
| 2283 | |
| 2284 | @return In case of success, table_list->table points to a local copy |
| 2285 | of the delayed table or is set to NULL, which indicates a |
| 2286 | request for lock upgrade. In case of failure, value of |
| 2287 | table_list->table is undefined. |
| 2288 | @retval TRUE - this thread ran out of resources OR |
| 2289 | - a newly created delayed insert thread ran out of |
| 2290 | resources OR |
| 2291 | - the created thread failed to open and lock the table |
| 2292 | (e.g. because it does not exist) OR |
| 2293 | - the table opened in the created thread turned out to |
| 2294 | be a view |
| 2295 | @retval FALSE - table successfully opened OR |
| 2296 | - too many delayed insert threads OR |
| 2297 | - the table has triggers and we have to fall back to |
| 2298 | a normal INSERT |
| 2299 | Two latter cases indicate a request for lock upgrade. |
| 2300 | |
| 2301 | XXX: why do we regard INSERT DELAYED into a view as an error and |
| 2302 | do not simply perform a lock upgrade? |
| 2303 | |
| 2304 | TODO: The approach with using two mutexes to work with the |
| 2305 | delayed thread list -- LOCK_delayed_insert and |
| 2306 | LOCK_delayed_create -- is redundant, and we only need one of |
| 2307 | them to protect the list. The reason we have two locks is that |
| 2308 | we do not want to block look-ups in the list while we're waiting |
| 2309 | for the newly created thread to open the delayed table. However, |
| 2310 | this wait itself is redundant -- we always call get_local_table |
| 2311 | later on, and there wait again until the created thread acquires |
| 2312 | a table lock. |
| 2313 | |
| 2314 | As is redundant the concept of locks_in_memory, since we already |
| 2315 | have another counter with similar semantics - tables_in_use, |
| 2316 | both of them are devoted to counting the number of producers for |
| 2317 | a given consumer (delayed insert thread), only at different |
| 2318 | stages of producer-consumer relationship. |
| 2319 | |
| 2320 | The 'status' variable in Delayed_insert is redundant |
| 2321 | too, since there is already di->stacked_inserts. |
| 2322 | */ |
| 2323 | |
| 2324 | static |
| 2325 | bool delayed_get_table(THD *thd, MDL_request *grl_protection_request, |
| 2326 | TABLE_LIST *table_list) |
| 2327 | { |
| 2328 | int error; |
| 2329 | Delayed_insert *di; |
| 2330 | DBUG_ENTER("delayed_get_table" ); |
| 2331 | |
| 2332 | /* Must be set in the parser */ |
| 2333 | DBUG_ASSERT(table_list->db.str); |
| 2334 | |
| 2335 | /* Find the thread which handles this table. */ |
| 2336 | if (!(di= find_handler(thd, table_list))) |
| 2337 | { |
| 2338 | /* |
| 2339 | No match. Create a new thread to handle the table, but |
| 2340 | no more than max_insert_delayed_threads. |
| 2341 | */ |
| 2342 | if (delayed_insert_threads >= thd->variables.max_insert_delayed_threads) |
| 2343 | DBUG_RETURN(0); |
| 2344 | THD_STAGE_INFO(thd, stage_creating_delayed_handler); |
| 2345 | mysql_mutex_lock(&LOCK_delayed_create); |
| 2346 | /* |
| 2347 | The first search above was done without LOCK_delayed_create. |
| 2348 | Another thread might have created the handler in between. Search again. |
| 2349 | */ |
| 2350 | if (! (di= find_handler(thd, table_list))) |
| 2351 | { |
| 2352 | if (!(di= new Delayed_insert(thd->lex->current_select))) |
| 2353 | goto end_create; |
| 2354 | |
| 2355 | /* |
| 2356 | Annotating delayed inserts is not supported. |
| 2357 | */ |
| 2358 | di->thd.variables.binlog_annotate_row_events= 0; |
| 2359 | |
| 2360 | di->thd.set_db(&table_list->db); |
| 2361 | di->thd.set_query(my_strndup(table_list->table_name.str, |
| 2362 | table_list->table_name.length, |
| 2363 | MYF(MY_WME | ME_FATALERROR)), |
| 2364 | table_list->table_name.length, system_charset_info); |
| 2365 | if (di->thd.db.str == NULL || di->thd.query() == NULL) |
| 2366 | { |
| 2367 | /* The error is reported */ |
| 2368 | delete di; |
| 2369 | goto end_create; |
| 2370 | } |
| 2371 | di->table_list= *table_list; // Needed to open table |
| 2372 | /* Replace volatile strings with local copies */ |
| 2373 | di->table_list.alias.str= di->table_list.table_name.str= di->thd.query(); |
| 2374 | di->table_list.alias.length= di->table_list.table_name.length= di->thd.query_length(); |
| 2375 | di->table_list.db= di->thd.db; |
| 2376 | /* We need the tickets so that they can be cloned in handle_delayed_insert */ |
| 2377 | di->grl_protection.init(MDL_key::GLOBAL, "" , "" , |
| 2378 | MDL_INTENTION_EXCLUSIVE, MDL_STATEMENT); |
| 2379 | di->grl_protection.ticket= grl_protection_request->ticket; |
| 2380 | init_mdl_requests(&di->table_list); |
| 2381 | di->table_list.mdl_request.ticket= table_list->mdl_request.ticket; |
| 2382 | |
| 2383 | di->lock(); |
| 2384 | mysql_mutex_lock(&di->mutex); |
| 2385 | if ((error= mysql_thread_create(key_thread_delayed_insert, |
| 2386 | &di->thd.real_id, &connection_attrib, |
| 2387 | handle_delayed_insert, (void*) di))) |
| 2388 | { |
| 2389 | DBUG_PRINT("error" , |
| 2390 | ("Can't create thread to handle delayed insert (error %d)" , |
| 2391 | error)); |
| 2392 | mysql_mutex_unlock(&di->mutex); |
| 2393 | di->unlock(); |
| 2394 | delete di; |
| 2395 | my_error(ER_CANT_CREATE_THREAD, MYF(ME_FATALERROR), error); |
| 2396 | goto end_create; |
| 2397 | } |
| 2398 | |
| 2399 | /* |
| 2400 | Wait until table is open unless the handler thread or the connection |
| 2401 | thread has been killed. Note that we in all cases must wait until the |
| 2402 | handler thread has been properly initialized before exiting. Otherwise |
| 2403 | we risk doing clone_ticket() on a ticket that is no longer valid. |
| 2404 | */ |
| 2405 | THD_STAGE_INFO(thd, stage_waiting_for_handler_open); |
| 2406 | while (!di->handler_thread_initialized || |
| 2407 | (!di->thd.killed && !di->table && !thd->killed)) |
| 2408 | { |
| 2409 | mysql_cond_wait(&di->cond_client, &di->mutex); |
| 2410 | } |
| 2411 | mysql_mutex_unlock(&di->mutex); |
| 2412 | THD_STAGE_INFO(thd, stage_got_old_table); |
| 2413 | if (thd->killed) |
| 2414 | { |
| 2415 | di->unlock(); |
| 2416 | goto end_create; |
| 2417 | } |
| 2418 | if (di->thd.killed) |
| 2419 | { |
| 2420 | if (di->thd.is_error() && ! di->retry) |
| 2421 | { |
| 2422 | /* |
| 2423 | Copy the error message. Note that we don't treat fatal |
| 2424 | errors in the delayed thread as fatal errors in the |
| 2425 | main thread. If delayed thread was killed, we don't |
| 2426 | want to send "Server shutdown in progress" in the |
| 2427 | INSERT THREAD. |
| 2428 | */ |
| 2429 | my_message(di->thd.get_stmt_da()->sql_errno(), |
| 2430 | di->thd.get_stmt_da()->message(), |
| 2431 | MYF(0)); |
| 2432 | } |
| 2433 | di->unlock(); |
| 2434 | goto end_create; |
| 2435 | } |
| 2436 | mysql_mutex_lock(&LOCK_delayed_insert); |
| 2437 | delayed_threads.append(di); |
| 2438 | mysql_mutex_unlock(&LOCK_delayed_insert); |
| 2439 | } |
| 2440 | mysql_mutex_unlock(&LOCK_delayed_create); |
| 2441 | } |
| 2442 | |
| 2443 | mysql_mutex_lock(&di->mutex); |
| 2444 | table_list->table= di->get_local_table(thd); |
| 2445 | mysql_mutex_unlock(&di->mutex); |
| 2446 | if (table_list->table) |
| 2447 | { |
| 2448 | DBUG_ASSERT(! thd->is_error()); |
| 2449 | thd->di= di; |
| 2450 | } |
| 2451 | /* Unlock the delayed insert object after its last access. */ |
| 2452 | di->unlock(); |
| 2453 | DBUG_RETURN((table_list->table == NULL)); |
| 2454 | |
| 2455 | end_create: |
| 2456 | mysql_mutex_unlock(&LOCK_delayed_create); |
| 2457 | DBUG_RETURN(thd->is_error()); |
| 2458 | } |
| 2459 | |
| 2460 | #define memdup_vcol(thd, vcol) \ |
| 2461 | if (vcol) \ |
| 2462 | { \ |
| 2463 | (vcol)= (Virtual_column_info*)(thd)->memdup((vcol), sizeof(*(vcol))); \ |
| 2464 | (vcol)->expr= NULL; \ |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | As we can't let many client threads modify the same TABLE |
| 2469 | structure of the dedicated delayed insert thread, we create an |
| 2470 | own structure for each client thread. This includes a row |
| 2471 | buffer to save the column values and new fields that point to |
| 2472 | the new row buffer. The memory is allocated in the client |
| 2473 | thread and is freed automatically. |
| 2474 | |
| 2475 | @pre This function is called from the client thread. Delayed |
| 2476 | insert thread mutex must be acquired before invoking this |
| 2477 | function. |
| 2478 | |
| 2479 | @return Not-NULL table object on success. NULL in case of an error, |
| 2480 | which is set in client_thd. |
| 2481 | */ |
| 2482 | |
| 2483 | TABLE *Delayed_insert::get_local_table(THD* client_thd) |
| 2484 | { |
| 2485 | my_ptrdiff_t adjust_ptrs; |
| 2486 | Field **field,**org_field, *found_next_number_field; |
| 2487 | TABLE *copy; |
| 2488 | TABLE_SHARE *share; |
| 2489 | uchar *bitmap; |
| 2490 | char *copy_tmp; |
| 2491 | uint bitmaps_used; |
| 2492 | DBUG_ENTER("Delayed_insert::get_local_table" ); |
| 2493 | |
| 2494 | /* First request insert thread to get a lock */ |
| 2495 | status=1; |
| 2496 | tables_in_use++; |
| 2497 | if (!thd.lock) // Table is not locked |
| 2498 | { |
| 2499 | THD_STAGE_INFO(client_thd, stage_waiting_for_handler_lock); |
| 2500 | mysql_cond_signal(&cond); // Tell handler to lock table |
| 2501 | while (!thd.killed && !thd.lock && ! client_thd->killed) |
| 2502 | { |
| 2503 | mysql_cond_wait(&cond_client, &mutex); |
| 2504 | } |
| 2505 | THD_STAGE_INFO(client_thd, stage_got_handler_lock); |
| 2506 | if (client_thd->killed) |
| 2507 | goto error; |
| 2508 | if (thd.killed) |
| 2509 | { |
| 2510 | /* |
| 2511 | Copy the error message. Note that we don't treat fatal |
| 2512 | errors in the delayed thread as fatal errors in the |
| 2513 | main thread. If delayed thread was killed, we don't |
| 2514 | want to send "Server shutdown in progress" in the |
| 2515 | INSERT THREAD. |
| 2516 | |
| 2517 | The thread could be killed with an error message if |
| 2518 | di->handle_inserts() or di->open_and_lock_table() fails. |
| 2519 | The thread could be killed without an error message if |
| 2520 | killed using kill_delayed_threads_for_table(). |
| 2521 | */ |
| 2522 | if (!thd.is_error()) |
| 2523 | my_message(ER_QUERY_INTERRUPTED, ER_THD(&thd, ER_QUERY_INTERRUPTED), |
| 2524 | MYF(0)); |
| 2525 | else |
| 2526 | my_message(thd.get_stmt_da()->sql_errno(), |
| 2527 | thd.get_stmt_da()->message(), MYF(0)); |
| 2528 | goto error; |
| 2529 | } |
| 2530 | } |
| 2531 | share= table->s; |
| 2532 | |
| 2533 | /* |
| 2534 | Allocate memory for the TABLE object, the field pointers array, and |
| 2535 | one record buffer of reclength size. Normally a table has three |
| 2536 | record buffers of rec_buff_length size, which includes alignment |
| 2537 | bytes. Since the table copy is used for creating one record only, |
| 2538 | the other record buffers and alignment are unnecessary. |
| 2539 | */ |
| 2540 | THD_STAGE_INFO(client_thd, stage_allocating_local_table); |
| 2541 | copy_tmp= (char*) client_thd->alloc(sizeof(*copy)+ |
| 2542 | (share->fields+1)*sizeof(Field**)+ |
| 2543 | share->reclength + |
| 2544 | share->column_bitmap_size*4); |
| 2545 | if (!copy_tmp) |
| 2546 | goto error; |
| 2547 | |
| 2548 | /* Copy the TABLE object. */ |
| 2549 | copy= new (copy_tmp) TABLE; |
| 2550 | *copy= *table; |
| 2551 | |
| 2552 | /* We don't need to change the file handler here */ |
| 2553 | /* Assign the pointers for the field pointers array and the record. */ |
| 2554 | field= copy->field= (Field**) (copy + 1); |
| 2555 | bitmap= (uchar*) (field + share->fields + 1); |
| 2556 | copy->record[0]= (bitmap + share->column_bitmap_size*4); |
| 2557 | memcpy((char*) copy->record[0], (char*) table->record[0], share->reclength); |
| 2558 | if (share->default_fields || share->default_expressions) |
| 2559 | { |
| 2560 | copy->default_field= (Field**) |
| 2561 | client_thd->alloc((share->default_fields + |
| 2562 | share->default_expressions + 1)* |
| 2563 | sizeof(Field*)); |
| 2564 | if (!copy->default_field) |
| 2565 | goto error; |
| 2566 | } |
| 2567 | |
| 2568 | if (share->virtual_fields) |
| 2569 | { |
| 2570 | copy->vfield= (Field **) client_thd->alloc((share->virtual_fields+1)* |
| 2571 | sizeof(Field*)); |
| 2572 | if (!copy->vfield) |
| 2573 | goto error; |
| 2574 | } |
| 2575 | copy->expr_arena= NULL; |
| 2576 | |
| 2577 | /* Ensure we don't use the table list of the original table */ |
| 2578 | copy->pos_in_table_list= 0; |
| 2579 | |
| 2580 | /* |
| 2581 | Make a copy of all fields. |
| 2582 | The copied fields need to point into the copied record. This is done |
| 2583 | by copying the field objects with their old pointer values and then |
| 2584 | "move" the pointers by the distance between the original and copied |
| 2585 | records. That way we preserve the relative positions in the records. |
| 2586 | */ |
| 2587 | adjust_ptrs= PTR_BYTE_DIFF(copy->record[0], table->record[0]); |
| 2588 | found_next_number_field= table->found_next_number_field; |
| 2589 | for (org_field= table->field; *org_field; org_field++, field++) |
| 2590 | { |
| 2591 | if (!(*field= (*org_field)->make_new_field(client_thd->mem_root, copy, 1))) |
| 2592 | goto error; |
| 2593 | (*field)->unireg_check= (*org_field)->unireg_check; |
| 2594 | (*field)->orig_table= copy; // Remove connection |
| 2595 | (*field)->move_field_offset(adjust_ptrs); // Point at copy->record[0] |
| 2596 | memdup_vcol(client_thd, (*field)->vcol_info); |
| 2597 | memdup_vcol(client_thd, (*field)->default_value); |
| 2598 | memdup_vcol(client_thd, (*field)->check_constraint); |
| 2599 | if (*org_field == found_next_number_field) |
| 2600 | (*field)->table->found_next_number_field= *field; |
| 2601 | } |
| 2602 | *field=0; |
| 2603 | |
| 2604 | if (share->virtual_fields || share->default_expressions || |
| 2605 | share->default_fields) |
| 2606 | { |
| 2607 | bool error_reported= FALSE; |
| 2608 | if (unlikely(!(copy->def_vcol_set= |
| 2609 | (MY_BITMAP*) alloc_root(client_thd->mem_root, |
| 2610 | sizeof(MY_BITMAP))))) |
| 2611 | goto error; |
| 2612 | if (unlikely(parse_vcol_defs(client_thd, client_thd->mem_root, copy, |
| 2613 | &error_reported))) |
| 2614 | goto error; |
| 2615 | } |
| 2616 | |
| 2617 | switch_defaults_to_nullable_trigger_fields(copy); |
| 2618 | |
| 2619 | /* Adjust in_use for pointing to client thread */ |
| 2620 | copy->in_use= client_thd; |
| 2621 | |
| 2622 | /* Adjust lock_count. This table object is not part of a lock. */ |
| 2623 | copy->lock_count= 0; |
| 2624 | |
| 2625 | /* Adjust bitmaps */ |
| 2626 | copy->def_read_set.bitmap= (my_bitmap_map*) bitmap; |
| 2627 | copy->def_write_set.bitmap= ((my_bitmap_map*) |
| 2628 | (bitmap + share->column_bitmap_size)); |
| 2629 | bitmaps_used= 2; |
| 2630 | if (share->virtual_fields) |
| 2631 | { |
| 2632 | my_bitmap_init(copy->def_vcol_set, |
| 2633 | (my_bitmap_map*) (bitmap + |
| 2634 | bitmaps_used*share->column_bitmap_size), |
| 2635 | share->fields, FALSE); |
| 2636 | bitmaps_used++; |
| 2637 | copy->vcol_set= copy->def_vcol_set; |
| 2638 | } |
| 2639 | if (share->default_fields || share->default_expressions) |
| 2640 | { |
| 2641 | my_bitmap_init(©->has_value_set, |
| 2642 | (my_bitmap_map*) (bitmap + |
| 2643 | bitmaps_used*share->column_bitmap_size), |
| 2644 | share->fields, FALSE); |
| 2645 | } |
| 2646 | copy->tmp_set.bitmap= 0; // To catch errors |
| 2647 | bzero((char*) bitmap, share->column_bitmap_size * bitmaps_used); |
| 2648 | copy->read_set= ©->def_read_set; |
| 2649 | copy->write_set= ©->def_write_set; |
| 2650 | |
| 2651 | DBUG_RETURN(copy); |
| 2652 | |
| 2653 | /* Got fatal error */ |
| 2654 | error: |
| 2655 | tables_in_use--; |
| 2656 | mysql_cond_signal(&cond); // Inform thread about abort |
| 2657 | DBUG_RETURN(0); |
| 2658 | } |
| 2659 | |
| 2660 | |
| 2661 | /* Put a question in queue */ |
| 2662 | |
| 2663 | static |
| 2664 | int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic, |
| 2665 | LEX_STRING query, bool ignore, bool log_on) |
| 2666 | { |
| 2667 | delayed_row *row= 0; |
| 2668 | Delayed_insert *di=thd->di; |
| 2669 | const Discrete_interval *forced_auto_inc; |
| 2670 | DBUG_ENTER("write_delayed" ); |
| 2671 | DBUG_PRINT("enter" , ("query = '%s' length %lu" , query.str, |
| 2672 | (ulong) query.length)); |
| 2673 | |
| 2674 | THD_STAGE_INFO(thd, stage_waiting_for_handler_insert); |
| 2675 | mysql_mutex_lock(&di->mutex); |
| 2676 | while (di->stacked_inserts >= delayed_queue_size && !thd->killed) |
| 2677 | mysql_cond_wait(&di->cond_client, &di->mutex); |
| 2678 | THD_STAGE_INFO(thd, stage_storing_row_into_queue); |
| 2679 | |
| 2680 | if (thd->killed) |
| 2681 | goto err; |
| 2682 | |
| 2683 | /* |
| 2684 | Take a copy of the query string, if there is any. The string will |
| 2685 | be free'ed when the row is destroyed. If there is no query string, |
| 2686 | we don't do anything special. |
| 2687 | */ |
| 2688 | |
| 2689 | if (query.str) |
| 2690 | { |
| 2691 | char *str; |
| 2692 | if (!(str= my_strndup(query.str, query.length, MYF(MY_WME)))) |
| 2693 | goto err; |
| 2694 | query.str= str; |
| 2695 | } |
| 2696 | row= new delayed_row(query, duplic, ignore, log_on); |
| 2697 | if (row == NULL) |
| 2698 | { |
| 2699 | my_free(query.str); |
| 2700 | goto err; |
| 2701 | } |
| 2702 | |
| 2703 | /* This can't be THREAD_SPECIFIC as it's freed in delayed thread */ |
| 2704 | if (!(row->record= (char*) my_malloc(table->s->reclength, |
| 2705 | MYF(MY_WME)))) |
| 2706 | goto err; |
| 2707 | memcpy(row->record, table->record[0], table->s->reclength); |
| 2708 | row->start_time= thd->start_time; |
| 2709 | row->start_time_sec_part= thd->start_time_sec_part; |
| 2710 | row->query_start_sec_part_used= thd->query_start_sec_part_used; |
| 2711 | /* |
| 2712 | those are for the binlog: LAST_INSERT_ID() has been evaluated at this |
| 2713 | time, so record does not need it, but statement-based binlogging of the |
| 2714 | INSERT will need when the row is actually inserted. |
| 2715 | As for SET INSERT_ID, DELAYED does not honour it (BUG#20830). |
| 2716 | */ |
| 2717 | row->stmt_depends_on_first_successful_insert_id_in_prev_stmt= |
| 2718 | thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt; |
| 2719 | row->first_successful_insert_id_in_prev_stmt= |
| 2720 | thd->first_successful_insert_id_in_prev_stmt; |
| 2721 | |
| 2722 | /* Add session variable timezone |
| 2723 | Time_zone object will not be freed even the thread is ended. |
| 2724 | So we can get time_zone object from thread which handling delayed statement. |
| 2725 | See the comment of my_tz_find() for detail. |
| 2726 | */ |
| 2727 | if (thd->time_zone_used) |
| 2728 | { |
| 2729 | row->time_zone = thd->variables.time_zone; |
| 2730 | } |
| 2731 | else |
| 2732 | { |
| 2733 | row->time_zone = NULL; |
| 2734 | } |
| 2735 | /* Copy session variables. */ |
| 2736 | row->auto_increment_increment= thd->variables.auto_increment_increment; |
| 2737 | row->auto_increment_offset= thd->variables.auto_increment_offset; |
| 2738 | row->sql_mode= thd->variables.sql_mode; |
| 2739 | row->auto_increment_field_not_null= table->auto_increment_field_not_null; |
| 2740 | |
| 2741 | /* Copy the next forced auto increment value, if any. */ |
| 2742 | if ((forced_auto_inc= thd->auto_inc_intervals_forced.get_next())) |
| 2743 | { |
| 2744 | row->forced_insert_id= forced_auto_inc->minimum(); |
| 2745 | DBUG_PRINT("delayed" , ("transmitting auto_inc: %lu" , |
| 2746 | (ulong) row->forced_insert_id)); |
| 2747 | } |
| 2748 | |
| 2749 | di->rows.push_back(row); |
| 2750 | di->stacked_inserts++; |
| 2751 | di->status=1; |
| 2752 | if (table->s->blob_fields) |
| 2753 | unlink_blobs(table); |
| 2754 | mysql_cond_signal(&di->cond); |
| 2755 | |
| 2756 | thread_safe_increment(delayed_rows_in_use,&LOCK_delayed_status); |
| 2757 | mysql_mutex_unlock(&di->mutex); |
| 2758 | DBUG_RETURN(0); |
| 2759 | |
| 2760 | err: |
| 2761 | delete row; |
| 2762 | mysql_mutex_unlock(&di->mutex); |
| 2763 | DBUG_RETURN(1); |
| 2764 | } |
| 2765 | |
| 2766 | /** |
| 2767 | Signal the delayed insert thread that this user connection |
| 2768 | is finished using it for this statement. |
| 2769 | */ |
| 2770 | |
| 2771 | static void end_delayed_insert(THD *thd) |
| 2772 | { |
| 2773 | DBUG_ENTER("end_delayed_insert" ); |
| 2774 | Delayed_insert *di=thd->di; |
| 2775 | mysql_mutex_lock(&di->mutex); |
| 2776 | DBUG_PRINT("info" ,("tables in use: %d" ,di->tables_in_use)); |
| 2777 | if (!--di->tables_in_use || di->thd.killed) |
| 2778 | { // Unlock table |
| 2779 | di->status=1; |
| 2780 | mysql_cond_signal(&di->cond); |
| 2781 | } |
| 2782 | mysql_mutex_unlock(&di->mutex); |
| 2783 | DBUG_VOID_RETURN; |
| 2784 | } |
| 2785 | |
| 2786 | |
| 2787 | /* We kill all delayed threads when doing flush-tables */ |
| 2788 | |
| 2789 | void kill_delayed_threads(void) |
| 2790 | { |
| 2791 | DBUG_ENTER("kill_delayed_threads" ); |
| 2792 | mysql_mutex_lock(&LOCK_delayed_insert); // For unlink from list |
| 2793 | |
| 2794 | I_List_iterator<Delayed_insert> it(delayed_threads); |
| 2795 | Delayed_insert *di; |
| 2796 | while ((di= it++)) |
| 2797 | { |
| 2798 | mysql_mutex_lock(&di->thd.LOCK_thd_kill); |
| 2799 | if (di->thd.killed < KILL_CONNECTION) |
| 2800 | di->thd.set_killed_no_mutex(KILL_CONNECTION); |
| 2801 | if (di->thd.mysys_var) |
| 2802 | { |
| 2803 | mysql_mutex_lock(&di->thd.mysys_var->mutex); |
| 2804 | if (di->thd.mysys_var->current_cond) |
| 2805 | { |
| 2806 | /* |
| 2807 | We need the following test because the main mutex may be locked |
| 2808 | in handle_delayed_insert() |
| 2809 | */ |
| 2810 | if (&di->mutex != di->thd.mysys_var->current_mutex) |
| 2811 | mysql_mutex_lock(di->thd.mysys_var->current_mutex); |
| 2812 | mysql_cond_broadcast(di->thd.mysys_var->current_cond); |
| 2813 | if (&di->mutex != di->thd.mysys_var->current_mutex) |
| 2814 | mysql_mutex_unlock(di->thd.mysys_var->current_mutex); |
| 2815 | } |
| 2816 | mysql_mutex_unlock(&di->thd.mysys_var->mutex); |
| 2817 | } |
| 2818 | mysql_mutex_unlock(&di->thd.LOCK_thd_kill); |
| 2819 | } |
| 2820 | mysql_mutex_unlock(&LOCK_delayed_insert); // For unlink from list |
| 2821 | DBUG_VOID_RETURN; |
| 2822 | } |
| 2823 | |
| 2824 | |
| 2825 | /** |
| 2826 | A strategy for the prelocking algorithm which prevents the |
| 2827 | delayed insert thread from opening tables with engines which |
| 2828 | do not support delayed inserts. |
| 2829 | |
| 2830 | Particularly it allows to abort open_tables() as soon as we |
| 2831 | discover that we have opened a MERGE table, without acquiring |
| 2832 | metadata locks on underlying tables. |
| 2833 | */ |
| 2834 | |
| 2835 | class Delayed_prelocking_strategy : public Prelocking_strategy |
| 2836 | { |
| 2837 | public: |
| 2838 | virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx, |
| 2839 | Sroutine_hash_entry *rt, sp_head *sp, |
| 2840 | bool *need_prelocking); |
| 2841 | virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx, |
| 2842 | TABLE_LIST *table_list, bool *need_prelocking); |
| 2843 | virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx, |
| 2844 | TABLE_LIST *table_list, bool *need_prelocking); |
| 2845 | }; |
| 2846 | |
| 2847 | |
| 2848 | bool Delayed_prelocking_strategy:: |
| 2849 | handle_table(THD *thd, Query_tables_list *prelocking_ctx, |
| 2850 | TABLE_LIST *table_list, bool *need_prelocking) |
| 2851 | { |
| 2852 | DBUG_ASSERT(table_list->lock_type == TL_WRITE_DELAYED); |
| 2853 | |
| 2854 | if (!(table_list->table->file->ha_table_flags() & HA_CAN_INSERT_DELAYED)) |
| 2855 | { |
| 2856 | my_error(ER_DELAYED_NOT_SUPPORTED, MYF(0), table_list->table_name.str); |
| 2857 | return TRUE; |
| 2858 | } |
| 2859 | return FALSE; |
| 2860 | } |
| 2861 | |
| 2862 | |
| 2863 | bool Delayed_prelocking_strategy:: |
| 2864 | handle_routine(THD *thd, Query_tables_list *prelocking_ctx, |
| 2865 | Sroutine_hash_entry *rt, sp_head *sp, |
| 2866 | bool *need_prelocking) |
| 2867 | { |
| 2868 | /* LEX used by the delayed insert thread has no routines. */ |
| 2869 | DBUG_ASSERT(0); |
| 2870 | return FALSE; |
| 2871 | } |
| 2872 | |
| 2873 | |
| 2874 | bool Delayed_prelocking_strategy:: |
| 2875 | handle_view(THD *thd, Query_tables_list *prelocking_ctx, |
| 2876 | TABLE_LIST *table_list, bool *need_prelocking) |
| 2877 | { |
| 2878 | /* We don't open views in the delayed insert thread. */ |
| 2879 | DBUG_ASSERT(0); |
| 2880 | return FALSE; |
| 2881 | } |
| 2882 | |
| 2883 | |
| 2884 | /** |
| 2885 | Open and lock table for use by delayed thread and check that |
| 2886 | this table is suitable for delayed inserts. |
| 2887 | |
| 2888 | @retval FALSE - Success. |
| 2889 | @retval TRUE - Failure. |
| 2890 | */ |
| 2891 | |
| 2892 | bool Delayed_insert::open_and_lock_table() |
| 2893 | { |
| 2894 | Delayed_prelocking_strategy prelocking_strategy; |
| 2895 | |
| 2896 | /* |
| 2897 | Use special prelocking strategy to get ER_DELAYED_NOT_SUPPORTED |
| 2898 | error for tables with engines which don't support delayed inserts. |
| 2899 | |
| 2900 | We can't do auto-repair in insert delayed thread, as it would hang |
| 2901 | when trying to an exclusive MDL_LOCK on the table during repair |
| 2902 | as the connection thread has a SHARED_WRITE lock. |
| 2903 | */ |
| 2904 | if (!(table= open_n_lock_single_table(&thd, &table_list, |
| 2905 | TL_WRITE_DELAYED, |
| 2906 | MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK | |
| 2907 | MYSQL_OPEN_IGNORE_REPAIR, |
| 2908 | &prelocking_strategy))) |
| 2909 | { |
| 2910 | /* If table was crashed, then upper level should retry open+repair */ |
| 2911 | retry= table_list.crashed; |
| 2912 | thd.fatal_error(); // Abort waiting inserts |
| 2913 | return TRUE; |
| 2914 | } |
| 2915 | |
| 2916 | if (table->triggers || table->check_constraints) |
| 2917 | { |
| 2918 | /* |
| 2919 | Table has triggers or check constraints. This is not an error, but we do |
| 2920 | not support these with delayed insert. Terminate the delayed |
| 2921 | thread without an error and thus request lock upgrade. |
| 2922 | */ |
| 2923 | return TRUE; |
| 2924 | } |
| 2925 | table->copy_blobs= 1; |
| 2926 | return FALSE; |
| 2927 | } |
| 2928 | |
| 2929 | |
| 2930 | /* |
| 2931 | * Create a new delayed insert thread |
| 2932 | */ |
| 2933 | |
| 2934 | pthread_handler_t handle_delayed_insert(void *arg) |
| 2935 | { |
| 2936 | Delayed_insert *di=(Delayed_insert*) arg; |
| 2937 | THD *thd= &di->thd; |
| 2938 | |
| 2939 | pthread_detach_this_thread(); |
| 2940 | /* Add thread to THD list so that's it's visible in 'show processlist' */ |
| 2941 | thd->set_start_time(); |
| 2942 | add_to_active_threads(thd); |
| 2943 | if (abort_loop) |
| 2944 | thd->set_killed(KILL_CONNECTION); |
| 2945 | else |
| 2946 | thd->reset_killed(); |
| 2947 | |
| 2948 | mysql_thread_set_psi_id(thd->thread_id); |
| 2949 | |
| 2950 | /* |
| 2951 | Wait until the client runs into mysql_cond_wait(), |
| 2952 | where we free it after the table is opened and di linked in the list. |
| 2953 | If we did not wait here, the client might detect the opened table |
| 2954 | before it is linked to the list. It would release LOCK_delayed_create |
| 2955 | and allow another thread to create another handler for the same table, |
| 2956 | since it does not find one in the list. |
| 2957 | */ |
| 2958 | mysql_mutex_lock(&di->mutex); |
| 2959 | if (my_thread_init()) |
| 2960 | { |
| 2961 | /* Can't use my_error since store_globals has not yet been called */ |
| 2962 | thd->get_stmt_da()->set_error_status(ER_OUT_OF_RESOURCES); |
| 2963 | di->handler_thread_initialized= TRUE; |
| 2964 | } |
| 2965 | else |
| 2966 | { |
| 2967 | DBUG_ENTER("handle_delayed_insert" ); |
| 2968 | thd->thread_stack= (char*) &thd; |
| 2969 | if (init_thr_lock() || thd->store_globals()) |
| 2970 | { |
| 2971 | /* Can't use my_error since store_globals has perhaps failed */ |
| 2972 | thd->get_stmt_da()->set_error_status(ER_OUT_OF_RESOURCES); |
| 2973 | di->handler_thread_initialized= TRUE; |
| 2974 | thd->fatal_error(); |
| 2975 | goto err; |
| 2976 | } |
| 2977 | |
| 2978 | thd->lex->sql_command= SQLCOM_INSERT; // For innodb::store_lock() |
| 2979 | |
| 2980 | /* |
| 2981 | INSERT DELAYED has to go to row-based format because the time |
| 2982 | at which rows are inserted cannot be determined in mixed mode. |
| 2983 | */ |
| 2984 | thd->set_current_stmt_binlog_format_row_if_mixed(); |
| 2985 | |
| 2986 | /* |
| 2987 | Clone tickets representing protection against GRL and the lock on |
| 2988 | the target table for the insert and add them to the list of granted |
| 2989 | metadata locks held by the handler thread. This is safe since the |
| 2990 | handler thread is not holding nor waiting on any metadata locks. |
| 2991 | */ |
| 2992 | if (thd->mdl_context.clone_ticket(&di->grl_protection) || |
| 2993 | thd->mdl_context.clone_ticket(&di->table_list.mdl_request)) |
| 2994 | { |
| 2995 | thd->mdl_context.release_transactional_locks(); |
| 2996 | di->handler_thread_initialized= TRUE; |
| 2997 | goto err; |
| 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | Now that the ticket has been cloned, it is safe for the connection |
| 3002 | thread to exit. |
| 3003 | */ |
| 3004 | di->handler_thread_initialized= TRUE; |
| 3005 | di->table_list.mdl_request.ticket= NULL; |
| 3006 | |
| 3007 | if (di->open_and_lock_table()) |
| 3008 | goto err; |
| 3009 | |
| 3010 | /* |
| 3011 | INSERT DELAYED generally expects thd->lex->current_select to be NULL, |
| 3012 | since this is not an attribute of the current thread. This can lead to |
| 3013 | problems if the thread that spawned the current one disconnects. |
| 3014 | current_select will then point to freed memory. But current_select is |
| 3015 | required to resolve the partition function. So, after fulfilling that |
| 3016 | requirement, we set the current_select to 0. |
| 3017 | */ |
| 3018 | thd->lex->current_select= NULL; |
| 3019 | |
| 3020 | /* Tell client that the thread is initialized */ |
| 3021 | mysql_cond_signal(&di->cond_client); |
| 3022 | |
| 3023 | /* |
| 3024 | Inform mdl that it needs to call mysql_lock_abort to abort locks |
| 3025 | for delayed insert. |
| 3026 | */ |
| 3027 | thd->mdl_context.set_needs_thr_lock_abort(TRUE); |
| 3028 | |
| 3029 | di->table->mark_columns_needed_for_insert(); |
| 3030 | /* Mark all columns for write as we don't know which columns we get from user */ |
| 3031 | bitmap_set_all(di->table->write_set); |
| 3032 | |
| 3033 | /* Now wait until we get an insert or lock to handle */ |
| 3034 | /* We will not abort as long as a client thread uses this thread */ |
| 3035 | |
| 3036 | for (;;) |
| 3037 | { |
| 3038 | if (thd->killed) |
| 3039 | { |
| 3040 | uint lock_count; |
| 3041 | DBUG_PRINT("delayed" , ("Insert delayed killed" )); |
| 3042 | /* |
| 3043 | Remove this from delay insert list so that no one can request a |
| 3044 | table from this |
| 3045 | */ |
| 3046 | mysql_mutex_unlock(&di->mutex); |
| 3047 | mysql_mutex_lock(&LOCK_delayed_insert); |
| 3048 | di->unlink(); |
| 3049 | lock_count=di->lock_count(); |
| 3050 | mysql_mutex_unlock(&LOCK_delayed_insert); |
| 3051 | mysql_mutex_lock(&di->mutex); |
| 3052 | if (!lock_count && !di->tables_in_use && !di->stacked_inserts && |
| 3053 | !thd->lock) |
| 3054 | break; // Time to die |
| 3055 | } |
| 3056 | |
| 3057 | /* Shouldn't wait if killed or an insert is waiting. */ |
| 3058 | DBUG_PRINT("delayed" , |
| 3059 | ("thd->killed: %d di->status: %d di->stacked_inserts: %d" , |
| 3060 | thd->killed, di->status, di->stacked_inserts)); |
| 3061 | if (!thd->killed && !di->status && !di->stacked_inserts) |
| 3062 | { |
| 3063 | struct timespec abstime; |
| 3064 | set_timespec(abstime, delayed_insert_timeout); |
| 3065 | |
| 3066 | /* Information for pthread_kill */ |
| 3067 | mysql_mutex_unlock(&di->mutex); |
| 3068 | mysql_mutex_lock(&di->thd.mysys_var->mutex); |
| 3069 | di->thd.mysys_var->current_mutex= &di->mutex; |
| 3070 | di->thd.mysys_var->current_cond= &di->cond; |
| 3071 | mysql_mutex_unlock(&di->thd.mysys_var->mutex); |
| 3072 | mysql_mutex_lock(&di->mutex); |
| 3073 | THD_STAGE_INFO(&(di->thd), stage_waiting_for_insert); |
| 3074 | |
| 3075 | DBUG_PRINT("info" ,("Waiting for someone to insert rows" )); |
| 3076 | while (!thd->killed && !di->status) |
| 3077 | { |
| 3078 | int error; |
| 3079 | mysql_audit_release(thd); |
| 3080 | error= mysql_cond_timedwait(&di->cond, &di->mutex, &abstime); |
| 3081 | #ifdef EXTRA_DEBUG |
| 3082 | if (error && error != EINTR && error != ETIMEDOUT) |
| 3083 | { |
| 3084 | fprintf(stderr, "Got error %d from mysql_cond_timedwait\n" , error); |
| 3085 | DBUG_PRINT("error" , ("Got error %d from mysql_cond_timedwait" , |
| 3086 | error)); |
| 3087 | } |
| 3088 | #endif |
| 3089 | if (error == ETIMEDOUT || error == ETIME) |
| 3090 | thd->set_killed(KILL_CONNECTION); |
| 3091 | } |
| 3092 | /* We can't lock di->mutex and mysys_var->mutex at the same time */ |
| 3093 | mysql_mutex_unlock(&di->mutex); |
| 3094 | mysql_mutex_lock(&di->thd.mysys_var->mutex); |
| 3095 | di->thd.mysys_var->current_mutex= 0; |
| 3096 | di->thd.mysys_var->current_cond= 0; |
| 3097 | mysql_mutex_unlock(&di->thd.mysys_var->mutex); |
| 3098 | mysql_mutex_lock(&di->mutex); |
| 3099 | } |
| 3100 | DBUG_PRINT("delayed" , |
| 3101 | ("thd->killed: %d di->tables_in_use: %d thd->lock: %d" , |
| 3102 | thd->killed, di->tables_in_use, thd->lock != 0)); |
| 3103 | |
| 3104 | if (di->tables_in_use && ! thd->lock && !thd->killed) |
| 3105 | { |
| 3106 | /* |
| 3107 | Request for new delayed insert. |
| 3108 | Lock the table, but avoid to be blocked by a global read lock. |
| 3109 | If we got here while a global read lock exists, then one or more |
| 3110 | inserts started before the lock was requested. These are allowed |
| 3111 | to complete their work before the server returns control to the |
| 3112 | client which requested the global read lock. The delayed insert |
| 3113 | handler will close the table and finish when the outstanding |
| 3114 | inserts are done. |
| 3115 | */ |
| 3116 | if (! (thd->lock= mysql_lock_tables(thd, &di->table, 1, 0))) |
| 3117 | { |
| 3118 | /* Fatal error */ |
| 3119 | thd->set_killed(KILL_CONNECTION); |
| 3120 | } |
| 3121 | mysql_cond_broadcast(&di->cond_client); |
| 3122 | } |
| 3123 | if (di->stacked_inserts) |
| 3124 | { |
| 3125 | if (di->handle_inserts()) |
| 3126 | { |
| 3127 | /* Some fatal error */ |
| 3128 | thd->set_killed(KILL_CONNECTION); |
| 3129 | } |
| 3130 | } |
| 3131 | di->status=0; |
| 3132 | if (!di->stacked_inserts && !di->tables_in_use && thd->lock) |
| 3133 | { |
| 3134 | /* |
| 3135 | No one is doing a insert delayed |
| 3136 | Unlock table so that other threads can use it |
| 3137 | */ |
| 3138 | MYSQL_LOCK *lock=thd->lock; |
| 3139 | thd->lock=0; |
| 3140 | mysql_mutex_unlock(&di->mutex); |
| 3141 | /* |
| 3142 | We need to release next_insert_id before unlocking. This is |
| 3143 | enforced by handler::ha_external_lock(). |
| 3144 | */ |
| 3145 | di->table->file->ha_release_auto_increment(); |
| 3146 | mysql_unlock_tables(thd, lock); |
| 3147 | trans_commit_stmt(thd); |
| 3148 | di->group_count=0; |
| 3149 | mysql_audit_release(thd); |
| 3150 | mysql_mutex_lock(&di->mutex); |
| 3151 | } |
| 3152 | if (di->tables_in_use) |
| 3153 | mysql_cond_broadcast(&di->cond_client); // If waiting clients |
| 3154 | } |
| 3155 | |
| 3156 | err: |
| 3157 | DBUG_LEAVE; |
| 3158 | } |
| 3159 | |
| 3160 | { |
| 3161 | DBUG_ENTER("handle_delayed_insert-cleanup" ); |
| 3162 | di->table=0; |
| 3163 | mysql_mutex_unlock(&di->mutex); |
| 3164 | |
| 3165 | /* |
| 3166 | Protect against mdl_locks trying to access open tables |
| 3167 | We use KILL_CONNECTION_HARD here to ensure that |
| 3168 | THD::notify_shared_lock() dosn't try to access open tables after |
| 3169 | this. |
| 3170 | */ |
| 3171 | mysql_mutex_lock(&thd->LOCK_thd_data); |
| 3172 | thd->mdl_context.set_needs_thr_lock_abort(0); |
| 3173 | mysql_mutex_unlock(&thd->LOCK_thd_data); |
| 3174 | thd->set_killed(KILL_CONNECTION_HARD); // If error |
| 3175 | |
| 3176 | close_thread_tables(thd); // Free the table |
| 3177 | thd->mdl_context.release_transactional_locks(); |
| 3178 | mysql_cond_broadcast(&di->cond_client); // Safety |
| 3179 | |
| 3180 | mysql_mutex_lock(&LOCK_delayed_create); // Because of delayed_get_table |
| 3181 | mysql_mutex_lock(&LOCK_delayed_insert); |
| 3182 | /* |
| 3183 | di should be unlinked from the thread handler list and have no active |
| 3184 | clients |
| 3185 | */ |
| 3186 | delete di; |
| 3187 | mysql_mutex_unlock(&LOCK_delayed_insert); |
| 3188 | mysql_mutex_unlock(&LOCK_delayed_create); |
| 3189 | |
| 3190 | DBUG_LEAVE; |
| 3191 | } |
| 3192 | my_thread_end(); |
| 3193 | pthread_exit(0); |
| 3194 | |
| 3195 | return 0; |
| 3196 | } |
| 3197 | |
| 3198 | |
| 3199 | /* Remove all pointers to data for blob fields so that original table doesn't try to free them */ |
| 3200 | |
| 3201 | static void unlink_blobs(TABLE *table) |
| 3202 | { |
| 3203 | for (Field **ptr=table->field ; *ptr ; ptr++) |
| 3204 | { |
| 3205 | if ((*ptr)->flags & BLOB_FLAG) |
| 3206 | ((Field_blob *) (*ptr))->clear_temporary(); |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | /* Free blobs stored in current row */ |
| 3211 | |
| 3212 | static void free_delayed_insert_blobs(TABLE *table) |
| 3213 | { |
| 3214 | for (Field **ptr=table->field ; *ptr ; ptr++) |
| 3215 | { |
| 3216 | if ((*ptr)->flags & BLOB_FLAG) |
| 3217 | ((Field_blob *) *ptr)->free(); |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | |
| 3222 | /* set value field for blobs to point to data in record */ |
| 3223 | |
| 3224 | static void set_delayed_insert_blobs(TABLE *table) |
| 3225 | { |
| 3226 | for (Field **ptr=table->field ; *ptr ; ptr++) |
| 3227 | { |
| 3228 | if ((*ptr)->flags & BLOB_FLAG) |
| 3229 | { |
| 3230 | Field_blob *blob= ((Field_blob *) *ptr); |
| 3231 | uchar *data= blob->get_ptr(); |
| 3232 | if (data) |
| 3233 | blob->set_value(data); // Set value.ptr() to point to data |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | |
| 3239 | bool Delayed_insert::handle_inserts(void) |
| 3240 | { |
| 3241 | int error; |
| 3242 | ulong max_rows; |
| 3243 | bool has_trans = TRUE; |
| 3244 | bool using_ignore= 0, using_opt_replace= 0, |
| 3245 | using_bin_log= mysql_bin_log.is_open(); |
| 3246 | delayed_row *row; |
| 3247 | DBUG_ENTER("handle_inserts" ); |
| 3248 | |
| 3249 | /* Allow client to insert new rows */ |
| 3250 | mysql_mutex_unlock(&mutex); |
| 3251 | |
| 3252 | table->next_number_field=table->found_next_number_field; |
| 3253 | table->use_all_columns(); |
| 3254 | |
| 3255 | THD_STAGE_INFO(&thd, stage_upgrading_lock); |
| 3256 | if (thr_upgrade_write_delay_lock(*thd.lock->locks, delayed_lock, |
| 3257 | thd.variables.lock_wait_timeout)) |
| 3258 | { |
| 3259 | /* |
| 3260 | This can happen if thread is killed either by a shutdown |
| 3261 | or if another thread is removing the current table definition |
| 3262 | from the table cache. |
| 3263 | */ |
| 3264 | my_error(ER_DELAYED_CANT_CHANGE_LOCK, MYF(ME_FATALERROR | ME_NOREFRESH), |
| 3265 | table->s->table_name.str); |
| 3266 | goto err; |
| 3267 | } |
| 3268 | |
| 3269 | THD_STAGE_INFO(&thd, stage_insert); |
| 3270 | max_rows= delayed_insert_limit; |
| 3271 | if (thd.killed || table->s->tdc->flushed) |
| 3272 | { |
| 3273 | thd.set_killed(KILL_SYSTEM_THREAD); |
| 3274 | max_rows= ULONG_MAX; // Do as much as possible |
| 3275 | } |
| 3276 | |
| 3277 | /* |
| 3278 | We can't use row caching when using the binary log because if |
| 3279 | we get a crash, then binary log will contain rows that are not yet |
| 3280 | written to disk, which will cause problems in replication. |
| 3281 | */ |
| 3282 | if (!using_bin_log) |
| 3283 | table->file->extra(HA_EXTRA_WRITE_CACHE); |
| 3284 | mysql_mutex_lock(&mutex); |
| 3285 | |
| 3286 | while ((row=rows.get())) |
| 3287 | { |
| 3288 | int tmp_error; |
| 3289 | stacked_inserts--; |
| 3290 | mysql_mutex_unlock(&mutex); |
| 3291 | memcpy(table->record[0],row->record,table->s->reclength); |
| 3292 | if (table->s->blob_fields) |
| 3293 | set_delayed_insert_blobs(table); |
| 3294 | |
| 3295 | thd.start_time=row->start_time; |
| 3296 | thd.start_time_sec_part=row->start_time_sec_part; |
| 3297 | thd.query_start_sec_part_used=row->query_start_sec_part_used; |
| 3298 | /* |
| 3299 | To get the exact auto_inc interval to store in the binlog we must not |
| 3300 | use values from the previous interval (of the previous rows). |
| 3301 | */ |
| 3302 | bool log_query= (row->log_query && row->query.str != NULL); |
| 3303 | DBUG_PRINT("delayed" , ("query: '%s' length: %lu" , row->query.str ? |
| 3304 | row->query.str : "[NULL]" , |
| 3305 | (ulong) row->query.length)); |
| 3306 | if (log_query) |
| 3307 | { |
| 3308 | /* |
| 3309 | Guaranteed that the INSERT DELAYED STMT will not be here |
| 3310 | in SBR when mysql binlog is enabled. |
| 3311 | */ |
| 3312 | DBUG_ASSERT(!(mysql_bin_log.is_open() && |
| 3313 | !thd.is_current_stmt_binlog_format_row())); |
| 3314 | |
| 3315 | /* |
| 3316 | This is the first value of an INSERT statement. |
| 3317 | It is the right place to clear a forced insert_id. |
| 3318 | This is usually done after the last value of an INSERT statement, |
| 3319 | but we won't know this in the insert delayed thread. But before |
| 3320 | the first value is sufficiently equivalent to after the last |
| 3321 | value of the previous statement. |
| 3322 | */ |
| 3323 | table->file->ha_release_auto_increment(); |
| 3324 | thd.auto_inc_intervals_in_cur_stmt_for_binlog.empty(); |
| 3325 | } |
| 3326 | thd.first_successful_insert_id_in_prev_stmt= |
| 3327 | row->first_successful_insert_id_in_prev_stmt; |
| 3328 | thd.stmt_depends_on_first_successful_insert_id_in_prev_stmt= |
| 3329 | row->stmt_depends_on_first_successful_insert_id_in_prev_stmt; |
| 3330 | table->auto_increment_field_not_null= row->auto_increment_field_not_null; |
| 3331 | |
| 3332 | /* Copy the session variables. */ |
| 3333 | thd.variables.auto_increment_increment= row->auto_increment_increment; |
| 3334 | thd.variables.auto_increment_offset= row->auto_increment_offset; |
| 3335 | thd.variables.sql_mode= row->sql_mode; |
| 3336 | |
| 3337 | /* Copy a forced insert_id, if any. */ |
| 3338 | if (row->forced_insert_id) |
| 3339 | { |
| 3340 | DBUG_PRINT("delayed" , ("received auto_inc: %lu" , |
| 3341 | (ulong) row->forced_insert_id)); |
| 3342 | thd.force_one_auto_inc_interval(row->forced_insert_id); |
| 3343 | } |
| 3344 | |
| 3345 | info.ignore= row->ignore; |
| 3346 | info.handle_duplicates= row->dup; |
| 3347 | if (info.ignore || |
| 3348 | info.handle_duplicates != DUP_ERROR) |
| 3349 | { |
| 3350 | table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); |
| 3351 | using_ignore=1; |
| 3352 | } |
| 3353 | if (info.handle_duplicates == DUP_REPLACE && |
| 3354 | (!table->triggers || |
| 3355 | !table->triggers->has_delete_triggers())) |
| 3356 | { |
| 3357 | table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE); |
| 3358 | using_opt_replace= 1; |
| 3359 | } |
| 3360 | if (info.handle_duplicates == DUP_UPDATE) |
| 3361 | table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE); |
| 3362 | thd.clear_error(); // reset error for binlog |
| 3363 | |
| 3364 | tmp_error= 0; |
| 3365 | if (unlikely(table->vfield)) |
| 3366 | { |
| 3367 | /* |
| 3368 | Virtual fields where not calculated by caller as the temporary |
| 3369 | TABLE object used had vcol_set empty. Better to calculate them |
| 3370 | here to make the caller faster. |
| 3371 | */ |
| 3372 | tmp_error= table->update_virtual_fields(table->file, |
| 3373 | VCOL_UPDATE_FOR_WRITE); |
| 3374 | } |
| 3375 | |
| 3376 | if (unlikely(tmp_error) || unlikely(write_record(&thd, table, &info))) |
| 3377 | { |
| 3378 | info.error_count++; // Ignore errors |
| 3379 | thread_safe_increment(delayed_insert_errors,&LOCK_delayed_status); |
| 3380 | row->log_query = 0; |
| 3381 | } |
| 3382 | |
| 3383 | if (using_ignore) |
| 3384 | { |
| 3385 | using_ignore=0; |
| 3386 | table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); |
| 3387 | } |
| 3388 | if (using_opt_replace) |
| 3389 | { |
| 3390 | using_opt_replace= 0; |
| 3391 | table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); |
| 3392 | } |
| 3393 | |
| 3394 | if (table->s->blob_fields) |
| 3395 | free_delayed_insert_blobs(table); |
| 3396 | thread_safe_decrement(delayed_rows_in_use,&LOCK_delayed_status); |
| 3397 | thread_safe_increment(delayed_insert_writes,&LOCK_delayed_status); |
| 3398 | mysql_mutex_lock(&mutex); |
| 3399 | |
| 3400 | /* |
| 3401 | Reset the table->auto_increment_field_not_null as it is valid for |
| 3402 | only one row. |
| 3403 | */ |
| 3404 | table->auto_increment_field_not_null= FALSE; |
| 3405 | |
| 3406 | delete row; |
| 3407 | /* |
| 3408 | Let READ clients do something once in a while |
| 3409 | We should however not break in the middle of a multi-line insert |
| 3410 | if we have binary logging enabled as we don't want other commands |
| 3411 | on this table until all entries has been processed |
| 3412 | */ |
| 3413 | if (group_count++ >= max_rows && (row= rows.head()) && |
| 3414 | (!(row->log_query & using_bin_log))) |
| 3415 | { |
| 3416 | group_count=0; |
| 3417 | if (stacked_inserts || tables_in_use) // Let these wait a while |
| 3418 | { |
| 3419 | if (tables_in_use) |
| 3420 | mysql_cond_broadcast(&cond_client); // If waiting clients |
| 3421 | THD_STAGE_INFO(&thd, stage_reschedule); |
| 3422 | mysql_mutex_unlock(&mutex); |
| 3423 | if (unlikely((error=table->file->extra(HA_EXTRA_NO_CACHE)))) |
| 3424 | { |
| 3425 | /* This should never happen */ |
| 3426 | table->file->print_error(error,MYF(0)); |
| 3427 | sql_print_error("%s" , thd.get_stmt_da()->message()); |
| 3428 | DBUG_PRINT("error" , ("HA_EXTRA_NO_CACHE failed in loop" )); |
| 3429 | goto err; |
| 3430 | } |
| 3431 | query_cache_invalidate3(&thd, table, 1); |
| 3432 | if (thr_reschedule_write_lock(*thd.lock->locks, |
| 3433 | thd.variables.lock_wait_timeout)) |
| 3434 | { |
| 3435 | /* This is not known to happen. */ |
| 3436 | my_error(ER_DELAYED_CANT_CHANGE_LOCK, |
| 3437 | MYF(ME_FATALERROR | ME_NOREFRESH), |
| 3438 | table->s->table_name.str); |
| 3439 | goto err; |
| 3440 | } |
| 3441 | if (!using_bin_log) |
| 3442 | table->file->extra(HA_EXTRA_WRITE_CACHE); |
| 3443 | mysql_mutex_lock(&mutex); |
| 3444 | THD_STAGE_INFO(&thd, stage_insert); |
| 3445 | } |
| 3446 | if (tables_in_use) |
| 3447 | mysql_cond_broadcast(&cond_client); // If waiting clients |
| 3448 | } |
| 3449 | } |
| 3450 | |
| 3451 | if (WSREP((&thd))) |
| 3452 | thd_proc_info(&thd, "Insert done" ); |
| 3453 | else |
| 3454 | thd_proc_info(&thd, 0); |
| 3455 | mysql_mutex_unlock(&mutex); |
| 3456 | |
| 3457 | /* |
| 3458 | We need to flush the pending event when using row-based |
| 3459 | replication since the flushing normally done in binlog_query() is |
| 3460 | not done last in the statement: for delayed inserts, the insert |
| 3461 | statement is logged *before* all rows are inserted. |
| 3462 | |
| 3463 | We can flush the pending event without checking the thd->lock |
| 3464 | since the delayed insert *thread* is not inside a stored function |
| 3465 | or trigger. |
| 3466 | |
| 3467 | TODO: Move the logging to last in the sequence of rows. |
| 3468 | */ |
| 3469 | has_trans= thd.lex->sql_command == SQLCOM_CREATE_TABLE || |
| 3470 | table->file->has_transactions(); |
| 3471 | if (thd.is_current_stmt_binlog_format_row() && |
| 3472 | thd.binlog_flush_pending_rows_event(TRUE, has_trans)) |
| 3473 | goto err; |
| 3474 | |
| 3475 | if (unlikely((error=table->file->extra(HA_EXTRA_NO_CACHE)))) |
| 3476 | { // This shouldn't happen |
| 3477 | table->file->print_error(error,MYF(0)); |
| 3478 | sql_print_error("%s" , thd.get_stmt_da()->message()); |
| 3479 | DBUG_PRINT("error" , ("HA_EXTRA_NO_CACHE failed after loop" )); |
| 3480 | goto err; |
| 3481 | } |
| 3482 | query_cache_invalidate3(&thd, table, 1); |
| 3483 | mysql_mutex_lock(&mutex); |
| 3484 | DBUG_RETURN(0); |
| 3485 | |
| 3486 | err: |
| 3487 | #ifndef DBUG_OFF |
| 3488 | max_rows= 0; // For DBUG output |
| 3489 | #endif |
| 3490 | /* Remove all not used rows */ |
| 3491 | mysql_mutex_lock(&mutex); |
| 3492 | while ((row=rows.get())) |
| 3493 | { |
| 3494 | if (table->s->blob_fields) |
| 3495 | { |
| 3496 | memcpy(table->record[0],row->record,table->s->reclength); |
| 3497 | set_delayed_insert_blobs(table); |
| 3498 | free_delayed_insert_blobs(table); |
| 3499 | } |
| 3500 | delete row; |
| 3501 | thread_safe_increment(delayed_insert_errors,&LOCK_delayed_status); |
| 3502 | stacked_inserts--; |
| 3503 | #ifndef DBUG_OFF |
| 3504 | max_rows++; |
| 3505 | #endif |
| 3506 | } |
| 3507 | DBUG_PRINT("error" , ("dropped %lu rows after an error" , max_rows)); |
| 3508 | thread_safe_increment(delayed_insert_errors, &LOCK_delayed_status); |
| 3509 | DBUG_RETURN(1); |
| 3510 | } |
| 3511 | #endif /* EMBEDDED_LIBRARY */ |
| 3512 | |
| 3513 | /*************************************************************************** |
| 3514 | Store records in INSERT ... SELECT * |
| 3515 | ***************************************************************************/ |
| 3516 | |
| 3517 | |
| 3518 | /* |
| 3519 | make insert specific preparation and checks after opening tables |
| 3520 | |
| 3521 | SYNOPSIS |
| 3522 | mysql_insert_select_prepare() |
| 3523 | thd thread handler |
| 3524 | |
| 3525 | RETURN |
| 3526 | FALSE OK |
| 3527 | TRUE Error |
| 3528 | */ |
| 3529 | |
| 3530 | bool mysql_insert_select_prepare(THD *thd) |
| 3531 | { |
| 3532 | LEX *lex= thd->lex; |
| 3533 | SELECT_LEX *select_lex= &lex->select_lex; |
| 3534 | DBUG_ENTER("mysql_insert_select_prepare" ); |
| 3535 | |
| 3536 | |
| 3537 | /* |
| 3538 | SELECT_LEX do not belong to INSERT statement, so we can't add WHERE |
| 3539 | clause if table is VIEW |
| 3540 | */ |
| 3541 | |
| 3542 | if (mysql_prepare_insert(thd, lex->query_tables, |
| 3543 | lex->query_tables->table, lex->field_list, 0, |
| 3544 | lex->update_list, lex->value_list, lex->duplicates, |
| 3545 | &select_lex->where, TRUE)) |
| 3546 | DBUG_RETURN(TRUE); |
| 3547 | |
| 3548 | DBUG_ASSERT(select_lex->leaf_tables.elements != 0); |
| 3549 | List_iterator<TABLE_LIST> ti(select_lex->leaf_tables); |
| 3550 | TABLE_LIST *table; |
| 3551 | uint insert_tables; |
| 3552 | |
| 3553 | if (select_lex->first_cond_optimization) |
| 3554 | { |
| 3555 | /* Back up leaf_tables list. */ |
| 3556 | Query_arena *arena= thd->stmt_arena, backup; |
| 3557 | arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test |
| 3558 | |
| 3559 | insert_tables= select_lex->insert_tables; |
| 3560 | while ((table= ti++) && insert_tables--) |
| 3561 | { |
| 3562 | select_lex->leaf_tables_exec.push_back(table); |
| 3563 | table->tablenr_exec= table->table->tablenr; |
| 3564 | table->map_exec= table->table->map; |
| 3565 | table->maybe_null_exec= table->table->maybe_null; |
| 3566 | } |
| 3567 | if (arena) |
| 3568 | thd->restore_active_arena(arena, &backup); |
| 3569 | } |
| 3570 | ti.rewind(); |
| 3571 | /* |
| 3572 | exclude first table from leaf tables list, because it belong to |
| 3573 | INSERT |
| 3574 | */ |
| 3575 | /* skip all leaf tables belonged to view where we are insert */ |
| 3576 | insert_tables= select_lex->insert_tables; |
| 3577 | while ((table= ti++) && insert_tables--) |
| 3578 | ti.remove(); |
| 3579 | |
| 3580 | DBUG_RETURN(FALSE); |
| 3581 | } |
| 3582 | |
| 3583 | |
| 3584 | select_insert::select_insert(THD *thd_arg, TABLE_LIST *table_list_par, |
| 3585 | TABLE *table_par, |
| 3586 | List<Item> *fields_par, |
| 3587 | List<Item> *update_fields, |
| 3588 | List<Item> *update_values, |
| 3589 | enum_duplicates duplic, |
| 3590 | bool ignore_check_option_errors): |
| 3591 | select_result_interceptor(thd_arg), |
| 3592 | table_list(table_list_par), table(table_par), fields(fields_par), |
| 3593 | autoinc_value_of_last_inserted_row(0), |
| 3594 | insert_into_view(table_list_par && table_list_par->view != 0) |
| 3595 | { |
| 3596 | bzero((char*) &info,sizeof(info)); |
| 3597 | info.handle_duplicates= duplic; |
| 3598 | info.ignore= ignore_check_option_errors; |
| 3599 | info.update_fields= update_fields; |
| 3600 | info.update_values= update_values; |
| 3601 | info.view= (table_list_par->view ? table_list_par : 0); |
| 3602 | info.table_list= table_list_par; |
| 3603 | } |
| 3604 | |
| 3605 | |
| 3606 | int |
| 3607 | select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u) |
| 3608 | { |
| 3609 | LEX *lex= thd->lex; |
| 3610 | int res; |
| 3611 | table_map map= 0; |
| 3612 | SELECT_LEX *lex_current_select_save= lex->current_select; |
| 3613 | DBUG_ENTER("select_insert::prepare" ); |
| 3614 | |
| 3615 | unit= u; |
| 3616 | |
| 3617 | /* |
| 3618 | Since table in which we are going to insert is added to the first |
| 3619 | select, LEX::current_select should point to the first select while |
| 3620 | we are fixing fields from insert list. |
| 3621 | */ |
| 3622 | lex->current_select= &lex->select_lex; |
| 3623 | |
| 3624 | res= (setup_fields(thd, Ref_ptr_array(), |
| 3625 | values, MARK_COLUMNS_READ, 0, NULL, 0) || |
| 3626 | check_insert_fields(thd, table_list, *fields, values, |
| 3627 | !insert_into_view, 1, &map)); |
| 3628 | |
| 3629 | if (!res && fields->elements) |
| 3630 | { |
| 3631 | bool saved_abort_on_warning= thd->abort_on_warning; |
| 3632 | thd->abort_on_warning= !info.ignore && thd->is_strict_mode(); |
| 3633 | res= check_that_all_fields_are_given_values(thd, table_list->table, table_list); |
| 3634 | thd->abort_on_warning= saved_abort_on_warning; |
| 3635 | } |
| 3636 | |
| 3637 | if (info.handle_duplicates == DUP_UPDATE && !res) |
| 3638 | { |
| 3639 | Name_resolution_context *context= &lex->select_lex.context; |
| 3640 | Name_resolution_context_state ctx_state; |
| 3641 | |
| 3642 | /* Save the state of the current name resolution context. */ |
| 3643 | ctx_state.save_state(context, table_list); |
| 3644 | |
| 3645 | /* Perform name resolution only in the first table - 'table_list'. */ |
| 3646 | table_list->next_local= 0; |
| 3647 | context->resolve_in_table_list_only(table_list); |
| 3648 | |
| 3649 | lex->select_lex.no_wrap_view_item= TRUE; |
| 3650 | res= res || |
| 3651 | check_update_fields(thd, context->table_list, |
| 3652 | *info.update_fields, *info.update_values, |
| 3653 | /* |
| 3654 | In INSERT SELECT ON DUPLICATE KEY UPDATE col=x |
| 3655 | 'x' can legally refer to a non-inserted table. |
| 3656 | 'x' is not even resolved yet. |
| 3657 | */ |
| 3658 | true, |
| 3659 | &map); |
| 3660 | lex->select_lex.no_wrap_view_item= FALSE; |
| 3661 | /* |
| 3662 | When we are not using GROUP BY and there are no ungrouped aggregate functions |
| 3663 | we can refer to other tables in the ON DUPLICATE KEY part. |
| 3664 | We use next_name_resolution_table descructively, so check it first (views?) |
| 3665 | */ |
| 3666 | DBUG_ASSERT (!table_list->next_name_resolution_table); |
| 3667 | if (lex->select_lex.group_list.elements == 0 && |
| 3668 | !lex->select_lex.with_sum_func) |
| 3669 | /* |
| 3670 | We must make a single context out of the two separate name resolution contexts : |
| 3671 | the INSERT table and the tables in the SELECT part of INSERT ... SELECT. |
| 3672 | To do that we must concatenate the two lists |
| 3673 | */ |
| 3674 | table_list->next_name_resolution_table= |
| 3675 | ctx_state.get_first_name_resolution_table(); |
| 3676 | |
| 3677 | res= res || setup_fields(thd, Ref_ptr_array(), *info.update_values, |
| 3678 | MARK_COLUMNS_READ, 0, NULL, 0); |
| 3679 | if (!res) |
| 3680 | { |
| 3681 | /* |
| 3682 | Traverse the update values list and substitute fields from the |
| 3683 | select for references (Item_ref objects) to them. This is done in |
| 3684 | order to get correct values from those fields when the select |
| 3685 | employs a temporary table. |
| 3686 | */ |
| 3687 | List_iterator<Item> li(*info.update_values); |
| 3688 | Item *item; |
| 3689 | |
| 3690 | while ((item= li++)) |
| 3691 | { |
| 3692 | item->transform(thd, &Item::update_value_transformer, |
| 3693 | (uchar*)lex->current_select); |
| 3694 | } |
| 3695 | } |
| 3696 | |
| 3697 | /* Restore the current context. */ |
| 3698 | ctx_state.restore_state(context, table_list); |
| 3699 | } |
| 3700 | |
| 3701 | lex->current_select= lex_current_select_save; |
| 3702 | if (res) |
| 3703 | DBUG_RETURN(1); |
| 3704 | /* |
| 3705 | if it is INSERT into join view then check_insert_fields already found |
| 3706 | real table for insert |
| 3707 | */ |
| 3708 | table= table_list->table; |
| 3709 | |
| 3710 | /* |
| 3711 | Is table which we are changing used somewhere in other parts of |
| 3712 | query |
| 3713 | */ |
| 3714 | if (unique_table(thd, table_list, table_list->next_global, 0)) |
| 3715 | { |
| 3716 | /* Using same table for INSERT and SELECT */ |
| 3717 | lex->current_select->options|= OPTION_BUFFER_RESULT; |
| 3718 | lex->current_select->join->select_options|= OPTION_BUFFER_RESULT; |
| 3719 | } |
| 3720 | else if (!(lex->current_select->options & OPTION_BUFFER_RESULT) && |
| 3721 | thd->locked_tables_mode <= LTM_LOCK_TABLES) |
| 3722 | { |
| 3723 | /* |
| 3724 | We must not yet prepare the result table if it is the same as one of the |
| 3725 | source tables (INSERT SELECT). The preparation may disable |
| 3726 | indexes on the result table, which may be used during the select, if it |
| 3727 | is the same table (Bug #6034). Do the preparation after the select phase |
| 3728 | in select_insert::prepare2(). |
| 3729 | We won't start bulk inserts at all if this statement uses functions or |
| 3730 | should invoke triggers since they may access to the same table too. |
| 3731 | */ |
| 3732 | table->file->ha_start_bulk_insert((ha_rows) 0); |
| 3733 | } |
| 3734 | restore_record(table,s->default_values); // Get empty record |
| 3735 | table->reset_default_fields(); |
| 3736 | table->next_number_field=table->found_next_number_field; |
| 3737 | |
| 3738 | #ifdef HAVE_REPLICATION |
| 3739 | if (thd->rgi_slave && |
| 3740 | (info.handle_duplicates == DUP_UPDATE) && |
| 3741 | (table->next_number_field != NULL) && |
| 3742 | rpl_master_has_bug(thd->rgi_slave->rli, 24432, TRUE, NULL, NULL)) |
| 3743 | DBUG_RETURN(1); |
| 3744 | #endif |
| 3745 | |
| 3746 | thd->cuted_fields=0; |
| 3747 | if (info.ignore || info.handle_duplicates != DUP_ERROR) |
| 3748 | table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); |
| 3749 | if (info.handle_duplicates == DUP_REPLACE && |
| 3750 | (!table->triggers || !table->triggers->has_delete_triggers())) |
| 3751 | table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE); |
| 3752 | if (info.handle_duplicates == DUP_UPDATE) |
| 3753 | table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE); |
| 3754 | thd->abort_on_warning= !info.ignore && thd->is_strict_mode(); |
| 3755 | res= (table_list->prepare_where(thd, 0, TRUE) || |
| 3756 | table_list->prepare_check_option(thd)); |
| 3757 | |
| 3758 | if (!res) |
| 3759 | { |
| 3760 | table->prepare_triggers_for_insert_stmt_or_event(); |
| 3761 | table->mark_columns_needed_for_insert(); |
| 3762 | } |
| 3763 | |
| 3764 | DBUG_RETURN(res); |
| 3765 | } |
| 3766 | |
| 3767 | |
| 3768 | /* |
| 3769 | Finish the preparation of the result table. |
| 3770 | |
| 3771 | SYNOPSIS |
| 3772 | select_insert::prepare2() |
| 3773 | void |
| 3774 | |
| 3775 | DESCRIPTION |
| 3776 | If the result table is the same as one of the source tables (INSERT SELECT), |
| 3777 | the result table is not finally prepared at the join prepair phase. |
| 3778 | Do the final preparation now. |
| 3779 | |
| 3780 | RETURN |
| 3781 | 0 OK |
| 3782 | */ |
| 3783 | |
| 3784 | int select_insert::prepare2(JOIN *) |
| 3785 | { |
| 3786 | DBUG_ENTER("select_insert::prepare2" ); |
| 3787 | if (thd->lex->current_select->options & OPTION_BUFFER_RESULT && |
| 3788 | thd->locked_tables_mode <= LTM_LOCK_TABLES && |
| 3789 | !thd->lex->describe) |
| 3790 | table->file->ha_start_bulk_insert((ha_rows) 0); |
| 3791 | if (table->validate_default_values_of_unset_fields(thd)) |
| 3792 | DBUG_RETURN(1); |
| 3793 | DBUG_RETURN(0); |
| 3794 | } |
| 3795 | |
| 3796 | |
| 3797 | void select_insert::cleanup() |
| 3798 | { |
| 3799 | /* select_insert/select_create are never re-used in prepared statement */ |
| 3800 | DBUG_ASSERT(0); |
| 3801 | } |
| 3802 | |
| 3803 | select_insert::~select_insert() |
| 3804 | { |
| 3805 | DBUG_ENTER("~select_insert" ); |
| 3806 | if (table && table->is_created()) |
| 3807 | { |
| 3808 | table->next_number_field=0; |
| 3809 | table->auto_increment_field_not_null= FALSE; |
| 3810 | table->file->ha_reset(); |
| 3811 | } |
| 3812 | thd->count_cuted_fields= CHECK_FIELD_IGNORE; |
| 3813 | thd->abort_on_warning= 0; |
| 3814 | DBUG_VOID_RETURN; |
| 3815 | } |
| 3816 | |
| 3817 | |
| 3818 | int select_insert::send_data(List<Item> &values) |
| 3819 | { |
| 3820 | DBUG_ENTER("select_insert::send_data" ); |
| 3821 | bool error=0; |
| 3822 | |
| 3823 | if (unit->offset_limit_cnt) |
| 3824 | { // using limit offset,count |
| 3825 | unit->offset_limit_cnt--; |
| 3826 | DBUG_RETURN(0); |
| 3827 | } |
| 3828 | if (unlikely(thd->killed == ABORT_QUERY)) |
| 3829 | DBUG_RETURN(0); |
| 3830 | |
| 3831 | thd->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields |
| 3832 | store_values(values); |
| 3833 | if (table->default_field && |
| 3834 | unlikely(table->update_default_fields(0, info.ignore))) |
| 3835 | DBUG_RETURN(1); |
| 3836 | thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL; |
| 3837 | if (unlikely(thd->is_error())) |
| 3838 | { |
| 3839 | table->auto_increment_field_not_null= FALSE; |
| 3840 | DBUG_RETURN(1); |
| 3841 | } |
| 3842 | |
| 3843 | table->vers_write= table->versioned(); |
| 3844 | if (table_list) // Not CREATE ... SELECT |
| 3845 | { |
| 3846 | switch (table_list->view_check_option(thd, info.ignore)) { |
| 3847 | case VIEW_CHECK_SKIP: |
| 3848 | DBUG_RETURN(0); |
| 3849 | case VIEW_CHECK_ERROR: |
| 3850 | DBUG_RETURN(1); |
| 3851 | } |
| 3852 | } |
| 3853 | |
| 3854 | error= write_record(thd, table, &info); |
| 3855 | table->vers_write= table->versioned(); |
| 3856 | table->auto_increment_field_not_null= FALSE; |
| 3857 | |
| 3858 | if (likely(!error)) |
| 3859 | { |
| 3860 | if (table->triggers || info.handle_duplicates == DUP_UPDATE) |
| 3861 | { |
| 3862 | /* |
| 3863 | Restore fields of the record since it is possible that they were |
| 3864 | changed by ON DUPLICATE KEY UPDATE clause. |
| 3865 | |
| 3866 | If triggers exist then whey can modify some fields which were not |
| 3867 | originally touched by INSERT ... SELECT, so we have to restore |
| 3868 | their original values for the next row. |
| 3869 | */ |
| 3870 | restore_record(table, s->default_values); |
| 3871 | } |
| 3872 | if (table->next_number_field) |
| 3873 | { |
| 3874 | /* |
| 3875 | If no value has been autogenerated so far, we need to remember the |
| 3876 | value we just saw, we may need to send it to client in the end. |
| 3877 | */ |
| 3878 | if (thd->first_successful_insert_id_in_cur_stmt == 0) // optimization |
| 3879 | autoinc_value_of_last_inserted_row= |
| 3880 | table->next_number_field->val_int(); |
| 3881 | /* |
| 3882 | Clear auto-increment field for the next record, if triggers are used |
| 3883 | we will clear it twice, but this should be cheap. |
| 3884 | */ |
| 3885 | table->next_number_field->reset(); |
| 3886 | } |
| 3887 | } |
| 3888 | DBUG_RETURN(error); |
| 3889 | } |
| 3890 | |
| 3891 | |
| 3892 | void select_insert::store_values(List<Item> &values) |
| 3893 | { |
| 3894 | DBUG_ENTER("select_insert::store_values" ); |
| 3895 | |
| 3896 | if (fields->elements) |
| 3897 | fill_record_n_invoke_before_triggers(thd, table, *fields, values, 1, |
| 3898 | TRG_EVENT_INSERT); |
| 3899 | else |
| 3900 | fill_record_n_invoke_before_triggers(thd, table, table->field_to_fill(), |
| 3901 | values, 1, TRG_EVENT_INSERT); |
| 3902 | |
| 3903 | DBUG_VOID_RETURN; |
| 3904 | } |
| 3905 | |
| 3906 | bool select_insert::prepare_eof() |
| 3907 | { |
| 3908 | int error; |
| 3909 | bool const trans_table= table->file->has_transactions(); |
| 3910 | bool changed; |
| 3911 | killed_state killed_status= thd->killed; |
| 3912 | |
| 3913 | DBUG_ENTER("select_insert::prepare_eof" ); |
| 3914 | DBUG_PRINT("enter" , ("trans_table=%d, table_type='%s'" , |
| 3915 | trans_table, table->file->table_type())); |
| 3916 | |
| 3917 | error= (IF_WSREP((thd->wsrep_conflict_state == MUST_ABORT || |
| 3918 | thd->wsrep_conflict_state == CERT_FAILURE) ? -1 :, ) |
| 3919 | (thd->locked_tables_mode <= LTM_LOCK_TABLES ? |
| 3920 | table->file->ha_end_bulk_insert() : 0)); |
| 3921 | |
| 3922 | if (likely(!error) && unlikely(thd->is_error())) |
| 3923 | error= thd->get_stmt_da()->sql_errno(); |
| 3924 | |
| 3925 | table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); |
| 3926 | table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); |
| 3927 | |
| 3928 | if (likely((changed= (info.copied || info.deleted || info.updated)))) |
| 3929 | { |
| 3930 | /* |
| 3931 | We must invalidate the table in the query cache before binlog writing |
| 3932 | and ha_autocommit_or_rollback. |
| 3933 | */ |
| 3934 | query_cache_invalidate3(thd, table, 1); |
| 3935 | } |
| 3936 | |
| 3937 | if (thd->transaction.stmt.modified_non_trans_table) |
| 3938 | thd->transaction.all.modified_non_trans_table= TRUE; |
| 3939 | thd->transaction.all.m_unsafe_rollback_flags|= |
| 3940 | (thd->transaction.stmt.m_unsafe_rollback_flags & THD_TRANS::DID_WAIT); |
| 3941 | |
| 3942 | DBUG_ASSERT(trans_table || !changed || |
| 3943 | thd->transaction.stmt.modified_non_trans_table); |
| 3944 | |
| 3945 | /* |
| 3946 | Write to binlog before commiting transaction. No statement will |
| 3947 | be written by the binlog_query() below in RBR mode. All the |
| 3948 | events are in the transaction cache and will be written when |
| 3949 | ha_autocommit_or_rollback() is issued below. |
| 3950 | */ |
| 3951 | if ((WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open()) && |
| 3952 | (likely(!error) || thd->transaction.stmt.modified_non_trans_table)) |
| 3953 | { |
| 3954 | int errcode= 0; |
| 3955 | if (likely(!error)) |
| 3956 | thd->clear_error(); |
| 3957 | else |
| 3958 | errcode= query_error_code(thd, killed_status == NOT_KILLED); |
| 3959 | if (thd->binlog_query(THD::ROW_QUERY_TYPE, |
| 3960 | thd->query(), thd->query_length(), |
| 3961 | trans_table, FALSE, FALSE, errcode)) |
| 3962 | { |
| 3963 | table->file->ha_release_auto_increment(); |
| 3964 | DBUG_RETURN(true); |
| 3965 | } |
| 3966 | } |
| 3967 | table->file->ha_release_auto_increment(); |
| 3968 | |
| 3969 | if (unlikely(error)) |
| 3970 | { |
| 3971 | table->file->print_error(error,MYF(0)); |
| 3972 | DBUG_RETURN(true); |
| 3973 | } |
| 3974 | |
| 3975 | DBUG_RETURN(false); |
| 3976 | } |
| 3977 | |
| 3978 | bool select_insert::send_ok_packet() { |
| 3979 | char message[160]; /* status message */ |
| 3980 | ulonglong row_count; /* rows affected */ |
| 3981 | ulonglong id; /* last insert-id */ |
| 3982 | |
| 3983 | DBUG_ENTER("select_insert::send_ok_packet" ); |
| 3984 | |
| 3985 | if (info.ignore) |
| 3986 | my_snprintf(message, sizeof(message), ER(ER_INSERT_INFO), |
| 3987 | (ulong) info.records, (ulong) (info.records - info.copied), |
| 3988 | (long) thd->get_stmt_da()->current_statement_warn_count()); |
| 3989 | else |
| 3990 | my_snprintf(message, sizeof(message), ER(ER_INSERT_INFO), |
| 3991 | (ulong) info.records, (ulong) (info.deleted + info.updated), |
| 3992 | (long) thd->get_stmt_da()->current_statement_warn_count()); |
| 3993 | |
| 3994 | row_count= info.copied + info.deleted + |
| 3995 | ((thd->client_capabilities & CLIENT_FOUND_ROWS) ? |
| 3996 | info.touched : info.updated); |
| 3997 | |
| 3998 | id= (thd->first_successful_insert_id_in_cur_stmt > 0) ? |
| 3999 | thd->first_successful_insert_id_in_cur_stmt : |
| 4000 | (thd->arg_of_last_insert_id_function ? |
| 4001 | thd->first_successful_insert_id_in_prev_stmt : |
| 4002 | (info.copied ? autoinc_value_of_last_inserted_row : 0)); |
| 4003 | |
| 4004 | ::my_ok(thd, row_count, id, message); |
| 4005 | |
| 4006 | DBUG_RETURN(false); |
| 4007 | } |
| 4008 | |
| 4009 | bool select_insert::send_eof() |
| 4010 | { |
| 4011 | bool res; |
| 4012 | DBUG_ENTER("select_insert::send_eof" ); |
| 4013 | res= (prepare_eof() || (!suppress_my_ok && send_ok_packet())); |
| 4014 | DBUG_RETURN(res); |
| 4015 | } |
| 4016 | |
| 4017 | void select_insert::abort_result_set() { |
| 4018 | |
| 4019 | DBUG_ENTER("select_insert::abort_result_set" ); |
| 4020 | /* |
| 4021 | If the creation of the table failed (due to a syntax error, for |
| 4022 | example), no table will have been opened and therefore 'table' |
| 4023 | will be NULL. In that case, we still need to execute the rollback |
| 4024 | and the end of the function. |
| 4025 | */ |
| 4026 | if (table) |
| 4027 | { |
| 4028 | bool changed, transactional_table; |
| 4029 | /* |
| 4030 | If we are not in prelocked mode, we end the bulk insert started |
| 4031 | before. |
| 4032 | */ |
| 4033 | if (thd->locked_tables_mode <= LTM_LOCK_TABLES) |
| 4034 | table->file->ha_end_bulk_insert(); |
| 4035 | |
| 4036 | /* |
| 4037 | If at least one row has been inserted/modified and will stay in |
| 4038 | the table (the table doesn't have transactions) we must write to |
| 4039 | the binlog (and the error code will make the slave stop). |
| 4040 | |
| 4041 | For many errors (example: we got a duplicate key error while |
| 4042 | inserting into a MyISAM table), no row will be added to the table, |
| 4043 | so passing the error to the slave will not help since there will |
| 4044 | be an error code mismatch (the inserts will succeed on the slave |
| 4045 | with no error). |
| 4046 | |
| 4047 | If table creation failed, the number of rows modified will also be |
| 4048 | zero, so no check for that is made. |
| 4049 | */ |
| 4050 | changed= (info.copied || info.deleted || info.updated); |
| 4051 | transactional_table= table->file->has_transactions(); |
| 4052 | if (thd->transaction.stmt.modified_non_trans_table || |
| 4053 | thd->log_current_statement) |
| 4054 | { |
| 4055 | if (!can_rollback_data()) |
| 4056 | thd->transaction.all.modified_non_trans_table= TRUE; |
| 4057 | |
| 4058 | if(WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open()) |
| 4059 | { |
| 4060 | int errcode= query_error_code(thd, thd->killed == NOT_KILLED); |
| 4061 | /* error of writing binary log is ignored */ |
| 4062 | (void) thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(), |
| 4063 | thd->query_length(), |
| 4064 | transactional_table, FALSE, FALSE, errcode); |
| 4065 | } |
| 4066 | if (changed) |
| 4067 | query_cache_invalidate3(thd, table, 1); |
| 4068 | } |
| 4069 | DBUG_ASSERT(transactional_table || !changed || |
| 4070 | thd->transaction.stmt.modified_non_trans_table); |
| 4071 | table->file->ha_release_auto_increment(); |
| 4072 | } |
| 4073 | |
| 4074 | DBUG_VOID_RETURN; |
| 4075 | } |
| 4076 | |
| 4077 | |
| 4078 | /*************************************************************************** |
| 4079 | CREATE TABLE (SELECT) ... |
| 4080 | ***************************************************************************/ |
| 4081 | |
| 4082 | Field *Item::create_field_for_create_select(TABLE *table) |
| 4083 | { |
| 4084 | Field *def_field, *tmp_field; |
| 4085 | return ::create_tmp_field(table->in_use, table, this, type(), |
| 4086 | (Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0); |
| 4087 | } |
| 4088 | |
| 4089 | |
| 4090 | /** |
| 4091 | Create table from lists of fields and items (or just return TABLE |
| 4092 | object for pre-opened existing table). |
| 4093 | |
| 4094 | @param thd [in] Thread object |
| 4095 | @param create_info [in] Create information (like MAX_ROWS, ENGINE or |
| 4096 | temporary table flag) |
| 4097 | @param create_table [in] Pointer to TABLE_LIST object providing database |
| 4098 | and name for table to be created or to be open |
| 4099 | @param alter_info [in/out] Initial list of columns and indexes for the |
| 4100 | table to be created |
| 4101 | @param items [in] List of items which should be used to produce |
| 4102 | rest of fields for the table (corresponding |
| 4103 | fields will be added to the end of |
| 4104 | alter_info->create_list) |
| 4105 | @param lock [out] Pointer to the MYSQL_LOCK object for table |
| 4106 | created will be returned in this parameter. |
| 4107 | Since this table is not included in THD::lock |
| 4108 | caller is responsible for explicitly unlocking |
| 4109 | this table. |
| 4110 | @param hooks [in] Hooks to be invoked before and after obtaining |
| 4111 | table lock on the table being created. |
| 4112 | |
| 4113 | @note |
| 4114 | This function assumes that either table exists and was pre-opened and |
| 4115 | locked at open_and_lock_tables() stage (and in this case we just emit |
| 4116 | error or warning and return pre-opened TABLE object) or an exclusive |
| 4117 | metadata lock was acquired on table so we can safely create, open and |
| 4118 | lock table in it (we don't acquire metadata lock if this create is |
| 4119 | for temporary table). |
| 4120 | |
| 4121 | @note |
| 4122 | Since this function contains some logic specific to CREATE TABLE ... |
| 4123 | SELECT it should be changed before it can be used in other contexts. |
| 4124 | |
| 4125 | @retval non-zero Pointer to TABLE object for table created or opened |
| 4126 | @retval 0 Error |
| 4127 | */ |
| 4128 | |
| 4129 | TABLE *select_create::create_table_from_items(THD *thd, |
| 4130 | List<Item> *items, |
| 4131 | MYSQL_LOCK **lock, |
| 4132 | TABLEOP_HOOKS *hooks) |
| 4133 | { |
| 4134 | TABLE tmp_table; // Used during 'Create_field()' |
| 4135 | TABLE_SHARE share; |
| 4136 | TABLE *table= 0; |
| 4137 | uint select_field_count= items->elements; |
| 4138 | /* Add selected items to field list */ |
| 4139 | List_iterator_fast<Item> it(*items); |
| 4140 | Item *item; |
| 4141 | DBUG_ENTER("select_create::create_table_from_items" ); |
| 4142 | |
| 4143 | tmp_table.s= &share; |
| 4144 | init_tmp_table_share(thd, &share, "" , 0, "" , "" ); |
| 4145 | |
| 4146 | tmp_table.s->db_create_options=0; |
| 4147 | tmp_table.null_row= 0; |
| 4148 | tmp_table.maybe_null= 0; |
| 4149 | tmp_table.in_use= thd; |
| 4150 | |
| 4151 | if (!opt_explicit_defaults_for_timestamp) |
| 4152 | promote_first_timestamp_column(&alter_info->create_list); |
| 4153 | |
| 4154 | if (create_info->vers_fix_system_fields(thd, alter_info, *create_table, |
| 4155 | true)) |
| 4156 | DBUG_RETURN(NULL); |
| 4157 | |
| 4158 | while ((item=it++)) |
| 4159 | { |
| 4160 | Field *tmp_field= item->create_field_for_create_select(&tmp_table); |
| 4161 | |
| 4162 | if (!tmp_field) |
| 4163 | DBUG_RETURN(NULL); |
| 4164 | |
| 4165 | Field *table_field; |
| 4166 | |
| 4167 | switch (item->type()) |
| 4168 | { |
| 4169 | /* |
| 4170 | We have to take into account both the real table's fields and |
| 4171 | pseudo-fields used in trigger's body. These fields are used |
| 4172 | to copy defaults values later inside constructor of |
| 4173 | the class Create_field. |
| 4174 | */ |
| 4175 | case Item::FIELD_ITEM: |
| 4176 | case Item::TRIGGER_FIELD_ITEM: |
| 4177 | table_field= ((Item_field *) item)->field; |
| 4178 | break; |
| 4179 | default: |
| 4180 | table_field= NULL; |
| 4181 | } |
| 4182 | |
| 4183 | Create_field *cr_field= new (thd->mem_root) |
| 4184 | Create_field(thd, tmp_field, table_field); |
| 4185 | |
| 4186 | if (!cr_field) |
| 4187 | DBUG_RETURN(NULL); |
| 4188 | |
| 4189 | if (item->maybe_null) |
| 4190 | cr_field->flags &= ~NOT_NULL_FLAG; |
| 4191 | alter_info->create_list.push_back(cr_field, thd->mem_root); |
| 4192 | } |
| 4193 | |
| 4194 | if (create_info->vers_check_system_fields(thd, alter_info, *create_table)) |
| 4195 | DBUG_RETURN(NULL); |
| 4196 | |
| 4197 | DEBUG_SYNC(thd,"create_table_select_before_create" ); |
| 4198 | |
| 4199 | /* Check if LOCK TABLES + CREATE OR REPLACE of existing normal table*/ |
| 4200 | if (thd->locked_tables_mode && create_table->table && |
| 4201 | !create_info->tmp_table()) |
| 4202 | { |
| 4203 | /* Remember information about the locked table */ |
| 4204 | create_info->pos_in_locked_tables= |
| 4205 | create_table->table->pos_in_locked_tables; |
| 4206 | create_info->mdl_ticket= create_table->table->mdl_ticket; |
| 4207 | } |
| 4208 | |
| 4209 | /* |
| 4210 | Create and lock table. |
| 4211 | |
| 4212 | Note that we either creating (or opening existing) temporary table or |
| 4213 | creating base table on which name we have exclusive lock. So code below |
| 4214 | should not cause deadlocks or races. |
| 4215 | |
| 4216 | We don't log the statement, it will be logged later. |
| 4217 | |
| 4218 | If this is a HEAP table, the automatic DELETE FROM which is written to the |
| 4219 | binlog when a HEAP table is opened for the first time since startup, must |
| 4220 | not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we |
| 4221 | don't want to delete from it) 2) it would be written before the CREATE |
| 4222 | TABLE, which is a wrong order. So we keep binary logging disabled when we |
| 4223 | open_table(). |
| 4224 | */ |
| 4225 | |
| 4226 | if (!mysql_create_table_no_lock(thd, &create_table->db, |
| 4227 | &create_table->table_name, |
| 4228 | create_info, alter_info, NULL, |
| 4229 | select_field_count, create_table)) |
| 4230 | { |
| 4231 | DEBUG_SYNC(thd,"create_table_select_before_open" ); |
| 4232 | |
| 4233 | /* |
| 4234 | If we had a temporary table or a table used with LOCK TABLES, |
| 4235 | it was closed by mysql_create() |
| 4236 | */ |
| 4237 | create_table->table= 0; |
| 4238 | |
| 4239 | if (!create_info->tmp_table()) |
| 4240 | { |
| 4241 | Open_table_context ot_ctx(thd, MYSQL_OPEN_REOPEN); |
| 4242 | TABLE_LIST::enum_open_strategy save_open_strategy; |
| 4243 | |
| 4244 | /* Force the newly created table to be opened */ |
| 4245 | save_open_strategy= create_table->open_strategy; |
| 4246 | create_table->open_strategy= TABLE_LIST::OPEN_NORMAL; |
| 4247 | /* |
| 4248 | Here we open the destination table, on which we already have |
| 4249 | an exclusive metadata lock. |
| 4250 | */ |
| 4251 | if (open_table(thd, create_table, &ot_ctx)) |
| 4252 | { |
| 4253 | quick_rm_table(thd, create_info->db_type, &create_table->db, |
| 4254 | table_case_name(create_info, &create_table->table_name), |
| 4255 | 0); |
| 4256 | } |
| 4257 | /* Restore */ |
| 4258 | create_table->open_strategy= save_open_strategy; |
| 4259 | } |
| 4260 | else |
| 4261 | { |
| 4262 | /* |
| 4263 | The pointer to the newly created temporary table has been stored in |
| 4264 | table->create_info. |
| 4265 | */ |
| 4266 | create_table->table= create_info->table; |
| 4267 | if (!create_table->table) |
| 4268 | { |
| 4269 | /* |
| 4270 | This shouldn't happen as creation of temporary table should make |
| 4271 | it preparable for open. Anyway we can't drop temporary table if |
| 4272 | we are unable to find it. |
| 4273 | */ |
| 4274 | DBUG_ASSERT(0); |
| 4275 | } |
| 4276 | } |
| 4277 | } |
| 4278 | else |
| 4279 | create_table->table= 0; // Create failed |
| 4280 | |
| 4281 | if (unlikely(!(table= create_table->table))) |
| 4282 | { |
| 4283 | if (likely(!thd->is_error())) // CREATE ... IF NOT EXISTS |
| 4284 | my_ok(thd); // succeed, but did nothing |
| 4285 | DBUG_RETURN(NULL); |
| 4286 | } |
| 4287 | |
| 4288 | DEBUG_SYNC(thd,"create_table_select_before_lock" ); |
| 4289 | |
| 4290 | table->reginfo.lock_type=TL_WRITE; |
| 4291 | hooks->prelock(&table, 1); // Call prelock hooks |
| 4292 | /* |
| 4293 | mysql_lock_tables() below should never fail with request to reopen table |
| 4294 | since it won't wait for the table lock (we have exclusive metadata lock on |
| 4295 | the table) and thus can't get aborted. |
| 4296 | */ |
| 4297 | if (unlikely(!((*lock)= mysql_lock_tables(thd, &table, 1, 0)) || |
| 4298 | hooks->postlock(&table, 1))) |
| 4299 | { |
| 4300 | /* purecov: begin tested */ |
| 4301 | /* |
| 4302 | This can happen in innodb when you get a deadlock when using same table |
| 4303 | in insert and select or when you run out of memory. |
| 4304 | */ |
| 4305 | my_error(ER_CANT_LOCK, MYF(0), my_errno); |
| 4306 | if (*lock) |
| 4307 | { |
| 4308 | mysql_unlock_tables(thd, *lock); |
| 4309 | *lock= 0; |
| 4310 | } |
| 4311 | drop_open_table(thd, table, &create_table->db, &create_table->table_name); |
| 4312 | DBUG_RETURN(0); |
| 4313 | /* purecov: end */ |
| 4314 | } |
| 4315 | DBUG_RETURN(table); |
| 4316 | } |
| 4317 | |
| 4318 | |
| 4319 | int |
| 4320 | select_create::prepare(List<Item> &_values, SELECT_LEX_UNIT *u) |
| 4321 | { |
| 4322 | List<Item> values(_values, thd->mem_root); |
| 4323 | MYSQL_LOCK *= NULL; |
| 4324 | DBUG_ENTER("select_create::prepare" ); |
| 4325 | |
| 4326 | TABLEOP_HOOKS *hook_ptr= NULL; |
| 4327 | /* |
| 4328 | For row-based replication, the CREATE-SELECT statement is written |
| 4329 | in two pieces: the first one contain the CREATE TABLE statement |
| 4330 | necessary to create the table and the second part contain the rows |
| 4331 | that should go into the table. |
| 4332 | |
| 4333 | For non-temporary tables, the start of the CREATE-SELECT |
| 4334 | implicitly commits the previous transaction, and all events |
| 4335 | forming the statement will be stored the transaction cache. At end |
| 4336 | of the statement, the entire statement is committed as a |
| 4337 | transaction, and all events are written to the binary log. |
| 4338 | |
| 4339 | On the master, the table is locked for the duration of the |
| 4340 | statement, but since the CREATE part is replicated as a simple |
| 4341 | statement, there is no way to lock the table for accesses on the |
| 4342 | slave. Hence, we have to hold on to the CREATE part of the |
| 4343 | statement until the statement has finished. |
| 4344 | */ |
| 4345 | class MY_HOOKS : public TABLEOP_HOOKS { |
| 4346 | public: |
| 4347 | MY_HOOKS(select_create *x, TABLE_LIST *create_table_arg, |
| 4348 | TABLE_LIST *select_tables_arg) |
| 4349 | : ptr(x), |
| 4350 | create_table(create_table_arg), |
| 4351 | select_tables(select_tables_arg) |
| 4352 | { |
| 4353 | } |
| 4354 | |
| 4355 | private: |
| 4356 | virtual int do_postlock(TABLE **tables, uint count) |
| 4357 | { |
| 4358 | int error; |
| 4359 | THD *thd= const_cast<THD*>(ptr->get_thd()); |
| 4360 | TABLE_LIST *save_next_global= create_table->next_global; |
| 4361 | |
| 4362 | create_table->next_global= select_tables; |
| 4363 | |
| 4364 | error= thd->decide_logging_format(create_table); |
| 4365 | |
| 4366 | create_table->next_global= save_next_global; |
| 4367 | |
| 4368 | if (unlikely(error)) |
| 4369 | return error; |
| 4370 | |
| 4371 | TABLE const *const table = *tables; |
| 4372 | if (thd->is_current_stmt_binlog_format_row() && |
| 4373 | !table->s->tmp_table) |
| 4374 | { |
| 4375 | int error; |
| 4376 | if (unlikely((error= ptr->binlog_show_create_table(tables, count)))) |
| 4377 | return error; |
| 4378 | } |
| 4379 | return 0; |
| 4380 | } |
| 4381 | select_create *ptr; |
| 4382 | TABLE_LIST *create_table; |
| 4383 | TABLE_LIST *select_tables; |
| 4384 | }; |
| 4385 | |
| 4386 | MY_HOOKS hooks(this, create_table, select_tables); |
| 4387 | hook_ptr= &hooks; |
| 4388 | |
| 4389 | unit= u; |
| 4390 | |
| 4391 | /* |
| 4392 | Start a statement transaction before the create if we are using |
| 4393 | row-based replication for the statement. If we are creating a |
| 4394 | temporary table, we need to start a statement transaction. |
| 4395 | */ |
| 4396 | if (!thd->lex->tmp_table() && |
| 4397 | thd->is_current_stmt_binlog_format_row() && |
| 4398 | mysql_bin_log.is_open()) |
| 4399 | { |
| 4400 | thd->binlog_start_trans_and_stmt(); |
| 4401 | } |
| 4402 | |
| 4403 | DEBUG_SYNC(thd,"create_table_select_before_check_if_exists" ); |
| 4404 | |
| 4405 | if (!(table= create_table_from_items(thd, &values, &extra_lock, hook_ptr))) |
| 4406 | /* abort() deletes table */ |
| 4407 | DBUG_RETURN(-1); |
| 4408 | |
| 4409 | if (create_info->tmp_table()) |
| 4410 | { |
| 4411 | /* |
| 4412 | When the temporary table was created & opened in create_table_impl(), |
| 4413 | the table's TABLE_SHARE (and thus TABLE) object was also linked to THD |
| 4414 | temporary tables lists. So, we must temporarily remove it from the |
| 4415 | list to keep them inaccessible from inner statements. |
| 4416 | e.g. CREATE TEMPORARY TABLE `t1` AS SELECT * FROM `t1`; |
| 4417 | */ |
| 4418 | saved_tmp_table_share= thd->save_tmp_table_share(create_table->table); |
| 4419 | } |
| 4420 | |
| 4421 | if (extra_lock) |
| 4422 | { |
| 4423 | DBUG_ASSERT(m_plock == NULL); |
| 4424 | |
| 4425 | if (create_info->tmp_table()) |
| 4426 | m_plock= &m_lock; |
| 4427 | else |
| 4428 | m_plock= &thd->extra_lock; |
| 4429 | |
| 4430 | *m_plock= extra_lock; |
| 4431 | } |
| 4432 | |
| 4433 | if (table->s->fields < values.elements) |
| 4434 | { |
| 4435 | my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L); |
| 4436 | DBUG_RETURN(-1); |
| 4437 | } |
| 4438 | |
| 4439 | /* First field to copy */ |
| 4440 | field= table->field+table->s->fields; |
| 4441 | |
| 4442 | /* Mark all fields that are given values */ |
| 4443 | for (uint n= values.elements; n; ) |
| 4444 | { |
| 4445 | if ((*--field)->invisible >= INVISIBLE_SYSTEM) |
| 4446 | continue; |
| 4447 | n--; |
| 4448 | bitmap_set_bit(table->write_set, (*field)->field_index); |
| 4449 | } |
| 4450 | |
| 4451 | table->next_number_field=table->found_next_number_field; |
| 4452 | |
| 4453 | restore_record(table,s->default_values); // Get empty record |
| 4454 | thd->cuted_fields=0; |
| 4455 | if (info.ignore || info.handle_duplicates != DUP_ERROR) |
| 4456 | table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); |
| 4457 | if (info.handle_duplicates == DUP_REPLACE && |
| 4458 | (!table->triggers || !table->triggers->has_delete_triggers())) |
| 4459 | table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE); |
| 4460 | if (info.handle_duplicates == DUP_UPDATE) |
| 4461 | table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE); |
| 4462 | if (thd->locked_tables_mode <= LTM_LOCK_TABLES) |
| 4463 | table->file->ha_start_bulk_insert((ha_rows) 0); |
| 4464 | thd->abort_on_warning= !info.ignore && thd->is_strict_mode(); |
| 4465 | if (check_that_all_fields_are_given_values(thd, table, table_list)) |
| 4466 | DBUG_RETURN(1); |
| 4467 | table->mark_columns_needed_for_insert(); |
| 4468 | table->file->extra(HA_EXTRA_WRITE_CACHE); |
| 4469 | // Mark table as used |
| 4470 | table->query_id= thd->query_id; |
| 4471 | DBUG_RETURN(0); |
| 4472 | } |
| 4473 | |
| 4474 | int |
| 4475 | select_create::binlog_show_create_table(TABLE **tables, uint count) |
| 4476 | { |
| 4477 | /* |
| 4478 | Note 1: In RBR mode, we generate a CREATE TABLE statement for the |
| 4479 | created table by calling show_create_table(). In the event of an error, |
| 4480 | nothing should be written to the binary log, even if the table is |
| 4481 | non-transactional; therefore we pretend that the generated CREATE TABLE |
| 4482 | statement is for a transactional table. The event will then be put in the |
| 4483 | transaction cache, and any subsequent events (e.g., table-map events and |
| 4484 | binrow events) will also be put there. We can then use |
| 4485 | ha_autocommit_or_rollback() to either throw away the entire kaboodle of |
| 4486 | events, or write them to the binary log. |
| 4487 | |
| 4488 | We write the CREATE TABLE statement here and not in prepare() |
| 4489 | since there potentially are sub-selects or accesses to information |
| 4490 | schema that will do a close_thread_tables(), destroying the |
| 4491 | statement transaction cache. |
| 4492 | */ |
| 4493 | DBUG_ASSERT(thd->is_current_stmt_binlog_format_row()); |
| 4494 | DBUG_ASSERT(tables && *tables && count > 0); |
| 4495 | |
| 4496 | char buf[2048]; |
| 4497 | String query(buf, sizeof(buf), system_charset_info); |
| 4498 | int result; |
| 4499 | TABLE_LIST tmp_table_list; |
| 4500 | |
| 4501 | memset(&tmp_table_list, 0, sizeof(tmp_table_list)); |
| 4502 | tmp_table_list.table = *tables; |
| 4503 | query.length(0); // Have to zero it since constructor doesn't |
| 4504 | |
| 4505 | result= show_create_table(thd, &tmp_table_list, &query, |
| 4506 | create_info, WITH_DB_NAME); |
| 4507 | DBUG_ASSERT(result == 0); /* show_create_table() always return 0 */ |
| 4508 | |
| 4509 | if (WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open()) |
| 4510 | { |
| 4511 | int errcode= query_error_code(thd, thd->killed == NOT_KILLED); |
| 4512 | result= thd->binlog_query(THD::STMT_QUERY_TYPE, |
| 4513 | query.ptr(), query.length(), |
| 4514 | /* is_trans */ TRUE, |
| 4515 | /* direct */ FALSE, |
| 4516 | /* suppress_use */ FALSE, |
| 4517 | errcode); |
| 4518 | } |
| 4519 | |
| 4520 | ha_fake_trx_id(thd); |
| 4521 | |
| 4522 | return result; |
| 4523 | } |
| 4524 | |
| 4525 | void select_create::store_values(List<Item> &values) |
| 4526 | { |
| 4527 | fill_record_n_invoke_before_triggers(thd, table, field, values, 1, |
| 4528 | TRG_EVENT_INSERT); |
| 4529 | } |
| 4530 | |
| 4531 | |
| 4532 | bool select_create::send_eof() |
| 4533 | { |
| 4534 | DBUG_ENTER("select_create::send_eof" ); |
| 4535 | |
| 4536 | /* |
| 4537 | The routine that writes the statement in the binary log |
| 4538 | is in select_insert::prepare_eof(). For that reason, we |
| 4539 | mark the flag at this point. |
| 4540 | */ |
| 4541 | if (table->s->tmp_table) |
| 4542 | thd->transaction.stmt.mark_created_temp_table(); |
| 4543 | |
| 4544 | if (prepare_eof()) |
| 4545 | { |
| 4546 | abort_result_set(); |
| 4547 | DBUG_RETURN(true); |
| 4548 | } |
| 4549 | |
| 4550 | if (table->s->tmp_table) |
| 4551 | { |
| 4552 | /* |
| 4553 | Now is good time to add the new table to THD temporary tables list. |
| 4554 | But, before that we need to check if same table got created by the sub- |
| 4555 | statement. |
| 4556 | */ |
| 4557 | if (thd->find_tmp_table_share(table->s->table_cache_key.str, |
| 4558 | table->s->table_cache_key.length)) |
| 4559 | { |
| 4560 | my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->alias.c_ptr()); |
| 4561 | abort_result_set(); |
| 4562 | DBUG_RETURN(true); |
| 4563 | } |
| 4564 | else |
| 4565 | { |
| 4566 | DBUG_ASSERT(saved_tmp_table_share); |
| 4567 | thd->restore_tmp_table_share(saved_tmp_table_share); |
| 4568 | } |
| 4569 | } |
| 4570 | |
| 4571 | /* |
| 4572 | Do an implicit commit at end of statement for non-temporary |
| 4573 | tables. This can fail, but we should unlock the table |
| 4574 | nevertheless. |
| 4575 | */ |
| 4576 | if (!table->s->tmp_table) |
| 4577 | { |
| 4578 | trans_commit_stmt(thd); |
| 4579 | if (!(thd->variables.option_bits & OPTION_GTID_BEGIN)) |
| 4580 | trans_commit_implicit(thd); |
| 4581 | #ifdef WITH_WSREP |
| 4582 | if (WSREP_ON) |
| 4583 | { |
| 4584 | mysql_mutex_lock(&thd->LOCK_thd_data); |
| 4585 | if (thd->wsrep_conflict_state != NO_CONFLICT) |
| 4586 | { |
| 4587 | WSREP_DEBUG("select_create commit failed, thd: %lld err: %d %s" , |
| 4588 | (longlong) thd->thread_id, thd->wsrep_conflict_state, |
| 4589 | thd->query()); |
| 4590 | mysql_mutex_unlock(&thd->LOCK_thd_data); |
| 4591 | abort_result_set(); |
| 4592 | DBUG_RETURN(true); |
| 4593 | } |
| 4594 | mysql_mutex_unlock(&thd->LOCK_thd_data); |
| 4595 | } |
| 4596 | #endif /* WITH_WSREP */ |
| 4597 | } |
| 4598 | else if (!thd->is_current_stmt_binlog_format_row()) |
| 4599 | table->s->table_creation_was_logged= 1; |
| 4600 | |
| 4601 | /* |
| 4602 | exit_done must only be set after last potential call to |
| 4603 | abort_result_set(). |
| 4604 | */ |
| 4605 | exit_done= 1; // Avoid double calls |
| 4606 | |
| 4607 | table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); |
| 4608 | table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); |
| 4609 | |
| 4610 | send_ok_packet(); |
| 4611 | |
| 4612 | if (m_plock) |
| 4613 | { |
| 4614 | MYSQL_LOCK *lock= *m_plock; |
| 4615 | *m_plock= NULL; |
| 4616 | m_plock= NULL; |
| 4617 | |
| 4618 | if (create_info->pos_in_locked_tables) |
| 4619 | { |
| 4620 | /* |
| 4621 | If we are under lock tables, we have created a table that was |
| 4622 | originally locked. We should add back the lock to ensure that |
| 4623 | all tables in the thd->open_list are locked! |
| 4624 | */ |
| 4625 | table->mdl_ticket= create_info->mdl_ticket; |
| 4626 | |
| 4627 | /* The following should never fail, except if out of memory */ |
| 4628 | if (!thd->locked_tables_list.restore_lock(thd, |
| 4629 | create_info-> |
| 4630 | pos_in_locked_tables, |
| 4631 | table, lock)) |
| 4632 | DBUG_RETURN(false); // ok |
| 4633 | /* Fail. Continue without locking the table */ |
| 4634 | } |
| 4635 | mysql_unlock_tables(thd, lock); |
| 4636 | } |
| 4637 | DBUG_RETURN(false); |
| 4638 | } |
| 4639 | |
| 4640 | |
| 4641 | void select_create::abort_result_set() |
| 4642 | { |
| 4643 | ulonglong save_option_bits; |
| 4644 | DBUG_ENTER("select_create::abort_result_set" ); |
| 4645 | |
| 4646 | /* Avoid double calls, could happen in case of out of memory on cleanup */ |
| 4647 | if (exit_done) |
| 4648 | DBUG_VOID_RETURN; |
| 4649 | exit_done= 1; |
| 4650 | |
| 4651 | /* |
| 4652 | In select_insert::abort_result_set() we roll back the statement, including |
| 4653 | truncating the transaction cache of the binary log. To do this, we |
| 4654 | pretend that the statement is transactional, even though it might |
| 4655 | be the case that it was not. |
| 4656 | |
| 4657 | We roll back the statement prior to deleting the table and prior |
| 4658 | to releasing the lock on the table, since there might be potential |
| 4659 | for failure if the rollback is executed after the drop or after |
| 4660 | unlocking the table. |
| 4661 | |
| 4662 | We also roll back the statement regardless of whether the creation |
| 4663 | of the table succeeded or not, since we need to reset the binary |
| 4664 | log state. |
| 4665 | |
| 4666 | However if there was an original table that was deleted, as part of |
| 4667 | create or replace table, then we must log the statement. |
| 4668 | */ |
| 4669 | |
| 4670 | save_option_bits= thd->variables.option_bits; |
| 4671 | thd->variables.option_bits&= ~OPTION_BIN_LOG; |
| 4672 | select_insert::abort_result_set(); |
| 4673 | thd->transaction.stmt.modified_non_trans_table= FALSE; |
| 4674 | thd->variables.option_bits= save_option_bits; |
| 4675 | |
| 4676 | /* possible error of writing binary log is ignored deliberately */ |
| 4677 | (void) thd->binlog_flush_pending_rows_event(TRUE, TRUE); |
| 4678 | |
| 4679 | if (create_info->table_was_deleted) |
| 4680 | { |
| 4681 | /* Unlock locked table that was dropped by CREATE */ |
| 4682 | thd->locked_tables_list.unlock_locked_table(thd, |
| 4683 | create_info->mdl_ticket); |
| 4684 | } |
| 4685 | if (m_plock) |
| 4686 | { |
| 4687 | mysql_unlock_tables(thd, *m_plock); |
| 4688 | *m_plock= NULL; |
| 4689 | m_plock= NULL; |
| 4690 | } |
| 4691 | |
| 4692 | if (table) |
| 4693 | { |
| 4694 | bool tmp_table= table->s->tmp_table; |
| 4695 | |
| 4696 | if (tmp_table) |
| 4697 | { |
| 4698 | DBUG_ASSERT(saved_tmp_table_share); |
| 4699 | thd->restore_tmp_table_share(saved_tmp_table_share); |
| 4700 | } |
| 4701 | |
| 4702 | table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); |
| 4703 | table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); |
| 4704 | table->auto_increment_field_not_null= FALSE; |
| 4705 | drop_open_table(thd, table, &create_table->db, &create_table->table_name); |
| 4706 | table=0; // Safety |
| 4707 | if (thd->log_current_statement && mysql_bin_log.is_open()) |
| 4708 | { |
| 4709 | /* Remove logging of drop, create + insert rows */ |
| 4710 | binlog_reset_cache(thd); |
| 4711 | /* Original table was deleted. We have to log it */ |
| 4712 | log_drop_table(thd, &create_table->db, &create_table->table_name, tmp_table); |
| 4713 | } |
| 4714 | } |
| 4715 | DBUG_VOID_RETURN; |
| 4716 | } |
| 4717 | |