805
Mark all temporary tables which were used by the current statement or
806
substatement as free for reuse, but only if the query_id can be cleared.
808
@param session thread context
810
@remark For temp tables associated with a open SQL HANDLER the query_id
811
is not reset until the HANDLER is closed.
814
static void mark_temp_tables_as_free_for_reuse(Session *session)
816
for (Table *table= session->temporary_tables ; table ; table= table->next)
818
if (table->query_id == session->query_id)
821
table->file->ha_reset();
828
Mark all tables in the list which were used by current substatement
832
mark_used_tables_as_free_for_reuse()
833
session - thread context
834
table - head of the list of tables
837
Marks all tables in the list which were used by current substatement
838
(they are marked by its query_id) as free for reuse.
841
The reason we reset query_id is that it's not enough to just test
842
if table->query_id != session->query_id to know if a table is in use.
845
SELECT f1_that_uses_t1() FROM t1;
846
In f1_that_uses_t1() we will see one instance of t1 where query_id is
847
set to query_id of original query.
850
static void mark_used_tables_as_free_for_reuse(Session *session, Table *table)
852
for (; table ; table= table->next)
854
if (table->query_id == session->query_id)
857
table->file->ha_reset();
864
805
Auxiliary function to close all tables in the open_tables list.
866
807
@param session Thread context.
897
Close all tables used by the current substatement, or all tables
898
used by this thread if we are on the upper level.
901
close_thread_tables()
902
session Thread handler
905
Unlocks tables and frees derived tables.
906
Put all normal tables used by thread in free list.
908
It will only close/mark as free for reuse tables opened by this
909
substatement, it will also check if we are closing tables after
910
execution of complete query (i.e. we are on upper level) and will
911
leave prelocked mode if needed.
914
void close_thread_tables(Session *session)
919
We are assuming here that session->derived_tables contains ONLY derived
920
tables for this substatement. i.e. instead of approach which uses
921
query_id matching for determining which of the derived tables belong
922
to this substatement we rely on the ability of substatements to
923
save/restore session->derived_tables during their execution.
925
TODO: Probably even better approach is to simply associate list of
926
derived tables with (sub-)statement instead of thread and destroy
927
them at the end of its execution.
929
if (session->derived_tables)
933
Close all derived tables generated in queries like
934
SELECT * FROM (SELECT * FROM t1)
936
for (table= session->derived_tables ; table ; table= next)
939
table->free_tmp_table(session);
941
session->derived_tables= 0;
945
Mark all temporary tables used by this statement as free for reuse.
947
mark_temp_tables_as_free_for_reuse(session);
949
Let us commit transaction for statement. Since in 5.0 we only have
950
one statement transaction and don't allow several nested statement
951
transactions this call will do nothing if we are inside of stored
952
function or trigger (i.e. statement transaction is already active and
953
does not belong to statement for which we do close_thread_tables()).
954
TODO: This should be fixed in later releases.
956
if (!(session->state_flags & Open_tables_state::BACKUPS_AVAIL))
958
session->main_da.can_overwrite_status= true;
959
ha_autocommit_or_rollback(session, session->is_error());
960
session->main_da.can_overwrite_status= false;
961
session->transaction.stmt.reset();
964
if (session->locked_tables)
967
/* Ensure we are calling ha_reset() for all used tables */
968
mark_used_tables_as_free_for_reuse(session, session->open_tables);
971
We are under simple LOCK TABLES so should not do anything else.
979
For RBR we flush the pending event just before we unlock all the
980
tables. This means that we are at the end of a topmost
981
statement, so we ensure that the STMT_END_F flag is set on the
982
pending event. For statements that are *inside* stored
983
functions, the pending event will not be flushed: that will be
984
handled either before writing a query log event (inside
985
binlog_query()) or when preparing a pending event.
987
mysql_unlock_tables(session, session->lock);
991
Note that we need to hold LOCK_open while changing the
992
open_tables list. Another thread may work on it.
993
(See: remove_table_from_cache(), mysql_wait_completed_table())
994
Closing a MERGE child before the parent would be fatal if the
995
other thread tries to abort the MERGE lock in between.
997
if (session->open_tables)
998
close_open_tables(session);
1004
837
/* move one table to free list */
1006
839
bool close_thread_table(Session *session, Table **table_ptr)