diff --git a/mysql-test/include/show_binary_logs.inc b/mysql-test/include/show_binary_logs.inc
new file mode 100644
index 0000000000000000000000000000000000000000..43a9d5d2eedb36a41b2ccaac2adc36b2db6ffe21
--- /dev/null
+++ b/mysql-test/include/show_binary_logs.inc
@@ -0,0 +1,3 @@
+--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
+--replace_column 2 #
+show binary logs;
diff --git a/mysql-test/include/show_binlog_events.inc b/mysql-test/include/show_binlog_events.inc
index 5d60fb19cb5ac3ee5708b117c5d7976c857301a9..50b910b553bc09d3e3fe46da7949e6fdf877ff64 100644
--- a/mysql-test/include/show_binlog_events.inc
+++ b/mysql-test/include/show_binlog_events.inc
@@ -1,4 +1,10 @@
---let $binlog_start=98
---replace_column 5 #
---replace_regex /\/\* xid=.* \*\//\/* XID *\//
+# $binlog_start can be set by caller or take a default value
+
+if (!$binlog_start)
+{
+  let $binlog_start=98;
+}
+--replace_result $binlog_start <binlog_start>
+--replace_column 2 # 4 # 5 #
+--replace_regex /\/\* xid=.* \*\//\/* XID *\// /table_id: [0-9]+/table_id: #/ /file_id=[0-9]+/file_id=#/
 --eval show binlog events from $binlog_start
diff --git a/mysql-test/r/binlog_index.result b/mysql-test/r/binlog_index.result
new file mode 100644
index 0000000000000000000000000000000000000000..9c22a33435a7b2a577052d95ba7b27d41ee89a1a
--- /dev/null
+++ b/mysql-test/r/binlog_index.result
@@ -0,0 +1,39 @@
+flush logs;
+flush logs;
+flush logs;
+show binary logs;
+Log_name	File_size
+master-bin.000001	#
+master-bin.000002	#
+master-bin.000003	#
+master-bin.000004	#
+purge binary logs TO 'master-bin.000004';
+Warnings:
+Warning	1476	Being purged log MYSQLTEST_VARDIR/log/master-bin.000001 was not found
+*** must show a list starting from the 'TO' argument of PURGE ***
+show binary logs;
+Log_name	File_size
+master-bin.000004	#
+reset master;
+flush logs;
+flush logs;
+flush logs;
+*** must be a warning master-bin.000001 was not found ***
+Warnings:
+Warning	1476	Being purged log MYSQLTEST_VARDIR/log/master-bin.000001 was not found
+*** must show one record, of the active binlog, left in the index file after PURGE ***
+show binary logs;
+Log_name	File_size
+master-bin.000004	#
+reset master;
+flush logs;
+flush logs;
+flush logs;
+purge binary logs TO 'master-bin.000002';
+ERROR HY000: Fatal error during log purge
+show warnings;
+Level	Code	Message
+Error	1377	a problem with deleting MYSQLTEST_VARDIR/log/master-bin.000001; consider examining correspondence of your binlog index file to the actual binlog files
+Error	1377	Fatal error during log purge
+reset master;
+End of tests
diff --git a/mysql-test/r/binlog_killed_simulate.result b/mysql-test/r/binlog_killed_simulate.result
index a21ac5b1e32ae16471501ab9eeed1b9b547bca02..c00873164206a4947ae273487b3fbdcd4340c31d 100644
--- a/mysql-test/r/binlog_killed_simulate.result
+++ b/mysql-test/r/binlog_killed_simulate.result
@@ -16,10 +16,10 @@ create table t2 (a int, b int) ENGINE=MyISAM;
 reset master;
 load data infile '../std_data_ln/rpl_loaddata.dat' into table t2 /* will be "killed" in the middle */;
 ERROR 70100: Query execution was interrupted
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Begin_load_query	1	#	;file_id=1;block_len=12
-master-bin.000001	133	Execute_load_query	1	#	use `test`; load data infile '../std_data_ln/rpl_loaddata.dat' into table t2 /* will be "killed" in the middle */ ;file_id=1
+master-bin.000001	#	Begin_load_query	#	#	;file_id=#;block_len=12
+master-bin.000001	#	Execute_load_query	#	#	use `test`; load data infile '../std_data_ln/rpl_loaddata.dat' into table t2 /* will be "killed" in the middle */ ;file_id=#
 select
 (@a:=load_file("MYSQLTEST_VARDIR/tmp/binlog_killed_bug27571.binlog"))
 is not null;
diff --git a/mysql-test/r/blackhole.result b/mysql-test/r/blackhole.result
index a4b780f9db55eb18ab63b7e90112e5935b7ce37c..51c19c29b0f1d188cf25e53a542e8f9f770f7d8a 100644
--- a/mysql-test/r/blackhole.result
+++ b/mysql-test/r/blackhole.result
@@ -138,3 +138,20 @@ ALTER TABLE t1 DROP INDEX a;
 ALTER TABLE t1 ADD PRIMARY KEY(a);
 DELETE FROM t1 WHERE a=10;
 DROP TABLE t1;
