• Dmitry Lenev's avatar
    Fix for bug #12641342 - "61401: UPDATE PERFORMANCE DEGRADES · db114007
    Dmitry Lenev authored
    GRADUALLY IF A TRIGGER EXISTS".
    
    This bug manifested itself in two ways:
    
    - Firstly execution of any data-changing statement which
      required prelocking (i.e. involved stored function or
      trigger) as part of transaction slowed down a bit all
      subsequent statements in this transaction. So performance
      in transaction which periodically involved such statements
      gradually degraded over time.
    - Secondly execution of any data-changing statement which
      required prelocking as part of transaction prevented
      concurrent FLUSH TABLES WITH READ LOCK from proceeding
      until the end of transaction instead of end of particular
      statement.
      
    The problem was caused by incorrect handling of metadata lock
    used in FTWRL implementation for statements requiring prelocked 
    mode. 
    Each statement which changes data acquires global IX lock
    with STATEMENT duration. This lock is supposed to block 
    concurrent FTWRL from proceeding until the statement ends.
    
    When entering prelocked mode, durations of all metadata locks
    acquired so far were changed to EXPLICIT, to prevent 
    substatements from releasing these locks. When prelocked mode
    was left, durations of metadata locks were changed to
    TRANSACTIONAL (with a few exceptions) so they can be properly
    released at the end of transaction. 
    Unfortunately, this meant that the global IX lock blocking
    FTWRL with STATEMENT duration was moved to TRANSACTIONAL
    duration after execution of statement requiring prelocking.
    
    Since each subsequent statement that required prelocking and
    tried to acquire global IX lock with STATEMENT duration got
    a new instance of MDL_ticket, which was later moved to
    TRANSACTIONAL duration, this led to unwarranted growth of
    number of tickets with TRANSACITONAL duration in this
    connection's MDL_context. As result searching for other
    tickets in it became slow and acquisition of other metadata
    locks by this transaction started to hog CPU.
    
    Moreover, this also meant that after execution of statement
    requiring prelocking concurrent FTWRL was blocked
    until the end of transaction instead of end of statement.
    
    This patch solves this problem by not moving locks to EXPLICIT
    duration when thread enters prelocked mode (unless it is a real 
    LOCK TABLES mode). This step turned out to be not really 
    necessary as substatements don't try to release metadata locks.
    Consequently, the global IX lock blocking FTWRL keeps its
    STATEMENT duration and is properly released at the end of
    statement and the above issue goes away.
    db114007
mdl.h 27.8 KB