commit d1ee0311db83467ffd40eeac1ab6441edc5e2439
Author: Thirunarayanan Balathandayuthapani <thiru@mariadb.com>
Date:   Mon May 4 15:27:24 2020 +0530

    MDEV-22446 InnoDB aborts while adding instant column for discarded tablespace
    
    - Instant alter should change the metadata alone when table is
    discarded. It shouldn't try to add metadata record in clustered index.
    - btr_cur_instant_root_init() can get index in instant format when
    InnoDB tries to import instant table.

diff --git a/mysql-test/suite/innodb/r/instant_alter_bugs.result b/mysql-test/suite/innodb/r/instant_alter_bugs.result
index d07067fa19f..6f4b0c10521 100644
--- a/mysql-test/suite/innodb/r/instant_alter_bugs.result
+++ b/mysql-test/suite/innodb/r/instant_alter_bugs.result
@@ -169,4 +169,35 @@ DROP FOREIGN KEY fk1,
 CHANGE b d INT UNSIGNED,
 ADD c INT;
 DROP TABLE t2, t1;
+#
+# MDEV-22446 InnoDB aborts while adding instant column
+# for discarded tablespace
+CREATE TABLE t1(c1 INT NOT NULL, c2 INT NOT NULL DEFAULT 0)ENGINE=InnoDB;
+INSERT INTO t1(c1) VALUES(1);
+ALTER TABLE t1 ADD COLUMN c3 INT DEFAULT 10;
+FLUSH TABLES t1 FOR EXPORT;
+backup: t1
+UNLOCK TABLES;
+DROP TABLE t1;
+CREATE TABLE t1(c1 INT NOT NULL)Engine=InnoDB;
+ALTER TABLE t1 DISCARD TABLESPACE;
+ALTER TABLE t1 ADD COLUMN c2 INT NOT NULL;
+Warnings:
+Warning	1814	Tablespace has been discarded for table `t1`
+ALTER TABLE t1 ADD COLUMN c3 INT DEFAULT 10;
+Warnings:
+Warning	1814	Tablespace has been discarded for table `t1`
+restore: t1 .ibd and .cfg files
+ALTER TABLE t1 IMPORT TABLESPACE;
+SHOW CREATE TABLE t1;
+Table	Create Table
+t1	CREATE TABLE `t1` (
+  `c1` int(11) NOT NULL,
+  `c2` int(11) NOT NULL,
+  `c3` int(11) DEFAULT 10
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+SELECT * FROM t1;
+c1	c2	c3
+1	0	10
+DROP TABLE t1;
 # End of 10.3 tests
diff --git a/mysql-test/suite/innodb/t/instant_alter_bugs.test b/mysql-test/suite/innodb/t/instant_alter_bugs.test
index a2626ba34a1..ac1daf64d31 100644
--- a/mysql-test/suite/innodb/t/instant_alter_bugs.test
+++ b/mysql-test/suite/innodb/t/instant_alter_bugs.test
@@ -170,4 +170,37 @@ ALTER TABLE t2
     ADD c INT;
 DROP TABLE t2, t1;
 
+--echo #
+--echo # MDEV-22446 InnoDB aborts while adding instant column
+--echo # for discarded tablespace
+
+let MYSQLD_DATADIR =`SELECT @@datadir`;
+CREATE TABLE t1(c1 INT NOT NULL, c2 INT NOT NULL DEFAULT 0)ENGINE=InnoDB;
+INSERT INTO t1(c1) VALUES(1);
+
+ALTER TABLE t1 ADD COLUMN c3 INT DEFAULT 10;
+--replace_regex /, .*\).*t1.cfg/, Bad file descriptor) t1.cfg/
+FLUSH TABLES t1 FOR EXPORT;
+
+perl;
+do "$ENV{MTR_SUITE_DIR}/include/innodb-util.pl";
+ib_backup_tablespaces("test", "t1");
+EOF
+UNLOCK TABLES;
+DROP TABLE t1;
+
+# Restore of instant table
+CREATE TABLE t1(c1 INT NOT NULL)Engine=InnoDB;
+ALTER TABLE t1 DISCARD TABLESPACE;
+ALTER TABLE t1 ADD COLUMN c2 INT NOT NULL;
+ALTER TABLE t1 ADD COLUMN c3 INT DEFAULT 10;
+# Restore files
+perl;
+do "$ENV{MTR_SUITE_DIR}/include/innodb-util.pl";
+ib_restore_tablespaces("test", "t1");
+EOF
+ALTER TABLE t1 IMPORT TABLESPACE;
+SHOW CREATE TABLE t1;
+SELECT * FROM t1;
+DROP TABLE t1;
 --echo # End of 10.3 tests
diff --git a/storage/innobase/btr/btr0cur.cc b/storage/innobase/btr/btr0cur.cc
index c79ea1c5467..94f612729f6 100644
--- a/storage/innobase/btr/btr0cur.cc
+++ b/storage/innobase/btr/btr0cur.cc
@@ -563,7 +563,6 @@ bool btr_cur_instant_root_init(dict_index_t* index, const page_t* page)
 	ut_ad(page_get_page_no(page) == index->page);
 	ut_ad(!page_is_comp(page) == !dict_table_is_comp(index->table));
 	ut_ad(index->is_primary());
-	ut_ad(!index->is_instant());
 	ut_ad(index->table->supports_instant());
 	/* This is normally executed as part of btr_cur_instant_init()
 	when dict_load_table_one() is loading a table definition.
@@ -571,7 +570,8 @@ bool btr_cur_instant_root_init(dict_index_t* index, const page_t* page)
 	n_core_fields before dict_load_table_one() returns.
 
 	This can also be executed during IMPORT TABLESPACE, where the
-	table definition is exclusively locked. */
+	table definition is exclusively locked. Also index could be in
+	instant format already */
 
 	switch (fil_page_get_type(page)) {
 	default:
diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc
index 0686c21c711..6c8244930cf 100644
--- a/storage/innobase/handler/handler0alter.cc
+++ b/storage/innobase/handler/handler0alter.cc
@@ -4441,6 +4441,11 @@ innobase_add_instant_try(
 		return true;
 	}
 
+	/* If the table has been discarded then change the metadata alone */
+	if (!user_table->space) {
+		return false;
+	}
+
 	unsigned i = unsigned(user_table->n_cols) - DATA_N_SYS_COLS;
 	byte trx_id[DATA_TRX_ID_LEN], roll_ptr[DATA_ROLL_PTR_LEN];
 	dfield_set_data(dtuple_get_nth_field(row, i++), field_ref_zero,
