Enqueue Waits: Locks Thanks To Doug Burns For Much of The Row Lock Example

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

Enqueue Waits : Locks

Thanks to Doug Burns for much of the


Row Lock example
Kyle Hailey
Kylehailey.com
kyle@delphix.com
Content
Part I : Row Locks
– enq: TX - row lock contention
• Data in ASH
• OEM
• V$lock & v$session
• ASH
• Find blocking SQL
Part II : Row Locks ??
– enq: TX - row lock contention, mode 4
Part III : Other TX locks
– enq: TX - allocate ITL entry
– enq: TX - index contention
– enq: TX – contention
Part IV : TM locks
Lock Types
• 9i
– One Wait : “enqueue” TX TM
• 10g Transaction Table Modification
– 208 enqueue waits
– Specific to each type of enqueue
– select name,wait_class from v$event_name where name like 'enq%'
enq: HW - contention Configuration
enq: SQ - contention Configuration
enq: SS - contention Configuration
enq: ST - contention Configuration
enq: TM - contention Application
enq: TW - contention Administrative
enq: TX - allocate ITL entry Configuration
enq: TX - index contention Concurrency
enq: TX - row lock contention Application
enq: TX – contention Application
Lock Modes
# Type Name
--- ------- ---------------------------
1 Null Null
2 SS Sub share
3 SX Sub exclusive
4 S Share
5 SSX Share/sub exclusive
6 X Exclusive
Lock Type and Mode
Select parameter1 from v$event_name where name like ‘enq%’;

Parameter1
----------
Name|mode
Select p1, p1raw from v$session where event like 'enq%';

P1 P1RAW
---------- --------
1415053318 54580006

Name: 5458 Mode: 0006


Hex Decimal ASCII
54 = 84 = “T” Lock = TX 6
58 = 88 = “X”
Type and Mode
SELECT
SELECT
chr(bitand(p1,-16777216)/16777215)||
chr(bitand(p1,-16777216)/16777215)||
chr(bitand(p1,
chr(bitand(p1, 16711680)/65535)
16711680)/65535)
TYPE,
TYPE,
mod(p1,16)
mod(p1,16) LMODE
LMODE
from
from v$session
v$session
where
where event
event like
like ‘enq%’;
‘enq%’;

TY
TY LMODE
LMODE
--
-- ----------
----------
TX
TX 66
enq: TX - row lock contention, mode 6

To Solve we need:

1. Waiter
2. Blocker
3. Blocking Object
4. Lock Type
– type
– mode

Missing : blocking SQL


– Possibly with log miner
OEM 10g

if P1 = 1415053318
then mode = 6
Then it is a data block
row lock
enq: TX - row lock contention
Mode 6, row in data block

User 1 User 2
SQL> delete from toto
where id = 1;
SQL> delete from toto
where id =1;

Table
ID Value
1 foo
Power of ASH :
v$active_session_history
• Waiter • SQL Waiting
– SESSION_ID
- SQL_ID
– SESSION_SERIAL#
– USER_ID • Blocker
• Object - BLOCKING_SESSION
– CURRENT_OBJ#
- BLOCKING_SESSION_STATUS
– CURRENT_FILE#
– CURRENT_BLOCK# - BLOCKING_SESSION_SERIAL#
• Lock Type and Mode
- Event = Type (name)
- P1 = Type | Mode

Missing: Blocking SQL !


Querying ASH
select
substr(event,0,20) lock_name,
ash.session_id waiter,
mod(ash.p1,16) lmode,
ash.p2 p2,
ash.p3 p3,
o.object_name object,
o.object_type otype,
CURRENT_FILE# filen,
CURRENT_BLOCK# blockn,
ash.SQL_ID waiting_sql,
BLOCKING_SESSION blocker
from
v$active_session_history ash,
all_objects o
where
event like 'enq: %'
and o.object_id (+)= ash.CURRENT_OBJ#
/
http://ashmasters.com
Other Data Sources

In “real time” can also use


– v$lock
– v$session (v$session_wait)
– dba_blockers
– dba_waiters
– ?/rdbms/admin/utllockt.sql
– http://www.evdbt.com/enqwaits.sql
Step one: get a lock

