-
Notifications
You must be signed in to change notification settings - Fork 892
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
Merged
Emyrk
merged 6 commits into
stevenmasley/postgres_tx_serial
from
stevenmasley/postgres_tx_serial_pg_locks
Nov 1, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/jmoiron/sqlx" | ||
|
||
"github.com/coder/coder/v2/coderd/util/slice" | ||
) | ||
|
||
// PGLock docs see: https://www.postgresql.org/docs/current/view-pg-locks.html#VIEW-PG-LOCKS | ||
type PGLock struct { | ||
// LockType see: https://www.postgresql.org/docs/current/monitoring-stats.html#WAIT-EVENT-LOCK-TABLE | ||
LockType *string `db:"locktype"` | ||
Database *string `db:"database"` // oid | ||
Relation *string `db:"relation"` // oid | ||
RelationName *string `db:"relation_name"` | ||
Page *int `db:"page"` | ||
Tuple *int `db:"tuple"` | ||
VirtualXID *string `db:"virtualxid"` | ||
TransactionID *string `db:"transactionid"` // xid | ||
ClassID *string `db:"classid"` // oid | ||
ObjID *string `db:"objid"` // oid | ||
ObjSubID *int `db:"objsubid"` | ||
VirtualTransaction *string `db:"virtualtransaction"` | ||
PID int `db:"pid"` | ||
Mode *string `db:"mode"` | ||
Granted bool `db:"granted"` | ||
FastPath *bool `db:"fastpath"` | ||
WaitStart *time.Time `db:"waitstart"` | ||
} | ||
|
||
func (l PGLock) Equal(b PGLock) bool { | ||
// Lazy, but hope this works | ||
return reflect.DeepEqual(l, b) | ||
} | ||
|
||
func (l PGLock) String() string { | ||
granted := "granted" | ||
if !l.Granted { | ||
granted = "waiting" | ||
} | ||
var details string | ||
switch safeString(l.LockType) { | ||
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" | ||
default: | ||
details = "???" | ||
} | ||
return fmt.Sprintf("%d-%5s [%s] %s/%s/%s: %s", | ||
l.PID, | ||
safeString(l.TransactionID), | ||
granted, | ||
safeString(l.RelationName), | ||
safeString(l.LockType), | ||
safeString(l.Mode), | ||
details, | ||
) | ||
} | ||
|
||
// PGLocks returns a list of all locks in the database currently in use. | ||
func (q *sqlQuerier) PGLocks(ctx context.Context) (PGLocks, error) { | ||
rows, err := q.sdb.QueryContext(ctx, ` | ||
SELECT | ||
relation::regclass AS relation_name, | ||
* | ||
FROM pg_locks; | ||
`) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer rows.Close() | ||
|
||
var locks []PGLock | ||
err = sqlx.StructScan(rows, &locks) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return locks, err | ||
} | ||
|
||
type PGLocks []PGLock | ||
|
||
func (l PGLocks) String() string { | ||
// Try to group things together by relation name. | ||
sort.Slice(l, func(i, j int) bool { | ||
return safeString(l[i].RelationName) < safeString(l[j].RelationName) | ||
}) | ||
|
||
var out strings.Builder | ||
for i, lock := range l { | ||
if i != 0 { | ||
_, _ = out.WriteString("\n") | ||
} | ||
_, _ = out.WriteString(lock.String()) | ||
} | ||
return out.String() | ||
} | ||
|
||
// Difference returns the difference between two sets of locks. | ||
// This is helpful to determine what changed between the two sets. | ||
func (l PGLocks) Difference(to PGLocks) (new PGLocks, removed PGLocks) { | ||
return slice.SymmetricDifferenceFunc(l, to, func(a, b PGLock) bool { | ||
return a.Equal(b) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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