List:Commits« Previous MessageNext Message »
From:Davi Arnaut Date:September 29 2010 10:39pm
Subject:bzr commit into mysql-5.5-runtime branch (davi:3149) Bug#49938 Bug#54678
View as plain text  
# At a local mysql-5.5-runtime repository of davi

 3149 Davi Arnaut	2010-09-29
      Bug#49938: Failing assertion: inode or deadlock in fsp/fsp0fsp.c
      Bug#54678: InnoDB, TRUNCATE, ALTER, I_S SELECT, crash or deadlock
      
      The problem was that for storage engines that do not support
      truncate table via a external drop and recreate, such as InnoDB
      which implements truncate via a internal drop and recreate, the
      delete_all_rows method could be invoked with a shared metadata
      lock, causing problems if the engine needed exclusive access
      to some internal metadata. This problem originated with the
      fact that there is no truncate specific handler method, which
      ended up leading to a abuse of the delete_all_rows method that
      is primarily used for delete operations without a condition.
      
      The solution is to introduce a truncate handler method that is
      invoked when the engine does not support truncation via a table
      drop and recreate. This method is invoked under a exclusive
      metadata lock, so that there is only a single instance of the
      table when the method is invoked.
      
      Also, the method is not invoked and a error is thrown if
      the table participates in a non-self-referencing foreign key
      relationship. This was necessary to avoid inconsistency as
      integrity checks are bypassed. This is inline with the fact
      that truncate is primarily a DDL operation that was designed
      to quickly remove all data from a table.
      
      Incompatible change: truncate no longer resorts to a high-level
      row by row delete if the storage engine does not support the
      truncate method. Consequently, the count of rows affected does
      not reflect the actual number of rows.
     @ mysql-test/suite/innodb/t/innodb-truncate.test
        Add test cases for truncate and foreign key checks.
        Also test that InnoDB resets auto-increment on truncate.
     @ mysql-test/suite/innodb/t/innodb.test
        FK is not necessary, test is related to auto-increment.
        
        Update error number, truncate is no longer invoked if
        table is parent in a FK relationship.
     @ mysql-test/suite/innodb/t/innodb_mysql.test
        Update error number, truncate is no longer invoked if
        table is parent in a FK relationship.
        
        Use delete instead of truncate, test is used to check
        the interaction of FKs, triggers and delete.
     @ mysql-test/suite/parts/inc/partition_check.inc
        Fix typo.
     @ mysql-test/suite/sys_vars/t/foreign_key_checks_func.test
        Update error number, truncate is no longer invoked if
        table is parent in a FK relationship.
     @ mysql-test/t/mdl_sync.test
        Modify test case to reflect and ensure that truncate takes
        a exclusive metadata lock.
     @ mysql-test/t/trigger-trans.test
        Update error number, truncate is no longer invoked if
        table is parent in a FK relationship.
     @ sql/ha_partition.cc
        Reorganize the various truncate methods. delete_all_rows is now
        passed directly to the underlying engines, so as truncate. The
        code responsible for truncating individual partitions is moved
        to ha_partition::truncate_partition, which is invoked when a
        ALTER TABLE t1 TRUNCATE PARTITION p statement is executed.
        
        Since the partition truncate no longer can be invoked via
        delete, the bitmap operations are not necessary anymore. The
        explicit reset of the auto-increment value is also removed
        as the underlying engines are now responsible for reseting
        the value.
     @ sql/handler.cc
        Wire up the handler truncate method.
     @ sql/handler.h
        Introduce and document the truncate handler method. It assumes
        certain use cases of delete_all_rows.
     @ sql/share/errmsg-utf8.txt
        Add error message for truncate and FK.
     @ sql/sql_lex.h
        Introduce a flag so that the partition engine can detect when
        a partition is being truncated. Used to give a special error.
     @ sql/sql_parse.cc
        Function mysql_truncate_table no longer exists.
     @ sql/sql_partition_admin.cc
        Implement the TRUNCATE PARTITION statement.
     @ sql/sql_truncate.cc
        Change the truncate table implementation to use the new truncate
        handler method and to not rely on row-by-row delete anymore.
        
        The truncate handler method is always invoked with a exclusive
        metadata lock. Also, it is no longer possible to truncate a
        table that is parent in some non-self-referencing foreign key.
     @ storage/archive/ha_archive.cc
        Rename method as the description indicates that in the future
        this could be a truncate operation.
     @ storage/blackhole/ha_blackhole.cc
        Implement truncate as no operation for the blackhole engine in
        order to remain compatible with older releases.
     @ storage/csv/ha_tina.cc
        Introduce truncate method that invokes a delete_all_rows.
        This is required to support partition truncate as this
        form of truncate does not implement the drop and recreate
        protocol.
     @ storage/federated/ha_federated.cc
        Introduce truncate method that invokes a delete_all_rows.
        This is required to support partition truncate as this
        form of truncate does not implement the drop and recreate
        protocol.
     @ storage/heap/ha_heap.cc
        Introduce truncate method that invokes a delete_all_rows.
        This is required to support partition truncate as this
        form of truncate does not implement the drop and recreate
        protocol.
     @ storage/ibmdb2i/ha_ibmdb2i.cc
        Introduce truncate method that invokes a delete_all_rows.
        This is required to support partition truncate as this
        form of truncate does not implement the drop and recreate
        protocol.
     @ storage/myisammrg/ha_myisammrg.cc
        Introduce truncate method that invokes a delete_all_rows.
        This is required to support partition truncate as this
        form of truncate does not implement the drop and recreate
        protocol.

    added:
      mysql-test/suite/innodb/r/innodb-truncate.result
      mysql-test/suite/innodb/t/innodb-truncate.test
    modified:
      mysql-test/r/mdl_sync.result
      mysql-test/r/trigger-trans.result
      mysql-test/suite/innodb/r/innodb.result
      mysql-test/suite/innodb/r/innodb_mysql.result
      mysql-test/suite/innodb/t/innodb.test
      mysql-test/suite/innodb/t/innodb_mysql.test
      mysql-test/suite/parts/inc/partition_check.inc
      mysql-test/suite/sys_vars/r/foreign_key_checks_func.result
      mysql-test/suite/sys_vars/t/foreign_key_checks_func.test
      mysql-test/t/mdl_sync.test
      mysql-test/t/trigger-trans.test
      sql/ha_partition.cc
      sql/ha_partition.h
      sql/handler.cc
      sql/handler.h
      sql/share/errmsg-utf8.txt
      sql/sql_lex.h
      sql/sql_parse.cc
      sql/sql_partition_admin.cc
      sql/sql_partition_admin.h
      sql/sql_truncate.cc
      sql/sql_truncate.h
      storage/archive/ha_archive.cc
      storage/archive/ha_archive.h
      storage/blackhole/ha_blackhole.cc
      storage/blackhole/ha_blackhole.h
      storage/csv/ha_tina.cc
      storage/csv/ha_tina.h
      storage/example/ha_example.cc
      storage/example/ha_example.h
      storage/federated/ha_federated.cc
      storage/federated/ha_federated.h
      storage/heap/ha_heap.cc
      storage/heap/ha_heap.h
      storage/ibmdb2i/ha_ibmdb2i.cc
      storage/innobase/handler/ha_innodb.cc
      storage/innobase/handler/ha_innodb.h
      storage/innobase/row/row0mysql.c
      storage/myisam/ha_myisam.cc
      storage/myisam/ha_myisam.h
      storage/myisammrg/ha_myisammrg.cc
      storage/myisammrg/ha_myisammrg.h
      storage/perfschema/ha_perfschema.cc
      storage/perfschema/ha_perfschema.h
=== modified file 'mysql-test/r/mdl_sync.result'
--- a/mysql-test/r/mdl_sync.result	2010-09-08 08:25:37 +0000
+++ b/mysql-test/r/mdl_sync.result	2010-09-29 22:38:57 +0000
@@ -2647,10 +2647,10 @@ FLUSH TABLES t1;
 # Connection: default
 SET debug_sync='now WAIT_FOR parked_flush';
 SET debug_sync='now SIGNAL go_truncate';
+# Ensure that truncate waits for a exclusive lock
+SET debug_sync= 'now SIGNAL go_show';
 # Connection: con1
 # Reaping...
-# Connection: default
-SET debug_sync= 'now SIGNAL go_show';
 # Connection: con2 (SHOW FIELDS FROM t1)
 # Reaping...
 Field	Type	Null	Key	Default	Extra

=== modified file 'mysql-test/r/trigger-trans.result'
--- a/mysql-test/r/trigger-trans.result	2008-10-07 16:54:12 +0000
+++ b/mysql-test/r/trigger-trans.result	2010-09-29 22:38:57 +0000
@@ -151,9 +151,14 @@ CREATE TRIGGER t1_ad AFTER DELETE ON t1
 SET @a = 0;
 SET @b = 0;
 TRUNCATE t1;
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 SELECT @a, @b;
 @a	@b
 0	0
+DELETE FROM t1;
+SELECT @a, @b;
+@a	@b
+1	1
 INSERT INTO t1 VALUES (1);
 DELETE FROM t1;
 SELECT @a, @b;