+reset master;
+create table t1 (a int auto_increment, primary key (a)) engine=blackhole;
+insert into t1 values (11), (NULL), (NULL), (NULL);
+set insert_id= 3;
+insert into t1 values (NULL), (33), (NULL);
+set insert_id= 5;
+insert into t1 values (55), (NULL);
+show binlog events from <binlog_start>;
+Log_name	Pos	Event_type	Server_id	End_log_pos	Info
+master-bin.000001	#	Query	1	#	use `test`; create table t1 (a int auto_increment, primary key (a)) engine=blackhole
+master-bin.000001	#	Intvar	1	#	INSERT_ID=1
+master-bin.000001	#	Query	1	#	use `test`; insert into t1 values (11), (NULL), (NULL), (NULL)
+master-bin.000001	#	Intvar	1	#	INSERT_ID=3
+master-bin.000001	#	Query	1	#	use `test`; insert into t1 values (NULL), (33), (NULL)
+master-bin.000001	#	Intvar	1	#	INSERT_ID=5
+master-bin.000001	#	Query	1	#	use `test`; insert into t1 values (55), (NULL)
+drop table t1;
diff --git a/mysql-test/r/mix_innodb_myisam_binlog.result b/mysql-test/r/mix_innodb_myisam_binlog.result
index b1d328a61dcd0e902a51cb65643de5524bd1857d..eda3ff41c89ad433c2581c763df8c487ac54e44f 100644
--- a/mysql-test/r/mix_innodb_myisam_binlog.result
+++ b/mysql-test/r/mix_innodb_myisam_binlog.result
@@ -6,12 +6,12 @@ begin;
 insert into t1 values(1);
 insert into t2 select * from t1;
 commit;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(1)
-master-bin.000001	253	Query	1	#	use `test`; insert into t2 select * from t1
-master-bin.000001	347	Xid	1	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(1)
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 delete from t1;
 delete from t2;
 reset master;
@@ -21,12 +21,12 @@ insert into t2 select * from t1;
 rollback;
 Warnings:
 Warning	1196	Some non-transactional changed tables couldn't be rolled back
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(2)
-master-bin.000001	253	Query	1	#	use `test`; insert into t2 select * from t1
-master-bin.000001	347	Query	1	#	use `test`; ROLLBACK
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(2)
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; ROLLBACK
 delete from t1;
 delete from t2;
 reset master;
@@ -39,15 +39,15 @@ rollback to savepoint my_savepoint;
 Warnings:
 Warning	1196	Some non-transactional changed tables couldn't be rolled back
 commit;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(3)
-master-bin.000001	253	Query	1	#	use `test`; savepoint my_savepoint
-master-bin.000001	338	Query	1	#	use `test`; insert into t1 values(4)
-master-bin.000001	425	Query	1	#	use `test`; insert into t2 select * from t1
-master-bin.000001	519	Query	1	#	use `test`; rollback to savepoint my_savepoint
-master-bin.000001	616	Xid	1	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(3)
+master-bin.000001	#	Query	#	#	use `test`; savepoint my_savepoint
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(4)
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; rollback to savepoint my_savepoint
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 delete from t1;
 delete from t2;
 reset master;
@@ -65,16 +65,16 @@ select a from t1 order by a;
 a
 5
 7
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(5)
-master-bin.000001	253	Query	1	#	use `test`; savepoint my_savepoint
-master-bin.000001	338	Query	1	#	use `test`; insert into t1 values(6)
-master-bin.000001	425	Query	1	#	use `test`; insert into t2 select * from t1
-master-bin.000001	519	Query	1	#	use `test`; rollback to savepoint my_savepoint
-master-bin.000001	616	Query	1	#	use `test`; insert into t1 values(7)
-master-bin.000001	703	Xid	1	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(5)
+master-bin.000001	#	Query	#	#	use `test`; savepoint my_savepoint
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(6)
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; rollback to savepoint my_savepoint
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(7)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 delete from t1;
 delete from t2;
 reset master;
@@ -87,46 +87,46 @@ insert into t2 select * from t1;
 select get_lock("a",10);
 get_lock("a",10)
 1
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(8)
-master-bin.000001	253	Query	1	#	use `test`; insert into t2 select * from t1
-master-bin.000001	347	Query	1	#	use `test`; ROLLBACK
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(8)
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; ROLLBACK
 delete from t1;
 delete from t2;
 reset master;
 insert into t1 values(9);
 insert into t2 select * from t1;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(9)
-master-bin.000001	253	Xid	1	#	COMMIT /* XID */
-master-bin.000001	280	Query	1	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(9)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
 delete from t1;
 delete from t2;
 reset master;
 insert into t1 values(10);
 begin;
 insert into t2 select * from t1;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(10)
-master-bin.000001	254	Xid	1	#	COMMIT /* XID */
-master-bin.000001	281	Query	1	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(10)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
 insert into t1 values(11);
 commit;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(10)
-master-bin.000001	254	Xid	1	#	COMMIT /* XID */
-master-bin.000001	281	Query	1	#	use `test`; insert into t2 select * from t1
-master-bin.000001	375	Query	1	#	use `test`; BEGIN
-master-bin.000001	443	Query	1	#	use `test`; insert into t1 values(11)
-master-bin.000001	531	Xid	1	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(10)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(11)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 alter table t2 engine=INNODB;
 delete from t1;
 delete from t2;
