|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/jmoiron/sqlx" |
| 12 | + |
| 13 | + "github.com/coder/coder/v2/coderd/util/slice" |
| 14 | +) |
| 15 | + |
| 16 | +// PGLock docs see: https://www.postgresql.org/docs/current/view-pg-locks.html#VIEW-PG-LOCKS |
| 17 | +type PGLock struct { |
| 18 | + // LockType see: https://www.postgresql.org/docs/current/monitoring-stats.html#WAIT-EVENT-LOCK-TABLE |
| 19 | + LockType *string `db:"locktype"` |
| 20 | + Database *string `db:"database"` // oid |
| 21 | + Relation *string `db:"relation"` // oid |
| 22 | + RelationName *string `db:"relation_name"` |
| 23 | + Page *int `db:"page"` |
| 24 | + Tuple *int `db:"tuple"` |
| 25 | + VirtualXID *string `db:"virtualxid"` |
| 26 | + TransactionID *string `db:"transactionid"` // xid |
| 27 | + ClassID *string `db:"classid"` // oid |
| 28 | + ObjID *string `db:"objid"` // oid |
| 29 | + ObjSubID *int `db:"objsubid"` |
| 30 | + VirtualTransaction *string `db:"virtualtransaction"` |
| 31 | + PID int `db:"pid"` |
| 32 | + Mode *string `db:"mode"` |
| 33 | + Granted bool `db:"granted"` |
| 34 | + FastPath *bool `db:"fastpath"` |
| 35 | + WaitStart *time.Time `db:"waitstart"` |
| 36 | +} |
| 37 | + |
| 38 | +func (l PGLock) Equal(b PGLock) bool { |
| 39 | + // Lazy, but hope this works |
| 40 | + return reflect.DeepEqual(l, b) |
| 41 | +} |
| 42 | + |
| 43 | +func (l PGLock) String() string { |
| 44 | + granted := "granted" |
| 45 | + if !l.Granted { |
| 46 | + granted = "waiting" |
| 47 | + } |
| 48 | + var details string |
| 49 | + switch safeString(l.LockType) { |
| 50 | + case "relation": |
| 51 | + details = "" |
| 52 | + case "page": |
| 53 | + details = fmt.Sprintf("page=%d", *l.Page) |
| 54 | + case "tuple": |
| 55 | + details = fmt.Sprintf("page=%d tuple=%d", *l.Page, *l.Tuple) |
| 56 | + case "virtualxid": |
| 57 | + details = "waiting to acquire virtual tx id lock" |
| 58 | + default: |
| 59 | + details = "???" |
| 60 | + } |
| 61 | + return fmt.Sprintf("%d-%5s [%s] %s/%s/%s: %s", |
| 62 | + l.PID, |
| 63 | + safeString(l.TransactionID), |
| 64 | + granted, |
| 65 | + safeString(l.RelationName), |
| 66 | + safeString(l.LockType), |
| 67 | + safeString(l.Mode), |
| 68 | + details, |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +// PGLocks returns a list of all locks in the database currently in use. |
| 73 | +func (q *sqlQuerier) PGLocks(ctx context.Context) (PGLocks, error) { |
| 74 | + rows, err := q.sdb.QueryContext(ctx, ` |
| 75 | + SELECT |
| 76 | + relation::regclass AS relation_name, |
| 77 | + * |
| 78 | + FROM pg_locks; |
| 79 | + `) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + defer rows.Close() |
| 85 | + |
| 86 | + var locks []PGLock |
| 87 | + err = sqlx.StructScan(rows, &locks) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + return locks, err |
| 93 | +} |
| 94 | + |
| 95 | +type PGLocks []PGLock |
| 96 | + |
| 97 | +func (l PGLocks) String() string { |
| 98 | + // Try to group things together by relation name. |
| 99 | + sort.Slice(l, func(i, j int) bool { |
| 100 | + return safeString(l[i].RelationName) < safeString(l[j].RelationName) |
| 101 | + }) |
| 102 | + |
| 103 | + var out strings.Builder |
| 104 | + for i, lock := range l { |
| 105 | + if i != 0 { |
| 106 | + out.WriteString("\n") |
| 107 | + } |
| 108 | + out.WriteString(lock.String()) |
| 109 | + } |
| 110 | + return out.String() |
| 111 | +} |
| 112 | + |
| 113 | +// Difference returns the difference between two sets of locks. |
| 114 | +// This is helpful to determine what changed between the two sets. |
| 115 | +func (l PGLocks) Difference(to PGLocks) (new PGLocks, removed PGLocks) { |
| 116 | + return slice.SymmetricDifferenceFunc(l, to, func(a, b PGLock) bool { |
| 117 | + return a.Equal(b) |
| 118 | + }) |
| 119 | +} |
0 commit comments