Closed kaaveland closed 6 months ago
add_authors.sql
This is a human readable lock tracing and migration report generated by eugene to assist you in writing safer database migration scripts.
Here are some tips for reading it:
There is a summary section for the entire script at the start of the report and then a section for each statement in the script, that goes over the state of the database at the time the script was executed, as well as effects or hints specific to that particular statement
Started at | Total duration (ms) | Number of dangerous locks |
---|---|---|
2024-05-06T18:35:53.405235506+00:00 | 4 | 2 ❌ |
Schema | Object | Mode | Relkind | OID | Safe | Duration held (ms) |
---|---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ | 1 |
public |
books |
AccessShareLock |
Table | 16416 | ✅ | 1 |
public |
books |
ShareRowExclusiveLock |
Table | 16416 | ❌ | 1 |
public |
books_pkey |
AccessShareLock |
Index | 16422 | ✅ | 1 |
AccessExclusiveLock
would block the following operations on public.books
:
SELECT
FOR UPDATE
FOR NO KEY UPDATE
FOR SHARE
FOR KEY SHARE
UPDATE
DELETE
INSERT
MERGE
ShareRowExclusiveLock
would block the following operations on public.books
:
UPDATE
DELETE
INSERT
MERGE
create table authors(id serial primary key, name text not null);
No locks held at the start of this statement.
No new locks taken by this statement.
alter table books alter column title set not null;
No locks held at the start of this statement.
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
NOT NULL
columnID: make_column_not_nullable_with_lock
A column was changed from NULL
to NOT NULL
. This blocks all table access until all rows are validated. A safer way is: Add a CHECK
constraint as NOT VALID
, validate it later, then make the column NOT NULL
.
The column title
in the table public.books
was changed to NOT NULL
. If there is a CHECK (title IS NOT NULL)
constraint on public.books
, this is safe. Splitting this kind of change into 3 steps can make it safe:
CHECK (title IS NOT NULL) NOT VALID;
constraint on public.books
.ALTER TABLE public.books VALIDATE CONSTRAINT ...
.NOT NULL
ID: dangerous_lock_without_timeout
A lock that would block many common operations was taken without a timeout. This can block all other operations on the table indefinitely if any other transaction holds a conflicting lock while idle in transaction
or active
. A safer way is: Run SET lock_timeout = '2s';
before the statement and retry the migration if necessary.
The statement took AccessExclusiveLock
on the Table public.books
without a timeout. It blocks SELECT
, FOR UPDATE
, FOR NO KEY UPDATE
, FOR SHARE
, FOR KEY SHARE
, UPDATE
, DELETE
, INSERT
, MERGE
while waiting to acquire the lock.
alter table books add column author_id integer not null;
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
No new locks taken by this statement.
AccessExclusiveLock
ID: holding_access_exclusive
A transaction that holds an AccessExclusiveLock
started a new statement. This blocks all access to the table for the duration of this statement. A safer way is: Run this statement in a new transaction.
The statement is running while holding an AccessExclusiveLock
on the Table public.books
, blocking all other transactions from accessing it.
alter table books add foreign key (author_id) references authors(id);
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessShareLock |
Table | 16416 | ✅ |
public |
books |
ShareRowExclusiveLock |
Table | 16416 | ❌ |
public |
books_pkey |
AccessShareLock |
Index | 16422 | ✅ |
ID: validate_constraint_with_lock
A new constraint was added and it is already VALID
. This blocks all table access until all rows are validated. A safer way is: Add the constraint as NOT VALID
and validate it with ALTER TABLE ... VALIDATE CONSTRAINT
later.
A new constraint books_author_id_fkey
of type FOREIGN KEY
was added to the table books
as VALID
. Constraints that are NOT VALID
can be made VALID
by ALTER TABLE public.books VALIDATE CONSTRAINT books_author_id_fkey
which takes a lesser lock.
AccessExclusiveLock
ID: holding_access_exclusive
A transaction that holds an AccessExclusiveLock
started a new statement. This blocks all access to the table for the duration of this statement. A safer way is: Run this statement in a new transaction.
The statement is running while holding an AccessExclusiveLock
on the Table public.books
, blocking all other transactions from accessing it.
ID: dangerous_lock_without_timeout
A lock that would block many common operations was taken without a timeout. This can block all other operations on the table indefinitely if any other transaction holds a conflicting lock while idle in transaction
or active
. A safer way is: Run SET lock_timeout = '2s';
before the statement and retry the migration if necessary.
The statement took ShareRowExclusiveLock
on the Table public.books
without a timeout. It blocks UPDATE
, DELETE
, INSERT
, MERGE
while waiting to acquire the lock.
select * from books;
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
public |
books |
AccessShareLock |
Table | 16416 | ✅ |
public |
books |
ShareRowExclusiveLock |
Table | 16416 | ❌ |
public |
books_pkey |
AccessShareLock |
Index | 16422 | ✅ |
No new locks taken by this statement.
AccessExclusiveLock
ID: holding_access_exclusive
A transaction that holds an AccessExclusiveLock
started a new statement. This blocks all access to the table for the duration of this statement. A safer way is: Run this statement in a new transaction.
The statement is running while holding an AccessExclusiveLock
on the Table public.books
, blocking all other transactions from accessing it.
add_authors.sql
This is a human readable lock tracing and migration report generated by eugene to assist you in writing safer database migration scripts.
Here are some tips for reading it:
There is a summary section for the entire script at the start of the report and then a section for each statement in the script, that goes over the state of the database at the time the script was executed, as well as effects or hints specific to that particular statement
Started at | Total duration (ms) | Number of dangerous locks |
---|---|---|
2024-05-06T18:39:31.713433668+00:00 | 5 | 2 ❌ |
Schema | Object | Mode | Relkind | OID | Safe | Duration held (ms) |
---|---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ | 1 |
public |
books |
AccessShareLock |
Table | 16416 | ✅ | 1 |
public |
books |
ShareRowExclusiveLock |
Table | 16416 | ❌ | 1 |
public |
books_pkey |
AccessShareLock |
Index | 16422 | ✅ | 1 |
AccessExclusiveLock
would block the following operations on public.books
:
SELECT
FOR UPDATE
FOR NO KEY UPDATE
FOR SHARE
FOR KEY SHARE
UPDATE
DELETE
INSERT
MERGE
ShareRowExclusiveLock
would block the following operations on public.books
:
UPDATE
DELETE
INSERT
MERGE
create table authors(id serial primary key, name text not null);
No locks held at the start of this statement.
No new locks taken by this statement.
alter table books alter column title set not null;
No locks held at the start of this statement.
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
NOT NULL
columnID: make_column_not_nullable_with_lock
A column was changed from NULL
to NOT NULL
. This blocks all table access until all rows are validated. A safer way is: Add a CHECK
constraint as NOT VALID
, validate it later, then make the column NOT NULL
.
The column title
in the table public.books
was changed to NOT NULL
. If there is a CHECK (title IS NOT NULL)
constraint on public.books
, this is safe. Splitting this kind of change into 3 steps can make it safe:
CHECK (title IS NOT NULL) NOT VALID;
constraint on public.books
.ALTER TABLE public.books VALIDATE CONSTRAINT ...
.NOT NULL
ID: dangerous_lock_without_timeout
A lock that would block many common operations was taken without a timeout. This can block all other operations on the table indefinitely if any other transaction holds a conflicting lock while idle in transaction
or active
. A safer way is: Run SET lock_timeout = '2s';
before the statement and retry the migration if necessary.
The statement took AccessExclusiveLock
on the Table public.books
without a timeout. It blocks SELECT
, FOR UPDATE
, FOR NO KEY UPDATE
, FOR SHARE
, FOR KEY SHARE
, UPDATE
, DELETE
, INSERT
, MERGE
while waiting to acquire the lock.
alter table books add column author_id integer not null;
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
No new locks taken by this statement.
AccessExclusiveLock
ID: holding_access_exclusive
A transaction that holds an AccessExclusiveLock
started a new statement. This blocks all access to the table for the duration of this statement. A safer way is: Run this statement in a new transaction.
The statement is running while holding an AccessExclusiveLock
on the Table public.books
, blocking all other transactions from accessing it.
alter table books add foreign key (author_id) references authors(id);
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessShareLock |
Table | 16416 | ✅ |
public |
books |
ShareRowExclusiveLock |
Table | 16416 | ❌ |
public |
books_pkey |
AccessShareLock |
Index | 16422 | ✅ |
ID: validate_constraint_with_lock
A new constraint was added and it is already VALID
. This blocks all table access until all rows are validated. A safer way is: Add the constraint as NOT VALID
and validate it with ALTER TABLE ... VALIDATE CONSTRAINT
later.
A new constraint books_author_id_fkey
of type FOREIGN KEY
was added to the table books
as VALID
. Constraints that are NOT VALID
can be made VALID
by ALTER TABLE public.books VALIDATE CONSTRAINT books_author_id_fkey
which takes a lesser lock.
AccessExclusiveLock
ID: holding_access_exclusive
A transaction that holds an AccessExclusiveLock
started a new statement. This blocks all access to the table for the duration of this statement. A safer way is: Run this statement in a new transaction.
The statement is running while holding an AccessExclusiveLock
on the Table public.books
, blocking all other transactions from accessing it.
ID: dangerous_lock_without_timeout
A lock that would block many common operations was taken without a timeout. This can block all other operations on the table indefinitely if any other transaction holds a conflicting lock while idle in transaction
or active
. A safer way is: Run SET lock_timeout = '2s';
before the statement and retry the migration if necessary.
The statement took ShareRowExclusiveLock
on the Table public.books
without a timeout. It blocks UPDATE
, DELETE
, INSERT
, MERGE
while waiting to acquire the lock.
select * from books;
Schema | Object | Mode | Relkind | OID | Safe |
---|---|---|---|---|---|
public |
books |
AccessExclusiveLock |
Table | 16416 | ❌ |
public |
books |
AccessShareLock |
Table | 16416 | ✅ |
public |
books |
ShareRowExclusiveLock |
Table | 16416 | ❌ |
public |
books_pkey |
AccessShareLock |
Index | 16422 | ✅ |
No new locks taken by this statement.
AccessExclusiveLock
ID: holding_access_exclusive
A transaction that holds an AccessExclusiveLock
started a new statement. This blocks all access to the table for the duration of this statement. A safer way is: Run this statement in a new transaction.
The statement is running while holding an AccessExclusiveLock
on the Table public.books
, blocking all other transactions from accessing it.
Solves #19