Skip to content

chore: add PGLocks query to analyze what locks are held in pg #15308

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

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Oct 31, 2024

Adds PGLocks query similar to how we have Ping(). I cannot do this via SQLc, as SQLc does not know about pg_locks table.

The string format is able to be changed to extract more value. I recognize this is not perfect, but I wrote it trying to learn more about these locks. It felt valuable enough to keep. Not all values are handled correctly here, I'll add functionality as required.

Some example output

  1. Open a TX in mode serializable and run GetQuotaConsumedForUser()
794-<nil> [granted] <nil>/virtualxid/ExclusiveLock: waiting to acquire virtual tx id lock
793-<nil> [granted] <nil>/virtualxid/ExclusiveLock: waiting to acquire virtual tx id lock
794-<nil> [granted] pg_locks/relation/AccessShareLock: 
793-<nil> [granted] workspace_builds/relation/AccessShareLock: 
793-<nil> [granted] workspace_builds/tuple/SIReadLock: page=0 tuple=1
793-<nil> [granted] workspace_builds_job_id_key/relation/AccessShareLock: 
793-<nil> [granted] workspace_builds_pkey/relation/AccessShareLock: 
793-<nil> [granted] workspace_builds_workspace_id_build_number_key/page/SIReadLock: page=1
793-<nil> [granted] workspace_builds_workspace_id_build_number_key/relation/AccessShareLock: 
793-<nil> [granted] workspaces/relation/AccessShareLock: 
793-<nil> [granted] workspaces/relation/SIReadLock: 
793-<nil> [granted] workspaces_owner_id_lower_idx/relation/AccessShareLock: 
793-<nil> [granted] workspaces_owner_id_lower_idx/page/SIReadLock: page=1
793-<nil> [granted] workspaces_pkey/relation/AccessShareLock: 
  1. Run UpdateWorkspaceBuildCostByID in the TX
868-<nil> [granted] <nil>/virtualxid/ExclusiveLock: waiting to acquire virtual tx id lock
867- 3689 [granted] <nil>/transactionid/ExclusiveLock: ???
867-<nil> [granted] <nil>/virtualxid/ExclusiveLock: waiting to acquire virtual tx id lock
868-<nil> [granted] pg_locks/relation/AccessShareLock: 
867-<nil> [granted] workspace_builds/relation/AccessShareLock: 
867-<nil> [granted] workspace_builds/relation/RowExclusiveLock: 
867-<nil> [granted] workspace_builds_job_id_key/relation/AccessShareLock: 
867-<nil> [granted] workspace_builds_job_id_key/relation/RowExclusiveLock: 
867-<nil> [granted] workspace_builds_pkey/relation/RowExclusiveLock: 
867-<nil> [granted] workspace_builds_pkey/relation/AccessShareLock: 
867-<nil> [granted] workspace_builds_pkey/page/SIReadLock: page=1
867-<nil> [granted] workspace_builds_workspace_id_build_number_key/relation/RowExclusiveLock: 
867-<nil> [granted] workspace_builds_workspace_id_build_number_key/relation/AccessShareLock: 
867-<nil> [granted] workspace_builds_workspace_id_build_number_key/page/SIReadLock: page=1
867-<nil> [granted] workspaces/relation/AccessShareLock: 
867-<nil> [granted] workspaces/relation/SIReadLock: 
867-<nil> [granted] workspaces_owner_id_lower_idx/relation/AccessShareLock: 
867-<nil> [granted] workspaces_owner_id_lower_idx/page/SIReadLock: page=1
867-<nil> [granted] workspaces_pkey/relation/AccessShareLock: 

Specifically these are the new locks aquired:

868-<nil> [granted] <nil>/virtualxid/ExclusiveLock: waiting to acquire virtual tx id lock
867- 3689 [granted] <nil>/transactionid/ExclusiveLock: ???
868-<nil> [granted] pg_locks/relation/AccessShareLock: 
867-<nil> [granted] workspace_builds/relation/RowExclusiveLock: 
867-<nil> [granted] workspace_builds_job_id_key/relation/RowExclusiveLock: 
867-<nil> [granted] workspace_builds_pkey/relation/RowExclusiveLock: 
867-<nil> [granted] workspace_builds_pkey/page/SIReadLock: page=1
867-<nil> [granted] workspace_builds_workspace_id_build_number_key/relation/RowExclusiveLock: 

Used to respond here: #15261 (comment)

@Emyrk Emyrk marked this pull request as ready for review October 31, 2024 15:04
Comment on lines +50 to +57
case "relation":
details = ""
case "page":
details = fmt.Sprintf("page=%d", *l.Page)
case "tuple":
details = fmt.Sprintf("page=%d tuple=%d", *l.Page, *l.Tuple)
case "virtualxid":
details = "waiting to acquire virtual tx id lock"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will implement this as needed. Not going to be exhaustive here right now

@Emyrk Emyrk requested a review from spikecurtis October 31, 2024 16:32
@Emyrk Emyrk merged commit 3a9270d into stevenmasley/postgres_tx_serial Nov 1, 2024
26 checks passed
@Emyrk Emyrk deleted the stevenmasley/postgres_tx_serial_pg_locks branch November 1, 2024 13:25
@github-actions github-actions bot locked and limited conversation to collaborators Nov 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants