The idea · Lab 2.7.2An advisory lock is a mutex PostgreSQL manages for you, identified by nothing more than a number you pick — it has no relationship to any table, row, or object. That is exactly what makes it useful for application-level coordination that has nothing to do with data: two instances of a job, a leader election, a "only one migration script at a time" guard. Whoever acquires the number first wins; everyone else can either wait or, more usefully, ask non-blockingly and immediately learn they lost.
This lab makes the mutex genuinely observable: one session holds an advisory lock and is kept alive with a wrapping sleep the exact way earlier labs kept a row lock or a table lock alive, a second session races for the same number and loses immediately rather than waiting, and pg_locks shows the held lock directly while all of this is happening.
Non-Blocking Acquisition
SELECT pg_try_advisory_lock(424242); -- true if acquired, false if already held elsewhere — never waits
Compare with the blocking form:
SELECT pg_advisory_lock(424242); -- waits until it can acquire the lock, like any other PostgreSQL lock wait
Two Scopes, Two Different Release Rules
| Function | Released by | Use it when |
|---|
pg_try_advisory_lock() / pg_advisory_lock() | An explicit pg_advisory_unlock() call, or the session disconnecting | The coordination needs to outlive a single transaction (a long-running job, a leader election) |
pg_advisory_xact_lock() | Automatically, the instant the current transaction commits or rolls back | The coordination should never accidentally outlive one transaction — no unlock call to forget |
Seeing the Lock, Live
SELECT locktype, mode, granted, objid FROM pg_locks WHERE locktype = 'advisory';
This is the exact same pg_locks view used for every other lock type in this course — an advisory lock is a first-class PostgreSQL lock, just one your own application chose to take rather than one the database took on your behalf.
A Cron-Safe Wrapper
CREATE OR REPLACE FUNCTION run_nightly_etl() RETURNS text LANGUAGE plpgsql AS $$
BEGIN
IF NOT pg_try_advisory_lock(555) THEN
RETURN 'already running, skipped';
END IF;
-- ... the real job body goes here ...
PERFORM pg_advisory_unlock(555);
RETURN 'completed';
END;
$$;
Two overlapping cron triggers of this same job — one running long, one starting on schedule regardless — resolve themselves with zero coordination infrastructure: the second one simply sees the lock already held and exits immediately.
Key concepts
Advisory Lock
A mutex identified purely by an application-chosen number (or two 32-bit numbers), with no connection to any table, row, or database object whatsoever. PostgreSQL enforces mutual exclusion on that number exactly like any other lock, but the meaning of the number — what it is coordinating — is entirely up to the application, not the database.
Connection-Scoped vs Transaction-Scoped
pg_advisory_lock() / pg_try_advisory_lock() locks live for the connection's lifetime and need an explicit pg_advisory_unlock() (or disconnection) to release — appropriate for coordination spanning multiple transactions. pg_advisory_xact_lock() locks are tied to the current transaction and release automatically and unconditionally at COMMIT or ROLLBACK, which is safer whenever the coordination should never be allowed to outlive one transaction by a forgotten unlock call.
What to remember
- An advisory lock is a mutex identified purely by an application-chosen number, with no connection to any table or row
- pg_try_advisory_lock() never waits — it returns false immediately if the lock is already held, which is what makes it useful for a losing process to react immediately instead of blocking
- Advisory locks are first-class PostgreSQL locks, visible in the same pg_locks view as every other lock type in this course
- Connection-scoped locks need an explicit pg_advisory_unlock() — relying on disconnection to release one is risky behind a connection pooler
- Transaction-scoped locks (pg_advisory_xact_lock) release automatically and unconditionally at COMMIT or ROLLBACK, with no way to forget to unlock
- hashtext() turns a descriptive string into a stable lock number, making accidental collisions between unrelated features far less likely