multi-table-lock¶
Default severity: warn
What it does¶
Reports the first new statement of a transaction that locks a second table that
has rows against writes (SHARE or a stronger lock: most ALTER TABLE forms,
a foreign key on both tables, CREATE INDEX, REINDEX and
REFRESH MATERIALIZED VIEW without CONCURRENTLY, CLUSTER, VACUUM FULL,
TRUNCATE, DROP TABLE, DROP INDEX, LOCK TABLE), while an earlier statement of the transaction
still holds the first. One statement that locks several tables, which it needs
at once, is not reported. Once per transaction.
against.sql:
CREATE TABLE users (id BIGINT PRIMARY KEY);
CREATE TABLE orders (id BIGINT PRIMARY KEY);
``sql migration
BEGIN;
SET LOCAL lock_timeout = '2s';
ALTER TABLE users ADD COLUMN last_seen_at TIMESTAMPTZ;
-- error: [multi-table-lock] "locksorderswhile this transaction still holdsusers: queries of any of them now wait for all of them, untilCOMMIT`; change one table per transaction"
ALTER TABLE orders ADD COLUMN channel TEXT;
COMMIT;
## Why is this bad?
A transaction keeps every lock it takes until it ends. Each `ALTER TABLE`
waits for the queries of its table, and holds the locks it already has while
it waits: one slow query on any of the tables stalls the queries of all of
them, and the more tables, the likelier a deadlock with the application.
## How to fix
Change one table per transaction. Ignore the rule where the statements must
commit together, such as renames that swap two tables:
```sql migration
SET lock_timeout = '2s';
-- error:
ALTER TABLE users ADD COLUMN last_seen_at TIMESTAMPTZ;
ALTER TABLE orders ADD COLUMN channel TEXT;
Configuration¶
[tool.sqlty.rules]
multi-table-lock = "ignore"
Judged by sqlty breaking
in the postgres dialect, on the statements of the
schema files that the --against baseline does
not have. A table created earlier in the migrations is new and empty, so it is not
reported until rows are copied into it. -- sqlty: ignore[multi-table-lock] on the first line of a statement, or alone on its line above the statement, drops it.