Skip to content

pg_lock_pool

Wait on a PostgreSQL advisory lock pool

pg_lock_pool · Wait on a PostgreSQL advisory lock pool

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100622pg_lock_poolpg_lock_pool0.0.1ADMINPostgreSQLSQL

PostgreSQL · 12, 13, 14, 15, 16, 17

Relationships
Requiresplpgsql

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

CREATE EXTENSION pg_lock_pool;

SELECT get_lock_pool(999, 3, 30) AS slot;
SELECT pg_advisory_unlock(999, 1);

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

BEGIN;
SELECT get_xact_lock_pool(999, 3, 30) AS slot;
COMMIT;

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.

Table reader