@@ -135,12 +135,12 @@ begin;
 insert into t1 values(12);
 insert into t2 select * from t1;
 commit;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(12)
-master-bin.000001	254	Query	1	#	use `test`; insert into t2 select * from t1
-master-bin.000001	348	Xid	1	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(12)
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 select * from t1
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 delete from t1;
 delete from t2;
 reset master;
@@ -148,7 +148,7 @@ begin;
 insert into t1 values(13);
 insert into t2 select * from t1;
 rollback;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
 delete from t1;
 delete from t2;
@@ -160,11 +160,11 @@ insert into t1 values(15);
 insert into t2 select * from t1;
 rollback to savepoint my_savepoint;
 commit;
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(14)
-master-bin.000001	254	Xid	1	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(14)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 delete from t1;
 delete from t2;
 reset master;
@@ -180,12 +180,12 @@ select a from t1 order by a;
 a
 16
 18
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(16)
-master-bin.000001	254	Query	1	#	use `test`; insert into t1 values(18)
-master-bin.000001	342	Xid	1	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(16)
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(18)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 delete from t1;
 delete from t2;
 alter table t2 type=MyISAM;
@@ -232,35 +232,35 @@ insert into t2 values (3);
 select get_lock("lock1",60);
 get_lock("lock1",60)
 1
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; BEGIN
-master-bin.000001	166	Query	1	#	use `test`; insert into t1 values(16)
-master-bin.000001	254	Query	1	#	use `test`; insert into t1 values(18)
-master-bin.000001	342	Xid	1	#	COMMIT /* XID */
-master-bin.000001	369	Query	1	#	use `test`; BEGIN
-master-bin.000001	437	Query	1	#	use `test`; delete from t1
-master-bin.000001	514	Xid	1	#	COMMIT /* XID */
-master-bin.000001	541	Query	1	#	use `test`; BEGIN
-master-bin.000001	609	Query	1	#	use `test`; delete from t2
-master-bin.000001	686	Xid	1	#	COMMIT /* XID */
-master-bin.000001	713	Query	1	#	use `test`; alter table t2 type=MyISAM
-master-bin.000001	802	Query	1	#	use `test`; BEGIN
-master-bin.000001	870	Query	1	#	use `test`; insert into t1 values (1)
-master-bin.000001	958	Xid	1	#	COMMIT /* XID */
-master-bin.000001	985	Query	1	#	use `test`; insert into t2 values (20)
-master-bin.000001	1074	Query	1	#	use `test`; drop table t1,t2
-master-bin.000001	1153	Query	1	#	use `test`; create temporary table ti (a int) engine=innodb
-master-bin.000001	1263	Query	1	#	use `test`; BEGIN
-master-bin.000001	1331	Query	1	#	use `test`; insert into ti values(1)
-master-bin.000001	1418	Xid	1	#	COMMIT /* XID */
-master-bin.000001	1445	Query	1	#	use `test`; create temporary table t1 (a int) engine=myisam
-master-bin.000001	1555	Query	1	#	use `test`; insert t1 values (1)
-master-bin.000001	1638	Query	1	#	use `test`; create table t0 (n int)
-master-bin.000001	1724	Query	1	#	use `test`; insert t0 select * from t1
-master-bin.000001	1813	Query	1	#	use `test`; insert into t0 select GET_LOCK("lock1",null)
-master-bin.000001	1920	Query	1	#	use `test`; create table t2 (n int) engine=innodb
-master-bin.000001	2020	Query	1	#	use `test`; DROP /*!40005 TEMPORARY */ TABLE IF EXISTS `test`.`t1`,`test`.`ti`
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(16)
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values(18)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; delete from t1
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; delete from t2
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; alter table t2 type=MyISAM
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into t1 values (1)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; insert into t2 values (20)
+master-bin.000001	#	Query	#	#	use `test`; drop table t1,t2
+master-bin.000001	#	Query	#	#	use `test`; create temporary table ti (a int) engine=innodb
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Query	#	#	use `test`; insert into ti values(1)
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+master-bin.000001	#	Query	#	#	use `test`; create temporary table t1 (a int) engine=myisam
+master-bin.000001	#	Query	#	#	use `test`; insert t1 values (1)
+master-bin.000001	#	Query	#	#	use `test`; create table t0 (n int)
+master-bin.000001	#	Query	#	#	use `test`; insert t0 select * from t1
+master-bin.000001	#	Query	#	#	use `test`; insert into t0 select GET_LOCK("lock1",null)
+master-bin.000001	#	Query	#	#	use `test`; create table t2 (n int) engine=innodb
+master-bin.000001	#	Query	#	#	use `test`; DROP /*!40005 TEMPORARY */ TABLE IF EXISTS `test`.`t1`,`test`.`ti`
 do release_lock("lock1");
 drop table t0,t2;
 reset master;
