Documentation
¶
Overview ¶
Package advisorylock holds the platform's PostgreSQL session advisory locks on one database session per process (#2169).
An advisory lock belongs to the session that took it, so a lock held for a background pass used to pin a pooled connection of its own for the whole pass, while the pass drew its queries from the same pool. Each locked loop added one more pinned connection. With four loops locked on a pool of five, the passes waited on the pool for their own queries, never finished, never released their locks, and starved every request on the replica.
PostgreSQL lets one session hold any number of advisory locks, so every try-lock the process takes goes onto one Session: however many loops hold a lock, together they pin one connection, and the session goes back to the pool when the last lock is released. The pass itself runs on the pool, never on the lock's session.
A session lock is reentrant in PostgreSQL: the session holding a key is granted it again. A Session therefore refuses a key the process already holds, so two goroutines of one process exclude each other as two replicas do.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key is one advisory lock: a single bigint, or the two-integer form, which PostgreSQL keeps in a key space of its own.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is the one database session a process takes its advisory locks on. It is safe for concurrent use.
func New ¶
New builds a Session over db. Production code uses For, which keeps one per database handle; New is for a test that stands two replicas on one.