Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdown lock trace report #15

Merged
merged 3 commits into from
May 4, 2024
Merged

Conversation

kaaveland
Copy link
Owner

@kaaveland kaaveland commented May 4, 2024

Add a first draft markdown report of a lock trace, something suitable to post as a comment on a merge request. This PR is big because it also deletes the machinery that was in place for supporting multiple formats and detail levels, in favor of doing something simpler -- just working with strings. Here's the example report:

Lock trace of mig.sql

In the below trace, a lock is considered dangerous if it conflicts with application code queries, such as SELECT or INSERT.

We should aim to hold dangerous locks for as little time as possible. If a dangerous lock is held while doing an operation that does not require it, we should split the migration into two steps.

For example, it often will make sense to add a new column in one migration, then backfill it in a separate one, since adding a column requires an AccessExclusiveLock while backfilling can do with a RowExclusiveLock which is much less likely to block concurrent transactions.

Overall Summary

Started at Total duration (ms) Number of dangerous locks
2024-05-05T00:45:47.027917+02:00 1037 1 ❌

All locks acquired

Schema Object Mode Relkind OID Safe Duration held (ms)
public books AccessExclusiveLock Table 16386 1011

Dangerous locks explanation

  • 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

Statement number 1 for 26 ms

SQL

create table author(name text not null);

Locks at start

No locks held at the start of this statement.

New locks taken

No new locks taken by this statement.

Statement number 2 for 1 ms

SQL

alter table books alter column title set not null;

Locks at start

No locks held at the start of this statement.

New locks taken

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16386

Statement number 3 for 1007 ms

SQL

select pg_sleep(1);

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16386

New locks taken

No new locks taken by this statement.

Statement number 4 for 3 ms

SQL

select * from books;

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16386

New locks taken

No new locks taken by this statement.

Conceptually cool and fun to play with, but added lots of extra complexity for
not so much benefit. This is preparation to instead do plain text and markdown
output in a more sensible/simple way.
@kaaveland kaaveland force-pushed the add-markdown-lock-trace-report branch 3 times, most recently from 806caf9 to 1e1d7e1 Compare May 4, 2024 22:47
@kaaveland kaaveland force-pushed the add-markdown-lock-trace-report branch 5 times, most recently from cdf3aa0 to 9af65d9 Compare May 4, 2024 23:17
@kaaveland
Copy link
Owner Author

Eugene 🔒 trace of add_authors.sql

In the below trace, a lock is considered dangerous if it conflicts with application code queries, such as SELECT or INSERT.

We should aim to hold dangerous locks for as little time as possible. If a dangerous lock is held while doing an operation that does not require it, we should split the migration into two steps.

For example, it often will make sense to add a new column in one migration, then backfill it in a separate one, since adding a column requires an AccessExclusiveLock while backfilling can do with a RowExclusiveLock which is much less likely to block concurrent transactions.

Overall Summary

Started at Total duration (ms) Number of dangerous locks
2024-05-04T23:18:19.997774940+00:00 1002 1 ❌

All locks acquired

Schema Object Mode Relkind OID Safe Duration held (ms)
public books AccessExclusiveLock Table 16389 1001

Dangerous locks found

  • 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

Statement number 1 for 1 ms

SQL

create table authors(name text not null);

Locks at start

No locks held at the start of this statement.

New locks taken

No new locks taken by this statement.

Statement number 2 for 0 ms

SQL

alter table books alter column title set not null;

Locks at start

No locks held at the start of this statement.

New locks taken

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

Statement number 3 for 1001 ms

SQL

select pg_sleep(1);

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

New locks taken

No new locks taken by this statement.

Statement number 4 for 0 ms

SQL

select * from books;

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

New locks taken

No new locks taken by this statement.

@kaaveland kaaveland force-pushed the add-markdown-lock-trace-report branch from 9af65d9 to 93e9b6f Compare May 4, 2024 23:19
@kaaveland
Copy link
Owner Author

Eugene 🔒 trace of add_authors.sql

In the below trace, a lock is considered dangerous if it conflicts with application code queries, such as SELECT or INSERT.

We should aim to hold dangerous locks for as little time as possible. If a dangerous lock is held while doing an operation that does not require it, we should split the migration into two steps.

For example, it often will make sense to add a new column in one migration, then backfill it in a separate one, since adding a column requires an AccessExclusiveLock while backfilling can do with a RowExclusiveLock which is much less likely to block concurrent transactions.

Overall Summary

Started at Total duration (ms) Number of dangerous locks
2024-05-04T23:20:37.765772580+00:00 1003 1 ❌

All locks acquired

Schema Object Mode Relkind OID Safe Duration held (ms)
public books AccessExclusiveLock Table 16389 1001
public books AccessShareLock Table 16389 0
public books_pkey AccessShareLock Index 16395 0

Dangerous locks found

  • 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

Statement number 1 for 2 ms

SQL

create table authors(name text not null);

Locks at start

No locks held at the start of this statement.

New locks taken

No new locks taken by this statement.

Statement number 2 for 0 ms

SQL

alter table books alter column title set not null;

Locks at start

No locks held at the start of this statement.

New locks taken

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

Statement number 3 for 1001 ms

SQL

select pg_sleep(1);

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

New locks taken

No new locks taken by this statement.

Statement number 4 for 0 ms

SQL

select * from books;

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

New locks taken

Schema Object Mode Relkind OID Safe
public books AccessShareLock Table 16389
public books_pkey AccessShareLock Index 16395

@kaaveland kaaveland force-pushed the add-markdown-lock-trace-report branch from 93e9b6f to df045fa Compare May 4, 2024 23:24
@kaaveland
Copy link
Owner Author

Eugene 🔒 trace of add_authors.sql

In the below trace, a lock is considered dangerous if it conflicts with application code queries, such as SELECT or INSERT.

We should aim to hold dangerous locks for as little time as possible. If a dangerous lock is held while doing an operation that does not require it, we should split the migration into two steps.

For example, it often will make sense to add a new column in one migration, then backfill it in a separate one, since adding a column requires an AccessExclusiveLock while backfilling can do with a RowExclusiveLock which is much less likely to block concurrent transactions.

Overall Summary

Started at Total duration (ms) Number of dangerous locks
2024-05-04T23:25:16.715110214+00:00 1002 1 ❌

All locks acquired

Schema Object Mode Relkind OID Safe Duration held (ms)
public books AccessExclusiveLock Table 16389 1001
public books_pkey AccessShareLock Index 16395 0
public books AccessShareLock Table 16389 0

Dangerous locks found

  • 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

Statement number 1 for 1 ms

SQL

create table authors(name text not null);

Locks at start

No locks held at the start of this statement.

New locks taken

No new locks taken by this statement.

Statement number 2 for 0 ms

SQL

alter table books alter column title set not null;

Locks at start

No locks held at the start of this statement.

New locks taken

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

Statement number 3 for 1001 ms

SQL

select pg_sleep(1);

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

New locks taken

No new locks taken by this statement.

Statement number 4 for 0 ms

SQL

select * from books;

Locks at start

Schema Object Mode Relkind OID Safe
public books AccessExclusiveLock Table 16389

New locks taken

Schema Object Mode Relkind OID Safe
public books_pkey AccessShareLock Index 16395
public books AccessShareLock Table 16389

@kaaveland kaaveland merged commit 5d19232 into main May 4, 2024
6 checks passed
@kaaveland kaaveland deleted the add-markdown-lock-trace-report branch May 4, 2024 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant