Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Concurrency and Locks
00:00
5 left

SQL Concurrency and Locks

HardSQL · PostgreSQL

Problem

They asked me about concurrency/lock problems, how I would work with those requirements.

Using the provided lock request and lock compatibility data, write a SQL query to identify currently waiting requests blocked by granted requests on the same relation. Exclude requests from the same transaction.

Output

  1. One row per waiting and blocking request pair
  2. Columns: waiting_request_id, blocking_request_id, relation_name, waiting_mode, and blocking_mode
  3. Include only incompatible lock pairs, ordered by waiting_request_id, then blocking_request_id

Schema

lock_requests
ColumnTypeDescription
request_idPKINTUnique lock request identifier
transaction_idINTTransaction that owns or requests the lock
relation_nameVARCHAR(100)Database relation being locked
requested_modeVARCHAR(50)Requested PostgreSQL lock mode
grantedBOOLEANWhether the requested lock has been granted
requested_atTIMESTAMPTime at which the lock was requested
lock_mode_compatibility
ColumnTypeDescription
mode_aPKVARCHAR(50)First mode in canonical lexical order
mode_bPKVARCHAR(50)Second mode in canonical lexical order
is_compatibleBOOLEANWhether the two lock modes can coexist
Tableslock_requestslock_mode_compatibility
Interviewer

Your question is SQL Concurrency and Locks. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.