From f491db2119ed75a818a2b767344fb5c57711fa0b Mon Sep 17 00:00:00 2001
From: unknown <msvensson@neptunus.(none)>
Date: Wed, 8 Mar 2006 10:15:48 +0100
Subject: [PATCH] Bug#17137 Running "truncate table" on temporary table leaves
 the table open on a slave  - Decrease "slave_open_temp_tables" during reopen
 of truncated table.  - Add test "rpl_trunc_temp"

sql/sql_delete.cc:
  Decrease "slave_open_temp_tables" after temporary table has been closed, it will be
  increased again when the temp table is reopened after it's been truncated.
mysql-test/r/rpl_trunc_temp.result:
  New BitKeeper file ``mysql-test/r/rpl_trunc_temp.result''
mysql-test/t/rpl_trunc_temp.test:
  New BitKeeper file ``mysql-test/t/rpl_trunc_temp.test''
---
 mysql-test/r/rpl_trunc_temp.result | 22 +++++++++++++++++++
 mysql-test/t/rpl_trunc_temp.test   | 35 ++++++++++++++++++++++++++++++
 sql/sql_delete.cc                  |  2 ++
 3 files changed, 59 insertions(+)
 create mode 100644 mysql-test/r/rpl_trunc_temp.result
 create mode 100644 mysql-test/t/rpl_trunc_temp.test

diff --git a/mysql-test/r/rpl_trunc_temp.result b/mysql-test/r/rpl_trunc_temp.result
new file mode 100644
index 0000000000..44624a3887
--- /dev/null
+++ b/mysql-test/r/rpl_trunc_temp.result
@@ -0,0 +1,22 @@
+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;
+create temporary table t1 (n int);
+insert into t1 values(1);
+show status like 'Slave_open_temp_tables';
+Variable_name	Value
+Slave_open_temp_tables	1
+delete from t1;
+show status like 'Slave_open_temp_tables';
+Variable_name	Value
+Slave_open_temp_tables	1
+truncate t1;
+show status like 'Slave_open_temp_tables';
+Variable_name	Value
+Slave_open_temp_tables	1
+show status like 'Slave_open_temp_tables';
+Variable_name	Value
+Slave_open_temp_tables	0
diff --git a/mysql-test/t/rpl_trunc_temp.test b/mysql-test/t/rpl_trunc_temp.test
new file mode 100644
index 0000000000..be570a6f80
--- /dev/null
+++ b/mysql-test/t/rpl_trunc_temp.test
@@ -0,0 +1,35 @@
+source include/master-slave.inc;
+
+#
+# Bug#17137 Running "truncate table" on temporary table
+# leaves the table open on a slave
+#
+
+create temporary table t1 (n int);
+insert into t1 values(1);
+sync_slave_with_master;
+show status like 'Slave_open_temp_tables';
+
+# Perform a delete from temp table
+connection master;
+delete from t1;
+sync_slave_with_master;
+show status like 'Slave_open_temp_tables';
+
+# Perform truncate on temp table
+connection master;
+truncate t1;
+sync_slave_with_master;
+show status like 'Slave_open_temp_tables';
+
+# Disconnect the master, temp table on slave should dissapear
+disconnect master;
+--real_sleep 3 # time for DROP to be read by slave
+connection slave;
+show status like 'Slave_open_temp_tables';
+
+
+connection master;
+
+
+# End of 4.1 tests
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 203173f52f..b085d37be7 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -641,6 +641,8 @@ int mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok)
     strmov(path,table->path);
     *table_ptr= table->next;			// Unlink table from list
     close_temporary(table,0);
+    if (thd->slave_thread)
+      --slave_open_temp_tables;
     *fn_ext(path)=0;				// Remove the .frm extension
     ha_create_table(path, &create_info,1);
     // We don't need to call invalidate() because this table is not in cache
-- 
2.30.9