0
0
PostgreSQLquery~5 mins

Advisory locks in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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)
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
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
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)
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
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.