From: Date: August 17 2008 7:13pm Subject: bzr commit into mysql-5.1 branch (v.narayanan:2680) Bug#38338 List-Archive: http://lists.mysql.com/commits/51814 X-Bug: 38338 Message-Id: <200808171713.m7HHDksW023521@vn-laptop> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit #At file:///home/vn/work/mysql/workspaces-bazaar/shared_repository_directory/mysql-5.1-bug38338/ 2680 Narayanan V 2008-08-17 Bug#38338 Fix the write_record function to record auto increment values in a consistent way. modified: sql/sql_insert.cc per-file messages: sql/sql_insert.cc The algorithm for the write_record function in sql_insert.cc is (more emphasis given to the parts that deal with the autogenerated values) 1) If a write fails 1.1) save the autogenerated value to avoid thd->insert_id_for_cur_row to become 0. 1.2) 2) record the first successful insert id. explanation of the failure -------------------------- As long as 1.1) was executed 2) worked fine. 1.1) was always executed when REPLACE worked with the last row update optimization, but in cases where 1.1) was not executed 2) would fail and would result in the autogenerated value not being saved. solution -------- repeat a check for thd->insert_id_for_cur_row being zero similar to 1.1) before 2) and ensure that the correct value is saved. === modified file 'sql/sql_insert.cc' --- a/sql/sql_insert.cc 2008-07-11 18:51:10 +0000 +++ b/sql/sql_insert.cc 2008-08-17 17:12:39 +0000 @@ -1545,6 +1545,17 @@ int write_record(THD *thd, TABLE *table, } } } + + /* + If more than one iteration of the above while loop is done, from the second + one the row being inserted will have an explicit value in the autoinc field, + which was set at the first call of handler::update_auto_increment(). This + value is saved to avoid thd->insert_id_for_cur_row becoming 0. Use this saved + autoinc value. + */ + if (table->file->insert_id_for_cur_row <= 0) + table->file->insert_id_for_cur_row= insert_id_for_cur_row; + thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row); /* Restore column maps if they where replaced during an duplicate key