2121
Mark all temporary tables which were used by the current statement or
2122
substatement as free for reuse, but only if the query_id can be cleared.
2124
@param session thread context
2126
@remark For temp tables associated with a open SQL HANDLER the query_id
2127
is not reset until the HANDLER is closed.
2130
void Session::mark_temp_tables_as_free_for_reuse()
2132
for (Table *table= temporary_tables ; table ; table= table->next)
2134
if (table->query_id == query_id)
2137
table->file->ha_reset();
2144
Mark all tables in the list which were used by current substatement
2148
mark_used_tables_as_free_for_reuse()
2149
session - thread context
2150
table - head of the list of tables
2153
Marks all tables in the list which were used by current substatement
2154
(they are marked by its query_id) as free for reuse.
2157
The reason we reset query_id is that it's not enough to just test
2158
if table->query_id != session->query_id to know if a table is in use.
2161
SELECT f1_that_uses_t1() FROM t1;
2162
In f1_that_uses_t1() we will see one instance of t1 where query_id is
2163
set to query_id of original query.
2166
void Session::mark_used_tables_as_free_for_reuse(Table *table)
2168
for (; table ; table= table->next)
2170
if (table->query_id == query_id)
2173
table->file->ha_reset();
2180
Close all tables used by the current substatement, or all tables
2181
used by this thread if we are on the upper level.
2184
close_thread_tables()
2185
session Thread handler
2188
Unlocks tables and frees derived tables.
2189
Put all normal tables used by thread in free list.
2191
It will only close/mark as free for reuse tables opened by this
2192
substatement, it will also check if we are closing tables after
2193
execution of complete query (i.e. we are on upper level) and will
2194
leave prelocked mode if needed.
2197
void Session::close_thread_tables()
2202
We are assuming here that session->derived_tables contains ONLY derived
2203
tables for this substatement. i.e. instead of approach which uses
2204
query_id matching for determining which of the derived tables belong
2205
to this substatement we rely on the ability of substatements to
2206
save/restore session->derived_tables during their execution.
2208
TODO: Probably even better approach is to simply associate list of
2209
derived tables with (sub-)statement instead of thread and destroy
2210
them at the end of its execution.
2216
Close all derived tables generated in queries like
2217
SELECT * FROM (SELECT * FROM t1)
2219
for (table= derived_tables ; table ; table= next)
2222
table->free_tmp_table(this);
2228
Mark all temporary tables used by this statement as free for reuse.
2230
mark_temp_tables_as_free_for_reuse();
2232
Let us commit transaction for statement. Since in 5.0 we only have
2233
one statement transaction and don't allow several nested statement
2234
transactions this call will do nothing if we are inside of stored
2235
function or trigger (i.e. statement transaction is already active and
2236
does not belong to statement for which we do close_thread_tables()).
2237
TODO: This should be fixed in later releases.
2239
if (!(state_flags & Open_tables_state::BACKUPS_AVAIL))
2241
main_da.can_overwrite_status= true;
2242
ha_autocommit_or_rollback(this, is_error());
2243
main_da.can_overwrite_status= false;
2244
transaction.stmt.reset();
2250
/* Ensure we are calling ha_reset() for all used tables */
2251
mark_used_tables_as_free_for_reuse(open_tables);
2254
We are under simple LOCK TABLES so should not do anything else.
2262
For RBR we flush the pending event just before we unlock all the
2263
tables. This means that we are at the end of a topmost
2264
statement, so we ensure that the STMT_END_F flag is set on the
2265
pending event. For statements that are *inside* stored
2266
functions, the pending event will not be flushed: that will be
2267
handled either before writing a query log event (inside
2268
binlog_query()) or when preparing a pending event.
2270
mysql_unlock_tables(this, lock);
2274
Note that we need to hold LOCK_open while changing the
2275
open_tables list. Another thread may work on it.
2276
(See: remove_table_from_cache(), mysql_wait_completed_table())
2277
Closing a MERGE child before the parent would be fatal if the
2278
other thread tries to abort the MERGE lock in between.
2281
close_open_tables();