transaction

Advisory lock

Application-defined cooperative lock. Transaction-level locks release at transaction end; session locks have a different lifetime. All participants must agree on keys. Target: PostgreSQL 17. Example is a connection-specific schedule, not a single executable script.

Formal Syntax & Structure

sql syntaxsql
SELECT pg_try_advisory_xact_lock(42)

Working Example

Example Snippetsql
-- Fresh fixture: CREATE TABLE t(id INT PRIMARY KEY, n INT NOT NULL);-- INSERT INTO t VALUES (1,10),(2,10);-- Use two independent connections A and B; setup committed first.-- A: BEGIN;-- A: SELECT pg_try_advisory_xact_lock(42);-- B: BEGIN;-- B: SELECT pg_try_advisory_xact_lock(42);-- A: COMMIT;-- B: SELECT pg_try_advisory_xact_lock(42);-- Roll back any open transactions after observation.
SQL Reference