From e63b5546c597f65696868eaf69159107bc4a8e44 Mon Sep 17 00:00:00 2001 From: Michael Widenius Date: Tue, 4 Jan 2011 00:55:41 +0200 Subject: Implementation of MWL#172: Add support for prepared statements to HANDLER READ It includes speed optimizations for HANDLER READ by caching as much as possible in HANDLER OPEN Other things: - Added mysqld option --disable-thr-alarm to be able to benchmark things without thr_alarm - Changed 'Locked' state to 'System lock' and 'Table lock' (these where used in the code but never shown to end user) - Better error message if mysql_install_db.sh fails - Moved handler function prototypes to sql_handler.h - Remove not anymore used 'thd->locked' member include/thr_alarm.h: Added my_disable_thr_alarm include/thr_lock.h: Add new member to THR_LOCK_DATA to remember original lock type state. This is needed as thr_unlock() resets type to TL_UNLOCK. mysql-test/include/check_no_concurrent_insert.inc: Locked -> Table lock mysql-test/include/handler.inc: Locked -> Table lock mysql-test/r/handler_innodb.result: Updated results for new tests mysql-test/r/handler_myisam.result: Updated results for new tests mysql-test/r/sp-threads.result: Locked -> Table lock mysql-test/suite/binlog/t/binlog_stm_row.test: Locked -> Table lock mysql-test/suite/funcs_1/datadict/processlist_val.inc: Locked -> Table lock mysql-test/suite/pbxt/t/lock_multi.test: Locked -> Table lock mysql-test/suite/sys_vars/r/concurrent_insert_func.result: Locked -> Table lock mysql-test/suite/sys_vars/t/concurrent_insert_func.test: Locked -> Table lock mysql-test/suite/sys_vars/t/delayed_insert_limit_func.test: Locked -> Table lock mysql-test/suite/sys_vars/t/query_cache_wlock_invalidate_func.test: Locked -> Table lock mysql-test/suite/sys_vars/t/sql_low_priority_updates_func.test: Locked -> Table lock mysql-test/t/insert_notembedded.test: Locked -> Table lock mysql-test/t/lock_multi.test: Locked -> Table lock mysql-test/t/merge-big.test: Locked -> Table lock mysql-test/t/multi_update.test: Locked -> Table lock mysql-test/t/query_cache_28249.test: Locked -> Table lock mysql-test/t/sp_notembedded.test: Locked -> Table lock mysql-test/t/sp_sync.test: Locked -> Table lock mysql-test/t/status.test: Locked -> Table lock mysql-test/t/trigger_notembedded.test: Locked -> Table lock mysys/thr_alarm.c: Added option to disable thr_alarm mysys/thr_lock.c: Detect loops scripts/mysql_install_db.sh: Give better error message if something goes wrong sql/Makefile.am: Added sql_handler.h sql/lock.cc: Split functions to allow one to cache value if store_lock() (for HANDLER functions). - Split mysql_lock_tables() into two functions, where first one allocates MYSQL_LOCK and other other one uses it. - Made get_lock_data() an external function. - Added argument to mysql_unlock_tables() to not free sql_lock. - Added argument to reset_lock_data() to reset lock structure to initial state (as after get_lock_data()) sql/mysql_priv.h: Moved handler function prototypes to sql_handler.h Added new lock functions. sql/mysqld.cc: Added --thread-alarm startup option sql/net_serv.cc: Don't call vio_blocking() if not needed sql/sql_base.cc: include sql_handler.h sql/sql_class.cc: include sql_handler.h Remove not anymore used 'thd->locked' member sql/sql_class.h: Remove not anymore used 'thd->locked' member sql/sql_db.cc: include sql_handler.h sql/sql_delete.cc: include sql_handler.h sql/sql_handler.cc: Rewrote all code to use SQL_HANDLER instead of TABLE_LIST (original interface) Rewrote mysql_ha_open() to cache all things from TABLE_LIST and items for field list, where etc. In mysql_ha_open() also cache MYSQL_LOCK structure from get_lock_data(). Split functions into smaller sub functions (needed to be able to implement mysql_ha_read_prepare()) Added mysql_ha_read_prepare() to allow one to prepare HANDLER READ. sql/sql_handler.h: Interface to sql_handler.cc sql/sql_parse.cc: include sql_handler.h sql/sql_prepare.cc: Added mysql_test_handler_read(), prepare for HANDLER READ sql/sql_rename.cc: include sql_handler.h sql/sql_show.cc: Removed usage of thd->locked sql/sql_table.cc: include sql_handler.h sql/sql_trigger.cc: include sql_handler.h --- include/thr_alarm.h | 1 + include/thr_lock.h | 4 +- mysql-test/include/check_no_concurrent_insert.inc | 2 +- mysql-test/include/handler.inc | 76 ++- mysql-test/r/handler_innodb.result | 129 +++++ mysql-test/r/handler_myisam.result | 129 +++++ mysql-test/r/sp-threads.result | 2 +- mysql-test/suite/binlog/t/binlog_stm_row.test | 2 +- .../suite/funcs_1/datadict/processlist_val.inc | 10 +- mysql-test/suite/pbxt/t/lock_multi.test | 16 +- .../suite/sys_vars/r/concurrent_insert_func.result | 4 +- .../suite/sys_vars/t/concurrent_insert_func.test | 4 +- .../sys_vars/t/delayed_insert_limit_func.test | 4 +- .../t/query_cache_wlock_invalidate_func.test | 2 +- .../sys_vars/t/sql_low_priority_updates_func.test | 8 +- mysql-test/t/insert_notembedded.test | 2 +- mysql-test/t/lock_multi.test | 22 +- mysql-test/t/merge-big.test | 2 +- mysql-test/t/multi_update.test | 8 +- mysql-test/t/query_cache_28249.test | 4 +- mysql-test/t/sp_notembedded.test | 2 +- mysql-test/t/sp_sync.test | 2 +- mysql-test/t/status.test | 2 +- mysql-test/t/trigger_notembedded.test | 2 +- mysys/thr_alarm.c | 26 +- mysys/thr_lock.c | 11 +- scripts/mysql_install_db.sh | 12 +- sql/Makefile.am | 2 +- sql/lock.cc | 304 ++++++----- sql/mysql_priv.h | 21 +- sql/mysqld.cc | 11 +- sql/net_serv.cc | 6 +- sql/sql_base.cc | 1 + sql/sql_class.cc | 3 +- sql/sql_class.h | 2 +- sql/sql_db.cc | 1 + sql/sql_delete.cc | 1 + sql/sql_handler.cc | 603 +++++++++++++-------- sql/sql_handler.h | 61 +++ sql/sql_parse.cc | 1 + sql/sql_prepare.cc | 61 +++ sql/sql_rename.cc | 2 +- sql/sql_show.cc | 6 +- sql/sql_table.cc | 1 + sql/sql_trigger.cc | 1 + 45 files changed, 1110 insertions(+), 466 deletions(-) create mode 100644 sql/sql_handler.h diff --git a/include/thr_alarm.h b/include/thr_alarm.h index 99812ed808c..806735b2d38 100644 --- a/include/thr_alarm.h +++ b/include/thr_alarm.h @@ -88,6 +88,7 @@ typedef struct st_alarm { extern uint thr_client_alarm; extern pthread_t alarm_thread; +extern my_bool my_disable_thr_alarm; #define thr_alarm_init(A) (*(A))=0 #define thr_alarm_in_use(A) (*(A)!= 0) diff --git a/include/thr_lock.h b/include/thr_lock.h index 08cc8bd5408..ec24bbabddd 100644 --- a/include/thr_lock.h +++ b/include/thr_lock.h @@ -123,8 +123,10 @@ typedef struct st_thr_lock_data { struct st_thr_lock *lock; pthread_cond_t *cond; void *status_param; /* Param to status functions */ - void *debug_print_param; enum thr_lock_type type; + + enum thr_lock_type org_type; /* Cache for MariaDB */ + void *debug_print_param; /* For error messages */ uint priority; } THR_LOCK_DATA; diff --git a/mysql-test/include/check_no_concurrent_insert.inc b/mysql-test/include/check_no_concurrent_insert.inc index 6938c53fd16..f6a3d2052f5 100644 --- a/mysql-test/include/check_no_concurrent_insert.inc +++ b/mysql-test/include/check_no_concurrent_insert.inc @@ -43,7 +43,7 @@ connection default; # of our statement. let $wait_condition= select count(*) = 1 from information_schema.processlist - where state = "Locked" and info = "insert into $table (i) values (0)"; + where state = "Table lock" and info = "insert into $table (i) values (0)"; --source include/wait_condition.inc --disable_result_log diff --git a/mysql-test/include/handler.inc b/mysql-test/include/handler.inc index 6e7f53ba9b2..e8aeeeefcb2 100644 --- a/mysql-test/include/handler.inc +++ b/mysql-test/include/handler.inc @@ -77,8 +77,9 @@ handler t2 read a prev limit 10; handler t2 read a>=(16) limit 4; handler t2 read a>=(16) limit 2,2; +select * from t1 where a>=16 limit 2,2; handler t2 read a last limit 3; - +handler t2 read a=(16) limit 1,3; handler t2 read a=(19); handler t2 read a=(19) where b="yyy"; @@ -105,6 +106,79 @@ eval alter table t1 engine = $engine_type; --error 1109 handler t2 read first; +handler t1 open; +handler t1 read a=(16) limit 1,3; +flush tables; +handler t1 read a=(16) limit 1,3; +handler t1 close; + +# +# Test with prepared statements +# + +handler t1 open; +prepare stmt from 'handler t1 read a=(?) limit ?,?'; +set @a=16,@b=1,@c=100; +execute stmt using @a,@b,@c; +set @a=16,@b=2,@c=1; +execute stmt using @a,@b,@c; +set @a=16,@b=0,@c=2; +execute stmt using @a,@b,@c; +deallocate prepare stmt; + +prepare stmt from 'handler t1 read a next limit ?'; +handler t1 read a>=(11); +set @a=3; +execute stmt using @a; +execute stmt using @a; +execute stmt using @a; +deallocate prepare stmt; + +prepare stmt from 'handler t1 read b prev limit ?'; +execute stmt using @a; +execute stmt using @a; +execute stmt using @a; +execute stmt using @a; +deallocate prepare stmt; + +prepare stmt from 'handler t1 read b=(?,?)'; +set @a=14, @b='aaa'; +execute stmt using @a,@b; +set @a=14, @b='not found'; +execute stmt using @a,@b; +deallocate prepare stmt; + +prepare stmt from 'handler t1 read b=(1+?) limit 10'; +set @a=15; +execute stmt using @a; +execute stmt using @a; +deallocate prepare stmt; + +prepare stmt from 'handler t1 read a>=(?) where a < ? limit 5'; +set @a=15, @b=20; +execute stmt using @a,@b; +execute stmt using @a,@b; +deallocate prepare stmt; + +prepare stmt from 'handler t1 read a=(?)'; +set @a=16; +execute stmt using @a; +alter table t1 add c int; +--error 1109 +execute stmt using @a; +deallocate prepare stmt; +--error 1109 +handler t1 close; + +handler t1 open; +prepare stmt from 'handler t1 read a=(?)'; +flush tables; +set @a=16; +--error ER_NEED_REPREPARE +execute stmt using @a; +deallocate prepare stmt; +handler t1 close; + # # DROP TABLE / ALTER TABLE # diff --git a/mysql-test/r/handler_innodb.result b/mysql-test/r/handler_innodb.result index 957fc30acef..c13bea72cf1 100644 --- a/mysql-test/r/handler_innodb.result +++ b/mysql-test/r/handler_innodb.result @@ -115,11 +115,18 @@ handler t2 read a>=(16) limit 2,2; a b 17 ddd 18 eee +select * from t1 where a>=16 limit 2,2; +a b +17 ddd +18 eee handler t2 read a last limit 3; a b 22 iii 21 hhh 20 ggg +handler t2 read a=(16) limit 1,3; +a b +16 xxx handler t2 read a=(19); a b 19 fff @@ -161,6 +168,128 @@ a b alter table t1 engine = InnoDB; handler t2 read first; ERROR 42S02: Unknown table 't2' in HANDLER +handler t1 open; +handler t1 read a=(16) limit 1,3; +a b +16 xxx +flush tables; +handler t1 read a=(16) limit 1,3; +a b +16 xxx +handler t1 close; +handler t1 open; +prepare stmt from 'handler t1 read a=(?) limit ?,?'; +set @a=16,@b=1,@c=100; +execute stmt using @a,@b,@c; +a b +16 xxx +set @a=16,@b=2,@c=1; +execute stmt using @a,@b,@c; +a b +set @a=16,@b=0,@c=2; +execute stmt using @a,@b,@c; +a b +16 ccc +16 xxx +deallocate prepare stmt; +prepare stmt from 'handler t1 read a next limit ?'; +handler t1 read a>=(11); +a b +14 aaa +set @a=3; +execute stmt using @a; +a b +15 bbb +16 ccc +16 xxx +execute stmt using @a; +a b +17 ddd +18 eee +19 fff +execute stmt using @a; +a b +19 yyy +20 ggg +21 hhh +deallocate prepare stmt; +prepare stmt from 'handler t1 read b prev limit ?'; +execute stmt using @a; +a b +22 iii +21 hhh +20 ggg +execute stmt using @a; +a b +19 yyy +19 fff +18 eee +execute stmt using @a; +a b +17 ddd +16 xxx +16 ccc +execute stmt using @a; +a b +15 bbb +14 aaa +deallocate prepare stmt; +prepare stmt from 'handler t1 read b=(?,?)'; +set @a=14, @b='aaa'; +execute stmt using @a,@b; +a b +14 aaa +set @a=14, @b='not found'; +execute stmt using @a,@b; +a b +deallocate prepare stmt; +prepare stmt from 'handler t1 read b=(1+?) limit 10'; +set @a=15; +execute stmt using @a; +a b +16 ccc +16 xxx +execute stmt using @a; +a b +16 ccc +16 xxx +deallocate prepare stmt; +prepare stmt from 'handler t1 read a>=(?) where a < ? limit 5'; +set @a=15, @b=20; +execute stmt using @a,@b; +a b +15 bbb +16 ccc +16 xxx +17 ddd +18 eee +execute stmt using @a,@b; +a b +15 bbb +16 ccc +16 xxx +17 ddd +18 eee +deallocate prepare stmt; +prepare stmt from 'handler t1 read a=(?)'; +set @a=16; +execute stmt using @a; +a b +16 ccc +alter table t1 add c int; +execute stmt using @a; +ERROR 42S02: Unknown table 't1' in HANDLER +deallocate prepare stmt; +handler t1 close; +ERROR 42S02: Unknown table 't1' in HANDLER +handler t1 open; +prepare stmt from 'handler t1 read a=(?)'; +flush tables; +set @a=16; +execute stmt using @a; +ERROR HY000: Prepared statement needs to be re-prepared +deallocate prepare stmt; +handler t1 close; handler t1 open as t2; drop table t1; create table t1 (a int); diff --git a/mysql-test/r/handler_myisam.result b/mysql-test/r/handler_myisam.result index b20b8dbb138..420cb956d37 100644 --- a/mysql-test/r/handler_myisam.result +++ b/mysql-test/r/handler_myisam.result @@ -115,11 +115,18 @@ handler t2 read a>=(16) limit 2,2; a b 17 ddd 18 eee +select * from t1 where a>=16 limit 2,2; +a b +17 ddd +18 eee handler t2 read a last limit 3; a b 22 iii 21 hhh 20 ggg +handler t2 read a=(16) limit 1,3; +a b +16 xxx handler t2 read a=(19); a b 19 fff @@ -161,6 +168,128 @@ a b alter table t1 engine = MyISAM; handler t2 read first; ERROR 42S02: Unknown table 't2' in HANDLER +handler t1 open; +handler t1 read a=(16) limit 1,3; +a b +16 xxx +flush tables; +handler t1 read a=(16) limit 1,3; +a b +16 xxx +handler t1 close; +handler t1 open; +prepare stmt from 'handler t1 read a=(?) limit ?,?'; +set @a=16,@b=1,@c=100; +execute stmt using @a,@b,@c; +a b +16 xxx +set @a=16,@b=2,@c=1; +execute stmt using @a,@b,@c; +a b +set @a=16,@b=0,@c=2; +execute stmt using @a,@b,@c; +a b +16 ccc +16 xxx +deallocate prepare stmt; +prepare stmt from 'handler t1 read a next limit ?'; +handler t1 read a>=(11); +a b +14 aaa +set @a=3; +execute stmt using @a; +a b +15 bbb +16 ccc +16 xxx +execute stmt using @a; +a b +17 ddd +18 eee +19 fff +execute stmt using @a; +a b +19 yyy +20 ggg +21 hhh +deallocate prepare stmt; +prepare stmt from 'handler t1 read b prev limit ?'; +execute stmt using @a; +a b +22 iii +21 hhh +20 ggg +execute stmt using @a; +a b +19 yyy +19 fff +18 eee +execute stmt using @a; +a b +17 ddd +16 xxx +16 ccc +execute stmt using @a; +a b +15 bbb +14 aaa +deallocate prepare stmt; +prepare stmt from 'handler t1 read b=(?,?)'; +set @a=14, @b='aaa'; +execute stmt using @a,@b; +a b +14 aaa +set @a=14, @b='not found'; +execute stmt using @a,@b; +a b +deallocate prepare stmt; +prepare stmt from 'handler t1 read b=(1+?) limit 10'; +set @a=15; +execute stmt using @a; +a b +16 ccc +16 xxx +execute stmt using @a; +a b +16 ccc +16 xxx +deallocate prepare stmt; +prepare stmt from 'handler t1 read a>=(?) where a < ? limit 5'; +set @a=15, @b=20; +execute stmt using @a,@b; +a b +15 bbb +16 ccc +16 xxx +17 ddd +18 eee +execute stmt using @a,@b; +a b +15 bbb +16 ccc +16 xxx +17 ddd +18 eee +deallocate prepare stmt; +prepare stmt from 'handler t1 read a=(?)'; +set @a=16; +execute stmt using @a; +a b +16 ccc +alter table t1 add c int; +execute stmt using @a; +ERROR 42S02: Unknown table 't1' in HANDLER +deallocate prepare stmt; +handler t1 close; +ERROR 42S02: Unknown table 't1' in HANDLER +handler t1 open; +prepare stmt from 'handler t1 read a=(?)'; +flush tables; +set @a=16; +execute stmt using @a; +ERROR HY000: Prepared statement needs to be re-prepared +deallocate prepare stmt; +handler t1 close; handler t1 open as t2; drop table t1; create table t1 (a int); diff --git a/mysql-test/r/sp-threads.result b/mysql-test/r/sp-threads.result index 953830ecc87..d974cfb9605 100644 --- a/mysql-test/r/sp-threads.result +++ b/mysql-test/r/sp-threads.result @@ -35,7 +35,7 @@ call bug9486(); show processlist; Id User Host db Command Time State Info # root localhost test Sleep # NULL -# root localhost test Query # Locked update t1, t2 set val= 1 where id1=id2 +# root localhost test Query # Table lock update t1, t2 set val= 1 where id1=id2 # root localhost test Query # NULL show processlist # root localhost test Sleep # NULL unlock tables; diff --git a/mysql-test/suite/binlog/t/binlog_stm_row.test b/mysql-test/suite/binlog/t/binlog_stm_row.test index 29b0a69330d..47d9cbbbfb6 100644 --- a/mysql-test/suite/binlog/t/binlog_stm_row.test +++ b/mysql-test/suite/binlog/t/binlog_stm_row.test @@ -60,7 +60,7 @@ let $wait_condition= --echo # con1 let $wait_condition= SELECT COUNT(*) = 1 FROM information_schema.processlist WHERE - state = "Locked" and info = "INSERT INTO t2 VALUES (3)"; + state = "Table Lock" and info = "INSERT INTO t2 VALUES (3)"; --source include/wait_condition.inc SELECT RELEASE_LOCK('Bug#34306'); --connection con2 diff --git a/mysql-test/suite/funcs_1/datadict/processlist_val.inc b/mysql-test/suite/funcs_1/datadict/processlist_val.inc index b1c1130cbdf..e06c5f081f5 100644 --- a/mysql-test/suite/funcs_1/datadict/processlist_val.inc +++ b/mysql-test/suite/funcs_1/datadict/processlist_val.inc @@ -368,13 +368,13 @@ echo ; connection default; echo -# Poll till INFO is no more NULL and State = 'Locked'. +# Poll till INFO is no more NULL and State = "Table Lock". ; let $wait_condition= SELECT COUNT(*) FROM INFORMATION_SCHEMA.PROCESSLIST - WHERE INFO IS NOT NULL AND STATE = 'Locked'; + WHERE INFO IS NOT NULL AND STATE = "Table Lock"; --source include/wait_condition.inc # -# Expect to see the state 'Locked' for the third connection because the SELECT +# Expect to see the state "Table Lock" for the third connection because the SELECT # collides with the WRITE TABLE LOCK. --replace_column 1 3 6