mirror of
https://github.com/MariaDB/server.git
synced 2025-01-16 20:12:31 +01:00
9dc3088f9e
causes full table lock on innodb table. Also fixes Bug#28502 Triggers that update another innodb table will block on X lock unnecessarily (duplciate). Code review fixes. Both bugs' synopses are misleading: InnoDB table is not X locked. The statements, however, cannot proceed concurrently, but this happens due to lock conflicts for tables used in triggers, not for the InnoDB table. If a user had an InnoDB table, and two triggers, AFTER UPDATE and AFTER INSERT, competing for different resources (e.g. two distinct MyISAM tables), then these two triggers would not be able to execute concurrently. Moreover, INSERTS/UPDATES of the InnoDB table would not be able to run concurrently. The problem had other side-effects (see respective bug reports). This behavior was a consequence of a shortcoming of the pre-locking algorithm, which would not distinguish between different DML operations (e.g. INSERT and DELETE) and pre-lock all the tables that are used by any trigger defined on the subject table. The idea of the fix is to extend the pre-locking algorithm to keep track, for each table, what DML operation it is used for and not load triggers that are known to never be fired. mysql-test/r/trigger-trans.result: Update results (Bug#26141) mysql-test/r/trigger.result: Update results (Bug#28502) mysql-test/t/trigger-trans.test: Add a test case for Bug#26141 mixing table types in trigger causes full table lock on innodb table. mysql-test/t/trigger.test: Add a test case for Bug#28502 Triggers that update another innodb table will block echo on X lock unnecessarily. Add more test coverage for triggers. sql/item.h: enum trg_event_type is needed in table.h sql/sp.cc: Take into account table_list->trg_event_map when determining what tables to pre-lock. After this change, if we attempt to fire a trigger for which we had not pre-locked any tables, error 'Table was not locked with LOCK TABLES' will be printed. This, however, should never happen, provided the pre-locking algorithm has no programming bugs. Previously a trigger key in the sroutines hash was based on the name of the table the trigger belongs to. This was possible because we would always add to the pre-locking list all the triggers defined for a table when handling this table. Now the key is based on the name of the trigger, owing to the fact that a trigger name must be unique in the database it belongs to. sql/sp_head.cc: Generate sroutines hash key in init_spname(). This is a convenient place since there we have all the necessary information and can avoid an extra alloc. Maintain and merge trg_event_map when adding and merging elements of the pre-locking list. sql/sp_head.h: Add ,m_sroutines_key member, used when inserting the sphead for a trigger into the cache of routines used by a statement. Previously the key was based on the table name the trigger belonged to, since for a given table we would add to the sroutines list all the triggers defined on it. sql/sql_lex.cc: Introduce a new lex step: set_trg_event_type_for_tables(). It is called when we have finished parsing but before opening and locking tables. Now this step is used to evaluate for each TABLE_LIST instance which INSERT/UPDATE/DELETE operation, if any, it is used in. In future this method could be extended to aggregate other information that is hard to aggregate during parsing. sql/sql_lex.h: Add declaration for set_trg_event_type_for_tables(). sql/sql_parse.cc: Call set_trg_event_type_for_tables() after MYSQLparse(). Remove tabs. sql/sql_prepare.cc: Call set_trg_event_type_for_tables() after MYSQLparse(). sql/sql_trigger.cc: Call set_trg_event_type_for_tables() after MYSQLparse(). sql/sql_trigger.h: Remove an obsolete member. sql/sql_view.cc: Call set_trg_event_type_for_tables() after MYSQLparse(). sql/sql_yacc.yy: Move assignment of sp_head::m_type before calling sp_head::init_spname(), one is now used inside another. sql/table.cc: Implement TABLE_LIST::set_trg_event_map() - a method that calculates wh triggers may be fired on this table when executing a statement. sql/table.h: Add missing declarations. Move declaration of trg_event_type from item.h (it will be needed for trg_event_map bitmap when we start using Bitmap template instead of uint8).
142 lines
5.2 KiB
C++
142 lines
5.2 KiB
C++
/* Copyright (C) 2004-2005 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 */
|
|
|
|
|
|
/*
|
|
This class holds all information about triggers of table.
|
|
|
|
QQ: Will it be merged into TABLE in future ?
|
|
*/
|
|
|
|
class Table_triggers_list: public Sql_alloc
|
|
{
|
|
/* Triggers as SPs grouped by event, action_time */
|
|
sp_head *bodies[TRG_EVENT_MAX][TRG_ACTION_MAX];
|
|
/*
|
|
Heads of the lists linking items for all fields used in triggers
|
|
grouped by event and action_time.
|
|
*/
|
|
Item_trigger_field *trigger_fields[TRG_EVENT_MAX][TRG_ACTION_MAX];
|
|
/*
|
|
Copy of TABLE::Field array with field pointers set to TABLE::record[1]
|
|
buffer instead of TABLE::record[0] (used for OLD values in on UPDATE
|
|
trigger and DELETE trigger when it is called for REPLACE).
|
|
*/
|
|
Field **record1_field;
|
|
/*
|
|
During execution of trigger new_field and old_field should point to the
|
|
array of fields representing new or old version of row correspondingly
|
|
(so it can point to TABLE::field or to Tale_triggers_list::record1_field)
|
|
*/
|
|
Field **new_field;
|
|
Field **old_field;
|
|
|
|
/* TABLE instance for which this triggers list object was created */
|
|
TABLE *trigger_table;
|
|
/*
|
|
Names of triggers.
|
|
Should correspond to order of triggers on definitions_list,
|
|
used in CREATE/DROP TRIGGER for looking up trigger by name.
|
|
*/
|
|
List<LEX_STRING> names_list;
|
|
/*
|
|
List of "ON table_name" parts in trigger definitions, used for
|
|
updating trigger definitions during RENAME TABLE.
|
|
*/
|
|
List<LEX_STRING> on_table_names_list;
|
|
|
|
/*
|
|
Grant information for each trigger (pair: subject table, trigger definer).
|
|
*/
|
|
GRANT_INFO subject_table_grants[TRG_EVENT_MAX][TRG_ACTION_MAX];
|
|
|
|
public:
|
|
/*
|
|
Field responsible for storing triggers definitions in file.
|
|
It have to be public because we are using it directly from parser.
|
|
*/
|
|
List<LEX_STRING> definitions_list;
|
|
/*
|
|
List of sql modes for triggers
|
|
*/
|
|
List<ulonglong> definition_modes_list;
|
|
|
|
List<LEX_STRING> definers_list;
|
|
|
|
Table_triggers_list(TABLE *table_arg):
|
|
record1_field(0), trigger_table(table_arg)
|
|
{
|
|
bzero((char *)bodies, sizeof(bodies));
|
|
bzero((char *)trigger_fields, sizeof(trigger_fields));
|
|
bzero((char *)&subject_table_grants, sizeof(subject_table_grants));
|
|
}
|
|
~Table_triggers_list();
|
|
|
|
bool create_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
|
|
bool drop_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
|
|
bool process_triggers(THD *thd, trg_event_type event,
|
|
trg_action_time_type time_type,
|
|
bool old_row_is_record1);
|
|
bool get_trigger_info(THD *thd, trg_event_type event,
|
|
trg_action_time_type time_type,
|
|
LEX_STRING *trigger_name, LEX_STRING *trigger_stmt,
|
|
ulong *sql_mode,
|
|
LEX_STRING *definer);
|
|
|
|
static bool check_n_load(THD *thd, const char *db, const char *table_name,
|
|
TABLE *table, bool names_only);
|
|
static bool drop_all_triggers(THD *thd, char *db, char *table_name);
|
|
static bool change_table_name(THD *thd, const char *db,
|
|
const char *old_table,
|
|
const char *new_db,
|
|
const char *new_table);
|
|
bool has_triggers(trg_event_type event_type,
|
|
trg_action_time_type action_time)
|
|
{
|
|
return (bodies[event_type][action_time] != NULL);
|
|
}
|
|
bool has_delete_triggers()
|
|
{
|
|
return (bodies[TRG_EVENT_DELETE][TRG_ACTION_BEFORE] ||
|
|
bodies[TRG_EVENT_DELETE][TRG_ACTION_AFTER]);
|
|
}
|
|
|
|
void set_table(TABLE *new_table);
|
|
|
|
void mark_fields_used(THD *thd, trg_event_type event);
|
|
|
|
bool is_updated_in_before_update_triggers(Field *fld);
|
|
|
|
friend class Item_trigger_field;
|
|
friend int sp_cache_routines_and_add_tables_for_triggers(THD *thd, LEX *lex,
|
|
TABLE_LIST *table);
|
|
|
|
private:
|
|
bool prepare_record1_accessors(TABLE *table);
|
|
LEX_STRING* change_table_name_in_trignames(const char *db_name,
|
|
LEX_STRING *new_table_name,
|
|
LEX_STRING *stopper);
|
|
bool change_table_name_in_triggers(THD *thd,
|
|
const char *db_name,
|
|
LEX_STRING *old_table_name,
|
|
LEX_STRING *new_table_name);
|
|
};
|
|
|
|
extern const LEX_STRING trg_action_time_type_names[];
|
|
extern const LEX_STRING trg_event_type_names[];
|
|
|
|
int
|
|
add_table_for_trigger(THD *thd, sp_name *trig, bool if_exists,
|
|
TABLE_LIST **table);
|