=== added file 'mysql-test/suite/innodb/r/innodb-truncate.result'
--- a/mysql-test/suite/innodb/r/innodb-truncate.result	1970-01-01 00:00:00 +0000
+++ b/mysql-test/suite/innodb/r/innodb-truncate.result	2010-09-29 22:38:57 +0000
@@ -0,0 +1,68 @@
+#
+# TRUNCATE TABLE
+#
+# Truncating is disallowed for parent tables unless such table
+# participates in self-referencing foreign keys only.
+#
+CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=INNODB;
+CREATE TABLE t2 (fk INT NOT NULL, FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+TRUNCATE TABLE t1;
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
+# Truncation of child should succeed.
+TRUNCATE TABLE t2;
+DROP TABLE t2;
+DROP TABLE t1;
+CREATE TABLE t1 (pk INT PRIMARY KEY, fk INT,
+FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+# Truncation of self-referencing table should succeed.
+TRUNCATE TABLE t1;
+DROP TABLE t1;
+#
+# Also, truncating such tables is allowed if foreign key
+# checks are  disabled.
+#
+SET @old_foreign_key_checks = @@SESSION.foreign_key_checks;
+CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=INNODB;
+CREATE TABLE t2 (fk INT NOT NULL, FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+CREATE TABLE t3 (pk INT PRIMARY KEY, fk INT,
+FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+SET @@SESSION.foreign_key_checks = 0;
+TRUNCATE TABLE t1;
+TRUNCATE TABLE t2;
+TRUNCATE TABLE t3;
+SET @@SESSION.foreign_key_checks = 1;
+TRUNCATE TABLE t1;
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
+TRUNCATE TABLE t2;
+TRUNCATE TABLE t3;
+LOCK TABLES t1 WRITE;
+SET @@SESSION.foreign_key_checks = 0;
+TRUNCATE TABLE t1;
+SET @@SESSION.foreign_key_checks = 1;
+TRUNCATE TABLE t1;
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
+UNLOCK TABLES;
+DROP TABLE t3,t2,t1;
+SET @@SESSION.foreign_key_checks = @old_foreign_key_checks;
+#
+# Test that TRUNCATE resets auto-increment.
+#
+CREATE TABLE t1 (a INT PRIMARY KEY NOT NULL AUTO_INCREMENT);
+INSERT INTO t1 VALUES (NULL), (NULL);
+SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 't1';
+AUTO_INCREMENT
+3
+SELECT * FROM t1 ORDER BY a;
+a
+1
+2
+TRUNCATE TABLE t1;
+SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 't1';
+AUTO_INCREMENT
+1
+INSERT INTO t1 VALUES (NULL), (NULL);
+SELECT * FROM t1 ORDER BY a;
+a
+1
+2
+DROP TABLE t1;

=== modified file 'mysql-test/suite/innodb/r/innodb.result'
--- a/mysql-test/suite/innodb/r/innodb.result	2010-06-22 15:58:28 +0000
+++ b/mysql-test/suite/innodb/r/innodb.result	2010-09-29 22:38:57 +0000
@@ -2424,10 +2424,6 @@ drop table t1,t2;
 CREATE TABLE t1 (
 id INTEGER NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)
 ) ENGINE=InnoDB;
-CREATE TABLE t2 (
-id INTEGER NOT NULL,
-FOREIGN KEY (id) REFERENCES t1 (id)
-) ENGINE=InnoDB;
 INSERT INTO t1 (id) VALUES (NULL);
 SELECT * FROM t1;
 id
@@ -2443,7 +2439,7 @@ INSERT INTO t1 (id) VALUES (NULL);
 SELECT * FROM t1;
 id
 1
-DROP TABLE t2, t1;
+DROP TABLE t1;
 CREATE TABLE t1
 (
 id INT PRIMARY KEY
@@ -2621,13 +2617,15 @@ ERROR 23000: Cannot delete or update a p
 update t4 set a=2;
 ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t4`, CONSTRAINT `t4_ibfk_1` FOREIGN KEY (`a`) REFERENCES `t3` (`a`))
 truncate t1;
-ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`a`) REFERENCES `t1` (`a`))
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 truncate t3;
-ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t4`, CONSTRAINT `t4_ibfk_1` FOREIGN KEY (`a`) REFERENCES `t3` (`a`))
+ERROR 42000: Cannot truncate table 't3' that is referred to by foreign key in another table
 truncate t2;
 truncate t4;
 truncate t1;
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 truncate t3;
+ERROR 42000: Cannot truncate table 't3' that is referred to by foreign key in another table
 drop table t4,t3,t2,t1;
 create table t1 (a varchar(255) character set utf8,
 b varchar(255) character set utf8,

=== modified file 'mysql-test/suite/innodb/r/innodb_mysql.result'
--- a/mysql-test/suite/innodb/r/innodb_mysql.result	2010-08-30 08:36:02 +0000
+++ b/mysql-test/suite/innodb/r/innodb_mysql.result	2010-09-29 22:38:57 +0000
@@ -1941,7 +1941,7 @@ INSERT INTO t2 VALUES (3,2);
 SET AUTOCOMMIT = 0;
 START TRANSACTION;
 TRUNCATE TABLE t1;
-ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`t1_id`) REFERENCES `t1` (`id`))
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 SELECT * FROM t1;
 id
 1
@@ -1953,7 +1953,7 @@ id
 2
 START TRANSACTION;
 TRUNCATE TABLE t1;
-ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`t1_id`) REFERENCES `t1` (`id`))
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 SELECT * FROM t1;
 id
 1
@@ -1971,7 +1971,7 @@ id
 2
 COMMIT;
 TRUNCATE TABLE t1;
-ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`t1_id`) REFERENCES `t1` (`id`))
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 SELECT * FROM t1;
 id
 1
@@ -1983,9 +1983,12 @@ id
 1
 2
 TRUNCATE TABLE t1;
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 ROLLBACK;
 SELECT * FROM t1;
 id
+1
+2
 TRUNCATE TABLE t2;
 DROP TABLE t2;
 DROP TABLE t1;
@@ -2077,9 +2080,9 @@ i	i
 ** error handling inside a row iteration.
 **
 DROP TRIGGER trg;
-TRUNCATE TABLE t1;
-TRUNCATE TABLE t2;
-TRUNCATE TABLE t3;
+DELETE FROM t1;
+DELETE FROM t2;
+DELETE FROM t3;
 INSERT INTO t1 VALUES (1),(2),(3),(4);
 INSERT INTO t3 VALUES (1),(2),(3),(4);
 INSERT INTO t4 VALUES (3,3),(4,4);
@@ -2105,9 +2108,9 @@ DROP TRIGGER trg;
 **
 ** Induce an error midway through an AFTER-trigger
 **
-TRUNCATE TABLE t4;
-TRUNCATE TABLE t1;
-TRUNCATE TABLE t3;
+DELETE FROM t4;
+DELETE FROM t1;
+DELETE FROM t3;
 INSERT INTO t1 VALUES (1),(2),(3),(4);
 INSERT INTO t3 VALUES (1),(2),(3),(4);
 CREATE TRIGGER trg AFTER DELETE ON t1 FOR EACH ROW

