2013-08-20 11:12:34 +02:00
|
|
|
/* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
|
2018-04-19 22:27:02 +02:00
|
|
|
Copyright (c) 2011, 2018, MariaDB
|
|
|
|
|
2010-03-31 16:05:33 +02:00
|
|
|
|
|
|
|
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
|
2019-05-11 20:29:06 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
|
2010-03-31 16:05:33 +02:00
|
|
|
|
|
|
|
#ifndef SQL_BASE_INCLUDED
|
|
|
|
#define SQL_BASE_INCLUDED
|
|
|
|
|
2018-02-09 19:43:42 +01:00
|
|
|
#include "sql_class.h" /* enum_column_usage */
|
2016-10-02 14:35:08 +02:00
|
|
|
#include "sql_trigger.h" /* trg_event_type */
|
2010-03-31 16:05:33 +02:00
|
|
|
#include "mysqld.h" /* key_map */
|
2013-08-14 10:48:50 +02:00
|
|
|
#include "table_cache.h"
|
2010-03-31 16:05:33 +02:00
|
|
|
|
|
|
|
class Item_ident;
|
2010-04-12 15:17:37 +02:00
|
|
|
struct Name_resolution_context;
|
2010-03-31 16:05:33 +02:00
|
|
|
class Open_table_context;
|
|
|
|
class Open_tables_state;
|
|
|
|
class Prelocking_strategy;
|
2010-04-12 15:17:37 +02:00
|
|
|
struct TABLE_LIST;
|
2010-03-31 16:05:33 +02:00
|
|
|
class THD;
|
2010-04-12 15:17:37 +02:00
|
|
|
struct handlerton;
|
2010-03-31 16:05:33 +02:00
|
|
|
struct TABLE;
|
|
|
|
|
|
|
|
typedef class st_select_lex SELECT_LEX;
|
|
|
|
|
|
|
|
typedef struct st_lock_param_type ALTER_PARTITION_PARAM_TYPE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This enumeration type is used only by the function find_item_in_list
|
|
|
|
to return the info on how an item has been resolved against a list
|
|
|
|
of possibly aliased items.
|
|
|
|
The item can be resolved:
|
|
|
|
- against an alias name of the list's element (RESOLVED_AGAINST_ALIAS)
|
|
|
|
- against non-aliased field name of the list (RESOLVED_WITH_NO_ALIAS)
|
|
|
|
- against an aliased field name of the list (RESOLVED_BEHIND_ALIAS)
|
|
|
|
- ignoring the alias name in cases when SQL requires to ignore aliases
|
|
|
|
(e.g. when the resolved field reference contains a table name or
|
|
|
|
when the resolved item is an expression) (RESOLVED_IGNORING_ALIAS)
|
|
|
|
*/
|
|
|
|
enum enum_resolution_type {
|
|
|
|
NOT_RESOLVED=0,
|
|
|
|
RESOLVED_IGNORING_ALIAS,
|
|
|
|
RESOLVED_BEHIND_ALIAS,
|
|
|
|
RESOLVED_WITH_NO_ALIAS,
|
|
|
|
RESOLVED_AGAINST_ALIAS
|
|
|
|
};
|
|
|
|
|
2018-11-06 16:05:24 +01:00
|
|
|
/* Argument to flush_tables() of what to flush */
|
|
|
|
enum flush_tables_type {
|
|
|
|
FLUSH_ALL,
|
|
|
|
FLUSH_NON_TRANS_TABLES,
|
|
|
|
FLUSH_SYS_TABLES
|
|
|
|
};
|
|
|
|
|
2010-03-31 16:05:33 +02:00
|
|
|
enum find_item_error_report_type {REPORT_ALL_ERRORS, REPORT_EXCEPT_NOT_FOUND,
|
|
|
|
IGNORE_ERRORS, REPORT_EXCEPT_NON_UNIQUE,
|
|
|
|
IGNORE_EXCEPT_NON_UNIQUE};
|
|
|
|
|
2018-05-16 20:51:46 +02:00
|
|
|
/* Flag bits for unique_table() */
|
|
|
|
#define CHECK_DUP_ALLOW_DIFFERENT_ALIAS 1
|
|
|
|
#define CHECK_DUP_FOR_CREATE 2
|
2018-06-26 08:03:58 +02:00
|
|
|
#define CHECK_DUP_SKIP_TEMP_TABLE 4
|
2018-05-16 20:51:46 +02:00
|
|
|
|
2013-06-15 17:32:08 +02:00
|
|
|
uint get_table_def_key(const TABLE_LIST *table_list, const char **key);
|
2010-03-31 16:05:33 +02:00
|
|
|
TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update,
|
|
|
|
uint lock_flags);
|
2010-08-13 11:51:48 +02:00
|
|
|
|
|
|
|
/* mysql_lock_tables() and open_table() flags bits */
|
|
|
|
#define MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK 0x0001
|
|
|
|
#define MYSQL_OPEN_IGNORE_FLUSH 0x0002
|
2013-06-27 16:42:18 +02:00
|
|
|
/* MYSQL_OPEN_TEMPORARY_ONLY (0x0004) is not used anymore. */
|
2010-08-13 11:51:48 +02:00
|
|
|
#define MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY 0x0008
|
|
|
|
#define MYSQL_LOCK_LOG_TABLE 0x0010
|
|
|
|
/**
|
|
|
|
Do not try to acquire a metadata lock on the table: we
|
|
|
|
already have one.
|
|
|
|
*/
|
|
|
|
#define MYSQL_OPEN_HAS_MDL_LOCK 0x0020
|
|
|
|
/**
|
|
|
|
If in locked tables mode, ignore the locked tables and get
|
|
|
|
a new instance of the table.
|
|
|
|
*/
|
|
|
|
#define MYSQL_OPEN_GET_NEW_TABLE 0x0040
|
2013-06-27 16:42:18 +02:00
|
|
|
/* 0x0080 used to be MYSQL_OPEN_SKIP_TEMPORARY */
|
2010-08-13 11:51:48 +02:00
|
|
|
/** Fail instead of waiting when conficting metadata lock is discovered. */
|
|
|
|
#define MYSQL_OPEN_FAIL_ON_MDL_CONFLICT 0x0100
|
|
|
|
/** Open tables using MDL_SHARED lock instead of one specified in parser. */
|
|
|
|
#define MYSQL_OPEN_FORCE_SHARED_MDL 0x0200
|
|
|
|
/**
|
|
|
|
Open tables using MDL_SHARED_HIGH_PRIO lock instead of one specified
|
|
|
|
in parser.
|
|
|
|
*/
|
|
|
|
#define MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL 0x0400
|
|
|
|
/**
|
|
|
|
When opening or locking the table, use the maximum timeout
|
|
|
|
(LONG_TIMEOUT = 1 year) rather than the user-supplied timeout value.
|
|
|
|
*/
|
|
|
|
#define MYSQL_LOCK_IGNORE_TIMEOUT 0x0800
|
2010-09-09 16:29:14 +02:00
|
|
|
/**
|
|
|
|
When acquiring "strong" (SNW, SNRW, X) metadata locks on tables to
|
|
|
|
be open do not acquire global and schema-scope IX locks.
|
|
|
|
*/
|
|
|
|
#define MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK 0x1000
|
2011-10-19 21:45:18 +02:00
|
|
|
#define MYSQL_LOCK_NOT_TEMPORARY 0x2000
|
2017-12-22 13:56:09 +01:00
|
|
|
#define MYSQL_LOCK_USE_MALLOC 0x4000
|
2013-06-15 17:32:08 +02:00
|
|
|
/**
|
|
|
|
Only check THD::killed if waits happen (e.g. wait on MDL, wait on
|
|
|
|
table flush, wait on thr_lock.c locks) while opening and locking table.
|
|
|
|
*/
|
2013-07-21 16:39:19 +02:00
|
|
|
#define MYSQL_OPEN_IGNORE_KILLED 0x8000
|
2016-04-07 18:26:25 +02:00
|
|
|
/**
|
|
|
|
Don't try to auto-repair table
|
|
|
|
*/
|
|
|
|
#define MYSQL_OPEN_IGNORE_REPAIR 0x10000
|
2010-08-13 11:51:48 +02:00
|
|
|
|
|
|
|
/** Please refer to the internals manual. */
|
|
|
|
#define MYSQL_OPEN_REOPEN (MYSQL_OPEN_IGNORE_FLUSH |\
|
|
|
|
MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |\
|
|
|
|
MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |\
|
|
|
|
MYSQL_LOCK_IGNORE_TIMEOUT |\
|
|
|
|
MYSQL_OPEN_GET_NEW_TABLE |\
|
|
|
|
MYSQL_OPEN_HAS_MDL_LOCK)
|
|
|
|
|
2018-09-02 07:24:33 +02:00
|
|
|
bool is_locked_view(THD *thd, TABLE_LIST *t);
|
2015-03-10 10:24:20 +01:00
|
|
|
bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx);
|
2010-03-31 16:05:33 +02:00
|
|
|
|
|
|
|
bool get_key_map_from_key_list(key_map *map, TABLE *table,
|
|
|
|
List<String> *index_list);
|
|
|
|
TABLE *find_locked_table(TABLE *list, const char *db, const char *table_name);
|
|
|
|
TABLE *find_write_locked_table(TABLE *list, const char *db,
|
|
|
|
const char *table_name);
|
Committing on behalf or Dmitry Lenev:
Fix for bug #46947 "Embedded SELECT without FOR UPDATE is
causing a lock", with after-review fixes.
SELECT statements with subqueries referencing InnoDB tables
were acquiring shared locks on rows in these tables when they
were executed in REPEATABLE-READ mode and with statement or
mixed mode binary logging turned on.
This was a regression which were introduced when fixing
bug 39843.
The problem was that for tables belonging to subqueries
parser set TL_READ_DEFAULT as a lock type. In cases when
statement/mixed binary logging at open_tables() time this
type of lock was converted to TL_READ_NO_INSERT lock at
open_tables() time and caused InnoDB engine to acquire
shared locks on reads from these tables. Although in some
cases such behavior was correct (e.g. for subqueries in
DELETE) in case of SELECT it has caused unnecessary locking.
This patch tries to solve this problem by rethinking our
approach to how we handle locking for SELECT and subqueries.
Now we always set TL_READ_DEFAULT lock type for all cases
when we read data. When at open_tables() time this lock
is interpreted as TL_READ_NO_INSERT or TL_READ depending
on whether this statement as a whole or call to function
which uses particular table should be written to the
binary log or not (if yes then statement should be properly
serialized with concurrent statements and stronger lock
should be acquired).
Test coverage is added for both InnoDB and MyISAM.
This patch introduces an "incompatible" change in locking
scheme for subqueries used in SELECT ... FOR UPDATE and
SELECT .. IN SHARE MODE.
In 4.1 the server would use a snapshot InnoDB read for
subqueries in SELECT FOR UPDATE and SELECT .. IN SHARE MODE
statements, regardless of whether the binary log is on or off.
If the user required a different type of read (i.e. locking read),
he/she could request so explicitly by providing FOR UPDATE/IN SHARE MODE
clause for each individual subquery.
On of the patches for 5.0 broke this behaviour (which was not documented
or tested), and started to use locking reads fora all subqueries in SELECT ...
FOR UPDATE/IN SHARE MODE. This patch restored 4.1 behaviour.
mysql-test/include/check_concurrent_insert.inc:
Added auxiliary script which allows to check if statement
reading table allows concurrent inserts in it.
mysql-test/include/check_no_concurrent_insert.inc:
Added auxiliary script which allows to check that statement
reading table doesn't allow concurrent inserts in it.
mysql-test/include/check_no_row_lock.inc:
Added auxiliary script which allows to check if statement
reading table doesn't take locks on its rows.
mysql-test/include/check_shared_row_lock.inc:
Added auxiliary script which allows to check if statement
reading table takes shared locks on some of its rows.
mysql-test/r/bug39022.result:
After bug #46947 'Embedded SELECT without FOR UPDATE is
causing a lock' was fixed test case for bug 39022 has to
be adjusted in order to trigger execution path on which
original problem was encountered.
mysql-test/r/innodb_mysql_lock2.result:
Added coverage for handling of locking in various cases when
we read data from InnoDB tables (includes test case for
bug #46947 'Embedded SELECT without FOR UPDATE is causing a
lock').
mysql-test/r/lock_sync.result:
Added coverage for handling of locking in various cases when
we read data from MyISAM tables.
mysql-test/t/bug39022.test:
After bug #46947 'Embedded SELECT without FOR UPDATE is
causing a lock' was fixed test case for bug 39022 has to
be adjusted in order to trigger execution path on which
original problem was encountered.
mysql-test/t/innodb_mysql_lock2.test:
Added coverage for handling of locking in various cases when
we read data from InnoDB tables (includes test case for
bug #46947 'Embedded SELECT without FOR UPDATE is causing a
lock').
mysql-test/t/lock_sync.test:
Added coverage for handling of locking in various cases when
we read data from MyISAM tables.
sql/log_event.cc:
Since LEX::lock_option member was removed we no longer can
rely on its value in Load_log_event::print_query() to
determine that log event correponds to LOAD DATA CONCURRENT
statement (this was not correct in all situations anyway).
A new Load_log_event's member was introduced as a replacement.
It is initialized at event object construction time and
explicitly indicates whether LOAD DATA was concurrent.
sql/log_event.h:
Since LEX::lock_option member was removed we no longer can
rely on its value in Load_log_event::print_query() to
determine that log event correponds to LOAD DATA CONCURRENT
statement (this was not correct in all situations anyway).
A new Load_log_event's member was introduced as a replacement.
It is initialized at event object construction time and
explicitly indicates whether LOAD DATA was concurrent.
sql/sp_head.cc:
sp_head::reset_lex():
Before parsing substatement reset part of parser state
which needs this (e.g. set Yacc_state::m_lock_type to
default value).
sql/sql_acl.cc:
Since LEX::reset_n_backup_query_tables_list() now also
resets LEX::sql_command member (as it became part of
Query_tables_list class) we have to restore it in cases
when while working with proxy Query_table_list we assume
that LEX::sql_command still corresponds to original SQL
command being executed (for example, when we are logging
statement to the binary log while having Query_tables_list
reset and backed up).
sql/sql_base.cc:
Changed read_lock_type_for_table() to return a weak TL_READ
type of lock in cases when we are executing statement which
won't update tables directly and table doesn't belong to
statement's prelocking list and thus can't be used by a
stored function. It is OK to do so since in this case table
won't be used by statement or function call which will be
written to the binary log, so serializability requirements
for it can be relaxed.
One of results from this change is that SELECTs on InnoDB
tables no longer takes shared row locks for tables which
are used in subqueries (i.e. bug #46947 is fixed).
Another result is that for similar SELECTs on MyISAM tables
concurrent inserts are allowed.
In order to implement this change signature of
read_lock_type_for_table() function was changed to take
pointers to Query_tables_list and TABLE_LIST objects.
sql/sql_base.h:
- Function read_lock_type_for_table() now takes pointers
to Query_tables_list and TABLE_LIST elements as its
arguments since to correctly determine lock type it needs
to know what statement is being performed and whether table
element for which lock type to be determined belongs to
prelocking list.
sql/sql_lex.cc:
- Removed LEX::lock_option and st_select_lex::lock_option
members. Places in parser that were using them now use
Yacc_state::m_lock_type instead.
- To emphasize that LEX::sql_command member is used during
process of opening and locking of tables it was moved to
Query_tables_list class. It is now reset by
Query_tables_list::reset_query_tables_list() method.
sql/sql_lex.h:
- Removed st_select_lex::lock_option member as there is no
real need for per-SELECT lock type (HIGH_PRIORITY option
should apply to the whole statement. FOR UPDATE/LOCK IN
SHARE MODE clauses can be handled without this member).
The main effect which was achieved by introduction of this
member, i.e. using TL_READ_DEFAULT lock type for
subqueries, is now achieved by setting LEX::lock_option
(or rather its replacement - Yacc_state::m_lock_type) to
TL_READ_DEFAULT in almost all cases.
- To emphasize that LEX::sql_command member is used during
process of opening and locking of tables it was moved to
Query_tables_list class.
- Replaced LEX::lock_option with Yacc_state::m_lock_type
in order to emphasize that this value is relevant only
during parsing. Unlike for LEX::lock_option the default
value for Yacc_state::m_lock_type is TL_READ_DEFAULT.
Note that for cases when it is OK to take a "weak" read
lock (e.g. simple SELECT) this lock type will be converted
to TL_READ at open_tables() time. So this change won't
cause negative change in behavior for such statements.
OTOH this change ensures that, for example, for SELECTs
which are used in stored functions TL_READ_NO_INSERT lock
is taken when necessary and as result calls to such stored
functions can be written to the binary log with correct
serialization.
sql/sql_load.cc:
Load_log_event constructor now requires a parameter that
indicates whether LOAD DATA is concurrent.
sql/sql_parse.cc:
LEX::lock_option was replaced with Yacc_state::m_lock_type.
And instead of resetting the latter implicitly in
mysql_init_multi_delete() we do it explicitly in the
places in parser which call this function.
sql/sql_priv.h:
- To be able more easily distinguish high-priority SELECTs
in st_select_lex::print() method added flag for
HIGH_PRIORITY option.
sql/sql_select.cc:
Changed code not to rely on LEX::lock_option to determine
that it is high-priority SELECT. It was replaced with
Yacc_state::m_lock_type which is accessible only at
parse time. So instead of LEX::lock_option we now rely
on a newly introduced flag for st_select_lex::options -
SELECT_HIGH_PRIORITY.
sql/sql_show.cc:
Since LEX::reset_n_backup_query_tables_list() now also
resets LEX::sql_command member (as it became part of
Query_tables_list class) we have to restore it in cases
when while working with proxy Query_table_list we assume
that LEX::sql_command still corresponds to original SQL
command being executed.
sql/sql_table.cc:
Since LEX::reset_query_tables_list() now also resets
LEX::sql_command member (as it became part of
Query_tables_list class) we have to restore value of this
member when this method is called by mysql_admin_table(),
to make this code safe for re-execution.
sql/sql_trigger.cc:
Since LEX::reset_n_backup_query_tables_list() now also
resets LEX::sql_command member (as it became part of
Query_tables_list class) we have to restore it in cases
when while working with proxy Query_table_list we assume
that LEX::sql_command still corresponds to original SQL
command being executed (for example, when we are logging
statement to the binary log while having Query_tables_list
reset and backed up).
sql/sql_update.cc:
Function read_lock_type_for_table() now takes pointers
to Query_tables_list and TABLE_LIST elements as its
arguments since to correctly determine lock type it needs
to know what statement is being performed and whether table
element for which lock type to be determined belongs to
prelocking list.
sql/sql_yacc.yy:
- Removed st_select_lex::lock_option member as there is no
real need for per-SELECT lock type (HIGH_PRIORITY option
should apply to the whole statement. FOR UPDATE/LOCK IN
SHARE MODE clauses can be handled without this member).
The main effect which was achieved by introduction of this
member, i.e. using TL_READ_DEFAULT lock type for
subqueries, is now achieved by setting LEX::lock_option
(or rather its replacement - Yacc_state::m_lock_type) to
TL_READ_DEFAULT in almost all cases.
- Replaced LEX::lock_option with Yacc_state::m_lock_type
in order to emphasize that this value is relevant only
during parsing. Unlike for LEX::lock_option the default
value for Yacc_state::m_lock_type is TL_READ_DEFAULT.
Note that for cases when it is OK to take a "weak" read
lock (e.g. simple SELECT) this lock type will be converted
to TL_READ at open_tables() time. So this change won't
cause negative change in behavior for such statements.
OTOH this change ensures that, for example, for SELECTs
which are used in stored functions TL_READ_NO_INSERT lock
is taken when necessary and as result calls to such stored
functions can be written to the binary log with correct
serialization.
- To be able more easily distinguish high-priority SELECTs
in st_select_lex::print() method we now use new flag
in st_select_lex::options bit-field.
2010-04-28 12:04:11 +02:00
|
|
|
thr_lock_type read_lock_type_for_table(THD *thd,
|
|
|
|
Query_tables_list *prelocking_ctx,
|
2014-07-31 12:03:20 +02:00
|
|
|
TABLE_LIST *table_list,
|
|
|
|
bool routine_modifies_data);
|
2010-03-31 16:05:33 +02:00
|
|
|
|
|
|
|
my_bool mysql_rm_tmp_tables(void);
|
|
|
|
void close_tables_for_reopen(THD *thd, TABLE_LIST **tables,
|
2010-11-11 18:11:05 +01:00
|
|
|
const MDL_savepoint &start_of_statement_svp);
|
2018-09-26 20:49:51 +02:00
|
|
|
bool table_already_fk_prelocked(TABLE_LIST *tl, LEX_CSTRING *db,
|
|
|
|
LEX_CSTRING *table, thr_lock_type lock_type);
|
2010-03-31 16:05:33 +02:00
|
|
|
TABLE_LIST *find_table_in_list(TABLE_LIST *table,
|
|
|
|
TABLE_LIST *TABLE_LIST::*link,
|
2018-01-07 17:03:44 +01:00
|
|
|
const LEX_CSTRING *db_name,
|
|
|
|
const LEX_CSTRING *table_name);
|
2010-03-31 16:05:33 +02:00
|
|
|
void close_thread_tables(THD *thd);
|
2015-11-14 22:51:54 +01:00
|
|
|
void switch_to_nullable_trigger_fields(List<Item> &items, TABLE *);
|
2016-06-29 21:27:34 +02:00
|
|
|
void switch_defaults_to_nullable_trigger_fields(TABLE *table);
|
2012-10-17 14:43:56 +02:00
|
|
|
bool fill_record_n_invoke_before_triggers(THD *thd, TABLE *table,
|
|
|
|
List<Item> &fields,
|
2010-03-31 16:05:33 +02:00
|
|
|
List<Item> &values,
|
|
|
|
bool ignore_errors,
|
|
|
|
enum trg_event_type event);
|
2012-10-17 14:43:56 +02:00
|
|
|
bool fill_record_n_invoke_before_triggers(THD *thd, TABLE *table,
|
|
|
|
Field **field,
|
2010-03-31 16:05:33 +02:00
|
|
|
List<Item> &values,
|
|
|
|
bool ignore_errors,
|
|
|
|
enum trg_event_type event);
|
|
|
|
bool insert_fields(THD *thd, Name_resolution_context *context,
|
|
|
|
const char *db_name, const char *table_name,
|
2018-05-14 22:14:03 +02:00
|
|
|
List_iterator<Item> *it, bool any_privileges,
|
|
|
|
uint *hidden_bit_fields);
|
2015-04-17 12:30:15 +02:00
|
|
|
void make_leaves_list(THD *thd, List<TABLE_LIST> &list, TABLE_LIST *tables,
|
2011-10-19 21:45:18 +02:00
|
|
|
bool full_table_list, TABLE_LIST *boundary);
|
2010-03-31 16:05:33 +02:00
|
|
|
int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
|
2018-05-14 22:14:03 +02:00
|
|
|
List<Item> *sum_func_list, uint wild_num, uint * hidden_bit_fields);
|
2016-02-09 21:35:59 +01:00
|
|
|
bool setup_fields(THD *thd, Ref_ptr_array ref_pointer_array,
|
2018-02-09 19:43:42 +01:00
|
|
|
List<Item> &item, enum_column_usage column_usage,
|
2017-02-18 17:47:31 +01:00
|
|
|
List<Item> *sum_func_list, List<Item> *pre_fix,
|
|
|
|
bool allow_sum_func);
|
2011-10-19 21:45:18 +02:00
|
|
|
void unfix_fields(List<Item> &items);
|
2013-06-15 17:32:08 +02:00
|
|
|
bool fill_record(THD * thd, TABLE *table_arg, List<Item> &fields,
|
2016-06-29 09:14:22 +02:00
|
|
|
List<Item> &values, bool ignore_errors, bool update);
|
2012-10-17 14:43:56 +02:00
|
|
|
bool fill_record(THD *thd, TABLE *table, Field **field, List<Item> &values,
|
2010-11-25 18:17:28 +01:00
|
|
|
bool ignore_errors, bool use_value);
|
2010-03-31 16:05:33 +02:00
|
|
|
|
|
|
|
Field *
|
|
|
|
find_field_in_tables(THD *thd, Item_ident *item,
|
|
|
|
TABLE_LIST *first_table, TABLE_LIST *last_table,
|
|
|
|
Item **ref, find_item_error_report_type report_error,
|
|
|
|
bool check_privileges, bool register_tree_change);
|
|
|
|
Field *
|
|
|
|
find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
|
2018-02-06 13:55:58 +01:00
|
|
|
const char *name, size_t length,
|
2010-03-31 16:05:33 +02:00
|
|
|
const char *item_name, const char *db_name,
|
|
|
|
const char *table_name, Item **ref,
|
|
|
|
bool check_privileges, bool allow_rowid,
|
|
|
|
uint *cached_field_index_ptr,
|
|
|
|
bool register_tree_change, TABLE_LIST **actual_table);
|
|
|
|
Field *
|
2018-02-06 13:55:58 +01:00
|
|
|
find_field_in_table(THD *thd, TABLE *table, const char *name, size_t length,
|
2010-03-31 16:05:33 +02:00
|
|
|
bool allow_rowid, uint *cached_field_index_ptr);
|
|
|
|
Field *
|
|
|
|
find_field_in_table_sef(TABLE *table, const char *name);
|
|
|
|
Item ** find_item_in_list(Item *item, List<Item> &items, uint *counter,
|
|
|
|
find_item_error_report_type report_error,
|
2016-04-14 09:47:28 +02:00
|
|
|
enum_resolution_type *resolution, uint limit= 0);
|
2010-03-31 16:05:33 +02:00
|
|
|
bool setup_tables(THD *thd, Name_resolution_context *context,
|
|
|
|
List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
|
2011-10-19 21:45:18 +02:00
|
|
|
List<TABLE_LIST> &leaves, bool select_insert,
|
|
|
|
bool full_table_list);
|
2010-03-31 16:05:33 +02:00
|
|
|
bool setup_tables_and_check_access(THD *thd,
|
|
|
|
Name_resolution_context *context,
|
|
|
|
List<TABLE_LIST> *from_clause,
|
|
|
|
TABLE_LIST *tables,
|
2011-10-19 21:45:18 +02:00
|
|
|
List<TABLE_LIST> &leaves,
|
2010-03-31 16:05:33 +02:00
|
|
|
bool select_insert,
|
|
|
|
ulong want_access_first,
|
2011-10-19 21:45:18 +02:00
|
|
|
ulong want_access,
|
|
|
|
bool full_table_list);
|
2010-03-31 16:05:33 +02:00
|
|
|
bool wait_while_table_is_used(THD *thd, TABLE *table,
|
2013-08-12 15:46:35 +02:00
|
|
|
enum ha_extra_function function);
|
2010-03-31 16:05:33 +02:00
|
|
|
|
2018-01-07 17:03:44 +01:00
|
|
|
void drop_open_table(THD *thd, TABLE *table, const LEX_CSTRING *db_name,
|
|
|
|
const LEX_CSTRING *table_name);
|
2010-03-31 16:05:33 +02:00
|
|
|
void update_non_unique_table_error(TABLE_LIST *update,
|
|
|
|
const char *operation,
|
|
|
|
TABLE_LIST *duplicate);
|
2011-10-19 21:45:18 +02:00
|
|
|
int setup_conds(THD *thd, TABLE_LIST *tables, List<TABLE_LIST> &leaves,
|
2010-03-31 16:05:33 +02:00
|
|
|
COND **conds);
|
2011-10-19 21:45:18 +02:00
|
|
|
void wrap_ident(THD *thd, Item **conds);
|
2010-03-31 16:05:33 +02:00
|
|
|
int setup_ftfuncs(SELECT_LEX* select);
|
2018-05-15 12:10:19 +02:00
|
|
|
void cleanup_ftfuncs(SELECT_LEX *select_lex);
|
2010-03-31 16:05:33 +02:00
|
|
|
int init_ftfuncs(THD *thd, SELECT_LEX* select, bool no_order);
|
2014-12-08 07:56:08 +01:00
|
|
|
bool lock_table_names(THD *thd, const DDL_options_st &options,
|
|
|
|
TABLE_LIST *table_list,
|
2010-07-01 15:53:46 +02:00
|
|
|
TABLE_LIST *table_list_end, ulong lock_wait_timeout,
|
|
|
|
uint flags);
|
2014-12-08 07:56:08 +01:00
|
|
|
static inline bool
|
|
|
|
lock_table_names(THD *thd, TABLE_LIST *table_list,
|
|
|
|
TABLE_LIST *table_list_end, ulong lock_wait_timeout,
|
|
|
|
uint flags)
|
|
|
|
{
|
|
|
|
return lock_table_names(thd, thd->lex->create_info, table_list,
|
|
|
|
table_list_end, lock_wait_timeout, flags);
|
|
|
|
}
|
|
|
|
bool open_tables(THD *thd, const DDL_options_st &options,
|
2019-04-26 14:02:37 +02:00
|
|
|
TABLE_LIST **tables, uint *counter,
|
2019-06-12 22:54:46 +02:00
|
|
|
uint flags, Prelocking_strategy *prelocking_strategy);
|
2019-04-26 14:02:37 +02:00
|
|
|
|
2014-12-08 07:56:08 +01:00
|
|
|
static inline bool
|
|
|
|
open_tables(THD *thd, TABLE_LIST **tables, uint *counter, uint flags,
|
|
|
|
Prelocking_strategy *prelocking_strategy)
|
|
|
|
{
|
|
|
|
return open_tables(thd, thd->lex->create_info, tables, counter, flags,
|
|
|
|
prelocking_strategy);
|
|
|
|
}
|
2010-03-31 16:05:33 +02:00
|
|
|
/* open_and_lock_tables with optional derived handling */
|
2014-12-08 07:56:08 +01:00
|
|
|
bool open_and_lock_tables(THD *thd, const DDL_options_st &options,
|
|
|
|
TABLE_LIST *tables,
|
2010-03-31 16:05:33 +02:00
|
|
|
bool derived, uint flags,
|
|
|
|
Prelocking_strategy *prelocking_strategy);
|
2014-12-08 07:56:08 +01:00
|
|
|
static inline bool
|
|
|
|
open_and_lock_tables(THD *thd, TABLE_LIST *tables,
|
|
|
|
bool derived, uint flags,
|
|
|
|
Prelocking_strategy *prelocking_strategy)
|
|
|
|
{
|
|
|
|
return open_and_lock_tables(thd, thd->lex->create_info,
|
|
|
|
tables, derived, flags, prelocking_strategy);
|
|
|
|
}
|
2010-03-31 16:05:33 +02:00
|
|
|
/* simple open_and_lock_tables without derived handling for single table */
|
|
|
|
TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
|
2010-09-15 16:15:31 +02:00
|
|
|
thr_lock_type lock_type, uint flags,
|
|
|
|
Prelocking_strategy *prelocking_strategy);
|
2011-10-19 21:45:18 +02:00
|
|
|
bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags,
|
|
|
|
uint dt_phases);
|
2016-12-06 06:05:52 +01:00
|
|
|
bool open_tables_only_view_structure(THD *thd, TABLE_LIST *tables,
|
|
|
|
bool can_deadlock);
|
2017-12-22 13:56:09 +01:00
|
|
|
bool open_and_lock_internal_tables(TABLE *table, bool lock);
|
2010-03-31 16:05:33 +02:00
|
|
|
bool lock_tables(THD *thd, TABLE_LIST *tables, uint counter, uint flags);
|
|
|
|
int decide_logging_format(THD *thd, TABLE_LIST *tables);
|
2013-06-15 17:32:08 +02:00
|
|
|
void close_thread_table(THD *thd, TABLE **table_ptr);
|
2010-03-31 16:05:33 +02:00
|
|
|
TABLE_LIST *unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
|
2018-05-16 20:51:46 +02:00
|
|
|
uint check_flag);
|
2017-04-23 18:39:57 +02:00
|
|
|
bool is_equal(const LEX_CSTRING *a, const LEX_CSTRING *b);
|
2010-03-31 16:05:33 +02:00
|
|
|
|
2012-09-28 14:27:16 +02:00
|
|
|
class Open_tables_backup;
|
2010-03-31 16:05:33 +02:00
|
|
|
/* Functions to work with system tables. */
|
|
|
|
bool open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
|
|
|
|
Open_tables_backup *backup);
|
|
|
|
void close_system_tables(THD *thd, Open_tables_backup *backup);
|
A pre-requisite patch for the fix for Bug#52044.
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().
2010-07-27 12:25:53 +02:00
|
|
|
void close_mysql_tables(THD *thd);
|
2010-03-31 16:05:33 +02:00
|
|
|
TABLE *open_system_table_for_update(THD *thd, TABLE_LIST *one_table);
|
|
|
|
TABLE *open_log_table(THD *thd, TABLE_LIST *one_table, Open_tables_backup *backup);
|
|
|
|
void close_log_table(THD *thd, Open_tables_backup *backup);
|
|
|
|
|
2010-08-12 15:50:23 +02:00
|
|
|
bool close_cached_tables(THD *thd, TABLE_LIST *tables,
|
2010-07-27 15:34:58 +02:00
|
|
|
bool wait_for_refresh, ulong timeout);
|
2018-10-09 17:55:18 +02:00
|
|
|
void purge_tables(bool purge_flag);
|
2018-11-06 16:05:24 +01:00
|
|
|
bool flush_tables(THD *thd, flush_tables_type flag);
|
2017-04-23 18:39:57 +02:00
|
|
|
bool close_cached_connection_tables(THD *thd, LEX_CSTRING *connect_string);
|
2010-03-31 16:05:33 +02:00
|
|
|
void close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
|
2013-06-15 17:32:08 +02:00
|
|
|
ha_extra_function extra,
|
|
|
|
TABLE *skip_table);
|
2010-03-31 16:05:33 +02:00
|
|
|
OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild);
|
2015-12-29 16:09:11 +01:00
|
|
|
bool tdc_open_view(THD *thd, TABLE_LIST *table_list, uint flags);
|
2013-04-09 15:35:24 +02:00
|
|
|
|
2011-03-07 10:08:10 +01:00
|
|
|
TABLE *find_table_for_mdl_upgrade(THD *thd, const char *db,
|
2010-03-31 16:05:33 +02:00
|
|
|
const char *table_name,
|
2018-09-02 07:24:33 +02:00
|
|
|
int *p_error);
|
2010-03-31 16:05:33 +02:00
|
|
|
void mark_tmp_table_for_reuse(TABLE *table);
|
2013-04-09 15:35:15 +02:00
|
|
|
|
2011-10-19 21:45:18 +02:00
|
|
|
int dynamic_column_error_message(enum_dyncol_func_result rc);
|
2010-03-31 16:05:33 +02:00
|
|
|
|
2012-03-19 09:35:32 +01:00
|
|
|
/* open_and_lock_tables with optional derived handling */
|
|
|
|
int open_and_lock_tables_derived(THD *thd, TABLE_LIST *tables, bool derived);
|
|
|
|
|
|
|
|
extern "C" int simple_raw_key_cmp(void* arg, const void* key1,
|
|
|
|
const void* key2);
|
|
|
|
extern "C" int count_distinct_walk(void *elem, element_count count, void *arg);
|
|
|
|
int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2);
|
|
|
|
|
2010-03-31 16:05:33 +02:00
|
|
|
extern Item **not_found_item;
|
|
|
|
extern Field *not_found_field;
|
|
|
|
extern Field *view_ref_found;
|
|
|
|
|
|
|
|
/**
|
|
|
|
clean/setup table fields and map.
|
|
|
|
|
|
|
|
@param table TABLE structure pointer (which should be setup)
|
|
|
|
@param table_list TABLE_LIST structure pointer (owner of TABLE)
|
|
|
|
@param tablenr table number
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
inline void setup_table_map(TABLE *table, TABLE_LIST *table_list, uint tablenr)
|
|
|
|
{
|
|
|
|
table->used_fields= 0;
|
2011-10-19 21:45:18 +02:00
|
|
|
table_list->reset_const_table();
|
2010-03-31 16:05:33 +02:00
|
|
|
table->null_row= 0;
|
|
|
|
table->status= STATUS_NO_RECORD;
|
|
|
|
table->maybe_null= table_list->outer_join;
|
|
|
|
TABLE_LIST *embedding= table_list->embedding;
|
|
|
|
while (!table->maybe_null && embedding)
|
|
|
|
{
|
|
|
|
table->maybe_null= embedding->outer_join;
|
|
|
|
embedding= embedding->embedding;
|
|
|
|
}
|
|
|
|
table->tablenr= tablenr;
|
|
|
|
table->map= (table_map) 1 << tablenr;
|
|
|
|
table->force_index= table_list->force_index;
|
|
|
|
table->force_index_order= table->force_index_group= 0;
|
|
|
|
table->covering_keys= table->s->keys_for_keyread;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TABLE_LIST *find_table_in_global_list(TABLE_LIST *table,
|
2018-01-07 17:03:44 +01:00
|
|
|
LEX_CSTRING *db_name,
|
|
|
|
LEX_CSTRING *table_name)
|
2010-03-31 16:05:33 +02:00
|
|
|
{
|
|
|
|
return find_table_in_list(table, &TABLE_LIST::next_global,
|
|
|
|
db_name, table_name);
|
|
|
|
}
|
|
|
|
|
2016-02-09 21:35:59 +01:00
|
|
|
inline bool setup_fields_with_no_wrap(THD *thd, Ref_ptr_array ref_pointer_array,
|
2010-03-31 16:05:33 +02:00
|
|
|
List<Item> &item,
|
2018-02-09 19:43:42 +01:00
|
|
|
enum_column_usage column_usage,
|
2010-03-31 16:05:33 +02:00
|
|
|
List<Item> *sum_func_list,
|
|
|
|
bool allow_sum_func)
|
|
|
|
{
|
|
|
|
bool res;
|
2018-05-22 19:08:39 +02:00
|
|
|
SELECT_LEX *first= thd->lex->first_select_lex();
|
|
|
|
DBUG_ASSERT(thd->lex->current_select == first);
|
|
|
|
first->no_wrap_view_item= TRUE;
|
2018-02-09 19:43:42 +01:00
|
|
|
res= setup_fields(thd, ref_pointer_array, item, column_usage,
|
2017-02-18 17:47:31 +01:00
|
|
|
sum_func_list, NULL, allow_sum_func);
|
2018-05-22 19:08:39 +02:00
|
|
|
first->no_wrap_view_item= FALSE;
|
2010-03-31 16:05:33 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-06-01 12:19:05 +02:00
|
|
|
/**
|
|
|
|
An abstract class for a strategy specifying how the prelocking
|
|
|
|
algorithm should extend the prelocking set while processing
|
|
|
|
already existing elements in the set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Prelocking_strategy
|
|
|
|
{
|
|
|
|
public:
|
2023-02-07 12:57:20 +01:00
|
|
|
virtual ~Prelocking_strategy() = default;
|
2010-06-01 12:19:05 +02:00
|
|
|
|
2019-05-22 21:56:36 +02:00
|
|
|
virtual void reset(THD *thd) { };
|
2010-06-01 12:19:05 +02:00
|
|
|
virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
Sroutine_hash_entry *rt, sp_head *sp,
|
|
|
|
bool *need_prelocking) = 0;
|
|
|
|
virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
TABLE_LIST *table_list, bool *need_prelocking) = 0;
|
|
|
|
virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
TABLE_LIST *table_list, bool *need_prelocking)= 0;
|
2019-05-22 21:56:36 +02:00
|
|
|
virtual bool handle_end(THD *thd) { return 0; };
|
2010-06-01 12:19:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
A Strategy for prelocking algorithm suitable for DML statements.
|
|
|
|
|
|
|
|
Ensures that all tables used by all statement's SF/SP/triggers and
|
|
|
|
required for foreign key checks are prelocked and SF/SPs used are
|
|
|
|
cached.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DML_prelocking_strategy : public Prelocking_strategy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
Sroutine_hash_entry *rt, sp_head *sp,
|
|
|
|
bool *need_prelocking);
|
|
|
|
virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
TABLE_LIST *table_list, bool *need_prelocking);
|
|
|
|
virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
TABLE_LIST *table_list, bool *need_prelocking);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
A strategy for prelocking algorithm to be used for LOCK TABLES
|
|
|
|
statement.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Lock_tables_prelocking_strategy : public DML_prelocking_strategy
|
|
|
|
{
|
|
|
|
virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
TABLE_LIST *table_list, bool *need_prelocking);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Strategy for prelocking algorithm to be used for ALTER TABLE statements.
|
|
|
|
|
|
|
|
Unlike DML or LOCK TABLES strategy, it doesn't
|
|
|
|
prelock triggers, views or stored routines, since they are not
|
|
|
|
used during ALTER.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Alter_table_prelocking_strategy : public Prelocking_strategy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
Sroutine_hash_entry *rt, sp_head *sp,
|
|
|
|
bool *need_prelocking);
|
|
|
|
virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
TABLE_LIST *table_list, bool *need_prelocking);
|
|
|
|
virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
|
|
|
|
TABLE_LIST *table_list, bool *need_prelocking);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-12-08 07:56:08 +01:00
|
|
|
inline bool
|
|
|
|
open_tables(THD *thd, const DDL_options_st &options,
|
|
|
|
TABLE_LIST **tables, uint *counter, uint flags)
|
|
|
|
{
|
|
|
|
DML_prelocking_strategy prelocking_strategy;
|
|
|
|
|
|
|
|
return open_tables(thd, options, tables, counter, flags,
|
|
|
|
&prelocking_strategy);
|
|
|
|
}
|
2010-03-31 16:05:33 +02:00
|
|
|
inline bool
|
|
|
|
open_tables(THD *thd, TABLE_LIST **tables, uint *counter, uint flags)
|
|
|
|
{
|
|
|
|
DML_prelocking_strategy prelocking_strategy;
|
|
|
|
|
2014-12-08 07:56:08 +01:00
|
|
|
return open_tables(thd, thd->lex->create_info, tables, counter, flags,
|
|
|
|
&prelocking_strategy);
|
2010-03-31 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2010-09-15 16:15:31 +02:00
|
|
|
inline TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
|
|
|
|
thr_lock_type lock_type, uint flags)
|
|
|
|
{
|
|
|
|
DML_prelocking_strategy prelocking_strategy;
|
|
|
|
|
|
|
|
return open_n_lock_single_table(thd, table_l, lock_type, flags,
|
|
|
|
&prelocking_strategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-31 16:05:33 +02:00
|
|
|
/* open_and_lock_tables with derived handling */
|
2014-12-08 07:56:08 +01:00
|
|
|
inline bool open_and_lock_tables(THD *thd,
|
|
|
|
const DDL_options_st &options,
|
|
|
|
TABLE_LIST *tables,
|
2010-03-31 16:05:33 +02:00
|
|
|
bool derived, uint flags)
|
|
|
|
{
|
|
|
|
DML_prelocking_strategy prelocking_strategy;
|
|
|
|
|
2014-12-08 07:56:08 +01:00
|
|
|
return open_and_lock_tables(thd, options, tables, derived, flags,
|
|
|
|
&prelocking_strategy);
|
|
|
|
}
|
|
|
|
inline bool open_and_lock_tables(THD *thd, TABLE_LIST *tables,
|
|
|
|
bool derived, uint flags)
|
|
|
|
{
|
|
|
|
DML_prelocking_strategy prelocking_strategy;
|
|
|
|
|
|
|
|
return open_and_lock_tables(thd, thd->lex->create_info,
|
|
|
|
tables, derived, flags,
|
2010-03-31 16:05:33 +02:00
|
|
|
&prelocking_strategy);
|
|
|
|
}
|
|
|
|
|
2010-06-01 12:19:05 +02:00
|
|
|
|
2014-01-29 14:37:17 +01:00
|
|
|
bool restart_trans_for_tables(THD *thd, TABLE_LIST *table);
|
|
|
|
|
2019-04-03 17:21:10 +02:00
|
|
|
bool extend_table_list(THD *thd, TABLE_LIST *tables,
|
|
|
|
Prelocking_strategy *prelocking_strategy,
|
|
|
|
bool has_prelocking_list);
|
|
|
|
|
MDEV-25576: The statement EXPLAIN running as regular statement and as prepared statement produces different results for UPDATE with subquery
Both EXPLAIN and EXPLAIN EXTENDED statements produce different results set
in case it is run in normal way and in PS mode for the statements
UPDATE/DELETE with subquery.
The use case below reproduces the issue:
MariaDB [test]> CREATE TABLE t1 (c1 INT KEY) ENGINE=MyISAM;
Query OK, 0 rows affected (0,128 sec)
MariaDB [test]> CREATE TABLE t2 (c2 INT) ENGINE=MyISAM;
Query OK, 0 rows affected (0,023 sec)
MariaDB [test]> CREATE TABLE t3 (c3 INT) ENGINE=MyISAM;
Query OK, 0 rows affected (0,021 sec)
MariaDB [test]> EXPLAIN EXTENDED UPDATE t3 SET c3 =
-> ( SELECT COUNT(d1.c1) FROM ( SELECT a11.c1 FROM t1 AS a11
-> STRAIGHT_JOIN t2 AS a21 ON a21.c2 = a11.c1 JOIN t1 AS a12
-> ON a12.c1 = a11.c1 ) d1 );
+------+-------------+-------+------+---------------+------+---------+------+------+----------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+--------------------------------+
| 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 0 | 100.00 | |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables
+------+-------------+-------+------+---------------+------+---------+------+------+----------+--------------------------------+
2 rows in set (0,002 sec)
MariaDB [test]> PREPARE stmt FROM
-> EXPLAIN EXTENDED UPDATE t3 SET c3 =
-> ( SELECT COUNT(d1.c1) FROM ( SELECT a11.c1 FROM t1 AS a11
-> STRAIGHT_JOIN t2 AS a21 ON a21.c2 = a11.c1 JOIN t1 AS a12
-> ON a12.c1 = a11.c1 ) d1 );
Query OK, 0 rows affected (0,000 sec)
Statement prepared
MariaDB [test]> EXECUTE stmt;
+------+-------------+-------+------+---------------+------+---------+------+------+----------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+--------------------------------+
| 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 0 | 100.00 | |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | no matching row in const table |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+--------------------------------+
2 rows in set (0,000 sec)
The reason by that different result sets are produced is that on execution
of the statement 'EXECUTE stmt' the flag SELECT_DESCRIBE not set
in the data member SELECT_LEX::options for instances of SELECT_LEX that
correspond to subqueries used in the UPDTAE/DELETE statements.
Initially, these flags were set on parsing the statement
PREPARE stmt FROM "EXPLAIN EXTENDED UPDATE t3 SET ..."
but latter they were reset before starting real execution of
the parsed query during handling the statement 'EXECUTE stmt';
So, to fix the issue the functions mysql_update()/mysql_delete()
have been modified to set the flag SELECT_DESCRIBE forcibly
in the data member SELECT_LEX::options for the primary SELECT_LEX
of the UPDATE/DELETE statement.
2021-05-30 12:31:55 +02:00
|
|
|
void promote_select_describe_flag_if_needed(LEX *lex);
|
|
|
|
|
2010-06-01 12:19:05 +02:00
|
|
|
/**
|
|
|
|
A context of open_tables() function, used to recover
|
|
|
|
from a failed open_table() or open_routine() attempt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Open_table_context
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum enum_open_table_action
|
|
|
|
{
|
|
|
|
OT_NO_ACTION= 0,
|
2010-08-12 15:50:23 +02:00
|
|
|
OT_BACKOFF_AND_RETRY,
|
2010-07-27 15:34:58 +02:00
|
|
|
OT_REOPEN_TABLES,
|
2010-06-01 12:19:05 +02:00
|
|
|
OT_DISCOVER,
|
|
|
|
OT_REPAIR
|
|
|
|
};
|
2010-06-01 12:49:35 +02:00
|
|
|
Open_table_context(THD *thd, uint flags);
|
2010-06-01 12:19:05 +02:00
|
|
|
|
2013-08-20 11:12:34 +02:00
|
|
|
bool recover_from_failed_open();
|
2010-06-01 12:19:05 +02:00
|
|
|
bool request_backoff_action(enum_open_table_action action_arg,
|
2010-06-07 09:06:55 +02:00
|
|
|
TABLE_LIST *table);
|
2010-06-01 12:19:05 +02:00
|
|
|
|
|
|
|
bool can_recover_from_failed_open() const
|
|
|
|
{ return m_action != OT_NO_ACTION; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
When doing a back-off, we close all tables acquired by this
|
|
|
|
statement. Return an MDL savepoint taken at the beginning of
|
|
|
|
the statement, so that we can rollback to it before waiting on
|
|
|
|
locks.
|
|
|
|
*/
|
2010-11-11 18:11:05 +01:00
|
|
|
const MDL_savepoint &start_of_statement_svp() const
|
2010-06-01 12:19:05 +02:00
|
|
|
{
|
|
|
|
return m_start_of_statement_svp;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ulong get_timeout() const
|
|
|
|
{
|
|
|
|
return m_timeout;
|
|
|
|
}
|
|
|
|
|
2010-06-01 12:49:35 +02:00
|
|
|
uint get_flags() const { return m_flags; }
|
2010-11-11 18:11:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
Set flag indicating that we have already acquired metadata lock
|
|
|
|
protecting this statement against GRL while opening tables.
|
|
|
|
*/
|
2018-11-06 16:05:24 +01:00
|
|
|
void set_has_protection_against_grl(enum_mdl_type mdl_type)
|
2010-11-11 18:11:05 +01:00
|
|
|
{
|
2018-11-06 16:05:24 +01:00
|
|
|
m_has_protection_against_grl|= MDL_BIT(mdl_type);
|
2010-11-11 18:11:05 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 16:05:24 +01:00
|
|
|
bool has_protection_against_grl(enum_mdl_type mdl_type) const
|
2010-11-11 18:11:05 +01:00
|
|
|
{
|
2018-11-06 16:05:24 +01:00
|
|
|
return (bool) (m_has_protection_against_grl & MDL_BIT(mdl_type));
|
2010-11-11 18:11:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-01 12:19:05 +02:00
|
|
|
private:
|
2013-08-20 11:12:34 +02:00
|
|
|
/* THD for which tables are opened. */
|
|
|
|
THD *m_thd;
|
2010-06-01 12:19:05 +02:00
|
|
|
/**
|
|
|
|
For OT_DISCOVER and OT_REPAIR actions, the table list element for
|
|
|
|
the table which definition should be re-discovered or which
|
|
|
|
should be repaired.
|
|
|
|
*/
|
|
|
|
TABLE_LIST *m_failed_table;
|
2010-11-11 18:11:05 +01:00
|
|
|
MDL_savepoint m_start_of_statement_svp;
|
2010-06-01 12:19:05 +02:00
|
|
|
/**
|
|
|
|
Lock timeout in seconds. Initialized to LONG_TIMEOUT when opening system
|
|
|
|
tables or to the "lock_wait_timeout" system variable for regular tables.
|
|
|
|
*/
|
2010-06-01 12:49:35 +02:00
|
|
|
ulong m_timeout;
|
|
|
|
/* open_table() flags. */
|
|
|
|
uint m_flags;
|
|
|
|
/** Back off action. */
|
|
|
|
enum enum_open_table_action m_action;
|
|
|
|
/**
|
|
|
|
Whether we had any locks when this context was created.
|
|
|
|
If we did, they are from the previous statement of a transaction,
|
|
|
|
and we can't safely do back-off (and release them).
|
|
|
|
*/
|
|
|
|
bool m_has_locks;
|
2010-11-11 18:11:05 +01:00
|
|
|
/**
|
|
|
|
Indicates that in the process of opening tables we have acquired
|
|
|
|
protection against global read lock.
|
|
|
|
*/
|
2018-11-06 16:05:24 +01:00
|
|
|
mdl_bitmap_t m_has_protection_against_grl;
|
2010-06-01 12:19:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-15 17:32:08 +02:00
|
|
|
/**
|
|
|
|
Check if a TABLE_LIST instance represents a pre-opened temporary table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline bool is_temporary_table(TABLE_LIST *tl)
|
|
|
|
{
|
|
|
|
if (tl->view || tl->schema_table)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!tl->table)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
NOTE: 'table->s' might be NULL for specially constructed TABLE
|
|
|
|
instances. See SHOW TRIGGERS for example.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!tl->table->s)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return tl->table->s->tmp_table != NO_TMP_TABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-19 11:33:37 +02:00
|
|
|
/**
|
|
|
|
This internal handler is used to trap ER_NO_SUCH_TABLE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class No_such_table_error_handler : public Internal_error_handler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
No_such_table_error_handler()
|
|
|
|
: m_handled_errors(0), m_unhandled_errors(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool handle_condition(THD *thd,
|
|
|
|
uint sql_errno,
|
|
|
|
const char* sqlstate,
|
2017-01-10 17:28:24 +01:00
|
|
|
Sql_condition::enum_warning_level *level,
|
2010-08-19 11:33:37 +02:00
|
|
|
const char* msg,
|
2013-06-15 17:32:08 +02:00
|
|
|
Sql_condition ** cond_hdl);
|
2010-08-19 11:33:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns TRUE if one or more ER_NO_SUCH_TABLE errors have been
|
|
|
|
trapped and no other errors have been seen. FALSE otherwise.
|
|
|
|
*/
|
|
|
|
bool safely_trapped_errors();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_handled_errors;
|
|
|
|
int m_unhandled_errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-03-31 16:05:33 +02:00
|
|
|
#endif /* SQL_BASE_INCLUDED */
|