Professional Documents
Culture Documents
Enqueue Waits: Locks Thanks To Doug Burns For Much of The Row Lock Example
Enqueue Waits: Locks Thanks To Doug Burns For Much of The Row Lock Example
Enqueue Waits: Locks Thanks To Doug Burns For Much of The Row Lock Example
Parameter1
----------
Name|mode
Select p1, p1raw from v$session where event like 'enq%';
P1 P1RAW
---------- --------
1415053318 54580006
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
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
Session 1
PK_ID
----------
4051
Step 2: create a blocker
Session 2 -
Hangs …
Step 3: release the lock and blocking
Session 1
Session_1> commit;
Commit complete.
Application
Wait
click
Click on
SQL ID
Click on SQL
Click on
Session ID
No info is lock is gone
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;
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 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';
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
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;
FILE_ID BLOCK_NUMBER
---------- ------------
10 106571
Data block row update
FILE_ID BLOCK_NUMBER
---------- ------------
10 106571
C:\ORACLE\PRODUCT\10.2.0\ORADATA\TEST1020\REDO01.LOG
System altered
0A00240064180000
“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
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
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
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
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
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
Foreign Key
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