diff --git a/mysql-test/r/multi_update.result b/mysql-test/r/multi_update.result
index 7a12fc87bdc500c0b307ab5eaea998c9f6b7d060..18394bfc88ac651c6d0f4defd5f5101c56194561 100644
--- a/mysql-test/r/multi_update.result
+++ b/mysql-test/r/multi_update.result
@@ -573,8 +573,8 @@ count(*)
 select count(*) from t3 /* must be 1 */;
 count(*)
 1
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; delete t3.* from t2,t3 where t2.a=t3.a
+master-bin.000001	#	Query	#	#	use `test`; delete t3.* from t2,t3 where t2.a=t3.a
 drop table t1, t2, t3;
 end of tests
diff --git a/mysql-test/r/rpl_auto_increment_bug33029.result b/mysql-test/r/rpl_auto_increment_bug33029.result
new file mode 100644
index 0000000000000000000000000000000000000000..fb17553f5a0216f06a03478dabce66c7499c2d7d
--- /dev/null
+++ b/mysql-test/r/rpl_auto_increment_bug33029.result
@@ -0,0 +1,165 @@
+stop slave;
+drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
+reset master;
+reset slave;
+drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
+start slave;
+DROP TABLE IF EXISTS t1, t2;
+DROP PROCEDURE IF EXISTS p1;
+DROP PROCEDURE IF EXISTS p2;
+DROP TRIGGER IF EXISTS tr1;
+DROP FUNCTION IF EXISTS f1;
+CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY);
+CREATE TABLE t2 (id INT AUTO_INCREMENT PRIMARY KEY);
+CREATE PROCEDURE p1()
+BEGIN
+DECLARE ins_count INT DEFAULT 10; 
+WHILE ins_count > 0 DO
+INSERT INTO t1 VALUES (NULL);
+SET ins_count = ins_count - 1;
+END WHILE;
+DELETE FROM t1 WHERE id = 1;
+DELETE FROM t1 WHERE id = 2;
+DELETE FROM t2 WHERE id = 1;
+DELETE FROM t2 WHERE id = 2;
+END//
+CREATE PROCEDURE p2()
+BEGIN
+INSERT INTO t1 VALUES (NULL);
+DELETE FROM t1 WHERE id = f1(3);
+DELETE FROM t1 WHERE id = f1(4);
+DELETE FROM t2 WHERE id = 3;
+DELETE FROM t2 WHERE id = 4;
+END//
+CREATE TRIGGER tr1 BEFORE DELETE
+ON t1 FOR EACH ROW 
+BEGIN
+INSERT INTO t2  VALUES (NULL);
+END//
+CREATE FUNCTION f1 (i int) RETURNS int
+BEGIN
+INSERT INTO t2 VALUES (NULL);
+RETURN i;
+END//
+CALL p1();
+show binlog events from <binlog_start>;
+Log_name	Pos	Event_type	Server_id	End_log_pos	Info
+master-bin.000001	#	Intvar	#	#	INSERT_ID=1
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=2
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=3
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=4
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=5
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=6
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=7
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=8
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=9
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Intvar	#	#	INSERT_ID=10
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t1 WHERE id = 1
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t1 WHERE id = 2
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t2 WHERE id = 1
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t2 WHERE id = 2
+# Result on master
+SELECT * FROM t1;
+id
+3
+4
+5
+6
+7
+8
+9
+10
+SELECT * FROM t2;
+id
+# Result on slave
+SELECT * FROM t1;
+id
+3
+4
+5
+6
+7
+8
+9
+10
+SELECT * FROM t2;
+id
+DROP TRIGGER tr1;
+CALL p2();
+show binlog events from <binlog_start>;
+Log_name	Pos	Event_type	Server_id	End_log_pos	Info
+master-bin.000001	#	Intvar	#	#	INSERT_ID=11
+master-bin.000001	#	Query	#	#	use `test`; INSERT INTO t1 VALUES (NULL)
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t1 WHERE id = f1(3)
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t1 WHERE id = f1(4)
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t2 WHERE id = 3
+master-bin.000001	#	Query	#	#	use `test`; DELETE FROM t2 WHERE id = 4
+# Result on master
+SELECT * FROM t1;
+id
+5
+6
+7
+8
+9
+10
+11
+SELECT * FROM t2;
+id
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+# Result on slave
+SELECT * FROM t1;
+id
+5
+6
+7
+8
+9
+10
+11
+SELECT * FROM t2;
+id
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+DROP TABLE IF EXISTS t1, t2;
+DROP PROCEDURE IF EXISTS p1;
+DROP PROCEDURE IF EXISTS p2;
+DROP FUNCTION IF EXISTS f1;
+DROP TRIGGER IF EXISTS tr1;
diff --git a/mysql-test/r/rpl_packet.result b/mysql-test/r/rpl_packet.result
index 9425724b434e47189baeac43ef488b1f167f9d41..88c63994fff9a10859d00d44542f1da5877b7401 100644
--- a/mysql-test/r/rpl_packet.result
+++ b/mysql-test/r/rpl_packet.result
@@ -6,6 +6,10 @@ drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
 start slave;
 drop database if exists DB_NAME_OF_MAX_LENGTH_AKA_NAME_LEN_64_BYTES_____________________;
 create database DB_NAME_OF_MAX_LENGTH_AKA_NAME_LEN_64_BYTES_____________________;
