New 'RENAME TABLE ... NO LOCK CHECK' optional clause. #426
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR proposes an optional
NO LOCK CHECK
clause to theRENAME TABLE
statement, like so:rename table t1 to t0, t2 to t1, t0 to t2 no lock check;
This clause disables (skips) the existing pre-RENAME validation that all relevant tables are either all locked or none is locked.
A bit of context. From the docs:
So pre
8.0.13
any attempt toLOCK TABLES any_table_whatsoever WRITE; RENAME TABLE ...
would result with error.In
8.0.13
a check is added: either no table is locked, or all affected tables must be locked. This was developed to assist 3rd party online schema change tools, specificallygh-ost
(much appreciated!), to manage the final cut-over phase. However, as it turned out, this implementation created a different kind of complexity togh-ost
(for reasons we can elaborate on if so desired). Today, as I'm looking into bothgh-ost
andvitess
online schema changes, I'm again facing the cut-over issue.For even more context, the following should be considered:
8.0.13
locks were impossible.RENAME
operation grabs all necessary locks, whether locks were present beforehand or not.mysql-server/sql/sql_rename.cc
Lines 248 to 250 in a246bad
However, I think the risk of deadlock is very low, and easily mitigated by
SET LOCK_WAIT_TIMEOUT
to any reasonable value, which then works well to resolve a table-lock deadlock scenario.fb-osc
,gh-ost
andvitess
is toLOCK WRITE
the original table only, then keep digesting what remains of the changelog, applying it to the "shadow table", then flipping both.With this new optional clause, an OSC like
gh-ost
orvitess
would therefore run:#1
:LOCK TABLES original_table WRITE
#2
: apply any remainingINSERT/DELETE/UPDATE
on the shadow table#1
:RENAME TABLE original_table TO _old, _shadow_table TO original_table
orRENAME TABLE original_table TO _swap, _shadow_table TO original_table, _swap TO _shadow_table
The
RENAME
operation escalates connection#1
locks to hold bothoriginal_table
and_shadow_table
#1
:UNLOCK TABLES
That's a much shorter, less error prone, and overall less locking, flow than the one described in http://code.openark.org/blog/mysql/solving-the-non-atomic-table-swap-take-iii-making-it-atomic (see additional writeup here: vitessio/vitess#11460 (comment)). But the flow is currently impossible because it grabs a lock on a single table out of the two/more tables used in the
RENAME
statement, which fails theRENAME
.And so this contribution offers:
NO LOCK CHECK
RENAME
statementgh-ost
,vitess
andfb-osc
Thank you for your consideration!