Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

any script to find which user locking other user

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
ajaybelde
Asked:
ajaybelde
  • 6
  • 5
  • 4
2 Solutions
 
MikeOM_DBACommented:
0
 
ajaybeldeAuthor Commented:
do you have script wich already written which will list the sid beoing locked and owner of the objects and locking user
0
 
ajaybeldeAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
sdstuberCommented:
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
 
MikeOM_DBACommented:
Did you check out the script I posted?

Or look for another one here.
0
 
MikeOM_DBACommented:
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
 
sdstuberCommented:
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
 
MikeOM_DBACommented:
Excuse me: utllockt.sql,v 1.2 1995/05/05 10:16:23
Modified for Oracle version 7!
0
 
sdstuberCommented:
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
 
ajaybeldeAuthor Commented:
can we identify based on history views like active session history
0
 
ajaybeldeAuthor Commented:
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
 
ajaybeldeAuthor Commented:
does AND v.session_id = s.sid  causing any confusion ?because in RAC  it may have same session sid on multiple nodes
0
 
sdstuberCommented:
>>> 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
 
ajaybeldeAuthor Commented:
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
 
sdstuberCommented:
>>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now