+SET @@global.max_allowed_packet=1024;
+SET @@global.net_buffer_length=1024;
+STOP SLAVE;
+START SLAVE;
 select @@net_buffer_length, @@max_allowed_packet;
 @@net_buffer_length	@@max_allowed_packet
 1024	1024
diff --git a/mysql-test/r/rpl_user.result b/mysql-test/r/rpl_user.result
index 25e0f310f55d6d14f2443d2d9c800112b697f96f..be24ce0bc4650678d21f5e4adc77a3fdc00b12cc 100644
--- a/mysql-test/r/rpl_user.result
+++ b/mysql-test/r/rpl_user.result
@@ -35,11 +35,11 @@ drop user 'not_exist_user1'@'fakehost', 'not_exist_user2'@'fakehost';
 ERROR HY000: Operation DROP USER failed for 'not_exist_user1'@'fakehost','not_exist_user2'@'fakehost'
 select Host,User from mysql.user where Host='fakehost';
 Host	User
-show binlog events from 98;
+show binlog events from <binlog_start>;
 Log_name	Pos	Event_type	Server_id	End_log_pos	Info
-master-bin.000001	98	Query	1	#	use `test`; create user 'foo'@'fakehost'
-master-bin.000001	189	Query	1	#	use `test`; create user 'foo'@'fakehost', 'bar'@'fakehost'
-master-bin.000001	298	Query	1	#	use `test`; rename user 'foo'@'fakehost' to 'foofoo'@'fakehost'
-master-bin.000001	412	Query	1	#	use `test`; rename user 'not_exist_user1'@'fakehost' to 'foobar'@'fakehost', 'bar'@'fakehost' to 'barbar'@'fakehost'
-master-bin.000001	579	Query	1	#	use `test`; drop user 'foofoo'@'fakehost'
-master-bin.000001	671	Query	1	#	use `test`; drop user 'not_exist_user1'@'fakehost', 'barbar'@'fakehost'
+master-bin.000001	#	Query	#	#	use `test`; create user 'foo'@'fakehost'
+master-bin.000001	#	Query	#	#	use `test`; create user 'foo'@'fakehost', 'bar'@'fakehost'
+master-bin.000001	#	Query	#	#	use `test`; rename user 'foo'@'fakehost' to 'foofoo'@'fakehost'
+master-bin.000001	#	Query	#	#	use `test`; rename user 'not_exist_user1'@'fakehost' to 'foobar'@'fakehost', 'bar'@'fakehost' to 'barbar'@'fakehost'
+master-bin.000001	#	Query	#	#	use `test`; drop user 'foofoo'@'fakehost'
+master-bin.000001	#	Query	#	#	use `test`; drop user 'not_exist_user1'@'fakehost', 'barbar'@'fakehost'
diff --git a/mysql-test/t/binlog_index.test b/mysql-test/t/binlog_index.test
new file mode 100644
index 0000000000000000000000000000000000000000..1e44a9b02e206b220fdd2b2dd6a3d16b0884f8d1
--- /dev/null
+++ b/mysql-test/t/binlog_index.test
@@ -0,0 +1,69 @@
+#
+# testing of purging of binary log files bug#18199/Bug#18453
+#
+source include/have_log_bin.inc;
+source include/not_embedded.inc;
+
+#
+# testing purge binary logs TO
+#
+
+flush logs;
+flush logs;
+flush logs;
+
+source include/show_binary_logs.inc;
+
+remove_file $MYSQLTEST_VARDIR/log/master-bin.000001;
+
+# there must be a warning with file names
+--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
+purge binary logs TO 'master-bin.000004';
+
+--echo *** must show a list starting from the 'TO' argument of PURGE ***
+source include/show_binary_logs.inc;
+
+#
+# testing purge binary logs BEFORE
+#
+
+reset master;
+
+flush logs;
+flush logs;
+flush logs;
+remove_file $MYSQLTEST_VARDIR/log/master-bin.000001;
+
+--echo *** must be a warning master-bin.000001 was not found ***
+let $date=`select NOW()  +  INTERVAL 1 MINUTE`;
+--disable_query_log
+--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
+eval purge binary logs BEFORE '$date';
+--enable_query_log
+
+--echo *** must show one record, of the active binlog, left in the index file after PURGE ***
+source include/show_binary_logs.inc;
+
+#
+# testing a fatal error
+# Turning a binlog file into a directory must be a portable setup
+# 
+
+reset master;
+
+flush logs;
+flush logs;
+flush logs;
+
+remove_file $MYSQLTEST_VARDIR/log/master-bin.000001;
+mkdir $MYSQLTEST_VARDIR/log/master-bin.000001;
+
+--error ER_BINLOG_PURGE_FATAL_ERR
+purge binary logs TO 'master-bin.000002';
+--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
+show warnings;
+rmdir $MYSQLTEST_VARDIR/log/master-bin.000001;
+--disable_warnings
+reset master;
+--enable_warnings
+--echo End of tests
diff --git a/mysql-test/t/blackhole.test b/mysql-test/t/blackhole.test
index ba77b7b7f6a0d1754781cfb39c4e1e851099ff53..cb8010d79675133b74431651ac803b6d0e065c73 100644
--- a/mysql-test/t/blackhole.test
+++ b/mysql-test/t/blackhole.test
@@ -142,7 +142,6 @@ CREATE TABLE t1(a INT) ENGINE=BLACKHOLE;
 INSERT DELAYED INTO t1 VALUES(1);
 DROP TABLE t1;
 
