Commit 1f746cb0 authored by Marko Mäkelä's avatar Marko Mäkelä

Merge mysql-5.1 to mysql-5.5.

parents 08d5cbf1 a0a51251
#
#Bug#11759688 52020: InnoDB can still deadlock
#on just INSERT...ON DUPLICATE KEY
#a.k.a. Bug#7975 deadlock without any locking, simple select and update
#
CREATE TABLE t1 (a INT PRIMARY KEY, b INT NOT NULL) ENGINE=InnoDB;
INSERT INTO t1 VALUES(3,1);
BEGIN;
SET DEBUG_SYNC='write_row_noreplace SIGNAL insert1 WAIT_FOR select1';
INSERT INTO t1 VALUES(3,2);
SET DEBUG_SYNC='now WAIT_FOR insert1';
SELECT * FROM t1 LOCK IN SHARE MODE;
a b
3 1
SELECT * FROM t1 FOR UPDATE;
SET DEBUG_SYNC='now SIGNAL select1';
ERROR 23000: Duplicate entry '3' for key 'PRIMARY'
INSERT INTO t1 VALUES(3,3) ON DUPLICATE KEY UPDATE b=b+10;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
COMMIT;
SET DEBUG_SYNC='write_row_replace SIGNAL insert2 WAIT_FOR select2';
REPLACE INTO t1 VALUES(3,4);
SET DEBUG_SYNC='now WAIT_FOR insert2';
SELECT * FROM t1;
a b
3 11
SELECT * FROM t1 LOCK IN SHARE MODE;
SET DEBUG_SYNC='now SIGNAL select2';
SET DEBUG_SYNC='write_row_replace SIGNAL insert3 WAIT_FOR select3';
INSERT INTO t1 VALUES(3,5) ON DUPLICATE KEY UPDATE b=b+20;
a b
3 4
SET DEBUG_SYNC='now WAIT_FOR insert3';
SELECT b FROM t1 LOCK IN SHARE MODE;
SET DEBUG_SYNC='now SIGNAL select3';
b
24
SET DEBUG_SYNC='write_row_noreplace SIGNAL insert4 WAIT_FOR select4';
LOAD DATA INFILE '../../std_data/loaddata5.dat' INTO TABLE t1 FIELDS TERMINATED BY '' ENCLOSED BY '' (a, b);
SET DEBUG_SYNC='now WAIT_FOR insert4';
SELECT b FROM t1 WHERE a=3 LOCK IN SHARE MODE;
b
24
SELECT b FROM t1 WHERE a=3 FOR UPDATE;
SET DEBUG_SYNC='now SIGNAL select4';
b
24
ERROR 23000: Duplicate entry '3' for key 'PRIMARY'
SET DEBUG_SYNC='write_row_noreplace SIGNAL insert5 WAIT_FOR select5';
LOAD DATA INFILE '../../std_data/loaddata5.dat' IGNORE INTO TABLE t1 FIELDS TERMINATED BY '' ENCLOSED BY '' (a, b);
SET DEBUG_SYNC='now WAIT_FOR insert5';
SELECT * FROM t1;
a b
3 24
SELECT * FROM t1 WHERE a=3 LOCK IN SHARE MODE;
a b
3 24
SELECT * FROM t1 WHERE a=3 FOR UPDATE;
SET DEBUG_SYNC='now SIGNAL select5';
a b
3 24
SET DEBUG_SYNC='write_row_replace SIGNAL insert6 WAIT_FOR select6';
LOAD DATA INFILE '../../std_data/loaddata5.dat' REPLACE INTO TABLE t1 FIELDS TERMINATED BY '' ENCLOSED BY '' (a, b);
SET DEBUG_SYNC='now WAIT_FOR insert6';
SELECT * FROM t1;
a b
1 2
3 24
5 6
SELECT a,b FROM t1 LOCK IN SHARE MODE;
SET DEBUG_SYNC='now SIGNAL select6';
a b
1 2
3 4
5 6
SET DEBUG_SYNC='RESET';
DROP TABLE t1;
--source include/have_innodb.inc
--source include/have_debug_sync.inc
--echo #
--echo #Bug#11759688 52020: InnoDB can still deadlock
--echo #on just INSERT...ON DUPLICATE KEY
--echo #a.k.a. Bug#7975 deadlock without any locking, simple select and update
--echo #
CREATE TABLE t1 (a INT PRIMARY KEY, b INT NOT NULL) ENGINE=InnoDB;
INSERT INTO t1 VALUES(3,1);
connect (con1,localhost,root,,);
connect (con2,localhost,root,,);
connection con1;
BEGIN;
# normal INSERT of a duplicate should only S-lock the existing record (3,1)
SET DEBUG_SYNC='write_row_noreplace SIGNAL insert1 WAIT_FOR select1';
--send
INSERT INTO t1 VALUES(3,2);
connection default;
SET DEBUG_SYNC='now WAIT_FOR insert1';
# this should S-lock (3,1); no conflict
SELECT * FROM t1 LOCK IN SHARE MODE;
# this should X-lock (3,1), conflicting with con1
--send
SELECT * FROM t1 FOR UPDATE;
connection con2;
# Check that the above SELECT is blocked
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = 'Sending data' and
info = 'SELECT * FROM t1 FOR UPDATE';
--source include/wait_condition.inc
SET DEBUG_SYNC='now SIGNAL select1';
connection con1;
--error ER_DUP_ENTRY
reap;
# We are still holding an S-lock on (3,1) after the failed INSERT.
# The following will upgrade it to an X-lock, causing a deadlock.
# InnoDB should resolve the deadlock by aborting the blocked SELECT.
INSERT INTO t1 VALUES(3,3) ON DUPLICATE KEY UPDATE b=b+10;
connection default;
--error ER_LOCK_DEADLOCK
reap;
connection con1;
COMMIT;
SET DEBUG_SYNC='write_row_replace SIGNAL insert2 WAIT_FOR select2';
--send
REPLACE INTO t1 VALUES(3,4);
connection default;
SET DEBUG_SYNC='now WAIT_FOR insert2';
SELECT * FROM t1;
--send
SELECT * FROM t1 LOCK IN SHARE MODE;
connection con2;
# Check that the above SELECT is blocked because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = 'Sending data' and
info = 'SELECT * FROM t1 LOCK IN SHARE MODE';
--source include/wait_condition.inc
SET DEBUG_SYNC='now SIGNAL select2';
connection con1;
reap;
SET DEBUG_SYNC='write_row_replace SIGNAL insert3 WAIT_FOR select3';
--send
INSERT INTO t1 VALUES(3,5) ON DUPLICATE KEY UPDATE b=b+20;
connection default;
reap;
SET DEBUG_SYNC='now WAIT_FOR insert3';
--send
SELECT b FROM t1 LOCK IN SHARE MODE;
connection con2;
# Check that the above SELECT is blocked because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = 'Sending data' and
info = 'SELECT b FROM t1 LOCK IN SHARE MODE';
--source include/wait_condition.inc
SET DEBUG_SYNC='now SIGNAL select3';
connection default;
reap;
connection con1;
reap;
SET DEBUG_SYNC='write_row_noreplace SIGNAL insert4 WAIT_FOR select4';
--send
LOAD DATA INFILE '../../std_data/loaddata5.dat' INTO TABLE t1 FIELDS TERMINATED BY '' ENCLOSED BY '' (a, b);
connection default;
SET DEBUG_SYNC='now WAIT_FOR insert4';
# this should S-lock (3,1); no conflict
SELECT b FROM t1 WHERE a=3 LOCK IN SHARE MODE;
# this should X-lock (3,1), conflicting with con1
--send
SELECT b FROM t1 WHERE a=3 FOR UPDATE;
connection con2;
# Check that the above SELECT is blocked
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = 'statistics' and
info = 'SELECT b FROM t1 WHERE a=3 FOR UPDATE';
--source include/wait_condition.inc
SET DEBUG_SYNC='now SIGNAL select4';
connection default;
reap;
connection con1;
--error ER_DUP_ENTRY
reap;
SET DEBUG_SYNC='write_row_noreplace SIGNAL insert5 WAIT_FOR select5';
--send
LOAD DATA INFILE '../../std_data/loaddata5.dat' IGNORE INTO TABLE t1 FIELDS TERMINATED BY '' ENCLOSED BY '' (a, b);
connection default;
SET DEBUG_SYNC='now WAIT_FOR insert5';
SELECT * FROM t1;
# this should S-lock; no conflict
SELECT * FROM t1 WHERE a=3 LOCK IN SHARE MODE;
# this should X-lock, conflicting with the S-lock of the IGNORE in con1
--send
SELECT * FROM t1 WHERE a=3 FOR UPDATE;
connection con2;
# Check that the above SELECT is blocked
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = 'statistics' and
info = 'SELECT * FROM t1 WHERE a=3 FOR UPDATE';
--source include/wait_condition.inc
SET DEBUG_SYNC='now SIGNAL select5';
connection con1;
reap;
connection default;
reap;
connection con1;
SET DEBUG_SYNC='write_row_replace SIGNAL insert6 WAIT_FOR select6';
--send
LOAD DATA INFILE '../../std_data/loaddata5.dat' REPLACE INTO TABLE t1 FIELDS TERMINATED BY '' ENCLOSED BY '' (a, b);
connection default;
SET DEBUG_SYNC='now WAIT_FOR insert6';
SELECT * FROM t1;
# this should conflict with the X-lock acquired by the REPLACE
--send
SELECT a,b FROM t1 LOCK IN SHARE MODE;
connection con2;
# Check that the above SELECT is blocked
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = 'Sending data' and
info = 'SELECT a,b FROM t1 LOCK IN SHARE MODE';
--source include/wait_condition.inc
SET DEBUG_SYNC='now SIGNAL select6';
connection con1;
reap;
connection default;
reap;
disconnect con1;
disconnect con2;
connection default;
SET DEBUG_SYNC='RESET';
DROP TABLE t1;
...@@ -77,6 +77,8 @@ ...@@ -77,6 +77,8 @@
#include "transaction.h" #include "transaction.h"
#include "sql_audit.h" #include "sql_audit.h"
#include "debug_sync.h"
#ifndef EMBEDDED_LIBRARY #ifndef EMBEDDED_LIBRARY
static bool delayed_get_table(THD *thd, MDL_request *grl_protection_request, static bool delayed_get_table(THD *thd, MDL_request *grl_protection_request,
TABLE_LIST *table_list); TABLE_LIST *table_list);
...@@ -1541,6 +1543,8 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info) ...@@ -1541,6 +1543,8 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
error= HA_ERR_FOUND_DUPP_KEY; /* Database can't find key */ error= HA_ERR_FOUND_DUPP_KEY; /* Database can't find key */
goto err; goto err;
} }
DEBUG_SYNC(thd, "write_row_replace");
/* Read all columns for the row we are going to replace */ /* Read all columns for the row we are going to replace */
table->use_all_columns(); table->use_all_columns();
/* /*
...@@ -1729,6 +1733,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info) ...@@ -1729,6 +1733,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
} }
else if ((error=table->file->ha_write_row(table->record[0]))) else if ((error=table->file->ha_write_row(table->record[0])))
{ {
DEBUG_SYNC(thd, "write_row_noreplace");
if (!info->ignore || if (!info->ignore ||
table->file->is_fatal_error(error, HA_CHECK_DUP)) table->file->is_fatal_error(error, HA_CHECK_DUP))
goto err; goto err;
......
...@@ -5063,8 +5063,7 @@ no_commit: ...@@ -5063,8 +5063,7 @@ no_commit:
switch (sql_command) { switch (sql_command) {
case SQLCOM_LOAD: case SQLCOM_LOAD:
if ((trx->duplicates if (trx->duplicates) {
& (TRX_DUP_IGNORE | TRX_DUP_REPLACE))) {
goto set_max_autoinc; goto set_max_autoinc;
} }
...@@ -5339,8 +5338,7 @@ ha_innobase::update_row( ...@@ -5339,8 +5338,7 @@ ha_innobase::update_row(
&& table->next_number_field && table->next_number_field
&& new_row == table->record[0] && new_row == table->record[0]
&& thd_sql_command(user_thd) == SQLCOM_INSERT && thd_sql_command(user_thd) == SQLCOM_INSERT
&& (trx->duplicates & (TRX_DUP_IGNORE | TRX_DUP_REPLACE)) && trx->duplicates) {
== TRX_DUP_IGNORE) {
ulonglong auto_inc; ulonglong auto_inc;
ulonglong col_max_value; ulonglong col_max_value;
...@@ -8845,6 +8843,7 @@ ha_innobase::extra( ...@@ -8845,6 +8843,7 @@ ha_innobase::extra(
break; break;
case HA_EXTRA_RESET_STATE: case HA_EXTRA_RESET_STATE:
reset_template(prebuilt); reset_template(prebuilt);
thd_to_trx(ha_thd())->duplicates = 0;
break; break;
case HA_EXTRA_NO_KEYREAD: case HA_EXTRA_NO_KEYREAD:
prebuilt->read_just_key = 0; prebuilt->read_just_key = 0;
...@@ -8862,19 +8861,18 @@ ha_innobase::extra( ...@@ -8862,19 +8861,18 @@ ha_innobase::extra(
parameters below. We must not invoke update_thd() parameters below. We must not invoke update_thd()
either, because the calling threads may change. either, because the calling threads may change.
CAREFUL HERE, OR MEMORY CORRUPTION MAY OCCUR! */ CAREFUL HERE, OR MEMORY CORRUPTION MAY OCCUR! */
case HA_EXTRA_IGNORE_DUP_KEY: case HA_EXTRA_INSERT_WITH_UPDATE:
thd_to_trx(ha_thd())->duplicates |= TRX_DUP_IGNORE; thd_to_trx(ha_thd())->duplicates |= TRX_DUP_IGNORE;
break; break;
case HA_EXTRA_NO_IGNORE_DUP_KEY:
thd_to_trx(ha_thd())->duplicates &= ~TRX_DUP_IGNORE;
break;
case HA_EXTRA_WRITE_CAN_REPLACE: case HA_EXTRA_WRITE_CAN_REPLACE:
thd_to_trx(ha_thd())->duplicates |= TRX_DUP_REPLACE; thd_to_trx(ha_thd())->duplicates |= TRX_DUP_REPLACE;
break; break;
case HA_EXTRA_WRITE_CANNOT_REPLACE: case HA_EXTRA_WRITE_CANNOT_REPLACE:
thd_to_trx(ha_thd())->duplicates &= ~TRX_DUP_REPLACE; thd_to_trx(ha_thd())->duplicates &= ~TRX_DUP_REPLACE;
break; break;
case HA_EXTRA_NO_IGNORE_DUP_KEY:
thd_to_trx(ha_thd())->duplicates &=
~(TRX_DUP_IGNORE | TRX_DUP_REPLACE);
break;
default:/* Do nothing */ default:/* Do nothing */
; ;
} }
......
...@@ -1652,7 +1652,7 @@ row_ins_scan_sec_index_for_duplicate( ...@@ -1652,7 +1652,7 @@ row_ins_scan_sec_index_for_duplicate(
ulint n_fields_cmp; ulint n_fields_cmp;
btr_pcur_t pcur; btr_pcur_t pcur;
ulint err = DB_SUCCESS; ulint err = DB_SUCCESS;
unsigned allow_duplicates; ulint allow_duplicates;
mtr_t mtr; mtr_t mtr;
mem_heap_t* heap = NULL; mem_heap_t* heap = NULL;
ulint offsets_[REC_OFFS_NORMAL_SIZE]; ulint offsets_[REC_OFFS_NORMAL_SIZE];
...@@ -1683,7 +1683,7 @@ row_ins_scan_sec_index_for_duplicate( ...@@ -1683,7 +1683,7 @@ row_ins_scan_sec_index_for_duplicate(
btr_pcur_open(index, entry, PAGE_CUR_GE, BTR_SEARCH_LEAF, &pcur, &mtr); btr_pcur_open(index, entry, PAGE_CUR_GE, BTR_SEARCH_LEAF, &pcur, &mtr);
allow_duplicates = thr_get_trx(thr)->duplicates & TRX_DUP_IGNORE; allow_duplicates = thr_get_trx(thr)->duplicates;
/* Scan index records and check if there is a duplicate */ /* Scan index records and check if there is a duplicate */
...@@ -1817,7 +1817,7 @@ row_ins_duplicate_error_in_clust( ...@@ -1817,7 +1817,7 @@ row_ins_duplicate_error_in_clust(
sure that in roll-forward we get the same duplicate sure that in roll-forward we get the same duplicate
errors as in original execution */ errors as in original execution */
if (trx->duplicates & TRX_DUP_IGNORE) { if (trx->duplicates) {
/* If the SQL-query will update or replace /* If the SQL-query will update or replace
duplicate key we will take X-lock for duplicate key we will take X-lock for
...@@ -1861,7 +1861,7 @@ row_ins_duplicate_error_in_clust( ...@@ -1861,7 +1861,7 @@ row_ins_duplicate_error_in_clust(
offsets = rec_get_offsets(rec, cursor->index, offsets, offsets = rec_get_offsets(rec, cursor->index, offsets,
ULINT_UNDEFINED, &heap); ULINT_UNDEFINED, &heap);
if (trx->duplicates & TRX_DUP_IGNORE) { if (trx->duplicates) {
/* If the SQL-query will update or replace /* If the SQL-query will update or replace
duplicate key we will take X-lock for duplicate key we will take X-lock for
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment