mirror of
https://github.com/MariaDB/server.git
synced 2025-11-28 17:36:30 +03:00
- InnoDB DDL results in `Duplicate entry' if concurrent DML throws duplicate key error. The following scenario explains the problem connection con1: ALTER TABLE t1 FORCE; connection con2: INSERT INTO t1(pk, uk) VALUES (2, 2), (3, 2); In connection con2, InnoDB throws the 'DUPLICATE KEY' error because of unique index. Alter operation will throw the error when applying the concurrent DML log. - Inserting the duplicate key for unique index logs the insert operation for online ALTER TABLE. When insertion fails, transaction does rollback and it leads to logging of delete operation for online ALTER TABLE. While applying the insert log entries, alter operation encounters 'DUPLICATE KEY' error. - To avoid the above fake duplicate scenario, InnoDB should not write any log for online ALTER TABLE before DML transaction commit. - User thread which does DML can apply the online log if InnoDB ran out of online log and index is marked as completed. Set online log error if apply phase encountered any error. It can also clear all other indexes log, marks the newly added indexes as corrupted. - Removed the old online code which was a part of DML operations commit_inplace_alter_table() : Does apply the online log for the last batch of secondary index log and does frees the log for the completed index. trx_t::apply_online_log: Set to true while writing the undo log if the modified table has active DDL trx_t::apply_log(): Apply the DML changes to online DDL tables dict_table_t::is_active_ddl(): Returns true if the table has an active DDL dict_index_t::online_log_make_dummy(): Assign dummy value for clustered index online log to indicate the secondary indexes are being rebuild. dict_index_t::online_log_is_dummy(): Check whether the online log has dummy value ha_innobase_inplace_ctx::log_failure(): Handle the apply log failure for online DDL transaction row_log_mark_other_online_index_abort(): Clear out all other online index log after encountering the error during row_log_apply() row_log_get_error(): Get the error happened during row_log_apply() row_log_online_op(): Does apply the online log if index is completed and ran out of memory. Returns false if apply log fails UndorecApplier: Introduced a class to maintain the undo log record, latched undo buffer page, parse the undo log record, maintain the undo record type, info bits and update vector UndorecApplier::get_old_rec(): Get the correct version of the clustered index record that was modified by the current undo log record UndorecApplier::clear_undo_rec(): Clear the undo log related information after applying the undo log record UndorecApplier::log_update(): Handle the update, delete undo log and apply it on online indexes UndorecApplier::log_insert(): Handle the insert undo log and apply it on online indexes UndorecApplier::is_same(): Check whether the given roll pointer is generated by the current undo log record information trx_t::rollback_low(): Set apply_online_log for the transaction after partially rollbacked transaction has any active DDL prepare_inplace_alter_table_dict(): After allocating the online log, InnoDB does create fulltext common tables. Fulltext index doesn't allow the index to be online. So removed the dead code of online log removal Thanks to Marko Mäkelä for providing the initial prototype and Matthias Leich for testing the issue patiently.
127 lines
3.8 KiB
Plaintext
127 lines
3.8 KiB
Plaintext
set default_storage_engine=innodb;
|
|
CREATE TABLE `t` (
|
|
`a` VARCHAR(100),
|
|
`b` VARCHAR(100),
|
|
`c` VARCHAR(200) GENERATED ALWAYS AS (CONCAT(a,b)) VIRTUAL,
|
|
`h` VARCHAR(10) DEFAULT NULL,
|
|
`i` int
|
|
) ENGINE=InnoDB;
|
|
INSERT INTO t VALUES (REPEAT('g', 100), REPEAT('x', 10), DEFAULT, "kk", 1);
|
|
INSERT INTO t VALUES (REPEAT('a', 100), REPEAT('b', 100), DEFAULT, "mm", 2);
|
|
CREATE INDEX idx ON t(c(100));
|
|
SET session debug_dbug="+d,ib_alter_add_virtual_fail";
|
|
ALTER TABLE t ADD COLUMN x VARCHAR(200) GENERATED ALWAYS AS (a) VIRTUAL,
|
|
ALGORITHM = INPLACE;
|
|
ERROR 42000: The storage engine InnoDB can't index column `x`
|
|
ALTER TABLE t DROP COLUMN c, ALGORITHM = INPLACE;
|
|
ERROR 42000: The storage engine InnoDB can't index column `c`
|
|
SET session debug_dbug="";
|
|
DROP TABLE t;
|
|
CREATE TABLE t (a INT, b INT, c INT GENERATED ALWAYS AS(a+b), h VARCHAR(10));
|
|
INSERT INTO t VALUES (11, 3, DEFAULT, 'mm');
|
|
INSERT INTO t VALUES (18, 1, DEFAULT, 'mm');
|
|
INSERT INTO t VALUES (28, 1, DEFAULT, 'mm');
|
|
INSERT INTO t VALUES (null, null, DEFAULT, "mx");
|
|
SET DEBUG_SYNC = 'innodb_inplace_alter_table_enter SIGNAL start_create WAIT_FOR go_ahead';
|
|
CREATE INDEX idx ON t(c);
|
|
connect con1,localhost,root,,;
|
|
SET DEBUG_SYNC = 'now WAIT_FOR start_create';
|
|
update t set a=0 where a = 11;
|
|
start transaction;
|
|
update t set a=1 where a = 0;
|
|
ROLLBACK;
|
|
SET DEBUG_SYNC = 'now SIGNAL go_ahead';
|
|
connection default;
|
|
SELECT c FROM t;
|
|
c
|
|
NULL
|
|
3
|
|
19
|
|
29
|
|
SHOW CREATE TABLE t;
|
|
Table Create Table
|
|
t CREATE TABLE `t` (
|
|
`a` int(11) DEFAULT NULL,
|
|
`b` int(11) DEFAULT NULL,
|
|
`c` int(11) GENERATED ALWAYS AS (`a` + `b`) VIRTUAL,
|
|
`h` varchar(10) DEFAULT NULL,
|
|
KEY `idx` (`c`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1
|
|
SELECT * FROM t;
|
|
a b c h
|
|
0 3 3 mm
|
|
18 1 19 mm
|
|
28 1 29 mm
|
|
NULL NULL NULL mx
|
|
ALTER TABLE t FORCE, LOCK=NONE;
|
|
ERROR 0A000: LOCK=NONE is not supported. Reason: online rebuild with indexed virtual columns. Try LOCK=SHARED
|
|
disconnect con1;
|
|
DROP TABLE t;
|
|
SET DEBUG_SYNC = 'RESET';
|
|
CREATE TABLE t (a INT, b INT, c INT GENERATED ALWAYS AS(a+b), h VARCHAR(10));
|
|
INSERT INTO t VALUES (11, 3, DEFAULT, 'mm');
|
|
INSERT INTO t VALUES (18, 1, DEFAULT, 'mm');
|
|
INSERT INTO t VALUES (28, 1, DEFAULT, 'mm');
|
|
INSERT INTO t VALUES (null, null, DEFAULT, 'mm');
|
|
CREATE INDEX idx_1 on t(c);
|
|
SET @saved_dbug = @@SESSION.debug_dbug;
|
|
SET debug_dbug = '+d,create_index_fail';
|
|
ALTER TABLE t ADD COLUMN x INT GENERATED ALWAYS AS(a+b), ADD INDEX idx (x),
|
|
ADD INDEX idcx (c,x);
|
|
ERROR 23000: Duplicate entry '' for key '*UNKNOWN*'
|
|
UPDATE t SET a=a+1;
|
|
affected rows: 3
|
|
info: Rows matched: 4 Changed: 3 Warnings: 0
|
|
ALTER TABLE t ADD INDEX idc(c);
|
|
ERROR 23000: Duplicate entry '' for key '*UNKNOWN*'
|
|
SET debug_dbug = @saved_dbug;
|
|
affected rows: 0
|
|
UPDATE t SET b=b-1;
|
|
SHOW CREATE TABLE t;
|
|
Table Create Table
|
|
t CREATE TABLE `t` (
|
|
`a` int(11) DEFAULT NULL,
|
|
`b` int(11) DEFAULT NULL,
|
|
`c` int(11) GENERATED ALWAYS AS (`a` + `b`) VIRTUAL,
|
|
`h` varchar(10) DEFAULT NULL,
|
|
KEY `idx_1` (`c`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1
|
|
SELECT c FROM t;
|
|
c
|
|
NULL
|
|
14
|
|
19
|
|
29
|
|
DROP TABLE t;
|
|
SET DEBUG_SYNC = 'RESET';
|
|
#
|
|
# Bug#28825718 - ASSERTION FAILURE: TRX0REC.CC:NNN:N_IDX > 0 WHILE DOING REPLACE/INSERT
|
|
#
|
|
CREATE TABLE t1(a INT PRIMARY KEY, b INT, c INT GENERATED ALWAYS AS(b+1) VIRTUAL) ENGINE=InnoDB;
|
|
INSERT INTO t1(a, b) VALUES(1, 1);
|
|
connect con1,localhost,root,,;
|
|
SET DEBUG_SYNC = 'row_log_apply_after SIGNAL s1 WAIT_FOR s2';
|
|
SET lock_wait_timeout = 1;
|
|
ALTER TABLE t1 ADD UNIQUE INDEX(c, b);
|
|
connection default;
|
|
SET DEBUG_SYNC = 'now WAIT_FOR s1';
|
|
SET DEBUG_SYNC = 'row_log_insert_handle SIGNAL s2 WAIT_FOR s3';
|
|
INSERT INTO t1(a, b) VALUES(2, 2);
|
|
connection con1;
|
|
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
|
|
SET DEBUG_SYNC = 'now SIGNAL s3';
|
|
disconnect con1;
|
|
connection default;
|
|
SET DEBUG_SYNC = 'RESET';
|
|
ALTER TABLE t1 ADD KEY(b);
|
|
INSERT INTO t1(a, b) VALUES(3, 3);
|
|
SELECT * FROM t1;
|
|
a b c
|
|
1 1 2
|
|
2 2 3
|
|
3 3 4
|
|
CHECK TABLE t1;
|
|
Table Op Msg_type Msg_text
|
|
test.t1 check status OK
|
|
DROP TABLE t1;
|