Challenge - 5 Problems
Advisory Lock Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this advisory lock query?
Consider the following PostgreSQL query that attempts to acquire a session-level advisory lock with key 12345:
What will be the output if the lock is successfully acquired?
SELECT pg_try_advisory_lock(12345);
What will be the output if the lock is successfully acquired?
PostgreSQL
SELECT pg_try_advisory_lock(12345);
Attempts:
2 left
💡 Hint
pg_try_advisory_lock returns a boolean indicating success or failure.
✗ Incorrect
The function pg_try_advisory_lock returns true if the lock was successfully acquired, false if it was already held by another session.
🧠 Conceptual
intermediate2:00remaining
Which statement about advisory locks is true?
Which of the following statements about PostgreSQL advisory locks is correct?
Attempts:
2 left
💡 Hint
Think about how advisory locks behave and how they appear in system views.
✗ Incorrect
Advisory locks appear in pg_locks with locktype 'advisory'. They can be session-level or transaction-level, and session-level locks are released at session end, while transaction-level locks are released at transaction end.
📝 Syntax
advanced2:00remaining
Which query correctly releases a session-level advisory lock with key 9876?
You have acquired a session-level advisory lock with key 9876. Which of the following queries correctly releases that lock?
Attempts:
2 left
💡 Hint
Releasing a lock requires the exact key used to acquire it.
✗ Incorrect
pg_advisory_unlock releases the session-level exclusive lock for the given key. Unlocking with a different key or using shared unlock will not release the exclusive lock.
❓ query_result
advanced2:00remaining
What is the result of this transaction-level advisory lock query?
Given the following query inside a transaction:
What will be the output if the lock is already held by another transaction?
SELECT pg_try_advisory_xact_lock(5555);
What will be the output if the lock is already held by another transaction?
PostgreSQL
SELECT pg_try_advisory_xact_lock(5555);
Attempts:
2 left
💡 Hint
pg_try_advisory_xact_lock tries to acquire the lock without waiting.
✗ Incorrect
pg_try_advisory_xact_lock returns false immediately if the lock is held by another transaction; it does not wait or raise an error.
🔧 Debug
expert2:00remaining
Why does this advisory lock query cause an error?
Consider this query:
Why does this query cause an error?
SELECT pg_advisory_lock('mylock');Why does this query cause an error?
Attempts:
2 left
💡 Hint
Check the data type of the argument passed to pg_advisory_lock.
✗ Incorrect
pg_advisory_lock requires one or two integer arguments. Passing a string causes a type error.