Solved

any script to find which user locking other user

Posted on 2014-03-24
16
539 Views
Last Modified: 2014-03-25
Hey,

we are seeing locking sessions just want to know if the objects in same schema causing it or other schema is blocking this schema. how to find which session belong to which user is causing locks  which object in other user.
0
Comment
Question by:ajaybelde
  • 6
  • 5
  • 4
16 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39951020
0
 

Author Comment

by:ajaybelde
ID: 39951033
do you have script wich already written which will list the sid beoing locked and owner of the objects and locking user
0
 

Author Comment

by:ajaybelde
ID: 39951061
SELECT username U_NAME, owner OBJ_OWNER,
object_name, object_type, s.osuser,
DECODE(l.block,
  0, 'Not Blocking',
  1, 'Blocking',
  2, 'Global') STATUS,
  DECODE(v.locked_mode,
    0, 'None',
    1, 'Null',
    2, 'Row-S (SS)',
    3, 'Row-X (SX)',
    4, 'Share',
    5, 'S/Row-X (SSX)',
    6, 'Exclusive', TO_CHAR(lmode)
  ) MODE_HELD
FROM gv$locked_object v, dba_objects d,
gv$lock l, gv$session s
WHERE v.object_id = d.object_id
AND (v.object_id = l.id1)
AND v.session_id = s.sid
ORDER BY username, session_id;

does it look good
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39951110
Try this....

It will show you locking chains (a blocking b blocking c blocking d, etc)  as well as identifying which row in a table (if a table lock) is in contention and sql being executed by each session currently (this is not necessarily the statement that created a lock though)

I use a scalar  subquery for the object lookup instead of a join, because it's possible that sessions are blocking each other but not because of object contention.  This could also be achieved with an outerjoin, mostly an aesthetic preference.


WITH lock_holders
     AS (SELECT *
           FROM gv$session
          WHERE COALESCE(blocking_instance, blocking_session) IS NOT NULL
             OR (inst_id, sid) IN (SELECT blocking_instance, blocking_session FROM gv$session))
    SELECT LPAD(' ', 3 * (LEVEL - 1)) || sid || DECODE(LEVEL, 1, ' root blocker') waiting_session,
           inst_id w_inst,
           serial#,
           status,
           event,
           username || ' - ' || osuser || ' - ' || machine user_info,
           (SELECT owner || '.' || object_name
              FROM dba_objects
             WHERE object_id = row_wait_obj#)
               waiting_on_object,
           CASE
               WHEN row_wait_obj# > 0
               THEN
                   DBMS_ROWID.rowid_create(
                       1,
                       row_wait_obj#,
                       row_wait_file#,
                       row_wait_block#,
                       row_wait_row#
                   )
           END
               waiting_on_rowid,
           (SELECT sql_text
              FROM v$sql s
             WHERE s.sql_id = h.sql_id)
               current_sql
      FROM lock_holders h
CONNECT BY PRIOR sid = blocking_session AND PRIOR inst_id = blocking_instance
START WITH blocking_session IS NULL;
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39951140
Did you check out the script I posted?

Or look for another one here.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39951367
Sorry, thought it would be obvious to look in the "Database Performance" section.
Also I found an OLD script that was provided by Oracle (attached).
db-locks.sql
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39951535
wow, 1989, that is old!

My script is from the last millineum too but not quite that old and has been updated with new syntax and features several times since then.


based on the naming similarities I'm going to guess my original inspiration was the script you posted.  I could never remember why I called that WITH (which I originally wrote as an inline view) "lock_holders"  that always seemed to me like an inadequate name.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39952090
Excuse me: utllockt.sql,v 1.2 1995/05/05 10:16:23
Modified for Oracle version 7!
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39952189
I was referring to the creation date.  utllockt has been updated a few more times since 95.  Just check $ORACLE_HOME/rdbms/admin in your most recent installation.

It's the same basic structure though,  that's why I wrote my own.  I wanted something I could run where I didn't have authority to create new tables.  Plus, it always seemed like a wasted step anyway.  And I needed a RAC aware, or actually an OPS-aware version, back then and utllockt doesn't cover that.
0
 

Author Comment

by:ajaybelde
ID: 39953955
can we identify based on history views like active session history
0
 

Author Comment

by:ajaybelde
ID: 39953969
and one more time is the script iam using cooorect.

SELECT username U_NAME, owner OBJ_OWNER,
object_name, object_type, s.osuser,
DECODE(l.block,
  0, 'Not Blocking',
  1, 'Blocking',
  2, 'Global') STATUS,
  DECODE(v.locked_mode,
    0, 'None',
    1, 'Null',
    2, 'Row-S (SS)',
    3, 'Row-X (SX)',
    4, 'Share',
    5, 'S/Row-X (SSX)',
    6, 'Exclusive', TO_CHAR(lmode)
  ) MODE_HELD
FROM gv$locked_object v, dba_objects d,
gv$lock l, gv$session s
WHERE v.object_id = d.object_id
AND (v.object_id = l.id1)
AND v.session_id = s.sid
ORDER BY username, session_id;

U_NAME  OBJ_OWNER  OBJECT_NAME      OBJECT_TYPE   STATUS MODE_HELD

SCHEMAB SchemaA    SCHEMA"A" object    TABLE  Row-X (SX)
schemab doesnot have any eread write privs on shema how come my above query shows schemb is locking schema A
0
 

Author Comment

by:ajaybelde
ID: 39953994
does AND v.session_id = s.sid  causing any confusion ?because in RAC  it may have same session sid on multiple nodes
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 39954025
>>> can we identify based on history views like active session history

not reliably

>>>  AND v.session_id = s.sid

if you are using the gv  views you need to also join on inst_id

try the query I posted above.  If you have table locks, it will tell you the exact rowid that is causing a lock
0
 

Author Comment

by:ajaybelde
ID: 39954764
sdstuber: does your script has to run all nodes?


and my out put is

 670
         1      19211 ACTIVE   enq: TM - contention                                             ORA_MON - oracle - server           ORA_MON.X                                                                                                                                                        AAAd0OAAAAAAAAAAAA
 lock table X in EXCLUSIVE mode
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39955006
>>> sdstuber: does your script has to run all nodes?

no, you run it on one, but the gv$ views query the other nodes for you.  That's how it detects if session on nodeA is blocking a session on nodeB


that output doesn't look something any query in this thread would have produced.  That might be due to the formatting by putting it in the text box or maybe you missed some output in a cut-and-paste
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question