Session 1

Session_1> select pk_id from test_tab1


where rownum=1
for update;

PK_ID
----------
4051
Step 2: create a blocker

Session 2 -

Session_2> select pk_id from test_tab1


where pk_id = 4051
for update;

Hangs …
Step 3: release the lock and blocking
Session 1

Session_1> commit;

Commit complete.

Session 2 now continues


PK_ID
----------
4051
Locks in OEM:

Application

Wait
click

One SQL One Session


Click on “Application”

Click on
SQL ID
Click on SQL

SQL waits for lock


get’s lock and does I/O to complete
Click on “Session ID”

Click on
Session ID
No info is lock is gone

Limited Use : lock is already released


Session Before Lock released

Click

Blocker
Waiter
Holds lock
ASH data in EM, but missing blocking info

BLOCKING_SESSION_STATUS
BLOCKING_SESSION
BLOCKING_SESSION_SERIAL#
v$lock & v$session – during lock wait
SQL> select s.sid, s.state, s.event, s.sql_id, l.type, l.lmode
2 from v$session s,
s, v$lock l
3 where s.sid = l.sid (+)
4 and s.username='TESTUSER'
5 order by s.sid;

SID STATE EVENT SQL_ID TY LMODE


---------- ----------- -------------------------------- ------------- -- -----
150 WAITING enq: TX - row lock contention 0ut22yp7mh229 TM 3
150 WAITING enq: TX - row lock contention 0ut22yp7mh229 TX 0
157 WAITING SQL*Net message from client TX 6
157 WAITING SQL*Net message from client TM 3

Blocker Waiter, wants locks

Idle, holds locks


v$lock : after commit by Session 1
SQL> select s.sid, s.state, s.event, s.sql_id, l.type, l.lmode
2 from v$session s, v$lock l
3 where s.sid = l.sid (+)
4 and s.username='TESTUSER'
5 order by s.sid;

SID STATE EVENT SQL_ID TY LMODE


---------- ----------- -------------------------------- ------------- -- -----
150 WAITING SQL*Net message from client TM 3
150 WAITING SQL*Net message from client TX 6
157 WAITING SQL*Net message from client

Non-blocking, idle
Holds Locks
v$active_session_history – during and after
SQL> select
a.sample_time, a.session_id,
a.event, a.session_state, a.sql_id,
a.blocking_session BSID,
a.blocking_session_status BSTATUS
from v$active_session_history a,
dba_users u
where u.user_id = a.user_id
and u.username = 'TESTUSER';

TIME SID STATE EVENT SQL_ID BSID BSTATUS


-------- --- ------- ----------------------------- ------------- ---- ------------
14.59.37 150 ON_CPU 0ut22yp7mh229 NOT IN WAIT
14.59.36 150 ON_CPU 0ut22yp7mh229 NOT IN WAIT
14.59.35 150 WAITING db file scattered read 0ut22yp7mh229 NO HOLDER
14.59.34 150 WAITING db file scattered read 0ut22yp7mh229 NO HOLDER
14.59.33 150 WAITING enq: TX - row lock contention 0ut22yp7mh229 157 VALID
14.59.32 150 WAITING enq: TX - row lock contention 0ut22yp7mh229 157 VALID
14.59.31 150 WAITING enq: TX - row lock contention 0ut22yp7mh229 157 VALID
14.59.30 150 WAITING enq: TX - row lock contention 0ut22yp7mh229 157 VALID
14.59.29 150 WAITING enq: TX - row lock contention 0ut22yp7mh229 157 VALID

Time flow
Where is the BLOCKER, 157, info in ASH ? direction
V$active_session_history
SQL> select
NOTE:
a.sample_time, a.session_id,
SESSION_ID
a.event, a.session_state, a.sql_id,
a.blocking_session BSID, +
a.blocking_session_status BSTATUS
from v$active_session_history a, SESSION_SERIAL#
dba_users u
where u.user_id = a.user_id Is unique
and u.username = 'TESTUSER';

TIME SID STATE EVENT SQL_ID BSID BSTATUS


