Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an advisory lock in PostgreSQL?
An advisory lock is a user-controlled lock that applications can use to coordinate access to resources without locking database rows or tables. It is voluntary and does not block normal database operations.
Click to reveal answer
beginner
How do you acquire a session-level advisory lock in PostgreSQL?
Use the function pg_advisory_lock(key) where key is a 64-bit integer. This lock is held until the session ends or the lock is explicitly released.
Click to reveal answer
intermediate
What happens if you try to acquire an advisory lock that is already held by another session?
The session requesting the lock will wait (block) until the lock is released by the other session, unless you use the non-blocking version pg_try_advisory_lock() which returns immediately with success or failure.
Click to reveal answer
beginner
How can you release an advisory lock in PostgreSQL?
You can release it explicitly using pg_advisory_unlock(key). If the session ends, all session-level advisory locks held by it are automatically released.
Click to reveal answer
intermediate
What is the difference between session-level and transaction-level advisory locks?
Session-level locks last until the session ends or are explicitly released. Transaction-level locks last only until the current transaction ends (commit or rollback). Use pg_advisory_xact_lock() for transaction-level locks.
Click to reveal answer
Which PostgreSQL function acquires a blocking session-level advisory lock?
Apg_advisory_lock(key)
Bpg_try_advisory_lock(key)
Cpg_advisory_unlock(key)
Dpg_advisory_xact_lock(key)
✗ Incorrect
pg_advisory_lock(key) acquires a blocking session-level advisory lock, waiting if necessary.
What does pg_try_advisory_lock(key) do if the lock is already held?
AWaits until the lock is free
BReleases the lock
CThrows an error
DReturns immediately with false
✗ Incorrect
pg_try_advisory_lock(key) returns immediately with false if the lock is already held.
When are session-level advisory locks automatically released?
AAt the end of the current transaction
BWhen the session ends
CAfter 5 minutes
DWhen the database restarts
✗ Incorrect
Session-level advisory locks are released automatically when the session ends.
Which function acquires a transaction-level advisory lock?
Apg_advisory_xact_lock(key)
Bpg_try_advisory_lock(key)
Cpg_advisory_unlock(key)
Dpg_advisory_lock(key)
✗ Incorrect
pg_advisory_xact_lock(key) acquires a transaction-level advisory lock that lasts only for the current transaction.
Can advisory locks block normal database operations like row inserts?
AOnly if the lock is held for more than 10 seconds
BYes, always
CNo, advisory locks do not block normal operations
DOnly on tables with triggers
✗ Incorrect
Advisory locks are voluntary and do not block normal database operations like inserts or updates.
Explain what advisory locks are in PostgreSQL and how they differ from regular locks.
Think about locks that applications use to coordinate without affecting database rows or tables.
You got /4 concepts.
Describe the difference between session-level and transaction-level advisory locks and when each is released.
Consider the scope and lifetime of the locks in relation to sessions and transactions.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of advisory locks in PostgreSQL?
easy
A. To control access to resources using user-defined keys
B. To automatically manage table-level locks during transactions
C. To speed up query execution by caching results
D. To backup the database safely without downtime
Solution
Step 1: Understand advisory locks concept
Advisory locks allow applications to coordinate access to resources by using custom keys, not automatic locks on tables or rows.
Step 2: Compare options
The other options describe other database features unrelated to advisory locks.
Final Answer:
To control access to resources using user-defined keys -> Option A
Quick Check:
Advisory locks = user-defined resource control [OK]
Hint: Advisory locks use keys to manage resource access [OK]
Common Mistakes:
Confusing advisory locks with automatic table locks
Thinking advisory locks speed up queries
Assuming advisory locks handle backups
2. Which of the following is the correct syntax to acquire a session-level advisory lock with key 12345?
easy
A. SELECT pg_advisory_lock(12345);
B. LOCK TABLE pg_advisory_lock(12345);
C. SELECT acquire_lock(12345);
D. BEGIN LOCK 12345;
Solution
Step 1: Recall advisory lock syntax
PostgreSQL uses the function pg_advisory_lock(key) to acquire a session-level advisory lock.
Step 2: Evaluate options
SELECT pg_advisory_lock(12345); is the correct function call. The other options use invalid syntax or non-existent functions.
Final Answer:
SELECT pg_advisory_lock(12345); -> Option A
Quick Check:
pg_advisory_lock(key) = correct syntax [OK]
Hint: Use SELECT pg_advisory_lock(key) to lock [OK]
Common Mistakes:
Using LOCK TABLE instead of function call
Calling non-existent functions like acquire_lock
Trying to lock with BEGIN LOCK syntax
3. What will be the result of this query if the advisory lock with key 999 is already held by another session?
SELECT pg_try_advisory_lock(999);
medium
A. true
B. false
C. null
D. error
Solution
Step 1: Understand pg_try_advisory_lock behavior
This function tries to acquire the lock immediately and returns true if successful, false if the lock is held by someone else.
Step 2: Analyze the scenario
Since the lock with key 999 is already held, the function returns false without waiting.
Final Answer:
false -> Option B
Quick Check:
pg_try_advisory_lock returns false if lock busy [OK]
Hint: pg_try_advisory_lock returns false if lock busy [OK]
Common Mistakes:
Expecting true even if lock is held
Thinking it returns null or error
Confusing pg_try_advisory_lock with pg_advisory_lock
4. You wrote this code:
SELECT pg_advisory_unlock(123);
But the lock was never acquired before. What will happen?
medium
A. The function returns true and releases the lock
B. The function blocks until the lock is acquired
C. The function throws an error
D. The function returns false because no lock was held
Solution
Step 1: Understand pg_advisory_unlock behavior
This function releases a lock if held and returns true; if no lock was held, it returns false.
Step 2: Analyze the scenario
Since the lock was never acquired, the function returns false without error or blocking.
Final Answer:
The function returns false because no lock was held -> Option D
Quick Check:
Unlock returns false if lock not held [OK]
Hint: Unlock returns false if no lock held [OK]
Common Mistakes:
Expecting an error when unlocking unheld lock
Thinking unlock blocks or waits
Assuming unlock always returns true
5. You want to ensure two different sessions do not run a critical section simultaneously using advisory locks. Which approach is best?
-- Session 1 and 2 run this code: SELECT pg_try_advisory_lock(42); -- If true, run critical section, then SELECT pg_advisory_unlock(42);
hard
A. Use pg_advisory_unlock before acquiring lock to clear old locks
B. Use pg_try_advisory_lock to attempt lock and skip if busy
C. Use pg_advisory_lock to wait until lock is available before running
D. Use random keys each time to avoid conflicts
Solution
Step 1: Understand locking strategies
pg_try_advisory_lock returns immediately and may skip critical section if lock busy; pg_advisory_lock waits until lock is free.
Step 2: Choose best approach for critical section
To ensure only one session runs critical section at a time, waiting for the lock is safer than skipping it.
Step 3: Evaluate other options
Unlocking before acquiring is unsafe and random keys defeat locking purpose.
Final Answer:
Use pg_advisory_lock to wait until lock is available before running -> Option C
Quick Check:
Waiting lock ensures exclusive access [OK]
Hint: Use pg_advisory_lock to wait for exclusive access [OK]
Common Mistakes:
Using try lock and skipping critical section silently