MySQL - LOCK TABLES for Parallel Consistent Snapshots

If using FLUSH TABLES WITH READ LOCK is not an option, you can use LOCK TABLES to create a consistent snapshot for InnoDB tables across parallel sessions.

You do not need to hold the table locks for the entire duration of the extraction - only until all worker sessions start their transactions with a consistent snapshot.

Workflow

The workflow to make a parallel, consistent snapshot using LOCK TABLES is as follows:

  -- Coordinator session acquires a read lock for all tables in the snapshot
  LOCK TABLES t1 READ, t2 READ, t3 READ, t4 READ;

With all tables locked, the parallel extract sessions start and initialize their consistent transactions:

  -- Session 1
  SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  START TRANSACTION WITH CONSISTENT SNAPSHOT;
 
  -- Session 2
  SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  START TRANSACTION WITH CONSISTENT SNAPSHOT;

Note that unlike with FLUSH TABLES WITH READ LOCK, sessions may see different binary log positions, as the log can continue advancing due to operations or updates on tables in other databases not included in the extract.

Once all sessions have started, the coordinator session releases the table read locks:

  UNLOCK TABLES;

Updates to the previously locked tables resume while each session independently extracts its tables from the same consistent snapshot:

  -- Session 1
  SELECT * FROM t1;
  SELECT * FROM t2;
 
  -- Session 2
  SELECT * FROM t3;
  SELECT * FROM t4;

How LOCK TABLES Affects In-Flight Transactions

Consider two active transactions that have executed inserts into two different tables but have not committed yet:

  -- Transaction 1
  START TRANSACTION;
  INSERT INTO t1 VALUES (1);    -- Holds SHARED_WRITE lock on t1
 
  -- Transaction 2
  START TRANSACTION;
  INSERT INTO t2 VALUES (1);   -- Holds SHARED_WRITE lock on t2

Now, the coordinator session tries to acquire the table read locks:

  LOCK TABLES t1 READ, t2 READ;
  /* Waits */

LOCK TABLES tries to acquire SHARED_READ_ONLY locks on t1 and t2 but gets blocked (Waiting for table metadata lock) by the sessions holding SHARED_WRITE locks from their uncommitted inserts.

Let's add two more sessions inserting into the same tables:

  -- Transaction 3
  START TRANSACTION;
  INSERT INTO t1 VALUES (3);    -- Holds SHARED_WRITE lock on t1
  /* Query OK, 1 row affected (0.019 sec) */
 
  -- Transaction 4
  START TRANSACTION;
  INSERT INTO t2 VALUES (4);   -- Holds SHARED_WRITE lock on t2
  /* Query OK, 1 row affected (0.108 sec) */

Note that the pending LOCK TABLES statement does not block them. Since SHARED_WRITE takes priority over SHARED_READ_ONLY in MySQL's metadata lock queue, Transactions 3 and 4 bypass the waiting session and execute their INSERTs immediately.

Let's release the locks on t1 by committing Transactions 1 and 3:

  -- Transaction 1
  COMMIT;
 
  -- Transaction 3
  COMMIT;

Now, LOCK TABLES acquires the lock on t1 but still waits on t2:

1427 t1 SHARED_READ_ONLY GRANTED LOCK TABLES t1 READ, t2 READ
1427 t2 SHARED_READ_ONLY PENDING LOCK TABLES t1 READ, t2 READ

If a new transaction tries to insert into t1, it is now blocked, whereas a new transaction inserting into t2 can still succeed:

  -- Transaction 5
  START TRANSACTION;
  INSERT INTO t1 VALUES (5);    -- Lock on t1 already acquired by LOCK TABLES
  /* Waits */
 
  -- Transaction 6
  START TRANSACTION;
  INSERT INTO t2 VALUES (6);   -- Holds SHARED_WRITE lock on t2
  /* Query OK, 1 row affected (0.107 sec) */

If new write transactions (SHARED_WRITE) keep jumping ahead of LOCK TABLES, it can stay stuck in Waiting for table metadata lock indefinitely until there is a brief pause where zero write transactions are open on the tables it needs to lock.

Is LOCK TABLES Short-Lived?

As you can see, writes take priority over LOCK TABLES while it is waiting for a table lock, but once the lock is granted, all writes to that table are blocked.

If you are taking a snapshot of a database and need to lock many tables, LOCK TABLES can significantly degrade the performance of your database and application - even if you only intend to hold the locks briefly to synchronize your extract sessions.

Because LOCK TABLES acquires locks sequentially and lets incoming write transactions jump ahead on tables it has not locked yet, it prolongs the lock duration on the tables it has already locked - potentially stalling your database and application for an extended period.