=== added file 'mysql-test/suite/innodb/t/innodb-truncate.test'
--- a/mysql-test/suite/innodb/t/innodb-truncate.test	1970-01-01 00:00:00 +0000
+++ b/mysql-test/suite/innodb/t/innodb-truncate.test	2010-09-29 22:38:57 +0000
@@ -0,0 +1,65 @@
+--source include/have_innodb.inc
+
+--echo #
+--echo # TRUNCATE TABLE
+--echo #
+--echo # Truncating is disallowed for parent tables unless such table
+--echo # participates in self-referencing foreign keys only.
+--echo #
+CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=INNODB;
+CREATE TABLE t2 (fk INT NOT NULL, FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+--error ER_TRUNCATE_ILLEGAL_FK
+TRUNCATE TABLE t1;
+--echo # Truncation of child should succeed.
+TRUNCATE TABLE t2;
+DROP TABLE t2;
+DROP TABLE t1;
+CREATE TABLE t1 (pk INT PRIMARY KEY, fk INT,
+                 FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+--echo # Truncation of self-referencing table should succeed.
+TRUNCATE TABLE t1;
+DROP TABLE t1;
+
+--echo #
+--echo # Also, truncating such tables is allowed if foreign key
+--echo # checks are  disabled.
+--echo #
+
+SET @old_foreign_key_checks = @@SESSION.foreign_key_checks;
+CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=INNODB;
+CREATE TABLE t2 (fk INT NOT NULL, FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+CREATE TABLE t3 (pk INT PRIMARY KEY, fk INT,
+                 FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
+SET @@SESSION.foreign_key_checks = 0;
+TRUNCATE TABLE t1;
+TRUNCATE TABLE t2;
+TRUNCATE TABLE t3;
+SET @@SESSION.foreign_key_checks = 1;
+--error ER_TRUNCATE_ILLEGAL_FK
+TRUNCATE TABLE t1;
+TRUNCATE TABLE t2;
+TRUNCATE TABLE t3;
+LOCK TABLES t1 WRITE;
+SET @@SESSION.foreign_key_checks = 0;
+TRUNCATE TABLE t1;
+SET @@SESSION.foreign_key_checks = 1;
+--error ER_TRUNCATE_ILLEGAL_FK
+TRUNCATE TABLE t1;
+UNLOCK TABLES;
+DROP TABLE t3,t2,t1;
+SET @@SESSION.foreign_key_checks = @old_foreign_key_checks;
+
+--echo #
+--echo # Test that TRUNCATE resets auto-increment.
+--echo #
+
+CREATE TABLE t1 (a INT PRIMARY KEY NOT NULL AUTO_INCREMENT);
+INSERT INTO t1 VALUES (NULL), (NULL);
+SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 't1';
+SELECT * FROM t1 ORDER BY a;
+TRUNCATE TABLE t1;
+SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 't1';
+INSERT INTO t1 VALUES (NULL), (NULL);
+SELECT * FROM t1 ORDER BY a;
+DROP TABLE t1;
+

=== modified file 'mysql-test/suite/innodb/t/innodb.test'
--- a/mysql-test/suite/innodb/t/innodb.test	2010-06-22 15:58:28 +0000
+++ b/mysql-test/suite/innodb/t/innodb.test	2010-09-29 22:38:57 +0000
@@ -1471,11 +1471,6 @@ CREATE TABLE t1 (
 id INTEGER NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)
 ) ENGINE=InnoDB;
 
-CREATE TABLE t2 (
-id INTEGER NOT NULL,
-FOREIGN KEY (id) REFERENCES t1 (id)
-) ENGINE=InnoDB;
-
 INSERT INTO t1 (id) VALUES (NULL);
 SELECT * FROM t1;
 TRUNCATE t1;
@@ -1488,7 +1483,7 @@ DELETE FROM t1;
 TRUNCATE t1;
 INSERT INTO t1 (id) VALUES (NULL);
 SELECT * FROM t1;
-DROP TABLE t2, t1;
+DROP TABLE t1;
 
 # Test that foreign keys in temporary tables are not accepted (bug #12084)
 CREATE TABLE t1
@@ -1723,13 +1718,15 @@ update t2 set a=2;
 update t3 set a=2;
 -- error 1452
 update t4 set a=2;
--- error 1451
+-- error ER_TRUNCATE_ILLEGAL_FK
 truncate t1;
--- error 1451
+-- error ER_TRUNCATE_ILLEGAL_FK
 truncate t3;
 truncate t2;
 truncate t4;
+-- error ER_TRUNCATE_ILLEGAL_FK
 truncate t1;
+-- error ER_TRUNCATE_ILLEGAL_FK
 truncate t3;
 
 drop table t4,t3,t2,t1;

=== modified file 'mysql-test/suite/innodb/t/innodb_mysql.test'
--- a/mysql-test/suite/innodb/t/innodb_mysql.test	2010-08-27 11:33:32 +0000
+++ b/mysql-test/suite/innodb/t/innodb_mysql.test	2010-09-29 22:38:57 +0000
@@ -151,14 +151,14 @@ INSERT INTO t2 VALUES (3,2);
 SET AUTOCOMMIT = 0;
 
 START TRANSACTION;
---error ER_ROW_IS_REFERENCED_2
+--error ER_TRUNCATE_ILLEGAL_FK
 TRUNCATE TABLE t1;
 SELECT * FROM t1;
 COMMIT;
 SELECT * FROM t1;
 
 START TRANSACTION;
---error ER_ROW_IS_REFERENCED_2
+--error ER_TRUNCATE_ILLEGAL_FK
 TRUNCATE TABLE t1;
 SELECT * FROM t1;
 ROLLBACK;
@@ -170,13 +170,14 @@ START TRANSACTION;
 SELECT * FROM t1;
 COMMIT;
 
---error ER_ROW_IS_REFERENCED_2
+--error ER_TRUNCATE_ILLEGAL_FK
 TRUNCATE TABLE t1;
 SELECT * FROM t1;
 DELETE FROM t2 WHERE id = 3;
 
 START TRANSACTION;
 SELECT * FROM t1;
+--error ER_TRUNCATE_ILLEGAL_FK
 TRUNCATE TABLE t1;
 ROLLBACK;
 SELECT * FROM t1;
@@ -275,9 +276,9 @@ SELECT * FROM t1 LEFT JOIN t3 ON t1.i=t3
 --echo ** error handling inside a row iteration.
 --echo **
 DROP TRIGGER trg;
-TRUNCATE TABLE t1;
-TRUNCATE TABLE t2;
-TRUNCATE TABLE t3;
+DELETE FROM t1;
+DELETE FROM t2;
+DELETE FROM t3;
 
 INSERT INTO t1 VALUES (1),(2),(3),(4);
 INSERT INTO t3 VALUES (1),(2),(3),(4);
@@ -304,9 +305,9 @@ DROP TRIGGER trg;
 --echo **
 --echo ** Induce an error midway through an AFTER-trigger
 --echo **
-TRUNCATE TABLE t4;
-TRUNCATE TABLE t1;
-TRUNCATE TABLE t3;
+DELETE FROM t4;
+DELETE FROM t1;
+DELETE FROM t3;
 INSERT INTO t1 VALUES (1),(2),(3),(4);
 INSERT INTO t3 VALUES (1),(2),(3),(4);
 delimiter ||;

=== modified file 'mysql-test/suite/parts/inc/partition_check.inc'
--- a/mysql-test/suite/parts/inc/partition_check.inc	2007-11-20 15:04:07 +0000
+++ b/mysql-test/suite/parts/inc/partition_check.inc	2010-09-29 22:38:57 +0000
@@ -177,7 +177,7 @@ let $any_unique= `SELECT @my_errno IN ($
 #                   @my_errno AS sql_errno;
 if (`SELECT @my_errno NOT IN (0,$ER_DUP_KEY,$ER_DUP_ENTRY)`)
 {
-   --echo #      The last command got an unexepected error response.
+   --echo #      The last command got an unexpected error response.
    --echo #      Expected/handled SQL codes are 0,$ER_DUP_KEY,$ER_DUP_ENTRY
    SELECT '#      SQL code we got was: ' AS "", @my_errno AS "";
    --echo #      Sorry, have to abort.
@@ -219,7 +219,7 @@ if ($any_unique)
    #                   @my_errno AS sql_errno;
    if (`SELECT @my_errno NOT IN (0,$ER_DUP_KEY,$ER_DUP_ENTRY)`)
    {
-      --echo #      The last command got an unexepected error response.
+      --echo #      The last command got an unexpected error response.
       --echo #      Expected/handled SQL codes are 0,$ER_DUP_KEY,$ER_DUP_ENTRY
       SELECT '#      SQL code we got was: ' AS "", @my_errno AS "";
       --echo #      Sorry, have to abort.
@@ -255,7 +255,7 @@ if ($any_unique)
    #                   @my_errno AS sql_errno;
    if (`SELECT @my_errno NOT IN (0,$ER_DUP_KEY,$ER_DUP_ENTRY)`)
    {
-      --echo #      The last command got an unexepected error response.
+      --echo #      The last command got an unexpected error response.
       --echo #      Expected/handled SQL codes are 0,$ER_DUP_KEY,$ER_DUP_ENTRY
       SELECT '#      SQL code we got was: ' AS "", @my_errno AS "";
       --echo #      Sorry, have to abort.
@@ -503,7 +503,7 @@ if ($no_debug)
 eval SET @my_errno = $mysql_errno;
 if (`SELECT @my_errno NOT IN (0,$ER_SAME_NAME_PARTITION,$ER_NO_PARTITION_FOR_GIVEN_VALUE)`)
 {
-   --echo #      The last command got an unexepected error response.
+   --echo #      The last command got an unexpected error response.
    --echo #      Expected/handled SQL codes are 0,$ER_SAME_NAME_PARTITION,$ER_NO_PARTITION_FOR_GIVEN_VALUE
    SELECT '#      SQL code we got was: ' AS "", @my_errno AS "";
    --echo #      Sorry, have to abort.
@@ -566,7 +566,7 @@ eval SET @my_errno = $mysql_errno;
 let $run= `SELECT @my_errno = 0`;
 if (`SELECT @my_errno NOT IN (0,$ER_BAD_NULL_ERROR)`)
 {
-   --echo #      The last command got an unexepected error response.
+   --echo #      The last command got an unexpected error response.
    --echo #      Expected/handled SQL codes are 0,$ER_BAD_NULL_ERROR
    SELECT '#      SQL code we got was: ' AS "", @my_errno AS "";
    --echo #      Sorry, have to abort.

=== modified file 'mysql-test/suite/sys_vars/r/foreign_key_checks_func.result'
--- a/mysql-test/suite/sys_vars/r/foreign_key_checks_func.result	2009-12-22 09:35:56 +0000
+++ b/mysql-test/suite/sys_vars/r/foreign_key_checks_func.result	2010-09-29 22:38:57 +0000
@@ -26,7 +26,7 @@ INSERT INTO t2 values (20,22);
 ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk` FOREIGN KEY (`b`) REFERENCES `t1` (`a`))
 '---Check when foreign_key_checks is disabled---'
 TRUNCATE t1;
-ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk` FOREIGN KEY (`b`) REFERENCES `t1` (`a`))
+ERROR 42000: Cannot truncate table 't1' that is referred to by foreign key in another table
 SET @@session.foreign_key_checks = 0;
 TRUNCATE t1;
 TRUNCATE t2;

=== modified file 'mysql-test/suite/sys_vars/t/foreign_key_checks_func.test'
--- a/mysql-test/suite/sys_vars/t/foreign_key_checks_func.test	2009-12-22 09:35:56 +0000
+++ b/mysql-test/suite/sys_vars/t/foreign_key_checks_func.test	2010-09-29 22:38:57 +0000
@@ -76,7 +76,7 @@ INSERT INTO t2 values (20,22);
 --echo '---Check when foreign_key_checks is disabled---'
 #===========================================================
 
---Error ER_ROW_IS_REFERENCED_2
+--Error ER_TRUNCATE_ILLEGAL_FK
 TRUNCATE t1;
 
 SET @@session.foreign_key_checks = 0;

=== modified file 'mysql-test/t/mdl_sync.test'
--- a/mysql-test/t/mdl_sync.test	2010-09-08 08:25:37 +0000
+++ b/mysql-test/t/mdl_sync.test	2010-09-29 22:38:57 +0000
@@ -3994,16 +3994,17 @@ connection default;
 --echo # Connection: default
 SET debug_sync='now WAIT_FOR parked_flush';
 SET debug_sync='now SIGNAL go_truncate';
+--echo # Ensure that truncate waits for a exclusive lock
+let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
+  WHERE state='Waiting for table metadata lock' AND info='TRUNCATE TABLE t1';
+--source include/wait_condition.inc
+SET debug_sync= 'now SIGNAL go_show';
 
 connection con1;
 --echo # Connection: con1
 --echo # Reaping...
 reap;
 
-connection default;
---echo # Connection: default
-SET debug_sync= 'now SIGNAL go_show';
-
 connection con2;
 --echo # Connection: con2 (SHOW FIELDS FROM t1)
 --echo # Reaping...

=== modified file 'mysql-test/t/trigger-trans.test'
--- a/mysql-test/t/trigger-trans.test	2008-10-07 16:54:12 +0000
+++ b/mysql-test/t/trigger-trans.test	2010-09-29 22:38:57 +0000
@@ -148,10 +148,15 @@ CREATE TRIGGER t1_ad AFTER DELETE ON t1
 SET @a = 0;
 SET @b = 0;
 
+--error ER_TRUNCATE_ILLEGAL_FK
 TRUNCATE t1;
 
 SELECT @a, @b;
 
+DELETE FROM t1;
+
+SELECT @a, @b;
+
 INSERT INTO t1 VALUES (1);
 
 DELETE FROM t1;

=== modified file 'sql/ha_partition.cc'
--- a/sql/ha_partition.cc	2010-08-19 08:22:23 +0000
+++ b/sql/ha_partition.cc	2010-09-29 22:38:57 +0000
@@ -3335,113 +3335,117 @@ int ha_partition::delete_row(const uchar
     Called from sql_delete.cc by mysql_delete().
     Called from sql_select.cc by JOIN::reinit().
     Called from sql_union.cc by st_select_lex_unit::exec().
- 
-     Also used for handle ALTER TABLE t TRUNCATE PARTITION ...
-     NOTE: auto increment value will be truncated in that partition as well!
 */
 
 int ha_partition::delete_all_rows()
 {
   int error;
-  bool truncate= FALSE;
   handler **file;
-  THD *thd= ha_thd();
   DBUG_ENTER("ha_partition::delete_all_rows");
 
-  if (thd->lex->sql_command == SQLCOM_TRUNCATE)
+  file= m_file;
+  do
   {
-    Alter_info *alter_info= &thd->lex->alter_info;
-    /* TRUNCATE also means resetting auto_increment */
-    lock_auto_increment();
-    table_share->ha_part_data->next_auto_inc_val= 0;
-    table_share->ha_part_data->auto_inc_initialized= FALSE;
-    unlock_auto_increment();
-    if (alter_info->flags & ALTER_ADMIN_PARTITION)
-    {
-      /* ALTER TABLE t TRUNCATE PARTITION ... */
-      List_iterator<partition_element> part_it(m_part_info->partitions);
-      int saved_error= 0;
-      uint num_parts= m_part_info->num_parts;
-      uint num_subparts= m_part_info->num_subparts;
-      uint i= 0;
-      uint num_parts_set= alter_info->partition_names.elements;
-      uint num_parts_found= set_part_state(alter_info, m_part_info,
-                                           PART_ADMIN);
-      if (num_parts_set != num_parts_found &&
-          (!(alter_info->flags & ALTER_ALL_PARTITION)))
-        DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);
+    if ((error= (*file)->ha_delete_all_rows()))
+      DBUG_RETURN(error);
+  } while (*(++file));
+  DBUG_RETURN(0);
+}
+
+
+/**
+  Manually truncate the table.
+
+  @remark Auto increment value will be truncated in that partition as well!
+
+  @retval  0    Success.
+  @retval  > 0  Error code.
+*/
+
+int ha_partition::truncate()
+{
+  int error;
+  handler **file;
+  DBUG_ENTER("ha_partition::truncate");
+
+  /* TRUNCATE also means resetting auto_increment */
+  lock_auto_increment();
+  table_share->ha_part_data->next_auto_inc_val= 0;
+  table_share->ha_part_data->auto_inc_initialized= FALSE;
+  unlock_auto_increment();
 
-      /*
-        Cannot return HA_ERR_WRONG_COMMAND here without correct pruning
-        since that whould delete the whole table row by row in sql_delete.cc
-      */
-      bitmap_clear_all(&m_part_info->used_partitions);
-      do
-      {
-        partition_element *part_elem= part_it++;
-        if (part_elem->part_state == PART_ADMIN)
-        {
-          if (m_is_sub_partitioned)
-          {
-            List_iterator<partition_element>
-                                        subpart_it(part_elem->subpartitions);
-            partition_element *sub_elem;
-            uint j= 0, part;
-            do
-            {
-              sub_elem= subpart_it++;
-              part= i * num_subparts + j;
-              bitmap_set_bit(&m_part_info->used_partitions, part);
-              if (!saved_error)
-              {
-                DBUG_PRINT("info", ("truncate subpartition %u (%s)",
-                                    part, sub_elem->partition_name));
-                if ((error= m_file[part]->ha_delete_all_rows()))
-                  saved_error= error;
-                /* If not reset_auto_increment is supported, just accept it */
-                if (!saved_error &&
-                    (error= m_file[part]->ha_reset_auto_increment(0)) &&
-                    error != HA_ERR_WRONG_COMMAND)
-                  saved_error= error;
-              }
-            } while (++j < num_subparts);
-          }
-          else
-          {
-            DBUG_PRINT("info", ("truncate partition %u (%s)", i,
-                                part_elem->partition_name));
-            bitmap_set_bit(&m_part_info->used_partitions, i);
-            if (!saved_error)
-            {
-              if ((error= m_file[i]->ha_delete_all_rows()) && !saved_error)
-                saved_error= error;
-              /* If not reset_auto_increment is supported, just accept it */
-              if (!saved_error &&
-                  (error= m_file[i]->ha_reset_auto_increment(0)) &&
-                  error != HA_ERR_WRONG_COMMAND)
-                saved_error= error;
-            }
-          }
-          part_elem->part_state= PART_NORMAL;
-        }
-      } while (++i < num_parts);
-      DBUG_RETURN(saved_error);
-    }
-    truncate= TRUE;
-  }
   file= m_file;
   do
   {
-    if ((error= (*file)->ha_delete_all_rows()))
+    if ((error= (*file)->ha_truncate()))
       DBUG_RETURN(error);
-    /* Ignore the error */
-    if (truncate)
-      (void) (*file)->ha_reset_auto_increment(0);
   } while (*(++file));
   DBUG_RETURN(0);
 }
 
 
+/**
+  Truncate a set of specific partitioins.
+
+  ALTER TABLE t TRUNCATE PARTITION ...
+*/
+
+int ha_partition::truncate_partition(Alter_info *alter_info)
+{
+  int error= 0;
+  List_iterator<partition_element> part_it(m_part_info->partitions);
+  uint num_parts= m_part_info->num_parts;
+  uint num_subparts= m_part_info->num_subparts;
+  uint i= 0;
+  uint num_parts_set= alter_info->partition_names.elements;
+  uint num_parts_found= set_part_state(alter_info, m_part_info,
+                                        PART_ADMIN);
+  DBUG_ENTER("ha_partition::truncate_partition");
+
+  /* TRUNCATE also means resetting auto_increment */
+  lock_auto_increment();
+  table_share->ha_part_data->next_auto_inc_val= 0;
+  table_share->ha_part_data->auto_inc_initialized= FALSE;
+  unlock_auto_increment();
+
+  if (num_parts_set != num_parts_found &&
+      (!(alter_info->flags & ALTER_ALL_PARTITION)))
+    DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);
+
+  do
+  {
+    partition_element *part_elem= part_it++;
+    if (part_elem->part_state == PART_ADMIN)
+    {
+      if (m_is_sub_partitioned)
+      {
+        List_iterator<partition_element>
+                                    subpart_it(part_elem->subpartitions);
+        partition_element *sub_elem;
+        uint j= 0, part;
+        do
+        {
+          sub_elem= subpart_it++;
+          part= i * num_subparts + j;
+          DBUG_PRINT("info", ("truncate subpartition %u (%s)",
+                              part, sub_elem->partition_name));
+          if ((error= m_file[part]->ha_truncate()))
+            break;
+        } while (++j < num_subparts);
+      }
+      else
+      {
+        DBUG_PRINT("info", ("truncate partition %u (%s)", i,
+                            part_elem->partition_name));
+        error= m_file[i]->ha_truncate();
+      }
+      part_elem->part_state= PART_NORMAL;
+    }
+  } while (!error && (++i < num_parts));
+  DBUG_RETURN(error);
+}
+
+
 /*
   Start a large batch of insert rows
 
@@ -6286,8 +6290,8 @@ void ha_partition::print_error(int error
   /* Should probably look for my own errors first */
   DBUG_PRINT("enter", ("error: %d", error));
 
-  if (error == HA_ERR_NO_PARTITION_FOUND &&
-      thd->lex->sql_command != SQLCOM_TRUNCATE)
+  if ((error == HA_ERR_NO_PARTITION_FOUND) &&
+      ! (thd->lex->alter_info.flags & ALTER_TRUNCATE_PARTITION))
     m_part_info->print_no_partition_found(table);
   else
   {

=== modified file 'sql/ha_partition.h'
--- a/sql/ha_partition.h	2010-07-23 20:09:27 +0000
+++ b/sql/ha_partition.h	2010-09-29 22:38:57 +0000
@@ -342,6 +342,7 @@ public:
   virtual int update_row(const uchar * old_data, uchar * new_data);
   virtual int delete_row(const uchar * buf);
   virtual int delete_all_rows(void);
+  virtual int truncate();
   virtual void start_bulk_insert(ha_rows rows);
   virtual int end_bulk_insert();
 private:
@@ -350,6 +351,12 @@ private:
   long estimate_read_buffer_size(long original_size);
 public:
 
+  /*
+    Method for truncating a specific partition.
+    (i.e. ALTER TABLE t1 TRUNCATE PARTITION p).
+  */
+  int truncate_partition(Alter_info *);
+
   virtual bool is_fatal_error(int error, uint flags)
   {
     if (!handler::is_fatal_error(error, flags) ||

=== modified file 'sql/handler.cc'
--- a/sql/handler.cc	2010-08-09 18:33:47 +0000
+++ b/sql/handler.cc	2010-09-29 22:38:57 +0000
@@ -3209,6 +3209,21 @@ handler::ha_delete_all_rows()
 
 
 /**
+  Truncate table: public interface.
+
+  @sa handler::truncate()
+*/
+
+int
+handler::ha_truncate()
+{
+  mark_trx_read_write();
+
+  return truncate();
+}
+
+
+/**
   Reset auto increment: public interface.
 
   @sa handler::reset_auto_increment()

=== modified file 'sql/handler.h'
--- a/sql/handler.h	2010-08-18 11:55:37 +0000
+++ b/sql/handler.h	2010-09-29 22:38:57 +0000
@@ -1331,6 +1331,7 @@ public:
   int ha_bulk_update_row(const uchar *old_data, uchar *new_data,
                          uint *dup_key_found);
   int ha_delete_all_rows();
+  int ha_truncate();
   int ha_reset_auto_increment(ulonglong value);
   int ha_optimize(THD* thd, HA_CHECK_OPT* check_opt);
   int ha_analyze(THD* thd, HA_CHECK_OPT* check_opt);
@@ -2010,12 +2011,31 @@ private:
     This is called to delete all rows in a table
     If the handler don't support this, then this function will
     return HA_ERR_WRONG_COMMAND and MySQL will delete the rows one
-    by one. It should reset auto_increment if
-    thd->lex->sql_command == SQLCOM_TRUNCATE.
+    by one.
   */
   virtual int delete_all_rows()
   { return (my_errno=HA_ERR_WRONG_COMMAND); }
   /**
+    Quickly remove all rows from a table.
+
+    @remark This method is responsible for implementing MySQL's TRUNCATE
+            TABLE statement, which is a DDL operation. As such, a engine
+            can bypass certain integrity checks and in some cases avoid
+            fine-grained locking (e.g. row locks) which would normally be
+            required for a DELETE statement.
+
+    @remark Typically, truncate is not used if it can result in integrity
+            violation. For example, truncate is not used when a foreign
+            key references the table, but it might be used if foreign key
+            checks are disabled.
+
+    @remark Engine is responsible for reseting the auto-increment counter.
+
+    @remark The table is locked in exclusive mode.
+  */
+  virtual int truncate()
+  { return HA_ERR_WRONG_COMMAND; }
+  /**
     Reset the auto-increment counter to the given value, i.e. the next row
     inserted will get the given value. This is called e.g. after TRUNCATE
     is emulated by doing a 'DELETE FROM t'. HA_ERR_WRONG_COMMAND is

=== modified file 'sql/share/errmsg-utf8.txt'
--- a/sql/share/errmsg-utf8.txt	2010-06-07 08:47:04 +0000
+++ b/sql/share/errmsg-utf8.txt	2010-09-29 22:38:57 +0000
@@ -6346,3 +6346,6 @@ ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_L
 ER_FAILED_READ_FROM_PAR_FILE
   eng "Failed to read from the .par file"
   swe "Misslyckades läsa från .par filen"
+
+ER_TRUNCATE_ILLEGAL_FK 42000
+  eng "Cannot truncate table '%s' that is referred to by foreign key in another table"

=== modified file 'sql/sql_lex.h'
--- a/sql/sql_lex.h	2010-09-24 08:44:09 +0000
+++ b/sql/sql_lex.h	2010-09-29 22:38:57 +0000
@@ -958,6 +958,7 @@ inline bool st_select_lex_unit::is_union
 #define ALTER_ALL_PARTITION      (1L << 21)
 #define ALTER_REMOVE_PARTITIONING (1L << 22)
 #define ALTER_FOREIGN_KEY        (1L << 23)
+#define ALTER_TRUNCATE_PARTITION (1L << 24)
 
 enum enum_alter_table_change_level
 {

=== modified file 'sql/sql_parse.cc'
--- a/sql/sql_parse.cc	2010-09-22 08:15:41 +0000
+++ b/sql/sql_parse.cc	2010-09-29 22:38:57 +0000
@@ -49,7 +49,7 @@
                               // mysql_recreate_table,
                               // mysql_backup_table,
                               // mysql_restore_table
-#include "sql_truncate.h"     // mysql_truncate_table
+#include "sql_truncate.h"     // Truncate_statement
 #include "sql_reload.h"       // reload_acl_and_cache
 #include "sql_admin.h"        // mysql_assign_to_keycache
 #include "sql_connect.h"      // check_user,

=== modified file 'sql/sql_partition_admin.cc'
--- a/sql/sql_partition_admin.cc	2010-08-16 14:25:23 +0000
+++ b/sql/sql_partition_admin.cc	2010-09-29 22:38:57 +0000
@@ -16,10 +16,10 @@
 #include "sql_parse.h"                      // check_one_table_access
 #include "sql_table.h"                      // mysql_alter_table, etc.
 #include "sql_lex.h"                        // Sql_statement
-#include "sql_truncate.h"                   // mysql_truncate_table,
-                                            // Truncate_statement
 #include "sql_admin.h"                      // Analyze/Check/.._table_statement
 #include "sql_partition_admin.h"            // Alter_table_*_partition
+#include "ha_partition.h"                   // ha_partition
+#include "sql_base.h"                       // open_and_lock_tables
 
 #ifndef WITH_PARTITION_STORAGE_ENGINE
 
@@ -46,7 +46,7 @@ bool Alter_table_analyze_partition_state
   m_lex->alter_info.flags|= ALTER_ADMIN_PARTITION;
 
   res= Analyze_table_statement::execute(thd);
-    
+
   DBUG_RETURN(res);
 }
 
@@ -104,36 +104,64 @@ bool Alter_table_repair_partition_statem
 
 bool Alter_table_truncate_partition_statement::execute(THD *thd)
 {
+  int error;
+  ha_partition *partition;
   TABLE_LIST *first_table= thd->lex->select_lex.table_list.first;
-  bool res;
-  enum_sql_command original_sql_command;
   DBUG_ENTER("Alter_table_truncate_partition_statement::execute");
 
   /*
-    Execute TRUNCATE PARTITION just like TRUNCATE TABLE.
-    Some storage engines (InnoDB, partition) checks thd_sql_command,
-    so we set it to SQLCOM_TRUNCATE during the execution.
-  */
-  original_sql_command= m_lex->sql_command;
-  m_lex->sql_command= SQLCOM_TRUNCATE;
-  
-  /*
     Flag that it is an ALTER command which administrates partitions, used
     by ha_partition.
   */
-  m_lex->alter_info.flags|= ALTER_ADMIN_PARTITION;
-   
+  m_lex->alter_info.flags|= ALTER_ADMIN_PARTITION |
+                            ALTER_TRUNCATE_PARTITION;
+
+  /* Fix the lock types (not the same as ordinary ALTER TABLE). */
+  first_table->lock_type= TL_WRITE;
+  first_table->mdl_request.set_type(MDL_EXCLUSIVE);
+
   /*
-    Fix the lock types (not the same as ordinary ALTER TABLE).
+    Check table permissions and open it with a exclusive lock.
+    Ensure it is a partitioned table and finally, upcast the
+    handler and invoke the partition truncate method. Lastly,
+    write the statement to the binary log if necessary.
   */
-  first_table->lock_type= TL_WRITE;
-  first_table->mdl_request.set_type(MDL_SHARED_NO_READ_WRITE);
 
-  /* execute as a TRUNCATE TABLE */
-  res= Truncate_statement::execute(thd);
+  if (check_one_table_access(thd, DROP_ACL, first_table))
+    DBUG_RETURN(TRUE);
 
-  m_lex->sql_command= original_sql_command;
-  DBUG_RETURN(res);
+  if (open_and_lock_tables(thd, first_table, FALSE, 0))
+    DBUG_RETURN(TRUE);
+
+  /*
+    TODO: Add support for TRUNCATE PARTITION for NDB and other
+          engines supporting native partitioning.
+  */
+  if (first_table->table->s->db_type() != partition_hton)
+  {
+    my_error(ER_PARTITION_MGMT_ON_NONPARTITIONED, MYF(0));
+    DBUG_RETURN(TRUE);
+  }
+
+  partition= (ha_partition *) first_table->table->file;
+
+  /* Invoke the handler method responsible for truncating the partition. */
+  if ((error= partition->truncate_partition(&thd->lex->alter_info)))
+    first_table->table->file->print_error(error, MYF(0));
+
+  /*
+    All effects of a truncate operation are committed even if the
+    operation fails. Thus, the query must be written to the binary
+    log. The only exception is a unimplemented truncate method. Also,
+    it is logged in statement format, regardless of the binlog format.
+  */
+  if (error != HA_ERR_WRONG_COMMAND)
+    error|= write_bin_log(thd, !error, thd->query(), thd->query_length());
+
+  if (! error)
+    my_ok(thd);
+
+  DBUG_RETURN(error);
 }
 
 #endif /* WITH_PARTITION_STORAGE_ENGINE */

=== modified file 'sql/sql_partition_admin.h'
--- a/sql/sql_partition_admin.h	2010-08-16 12:53:30 +0000
+++ b/sql/sql_partition_admin.h	2010-09-29 22:38:57 +0000
@@ -210,7 +210,7 @@ public:
 /**
   Class that represents the ALTER TABLE t1 TRUNCATE PARTITION p statement.
 */
-class Alter_table_truncate_partition_statement : public Truncate_statement
+class Alter_table_truncate_partition_statement : public Sql_statement
 {
 public:
   /**
@@ -218,10 +218,10 @@ public:
     @param lex the LEX structure for this statement.
   */
   Alter_table_truncate_partition_statement(LEX *lex)
-    : Truncate_statement(lex)
+    : Sql_statement(lex)
   {}
 
-  ~Alter_table_truncate_partition_statement()
+  virtual ~Alter_table_truncate_partition_statement()
   {}
 
   /**

=== modified file 'sql/sql_truncate.cc'
--- a/sql/sql_truncate.cc	2010-08-31 10:03:36 +0000
+++ b/sql/sql_truncate.cc	2010-09-29 22:38:57 +0000
@@ -29,145 +29,120 @@
 #include "sql_truncate.h"
 
 
-/*
-  Delete all rows of a locked table.
-
-  @param  thd           Thread context.
-  @param  table_list    Table list element for the table.
-  @param  rows_deleted  Whether rows might have been deleted.
-
-  @retval  FALSE  Success.
-  @retval  TRUE   Error.
+/**
+  Check if table which is going to be affected by TRUNCATE TABLE
+  is a parent table in some non-self-referencing foreign key and
+  if yes emit an error that it is illegal to truncate such table.
+
+  @param  thd    Thread context.
+  @param  table  Table handle.
+
+  @retval FALSE  This table is not parent in a non-self-referencing foreign
+                 key. Statement can proceed.
+  @retval TRUE   This table is parent in a non-self-referencing foreign key,
+                 error was emitted.
 */
 
 static bool
-delete_all_rows(THD *thd, TABLE *table)
+fk_truncate_illegal_if_parent(THD *thd, TABLE *table)
 {
-  int error;
-  READ_RECORD info;
-  bool is_bulk_delete;
-  bool some_rows_deleted= FALSE;
-  bool save_binlog_row_based= thd->is_current_stmt_binlog_format_row();
-  DBUG_ENTER("delete_all_rows");
+  FOREIGN_KEY_INFO *f_key_info;
+  List<FOREIGN_KEY_INFO> f_key_list;
+  List_iterator_fast<FOREIGN_KEY_INFO> it;
 
-  /* Replication of truncate table must be statement based. */
-  thd->clear_current_stmt_binlog_format_row();
+  /* Whether the table is referenced by a foreign key. */
+  if (! table->file->referenced_by_foreign_key())
+    return FALSE;
 
-  /*
-    Update handler statistics (e.g. table->file->stats.records).
-    Might be used by the storage engine to aggregate information
-    necessary to allow deletion. Currently, this seems to be
-    meaningful only to the archive storage engine, which uses
-    the info method to set the number of records. Although
-    archive does not support deletion, it becomes necessary in
-    order to return a error if the table is not empty.
-  */
-  error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
-  if (error && error != HA_ERR_WRONG_COMMAND)
-  {
-    table->file->print_error(error, MYF(0));
-    goto end;
-  }
+  table->file->get_foreign_key_list(thd, &f_key_list);
 
-  /*
-    Attempt to delete all rows in the table.
-    If it is unsupported, switch to row by row deletion.
-  */
-  if (! (error= table->file->ha_delete_all_rows()))
-    goto end;
+  it.init(f_key_list);
 
-  if (error != HA_ERR_WRONG_COMMAND)
+  /* Check whether it is a self-referencing FK. */
+  while ((f_key_info= it++))
   {
-    /*
-      If a transactional engine fails in the middle of deletion,
-      we expect it to be able to roll it back.  Some reasons
-      for the engine to fail would be media failure or corrupted
-      data dictionary (i.e. in case of a partitioned table). We
-      have sufficiently strong metadata locks to rule out any
-      potential deadlocks.
-
-      If a non-transactional engine fails here (that would
-      not be MyISAM, since MyISAM does TRUNCATE by recreate),
-      and binlog is on, replication breaks, since nothing gets
-      written to the binary log.  (XXX: is this a bug?)
-    */
-    table->file->print_error(error, MYF(0));
-    goto end;
+    if (my_strcasecmp(system_charset_info, f_key_info->referenced_db->str,
+                      table->s->db.str) ||
+        my_strcasecmp(system_charset_info, f_key_info->referenced_table->str,
+                      table->s->table_name.str))
+      break;
   }
 
-  /*
-    A workaround for Bug#53696  "Performance schema engine violates the
-    PSEA API by calling my_error()".
-  */
-  if (thd->is_error())
-    goto end;
-
-  /* Handler didn't support fast delete. Delete rows one by one. */
-
-  init_read_record(&info, thd, table, NULL, TRUE, TRUE, FALSE);
-
-  /*
-    Start bulk delete. If the engine does not support it, go on,
-    it's not an error.
-  */
-  is_bulk_delete= ! table->file->start_bulk_delete();
+  /* Table is parent in a non-self-referencing foreign key. */
+  if (f_key_list.is_empty() || f_key_info)
+  {
+    my_error(ER_TRUNCATE_ILLEGAL_FK, MYF(0), table->s->table_name.str);
+    return TRUE;
+  }
 
-  table->mark_columns_needed_for_delete();
+  return FALSE;
+}
 
-  while (!(error= info.read_record(&info)) && !thd->killed)
-  {
-    if ((error= table->file->ha_delete_row(table->record[0])))
-    {
-      table->file->print_error(error, MYF(0));
-      break;
-    }
 
-    some_rows_deleted= TRUE;
-  }
+/*
+  Open and truncate a locked table.
 
-  /* HA_ERR_END_OF_FILE */
-  if (error == -1)
-    error= 0;
+  @param  thd           Thread context.
+  @param  table_ref     Table list element for the table to be truncated.
+  @param  is_tmp_table  True if element refers to a temp table.
 
-  /* Close down the bulk delete. */
-  if (is_bulk_delete)
-  {
-    int bulk_delete_error= table->file->end_bulk_delete();
-    if (bulk_delete_error && !error)
-    {
-      table->file->print_error(bulk_delete_error, MYF(0));
-      error= bulk_delete_error;
-    }
-  }
+  @retval  0    Success.
+  @retval  > 0  Error code.
+*/
 
-  end_read_record(&info);
+int Truncate_statement::handler_truncate(THD *thd, TABLE_LIST *table_ref,
+                                         bool is_tmp_table)
+{
+  int error= 0;
+  uint flags;
+  DBUG_ENTER("Truncate_statement::handler_truncate");
 
   /*
-    Regardless of the error status, the query must be written to the
-    binary log if rows of the table is non-transactional.
+    Can't recreate, the engine must mechanically delete all rows
+    in the table. Use open_and_lock_tables() to open a write cursor.
   */
-  if (some_rows_deleted && !table->file->has_transactions())
+
+  /* If it is a temporary table, no need to take locks. */
+  if (is_tmp_table)
+    flags= MYSQL_OPEN_TEMPORARY_ONLY;
+  else
   {
-    thd->transaction.stmt.modified_non_trans_table= TRUE;
-    thd->transaction.all.modified_non_trans_table= TRUE;
+    /* We don't need to load triggers. */
+    DBUG_ASSERT(table_ref->trg_event_map == 0);
+    /*
+      Our metadata lock guarantees that no transaction is reading
+      or writing into the table. Yet, to open a write cursor we need
+      a thr_lock lock. Allow to open base tables only.
+    */
+    table_ref->required_type= FRMTYPE_TABLE;
+    /*
+      Ignore pending FLUSH TABLES since we don't want to release
+      the MDL lock taken above and otherwise there is no way to
+      wait for FLUSH TABLES in deadlock-free fashion.
+    */
+    flags= MYSQL_OPEN_IGNORE_FLUSH | MYSQL_OPEN_SKIP_TEMPORARY;
+    /*
+      Even though we have an MDL lock on the table here, we don't
+      pass MYSQL_OPEN_HAS_MDL_LOCK to open_and_lock_tables
+      since to truncate a MERGE table, we must open and lock
+      merge children, and on those we don't have an MDL lock.
+      Thus clear the ticket to satisfy MDL asserts.
+    */
+    table_ref->mdl_request.ticket= NULL;
+    /* The handler truncate protocol dictates a exclusive lock. */
+    table_ref->mdl_request.set_type(MDL_EXCLUSIVE);
   }
 
-  if (error || thd->killed)
-    goto end;
+  /* Open the table as it will handle some required preparations. */
+  if (open_and_lock_tables(thd, table_ref, FALSE, flags))
+    DBUG_RETURN(1);
 
-  /* Truncate resets the auto-increment counter. */
-  error= table->file->ha_reset_auto_increment(0);
-  if (error)
-  {
-    if (error != HA_ERR_WRONG_COMMAND)
-      table->file->print_error(error, MYF(0));
-    else
-      error= 0;
-  }
+  /* Whether to truncate regardless of foreign keys. */
+  if (! (thd->variables.option_bits & OPTION_NO_FOREIGN_KEY_CHECKS))
+    error= fk_truncate_illegal_if_parent(thd, table_ref->table);
 
-end:
-  if (save_binlog_row_based)
-    thd->set_current_stmt_binlog_format_row();
+  if (!error && (error= table_ref->table->file->ha_truncate()))
+    table_ref->table->file->print_error(error, MYF(0));
 
   DBUG_RETURN(error);
 }
@@ -225,30 +200,27 @@ static bool recreate_temporary_table(THD
 
 
 /*
-  Handle opening and locking if a base table for truncate.
+  Handle locking a base table for truncate.
 
   @param[in]  thd               Thread context.
   @param[in]  table_ref         Table list element for the table to
                                 be truncated.
   @param[out] hton_can_recreate Set to TRUE if table can be dropped
                                 and recreated.
-  @param[out] ticket_downgrade  Set if a lock must be downgraded after
-                                truncate is done.
 
   @retval  FALSE  Success.
   @retval  TRUE   Error.
 */
 
-static bool open_and_lock_table_for_truncate(THD *thd, TABLE_LIST *table_ref,
-                                             bool *hton_can_recreate,
-                                             MDL_ticket **ticket_downgrade)
+bool Truncate_statement::lock_table(THD *thd, TABLE_LIST *table_ref,
+                                    bool *hton_can_recreate)
 {
   TABLE *table= NULL;
-  handlerton *table_type;
-  DBUG_ENTER("open_and_lock_table_for_truncate");
+  DBUG_ENTER("Truncate_statement::lock_table");
 
   DBUG_ASSERT(table_ref->lock_type == TL_WRITE);
-  DBUG_ASSERT(table_ref->mdl_request.type == MDL_SHARED_NO_READ_WRITE);
+  DBUG_ASSERT(table_ref->mdl_request.type >= MDL_SHARED_NO_READ_WRITE);
+
   /*
     Before doing anything else, acquire a metadata lock on the table,
     or ensure we have one.  We don't use open_and_lock_tables()
@@ -268,8 +240,7 @@ static bool open_and_lock_table_for_trun
                                             table_ref->table_name, FALSE)))
       DBUG_RETURN(TRUE);
 
-    table_type= table->s->db_type();
-    *hton_can_recreate= ha_check_storage_engine_flag(table_type,
+    *hton_can_recreate= ha_check_storage_engine_flag(table->s->db_type(),
                                                      HTON_CAN_RECREATE);
     table_ref->mdl_request.ticket= table->mdl_ticket;
   }
@@ -285,86 +256,36 @@ static bool open_and_lock_table_for_trun
                          MYSQL_OPEN_SKIP_TEMPORARY))
       DBUG_RETURN(TRUE);
 
-    if (dd_frm_storage_engine(thd, table_ref->db, table_ref->table_name,
-                              &table_type))
+    if (dd_check_storage_engine_flag(thd, table_ref->db, table_ref->table_name,
+                                     HTON_CAN_RECREATE, hton_can_recreate))
       DBUG_RETURN(TRUE);
-    *hton_can_recreate= ha_check_storage_engine_flag(table_type,
-                                                     HTON_CAN_RECREATE);
   }
 
-#ifdef WITH_PARTITION_STORAGE_ENGINE
-  /*
-    TODO: Add support for TRUNCATE PARTITION for NDB and other engines
-    supporting native partitioning.
-  */
-  if (thd->lex->alter_info.flags & ALTER_ADMIN_PARTITION &&
-      table_type != partition_hton)
-  {
-    my_error(ER_PARTITION_MGMT_ON_NONPARTITIONED, MYF(0));
-    DBUG_RETURN(TRUE);
-  }
-#endif
   DEBUG_SYNC(thd, "lock_table_for_truncate");
 
-  if (*hton_can_recreate)
+  /*
+    Acquire an exclusive lock. A storage engine can recreate or
+    truncate the table only if there are no references to it from
+    anywhere, i.e. no cached TABLE in the table cache. To remove
+    the table from the cache we need an exclusive lock.
+  */
+  if (thd->locked_tables_mode)
   {
-    /*
-      Acquire an exclusive lock. The storage engine can recreate the
-      table only if there are no references to it from anywhere, i.e.
-      no cached TABLE in the table cache. To remove the table from the
-      cache we need an exclusive lock.
-    */
-    if (thd->locked_tables_mode)
-    {
-      if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN))
-        DBUG_RETURN(TRUE);
-      *ticket_downgrade= table->mdl_ticket;
+    if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN))
+      DBUG_RETURN(TRUE);
+    m_ticket_downgrade= table->mdl_ticket;
+    /* Close if table is going to be recreated. */
+    if (*hton_can_recreate)
       close_all_tables_for_name(thd, table->s, FALSE);
-    }
-    else
-    {
-      ulong timeout= thd->variables.lock_wait_timeout;
-      if (thd->mdl_context.
-          upgrade_shared_lock_to_exclusive(table_ref->mdl_request.ticket,
-                                           timeout))
-        DBUG_RETURN(TRUE);
-      tdc_remove_table(thd, TDC_RT_REMOVE_ALL, table_ref->db,
-                       table_ref->table_name, FALSE);
-    }
   }
   else
   {
-    /*
-      Can't recreate, we must mechanically delete all rows in
-      the table. Our metadata lock guarantees that no transaction
-      is reading or writing into the table. Yet, to open a write
-      cursor we need a thr_lock lock. Use open_and_lock_tables()
-      to do the necessary job.
-    */
-
-    /* Allow to open base tables only. */
-    table_ref->required_type= FRMTYPE_TABLE;
-    /* We don't need to load triggers. */
-    DBUG_ASSERT(table_ref->trg_event_map == 0);
-    /*
-      Even though we have an MDL lock on the table here, we don't
-      pass MYSQL_OPEN_HAS_MDL_LOCK to open_and_lock_tables
-      since to truncate a MERGE table, we must open and lock
-      merge children, and on those we don't have an MDL lock.
-      Thus clear the ticket to satisfy MDL asserts.
-    */
-    table_ref->mdl_request.ticket= NULL;
-
-    /*
-      Open the table as it will handle some required preparations.
-      Ignore pending FLUSH TABLES since we don't want to release
-      the MDL lock taken above and otherwise there is no way to
-      wait for FLUSH TABLES in deadlock-free fashion.
-    */
-    if (open_and_lock_tables(thd, table_ref, FALSE,
-                             MYSQL_OPEN_IGNORE_FLUSH |
-                             MYSQL_OPEN_SKIP_TEMPORARY))
+    ulong timeout= thd->variables.lock_wait_timeout;
+    if (thd->mdl_context.
+        upgrade_shared_lock_to_exclusive(table_ref->mdl_request.ticket, timeout))
       DBUG_RETURN(TRUE);
+    tdc_remove_table(thd, TDC_RT_REMOVE_ALL, table_ref->db,
+                     table_ref->table_name, FALSE);
   }
 
   DBUG_RETURN(FALSE);
@@ -385,14 +306,14 @@ static bool open_and_lock_table_for_trun
   @retval  TRUE   Error.
 */
 
-bool mysql_truncate_table(THD *thd, TABLE_LIST *table_ref)
+bool Truncate_statement::truncate_table(THD *thd, TABLE_LIST *table_ref)
 {
+  int error;
   TABLE *table;
-  bool error= TRUE, binlog_stmt;
-  MDL_ticket *mdl_ticket= NULL;
-  DBUG_ENTER("mysql_truncate_table");
+  bool binlog_stmt;
+  DBUG_ENTER("Truncate_statement::truncate_table");
 
-  /* Remove tables from the HANDLER's hash. */
+  /* Remove table from the HANDLER's hash. */
   mysql_ha_rm_tables(thd, table_ref);
 
   /* If it is a temporary table, no need to take locks. */
@@ -413,14 +334,11 @@ bool mysql_truncate_table(THD *thd, TABL
     {
       /*
         The engine does not support truncate-by-recreate. Open the
-        table and delete all rows. In such a manner this can in fact
-        open several tables if it's a temporary MyISAMMRG table.
+        table and invoke the handler truncate. In such a manner this
+        can in fact open several tables if it's a temporary MyISAMMRG
+        table.
       */
-      if (open_and_lock_tables(thd, table_ref, FALSE,
-                               MYSQL_OPEN_TEMPORARY_ONLY))
-        DBUG_RETURN(TRUE);
-
-      error= delete_all_rows(thd, table_ref->table);
+      error= handler_truncate(thd, table_ref, TRUE);
     }
 
     /*
@@ -434,8 +352,7 @@ bool mysql_truncate_table(THD *thd, TABL
   {
     bool hton_can_recreate;
 
-    if (open_and_lock_table_for_truncate(thd, table_ref,
-                                         &hton_can_recreate, &mdl_ticket))
+    if (lock_table(thd, table_ref, &hton_can_recreate))
       DBUG_RETURN(TRUE);
 
     if (hton_can_recreate)
@@ -454,13 +371,18 @@ bool mysql_truncate_table(THD *thd, TABL
     }
     else
     {
-      error= delete_all_rows(thd, table_ref->table);
+      /*
+        The engine does not support truncate-by-recreate.
+        Attempt to use the handler truncate method.
+      */
+      error= handler_truncate(thd, table_ref, FALSE);
 
       /*
-        Regardless of the error status, the query must be written to the
-        binary log if rows of a non-transactional table were deleted.
+        All effects of a TRUNCATE TABLE operation are committed even if
+        truncation fails. Thus, the query must be written to the binary
+        log. The only exception is a unimplemented truncate method.
       */
-      binlog_stmt= !error || thd->transaction.stmt.modified_non_trans_table;
+      binlog_stmt= !error || error != HA_ERR_WRONG_COMMAND;
     }
 
     query_cache_invalidate3(thd, table_ref, FALSE);
@@ -471,29 +393,25 @@ bool mysql_truncate_table(THD *thd, TABL
     error|= write_bin_log(thd, !error, thd->query(), thd->query_length());
 
   /*
-    All effects of a TRUNCATE TABLE operation are rolled back if a row
-    by row deletion fails. Otherwise, it is automatically committed at
-    the end.
-  */
-  if (error)
-  {
-    trans_rollback_stmt(thd);
-    trans_rollback(thd);
-  }
-
-  /*
     A locked table ticket was upgraded to a exclusive lock. After the
     the query has been written to the binary log, downgrade the lock
     to a shared one.
   */
-  if (mdl_ticket)
-    mdl_ticket->downgrade_exclusive_lock(MDL_SHARED_NO_READ_WRITE);
+  if (m_ticket_downgrade)
+    m_ticket_downgrade->downgrade_exclusive_lock(MDL_SHARED_NO_READ_WRITE);
 
-  DBUG_PRINT("exit", ("error: %d", error));
-  DBUG_RETURN(test(error));
+  DBUG_RETURN(error);
 }
 
 
+/**
+  Execute a TRUNCATE statement at runtime.
+
+  @param  thd   The current thread.
+
+  @return FALSE on success.
+*/
+
 bool Truncate_statement::execute(THD *thd)
 {
   TABLE_LIST *first_table= thd->lex->select_lex.table_list.first;
@@ -502,9 +420,10 @@ bool Truncate_statement::execute(THD *th
 
   if (check_one_table_access(thd, DROP_ACL, first_table))
     goto error;
+
   /*
     Don't allow this within a transaction because we want to use
-    re-generate table
+    re-generate table.
   */
   if (thd->in_active_multi_stmt_transaction())
   {
@@ -512,8 +431,11 @@ bool Truncate_statement::execute(THD *th
                ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
     goto error;
   }
-  if (! (res= mysql_truncate_table(thd, first_table)))
+
+  if (! (res= truncate_table(thd, first_table)))
     my_ok(thd);
+
 error:
   DBUG_RETURN(res);
 }
+

=== modified file 'sql/sql_truncate.h'
--- a/sql/sql_truncate.h	2010-08-16 12:53:30 +0000
+++ b/sql/sql_truncate.h	2010-09-29 22:38:57 +0000
@@ -18,23 +18,26 @@
 class THD;
 struct TABLE_LIST;
 
-bool mysql_truncate_table(THD *thd, TABLE_LIST *table_ref);
-
 /**
   Truncate_statement represents the TRUNCATE statement.
 */
 class Truncate_statement : public Sql_statement
 {
+private:
+  /* Set if a lock must be downgraded after truncate is done. */
+  MDL_ticket *m_ticket_downgrade;
+
 public:
   /**
     Constructor, used to represent a ALTER TABLE statement.
     @param lex the LEX structure for this statement.
   */
   Truncate_statement(LEX *lex)
-    : Sql_statement(lex)
+    : Sql_statement(lex),
+      m_ticket_downgrade(NULL)
   {}
 
-  ~Truncate_statement()
+  virtual ~Truncate_statement()
   {}
 
   /**
@@ -43,7 +46,20 @@ public:
     @return false on success.
   */
   bool execute(THD *thd);
-};
 
+protected:
+  /** Handle locking a base table for truncate. */
+  bool lock_table(THD *, TABLE_LIST *, bool *);
+
+  /** Truncate table via the handler method. */
+  int handler_truncate(THD *, TABLE_LIST *, bool);
+
+  /**
+    Optimized delete of all rows by doing a full generate of the table.
+    Depending on the storage engine, it can be accomplished through a
+    drop and recreate or via the handler truncate method.
+  */
+  bool truncate_table(THD *, TABLE_LIST *);
+};
 
 #endif

=== modified file 'storage/archive/ha_archive.cc'
--- a/storage/archive/ha_archive.cc	2010-07-26 15:54:20 +0000
+++ b/storage/archive/ha_archive.cc	2010-09-29 22:38:57 +0000
@@ -1650,9 +1650,9 @@ int ha_archive::end_bulk_insert()
   This is done for security reasons. In a later version we will enable this by 
   allowing the user to select a different row format.
 */
-int ha_archive::delete_all_rows()
+int ha_archive::truncate()
 {
-  DBUG_ENTER("ha_archive::delete_all_rows");
+  DBUG_ENTER("ha_archive::truncate");
   DBUG_RETURN(HA_ERR_WRONG_COMMAND);
 }
 

=== modified file 'storage/archive/ha_archive.h'
--- a/storage/archive/ha_archive.h	2010-07-26 15:54:20 +0000
+++ b/storage/archive/ha_archive.h	2010-09-29 22:38:57 +0000
@@ -115,7 +115,7 @@ public:
   int close(void);
   int write_row(uchar * buf);
   int real_write_row(uchar *buf, azio_stream *writer);
-  int delete_all_rows();
+  int truncate();
   int rnd_init(bool scan=1);
   int rnd_next(uchar *buf);
   int rnd_pos(uchar * buf, uchar *pos);

=== modified file 'storage/blackhole/ha_blackhole.cc'
--- a/storage/blackhole/ha_blackhole.cc	2010-07-08 21:20:08 +0000
+++ b/storage/blackhole/ha_blackhole.cc	2010-09-29 22:38:57 +0000
@@ -87,6 +87,12 @@ int ha_blackhole::create(const char *nam
   DBUG_RETURN(0);
 }
 
+int ha_blackhole::truncate()
+{
+  DBUG_ENTER("ha_blackhole::truncate");
+  DBUG_RETURN(0);
+}
+
 const char *ha_blackhole::index_type(uint key_number)
 {
   DBUG_ENTER("ha_blackhole::index_type");

=== modified file 'storage/blackhole/ha_blackhole.h'
--- a/storage/blackhole/ha_blackhole.h	2010-03-31 14:05:33 +0000
+++ b/storage/blackhole/ha_blackhole.h	2010-09-29 22:38:57 +0000
@@ -76,6 +76,7 @@ public:
   uint max_supported_key_part_length() const { return BLACKHOLE_MAX_KEY_LENGTH; }
   int open(const char *name, int mode, uint test_if_locked);
   int close(void);
+  int truncate();
   int rnd_init(bool scan);
   int rnd_next(uchar *buf);
   int rnd_pos(uchar * buf, uchar *pos);

=== modified file 'storage/csv/ha_tina.cc'
--- a/storage/csv/ha_tina.cc	2010-07-29 12:32:11 +0000
+++ b/storage/csv/ha_tina.cc	2010-09-29 22:38:57 +0000
@@ -1638,6 +1638,16 @@ int ha_tina::delete_all_rows()
 }
 
 /*
+  Manual truncate table.
+*/
+
+int ha_tina::truncate()
+{
+  /* Simply delete all rows. No auto increment to reset. */
+  return delete_all_rows();
+}
+
+/*
   Called by the database to lock the table. Keep in mind that this
   is an internal lock.
 */

=== modified file 'storage/csv/ha_tina.h'
--- a/storage/csv/ha_tina.h	2010-07-08 21:20:08 +0000
+++ b/storage/csv/ha_tina.h	2010-09-29 22:38:57 +0000
@@ -152,6 +152,7 @@ public:
   int info(uint);
   int extra(enum ha_extra_function operation);
   int delete_all_rows(void);
+  int truncate();
   int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info);
   bool check_if_incompatible_data(HA_CREATE_INFO *info,
                                   uint table_changes);

=== modified file 'storage/example/ha_example.cc'
--- a/storage/example/ha_example.cc	2010-08-05 12:34:19 +0000
+++ b/storage/example/ha_example.cc	2010-09-29 22:38:57 +0000
@@ -739,6 +739,26 @@ int ha_example::delete_all_rows()
 
 /**
   @brief
+  Used for handler specific truncate table.  The table is locked in
+  exclusive mode and handler is responsible for reseting the auto-
+  increment counter.
+
+    @details
+  Called from Truncate_statement::handler_truncate.
+
+    @see
+  Truncate_statement in sql_truncate.cc
+  Remarks in handler::truncate.
+*/
+int ha_example::truncate()
+{
+  DBUG_ENTER("ha_example::truncate");
+  DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+}
+
+
+/**
+  @brief
   This create a lock on the table. If you are implementing a storage engine
   that can handle transacations look at ha_berkely.cc to see how you will
   want to go about doing this. Otherwise you should consider calling flock()

=== modified file 'storage/example/ha_example.h'
--- a/storage/example/ha_example.h	2010-03-31 14:05:33 +0000
+++ b/storage/example/ha_example.h	2010-09-29 22:38:57 +0000
@@ -245,6 +245,7 @@ public:
   int extra(enum ha_extra_function operation);
   int external_lock(THD *thd, int lock_type);                   ///< required
   int delete_all_rows(void);
+  int truncate();
   ha_rows records_in_range(uint inx, key_range *min_key,
                            key_range *max_key);
   int delete_table(const char *from);

=== modified file 'storage/federated/ha_federated.cc'
--- a/storage/federated/ha_federated.cc	2010-07-23 20:17:55 +0000
+++ b/storage/federated/ha_federated.cc	2010-09-29 22:38:57 +0000
@@ -3042,6 +3042,16 @@ int ha_federated::delete_all_rows()
 
 
 /*
+  Used to manually truncate the table via a delete of all rows in a table.
+*/
+
+int ha_federated::truncate()
+{
+  return delete_all_rows();
+}
+
+
+/*
   The idea with handler::store_lock() is the following:
 
   The statement decided which locks we should need for the table

=== modified file 'storage/federated/ha_federated.h'
--- a/storage/federated/ha_federated.h	2010-03-24 15:03:44 +0000
+++ b/storage/federated/ha_federated.h	2010-09-29 22:38:57 +0000
@@ -248,6 +248,7 @@ public:
   int optimize(THD* thd, HA_CHECK_OPT* check_opt);
 
   int delete_all_rows(void);
+  int truncate();
   int create(const char *name, TABLE *form,
              HA_CREATE_INFO *create_info);                      //required
   ha_rows records_in_range(uint inx, key_range *start_key,

=== modified file 'storage/heap/ha_heap.cc'
--- a/storage/heap/ha_heap.cc	2010-07-23 20:17:55 +0000
+++ b/storage/heap/ha_heap.cc	2010-09-29 22:38:57 +0000
@@ -455,6 +455,13 @@ int ha_heap::delete_all_rows()
 }
 
 
+int ha_heap::truncate()
+{
+  int error= delete_all_rows();
+  return error ? error : reset_auto_increment(0);
+}
+
+
 int ha_heap::reset_auto_increment(ulonglong value)
 {
   file->s->auto_increment= value;

=== modified file 'storage/heap/ha_heap.h'
--- a/storage/heap/ha_heap.h	2010-03-31 14:05:33 +0000
+++ b/storage/heap/ha_heap.h	2010-09-29 22:38:57 +0000
@@ -99,6 +99,7 @@ public:
   int reset();
   int external_lock(THD *thd, int lock_type);
   int delete_all_rows(void);
+  int truncate();
   int reset_auto_increment(ulonglong value);
   int disable_indexes(uint mode);
   int enable_indexes(uint mode);

=== modified file 'storage/ibmdb2i/ha_ibmdb2i.cc'
--- a/storage/ibmdb2i/ha_ibmdb2i.cc	2010-07-08 21:20:08 +0000
+++ b/storage/ibmdb2i/ha_ibmdb2i.cc	2010-09-29 22:38:57 +0000
@@ -1806,6 +1806,13 @@ int ha_ibmdb2i::delete_all_rows()
 }
 
 
+int ha_ibmdb2i::truncate()
+{
+  int error = delete_all_rows();
+  return error ? error : reset_auto_increment(0);
+}
+
+
 int ha_ibmdb2i::external_lock(THD *thd, int lock_type)
 {
   int rc = 0;

=== modified file 'storage/innobase/handler/ha_innodb.cc'
--- a/storage/innobase/handler/ha_innodb.cc	2010-08-18 07:14:06 +0000
+++ b/storage/innobase/handler/ha_innodb.cc	2010-09-29 22:38:57 +0000
@@ -7072,33 +7072,21 @@ Deletes all rows of an InnoDB table.
 @return	error number */
 UNIV_INTERN
 int
-ha_innobase::delete_all_rows(void)
+ha_innobase::truncate(void)
 /*==============================*/
 {
 	int		error;
 
-	DBUG_ENTER("ha_innobase::delete_all_rows");
+	DBUG_ENTER("ha_innobase::truncate");
 
 	/* Get the transaction associated with the current thd, or create one
 	if not yet created, and update prebuilt->trx */
 
 	update_thd(ha_thd());
 
-	if (thd_sql_command(user_thd) != SQLCOM_TRUNCATE) {
-	fallback:
-		/* We only handle TRUNCATE TABLE t as a special case.
-		DELETE FROM t will have to use ha_innobase::delete_row(),
-		because DELETE is transactional while TRUNCATE is not. */
-		DBUG_RETURN(my_errno=HA_ERR_WRONG_COMMAND);
-	}
-
 	/* Truncate the table in InnoDB */
 
 	error = row_truncate_table_for_mysql(prebuilt->table, prebuilt->trx);
-	if (error == DB_ERROR) {
-		/* Cannot truncate; resort to ha_innobase::delete_row() */
-		goto fallback;
-	}
 
 	error = convert_error_code_to_mysql(error, prebuilt->table->flags,
 					    NULL);

=== modified file 'storage/innobase/handler/ha_innodb.h'
--- a/storage/innobase/handler/ha_innodb.h	2010-06-22 15:58:28 +0000
+++ b/storage/innobase/handler/ha_innodb.h	2010-09-29 22:38:57 +0000
@@ -178,7 +178,7 @@ class ha_innobase: public handler
 	void update_create_info(HA_CREATE_INFO* create_info);
 	int create(const char *name, register TABLE *form,
 					HA_CREATE_INFO *create_info);
-	int delete_all_rows();
+	int truncate();
 	int delete_table(const char *name);
 	int rename_table(const char* from, const char* to);
 	int check(THD* thd, HA_CHECK_OPT* check_opt);

=== modified file 'storage/innobase/row/row0mysql.c'
--- a/storage/innobase/row/row0mysql.c	2010-07-21 14:22:29 +0000
+++ b/storage/innobase/row/row0mysql.c	2010-09-29 22:38:57 +0000
@@ -2966,8 +2966,7 @@ next_rec:
 		dict_table_change_id_in_cache(table, new_id);
 	}
 
-	/* MySQL calls ha_innobase::reset_auto_increment() which does
-	the same thing. */
+	/* Reset auto-increment. */
 	dict_table_autoinc_lock(table);
 	dict_table_autoinc_initialize(table, 1);
 	dict_table_autoinc_unlock(table);

=== modified file 'storage/myisam/ha_myisam.cc'
--- a/storage/myisam/ha_myisam.cc	2010-07-08 21:20:08 +0000
+++ b/storage/myisam/ha_myisam.cc	2010-09-29 22:38:57 +0000
@@ -1788,6 +1788,12 @@ int ha_myisam::delete_all_rows()
   return mi_delete_all_rows(file);
 }
 
+int ha_myisam::truncate()
+{
+  int error= delete_all_rows();
+  return error ? error : reset_auto_increment(0);
+}
+
 int ha_myisam::reset_auto_increment(ulonglong value)
 {
   file->s->state.auto_increment= value;

=== modified file 'storage/myisam/ha_myisam.h'
--- a/storage/myisam/ha_myisam.h	2010-04-20 08:51:50 +0000
+++ b/storage/myisam/ha_myisam.h	2010-09-29 22:38:57 +0000
@@ -107,6 +107,7 @@ class ha_myisam: public handler
   int reset(void);
   int external_lock(THD *thd, int lock_type);
   int delete_all_rows(void);
+  int truncate();
   int reset_auto_increment(ulonglong value);
   int disable_indexes(uint mode);
   int enable_indexes(uint mode);

=== modified file 'storage/myisammrg/ha_myisammrg.cc'
--- a/storage/myisammrg/ha_myisammrg.cc	2010-09-08 08:25:37 +0000
+++ b/storage/myisammrg/ha_myisammrg.cc	2010-09-29 22:38:57 +0000
@@ -1203,6 +1203,22 @@ ha_rows ha_myisammrg::records_in_range(u
 }
 
 
+int ha_myisammrg::truncate()
+{
+  int err= 0;
+  MYRG_TABLE *table;
+  DBUG_ENTER("ha_myisammrg::truncate");
+
+  for (table= file->open_tables; table != file->end_table; table++)
+  {
+    if ((err= mi_delete_all_rows(table->table)))
+      break;
+  }
+
+  DBUG_RETURN(err);
+}
+
+
 int ha_myisammrg::info(uint flag)
 {
   MYMERGE_INFO mrg_info;

=== modified file 'storage/myisammrg/ha_myisammrg.h'
--- a/storage/myisammrg/ha_myisammrg.h	2009-12-08 13:57:25 +0000
+++ b/storage/myisammrg/ha_myisammrg.h	2010-09-29 22:38:57 +0000
@@ -131,6 +131,7 @@ public:
   int rnd_pos(uchar * buf, uchar *pos);
   void position(const uchar *record);
   ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key);
+  int truncate();
   int info(uint);
   int reset(void);
   int extra(enum ha_extra_function operation);

=== modified file 'storage/perfschema/ha_perfschema.cc'
--- a/storage/perfschema/ha_perfschema.cc	2010-09-10 09:10:38 +0000
+++ b/storage/perfschema/ha_perfschema.cc	2010-09-29 22:38:57 +0000
@@ -345,6 +345,11 @@ int ha_perfschema::delete_all_rows(void)
   DBUG_RETURN(result);
 }
 
+int ha_perfschema::truncate()
+{
+  return delete_all_rows();
+}
+
 THR_LOCK_DATA **ha_perfschema::store_lock(THD *thd,
                                        THR_LOCK_DATA **to,
                                        enum thr_lock_type lock_type)

=== modified file 'storage/perfschema/ha_perfschema.h'
--- a/storage/perfschema/ha_perfschema.h	2010-07-15 23:44:45 +0000
+++ b/storage/perfschema/ha_perfschema.h	2010-09-29 22:38:57 +0000
@@ -127,6 +127,8 @@ public:
 
   int delete_all_rows(void);
 
+  int truncate();
+
   int delete_table(const char *from);
 
   int rename_table(const char * from, const char * to);

Attachment: [text/bzr-bundle] bzr/davi.arnaut@oracle.com-20100929223857-wp5lrqudo8axrhvs.bundle
Thread
bzr commit into mysql-5.5-runtime branch (davi:3149) Bug#49938 Bug#54678Davi Arnaut30 Sep
Re: bzr commit into mysql-5.5-runtime branch (davi:3149) Bug#49938Bug#54678Dmitry Lenev4 Oct
  • Re: bzr commit into mysql-5.5-runtime branch (davi:3149) Bug#49938Bug#54678Davi Arnaut4 Oct
    • Re: bzr commit into mysql-5.5-runtime branch (davi:3149) Bug#49938Bug#54678Dmitry Lenev5 Oct