This query will return the session IDs of both the blocking session and the blocked session(s), based on the ID1 and ID2 columns which identify the specific resources that are locked.
SELECT blocking.session_id AS "Blocking Session", waiting.session_id AS "Blocked Session", blocking.id1, blocking.id2FROM v$lock blockingJOIN v$lock waiting ON (blocking.id1 = waiting.id1 AND blocking.id2 = waiting.id2)WHERE blocking.session_id != waiting.session_id AND waiting.request > 0;
After identifying the blocking session, you can further investigate to find the SQL being executed by that session using a query like this:
SELECT sql_fulltext FROM v$sql WHERE sql_id = ( SELECT sql_id FROM v$session WHERE session_id = 'YOUR_BLOCKING_SESSION_ID');Replace `'YOUR_BLOCKING_SESSION_ID'` with the session ID you identified as the blocking session from the first query.
Once you've gathered this information, you can decide on the appropriate action to take, such as waiting for the blocking session to complete its transaction, or, if necessary and safe to do so, terminating the blocking session to release the lock. To terminate a session, use a command like the one below:
ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;Replace `sid,serial#` with the SID and SERIAL# of the blocking session, which you can obtain from the `V$SESSION` view.