Predict the output
Two calls in the same session: what's printed?
SELECT pg_try_advisory_lock(42);
SELECT pg_try_advisory_lock(42);
SELECT pg_advisory_unlock(42), pg_advisory_unlock(42),
pg_advisory_unlock(42);Hint 1 of 1
Is the lock re-entrant within one session?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A t, t, then t|t|f
Correct. Session advisory locks are re-entrant: taken twice, released twice; the third unlock has nothing to release.
Session-level advisory locks count how many times the session took them. Two acquisitions need two unlocks to release, and the third unlock returns false because nothing is held.
Why not the others?
- B
t,f, thent|f|f. Another session would get f, but the same session can take it again. - C
t,t, thent|f|f. Each acquisition needs its own unlock. - D An error on the second call. try_ functions never wait or raise; they return a boolean.
| pg_advisory_unlock | pg_advisory_unlock | pg_advisory_unlock |
|---|---|---|
| t | t | f |
(1 row)