Bug #105903 Contribution by Tencent: data is incorrect after instant add column
Submitted: 15 Dec 2021 9:23 Modified: 23 Dec 2021 9:36
Reporter: zhang simon (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DDL Severity:S2 (Serious)
Version:8.0.12 and prior OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution

[15 Dec 2021 9:23] zhang simon
Description:
data is incorrect after instant add column

How to repeat:
CREATE TABLE t1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, a char(10));
INSERT INTO t1 VALUES(1, 'aa');
INSERT INTO t1 VALUES(2, 'bb');
alter table t1 add b varchar(20), algorithm=instant; //use instant
INSERT INTO t1 VALUES(3, 'cc', 'dd');
checksum table t1;
Table   Checksum
test.t1 1066571161
drop table t1;
CREATE TABLE t1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, a char(10));
INSERT INTO t1 VALUES(1, 'aa');
INSERT INTO t1 VALUES(2, 'bb');
alter table t1 add b varchar(20), algorithm=copy;  // use copy
INSERT INTO t1 VALUES(3, 'cc', 'dd');
checksum table t1;
Table   Checksum
test.t1 415156457 //mismatch 
drop table t1;

Suggested fix:
pack_record option should not reset after instant ddl.

--- a/storage/innobase/handler/handler0alter.cc
+++ b/storage/innobase/handler/handler0alter.cc
@@ -4126,7 +4126,19 @@ static void dd_commit_inplace_instant(Alter_inplace_info *ha_alter_info,
       row_mysql_unlock_data_dictionary(trx);
       break;
     case Instant_Type::INSTANT_ADD_COLUMN:
-      dd_copy_private(*new_dd_tab, *old_dd_tab);
+      {
+        dd::Properties *table_options = &new_dd_tab->options();
+        bool pack_record;
+        /* pack_record will change after add variable
+          length column */
+        if (table_options->exists("pack_record")) {
+          table_options->get("pack_record", &pack_record);
+        }
+        dd_copy_private(*new_dd_tab, *old_dd_tab);
+        if (table_options->exists("pack_record")) {
+          table_options->set("pack_record", pack_record);
+        }
+      }
 
       if (!dd_table_is_partitioned(new_dd_tab->table()) ||
           dd_part_is_first(reinterpret_cast<dd::Partition *>(new_dd_tab))) {
[15 Dec 2021 9:50] MySQL Verification Team
Hello zhang simon,

Thank you for the report and test case.

Thanks,
Umesh
[15 Dec 2021 10:00] MySQL Verification Team
Hello Zhang,

Please ensure to re-send the patch via "Contribution" tab. Otherwise we would not be able to accept it. Thank you.

regards,
Umesh
[15 Dec 2021 13:41] zhang simon
bugfix for instant add column 

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: instant.patch (application/octet-stream, text), 1.09 KiB.

[15 Dec 2021 13:41] zhang simon
bugfix for instant add column 

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: instant.patch (application/octet-stream, text), 1.09 KiB.

[23 Dec 2021 9:36] zhang simon
modify Synopsis and severity