mariadb/sql/sql_truncate.h
Sujatha Sivakumar 1b74f2e3da Bug#17942050:KILL OF TRUNCATE TABLE WILL LEAD TO BINARY LOG
WRITTEN WHILE ROWS REMAINS

Problem:
========
When truncate table fails while using transactional based
engines even though the operation errors out we still
continue and log it to binlog. Because of this master has
data but the truncate will be written to binary log which
will cause inconsistency.

Analysis:
========
Truncate table can happen either through drop and create of
table or by deleting rows. In the second case the existing
code is written in such a way that even if an error occurs
the truncate statement will always be binlogged. Which is not
correct.

Binlogging of TRUNCATE TABLE statement should check whether
truncate is executed "transactionally or not". If the table
is transaction based we log the TRUNCATE TABLE only on
successful completion.

If table is non transactional there are possibilities that on
error we could have partial changes done hence in such cases
we do log in spite of errors as some of the lines might have
been removed, so the statement has to be sent to slave.

Fix:
===
Using table handler whether truncate table is being executed
in transaction based mode or not is identified and statement
is binlogged accordingly.

mysql-test/suite/binlog/r/binlog_truncate_kill.result:
  Added test case to test the fix for Bug#17942050.
mysql-test/suite/binlog/t/binlog_truncate_kill.test:
  Added test case to test the fix for Bug#17942050.
sql/sql_truncate.cc:
  Check if truncation is successful or not and retun appropriate
  return values so that binlogging can be done based on that.
sql/sql_truncate.h:
  Added a new enum.
2014-04-15 15:17:25 +05:30

70 lines
2 KiB
C++

#ifndef SQL_TRUNCATE_INCLUDED
#define SQL_TRUNCATE_INCLUDED
/* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
class THD;
struct TABLE_LIST;
/**
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)
{}
virtual ~Truncate_statement()
{}
/**
Execute a TRUNCATE statement at runtime.
@param thd the current thread.
@return false on success.
*/
bool execute(THD *thd);
protected:
enum truncate_result{
TRUNCATE_OK=0,
TRUNCATE_FAILED_BUT_BINLOG,
TRUNCATE_FAILED_SKIP_BINLOG
};
/** Handle locking a base table for truncate. */
bool lock_table(THD *, TABLE_LIST *, bool *);
/** Truncate table via the handler method. */
enum truncate_result handler_truncate(THD *, TABLE_LIST *, bool);
/**
Optimized delete of all rows by doing a full regenerate 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