• Jon Olav Hauglid's avatar
    Bug #48940 MDL deadlocks against mysql_rm_db · 6331ef3e
    Jon Olav Hauglid authored
    This deadlock would occur between two connections A and B if statements
    where executed in the following way:
    1) Connection A executes a DML statement against table s1.t1 with
    autocommit off. This causes a shared metadata lock on s1.t1 to be 
    acquired. (With autocommit on, the metadata lock will be dropped once
    the statment completes and the deadlock will not occour.)
    2) Connection B tries to DROP DATABASE s1. This will block against the
    metadata lock connection A holds on s1.t1. While blocking, connection B
    will hold the LOCK_mysql_create_db mutex.
    3) Connection A tries to ALTER DATABASE s1. This will block when trying
    to get LOCK_mysql_create_db mutex held by connection B.
    4) Deadlock between DROP DATABASE and ALTER DATABASE (which has autocommit
    off).
    
    If Connection A used an explicitly started transaction rather than having
    autocommit off, this deadlock did not happen as ALTER DATABASE is 
    disallowed inside transactions.
    
    This patch fixes the problem by changing ALTER DATABASE to cause an
    implicit commit before executing. This will cause the metadata 
    lock on s1.t1 to be dropped, allowing DROP DATABASE to proceed. 
    This will in turn cause the LOCK_mysql_create_db mutex to be unlocked, 
    allowing ALTER DATABASE to proceed.
    
    Note that SQL commands other than ALTER DATABASE that also use 
    LOCK_mysql_create_db, already cause an implicit commit. 
    
    Incompatible change: ALTER DATABASE (and its synonym ALTER SCHEMA)
    now cause an implicit commit. This must be reflected in the 
    documentation.
    
    Test case added to schema.test.
    
    
    sql/sql_parse.cc:
      Added CF_AUTO_COMMIT_TRANS to SQLCOM_ALTER_DB.
      
      Removed thd->active_transaction() checks from SQLCOM_DROP_DB, 
      SQLCOM_ALTER_DB_UPGRADE and SQLCOM_ALTER_DB as these statements
      cause an implicit commit.
    6331ef3e
sql_parse.cc 230 KB