RefLib

Reference library for AI

Back

postgresql_16:3.6.27.10 Advisory Lock Functions

PostgreSQL 16 > The SQL Language > Functions and Operators > System Administration Functions > Advisory Lock Functions

The functions shown in (see functions-advisory-locks-table) manage advisory locks. For details about proper use of these functions, see (see postgresql_16:3.10.3.5).

All these functions are intended to be used to lock application-defined resources, which can be identified either by a single 64-bit key value or two 32-bit key values (note that these two key spaces do not overlap). If another session already holds a conflicting lock on the same resource identifier, the functions will either wait until the resource becomes available, or return a false result, as appropriate for the function. Locks can be either shared or exclusive: a shared lock does not conflict with other shared locks on the same resource, only with exclusive locks. Locks can be taken at session level (so that they are held until released or the session ends) or at transaction level (so that they are held until the current transaction ends; there is no provision for manual release). Multiple session-level lock requests stack, so that if the same resource identifier is locked three times there must then be three unlock requests to release the resource in advance of session end.

| Function Description | | --- | | pg_advisory_lock ( key bigint ) void pg_advisory_lock ( key1 integer, key2 integer ) void Obtains an exclusive session-level advisory lock, waiting if necessary. | | pg_advisory_lock_shared ( key bigint ) void pg_advisory_lock_shared ( key1 integer, key2 integer ) void Obtains a shared session-level advisory lock, waiting if necessary. | | pg_advisory_unlock ( key bigint ) boolean pg_advisory_unlock ( key1 integer, key2 integer ) boolean Releases a previously-acquired exclusive session-level advisory lock. Returns true if the lock is successfully released. If the lock was not held, false is returned, and in addition, an SQL warning will be reported by the server. | | pg_advisory_unlock_all () void Releases all session-level advisory locks held by the current session. (This function is implicitly invoked at session end, even if the client disconnects ungracefully.) | | pg_advisory_unlock_shared ( key bigint ) boolean pg_advisory_unlock_shared ( key1 integer, key2 integer ) boolean Releases a previously-acquired shared session-level advisory lock. Returns true if the lock is successfully released. If the lock was not held, false is returned, and in addition, an SQL warning will be reported by the server. | | pg_advisory_xact_lock ( key bigint ) void pg_advisory_xact_lock ( key1 integer, key2 integer ) void Obtains an exclusive transaction-level advisory lock, waiting if necessary. | | pg_advisory_xact_lock_shared ( key bigint ) void pg_advisory_xact_lock_shared ( key1 integer, key2 integer ) void Obtains a shared transaction-level advisory lock, waiting if necessary. | | pg_try_advisory_lock ( key bigint ) boolean pg_try_advisory_lock ( key1 integer, key2 integer ) boolean Obtains an exclusive session-level advisory lock if available. This will either obtain the lock immediately and return true, or return false without waiting if the lock cannot be acquired immediately. | | pg_try_advisory_lock_shared ( key bigint ) boolean pg_try_advisory_lock_shared ( key1 integer, key2 integer ) boolean Obtains a shared session-level advisory lock if available. This will either obtain the lock immediately and return true, or return false without waiting if the lock cannot be acquired immediately. | | pg_try_advisory_xact_lock ( key bigint ) boolean pg_try_advisory_xact_lock ( key1 integer, key2 integer ) boolean Obtains an exclusive transaction-level advisory lock if available. This will either obtain the lock immediately and return true, or return false without waiting if the lock cannot be acquired immediately. | | pg_try_advisory_xact_lock_shared ( key bigint ) boolean pg_try_advisory_xact_lock_shared ( key1 integer, key2 integer ) boolean Obtains a shared transaction-level advisory lock if available. This will either obtain the lock immediately and return true, or return false without waiting if the lock cannot be acquired immediately. |

Back