0
0
PostgreSQLquery~10 mins

Deadlock detection and prevention in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check for deadlocks using PostgreSQL system views.

PostgreSQL
SELECT * FROM pg_locks WHERE [1] = true;
Drag options to blanks, or click blank then click option'
Awaiting
Blocked
Cgranted
Dactive
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name that does not exist in pg_locks.
Confusing 'waiting' with 'granted'.
2fill in blank
medium

Complete the query to find transactions waiting for locks in PostgreSQL.

PostgreSQL
SELECT pid, blocked_pid FROM pg_blocking_pids([1]);
Drag options to blanks, or click blank then click option'
Atransaction_id
Bsession_id
Clock_id
Dpid
Attempts:
3 left
💡 Hint
Common Mistakes
Passing transaction or session IDs instead of process IDs.
Using a column that does not exist in the context.
3fill in blank
hard

Fix the error in the query to detect deadlocks by joining pg_locks and pg_stat_activity.

PostgreSQL
SELECT a.pid, a.query, l.locktype FROM pg_stat_activity a JOIN pg_locks l ON a.[1] = l.pid WHERE NOT l.[2];
Drag options to blanks, or click blank then click option'
Apid
Bprocess_id
Csession_pid
Dgranted
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect column names for join.
Filtering on the wrong lock status column.
4fill in blank
hard

Fill both blanks to write a query that detects deadlocks by checking waiting and granted locks.

PostgreSQL
SELECT blocked_locks.pid AS blocked_pid, blocking_locks.pid AS blocking_pid FROM pg_locks blocked_locks JOIN pg_locks blocking_locks ON blocked_locks.[1] = blocking_locks.[2] WHERE NOT blocked_locks.granted AND blocking_locks.granted;
Drag options to blanks, or click blank then click option'
Atransactionid
Bpid
Clocktype
Dmode
Attempts:
3 left
💡 Hint
Common Mistakes
Joining on process IDs instead of transaction IDs.
Using locktype or mode for join which is incorrect.
5fill in blank
hard

Fill all three blanks to write a query that prevents deadlocks by ordering lock acquisition.

PostgreSQL
BEGIN; LOCK TABLE [1] IN [2] MODE; LOCK TABLE [3] IN SHARE MODE; COMMIT;
Drag options to blanks, or click blank then click option'
Atable_a
BEXCLUSIVE
Ctable_b
DACCESS SHARE
Attempts:
3 left
💡 Hint
Common Mistakes
Locking tables in different orders causing deadlocks.
Using incorrect lock modes.