-# End of 4.1 tests
 
 #
 #Bug#19717: DELETE Query Error on BLACKHOLE when using WHERE on column with UNIQUE INDEX
@@ -159,4 +158,27 @@ ALTER TABLE t1 ADD PRIMARY KEY(a);
 DELETE FROM t1 WHERE a=10;
 DROP TABLE t1;
 
-# End of 5.0 tests
+#
+# Bug#35178 INSERT_ID not written to binary log for inserts against BLACKHOLE backed tables
+#
+#
+# the test checks that explicitly prescribed with set insert_id= value
+# preceeds the following autoincrement insert in a blachhole
+#
+
+reset master;
+create table t1 (a int auto_increment, primary key (a)) engine=blackhole;
+
+# not insert_id prescribed insert binlogs with the default set insert_id 1
+insert into t1 values (11), (NULL), (NULL), (NULL);
+set insert_id= 3;
+insert into t1 values (NULL), (33), (NULL);
+set insert_id= 5;
+insert into t1 values (55), (NULL);
+source include/show_binlog_events2.inc;
+
+# cleanup
+drop table t1;
+
+
+# End of tests
diff --git a/mysql-test/t/rpl_auto_increment_bug33029.test b/mysql-test/t/rpl_auto_increment_bug33029.test
new file mode 100644
index 0000000000000000000000000000000000000000..db548945c7ff3485a31f61344cfa74261a35213e
--- /dev/null
+++ b/mysql-test/t/rpl_auto_increment_bug33029.test
@@ -0,0 +1,109 @@
+# BUG#33029 5.0 to 5.1 replication fails on dup key when inserting
+# using a trig in SP
+
+# For all 5.0 up to 5.0.58 exclusive, and 5.1 up to 5.1.12 exclusive,
+# if one statement in a SP generated AUTO_INCREMENT value by the top
+# statement, all statements after it would be considered generated
+# AUTO_INCREMENT value by the top statement, and a erroneous INSERT_ID
+# value might be associated with these statement, which could cause
+# duplicate entry error and stop the slave.
+
+source include/master-slave.inc;
+
+--disable_warnings
+DROP TABLE IF EXISTS t1, t2;
+DROP PROCEDURE IF EXISTS p1;
+DROP PROCEDURE IF EXISTS p2;
+DROP TRIGGER IF EXISTS tr1;
+DROP FUNCTION IF EXISTS f1;
+--enable_warnings
+
+CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY);
+CREATE TABLE t2 (id INT AUTO_INCREMENT PRIMARY KEY);
+
+delimiter //;
+
+CREATE PROCEDURE p1()
+BEGIN
+   DECLARE ins_count INT DEFAULT 10; 
+
+   WHILE ins_count > 0 DO
+       INSERT INTO t1 VALUES (NULL);
+       SET ins_count = ins_count - 1;
+   END WHILE;
+
+   DELETE FROM t1 WHERE id = 1;
+   DELETE FROM t1 WHERE id = 2;
+   DELETE FROM t2 WHERE id = 1;
+   DELETE FROM t2 WHERE id = 2;
+END//
+
+CREATE PROCEDURE p2()
+BEGIN
+   INSERT INTO t1 VALUES (NULL);
+   DELETE FROM t1 WHERE id = f1(3);
+   DELETE FROM t1 WHERE id = f1(4);
+   DELETE FROM t2 WHERE id = 3;
+   DELETE FROM t2 WHERE id = 4;
+END//
+
+CREATE TRIGGER tr1 BEFORE DELETE
+    ON t1 FOR EACH ROW 
+    BEGIN
+        INSERT INTO t2  VALUES (NULL);
+    END//
+
+CREATE FUNCTION f1 (i int) RETURNS int
+    BEGIN
+        INSERT INTO t2 VALUES (NULL);
+        RETURN i;
+    END//
+
+delimiter ;//
+
+# the $binlog_start will be used by the show_binlog_events.inc, so
+# that we can skip binlog events we don't care
+let $binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
+CALL p1();
+source include/show_binlog_events.inc;
+
+echo # Result on master;
+SELECT * FROM t1;
+SELECT * FROM t2;
+
+sync_slave_with_master;
+
+echo # Result on slave;
+SELECT * FROM t1;
+SELECT * FROM t2;
+
+connection master;
+
+DROP TRIGGER tr1;
+
+# the $binlog_start will be used by the show_binlog_events.inc, so
+# that we can skip binlog events we don't care
+let $binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
+CALL p2();
+source include/show_binlog_events.inc;
+
+echo # Result on master;
+SELECT * FROM t1;
+SELECT * FROM t2;
+
+sync_slave_with_master;
+
+echo # Result on slave;
+SELECT * FROM t1;
+SELECT * FROM t2;
+
+# clean up
+connection master;
+disable_warnings;
+DROP TABLE IF EXISTS t1, t2;
+DROP PROCEDURE IF EXISTS p1;
+DROP PROCEDURE IF EXISTS p2;
+DROP FUNCTION IF EXISTS f1;
+DROP TRIGGER IF EXISTS tr1;
+enable_warnings;
+sync_slave_with_master;
diff --git a/mysql-test/t/rpl_packet.test b/mysql-test/t/rpl_packet.test
index 84bc5d908bf30095b9d8fa34d909ed961a8e37c9..a3efdf24bce06ff1884497a43f5af88633e68e51 100644
--- a/mysql-test/t/rpl_packet.test
+++ b/mysql-test/t/rpl_packet.test
@@ -13,13 +13,25 @@ enable_warnings;
 eval create database $db;
 
 connection master;
