How to determine if an Oracle table is locked or not?

  • We've been using BI software and a repository database that are installed on Oracle Enterprise 11gR2.

    Some of these batch reports will try to access a database table which may still be locked. How can I find out if an Oracle table is locked or not? Is there any SQL statement that displays like history details to analysis?

    I mean I want to display things within specific time interval.

    For example : I want to list all locked tables between 02:00:00 PM - 05:00:00 PM for further analysis.

    Generally I want to find locked tables in oracle ?

    @Selahattin Are you talking about an application level lock? Oracle does not generally perform table locking due to its serialisation level and design

  • Following query gives details of all locks.

    SELECT B.Owner, B.Object_Name, A.Oracle_Username, A.OS_User_Name  
    FROM V$Locked_Object A, All_Objects B
    WHERE A.Object_ID = B.Object_ID
  • The following script can be used in order quickly identify all lock objects within your Oracle system.

       v$locked_object a ,
       v$session b,
       dba_objects c
       b.sid = a.session_id
       a.object_id = c.object_id;

    Reference:-Oracle Tips by Burleson Consulting

  • You can query the currently locked objects from V$LOCKED_OBJECT.

    There is no history for the locks though, logging all the locks would case huge performance overhead and a lot of data to be stored.

    The closest that the database has is Active Session history V$ACTIVE_SESSION_HISTORY, DBA_HIST_ACTIVE_SESS_HISTORY (if you have the proper license to use it), where you can view blocking sessions, statements and other information, but not locked tables. Otherwise you can try to query the appropriate views and save the needed data with your own, custom script.

  • Using the below query you can find out locks on the table.

    column oracle_username format a15;
    column os_user_name format a15;
    column object_name format a37;
    column object_type format a37;
    select a.session_id,a.oracle_username, a.os_user_name, b.owner "OBJECT OWNER", b.object_name,b.object_type,a.locked_mode from 
    (select object_id, SESSION_ID, ORACLE_USERNAME, OS_USER_NAME, LOCKED_MODE from v$locked_object) a, 
    (select object_id, owner, object_name,object_type from dba_objects) b
    where a.object_id=b.object_id;

    Blocking Locks

  • If you wish to release the lock on locked object, then kill the corresponding session.

    -- Query to Get List of all locked objects
    SELECT B.Owner, B.Object_Name, A.Oracle_Username, A.OS_User_Name  
    FROM V$Locked_Object A, All_Objects B
    WHERE A.Object_ID = B.Object_ID ; 
    -- and A.OS_USER_NAME = 'mahendar' 
    -- Query to Get List of locked sessions        
    select SID,SERIAL#,INST_ID from gv$session a  where schemaname = 'SYSTEM';
    -- and osuser =  'mahendar';
    -- o/p: 314 26513   1
    -- Statement to Kill the session [pass values in the same order and append @ for inst_id]
    alter system kill session '314,26513,@1';
  • You can check table lock from v$lock and dba_objects view. Below query will give you the lock details.

    select a.sid||'|'|| a.serial#||'|'|| a.process
    from v$session a, v$locked_object b, dba_objects c
    where b.object_id = c.object_id
    and a.sid = b.session_id
    and OBJECT_NAME=upper('&TABLE_NAME');

    QUERY 2:

    (select username from v$session where sid=a.sid) blocker,
    ' is blocking ',
    (select username from v$session where sid=b.sid) blockee,
    v$lock a,
    v$lock b
    a.block = 1
    b.request > 0
    a.id1 = b.id1
    a.id2 = b.id2;

    You can use below query which will give you more detail. Table Lock

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM