<!-- Source: @site/projects/sqlens-for-laravel/docs/rules/deploy-preflight-metadata-lock-blocker.md -->
<!-- Canonical: https://docs.pushery.com/sqlens-for-laravel/rules/deploy-preflight-metadata-lock-blocker/ -->

# DEPLOY.PREFLIGHT.METADATA_LOCK_BLOCKER — a session is sitting on a table this migration is about to change

> What is holding a metadata lock on the tables the pending migration will alter, and why an empty reading is only a pass when nothing went unread.

- **Category:** safety
- **Severity:** high
- **Level:** 0
- **Downtime class:** `blocking` — the finding this check emits carries it. A metadata lock a
  DDL will queue behind is precisely a fact about concurrent traffic, which is what this axis
  measures
- **Stability:** stable
- **Suites:** deploy
- **Applies to:** MySQL — the check answers `appliesTo()` for the `mysql` driver and for nothing else

## What it reports

A session that has been running longer than this run's threshold **on one of the tables the pending
migration is about to change**.

The check builds that table list itself, from the pending statements the deploy already
canonicalized. A statement is considered only if its kind is one that takes an exclusive metadata
lock at its boundaries — `AlterTable`, `DropTable`, `TruncateTable`, `AddColumn`, `AlterColumn`,
`DropColumn`, `AddConstraint`, `AddPrimaryKey`, `AddForeignKey`, `DropConstraint`, `CreateIndex`,
`DropIndex`, `Rename`, and `RebuildTable`, which is `OPTIMIZE TABLE`. Of that statement's targets it keeps the ones that are **tables** and that the
statement **acts on**, not the ones it merely points at.

Those table names then go into the activity reading as its focus, together with the run's
`longRunningMs` threshold — 5,000 ms unless the project configures another. Handing the targets in is
what makes the match possible at all: a long-running session carries a `relation` only for the tables
the caller named, so a reading asked without a focus comes back with every relation null and nothing
can ever match, however blocked the instance is.

On MySQL that reading comes from the server's own state views. The long transactions come from
`information_schema.INNODB_TRX`; which of the focus tables each session currently **holds** a granted
metadata lock on comes from `performance_schema.metadata_locks`, joined to `performance_schema.threads`
so that the lock's `OWNER_THREAD_ID` lines up with the connection id `INNODB_TRX` speaks.

One finding is emitted per matching session, and it says which thread, in which state, on which
table, and for how long:

```text
Thread <session> has been <state> on `<table>` for <seconds> s, and this deploy is about to change
that table. […] Nothing here says that thread is wrong to be running; it says the migration is about
to collide with it.
```

## Why it matters

InnoDB's online DDL is **online in the middle and not at the ends**. It needs a brief EXCLUSIVE
metadata lock to start and another one to finish, and a transaction open on the target table blocks
exactly those two moments — whatever the algorithm, because the two ends are exclusive in all of
them.

While the `ALTER` waits for its metadata lock, every new statement touching that table queues behind
it. The table stops answering, and the `ALTER` itself is doing nothing wrong. MySQL grants metadata
locks in order, so one long transaction blocks the migration *and* every reader queued behind it:
the finding is about the queue, not only about the one session it names.

That is why the finding carries the `blocking` downtime class — what it describes is a lock that
blocks concurrent access for its duration. The check as a catalog entry carries none, for the reason
in the metadata above: a deploy check reports on the state of a target rather than on a DDL operation
of its own.

## What to do about it

The finding does not tell you to kill anything. It hands you the three facts the decision needs, and
the decision is *will this session still be there when the migration starts*:

- **Which session.** The label is the connection id, because that is the number `SHOW PROCESSLIST`
  and `KILL` accept — `performance_schema` identifies a session by `THREAD_ID`, which is a different
  numbering space and correlates with nothing else in the same snapshot. This finding always names a
  connection: the long runners come from `INNODB_TRX`, which speaks connection ids, and the
  held-lock reading drops every row whose `PROCESSLIST_ID` is null. The reader's *thread* spelling —
  *held by something you cannot `KILL`*, which it uses rather than fabricating an id — therefore
  belongs to its lock-wait rows, not to this check.
- **What it is doing.** The state comes through in the server's own vocabulary, and it is what
  separates the two very different cases: a long active query is somebody's slow report, while a long
  idle transaction is a connection somebody forgot to close.
- **How long it has been running.** Against the threshold this run was configured with.

Then act on that: let the session finish, end it, or move the deploy — and **re-run the preflight**,
because the reading is a snapshot and the answer is only about the moment it looked.

If the result is `undetermined` rather than `fail`, the reason names what to fix and the result
blocks exactly as a failure does — anything that is not a pass blocks. `sqlens:predeploy` is
fail-closed, and `--allow-undetermined` is the one door out of that: it proceeds when *every*
blocker is a check that could not answer, and never when one of them found something. Three shapes
reach that state:

- **no activity reader at all** for this run, so what is holding a metadata lock is unknown — *that
  is not the same as nothing holding one*
- **the activity views could not be read**, with the server's own refusal carried through verbatim
  rather than paraphrased, so it points at the right grant
- **the reading came back empty AND incomplete**, with every gap named — the instrument, and why it
  was not readable

Enabling `performance_schema` needs a server restart, not a grant. The metadata-lock instrument does
not: `UPDATE performance_schema.setup_instruments SET ENABLED = 'YES' WHERE NAME =
'wait/lock/metadata/sql/mdl'` switches it on at run time. Which privileges each state view
wants, and what each one does without them, is on
[the deploy readers page](https://docs.pushery.com/sqlens-for-laravel/deploy-readers.md) and in
[what `sqlens:predeploy` needs](https://docs.pushery.com/sqlens-for-laravel/predeploy-permissions.md).

## Why an empty reading is not automatically a pass

This is the whole check, and the reason it exists separately from its PostgreSQL counterpart.

`performance_schema` can be off, and the instrument that fills `metadata_locks`,
`wait/lock/metadata/sql/mdl`, can be switched off on its own at run time. MySQL 8.4 ships both on.
Neither produces an error when off: the views are present, they answer, and they answer with an
empty set. So *no blocker* and *the instrument that would have told me is switched off* arrive as the
very same answer.

The check therefore asks whether the reading is safe to read silence from before it reports anything
clean. An empty reading with a named gap is `undetermined`; an empty reading with no gaps is a pass.
Reporting a pass off the empty list alone would be the single failure this package exists to prevent.

**What produces that named gap is worth knowing exactly.** The activity reading asks the server
both switches in one query, `@@performance_schema` and the instrument's row in
`performance_schema.setup_instruments`, and names a gap when either is off, or when the server does
not list the instrument at all. That is the shape this check turns into `undetermined`.

## What it does not claim

- **It is a snapshot**, like its PostgreSQL counterpart: the blocking session may be gone by the time
  the migration runs.
- **It sees the tables the statements ACT ON.** The referenced side of a foreign key is deliberately
  excluded: MySQL takes a *shared* metadata lock there, which is not what this finding claims, and
  the finding says this deploy is about to CHANGE the table it names. The cost is named rather than
  hidden — a long transaction on the referenced table can still delay the statement, and this check
  does not look for that.
- **A constraint is not a relation the activity views can report**, so a constraint name never enters
  the reading.
- **A statement that takes no exclusive metadata lock is not considered.** Plain DML is not in the
  list of kinds above, so a backfill never makes a table look like a blocked `ALTER`.
- **Nothing is reported when nothing is pending.** With no pending migration the check passes without
  reading anything, because there is no table for a session to collide with.
- **One session holding two of the deploy's tables produces one finding naming one of them.** The
  deploy is blocked either way.
- **It says nothing about a session on a table this migration does not touch.** A long-running
  session is reported only when its relation is one of the tables the pending statements will change.

## Related

- [Understanding `undetermined`](https://docs.pushery.com/sqlens-for-laravel/understanding-undetermined.md) — why a check that could not run is
  reported rather than swallowed
- [The deploy readers](https://docs.pushery.com/sqlens-for-laravel/deploy-readers.md) — what the activity reader needs to see, and what a
  partial answer means
- [What `sqlens:predeploy` needs](https://docs.pushery.com/sqlens-for-laravel/predeploy-permissions.md) — the read-only role, and which checks
  go undetermined without which privilege