-select @@net_buffer_length, @@max_allowed_packet;
+SET @@global.max_allowed_packet=1024;
+SET @@global.net_buffer_length=1024;
+
+# Restart slave for setting to take effect
+connection slave;
+STOP SLAVE;
+source include/wait_for_slave_to_stop.inc;
+START SLAVE;
+source include/wait_for_slave_to_start.inc;
+
+# Reconnect to master for new setting to take effect
 disconnect master;
 
 # alas, can't use eval here; if db name changed apply the change here
 connect (master,localhost,root,,DB_NAME_OF_MAX_LENGTH_AKA_NAME_LEN_64_BYTES_____________________);
 
 connection master;
+select @@net_buffer_length, @@max_allowed_packet;
+
 create table `t1` (`f1` LONGTEXT) ENGINE=MyISAM;
 
 INSERT INTO `t1`(`f1`) VALUES ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1023');
@@ -52,7 +64,9 @@ SET @@global.net_buffer_length=4096;
 # Restart slave for new setting to take effect
 connection slave;
 STOP SLAVE;
+source include/wait_for_slave_to_stop.inc;
 START SLAVE;
+source include/wait_for_slave_to_start.inc;
 
 # Reconnect to master for new setting to take effect
 disconnect master;
diff --git a/sql/ha_blackhole.cc b/sql/ha_blackhole.cc
index e477686d18eb122b0a0248577f791d99df795222..01ede3d3bd25cf44d948e571a76ef572184a9d5f 100644
--- a/sql/ha_blackhole.cc
+++ b/sql/ha_blackhole.cc
@@ -115,7 +115,7 @@ const char *ha_blackhole::index_type(uint key_number)
 int ha_blackhole::write_row(byte * buf)
 {
   DBUG_ENTER("ha_blackhole::write_row");
-  DBUG_RETURN(0);
+  DBUG_RETURN(table->next_number_field ? update_auto_increment() : 0);
 }
 
 int ha_blackhole::rnd_init(bool scan)
diff --git a/sql/log.cc b/sql/log.cc
index 15e8679171c1534b1038edaf1ab5c0608a05eb41..9e112e46c6558a4e0aaf1cec7325bb0ad31d1856 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -1180,6 +1180,8 @@ int MYSQL_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
   RETURN VALUES
     0				ok
     LOG_INFO_EOF		to_log not found
+    LOG_INFO_FATAL              if any other than ENOENT error from
+                                my_stat() or my_delete()
 */
 
 int MYSQL_LOG::purge_logs(const char *to_log, 
@@ -1208,33 +1210,75 @@ int MYSQL_LOG::purge_logs(const char *to_log,
   while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
          !log_in_use(log_info.log_file_name))
   {
-    ulong file_size= 0;
-    if (decrease_log_space) //stat the file we want to delete
+    MY_STAT s;
+    if (!my_stat(log_info.log_file_name, &s, MYF(0)))
     {
-      MY_STAT s;
-
-      /* 
-         If we could not stat, we can't know the amount
-         of space that deletion will free. In most cases,
-         deletion won't work either, so it's not a problem.
-      */
-      if (my_stat(log_info.log_file_name,&s,MYF(0)))
-        file_size= s.st_size;
-      else
-	sql_print_information("Failed to execute my_stat on file '%s'",
+      if (my_errno == ENOENT) 
+      {
+        /*
+          It's not fatal if we can't stat a log file that does not exist;
+          If we could not stat, we won't delete.
+        */     
+        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
+                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
+                            log_info.log_file_name);
+        sql_print_information("Failed to execute my_stat on file '%s'",
 			      log_info.log_file_name);
+        my_errno= 0;
+      }
+      else
+      {
+        /*
+          Other than ENOENT are fatal
+        */
+        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
+                            ER_BINLOG_PURGE_FATAL_ERR,
+                            "a problem with getting info on being purged %s; "
+                            "consider examining correspondence "
+                            "of your binlog index file "
+                            "to the actual binlog files",
+                            log_info.log_file_name);
+        error= LOG_INFO_FATAL;
+        goto err;
+      }
+    }
+    else
+    {
+      DBUG_PRINT("info",("purging %s",log_info.log_file_name));
+      if (!my_delete(log_info.log_file_name, MYF(0)))
+      {
+        if (decrease_log_space)
+          *decrease_log_space-= s.st_size;
+      }
+      else
+      {
+        if (my_errno == ENOENT) 
+        {
+          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
+                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
+                              log_info.log_file_name);
+          sql_print_information("Failed to delete file '%s'",
+                                log_info.log_file_name);
+          my_errno= 0;
+        }
+        else
+        {
+          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
+                              ER_BINLOG_PURGE_FATAL_ERR,
+                              "a problem with deleting %s; "
+                              "consider examining correspondence "
+                              "of your binlog index file "
+                              "to the actual binlog files",
+                              log_info.log_file_name);
+          error= LOG_INFO_FATAL;
+          goto err;
+        }
+      }
     }
