summaryrefslogtreecommitdiff
path: root/sql/key.cc
diff options
context:
space:
mode:
authorunknown <dlenev@mockturtle.local>2006-09-21 13:39:29 +0400
committerunknown <dlenev@mockturtle.local>2006-09-21 13:39:29 +0400
commite0d2f525fe5ffc29e128713e7c4b5efd2ece9e91 (patch)
tree46d94da2ee46e9db021755af73dc09ad937f73c4 /sql/key.cc
parentf5e8d19f752f6bc71f3e22923b1a99fb6119d6e1 (diff)
downloadmariadb-git-e0d2f525fe5ffc29e128713e7c4b5efd2ece9e91.tar.gz
Fix for bug#20670 "UPDATE using key and invoking trigger that modifies
this key does not stop" (5.1 version). UPDATE statement which WHERE clause used key and which invoked trigger that modified field in this key worked indefinetely. This problem occured because in cases when UPDATE statement was executed in update-on-the-fly mode (in which row is updated right during evaluation of select for WHERE clause) the new version of the row became visible to select representing WHERE clause and was updated again and again. We already solve this problem for UPDATE statements which does not invoke triggers by detecting the fact that we are going to update field in key used for scanning and performing update in two steps, during the first step we gather information about the rows to be updated and then doing actual updates. We also do this for MULTI-UPDATE and in its case we even detect situation when such fields are updated in triggers (actually we simply assume that we always update fields used in key if we have before update trigger). The fix simply extends this check which is done with help of check_if_key_used()/QUICK_SELECT_I::check_if_keys_used() routine/method in such way that it also detects cases when field used in key is updated in trigger. We do this by changing check_if_key_used() to take field bitmap instead field list as argument and passing TABLE::write_set to it (we also have to add info about fields used in triggers to this bitmap a bit earlier). As nice side-effect we have more precise and thus more optimal perfomance-wise check for the MULTI-UPDATE. Also check_if_key_used() routine and similar method were renamed to is_key_used()/is_keys_used() in order to better reflect that it is simple boolean predicate. Finally, partition_key_modified() routine now also takes field bitmap instead of field list as argument. sql/key.cc: Now check_if_key_used() takes field bitmap instead of field list as argument (bitmaps are also used in its implementation). It is no longer responsible for checking if key uses automatically updated TIMESTAMP fields, instead callers should properly mark such fields in field bitmap. Also renamed this function to is_key_used(). sql/mysql_priv.h: Renamed check_if_key_used() to is_key_used(), also now this routine takes field bitmap instead of field list as argument. sql/opt_range.cc: QUICK_SELECT_I::check_if_keys_used() method and check_if_key_used() routine were renamed to is_keys_used()/is_key_used(). Also now they take bitmap instead of field list as argument. sql/opt_range.h: QUICK_SELECT_I::check_if_key_used() method was renamed to is_keys_used(). Now it takes field bitmap instead of field list as argument and no longer responsible for checking if key uses automatically updated TIMESTAMP fields. Instead callers should properly mark such fields in field bitmap. sql/sql_partition.cc: partition_key_modified() now takes field bitmap instead of list as argument. sql/sql_partition.h: partition_key_modified() now takes field bitmap as argument instead of field list. sql/sql_update.cc: To detect situation in which trigger modifies part of key which is going to be used for processing of where clause and thus makes processing of this update with update-on-the-fly method unsafe we use check_if_key_used() routine and similar method (which were renamed to is_key_used()) plus information from TABLE::write_map bitmap. Note that we have to call TABLE::mark_columns_needed_for_update() method earlier now to fill this bitmap with information about fields updated in triggers. safe_update_on_fly() routine now uses the same approach and no longer needs list of fields as argument.
Diffstat (limited to 'sql/key.cc')
-rw-r--r--sql/key.cc44
1 files changed, 21 insertions, 23 deletions
diff --git a/sql/key.cc b/sql/key.cc
index 69557d971e8..be21bf11c3c 100644
--- a/sql/key.cc
+++ b/sql/key.cc
@@ -359,31 +359,29 @@ void key_unpack(String *to,TABLE *table,uint idx)
/*
- Return 1 if any field in a list is part of key or the key uses a field
- that is automaticly updated (like a timestamp)
+ Check if key uses field that is marked in passed field bitmap.
+
+ SYNOPSIS
+ is_key_used()
+ table TABLE object with which keys and fields are associated.
+ idx Key to be checked.
+ fields Bitmap of fields to be checked.
+
+ NOTE
+ This function uses TABLE::tmp_set bitmap so the caller should care
+ about saving/restoring its state if it also uses this bitmap.
+
+ RETURN VALUE
+ TRUE Key uses field from bitmap
+ FALSE Otherwise
*/
-bool check_if_key_used(TABLE *table, uint idx, List<Item> &fields)
+bool is_key_used(TABLE *table, uint idx, const MY_BITMAP *fields)
{
- List_iterator_fast<Item> f(fields);
- KEY_PART_INFO *key_part,*key_part_end;
- for (key_part=table->key_info[idx].key_part,key_part_end=key_part+
- table->key_info[idx].key_parts ;
- key_part < key_part_end;
- key_part++)
- {
- Item_field *field;
-
- if (key_part->field == table->timestamp_field)
- return 1; // Can't be used for update
-
- f.rewind();
- while ((field=(Item_field*) f++))
- {
- if (key_part->field->eq(field->field))
- return 1;
- }
- }
+ bitmap_clear_all(&table->tmp_set);
+ table->mark_columns_used_by_index_no_reset(idx, &table->tmp_set);
+ if (bitmap_is_overlapping(&table->tmp_set, fields))
+ return 1;
/*
If table handler has primary key as part of the index, check that primary
@@ -391,7 +389,7 @@ bool check_if_key_used(TABLE *table, uint idx, List<Item> &fields)
*/
if (idx != table->s->primary_key && table->s->primary_key < MAX_KEY &&
(table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
- return check_if_key_used(table, table->s->primary_key, fields);
+ return is_key_used(table, table->s->primary_key, fields);
return 0;
}