• Arun Kumar

Find Locked Tables Inside Oracle

Oracle locking mechanism keeps database objects isolated from accidental changes by multiple users at a given point of time. Locks are good and this helps keep data consistent inside database.

But, what if there is a user who updated a table on friday and went out for weekend without issuing COMMIT inside database?

This creates a problem for other users and a DBA must be able to find locked tables inside database.

Create simple lock

Let us create a simple lock inside database and debug the issue.

Open a new sessions (using putty or sql* plus) and create a test table and insert one record into it

SQL> create table lock_test(
  id# number primary key,
  value varchar2(20)

SQL> insert into lock_test values (1, 'Insert lock test');

Open a new session (using putty or sql* plus) and try to run the same INSERT command

-- run it in a new session
SQL> insert into lock_test values (1, 'Insert lock test');

Things to note:

  • In session 1, we did not commit the transaction.

  • Session 2 is trying to insert a record with same primary key value

  • Session 2 goes on waiting until session 1 issues COMMIT / ROLLBACK

Find blocked sessions

Open a new sessions, just for activity purpose and execute below query to find:

  • Blocking session and

  • Blocked session

select a.SID "Blocking Session", b.SID "Blocked Session"  
from v$lock a, v$lock b 
where a.SID != b.SID and a.ID1 = b.ID1  and a.ID2 = b.ID2 and 
b.request > 0 and a.block = 1;


Another query that can help you with finding the blocking and blocked sessions

col blocking_status for a120;
select s1.username || '@' || s1.machine
 || ' ( SID=' || s1.sid || ' ) is blocking '
 || s2.username || '@' || s2.machine 
 || ' ( SID=' || s2.sid || ' ) ' AS blocking_status
 from v$lock l1, v$session s1, v$lock l2, v$session s2
 where s1.sid=l1.sid and s2.sid=l2.sid
 and l1.BLOCK=1 and l2.request > 0
 and l1.id1 = l2.id1
 and l2.id2 = l2.id2 ;

Find lock wait time

Another query that can help you find the blocking session, blocked session and wait time

  blocking_session "BLOCKING_SESSION",
  serial# "BLOCKED_SERIAL#", 
  seconds_in_wait/60 "WAIT_TIME(MINUTES)"
FROM v$session
WHERE blocking_session is not NULL
ORDER BY blocking_session;

Find blocked SQL

You can run below query to check what SQL is being run by the BLOCKED SESSION inside the database OR which SQL command is waiting


Find locked table

Run below query to find the table locked, table owner, lock type and other details

col session_id head 'Sid' form 9999
col object_name head "Table|Locked" form a30
col oracle_username head "Oracle|Username" form a10 truncate 
col os_user_name head "OS|Username" form a10 truncate 
col process head "Client|Process|ID" form 99999999
col owner head "Table|Owner" form a10
col mode_held form a15
select lo.session_id,lo.oracle_username,lo.os_user_name,
decode(lo.locked_mode,0, 'None',1, 'Null',2, 'Row Share (SS)',
3, 'Row Excl (SX)',4, 'Share',5, 'Share Row Excl (SSX)',6, 'Exclusive',
to_char(lo.locked_mode)) mode_held
from gv$locked_object lo, dba_objects do
where lo.object_id = do.object_id
order by 5


Resolving locks in oracle

As per Oracle database properties, the blocked (or waiting) session will continue to wait until:

  • Blocking session issues a COMMIT

  • Blocking session issues a ROLLBACK

  • Blocking session disconnects from the database

As a DBA, the only way you can help resolve a lock conflict is by killing either the blocking session or by killing the blocked (waiting) session.

Before you decide which session to kill, you must send the information to application team to get their approval on which session to be killed.

Use below query to kill a session inside Oracle

SQL> alter system kill session '38,4245';

--actual syntax
SQL> alter system kill session 'SID,Serial#';

If you don’t know the serial# then query v$session

SQL> select serial# from v$session where SID=38';

Important views

Below are some of the important views to help you while working with database locks in Oracle:

Important links

In case you would like to generate a report inside database regarding all the blocked and blocking sessions, then check the below link:


Recent Posts

See All

Oracle 12c to 19c Database Upgrade

In this article we will be looking at two most commonly used methods of upgrading Oracle database from 12c to 19c Pre-upgrade tasks Upgrade using DBUA Upgrade using Manual Method You can perform a dir