• Sujatha's avatar
    MDEV-18930: Failed CREATE OR REPLACE TEMPORARY not written into binary log... · eef75404
    Sujatha authored
    MDEV-18930: Failed CREATE OR REPLACE TEMPORARY not written into binary log makes data on master and slave diverge
    
    Problem:
    =======
    Failed CREATE OR REPLACE TEMPORARY TABLE statement which dropped the table but
    failed at a later stage of creation of temporary table is not written to
    binarylog in row based replication. This causes the slave to diverge.
    
    Analysis:
    ========
    CREATE OR REPLACE statements work as shown below.
    
    CREATE OR REPLACE TABLE table_name (a int);
    is basically the same as:
    
    DROP TABLE IF EXISTS table_name;
    CREATE TABLE table_name (a int);
    
    Hence every CREATE OR REPLACE TABLE command which dropped the table should be
    written to binary log, even when following CREATE TABLE part fails. In order
    to achieve this, during the execution of CREATE OR REPLACE command, when a
    table is dropped 'thd->log_current_statement' flag is set. When table creation
    results in an error within 'mysql_create_table' code, the error handling part
    looks for this flag. If it is set the failed CREATE OR REPLACE statement is
    written into the binary log inspite of error. This ensure that slave doesn't
    diverge from the master. In case of row based replication the error handling
    code returns very early, if the table is of type temporary. This is done based
    on the assumption that temporary tables are not replicated in row based
    replication.
    
    It fails to handle the cases where a temporary table was created as part of
    statement based replication at an earlier stage and the binary log format was
    changed to row because of an unsafe statement. In this case when a CREATE OR
    REPLACE statement is executed on this temporary table it will dropped but the
    query will not be written to binary log. Hence slave diverges.
    
    Fix:
    ===
    In error handling code check the return status of create table operation. If
    it is successful and replication mode is row based and table is of type
    temporary then return. Other wise proceed further to the code which checks for
    thd->log_current_statement flag and does appropriate logging.
    eef75404
sql_table.cc 340 KB