-    /*
-      It's not fatal if we can't delete a log file ;
-      if we could delete it, take its size into account
-    */
-    DBUG_PRINT("info",("purging %s",log_info.log_file_name));
-    if (!my_delete(log_info.log_file_name, MYF(0)) && decrease_log_space)
-      *decrease_log_space-= file_size;
     if (find_next_log(&log_info, 0) || exit_loop)
       break;
   }
-
+  
   /*
     If we get killed -9 here, the sysadmin would have to edit
     the log index file after restart - otherwise, this should be safe
@@ -1263,6 +1307,8 @@ err:
   RETURN VALUES
     0				ok
     LOG_INFO_PURGE_NO_ROTATE	Binary file that can't be rotated
+    LOG_INFO_FATAL              if any other than ENOENT error from
+                                my_stat() or my_delete()
 */
 
 int MYSQL_LOG::purge_logs_before_date(time_t purge_time)
@@ -1286,11 +1332,66 @@ int MYSQL_LOG::purge_logs_before_date(time_t purge_time)
   while (strcmp(log_file_name, log_info.log_file_name) &&
 	 !log_in_use(log_info.log_file_name))
   {
-    /* It's not fatal even if we can't delete a log file */
-    if (!my_stat(log_info.log_file_name, &stat_area, MYF(0)) ||
-	stat_area.st_mtime >= purge_time)
-      break;
-    my_delete(log_info.log_file_name, MYF(0));
+    if (!my_stat(log_info.log_file_name, &stat_area, MYF(0)))
+    {
+      if (my_errno == ENOENT) 
+      {
+        /*
+          It's not fatal if we can't stat a log file that does not exist.
+        */     
+        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
+                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
+                            log_info.log_file_name);
+	sql_print_information("Failed to execute my_stat on file '%s'",
+			      log_info.log_file_name);
+        my_errno= 0;
+      }
+      else
+      {
+        /*
+          Other than ENOENT are fatal
+        */
+        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
+                            ER_BINLOG_PURGE_FATAL_ERR,
+                            "a problem with getting info on being purged %s; "
+                            "consider examining correspondence "
+                            "of your binlog index file "
+                            "to the actual binlog files",
+                            log_info.log_file_name);
+        error= LOG_INFO_FATAL;
+        goto err;
+      }
+    }
+    else
+    {
+      if (stat_area.st_mtime >= purge_time)
+        break;
+      if (my_delete(log_info.log_file_name, MYF(0)))
+      {
+        if (my_errno == ENOENT) 
+        {
+          /* It's not fatal even if we can't delete a log file */
+          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
+                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
+                              log_info.log_file_name);
+          sql_print_information("Failed to delete file '%s'",
+                                log_info.log_file_name);
+          my_errno= 0;
+        }
+        else
+        {
+          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
+                              ER_BINLOG_PURGE_FATAL_ERR,
+                              "a problem with deleting %s; "
+                              "consider examining correspondence "
+                              "of your binlog index file "
+                              "to the actual binlog files",
+                              log_info.log_file_name);
+          error= LOG_INFO_FATAL;
+          goto err;
+        }
+      }
+    }
     if (find_next_log(&log_info, 0))
       break;
   }
diff --git a/sql/share/errmsg.txt b/sql/share/errmsg.txt
index a3514776d6efedfe822c2e183c6f75105f6fe663..a450f7a204569321ccb5dc826874ba73c8c05594 100644
--- a/sql/share/errmsg.txt
+++ b/sql/share/errmsg.txt
@@ -5641,7 +5641,7 @@ ER_NAME_BECOMES_EMPTY
         eng "Name '%-.64s' has become ''"
 ER_AMBIGUOUS_FIELD_TERM
 	eng "First character of the FIELDS TERMINATED string is ambiguous; please use non-optional and non-empty FIELDS ENCLOSED BY"
-
 ER_LOAD_DATA_INVALID_COLUMN
   eng "Invalid column reference (%-.64s) in LOAD DATA"
-
+ER_LOG_PURGE_NO_FILE  
+	eng "Being purged log %s was not found"
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index e36ed0c425fd654e335b6c6f8c867b2850274748..f541c8b3677b06ad9e14178b1ec43a5bf2cfa08c 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -622,6 +622,13 @@ void THD::cleanup_after_query()
   {
     clear_next_insert_id= 0;
     next_insert_id= 0;
+
+    /*
+      BUG#33029, if one statement in a SP set this member to 1, all
+      statment after this statement in the SP would be considered used
+      INSERT_ID value, reset this member after each query to fix this.
+    */
+    insert_id_used= 0;
   }
   /*
     Reset rand_used so that detection of calls to rand() will save random