pg_lock_pool
pg_lock_pool · Wait on a PostgreSQL advisory lock pool
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100622 | pg_lock_pool | pg_lock_pool | 0.0.1 | ADMIN | PostgreSQL | SQL |
PostgreSQL · 12, 13, 14, 15, 16, 17
| Relationships | |
|---|---|
| Requires | plpgsql |
Usage
Sources:
pg_lock_pool 0.0.1 provides PL/pgSQL helpers that wait for any free slot in a pool represented by two-key PostgreSQL advisory locks. Use it to cap cooperating work to poolsize concurrent holders without creating a lock table.
Session-Level Pool
get_lock_pool(key, poolsize, timeout) probes slots 1..poolsize with pg_try_advisory_lock(key, slot). On success it returns the positive slot number; the caller must pass the same key and returned slot to pg_advisory_unlock. A session-level lock otherwise remains until the database session ends, including across transaction rollback.
Transaction-Level Pool
get_xact_lock_pool uses pg_try_advisory_xact_lock and releases the chosen slot automatically at transaction end. Both functions default timeout to 10 seconds and return -1 when no slot is acquired before the loop reaches the timeout.
Operational Semantics
Each unsuccessful pass tries every slot, checks its integer retry counter, then sleeps for one second. The timeout is therefore an approximate retry duration rather than a precise wall-clock deadline, and each waiting caller occupies a PostgreSQL backend.
Advisory locks are cooperative: unrelated SQL can ignore them, and every participant must use the same key, poolsize, and session-versus-transaction convention. Validate that poolsize and timeout are positive in application code. Always retain the returned session slot and release it in error paths; connection pooling can otherwise keep the lock far longer than the logical request.