0
0
PostgreSQLquery~20 mins

Advisory locks in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advisory Lock Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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:

SELECT pg_try_advisory_lock(12345);

What will be the output if the lock is successfully acquired?
PostgreSQL
SELECT pg_try_advisory_lock(12345);
Atrue
Bfalse
Cnull
Dan error message
Attempts:
2 left
💡 Hint
pg_try_advisory_lock returns a boolean indicating success or failure.
🧠 Conceptual
intermediate
2:00remaining
Which statement about advisory locks is true?
Which of the following statements about PostgreSQL advisory locks is correct?
AAdvisory locks block other sessions until explicitly released or session ends.
BAdvisory locks can only be acquired using integer keys.
CAdvisory locks are visible in the pg_locks system view with locktype 'advisory'.
DAdvisory locks are automatically released at the end of the current transaction.
Attempts:
2 left
💡 Hint
Think about how advisory locks behave and how they appear in system views.
📝 Syntax
advanced
2: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?
ASELECT pg_advisory_unlock_all();
BSELECT pg_advisory_unlock_shared(9876);
CSELECT pg_advisory_unlock(1234);
DSELECT pg_advisory_unlock(9876);
Attempts:
2 left
💡 Hint
Releasing a lock requires the exact key used to acquire it.
query_result
advanced
2:00remaining
What is the result of this transaction-level advisory lock query?
Given the following query inside a 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);
Afalse
Bthe query waits until the lock is free
Ctrue
Dan error is raised
Attempts:
2 left
💡 Hint
pg_try_advisory_xact_lock tries to acquire the lock without waiting.
🔧 Debug
expert
2:00remaining
Why does this advisory lock query cause an error?
Consider this query:

SELECT pg_advisory_lock('mylock');

Why does this query cause an error?
AThe lock key 'mylock' is already held by another session.
BThe function pg_advisory_lock requires integer arguments, not text.
Cpg_advisory_lock cannot be called outside a transaction.
DThe function pg_advisory_lock does not exist in PostgreSQL.
Attempts:
2 left
💡 Hint
Check the data type of the argument passed to pg_advisory_lock.