This patch also fixes Bug#55452 "SET PASSWORD is replicated twice in RBR mode". The goal of this patch is to remove the release of metadata locks from close_thread_tables(). This is necessary to not mistakenly release the locks in the course of a multi-step operation that involves multiple close_thread_tables() or close_tables_for_reopen(). On the same token, move statement commit outside close_thread_tables(). Other cleanups: Cleanup COM_FIELD_LIST. Don't call close_thread_tables() in COM_SHUTDOWN -- there are no open tables there that can be closed (we leave the locked tables mode in THD destructor, and this close_thread_tables() won't leave it anyway). Make open_and_lock_tables() and open_and_lock_tables_derived() call close_thread_tables() upon failure. Remove the calls to close_thread_tables() that are now unnecessary. Simplify the back off condition in Open_table_context. Streamline metadata lock handling in LOCK TABLES implementation. Add asserts to ensure correct life cycle of statement transaction in a session. Remove a piece of dead code that has also become redundant after the fix for Bug 37521. mysql-test/r/variables.result: Update results: set @@autocommit and statement transaction/ prelocked mode. mysql-test/r/view.result: A harmless change in CHECK TABLE <view> status for a broken view. If previously a failure to prelock all functions used in a view would leave the connection in LTM_PRELOCKED mode, now we call close_thread_tables() from open_and_lock_tables() and leave prelocked mode, thus some check in mysql_admin_table() that works only in prelocked/locked tables mode is no longer activated. mysql-test/suite/rpl/r/rpl_row_implicit_commit_binlog.result: Fixed Bug#55452 "SET PASSWORD is replicated twice in RBR mode": extra binlog events are gone from the binary log. mysql-test/t/variables.test: Add a test case: set autocommit and statement transaction/prelocked mode. sql/event_data_objects.cc: Simplify code in Event_job_data::execute(). Move sp_head memory management to lex_end(). sql/event_db_repository.cc: Move the release of metadata locks outside close_thread_tables(). Make sure we call close_thread_tables() when open_and_lock_tables() fails and remove extra code from the events data dictionary. Use close_mysql_tables(), a new internal function to properly close mysql.* tables in the data dictionary. Contract Event_db_repository::drop_events_by_field, drop_schema_events into one function. When dropping all events in a schema, make sure we don't mistakenly release all locks acquired by DROP DATABASE. These include locks on the database name and the global intention exclusive metadata lock. sql/event_db_repository.h: Function open_event_table() does not require an instance of Event_db_repository. sql/events.cc: Use close_mysql_tables() instead of close_thread_tables() to bootstrap events, since the latter no longer releases metadata locks. sql/ha_ndbcluster.cc: - mysql_rm_table_part2 no longer releases acquired metadata locks. Do it in the caller. sql/ha_ndbcluster_binlog.cc: Deploy the new protocol for closing thread tables in run_query() and ndb_binlog_index code. sql/handler.cc: Assert that we never call ha_commit_trans/ ha_rollback_trans in sub-statement, which is now the case. sql/handler.h: Add an accessor to check whether THD_TRANS object is empty (has no transaction started). sql/log.cc: Update a comment. sql/log_event.cc: Since now we commit/rollback statement transaction in mysql_execute_command(), we need a mechanism to communicate from Query_log_event::do_apply_event() to mysql_execute_command() that the statement transaction should be rolled back, not committed. Ideally it would be a virtual method of THD. I hesitate to make THD a virtual base class in this already large patch. Use a thd->variables.option_bits for now. Remove a call to close_thread_tables() from the slave IO thread. It doesn't open any tables, and the protocol for closing thread tables is more complicated now. Make sure we properly close thread tables, however, in Load_data_log_event, which doesn't follow the standard server execution procedure with mysql_execute_command(). @todo: this piece should use Server_runnable framework instead. Remove an unnecessary call to mysql_unlock_tables(). sql/rpl_rli.cc: Update Relay_log_info::slave_close_thread_tables() to follow the new close protocol. sql/set_var.cc: Remove an unused header. sql/slave.cc: Remove an unnecessary call to close_thread_tables(). sql/sp.cc: Remove unnecessary calls to close_thread_tables() from SP DDL implementation. The tables will be closed by the caller, in mysql_execute_command(). When dropping all routines in a database, make sure to not mistakenly drop all metadata locks acquired so far, they include the scoped lock on the schema. sql/sp_head.cc: Correct the protocol that closes thread tables in an SP instruction. Clear lex->sphead before cleaning up lex with lex_end to make sure that we don't delete the sphead twice. It's considered to be "cleaner" and more in line with future changes than calling delete lex->sphead in other places that cleanup the lex. sql/sp_head.h: When destroying m_lex_keeper of an instruction, don't delete the sphead that all lex objects share. @todo: don't store a reference to routine's sp_head instance in instruction's lex. sql/sql_acl.cc: Don't call close_thread_tables() where the caller will do that for us. Fix Bug#55452 "SET PASSWORD is replicated twice in RBR mode" by disabling RBR replication in change_password() function. Use close_mysql_tables() in bootstrap and ACL reload code to make sure we release all metadata locks. sql/sql_base.cc: This is the main part of the patch: - remove manipulation with thd->transaction and thd->mdl_context from close_thread_tables(). Now this function is only responsible for closing tables, nothing else. This is necessary to be able to easily use close_thread_tables() in procedures, that involve multiple open/close tables, which all need to be protected continuously by metadata locks. Add asserts ensuring that TABLE object is only used when is protected by a metadata lock. Simplify the back off condition of Open_table_context, we no longer need to look at the autocommit mode. Make open_and_lock_tables() and open_normal_and_derived_tables() close thread tables and release metadata locks acquired so-far upon failure. This simplifies their usage. Implement close_mysql_tables(). sql/sql_base.h: Add declaration for close_mysql_tables(). sql/sql_class.cc: Remove a piece of dead code that has also become redundant after the fix for Bug 37521. The code became dead when my_eof() was made a non-protocol method, but a method that merely modifies the diagnostics area. The code became redundant with the fix for Bug#37521, when we started to cal close_thread_tables() before Protocol::end_statement(). sql/sql_do.cc: Do nothing in DO if inside a substatement (the assert moved out of trans_rollback_stmt). sql/sql_handler.cc: Add comments. sql/sql_insert.cc: Remove dead code. Release metadata locks explicitly at the end of the delayed insert thread. sql/sql_lex.cc: Add destruction of lex->sphead to lex_end(), lex "reset" method called at the end of each statement. sql/sql_parse.cc: Move close_thread_tables() and other related cleanups to mysql_execute_command() from dispatch_command(). This has become possible after the fix for Bug#37521. Mark federated SERVER statements as DDL. Next step: make sure that we don't store eof packet in the query cache, and move the query cache code outside mysql_parse. Brush up the code of COM_FIELD_LIST. Remove unnecessary calls to close_thread_tables(). When killing a query, don't report "OK" if it was a suicide. sql/sql_parse.h: Remove declaration of a function that is now static. sql/sql_partition.cc: Remove an unnecessary call to close_thread_tables(). sql/sql_plugin.cc: open_and_lock_tables() will clean up after itself after a failure. Move close_thread_tables() above end: label, and replace with close_mysql_tables(), which will also release the metadata lock on mysql.plugin. sql/sql_prepare.cc: Now that we no longer release locks in close_thread_tables() statement prepare code has become more straightforward. Remove the now redundant check for thd->killed() (used only by the backup project) from Execute_server_runnable. Reorder code to take into account that now mysql_execute_command() performs lex->unit.cleanup() and close_thread_tables(). sql/sql_priv.h: Add a new option to server options to interact between the slave SQL thread and execution framework (hack). @todo: use a virtual method of class THD instead. sql/sql_servers.cc: Due to Bug 25705 replication of DROP/CREATE/ALTER SERVER is broken. Make sure at least we do not attempt to replicate these statements using RBR, as this violates the assert in close_mysql_tables(). sql/sql_table.cc: Do not release metadata locks in mysql_rm_table_part2, this is done by the caller. Do not call close_thread_tables() in mysql_create_table(), this is done by the caller. Fix a bug in DROP TABLE under LOCK TABLES when, upon error in wait_while_table_is_used() we would mistakenly release the metadata lock on a non-dropped table. Explicitly release metadata locks when doing an implicit commit. sql/sql_trigger.cc: Now that we delete lex->sphead in lex_end(), zero the trigger's sphead in lex after loading the trigger, to avoid double deletion. sql/sql_udf.cc: Use close_mysql_tables() instead of close_thread_tables(). sql/sys_vars.cc: Remove code added in scope of WL#4284 which would break when we perform set @@session.autocommit along with setting other variables and using tables or functions. A test case added to variables.test. sql/transaction.cc: Add asserts. sql/tztime.cc: Use close_mysql_tables() rather than close_thread_tables().
748 lines
22 KiB
C++
748 lines
22 KiB
C++
/* Copyright (C) 2005-2006 MySQL AB
|
|
|
|
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 Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
#ifdef USE_PRAGMA_IMPLEMENTATION
|
|
#pragma implementation /* gcc class implementation */
|
|
#endif
|
|
|
|
#include "sql_priv.h"
|
|
#include "unireg.h"
|
|
#include "sql_cursor.h"
|
|
#include "sql_select.h"
|
|
#include "probes_mysql.h"
|
|
#include "sql_parse.h" // mysql_execute_command
|
|
#include "sql_base.h"
|
|
|
|
/****************************************************************************
|
|
Declarations.
|
|
****************************************************************************/
|
|
|
|
/**
|
|
Sensitive_cursor -- a sensitive non-materialized server side
|
|
cursor. An instance of this class cursor has its own runtime
|
|
state -- list of used items and memory root for runtime memory,
|
|
open and locked tables, change list for the changes of the
|
|
parsed tree. This state is freed when the cursor is closed.
|
|
*/
|
|
|
|
class Sensitive_cursor: public Server_side_cursor
|
|
{
|
|
MEM_ROOT main_mem_root;
|
|
Query_arena *stmt_arena;
|
|
JOIN *join;
|
|
TABLE *open_tables;
|
|
MYSQL_LOCK *lock;
|
|
TABLE *derived_tables;
|
|
/* List of items created during execution */
|
|
query_id_t query_id;
|
|
struct Engine_info
|
|
{
|
|
handlerton *ht;
|
|
void *read_view;
|
|
};
|
|
Engine_info ht_info[MAX_HA];
|
|
Item_change_list change_list;
|
|
my_bool close_at_commit;
|
|
THR_LOCK_OWNER lock_id;
|
|
private:
|
|
/* bzero cursor state in THD */
|
|
void reset_thd(THD *thd);
|
|
public:
|
|
Sensitive_cursor(THD *thd, select_result *result_arg);
|
|
|
|
THR_LOCK_OWNER *get_lock_id() { return &lock_id; }
|
|
/* Save THD state into cursor */
|
|
void post_open(THD *thd);
|
|
|
|
virtual bool is_open() const { return join != 0; }
|
|
virtual int open(JOIN *join);
|
|
virtual void fetch(ulong num_rows);
|
|
virtual void close();
|
|
virtual ~Sensitive_cursor();
|
|
};
|
|
|
|
|
|
/**
|
|
Materialized_cursor -- an insensitive materialized server-side
|
|
cursor. The result set of this cursor is saved in a temporary
|
|
table at open. The cursor itself is simply an interface for the
|
|
handler of the temporary table.
|
|
*/
|
|
|
|
class Materialized_cursor: public Server_side_cursor
|
|
{
|
|
MEM_ROOT main_mem_root;
|
|
/* A fake unit to supply to select_send when fetching */
|
|
SELECT_LEX_UNIT fake_unit;
|
|
TABLE *table;
|
|
List<Item> item_list;
|
|
ulong fetch_limit;
|
|
ulong fetch_count;
|
|
bool is_rnd_inited;
|
|
public:
|
|
Materialized_cursor(select_result *result, TABLE *table);
|
|
|
|
int fill_item_list(THD *thd, List<Item> &send_result_set_metadata);
|
|
virtual bool is_open() const { return table != 0; }
|
|
virtual int open(JOIN *join __attribute__((unused)));
|
|
virtual void fetch(ulong num_rows);
|
|
virtual void close();
|
|
virtual ~Materialized_cursor();
|
|
};
|
|
|
|
|
|
/**
|
|
Select_materialize -- a mediator between a cursor query and the
|
|
protocol. In case we were not able to open a non-materialzed
|
|
cursor, it creates an internal temporary HEAP table, and insert
|
|
all rows into it. When the table reaches max_heap_table_size,
|
|
it's converted to a MyISAM table. Later this table is used to
|
|
create a Materialized_cursor.
|
|
*/
|
|
|
|
class Select_materialize: public select_union
|
|
{
|
|
select_result *result; /**< the result object of the caller (PS or SP) */
|
|
public:
|
|
Materialized_cursor *materialized_cursor;
|
|
Select_materialize(select_result *result_arg)
|
|
:result(result_arg), materialized_cursor(0) {}
|
|
virtual bool send_result_set_metadata(List<Item> &list, uint flags);
|
|
};
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
/**
|
|
Attempt to open a materialized or non-materialized cursor.
|
|
|
|
@param thd thread handle
|
|
@param[in] flags create a materialized cursor or not
|
|
@param[in] result result class of the caller used as a destination
|
|
for the rows fetched from the cursor
|
|
@param[out] pcursor a pointer to store a pointer to cursor in
|
|
|
|
@retval
|
|
0 the query has been successfully executed; in this
|
|
case pcursor may or may not contain
|
|
a pointer to an open cursor.
|
|
@retval
|
|
non-zero an error, 'pcursor' has been left intact.
|
|
*/
|
|
|
|
int mysql_open_cursor(THD *thd, uint flags, select_result *result,
|
|
Server_side_cursor **pcursor)
|
|
{
|
|
Sensitive_cursor *sensitive_cursor;
|
|
select_result *save_result;
|
|
Select_materialize *result_materialize;
|
|
LEX *lex= thd->lex;
|
|
int rc;
|
|
|
|
/*
|
|
The lifetime of the sensitive cursor is the same or less as the
|
|
lifetime of the runtime memory of the statement it's opened for.
|
|
*/
|
|
if (! (result_materialize= new (thd->mem_root) Select_materialize(result)))
|
|
return 1;
|
|
|
|
if (! (sensitive_cursor= new (thd->mem_root) Sensitive_cursor(thd, result)))
|
|
{
|
|
delete result_materialize;
|
|
result_materialize= NULL;
|
|
return 1;
|
|
}
|
|
|
|
save_result= lex->result;
|
|
|
|
lex->result= result_materialize;
|
|
if (! (flags & (uint) ALWAYS_MATERIALIZED_CURSOR))
|
|
{
|
|
thd->lock_id= sensitive_cursor->get_lock_id();
|
|
thd->cursor= sensitive_cursor;
|
|
}
|
|
MYSQL_QUERY_EXEC_START(thd->query(),
|
|
thd->thread_id,
|
|
(char *) (thd->db ? thd->db : ""),
|
|
thd->security_ctx->priv_user,
|
|
(char *) thd->security_ctx->host_or_ip,
|
|
2);
|
|
rc= mysql_execute_command(thd);
|
|
MYSQL_QUERY_EXEC_DONE(rc);
|
|
|
|
lex->result= save_result;
|
|
thd->lock_id= &thd->main_lock_id;
|
|
thd->cursor= 0;
|
|
|
|
/*
|
|
Possible options here:
|
|
- a sensitive cursor is open. In this case rc is 0 and
|
|
result_materialize->materialized_cursor is NULL, or
|
|
- a materialized cursor is open. In this case rc is 0 and
|
|
result_materialize->materialized is not NULL
|
|
- an error occurred during materialization.
|
|
result_materialize->materialized_cursor is not NULL, but rc != 0
|
|
- successful completion of mysql_execute_command without
|
|
a cursor: rc is 0, result_materialize->materialized_cursor is NULL,
|
|
sensitive_cursor is not open.
|
|
This is possible if some command writes directly to the
|
|
network, bypassing select_result mechanism. An example of
|
|
such command is SHOW VARIABLES or SHOW STATUS.
|
|
*/
|
|
if (rc)
|
|
{
|
|
if (result_materialize->materialized_cursor)
|
|
delete result_materialize->materialized_cursor;
|
|
goto err_open;
|
|
}
|
|
|
|
if (sensitive_cursor->is_open())
|
|
{
|
|
DBUG_ASSERT(!result_materialize->materialized_cursor);
|
|
/*
|
|
It's safer if we grab THD state after mysql_execute_command
|
|
is finished and not in Sensitive_cursor::open(), because
|
|
currently the call to Sensitive_cursor::open is buried deep
|
|
in JOIN::exec of the top level join.
|
|
*/
|
|
sensitive_cursor->post_open(thd);
|
|
*pcursor= sensitive_cursor;
|
|
goto end;
|
|
}
|
|
else if (result_materialize->materialized_cursor)
|
|
{
|
|
Materialized_cursor *materialized_cursor=
|
|
result_materialize->materialized_cursor;
|
|
|
|
if ((rc= materialized_cursor->open(0)))
|
|
{
|
|
delete materialized_cursor;
|
|
materialized_cursor= NULL;
|
|
goto err_open;
|
|
}
|
|
|
|
*pcursor= materialized_cursor;
|
|
thd->stmt_arena->cleanup_stmt();
|
|
goto end;
|
|
}
|
|
|
|
err_open:
|
|
DBUG_ASSERT(! (sensitive_cursor && sensitive_cursor->is_open()));
|
|
delete sensitive_cursor;
|
|
end:
|
|
delete result_materialize;
|
|
return rc;
|
|
}
|
|
|
|
/****************************************************************************
|
|
Server_side_cursor
|
|
****************************************************************************/
|
|
|
|
Server_side_cursor::~Server_side_cursor()
|
|
{
|
|
}
|
|
|
|
|
|
void Server_side_cursor::operator delete(void *ptr, size_t size)
|
|
{
|
|
Server_side_cursor *cursor= (Server_side_cursor*) ptr;
|
|
MEM_ROOT own_root= *cursor->mem_root;
|
|
|
|
DBUG_ENTER("Server_side_cursor::operator delete");
|
|
TRASH(ptr, size);
|
|
/*
|
|
If this cursor has never been opened mem_root is empty. Otherwise
|
|
mem_root points to the memory the cursor object was allocated in.
|
|
In this case it's important to call free_root last, and free a copy
|
|
instead of *mem_root to avoid writing into freed memory.
|
|
*/
|
|
free_root(&own_root, MYF(0));
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
/****************************************************************************
|
|
Sensitive_cursor
|
|
****************************************************************************/
|
|
|
|
Sensitive_cursor::Sensitive_cursor(THD *thd, select_result *result_arg)
|
|
:Server_side_cursor(&main_mem_root, result_arg),
|
|
stmt_arena(0),
|
|
join(0),
|
|
close_at_commit(FALSE)
|
|
{
|
|
/* We will overwrite it at open anyway. */
|
|
init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
|
|
thr_lock_owner_init(&lock_id, &thd->lock_info);
|
|
bzero((void*) ht_info, sizeof(ht_info));
|
|
}
|
|
|
|
|
|
/**
|
|
Save THD state into cursor.
|
|
|
|
@todo
|
|
- What problems can we have with it if cursor is open?
|
|
- TODO: must be fixed because of the prelocked mode.
|
|
*/
|
|
void
|
|
Sensitive_cursor::post_open(THD *thd)
|
|
{
|
|
Engine_info *info;
|
|
/*
|
|
We need to save and reset thd->mem_root, otherwise it'll be
|
|
freed later in mysql_parse.
|
|
|
|
We can't just change thd->mem_root here as we want to keep the
|
|
things that are already allocated in thd->mem_root for
|
|
Sensitive_cursor::fetch()
|
|
*/
|
|
*mem_root= *thd->mem_root;
|
|
stmt_arena= thd->stmt_arena;
|
|
state= stmt_arena->state;
|
|
/* Allocate a new memory root for thd */
|
|
init_sql_alloc(thd->mem_root,
|
|
thd->variables.query_alloc_block_size,
|
|
thd->variables.query_prealloc_size);
|
|
|
|
/*
|
|
Save tables and zero THD pointers to prevent table close in
|
|
close_thread_tables.
|
|
*/
|
|
derived_tables= thd->derived_tables;
|
|
open_tables= thd->open_tables;
|
|
lock= thd->lock;
|
|
query_id= thd->query_id;
|
|
free_list= thd->free_list;
|
|
thd->change_list.move_elements_to(&change_list);
|
|
reset_thd(thd);
|
|
/* Now we have an active cursor and can cause a deadlock */
|
|
thd->lock_info.n_cursors++;
|
|
|
|
close_at_commit= FALSE; /* reset in case we're reusing the cursor */
|
|
info= &ht_info[0];
|
|
for (Ha_trx_info *ha_trx_info= thd->transaction.stmt.ha_list;
|
|
ha_trx_info; ha_trx_info= ha_trx_info->next())
|
|
{
|
|
handlerton *ht= ha_trx_info->ht();
|
|
close_at_commit|= test(ht->flags & HTON_CLOSE_CURSORS_AT_COMMIT);
|
|
if (ht->create_cursor_read_view)
|
|
{
|
|
info->ht= ht;
|
|
info->read_view= (ht->create_cursor_read_view)(ht, thd);
|
|
++info;
|
|
}
|
|
}
|
|
/*
|
|
What problems can we have with it if cursor is open?
|
|
TODO: must be fixed because of the prelocked mode.
|
|
*/
|
|
}
|
|
|
|
|
|
/**
|
|
bzero cursor state in THD.
|
|
*/
|
|
|
|
void
|
|
Sensitive_cursor::reset_thd(THD *thd)
|
|
{
|
|
thd->derived_tables= 0;
|
|
thd->set_open_tables(NULL);
|
|
thd->lock= 0;
|
|
thd->free_list= 0;
|
|
thd->change_list.empty();
|
|
}
|
|
|
|
|
|
int
|
|
Sensitive_cursor::open(JOIN *join_arg)
|
|
{
|
|
join= join_arg;
|
|
THD *thd= join->thd;
|
|
/* First non-constant table */
|
|
JOIN_TAB *join_tab= join->join_tab + join->const_tables;
|
|
DBUG_ENTER("Sensitive_cursor::open");
|
|
|
|
join->change_result(result);
|
|
/*
|
|
Send fields description to the client; server_status is sent
|
|
in 'EOF' packet, which follows send_result_set_metadata().
|
|
We don't simply use SEND_EOF flag of send_result_set_metadata because we also
|
|
want to flush the network buffer, which is done only in a standalone
|
|
send_eof().
|
|
*/
|
|
result->send_result_set_metadata(*join->fields, Protocol::SEND_NUM_ROWS);
|
|
thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
|
|
result->send_eof();
|
|
thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
|
|
|
|
/* Prepare JOIN for reading rows. */
|
|
join->tmp_table= 0;
|
|
join->join_tab[join->tables-1].next_select= setup_end_select_func(join);
|
|
join->send_records= 0;
|
|
join->fetch_limit= join->unit->offset_limit_cnt;
|
|
|
|
/* Disable JOIN CACHE as it is not working with cursors yet */
|
|
for (JOIN_TAB *tab= join_tab;
|
|
tab != join->join_tab + join->tables - 1;
|
|
tab++)
|
|
{
|
|
if (tab->next_select == sub_select_cache)
|
|
tab->next_select= sub_select;
|
|
}
|
|
|
|
DBUG_ASSERT(join_tab->table->reginfo.not_exists_optimize == 0);
|
|
DBUG_ASSERT(join_tab->not_used_in_distinct == 0);
|
|
/*
|
|
null_row is set only if row not found and it's outer join: should never
|
|
happen for the first table in join_tab list
|
|
*/
|
|
DBUG_ASSERT(join_tab->table->null_row == 0);
|
|
DBUG_RETURN(0);
|
|
}
|
|
|
|
|
|
/**
|
|
Fetch next num_rows rows from the cursor and send them to the client.
|
|
|
|
Precondition:
|
|
- Sensitive_cursor is open
|
|
|
|
@param num_rows fetch up to this number of rows (maybe less)
|
|
*/
|
|
|
|
void
|
|
Sensitive_cursor::fetch(ulong num_rows)
|
|
{
|
|
THD *thd= join->thd;
|
|
JOIN_TAB *join_tab= join->join_tab + join->const_tables;
|
|
enum_nested_loop_state error= NESTED_LOOP_OK;
|
|
Query_arena backup_arena;
|
|
Engine_info *info;
|
|
DBUG_ENTER("Sensitive_cursor::fetch");
|
|
DBUG_PRINT("enter",("rows: %lu", num_rows));
|
|
|
|
DBUG_ASSERT(thd->derived_tables == 0 && thd->open_tables == 0 &&
|
|
thd->lock == 0);
|
|
|
|
thd->derived_tables= derived_tables;
|
|
thd->set_open_tables(open_tables);
|
|
thd->lock= lock;
|
|
thd->set_query_id(query_id);
|
|
change_list.move_elements_to(&thd->change_list);
|
|
/* save references to memory allocated during fetch */
|
|
thd->set_n_backup_active_arena(this, &backup_arena);
|
|
|
|
for (info= ht_info; info->read_view ; info++)
|
|
(info->ht->set_cursor_read_view)(info->ht, thd, info->read_view);
|
|
|
|
join->fetch_limit+= num_rows;
|
|
|
|
error= sub_select(join, join_tab, 0);
|
|
if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
|
|
error= sub_select(join,join_tab,1);
|
|
if (error == NESTED_LOOP_QUERY_LIMIT)
|
|
error= NESTED_LOOP_OK; /* select_limit used */
|
|
if (error == NESTED_LOOP_CURSOR_LIMIT)
|
|
join->resume_nested_loop= TRUE;
|
|
|
|
ha_release_temporary_latches(thd);
|
|
|
|
/* Grab free_list here to correctly free it in close */
|
|
thd->restore_active_arena(this, &backup_arena);
|
|
|
|
thd->change_list.move_elements_to(&change_list);
|
|
reset_thd(thd);
|
|
|
|
for (info= ht_info; info->read_view; info++)
|
|
(info->ht->set_cursor_read_view)(info->ht, thd, 0);
|
|
|
|
if (error == NESTED_LOOP_CURSOR_LIMIT)
|
|
{
|
|
/* Fetch limit worked, possibly more rows are there */
|
|
thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
|
|
result->send_eof();
|
|
thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
|
|
}
|
|
else
|
|
{
|
|
close();
|
|
if (error == NESTED_LOOP_OK)
|
|
{
|
|
thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
|
|
result->send_eof();
|
|
thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT;
|
|
}
|
|
else if (error != NESTED_LOOP_KILLED)
|
|
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
|
|
}
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/**
|
|
@todo
|
|
Another hack: we need to set THD state as if in a fetch to be
|
|
able to call stmt close.
|
|
*/
|
|
void
|
|
Sensitive_cursor::close()
|
|
{
|
|
THD *thd= join->thd;
|
|
DBUG_ENTER("Sensitive_cursor::close");
|
|
|
|
for (Engine_info *info= ht_info; info->read_view; info++)
|
|
{
|
|
(info->ht->close_cursor_read_view)(info->ht, thd, info->read_view);
|
|
info->read_view= 0;
|
|
info->ht= 0;
|
|
}
|
|
|
|
change_list.move_elements_to(&thd->change_list);
|
|
{
|
|
/*
|
|
XXX: Another hack: we need to set THD state as if in a fetch to be
|
|
able to call stmt close.
|
|
*/
|
|
DBUG_ASSERT(lock || open_tables || derived_tables);
|
|
|
|
TABLE *tmp_derived_tables= thd->derived_tables;
|
|
MYSQL_LOCK *tmp_lock= thd->lock;
|
|
|
|
thd->set_open_tables(open_tables);
|
|
thd->derived_tables= derived_tables;
|
|
thd->lock= lock;
|
|
|
|
close_thread_tables(thd);
|
|
/* Is expected to at least close tables and empty thd->change_list */
|
|
stmt_arena->cleanup_stmt();
|
|
|
|
thd->set_open_tables(tmp_derived_tables);
|
|
thd->derived_tables= tmp_derived_tables;
|
|
thd->lock= tmp_lock;
|
|
}
|
|
thd->lock_info.n_cursors--; /* Decrease the number of active cursors */
|
|
join= 0;
|
|
stmt_arena= 0;
|
|
free_items();
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
Sensitive_cursor::~Sensitive_cursor()
|
|
{
|
|
if (is_open())
|
|
close();
|
|
}
|
|
|
|
/***************************************************************************
|
|
Materialized_cursor
|
|
****************************************************************************/
|
|
|
|
Materialized_cursor::Materialized_cursor(select_result *result_arg,
|
|
TABLE *table_arg)
|
|
:Server_side_cursor(&table_arg->mem_root, result_arg),
|
|
table(table_arg),
|
|
fetch_limit(0),
|
|
fetch_count(0),
|
|
is_rnd_inited(0)
|
|
{
|
|
fake_unit.init_query();
|
|
fake_unit.thd= table->in_use;
|
|
}
|
|
|
|
|
|
/**
|
|
Preserve the original metadata that would be sent to the client.
|
|
|
|
@param thd Thread identifier.
|
|
@param send_result_set_metadata List of fields that would be sent.
|
|
*/
|
|
|
|
int Materialized_cursor::fill_item_list(THD *thd, List<Item> &send_result_set_metadata)
|
|
{
|
|
Query_arena backup_arena;
|
|
int rc;
|
|
List_iterator_fast<Item> it_org(send_result_set_metadata);
|
|
List_iterator_fast<Item> it_dst(item_list);
|
|
Item *item_org;
|
|
Item *item_dst;
|
|
|
|
thd->set_n_backup_active_arena(this, &backup_arena);
|
|
|
|
if ((rc= table->fill_item_list(&item_list)))
|
|
goto end;
|
|
|
|
DBUG_ASSERT(send_result_set_metadata.elements == item_list.elements);
|
|
|
|
/*
|
|
Unless we preserve the original metadata, it will be lost,
|
|
since new fields describe columns of the temporary table.
|
|
Allocate a copy of the name for safety only. Currently
|
|
items with original names are always kept in memory,
|
|
but in case this changes a memory leak may be hard to notice.
|
|
*/
|
|
while ((item_dst= it_dst++, item_org= it_org++))
|
|
{
|
|
Send_field send_field;
|
|
Item_ident *ident= static_cast<Item_ident *>(item_dst);
|
|
item_org->make_field(&send_field);
|
|
|
|
ident->db_name= thd->strdup(send_field.db_name);
|
|
ident->table_name= thd->strdup(send_field.table_name);
|
|
}
|
|
end:
|
|
thd->restore_active_arena(this, &backup_arena);
|
|
/* Check for thd->is_error() in case of OOM */
|
|
return rc || thd->is_error();
|
|
}
|
|
|
|
int Materialized_cursor::open(JOIN *join __attribute__((unused)))
|
|
{
|
|
THD *thd= fake_unit.thd;
|
|
int rc;
|
|
Query_arena backup_arena;
|
|
thd->set_n_backup_active_arena(this, &backup_arena);
|
|
/* Create a list of fields and start sequential scan */
|
|
rc= result->prepare(item_list, &fake_unit);
|
|
if (!rc && !(rc= table->file->ha_rnd_init(TRUE)))
|
|
is_rnd_inited= 1;
|
|
|
|
thd->restore_active_arena(this, &backup_arena);
|
|
if (rc == 0)
|
|
{
|
|
/*
|
|
Now send the result set metadata to the client. We need to
|
|
do it here, as in Select_materialize::send_result_set_metadata the items
|
|
for column types are not yet created (send_result_set_metadata requires
|
|
a list of items). The new types may differ from the original
|
|
ones sent at prepare if some of them were altered by MySQL
|
|
HEAP tables mechanism -- used when create_tmp_field_from_item
|
|
may alter the original column type.
|
|
|
|
We can't simply supply SEND_EOF flag to send_result_set_metadata, because
|
|
send_result_set_metadata doesn't flush the network buffer.
|
|
*/
|
|
rc= result->send_result_set_metadata(item_list, Protocol::SEND_NUM_ROWS);
|
|
thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
|
|
result->send_eof();
|
|
thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
|
|
/**
|
|
Fetch up to the given number of rows from a materialized cursor.
|
|
|
|
Precondition: the cursor is open.
|
|
|
|
If the cursor points after the last row, the fetch will automatically
|
|
close the cursor and not send any data (except the 'EOF' packet
|
|
with SERVER_STATUS_LAST_ROW_SENT). This is an extra round trip
|
|
and probably should be improved to return
|
|
SERVER_STATUS_LAST_ROW_SENT along with the last row.
|
|
*/
|
|
|
|
void Materialized_cursor::fetch(ulong num_rows)
|
|
{
|
|
THD *thd= table->in_use;
|
|
|
|
int res= 0;
|
|
result->begin_dataset();
|
|
for (fetch_limit+= num_rows; fetch_count < fetch_limit; fetch_count++)
|
|
{
|
|
if ((res= table->file->rnd_next(table->record[0])))
|
|
break;
|
|
/* Send data only if the read was successful. */
|
|
result->send_data(item_list);
|
|
}
|
|
|
|
switch (res) {
|
|
case 0:
|
|
thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
|
|
result->send_eof();
|
|
thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
|
|
break;
|
|
case HA_ERR_END_OF_FILE:
|
|
thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
|
|
result->send_eof();
|
|
thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT;
|
|
close();
|
|
break;
|
|
default:
|
|
table->file->print_error(res, MYF(0));
|
|
close();
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
void Materialized_cursor::close()
|
|
{
|
|
/* Free item_list items */
|
|
free_items();
|
|
if (is_rnd_inited)
|
|
(void) table->file->ha_rnd_end();
|
|
/*
|
|
We need to grab table->mem_root to prevent free_tmp_table from freeing:
|
|
the cursor object was allocated in this memory.
|
|
*/
|
|
main_mem_root= table->mem_root;
|
|
mem_root= &main_mem_root;
|
|
clear_alloc_root(&table->mem_root);
|
|
free_tmp_table(table->in_use, table);
|
|
table= 0;
|
|
}
|
|
|
|
|
|
Materialized_cursor::~Materialized_cursor()
|
|
{
|
|
if (is_open())
|
|
close();
|
|
}
|
|
|
|
|
|
/***************************************************************************
|
|
Select_materialize
|
|
****************************************************************************/
|
|
|
|
bool Select_materialize::send_result_set_metadata(List<Item> &list, uint flags)
|
|
{
|
|
DBUG_ASSERT(table == 0);
|
|
if (create_result_table(unit->thd, unit->get_unit_column_types(),
|
|
FALSE, thd->variables.option_bits | TMP_TABLE_ALL_COLUMNS, ""))
|
|
return TRUE;
|
|
|
|
materialized_cursor= new (&table->mem_root)
|
|
Materialized_cursor(result, table);
|
|
|
|
if (! materialized_cursor)
|
|
{
|
|
free_tmp_table(table->in_use, table);
|
|
table= 0;
|
|
return TRUE;
|
|
}
|
|
if (materialized_cursor->fill_item_list(unit->thd, list))
|
|
{
|
|
delete materialized_cursor;
|
|
table= 0;
|
|
materialized_cursor= 0;
|
|
return TRUE;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|