When InnoDB tables are constantly updated during extraction, using FLUSH TABLES WITH READ LOCK is a well-known method for creating a consistent snapshot across multiple parallel sessions in MySQL and MariaDB.
Although this statement locks all tables across all databases with a global read lock, the lock is intended to be very short-lived (see details below) - lasting only until all sessions start their consistent transactions, rather than until the end of the extraction.
Note: You do not need to use this statement if you are creating a snapshot of InnoDB tables in a single session, as START TRANSACTION WITH CONSISTENT SNAPSHOT is sufficient.
Multiple parallel sessions help reduce transfer time, but you cannot start them at the exact same moment.
When the database is continuously updated, each session gets its own consistent view, but those views remain inconsistent across sessions:
The snapshot is inconsistent as it contains t1 and t2 before Transaction 1, and t3 and t4 after Transaction 1 is applied. Meanwhile, the snapshot excludes updates from Transaction 2 across all tables.
At the same time, the snapshot within each session is consistent: t1 is consistent with t2, and t3 is consistent with t4, no matter when their exports actually began.
FLUSH TABLES WITH READ LOCK prevents this inconsistency across sessions by blocking Transaction 1 until all sessions have started. Because no updates or commits can occur during this startup window, every session shares the exact same consistent view (the same binary log and GTID coordinates).
The workflow to make a parallel, consistent snapshot is as follows:
-- Coordinator session acquires a global read lock FLUSH TABLES WITH READ LOCK;
With all updates blocked, the parallel extract sessions are started (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;
Once all sessions have started, the coordinator session releases the global read lock:
UNLOCK TABLES;
Updates 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;
Consider two active transactions that have executed inserts but have not committed yet:
-- Transaction 1 START TRANSACTION; INSERT INTO t1 VALUES (1); -- Transaction 2 START TRANSACTION; INSERT INTO t1 VALUES (2);
Now, the coordinator session acquires a global lock:
FLUSH TABLES WITH READ LOCK; /* Query OK, 0 rows affected (0.010 sec) */
Note that the lock was acquired immediately without waiting or aborting the in-flight transactions, which are still running.
Now, let's execute one more insert in Transaction 1 and try to commit Transaction 2:
-- Transaction 1 INSERT INTO t1 VALUES (3); /* Waits */ -- Transaction 2 COMMIT; /* Waits */
Both statements are now blocked and waiting:
SHOW PROCESSLIST; /* | Query | Waiting for global read lock | INSERT INTO t1 VALUES (3) | | Query | Waiting for commit lock | COMMIT | */
They will wait until the coordinator executes UNLOCK TABLES to release the global read lock.
You can see that FLUSH TABLES WITH READ LOCK blocks writes and commit - meaning it directly impacts running applications.
Is it really as short-lived and safe to use as intended? Unfortunately, not always.
Let's extend our previous example by adding a third, long-running read transaction:
-- Transaction 1 START TRANSACTION; INSERT INTO t1 VALUES (1); -- Transaction 2 START TRANSACTION; INSERT INTO t1 VALUES (2); -- Transaction 3 START TRANSACTION; SELECT SLEEP(300) FROM t1 LIMIT 1;
Now, the coordinator cannot immediately acquire a global read lock and must wait:
FLUSH TABLES WITH READ LOCK; /* Waits */
What makes things much worse is that in-flight transactions also cannot continue writing to the database - only commits succeed:
-- Transaction 1 INSERT INTO t1 VALUES (3); /* Waits */ -- Transaction 2 COMMIT; /* Query OK, 0 rows affected (0.154 sec) */
Session status:
SHOW PROCESSLIST; /* | Query | Waiting for table flush | FLUSH TABLES WITH READ LOCK | | Query | Waiting for global read lock | INSERT INTO t1 VALUES (3) | | Query | User sleep | SELECT SLEEP(300) FROM t1 LIMIT 1 | */
As a result, all updates across all databases remain blocked until the long-running query finishes - which can take minutes or even hours, making this unacceptable for most production environments.
Therefore, you must monitor FLUSH TABLES WITH READ LOCK and kill either the lock statement or any blocking long-running queries if it takes too long to acquire.
FLUSH TABLES WITH READ LOCK requires the FLUSH_TABLES or RELOAD privilege, and it is not available on managed services like Amazon RDS.
As an alternative to FLUSH TABLES WITH READ LOCK, you can use the LOCK TABLES statement and explicitly list all the tables you want to export.