22.23.59 147 ON CPU 0ut22yp7mh229 NOT IN WAIT
22.23.58 147 WAITING enq: TX - row lock contention 0ut22yp7mh229 148 VALID
22.23.57 147 WAITING enq: TX - row lock contention 0ut22yp7mh229 148 VALID
22.23.56 147 WAITING enq: TX - row lock contention 0ut22yp7mh229 148 VALID
22.23.55 147 WAITING enq: TX - row lock contention 0ut22yp7mh229 148 VALID
22.23.33 148 WAITING db file scattered read cda14zb83bb5u NO HOLDER
22.23.32 148 WAITING db file scattered read cda14zb83bb5u NO HOLDER
22.23.31 148 WAITING db file scattered read cda14zb83bb5u NO HOLDER
22.23.30 148 WAITING db file scattered read cda14zb83bb5u NO HOLDER

SQL> select sql_text from v$sql where sql_id='cda14zb83bb5u


sql_id='cda14zb83bb5u’;
’;
SQL_TEXT
--------------------------------------------------------------------------------
select count(*) from test_tab1
How do we find the SQL that took the
lock?
Session B TX Lock
Undo
Wait for Tx Segment
update
update toto
toto set
set To commit Header
name
name == ‘ADAMS’
‘ADAMS’
where
where id
id == 1;
1; Undo
Segment
Table Toto
DataBlock
Data Block
Header
Header
Transaction 1
Session a
update
update toto
toto set
set
name
name == ‘SMITH’
‘SMITH’
Row 1 where
where id == 1;
Delete from1;
Delete id
from toto
toto where
where id
id
== 2;
2;
Delete from
Delete from toto
toto where
where id
id
Logminer : setup
SQL> select group#, thread#, status from v$log;

GROUP# THREAD# STATUS


---------- ---------- ----------------
1 1 CURRENT
2 1 ACTIVE
3 1 ACTIVE

SQL> select group#, member from v$logfile;

GROUP# MEMBER
---------- ------------------------------------------------------------
1 C:\ORACLE\PRODUCT\10.2.0\ORADATA\TEST1020\REDO01.LOG
2 C:\ORACLE\PRODUCT\10.2.0\ORADATA\TEST1020\REDO02.LOG
3 C:\ORACLE\PRODUCT\10.2.0\ORADATA\TEST1020\REDO03.LOG

SQL> exec dbms_logmnr.add_logfile('&&my_member');


Enter value for my_member: C:\ORACLE\PRODUCT\10.2.0\ORADATA\TEST1020\REDO01.LOG
PL/SQL procedure successfully completed.

SQL> exec dbms_logmnr.start_logmnr(options => dbms_logmnr.dict_from_online_catalog);


PL/SQL procedure successfully completed.
Logminer : setup locking
SQL> create table test_tab1
as select object_id pk_id, object_name from all_objects;

SQL> select rowid, object_name from test_tab1 where pk_id=313;


Future
Reference:
ROWID
ROWID OBJECT_NAME
------------------ ------------------------------
AAAN2NAAKAAAaBLAEX SYSTEM_PRIVILEGE_MAP

SESSION_1> update test_tab1 set object_name='SESSION 1' where pk_id=313; Create


SESSION_2> update test_tab1 set object_name='SESSION 2' where pk_id=313;
Lock
Wait
Logminer with XID
SQL> select xid, xidusn, xidslot, xidsqn
from v$transaction order by start_time;

XID XIDUSN XIDSLOT XIDSQN


-------------------- ---------- ---------- ---------- Only one transaction active on system, ours
0A00240064180000 10 36 6244
Normal use P2, P3 from wait:
'enq: TX - Row Lock Contention'
SQL> select username, session# sid, serial# , sql_redo P2=usn<<16||slot
from v$logmnr_contents P3=Sequence
where XID = '0A00240064180000'; => XID

USERNAME SID SERIAL# SQL_REDO


----------------- ---------- ---------- -----------------
UNKNOWN 0 0 set transaction read write;
UNKNOWN 0 0 update "SYS"."TEST_TAB1" set "OBJECT_NAME" =

'SESSION 1' where "OBJECT_NAME" =


