597
597
never be able to get a lock on the table. QQQ: why not
598
598
upgrade the lock here instead?
600
if (table_list->lock_type == TL_WRITE_DELAYED && thd->locked_tables &&
601
find_locked_table(thd, table_list->db, table_list->table_name))
600
if (table_list->lock_type == TL_WRITE_DELAYED &&
601
thd->locked_tables_mode &&
602
find_locked_table(thd->open_tables, table_list->db,
603
table_list->table_name))
603
605
my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
604
606
table_list->table_name);
618
620
lock_type= table_list->lock_type;
621
Breakpoints for backup testing.
623
BACKUP_BREAKPOINT("backup_cs_reading");
624
BACKUP_BREAKPOINT("commit_blocker_step_1");
622
DEBUG_SYNC(thd, "after_insert_locked_tables");
625
623
thd_proc_info(thd, "init");
626
624
thd->used_tables=0;
702
700
if (thd->slave_thread &&
703
701
(info.handle_duplicates == DUP_UPDATE) &&
704
702
(table->next_number_field != NULL) &&
705
rpl_master_has_bug(&active_mi->rli, 24432))
703
rpl_master_has_bug(&active_mi->rli, 24432, TRUE, NULL, NULL))
710
708
thd_proc_info(thd, "update");
711
if (duplic != DUP_ERROR || ignore)
712
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
713
709
if (duplic == DUP_REPLACE &&
714
710
(!table->triggers || !table->triggers->has_delete_triggers()))
715
711
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
727
723
values_list.elements, and - if nothing else - to initialize
728
724
the code to make the call of end_bulk_insert() below safe.
730
if (lock_type != TL_WRITE_DELAYED && !thd->prelocked_mode)
731
table->file->ha_start_bulk_insert(values_list.elements);
726
#ifndef EMBEDDED_LIBRARY
727
if (lock_type != TL_WRITE_DELAYED)
728
#endif /* EMBEDDED_LIBRARY */
730
if (duplic != DUP_ERROR || ignore)
731
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
733
This is a simple check for the case when the table has a trigger
734
that reads from it, or when the statement invokes a stored function
735
that reads from the table being inserted to.
736
Engines can't handle a bulk insert in parallel with a read form the
737
same table in the same connection.
739
if ((thd->locked_tables_mode <= LTM_LOCK_TABLES) &&
740
(values_list.elements > 1))
742
using_bulk_insert= 1;
743
table->file->ha_start_bulk_insert(values_list.elements);
733
747
thd->abort_on_warning= (!ignore && (thd->variables.sql_mode &
734
748
(MODE_STRICT_TRANS_TABLES |
842
856
auto_inc values from the delayed_insert thread as they share TABLE.
844
858
table->file->ha_release_auto_increment();
845
if (!thd->prelocked_mode && table->file->ha_end_bulk_insert() && !error)
859
if (using_bulk_insert && table->file->ha_end_bulk_insert(0) && !error)
847
861
table->file->print_error(my_errno,MYF(0));
864
if (duplic != DUP_ERROR || ignore)
865
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
850
867
transactional_table= table->file->has_transactions();
852
869
if ((changed= (info.copied || info.deleted || info.updated)))
939
956
table->next_number_field=0;
940
957
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
941
958
table->auto_increment_field_not_null= FALSE;
942
if (duplic != DUP_ERROR || ignore)
943
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
944
959
if (duplic == DUP_REPLACE &&
945
960
(!table->triggers || !table->triggers->has_delete_triggers()))
946
961
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1030
1045
DBUG_ASSERT(view->table != 0 && view->field_translation != 0);
1032
VOID(bitmap_init(&used_fields, used_fields_buff, table->s->fields, 0));
1047
(void) bitmap_init(&used_fields, used_fields_buff, table->s->fields, 0);
1033
1048
bitmap_clear_all(&used_fields);
1035
1050
view->contain_auto_increment= 0;
1177
1192
table_map map= 0;
1178
1193
DBUG_ENTER("mysql_prepare_insert");
1179
DBUG_PRINT("enter", ("table_list 0x%lx, table 0x%lx, view %d",
1180
(ulong)table_list, (ulong)table,
1194
DBUG_PRINT("enter", ("table_list %p table %p view %d",
1181
1196
(int)insert_into_view));
1182
1197
/* INSERT should have a SELECT or VALUES clause */
1183
1198
DBUG_ASSERT (!select_insert || !values);
1276
1291
select_lex->fix_prepare_information(thd, &fake_conds, &fake_conds);
1277
1292
select_lex->first_execution= 0;
1279
if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
1295
Only call prepare_for_posistion() if we are not performing a DELAYED
1296
operation. It will instead be executed by delayed insert thread.
1298
if ((duplic == DUP_UPDATE || duplic == DUP_REPLACE) &&
1299
(table->reginfo.lock_type != TL_WRITE_DELAYED))
1280
1300
table->prepare_for_position();
1281
1301
DBUG_RETURN(FALSE);
1717
1737
pthread_mutex_init(&mutex,MY_MUTEX_INIT_FAST);
1718
1738
pthread_cond_init(&cond,NULL);
1719
1739
pthread_cond_init(&cond_client,NULL);
1720
VOID(pthread_mutex_lock(&LOCK_thread_count));
1740
pthread_mutex_lock(&LOCK_thread_count);
1721
1741
delayed_insert_threads++;
1722
VOID(pthread_mutex_unlock(&LOCK_thread_count));
1742
pthread_mutex_unlock(&LOCK_thread_count);
1724
1744
~Delayed_insert()
1731
1751
close_thread_tables(&thd);
1732
VOID(pthread_mutex_lock(&LOCK_thread_count));
1752
pthread_mutex_lock(&LOCK_thread_count);
1733
1753
pthread_mutex_destroy(&mutex);
1734
1754
pthread_cond_destroy(&cond);
1735
1755
pthread_cond_destroy(&cond_client);
1738
1758
thd.security_ctx->user= thd.security_ctx->host=0;
1739
1759
thread_count--;
1740
1760
delayed_insert_threads--;
1741
VOID(pthread_mutex_unlock(&LOCK_thread_count));
1742
VOID(pthread_cond_broadcast(&COND_thread_count)); /* Tell main we are ready */
1761
pthread_mutex_unlock(&LOCK_thread_count);
1762
pthread_cond_broadcast(&COND_thread_count); /* Tell main we are ready */
1745
1765
/* The following is for checking when we can delete ourselves */
1871
1892
if (! (di= find_handler(thd, table_list)))
1873
1894
if (!(di= new Delayed_insert()))
1876
1895
goto end_create;
1878
1896
pthread_mutex_lock(&LOCK_thread_count);
1879
1897
thread_count++;
1880
1898
pthread_mutex_unlock(&LOCK_thread_count);
1881
1899
di->thd.set_db(table_list->db, strlen(table_list->db));
1882
di->thd.query= my_strdup(table_list->table_name, MYF(MY_WME));
1900
di->thd.query= my_strdup(table_list->table_name, MYF(MY_WME | ME_FATALERROR));
1883
1901
if (di->thd.db == NULL || di->thd.query == NULL)
1885
1903
/* The error is reported */
1888
1905
goto end_create;
1890
1907
di->table_list= *table_list; // Needed to open table
1902
1919
pthread_mutex_unlock(&di->mutex);
1905
my_error(ER_CANT_CREATE_THREAD, MYF(0), error);
1922
my_error(ER_CANT_CREATE_THREAD, MYF(ME_FATALERROR), error);
1907
1923
goto end_create;
2203
2219
void kill_delayed_threads(void)
2205
VOID(pthread_mutex_lock(&LOCK_delayed_insert)); // For unlink from list
2221
pthread_mutex_lock(&LOCK_delayed_insert); // For unlink from list
2207
2223
I_List_iterator<Delayed_insert> it(delayed_threads);
2208
2224
Delayed_insert *di;
2227
2243
pthread_mutex_unlock(&di->thd.mysys_var->mutex);
2230
VOID(pthread_mutex_unlock(&LOCK_delayed_insert)); // For unlink from list
2246
pthread_mutex_unlock(&LOCK_delayed_insert); // For unlink from list
2251
Open and lock table for use by delayed thread and check that
2252
this table is suitable for delayed inserts.
2254
@retval FALSE - Success.
2255
@retval TRUE - Failure.
2258
bool Delayed_insert::open_and_lock_table()
2260
if (!(table= open_n_lock_single_table(&thd, &table_list,
2261
TL_WRITE_DELAYED, 0)))
2263
thd.fatal_error(); // Abort waiting inserts
2266
if (!(table->file->ha_table_flags() & HA_CAN_INSERT_DELAYED))
2268
my_error(ER_DELAYED_NOT_SUPPORTED, MYF(ME_FATALERROR),
2269
table_list.table_name);
2272
if (table->triggers)
2275
Table has triggers. This is not an error, but we do
2276
not support triggers with delayed insert. Terminate the delayed
2277
thread without an error and thus request lock upgrade.
2281
table->copy_blobs= 1;
2293
2345
thd->lex->set_stmt_unsafe();
2294
2346
thd->set_current_stmt_binlog_row_based_if_mixed();
2297
if (!(di->table= open_n_lock_single_table(thd, &di->table_list,
2300
thd->fatal_error(); // Abort waiting inserts
2303
if (!(di->table->file->ha_table_flags() & HA_CAN_INSERT_DELAYED))
2306
my_error(ER_ILLEGAL_HA, MYF(0), di->table_list.table_name);
2309
if (di->table->triggers)
2312
Table has triggers. This is not an error, but we do
2313
not support triggers with delayed insert. Terminate the delayed
2314
thread without an error and thus request lock upgrade.
2318
di->table->copy_blobs=1;
2348
alloc_mdl_locks(&di->table_list, thd->mem_root);
2350
if (di->open_and_lock_table())
2320
2353
/* Tell client that the thread is initialized */
2321
2354
pthread_cond_signal(&di->cond_client);
2404
2437
if (! (thd->lock= mysql_lock_tables(thd, &di->table, 1,
2405
2438
MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK,
2410
thd->killed= THD::KILL_CONNECTION;
2444
We were waiting to obtain TL_WRITE_DELAYED (probably due to
2445
someone having or requesting TL_WRITE_ALLOW_READ) and got
2446
aborted. Try to reopen table and if it fails die.
2448
close_thread_tables(thd);
2450
if (di->open_and_lock_table())
2453
thd->killed= THD::KILL_CONNECTION;
2460
thd->killed= THD::KILL_CONNECTION;
2412
2463
pthread_cond_broadcast(&di->cond_client);
2437
2488
di->table->file->ha_release_auto_increment();
2438
2489
mysql_unlock_tables(thd, lock);
2490
ha_autocommit_or_rollback(thd, 0);
2439
2491
di->group_count=0;
2440
2492
mysql_audit_release(thd);
2441
2493
pthread_mutex_lock(&di->mutex);
2539
2591
thd_proc_info(&thd, "insert");
2540
2592
max_rows= delayed_insert_limit;
2541
if (thd.killed || table->needs_reopen_or_name_lock())
2593
if (thd.killed || table->needs_reopen())
2543
2595
thd.killed= THD::KILL_CONNECTION;
2544
2596
max_rows= ULONG_MAX; // Do as much as possible
2946
2998
lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
2948
3000
else if (!(lex->current_select->options & OPTION_BUFFER_RESULT) &&
2949
!thd->prelocked_mode)
3001
thd->locked_tables_mode <= LTM_LOCK_TABLES)
2952
3004
We must not yet prepare the result table if it is the same as one of the
2966
3018
if (thd->slave_thread &&
2967
3019
(info.handle_duplicates == DUP_UPDATE) &&
2968
3020
(table->next_number_field != NULL) &&
2969
rpl_master_has_bug(&active_mi->rli, 24432))
3021
rpl_master_has_bug(&active_mi->rli, 24432, TRUE, NULL, NULL))
2970
3022
DBUG_RETURN(1);
3132
3184
DBUG_PRINT("enter", ("trans_table=%d, table_type='%s'",
3133
3185
trans_table, table->file->table_type()));
3135
error= (!thd->prelocked_mode) ? table->file->ha_end_bulk_insert():0;
3187
error= ((thd->locked_tables_mode <= LTM_LOCK_TABLES) ?
3188
table->file->ha_end_bulk_insert(0) : 0);
3136
3189
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
3137
3190
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
3206
3259
If we are not in prelocked mode, we end the bulk insert started
3209
if (!thd->prelocked_mode)
3210
table->file->ha_end_bulk_insert();
3262
if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
3263
table->file->ha_end_bulk_insert(0);
3213
3266
If at least one row has been inserted/modified and will stay in
3307
3360
Field *tmp_field;
3362
enum_open_table_action not_used2;
3309
3363
DBUG_ENTER("create_table_from_items");
3311
3365
DBUG_EXECUTE_IF("sleep_create_select_before_check_if_exists", my_sleep(6000000););
3313
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
3314
create_table->table->db_stat)
3367
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) && create_table->table)
3316
3369
/* Table already exists and was open at open_and_lock_tables() stage. */
3317
3370
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
3404
3457
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
3406
VOID(pthread_mutex_lock(&LOCK_open));
3407
if (reopen_name_locked_table(thd, create_table, FALSE))
3459
enum enum_open_table_action ot_action_unused;
3461
Here we open the destination table, on which we already have
3462
an exclusive metadata lock.
3464
if (open_table(thd, create_table, thd->mem_root, &ot_action_unused,
3467
pthread_mutex_lock(&LOCK_open);
3409
3468
quick_rm_table(create_info->db_type, create_table->db,
3410
3469
table_case_name(create_info, create_table->table_name),
3471
pthread_mutex_unlock(&LOCK_open);
3414
3474
table= create_table->table;
3415
VOID(pthread_mutex_unlock(&LOCK_open));
3419
if (!(table= open_table(thd, create_table, thd->mem_root, (bool*) 0,
3420
MYSQL_OPEN_TEMPORARY_ONLY)) &&
3478
if (open_table(thd, create_table, thd->mem_root, ¬_used2,
3479
MYSQL_OPEN_TEMPORARY_ONLY) &&
3421
3480
!create_info->table_existed)
3439
3500
table->reginfo.lock_type=TL_WRITE;
3440
3501
hooks->prelock(&table, 1); // Call prelock hooks
3503
mysql_lock_tables() below should never fail with request to reopen table
3504
since it won't wait for the table lock (we have exclusive metadata lock on
3505
the table) and thus can't get aborted and since it ignores other threads
3506
setting THD::some_tables_deleted thanks to MYSQL_LOCK_IGNORE_FLUSH.
3441
3508
if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
3442
3509
MYSQL_LOCK_IGNORE_FLUSH, ¬_used)) ||
3443
3510
hooks->postlock(&table, 1))
3570
3637
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
3571
3638
if (info.handle_duplicates == DUP_UPDATE)
3572
3639
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
3573
if (!thd->prelocked_mode)
3640
if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
3574
3641
table->file->ha_start_bulk_insert((ha_rows) 0);
3575
3642
thd->abort_on_warning= (!info.ignore &&
3576
3643
(thd->variables.sql_mode &
3701
3768
DBUG_ENTER("select_create::abort");
3704
Disable binlog, because we "roll back" partial inserts in ::abort
3705
by removing the table, even for non-transactional tables.
3707
tmp_disable_binlog(thd);
3709
3771
In select_insert::abort() we roll back the statement, including
3710
truncating the transaction cache of the binary log.
3772
truncating the transaction cache of the binary log. To do this, we
3773
pretend that the statement is transactional, even though it might
3774
be the case that it was not.
3712
3776
We roll back the statement prior to deleting the table and prior
3713
3777
to releasing the lock on the table, since there might be potential