|
| 1 | +# |
| 2 | +# Index consistency after a failed UPDATE on a HEAP table |
| 3 | +# |
| 4 | +# heap_update() moves the changed key entries to their new key values |
| 5 | +# before it updates the record. A failure raised after that has to |
| 6 | +# move them back. An rb-tree node allocation failure used to be |
| 7 | +# reported as a duplicate key, and when the recovery itself cannot |
| 8 | +# restore a key the table is now marked crashed, so that no statement |
| 9 | +# can use an index that no longer describes the data. |
| 10 | +# |
| 11 | +# The keyword lists armed below always carry the inert |
| 12 | +# "heap_test_guard" keyword: the one-shot injection removes its own |
| 13 | +# keyword when it fires, and a keyword list that becomes empty while |
| 14 | +# debugging is on matches every keyword. |
| 15 | +# |
| 16 | +CALL mtr.add_suppression("Index for table .t1. is corrupt"); |
| 17 | +# |
| 18 | +# The hash key k1 is moved to the new value, then the rb-tree node |
| 19 | +# allocation for k2 fails once: the failure is reported as out of |
| 20 | +# memory, not as a duplicate, and both keys are moved back |
| 21 | +# |
| 22 | +CREATE TABLE t1 ( |
| 23 | +id INT, |
| 24 | +a INT, |
| 25 | +b INT, |
| 26 | +KEY k1 (a), |
| 27 | +KEY k2 (b) USING BTREE |
| 28 | +) ENGINE=HEAP; |
| 29 | +INSERT INTO t1 VALUES (1, 10, 100), (2, 20, 200); |
| 30 | +SET SESSION debug_dbug="+d,once_simulate_tree_insert_oom,heap_test_guard"; |
| 31 | +UPDATE t1 SET a= 11, b= 101 WHERE id = 1; |
| 32 | +ERROR HY000: Out of memory. |
| 33 | +SET SESSION debug_dbug=""; |
| 34 | +CHECK TABLE t1; |
| 35 | +Table Op Msg_type Msg_text |
| 36 | +test.t1 check status OK |
| 37 | +# Both indexes must still find the row under its unchanged values |
| 38 | +SELECT id, a, b FROM t1 FORCE INDEX(k1) WHERE a = 10; |
| 39 | +id a b |
| 40 | +1 10 100 |
| 41 | +SELECT id, a, b FROM t1 FORCE INDEX(k2) WHERE b = 100; |
| 42 | +id a b |
| 43 | +1 10 100 |
| 44 | +SELECT COUNT(*) FROM t1 FORCE INDEX(k1) WHERE a = 11; |
| 45 | +COUNT(*) |
| 46 | +0 |
| 47 | +SELECT COUNT(*) FROM t1 FORCE INDEX(k2) WHERE b = 101; |
| 48 | +COUNT(*) |
| 49 | +0 |
| 50 | +SELECT id, a, b FROM t1 ORDER BY id; |
| 51 | +id a b |
| 52 | +1 10 100 |
| 53 | +2 20 200 |
| 54 | +# |
| 55 | +# A duplicate key is still reported as a duplicate |
| 56 | +# |
| 57 | +CREATE TABLE t2 ( |
| 58 | +id INT, |
| 59 | +a INT, |
| 60 | +UNIQUE KEY k1 (a) USING BTREE |
| 61 | +) ENGINE=HEAP; |
| 62 | +INSERT INTO t2 VALUES (1, 10), (2, 20); |
| 63 | +UPDATE t2 SET a= 20 WHERE id = 1; |
| 64 | +ERROR 23000: Duplicate entry '20' for key 'k1' |
| 65 | +CHECK TABLE t2; |
| 66 | +Table Op Msg_type Msg_text |
| 67 | +test.t2 check status OK |
| 68 | +SELECT id, a FROM t2 ORDER BY id; |
| 69 | +id a |
| 70 | +1 10 |
| 71 | +2 20 |
| 72 | +DROP TABLE t2; |
| 73 | +# |
| 74 | +# The allocation failure persists, so restoring the old k2 entry |
| 75 | +# fails as well: the already moved k1 no longer describes the |
| 76 | +# record, the table is marked crashed, and every read and write on |
| 77 | +# it is refused until it is emptied |
| 78 | +# |
| 79 | +SET SESSION debug_dbug="+d,simulate_tree_insert_oom,heap_test_guard"; |
| 80 | +UPDATE t1 SET a= 11, b= 101 WHERE id = 1; |
| 81 | +ERROR HY000: Out of memory. |
| 82 | +SET SESSION debug_dbug=""; |
| 83 | +CHECK TABLE t1; |
| 84 | +Table Op Msg_type Msg_text |
| 85 | +test.t1 check error Corrupt |
| 86 | +SELECT id, a, b FROM t1 ORDER BY id; |
| 87 | +ERROR HY000: Index for table 't1' is corrupt; try to repair it |
| 88 | +SELECT id, a, b FROM t1 FORCE INDEX(k1) WHERE a = 10; |
| 89 | +ERROR HY000: Index for table 't1' is corrupt; try to repair it |
| 90 | +INSERT INTO t1 VALUES (3, 30, 300); |
| 91 | +ERROR HY000: Index for table 't1' is corrupt; try to repair it |
| 92 | +UPDATE t1 SET a= 12 WHERE id = 2; |
| 93 | +ERROR HY000: Index for table 't1' is corrupt; try to repair it |
| 94 | +DELETE FROM t1 WHERE id = 2; |
| 95 | +ERROR HY000: Index for table 't1' is corrupt; try to repair it |
| 96 | +# Emptying the table rebuilds the indexes from nothing |
| 97 | +TRUNCATE TABLE t1; |
| 98 | +CHECK TABLE t1; |
| 99 | +Table Op Msg_type Msg_text |
| 100 | +test.t1 check status OK |
| 101 | +INSERT INTO t1 VALUES (5, 50, 500); |
| 102 | +SELECT id, a, b FROM t1 FORCE INDEX(k2) WHERE b = 500; |
| 103 | +id a b |
| 104 | +5 50 500 |
| 105 | +DROP TABLE t1; |
0 commit comments