'CLUSTER_NODES' and ROWID = 'AAAT5XAABAAAW7hAEr’;
UNKNOWN 0 0 commit;
Finding XID from ASH
Normal use P2, P3 from wait:
WAIT TYPE = 'enq: TX - Row Lock Contention’
Then
P2=usn<<16||slot
P3=Sequence
=> XID
Normal use P2, P3 from wait:
select 'enq: TX - Row Lock Contention'
distinct P2=usn<<16||slot
lpad(ltrim(to_char(p2,'XXXXXX')),6,'0')||'00'|| P3=Sequence
ltrim(to_char(mod(p3,256),'XX'))|| => XID
ltrim(to_char(trunc(p3/256),'XX'))||'0000'
block_xid,
to_char(p2,'XXXXXXXX') p2hex,
to_char(p3,'XXXXXXXX') p3hex,
trunc(p2/65536) usn,
mod(p2,65536) slot,
p3 sqn, xid wait_xid
from v$active_session_history
where event like 'enq: T%'
;
Logminer by USN and SLOT
SQL> select xid, xidusn, xidslot, xidsqn
from v$transaction order by start_time;
XID XIDUSN XIDSLOT XIDSQN
-------------------- ---------- ---------- ----------
0A00240064180000 10 36 6244

SYS@TEST1020> select distinct


username, session# sid, serial# , sql_redo
from v$logmnr_contents
where timestamp > sysdate- &minutes/(60*24)
and xidusn=10
and xidslt=36;

Enter value for minutes: 10

USERNAME SID SERIAL# SQL_REDO


----------------- ---------- ---------- -----------------
UNKNOWN 0 0 set transaction read write;
UNKNOWN 0 0 update "SYS"."TEST_TAB1" set "OBJECT_NAME" =

'SESSION 1' where "OBJECT_NAME" =


'CLUSTER_NODES' and ROWID = 'AAAT5XAABAAAW7hAEr’;
UNKNOWN 0 0 commit;
Dumping Redo Directly
SQL> select rowid, object_name from test_tab1 where pk_id=313;

ROWID OBJECT_NAME
------------------ ------------------------------
AAAN2NAAKAAAaBLAEX SYSTEM_PRIVILEGE_MAP

SYS@TEST1020> select
dbms_rowid.rowid_block_number('&&my_rowid') block_number,
dbms_rowid.rowid_to_absolute_fno('&&my_rowid','SYS','TEST_TAB1') file_id
from dual;

Enter value for my_rowid: AAAN2NAAKAAAaBLAEX

FILE_ID BLOCK_NUMBER
---------- ------------
10 106571
Data block row update
FILE_ID BLOCK_NUMBER
---------- ------------
10 106571

SYS@TEST1020> alter system dump logfile '&&my_member'


dba min 10 106571 dba max 10 106571;

C:\ORACLE\PRODUCT\10.2.0\ORADATA\TEST1020\REDO01.LOG

System altered

DUMP OF REDO FROM FILE 'C:\ORACLE\PRODUCT\10.2.0\ORADATA\TEST1020\REDO01.LOG' Opcodes


