cockroachdb / cockroachdb/cockroach
sql: implement session-scoped advisory locks
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
Transaction-level advisory locks were implemented in #168355. To complete advisory lock support, we need to implement the session-scoped advisory lock builtins. Unlike transaction-scoped locks, session-scoped locks are held until explicitly released (or the session ends), and support both single-key (`bigint`) and dual-key (`integer, integer`) signatures.
### Blocking Session Locks:
`pg_advisory_lock(key bigint)` / `pg_advisory_lock(key1 integer, key2 integer)`: Acquires an exclusive session-level lock, blocking until available.
`pg_advisory_lock_shared(key bigint)` / `pg_advisory_lock_shared(key1 integer, key2 integer)`: Acquires a shared session-level lock, blocking until available.
### Non-Blocking ("Try") Session Locks:
`pg_try_advisory_lock(key bigint) → boolean` / `pg_try_advisory_lock(key1 integer, key2 integer) → boolean`: Attempts to acquire an exclusive session-level lock, returning a boolean immediately.
`pg_try_advisory_lock_shared(key bigint) → boolean` / `pg_try_advisory_lock_shared(key1 integer, key2 integer) → boolean`: Attempts to acquire a shared session-level lock, returning a boolean immediately.
### Session Lock Release:
`pg_advisory_unlock(key bigint) → boolean` / `pg_advisory_unlock(key1 integer, key2 integer) → boolean`: Releases a previously-acquired exclusive session-level lock. Returns `true` if released, `false` if not held (with an SQL warning).
`pg_advisory_unlock_shared(key bigint) → boolean` / `pg_advisory_unlock_shared(key1 integer, key2 integer) → boolean`: Releases a previously-acquired shared session-level lock. Returns `true` if released, `false` if not held (with an SQL warning).
`pg_advisory_unlock_all() → void`: Releases all session-level advisory locks held by the current session. Implicitly invoked at session end, even on ungraceful disconnect.
See the PostgreSQL docs: [Advisory Locks](https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS) and [Advisory Lock Functions](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS).
Epic: CRDB-63745
Jira issue: CRDB-63746
Contributor guide
Assessment
This issue has not been assessed yet.