From 93cfa3f7737a27c9c49f419bbc9cefa9a48e2848 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= <marko.makela@mariadb.com>
Date: Mon, 20 May 2019 13:49:17 +0300
Subject: [PATCH] MDEV-18518 WIP: Remove table_id

Note: This patch only identifies places that will have to be adjusted
in MDEV-18518. We should not remove these fields; they will remain
relevant for upgrading from older versions.
---
 storage/innobase/dict/dict0crea.cc        | 39 +----------------------
 storage/innobase/handler/ha_innodb.cc     |  3 +-
 storage/innobase/handler/handler0alter.cc |  5 ---
 storage/innobase/include/trx0trx.h        |  2 --
 storage/innobase/include/trx0undo.h       |  6 ++--
 storage/innobase/row/row0import.cc        |  5 ---
 storage/innobase/row/row0mysql.cc         |  1 -
 storage/innobase/trx/trx0roll.cc          | 12 -------
 storage/innobase/trx/trx0trx.cc           |  4 ---
 storage/innobase/trx/trx0undo.cc          | 13 --------
 10 files changed, 5 insertions(+), 85 deletions(-)

diff --git a/storage/innobase/dict/dict0crea.cc b/storage/innobase/dict/dict0crea.cc
index d8b9bcdaf39..426f869d26b 100644
--- a/storage/innobase/dict/dict0crea.cc
+++ b/storage/innobase/dict/dict0crea.cc
@@ -352,12 +352,10 @@ dict_build_table_def_step(
 {
 	ut_ad(mutex_own(&dict_sys->mutex));
 	dict_table_t*	table = node->table;
-	trx_t* trx = thr_get_trx(thr);
 	ut_ad(!table->is_temporary());
 	ut_ad(!table->space);
 	ut_ad(table->space_id == ULINT_UNDEFINED);
 	dict_hdr_get_new_id(&table->id, NULL, NULL);
-	trx->table_id = table->id;
 
 	/* Always set this bit for all new created tables */
 	DICT_TF2_FLAG_SET(table, DICT_TF2_FTS_AUX_HEX_NAME);
@@ -370,31 +368,6 @@ dict_build_table_def_step(
 
 		ut_ad(DICT_TF_GET_ZIP_SSIZE(table->flags) == 0
 		      || dict_table_has_atomic_blobs(table));
-		mtr_t mtr;
-		trx_undo_t* undo = trx->rsegs.m_redo.undo;
-		if (undo && !undo->table_id
-		    && trx_get_dict_operation(trx) == TRX_DICT_OP_TABLE) {
-			/* This must be a TRUNCATE operation where
-			the empty table is created after the old table
-			was renamed. Be sure to mark the transaction
-			associated with the new empty table, so that
-			we can remove it on recovery. */
-			mtr.start();
-			undo->table_id = trx->table_id;
-			undo->dict_operation = TRUE;
-			page_t* page = trx_undo_page_get(
-				page_id_t(trx->rsegs.m_redo.rseg->space->id,
-					  undo->hdr_page_no),
-				&mtr);
-			mlog_write_ulint(page + undo->hdr_offset
-					 + TRX_UNDO_DICT_TRANS,
-					 TRUE, MLOG_1BYTE, &mtr);
-			mlog_write_ull(page + undo->hdr_offset
-				       + TRX_UNDO_TABLE_ID,
-				       trx->table_id, &mtr);
-			mtr.commit();
-			log_write_up_to(mtr.commit_lsn(), true);
-		}
 		/* Get a new tablespace ID */
 		ulint space_id;
 		dict_hdr_get_new_id(NULL, NULL, &space_id);
@@ -440,6 +413,7 @@ dict_build_table_def_step(
 		}
 
 		table->space_id = space_id;
+		mtr_t mtr;
 		mtr.start();
 		mtr.set_named_space(table->space);
 		fsp_header_init(table->space, FIL_IBD_FILE_INITIAL_SIZE, &mtr);
@@ -737,11 +711,6 @@ dict_build_index_def_step(
 		return(DB_TABLE_NOT_FOUND);
 	}
 
-	if (!trx->table_id) {
-		/* Record only the first table id. */
-		trx->table_id = table->id;
-	}
-
 	ut_ad((UT_LIST_GET_LEN(table->indexes) > 0)
 	      || dict_index_is_clust(index));
 
@@ -776,12 +745,6 @@ dict_build_index_def(
 	trx_t*			trx)	/*!< in/out: InnoDB transaction handle */
 {
 	ut_ad(mutex_own(&dict_sys->mutex));
-
-	if (trx->table_id == 0) {
-		/* Record only the first table id. */
-		trx->table_id = table->id;
-	}
-
 	ut_ad((UT_LIST_GET_LEN(table->indexes) > 0)
 	      || dict_index_is_clust(index));
 
diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
index 04fdb0ff3b2..171efe52a1e 100644
--- a/storage/innobase/handler/ha_innodb.cc
+++ b/storage/innobase/handler/ha_innodb.cc
@@ -11147,8 +11147,7 @@ create_table_info_t::create_table_def()
 	dict_table_add_system_columns(table, heap);
 
 	if (table->is_temporary()) {
-		m_trx->table_id = table->id
-			= dict_sys->get_temporary_table_id();
+		table->id = dict_sys->get_temporary_table_id();
 		ut_ad(dict_tf_get_rec_format(table->flags)
 		      != REC_FORMAT_COMPRESSED);
 		table->space_id = SRV_TMP_SPACE_ID;
diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc
index 403dae8334d..89836a074c8 100644
--- a/storage/innobase/handler/handler0alter.cc
+++ b/storage/innobase/handler/handler0alter.cc
@@ -4055,9 +4055,6 @@ online_retry_drop_indexes_with_trx(
 	drop any incompletely created indexes that may have been left
 	behind in rollback_inplace_alter_table() earlier. */
 	if (table->drop_aborted) {
-
-		trx->table_id = 0;
-
 		trx_start_for_ddl(trx, TRX_DICT_OP_INDEX);
 
 		online_retry_drop_indexes_low(table, trx);
@@ -6736,7 +6733,6 @@ prepare_inplace_alter_table_dict(
 			holding dict_operation_lock X-latch. */
 			DBUG_ASSERT(ctx->new_table->get_ref_count() == 1);
 			DBUG_ASSERT(ctx->new_table->id != 0);
-			DBUG_ASSERT(ctx->new_table->id == ctx->trx->table_id);
 			break;
 		case DB_TABLESPACE_EXISTS:
 			my_error(ER_TABLESPACE_EXISTS, MYF(0),
@@ -6843,7 +6839,6 @@ prepare_inplace_alter_table_dict(
 		}
 	} else if (ctx->num_to_add_index) {
 		ut_ad(!ctx->is_instant());
-		ctx->trx->table_id = user_table->id;
 
 		for (ulint a = 0; a < ctx->num_to_add_index; a++) {
 			dict_index_t* index = ctx->add_index[a];
diff --git a/storage/innobase/include/trx0trx.h b/storage/innobase/include/trx0trx.h
index ee5f1c4aefb..8e1716d8979 100644
--- a/storage/innobase/include/trx0trx.h
+++ b/storage/innobase/include/trx0trx.h
@@ -932,8 +932,6 @@ struct trx_t {
 	ib_uint64_t	start_time_micro; /*!< start time of transaction in
 					microseconds */
 	lsn_t		commit_lsn;	/*!< lsn at the time of the commit */
-	table_id_t	table_id;	/*!< Table to drop iff dict_operation
-					== TRX_DICT_OP_TABLE, or 0. */
 	/*------------------------------*/
 	THD*		mysql_thd;	/*!< MySQL thread handle corresponding
 					to this trx, or NULL */
diff --git a/storage/innobase/include/trx0undo.h b/storage/innobase/include/trx0undo.h
index f3c52fff7b5..a19ffb40c73 100644
--- a/storage/innobase/include/trx0undo.h
+++ b/storage/innobase/include/trx0undo.h
@@ -1,7 +1,7 @@
 /*****************************************************************************
 
 Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
-Copyright (c) 2017, 2018, MariaDB Corporation.
+Copyright (c) 2017, 2019, MariaDB Corporation.
 
 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
@@ -335,8 +335,6 @@ struct trx_undo_t {
 	XID		xid;		/*!< X/Open XA transaction
 					identification */
 	ibool		dict_operation;	/*!< TRUE if a dict operation trx */
-	table_id_t	table_id;	/*!< if a dict operation, then the table
-					id */
 	trx_rseg_t*	rseg;		/*!< rseg where the undo log belongs */
 	/*-----------------------------*/
 	ulint		hdr_page_no;	/*!< page number of the header page in
@@ -472,8 +470,10 @@ which purge would not result in removing delete-marked records. */
 					in the usual way: a 'rollback' rather
 					means dropping the created or dropped
 					table, if it still exists */
+#if 0
 #define TRX_UNDO_TABLE_ID	22	/*!< Id of the table if the preceding
 					field is TRUE */
+#endif
 #define	TRX_UNDO_NEXT_LOG	30	/*!< Offset of the next undo log header
 					on this page, 0 if none */
 #define	TRX_UNDO_PREV_LOG	32	/*!< Offset of the previous undo log
diff --git a/storage/innobase/row/row0import.cc b/storage/innobase/row/row0import.cc
index 9ef737828a0..91cbaa4d74c 100644
--- a/storage/innobase/row/row0import.cc
+++ b/storage/innobase/row/row0import.cc
@@ -3823,11 +3823,6 @@ row_import_for_mysql(
 	/* So that we can send error messages to the user. */
 	trx->mysql_thd = prebuilt->trx->mysql_thd;
 
-	/* Ensure that the table will be dropped by trx_rollback_active()
-	in case of a crash. */
-
-	trx->table_id = table->id;
-
 	/* Assign an undo segment for the transaction, so that the
 	transaction will be recovered after a crash. */
 
diff --git a/storage/innobase/row/row0mysql.cc b/storage/innobase/row/row0mysql.cc
index afbcdfe4423..591416d4cfc 100644
--- a/storage/innobase/row/row0mysql.cc
+++ b/storage/innobase/row/row0mysql.cc
@@ -3583,7 +3583,6 @@ row_drop_table_for_mysql(
 	switch (trx_get_dict_operation(trx)) {
 	case TRX_DICT_OP_NONE:
 		trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
-		trx->table_id = table->id;
 	case TRX_DICT_OP_TABLE:
 		break;
 	case TRX_DICT_OP_INDEX:
diff --git a/storage/innobase/trx/trx0roll.cc b/storage/innobase/trx/trx0roll.cc
index a64097fb0fd..ed1f6d7fc42 100644
--- a/storage/innobase/trx/trx0roll.cc
+++ b/storage/innobase/trx/trx0roll.cc
@@ -674,18 +674,6 @@ trx_rollback_active(
 
 	ut_a(trx->lock.que_state == TRX_QUE_RUNNING);
 
-	if (!dictionary_locked || !trx->table_id) {
-	} else if (dict_table_t* table = dict_table_open_on_id(
-			   trx->table_id, TRUE, DICT_TABLE_OP_NORMAL)) {
-		ib::info() << "Dropping table " << table->name
-			   << ", with id " << trx->table_id
-			   << " in recovery";
-
-		dict_table_close_and_drop(trx, table);
-
-		trx_commit_for_mysql(trx);
-	}
-
 	ib::info() << "Rolled back recovered transaction " << trx_id;
 
 func_exit:
diff --git a/storage/innobase/trx/trx0trx.cc b/storage/innobase/trx/trx0trx.cc
index 7ef6b88a9a9..c9e26f360b2 100644
--- a/storage/innobase/trx/trx0trx.cc
+++ b/storage/innobase/trx/trx0trx.cc
@@ -123,8 +123,6 @@ trx_init(
 
 	trx->dict_operation = TRX_DICT_OP_NONE;
 
-	trx->table_id = 0;
-
 	trx->error_state = DB_SUCCESS;
 
 	trx->error_key_num = ULINT_UNDEFINED;
@@ -666,8 +664,6 @@ static void trx_resurrect(trx_undo_t *undo, trx_rseg_t *rseg,
   if (undo->dict_operation)
   {
     trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
-    if (!trx->table_id)
-      trx->table_id= undo->table_id;
   }
 
   trx_sys.rw_trx_hash.insert(trx);
diff --git a/storage/innobase/trx/trx0undo.cc b/storage/innobase/trx/trx0undo.cc
index dbd45979c93..91f8f7181c5 100644
--- a/storage/innobase/trx/trx0undo.cc
+++ b/storage/innobase/trx/trx0undo.cc
@@ -1114,7 +1114,6 @@ trx_undo_mem_create_at_db_start(trx_rseg_t* rseg, ulint id, ulint page_no,
 	mutex_exit(&rseg->mutex);
 
 	undo->dict_operation = undo_header[TRX_UNDO_DICT_TRANS];
-	undo->table_id = mach_read_from_8(undo_header + TRX_UNDO_TABLE_ID);
 	undo->size = flst_get_len(TRX_UNDO_SEG_HDR + TRX_UNDO_PAGE_LIST
 				  + undo_page);
 
@@ -1293,16 +1292,10 @@ trx_undo_create(trx_t* trx, trx_rseg_t* rseg, trx_undo_t** undo,
 	case TRX_DICT_OP_NONE:
 		break;
 	case TRX_DICT_OP_INDEX:
-		/* Do not discard the table on recovery. */
-		trx->table_id = 0;
-		/* fall through */
 	case TRX_DICT_OP_TABLE:
-		(*undo)->table_id = trx->table_id;
 		(*undo)->dict_operation = TRUE;
 		mlog_write_ulint(block->frame + offset + TRX_UNDO_DICT_TRANS,
 				 TRUE, MLOG_1BYTE, mtr);
-		mlog_write_ull(block->frame + offset + TRX_UNDO_TABLE_ID,
-			       trx->table_id, mtr);
 	}
 
 	*err = DB_SUCCESS;
@@ -1371,16 +1364,10 @@ trx_undo_reuse_cached(trx_t* trx, trx_rseg_t* rseg, trx_undo_t** pundo,
 	case TRX_DICT_OP_NONE:
 		return block;
 	case TRX_DICT_OP_INDEX:
-		/* Do not discard the table on recovery. */
-		trx->table_id = 0;
-		/* fall through */
 	case TRX_DICT_OP_TABLE:
-		undo->table_id = trx->table_id;
 		undo->dict_operation = TRUE;
 		mlog_write_ulint(block->frame + offset + TRX_UNDO_DICT_TRANS,
 				 TRUE, MLOG_1BYTE, mtr);
-		mlog_write_ull(block->frame + offset + TRX_UNDO_TABLE_ID,
-			       trx->table_id, mtr);
 	}
 
 	return block;
-- 
2.20.1