*.* DBAs: (file # 10, block # 106571) thru (file # 10, block # 106571) RBAs:
0x000000.00000000.0000 thru 0xffffffff.ffffffff.ffff SCNs: scn: 0x0000.00000000 thru
scn: 0xffff.ffffffff

REDO RECORD - Thread:1 RBA: 0x0000b3.00000009.0010 LEN: 0x0210 VLD: 0x0d SCN:
0x0000.008a42e6 SUBSCN: 1 04/20/2009 20:46:57 CHANGE #1 TYP:2 CLS: 1 AFN:10
DBA:0x0281a04b OBJ:56717 SCN:0x0000.008a4239 SEQ: 2 OP:11.19 KTB Redo op:
0x11 ver: 0x01 op: F xid: 0x000a.024.00001864 uba: 0x0080009d.031a.27

0A00240064180000

CLS: 1 - Data Class Block


OP:11.19 - Operation Update Multiple Rows.
Verify DBA and Table
REDO RECORD - Thread:1 RBA: 0x0000b3.00000009.0010 LEN: 0x0210 VLD: 0x0d SCN:
0x0000.008a42e6 SUBSCN: 1 04/20/2009 20:46:57 CHANGE #1 TYP:2 CLS: 1 AFN:10
DBA:0x0281a04b OBJ:56717 SCN:0x0000.008a4239 SEQ: 2 OP:11.19 KTB Redo op:
0x11 ver: 0x01 op: F xid: 0x000a.024.00001864 uba: 0x0080009d.031a.27

TESTUSER@TEST1020> select object_name, object_type


from user_objects
where object_id=56717;
OBJECT_NAME OBJECT_TYPE
------------------- -------------------
TEST_TAB1 TABLE

SYS@TEST1020> select to_char(dbms_utility.make_data_block_address(10,106571),


'XXXXXXXX') DBA from dual;
DBA
---------
281A04B
Update Value
“session 1”
Array Update of 1 rows:

col 1: [ 9] 53 45 53 53 49 4f 4e 20 30
CHANGE #3 TYP:2 CLS:1 AFN:1 DBA:0x00416ee1 OBJ:81495
SCN:0x0000.01052c55 SEQ:1 OP:11.19 ENC:0 RBL:0

col 1: [ 9] 53 45 53 53 49 4f 4e 20 32

“session 2”
Last Try: flashback
SELECT VERSIONS_XID
, VERSIONS_STARTTIME
, VERSIONS_ENDTIME
, VERSIONS_STARTSCN
, VERSIONS_ENDSCN
, VERSIONS_OPERATION
, object_name
FROM SYS.TEST_TAB1
VERSIONS BETWEEN
TIMESTAMP MINVALUE AND MAXVALUE
where VERSIONS_XID is not null
ORDER BY VERSIONS_STARTTIME
/
From ASH
BLOCK_XID P2HEX P3HEX USN SLOT SQN WAIT_XID
---------------- --------- --------- ---------- ---------- ---------- ----------------
0A00010072640000 A0001 6472 10 1 25714

From Flashback

VERSIONS_XID VERSIONS_STARTTIME ENDTIME STARTSCN ENDSCN V OBJECT_NAME


---------------- --------------------- ------- -------- ------- - --------------
0A00010072640000 15-OCT-13 06.46.30 PM 17042888 U SESSION 1
P1 = name | mode
select distinct parameter1 from v$event_name
where name like 'enq:%'

PARAMETER1
name|mode
select
event,
mod(p1,16) as "mode"
from v$active_session_history
where event like 'enq:%‘;

EVENT mode
enq: TX - row lock contention 6
enq: TX - row lock contention 4
enq: TX - row lock contention

• Mode 6 (exclusive)
– modification of a row lock

• Mode 4 (share)
– ??
enq: TX - row lock contention

if P1 = 1415053318
then mode = 6
Then it is a data block
row lock

Copyright 2006
TX – Mode 4

if P1 = 1415053316
then mode = 4
Not same data but conflicts

Copyright 2006
enq: TX - row lock contention
Mode 4, happens for 3 reasons
1. Unique key contention
2. Foreign Key contention
3. Bitmap index contention
(others?)
1. enq: TX - row lock contention
Mode 4 , unique index
User 1 User 2
create table p(n number);
create unique index p_i on p(n);
insert into p values(2);
insert into p values(2);

PK Table

ID ID Value
2? 2?
2. enq: TX - row lock contention
Mode 4, foreign key
User 1 User 2
create table parent (
id number primary key);
create table child (
id number references parent,
name varchar2(20));
insert into parent values (2);
insert into child values (2,88);
PK Parent Child
ID ID Value ID Name
2? 2? 2
3. enq: TX - row lock contention
Mode 4, bitmap
• Bitmaps are compressed
• Changes to the same bitmap cause locks

Value Start End Bitmap


Rowid Rowid
1 01010000111000011100001100
000.000.0000 000.000.000

2 01010000111000011100001100
000.000.0000 000.000.000
3. enq: TX - row lock contention
Mode 4, foreign key
Session 1
create
createtable
tablet1t1(( Different rows but
n1
n1 number(4),
number(4), same key value
row number(4));
row number(4));
insert
insert into
intot1
t1
select
select1,1,rownum
rownum
from
from all_objects
all_objects
where
whererownum
rownum <= <= 400;
400;
commit;
commit;
create
createbitmap
bitmapindex
indexi1
i1on
ont1(n1);
t1(n1);
update
updatet1 t1set
set n1
n1==22
where
whererow=row=12;12; Session 2
update
updatet1
t1set
set n1
n1==22
where
whererow=
row=13;
13;
3. enq: TX - row lock contention
Mode 4, foreign key
• Bitmaps are compressed
• Changes to the same bitmap chunk cause locks
Value Start End Bitmap
Rowid Rowid
1 01010000111000011100001100
200.0 204.7
1 01010000111000011100001100
205.0 210.3
2 01010000111000011100001100
200.0 205.6

block row
3. enq: TX - row lock contention
Mode 4, foreign key

Value Start End Bitmap


Rowid Rowid
1 01010000111000011100001100
200.0 204.7
2 01010000111000011100001100
205.0 210.3
3 01010000111000011100001100
200.0 205.6

Session 1 Session 2
Update id=12 Update id=13
set value 2 set value 2
Summary: TX 4 from ASH
uniq index Mode 4, unique key
ST EVENT SID LM P2 P3 OBJ OTYPE FN BLOCKN SQL_ID BSID
----- ---------------------- --- --- ------ ---- ----- ----- --- ------ --
10:39 enq: TX - row lock c 141 4 655406 6672 -1 0 0 bjvx94vnxtxgv 158
10:39 enq: TX - row lock c 141 4 655406 6672 -1 0 0 bjvx94vnxtxgv 158
10:39 enq: TX - row lock c 141 4 655406 6672 -1 0 0 bjvx94vnxtxgv 158
10:39 enq: TX - row lock c 141 4 655406 6672 -1 0 0 bjvx94vnxtxgv 158

FK (10.2.0.3)
Mode 4, foreign key
ST EVENT SID LM P2 P3 OBJ OTYPE FN BLOCKN SQL_ID BSID
----- ---------------------- --- --- ------ ---- ----- ----- --- ------ --
10:41 enq: TX - row lock c 144 4 179681 7074 CHILD TABLE 1 60954 ahm7c9rupbz9r 1
10:41 enq: TX - row lock c 144 4 179681 7074 CHILD TABLE 1 60954 ahm7c9rupbz9r 1
10:41 enq: TX - row lock c 144 4 179681 7074 CHILD TABLE 1 60954 ahm7c9rupbz9r 1

bitmap
Mode 4, bitmap
ST EVENT SID LM P2 P3 OBJ OTYPE FN BLOCKN SQL_ID BSID
----- ---------------------- --- --- ------ ---- ----- ----- --- ------ --
10:41 enq: TX - row lock c 143 4 966081 4598 I1 INDEX 0 0 azav296xxqcjx 144
10:41 enq: TX - row lock c 143 4 966081 4598 I1 INDEX 0 0 azav296xxqcjx 144
10:41 enq: TX - row lock c 143 4 966081 4598 I1 INDEX 0 0 azav296xxqcjx 144
10:41 enq: TX - row lock c 143 4 966081 4598 I1 INDEX 0 0 azav296xxqcjx 144

Copyright 2006 Kyle Hailey


Specific ‘enq: TX’ wait events
• enq: TX - allocate ITL entry
– ITL space waits
• enq: TX - index contention
– Index block split
• Unusual enq: TX – contention
– Alter tablespace … read only;
– Free Lists slot waits
– Possible with two phase commit
enq: TX - allocate ITL entry

Transaction 1 Info
DataBlock
Data Block
Transaction 2 Info
Header
Header
ITL

Data

Copyright 2006
enq: TX - allocate ITL entry

DataBlock
Data Block
Header
Header
Transaction 1 Transaction 3
Transaction 2
Row 3
Row 2
Data
Row 1

Copyright 2006
enq: TX - allocate ITL entry
create
create table
table itl
itl ((
id
id number,
number,
data
data varchar2(20)
varchar2(20)
))
pctfree
pctfree 00
initrans
initrans 11
;;
insert
insert into
into itl
itl select
select rownum,'a'
rownum,'a' from
from all_objects
all_objects
where
where rownum
rownum << 2000;
2000;
commit;
commit;
session
session 1:
1: update
update itl
itl set
set data=data
data=data where
where id=1;
id=1;
session
session 2:
2: update
update itl
itl set
set data=data
data=data where
where id=2;
id=2;
session
session 3:
3: update
update itl
itl set
set data=data
data=data where
where id=3;
id=3;
session
session 4:
4: update
update itl
itl set
set data=data
data=data where
where id=4;
id=4;
session
session 5:
5: update
update itl
itl set
set data=data
data=data where
where id=5;
id=5;
enq: TX - contention
1. Altering tablespace read only with open transaction
Example
– Session 1 – start transaction, don’t commit
– Session 2 – alter tablespace read only
1. Data File Extension – waiter waiting for another session to extend
file
enq: TM - contention

• TX locks have a corresponding TM lock


• TM locks the structure from change

 Parameter1 = object id
LOCK
LOCK Parmeter1
Parmeter1 Parmeter2(ID1)
Parmeter2(ID1) Parameter3(ID2)
Parameter3(ID2)
-------
------- ---------
--------- -------------
------------- ---------------
---------------
enq:
enq: TM
TM name|mode
name|mode object
object ## table/partition
table/partition
enq: TM - contention
Exclusive Row Level Lock

User 1 User 2
create table parent (
id number primary key);
create table child (
id number references parent,
name varchar2(20));
insert into parent values (1);
insert into parent values (2);
commit;
insert into child values (1,2);
delete from parent where id=2; No commit
enq: TM - contention
PK Parent Child
ID ID Value ID Name
1 1 1
X
2 X
2

Session 1 Insert into Child ID=1

Session 2 Delete from Parent where ID=2 :


Enqueue TM 4
Session 2 doesn’t know the value Session 1 inserted
Session 2 only knows there is an outstanding change
enq: TM – contention Solution
PK Parent Child
Index
ID ID Value ID ID Name
1 1
2

Foreign Key

Session 1: Insert into Child ID=1


Session 2: Delete from Parent ID=2
OK – can verify quickly in the child index
Without TM Locks

Child Delete
Parent
ID Name
1
2 Delete
3 Child
Parent
4
… Delete ID Name
10000 1
Child 2
3
4
Could scan Child table for value “2” and …
someone could come in behind and delete
value 2 in an area that is already scanned 10000
but before the scan has finished. The TM
lock prevents this
TM Further Investigation
select
event,
sql_id,
mod(p1,16) as "mode",
p2|| ' ' || o.name obj
from v$active_session_history ash,
obj$ o
where
event like 'enq: TM%'
and o.obj# (+)= ash.p2
order by sample_time;
EVENT SQL_ID mode OBJ
enq: TM - contention 8zw36yw3fq4yy 4 53372 CHILD
enq: TM - contention 8zw36yw3fq4yy 4 53372 CHILD
enq: TM - contention 8zw36yw3fq4yy 4 53372 CHILD
enq: TM - contention 8zw36yw3fq4yy 4 53372 CHILD
enq: TM - contention 8zw36yw3fq4yy 4 53372 CHILD
enq: TM - contention 8zw36yw3fq4yy 4 53372 CHILD
Other Resources

@?/rdbms/admin/utllockt
WAITING_SESSION LOCK_TYPE MODE_REQUESTED MODE_HELD LOCK_ID1 LOCK_ID2
--------------- ----------- -------------- --------- --------- --------
144 None
139 Transaction Share Exclusive 131113 7507
Blocking Sessions
Content
Part I : Row Locks
– enq: TX - row lock contention
• Data in ASH
• OEM
• V$lock & v$session
• ASH
• Find blocking SQL
Part II : Row Locks ??
– enq: TX - row lock contention, mode 4
Part III : Other TX locks
– enq: TX - allocate ITL entry
– enq: TX - index contention
– enq: TX – contention
Part IV : TM locks

You might also like