InnoDB is adding both persistent and temporary tables to a dict_sys->table_hash, which maps table names to dict_table_t.
We should only add persistent tables to the hash table, and we should not store any dict_table_t::name for temporary tables. It would be nice to store the user-specified table name instead of some #sql name, but RENAME TABLE is not being propagated to temporary tables.
The reason why we currently need the table name lookup is that handler::delete_table() can be invoked on temporary tables without prior handler::open(), so the only information that is available is the table name.
If THD::rm_temporary_table() were invoked with TABLE_SHARE, we could store the dict_table_t* or dict_table_t::id there by invoking handler::set_ha_share_ptr() and handler::get_ha_share_ptr().
For persistent tables, we will need both name and ID based lookups for the time being. (InnoDB rollback and purge will look up tables by ID, and the SQL layer will have to look up persistent tables by name.)
As part of this task, please ensure that the table name printed in errors will be the real table name, not the temporary table name:
InnoDB: Cannot add field `NEW` in table `tmp`.`#sql-temptable-10e7-7aa21-13bb` because after adding it, the row size is 8204 which is greater than maximum allowed size (8126 bytes) for a record