Download - Lock 09 Enqueues

Transcript
Page 1: Lock 09 Enqueues

Enqueue Waits : Locks

Page 2: Lock 09 Enqueues

#.2

Copyright 2006 Kyle Hailey

Locks

REDOREDO Lib Lib CacheCache

Buffer Buffer CacheCache

IOIO

LocksLocks

NetworkNetwork

Page 3: Lock 09 Enqueues

#.3

Copyright 2006 Kyle Hailey

Enqueues = Locks

Waits

Disk I/O

Library Cache

Enqueue

Undo

TX - row lock contention

TX - allocate ITL entry

HWRedo

Buffer Cache

SQL*Net

TM

ST

TS

TX - index contention

US

CI

SQ

Locks Locks

Come inCome in

ManyMany

TypesTypes

Page 4: Lock 09 Enqueues

#.4

Copyright 2006 Kyle Hailey

Locks Covered in this Section

Part I TM – table modification TX – Transaction locks UL – user lock

Part II CI – Cross Instance CU – Cursor Bind HW – High Water RO – Reusable Object ST – Space Transaction TS – Temporary Space

Page 5: Lock 09 Enqueues

#.5

Copyright 2006 Kyle Hailey

Locks Can be Intractable

Lock seems easy compared to latches or other esoteric waits, but they can be frustratingly intractable

Statspack & AWR lacks enough data to solve To Solve, you need either

Real timeASH

Insolvable: No dependable way to find the blocking SQL

Page 6: Lock 09 Enqueues

#.6

Copyright 2006 Kyle Hailey

Solving Enqueues Need SQL and Object/row

Statspack fails V$active_session_history succeeds

In “real time” can also usev$lock v$session dba_blockers dba_waiters ?/rdbms/admin/utllockt.sqlhttp://www.evdbt.com/enqwaits.sql

Page 7: Lock 09 Enqueues

#.7

Copyright 2006 Kyle Hailey

Statspack 9iTop 5 Timed Events Avg %Total

~~~~~~~~~~~~~~~~~~ wait CallEvent Waits Time (s) (ms) Time-------------------------- ------------ ----------- ------ ------Enqueue 42 126 3000 96.5CPU time 4 2.8db file sequential read 165 1 4 .4control file sequential read 214 0 1 .1log file switch completion 2 0 40 .1

Top 5 Timed Events Avg %Total

~~~~~~~~~~~~~~~~~~ wait CallEvent Waits Time (s) (ms) Time-------------------------- ------------ ----------- ------ ------Enqueue 42 126 3000 96.5CPU time 4 2.8db file sequential read 165 1 4 .4control file sequential read 214 0 1 .1log file switch completion 2 0 40 .1

Statspack is INSUFFICIENT, missingStatspack is INSUFFICIENT, missingWho is blockedWho is blockedWho BlocksWho BlocksWhat blocksWhat blocksHow they are blockedHow they are blocked

Page 8: Lock 09 Enqueues

#.8

9i Investigation

Select * from v$event_name where name=‘enqueue’;

Copyright 2006 Kyle Hailey

Parameter1 parameter2 parameter3---------- ---------- ----------

Name|mode id1 id2

Page 9: Lock 09 Enqueues

#.9

Copyright 2006 Kyle Hailey

9i Enqueue P1: Type and Mode

Type: 5458

P1 P1RAW

---------- --------

1415053318 54580006

Hex Decimal ASCII54 = 84 = “T”58 = 88 = “X”

Lock = TX 6

Mode: 0006

Page 10: Lock 09 Enqueues

#.10

9i Translating Lock and Mode

SELECT

chr(bitand(p1,-16777216)/16777215)|| chr(bitand(p1, 16711680)/65535) “Type",

mod(p1,16) as “Mode"

from v$session_wait

Where event=‘enqueue’

/

SELECT

chr(bitand(p1,-16777216)/16777215)|| chr(bitand(p1, 16711680)/65535) Type, mod(p1,16) lmodefrom v$session_wait where event=‘enqueue’;

TY LMODE-- ----------TX 6

SELECT

chr(bitand(p1,-16777216)/16777215)|| chr(bitand(p1, 16711680)/65535) Type, mod(p1,16) lmodefrom v$session_wait where event=‘enqueue’;

TY LMODE-- ----------TX 6

Page 11: Lock 09 Enqueues

#.11

Copyright 2006 Kyle Hailey

10g Translates all the Enquques

9i One Wait : “enqueue”

10g 208 enqueue waitsSpecific to each type of enqueue

enq: HW - contention enq: HW - contention ConfigurationConfigurationenq: SQ - contention enq: SQ - contention ConfigurationConfigurationenq: SS - contention enq: SS - contention ConfigurationConfigurationenq: ST - contention enq: ST - contention

ConfigurationConfigurationenq: TM - contention enq: TM - contention Application Application enq: TW - contention enq: TW - contention

AdministrativeAdministrativeenq: TX - allocate ITL entry enq: TX - allocate ITL entry

ConfigurationConfigurationenq: TX - index contention enq: TX - index contention ConcurrencyConcurrencyenq: TX - row lock contention enq: TX - row lock contention ApplicationApplicationenq: TX – contention enq: TX – contention ApplicationApplication

Page 12: Lock 09 Enqueues

#.12

Version 9 : P2 and P3

ID1 and ID2 had different meanings for each lock type Never defined

Defined in 10g Parameter 2 = ID1 Parameter 3 = ID2

Copyright 2006 Kyle Hailey

Parameter1 parameter2 parameter3---------- ---------- ----------

Name|mode id1 id2

Page 13: Lock 09 Enqueues

#.13

Copyright 2006 Kyle Hailey

Common Lock Types

Wait Event Parameter2 Parameter3enq: CI - contention opcode typeenq: CF - contention 0 operationenq: CU - contention handle handleenq: HW - contention table space # blockenq: IM - contention for blr pool # 0enq: RO - contention 2 0enq: RO - fast object reuse 2 0enq: SQ - contention object # 0enq: ST - contention 0 0enq: TM - contention object # table/partitionenq: TS - contention tablespace ID dbaenq: TX - allocate ITL entry usn<<16 | slot sequenceenq: TX - contention usn<<16 | slot sequenceenq: TX - index contention usn<<16 | slot sequenceenq: TX - row lock contention usn<<16 | slot sequenceenq: UL - contention id 0enq: US - contention undo segment # 0

Select event, parameter2 , parameter3 from v$event_nameSelect event, parameter2 , parameter3 from v$event_name

Page 14: Lock 09 Enqueues

#.14

Copyright 2006 Kyle Hailey

Statspack 10g5 Timed Events Avg %Total~~~~~~~~~~~~~~~~~~ wait CallEvent Waits Time (s) (ms) Time----------------------------- ------ -------- ------ ------enq: TX - row lock contention 42 126 3000 96.5CPU time 4 2.8db file sequential read 165 1 4 .4control file sequential read 214 0 1 .1log file switch completion 2 0 40 .1

5 Timed Events Avg %Total~~~~~~~~~~~~~~~~~~ wait CallEvent Waits Time (s) (ms) Time----------------------------- ------ -------- ------ ------enq: TX - row lock contention 42 126 3000 96.5CPU time 4 2.8db file sequential read 165 1 4 .4control file sequential read 214 0 1 .1log file switch completion 2 0 40 .1

10g Better, tells what kind of lock10g Better, tells what kind of lock

But still not But still not

who is blocked, who is blocked,

who blocks themwho blocks them

what they are blocked onwhat they are blocked on

Page 15: Lock 09 Enqueues

#.15

Copyright 2006 Kyle Hailey

V$active_session_historySucceeds Who is waiting

SESSION_ID SESSION_SERIAL# USER_ID

On what object CURRENT_OBJ# CURRENT_FILE# CURRENT_BLOCK#

With what SQL SQL_ID

Who is the blocker BLOCKING_SESSION BLOCKING_SESSION_STATUS BLOCKING_SESSION_SERIAL#

What is the Blocker SQL not reliably possible Maybe by dumping REDO

OEM externalize all of thisOEM externalize all of this

Page 16: Lock 09 Enqueues

#.16

Copyright 2006 Kyle Hailey

OEM 10g

if P1 = 1415053318

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

Page 17: Lock 09 Enqueues

#.17

Copyright 2006 Kyle Hailey

10g: find the lock mode P1 (parameter1) same for all locks

select distinct parameter1 from v$event_name select distinct parameter1 from v$event_name

where name like 'enq:%'where name like 'enq:%'

PARAMETER1PARAMETER1

--------------------

name|modename|mode

select distinct event, p1, to_char(p1,'XXXXXXXX') p1rawselect distinct event, p1, to_char(p1,'XXXXXXXX') p1raw

from v$active_session_history where event like 'enq:%‘;from v$active_session_history where event like 'enq:%‘;

EVENT P1 P1RAWEVENT P1 P1RAW

------------------------------ ---------- --------------------------------------- ---------- ---------

enq: TX - row lock contention 1415053316 54580004enq: TX - row lock contention 1415053316 54580004enq: TX - row lock contention 1415053318 54580006enq: TX - row lock contention 1415053318 54580006enq: TX - allocate ITL entry 1415053316 54580004enq: TX - allocate ITL entry 1415053316 54580004

Page 18: Lock 09 Enqueues

#.18

Copyright 2006 Kyle Hailey

P1 = name | mode P1 (parameter1) same for all locks

select distinct parameter1 from v$event_name select distinct parameter1 from v$event_name

where name like 'enq:%'where name like 'enq:%'

selectselect distinct event, distinct event, p1,p1, mod(p1,16) as "mode"mod(p1,16) as "mode" from v$active_session_historyfrom v$active_session_history where event like 'enq:%‘;where event like 'enq:%‘;

EVENT P1 modeEVENT P1 mode

----------------------------- ---------- --------------------------------- ---------- ----

enq: TX - allocate ITL entry 1415053316 4enq: TX - allocate ITL entry 1415053316 4enq: TX - row lock contention 1415053318 6enq: TX - row lock contention 1415053318 6enq: TX - row lock contention 1415053316 4enq: TX - row lock contention 1415053316 4

PARAMETER1PARAMETER1

--------------------

name|modename|mode

Page 19: Lock 09 Enqueues

#.19

Copyright 2006 Kyle Hailey

Mode Cheat Sheet

TX

if P1 = 1415053316

then mode = 4

if P1 = 1415053318

then mode = 6

Page 20: Lock 09 Enqueues

#.20

Copyright 2006 Kyle Hailey

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

Page 21: Lock 09 Enqueues

#.21

Copyright 2006 Kyle Hailey

Transaction Locks (TX)

TX = Transaction = Wait on UNDO Mode 6

modification of a row lockMode 4

Index block spiltUnique Index Key enforcementForeign key enforcement ITL space waitsBitmap chunk conflictsAlter tablespace … read only;Free Lists slot waitsPossible with two phase commit

Page 22: Lock 09 Enqueues

#.22

Copyright 2006 Kyle Hailey

Transaction Locks (TX)9i Only had one TX lock, but 10g has 4:

1.enq: TX - allocate ITL entry Wait on an ITL slot

2.enq: TX - index contention Index block split

3.enq: TX - row lock contention Mode 6 – classic row lock Mode 4 - pk violation, fk violation, bitmap chunk

wait4.enq: TX – contention

Wait for a data file extension, Alter tbs read only

Page 23: Lock 09 Enqueues

#.23

Copyright 2006 Kyle Hailey

TX Lock

update toto set name = ‘SMITH’ where id = 1;

update toto set name = ‘SMITH’ where id = 1;

Undo Undo SegmentSegment

Session aSession a

Data Block Data Block HeaderHeader

Transaction 1Transaction 1

Data Block Data Block HeaderHeader

Row 1Row 1

Table TotoTable Toto

Undo Undo SegmentSegmentHeaderHeader

Session BSession B

update toto set name = ‘ADAMS’ where id = 1;

update toto set name = ‘ADAMS’ where id = 1;

Wait for TxWait for TxTo commitTo commit

Delete from toto where id = 2;

Delete from toto where id = 2;Delete from toto where id = 9;

Delete from toto where id = 9;

Page 24: Lock 09 Enqueues

#.24

Copyright 2006 Kyle Hailey

enq: TX - row lock contention

User 1

SQL> delete from toto where id = 1;

User 2

SQL> delete from toto where id =1;--hang

Mode 6, row in data block – only one reason

SQL> Delete from titi Delete from titi

Where id = 1;Where id = 1;

Page 25: Lock 09 Enqueues

#.25

Copyright 2006 Kyle Hailey

enq: TX - row lock contention

if P1 = 1415053318

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

Page 26: Lock 09 Enqueues

#.26

Copyright 2006 Kyle Hailey

enq: TX - row lock contention

TableTable

ValueValueIDID

foofoo

Session 1: delete where id = 1; Session 1: delete where id = 1; no commitno commit

Session 2: delete where id = 1 ;Session 2: delete where id = 1 ;

Enqueue TX 6 Enqueue TX 6

11

TX Mode 6TX Mode 6

Page 27: Lock 09 Enqueues

#.27

Copyright 2006 Kyle Hailey

TX – Mode 4

if P1 = 1415053316

then mode = 4Not same data but conflicts

Page 28: Lock 09 Enqueues

#.28

enq: TX - row lock contention

Mode 4, happens for 3 reasons

1. Unique key contention

2. Foreign Key contention

3. Bitmap index contention

(others?)

Copyright 2006 Kyle Hailey

Page 29: Lock 09 Enqueues

#.29

Copyright 2006 Kyle Hailey

1. enq: TX - row lock contention

User 1

create table p(n number);

create unique index p_i on p(n);insert into p values(3);

User 2

insert into p values(3);

--hang

Mode 4 , unique index

Page 30: Lock 09 Enqueues

#.30

Copyright 2006 Kyle Hailey

1. enq: TX - row lock contentionTableTable

IDID ValueValueIDID

PKPK

Session 1: Insert key Session 1: Insert key no commitno commit

2 ? 2 ? 2 ? 2 ?

Session 2: Insert into same key :Session 2: Insert into same key :

Enqueue TX 4 Enqueue TX 4

22Mode 4 , unique index

Page 31: Lock 09 Enqueues

#.31

Copyright 2006 Kyle Hailey

2. enq: TX - row lock contention

User 8create table parent ( id number primary key); create table child ( id number references parent, name varchar2(20)); insert into parent values (2);

User 10

insert into child values (2,88);

-- hang

Mode 4, foreign key

Page 32: Lock 09 Enqueues

#.32

Copyright 2006 Kyle Hailey

2. enq: TX - row lock contention

ParentParent ChildChild

IDID IDID NameNameValueValueIDID

PKPK

Session 1: Insert key into parentSession 1: Insert key into parent

no commitno commit

2 ? 2 ? 2 ? 2 ? 2 2

Foreign KeyForeign Key

Session 2: Insert into child same key :Session 2: Insert into child same key : Enqueue TX 4 Enqueue TX 4

TX 4 - Foreign Key

Page 33: Lock 09 Enqueues

#.33

Copyright 2006 Kyle Hailey

3. enq: TX - row lock contention Mode 4 Bitmaps are compressed Changes to the same bitmap cause locks

ValueValue Start Start RowidRowid

EndEndRowidRowid

Bitmap Bitmap

11 000.000.0000000.000.0000

000.000.000000.000.000

01010000111000011100001100 01010000111000011100001100 22

000.000.0000000.000.0000

000.000.000000.000.000

01010000111000011100001100 01010000111000011100001100

Page 34: Lock 09 Enqueues

#.34

Copyright 2006 Kyle Hailey

3. enq: TX - row lock contention

update t1 set update t1 set n1 = 2n1 = 2 where n2 = 13;where n2 = 13;

update t1 set update t1 set n1 = 2n1 = 2 where n2 = 13;where n2 = 13;

Session 2Session 2

Different rows but Different rows but same key value same key value

create table t1 (create table t1 (n1n1 number(4),number(4),n2n2 number(4));number(4));

insert into t1insert into t1 select 1, rownumselect 1, rownum from all_objectsfrom all_objects where rownum <= 400;where rownum <= 400;commit;commit;create bitmap index i1 on t1(n1);create bitmap index i1 on t1(n1);

create table t1 (create table t1 (n1n1 number(4),number(4),n2n2 number(4));number(4));

insert into t1insert into t1 select 1, rownumselect 1, rownum from all_objectsfrom all_objects where rownum <= 400;where rownum <= 400;commit;commit;create bitmap index i1 on t1(n1);create bitmap index i1 on t1(n1);

update t1 set update t1 set n1 = 2n1 = 2 where n2 = 12;where n2 = 12;

update t1 set update t1 set n1 = 2n1 = 2 where n2 = 12;where n2 = 12;

Session 1Session 1

Page 35: Lock 09 Enqueues

#.35

Copyright 2006 Kyle Hailey

3. enq: TX - row lock contention

Bitmaps are compressed Changes to the same bitmap chunk cause

locksValueValue Start Start

RowidRowidEndEndRowidRowid

Bitmap Bitmap

11 200.0200.0

204.7204.7

01010000111000011100001100 01010000111000011100001100 11

205.0205.0 210.3210.3

01010000111000011100001100 01010000111000011100001100 22

200.0200.0 205.6205.6

01010000111000011100001100 01010000111000011100001100

blockblock rowrow

Page 36: Lock 09 Enqueues

#.36

Copyright 2006 Kyle Hailey

ValueValue Start Start RowidRowid

EndEndRowidRowid

Bitmap Bitmap

11 200.0200.0

204.7204.7

01010000111000011100001100 01010000111000011100001100 22

205.0205.0 210.3210.3

01010000111000011100001100 01010000111000011100001100 33

200.0200.0 205.6205.6

01010000111000011100001100 01010000111000011100001100

3. enq: TX - row lock contention

Session 1Session 1 Session 2Session 2Update id=800 Update id=800 set value 2set value 2 Update id=3Update id=3

set value 2set value 2

Page 37: Lock 09 Enqueues

#.37

Summary: TX 4 from ASHuniq index

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)

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

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

Page 38: Lock 09 Enqueues

#.38

Copyright 2006 Kyle Hailey

enq: TX - allocate ITL entry

Data Block Data Block HeaderHeader

ITLITL

DataData

Data Block Data Block HeaderHeader

Transaction 1 InfoTransaction 1 Info

Transaction 2 InfoTransaction 2 Info

Page 39: Lock 09 Enqueues

#.39

Copyright 2006 Kyle Hailey

enq: TX - allocate ITL entry

Data Block Data Block HeaderHeader

Transaction 1Transaction 1

DataData

Data Block Data Block HeaderHeader

Transaction 2Transaction 2

Row 1Row 1Row 2Row 2Row 3Row 3

Transaction 3Transaction 3

Page 40: Lock 09 Enqueues

#.40

Copyright 2006 Kyle Hailey

enq: TX - allocate ITL entrycreate table itl ( id number, data varchar2(20) ) pctfree 0 initrans 1 ; insert into itl select rownum,'a' from all_objects where rownum < 2000; commit;

session 1: update itl set data=data where id=1; session 2: update itl set data=data where id=2; session 3: update itl set data=data where id=3; session 4: update itl set data=data where id=4; session 5: update itl set data=data where id=5;

create table itl ( id number, data varchar2(20) ) pctfree 0 initrans 1 ; insert into itl select rownum,'a' from all_objects where rownum < 2000; commit;

session 1: update itl set data=data where id=1; session 2: update itl set data=data where id=2; session 3: update itl set data=data where id=3; session 4: update itl set data=data where id=4; session 5: update itl set data=data where id=5;

Page 41: Lock 09 Enqueues

#.41

Copyright 2006 Kyle Hailey

enq: TX - contention

Example Session 1 – start transaction, don’t commit Session 2 – alter tablespace read onlyExampleData File Extension – waiter waitng for another session to extend fileIndex Block Split – waiter waiting for another session to split the block

Others ? Free Lists

Non-ASSM Sessions freeing block If no txs free lists available (min 16, grow up depending on block size) , pick one and wait TX 4

2PC – two phase commit First does a prepare the commit Any read or write access in the intervening time waits TX 4

Page 42: Lock 09 Enqueues

#.42

Copyright 2006 Kyle Hailey

TX Further Investigation

EVENT SQL_ID OBJ FILE# BLOCK#EVENT SQL_ID OBJ FILE# BLOCK#

------------------------------ ------------- --------- --- ------------------------------------ ------------- --------- --- ------

enq: TX - row lock contention ahm7c9rupbz9r 53363 FOO 1 123586enq: TX - row lock contention ahm7c9rupbz9r 53363 FOO 1 123586enq: TX - row lock contention bjvx94vnxtxgv 53363 FOO 1 123586enq: TX - row lock contention bjvx94vnxtxgv 53363 FOO 1 123586enq: TX - row lock contention ahm7c9rupbz9r 53363 FOO 1 123586enq: TX - row lock contention ahm7c9rupbz9r 53363 FOO 1 123586enq: TX - row lock contention bjvx94vnxtxgv 53363 FOO 1 123586enq: TX - row lock contention bjvx94vnxtxgv 53363 FOO 1 123586enq: TX - row lock contention ahm7c9rupbz9r 53363 FOO 1 123586enq: TX - row lock contention ahm7c9rupbz9r 53363 FOO 1 123586

select event, sql_id,select event, sql_id, CURRENT_OBJ# || ' ' || name objCURRENT_OBJ# || ' ' || name obj ,CURRENT_FILE# file#,CURRENT_FILE# file# ,CURRENT_BLOCK# block#,CURRENT_BLOCK# block# from v$active_session_history ash,from v$active_session_history ash, obj$ oobj$ o wherewhere event like 'enq: TX%'event like 'enq: TX%' and o.obj# (+)= ash.current_obj#and o.obj# (+)= ash.current_obj# order by sample_timeorder by sample_time

Page 43: Lock 09 Enqueues

#.43

Copyright 2006 Kyle Hailey

TX Further Investigation

V$active_session_history :V$active_session_history : BLOCKING_SESSIONBLOCKING_SESSION BLOCKING_SESSION_STATUSBLOCKING_SESSION_STATUS BLOCKING_SESSION_SERIAL#BLOCKING_SESSION_SERIAL#

Who is the blocker:Who is the blocker:

No Guarentee of finding blocker SQL No Guarentee of finding blocker SQL

Page 44: Lock 09 Enqueues

#.44

Copyright 2006 Kyle Hailey

enq: TM - contention

TX locks have a corresponding TM lock TM locks the structure from change

LOCK Parmeter1 Parmeter2(ID1) Parameter3(ID2)------- --------- ------------- --------------- enq: TM name|mode object # table/partition

LOCK Parmeter1 Parmeter2(ID1) Parameter3(ID2)------- --------- ------------- --------------- enq: TM name|mode object # table/partition

Parameter1 = object id

Page 45: Lock 09 Enqueues

#.45

Copyright 2006 Kyle Hailey

enq: TM - contention

User 1create 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;

delete from parent where id=2;

User 2

insert into child values (1,2);

Exclusive Row Level Lock

Page 46: Lock 09 Enqueues

#.46

Copyright 2006 Kyle Hailey

enq: TM - contentionParentParent ChildChild

IDID IDID NameNameValueValueIDID

PKPK

Delete from Parent where ID=2 :Delete from Parent where ID=2 :

11 11 11

Insert into Child ID=1Insert into Child ID=1

Enqueue TM 4 Enqueue TM 4

Session 2 doesn’t know the value Session 1 insertedSession 2 doesn’t know the value Session 1 insertedSession 2 only knows there is an outstanding changeSession 2 only knows there is an outstanding change

XX XX

Session 1Session 1

Session 2Session 2

22 22

Page 47: Lock 09 Enqueues

#.47

Copyright 2006 Kyle Hailey

enq: TM – contentionSolution

ParentParent ChildChild

IDID IDID NameNameValueValueIDID

PKPK

Session 1: Insert into Child ID=1Session 1: Insert into Child ID=1

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

IDID

IndexIndex

Foreign KeyForeign Key

112211

Page 48: Lock 09 Enqueues

#.48

Copyright 2006 Kyle Hailey

TM Further Investigation

EVENT SQL_ID mode OBJEVENT SQL_ID mode OBJ

------------------------------ ------------- ---- --------------------------------------------- ------------- ---- ---------------

enq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILDenq: TM - contention 8zw36yw3fq4yy 4 53372 CHILD

selectselect event,event, sql_id,sql_id, mod(p1,16) as "mode",mod(p1,16) as "mode", p2|| ' ' || o.name objp2|| ' ' || o.name obj from v$active_session_history ash,from v$active_session_history ash, obj$ oobj$ o wherewhere event like 'enq: TM%'event like 'enq: TM%' and o.obj# (+)= ash.p2and o.obj# (+)= ash.p2 order by sample_time;order by sample_time;

Page 49: Lock 09 Enqueues

#.49

Copyright 2006 Kyle Hailey

UL Locks

User-defined Locksdbms_lock

Wait Event Parameter2Parameter3

enq: UL - contention id 0

dbms_lock.allocate_unique(v_lockname, v_lockhandle);dbms_lock.allocate_unique(v_lockname, v_lockhandle);dbms_lock.request(v_lockhandle, p_ltype);dbms_lock.request(v_lockhandle, p_ltype);dbms_lock.release(v_lockhandle);dbms_lock.release(v_lockhandle);

Page 50: Lock 09 Enqueues

#.50

Copyright 2006 Kyle Hailey

Internal Locks

CI – Cross Instance CU – Cursor Bind HW – High Water RO – Reuse Object ST – Space Transaction

Page 51: Lock 09 Enqueues

#.51

Copyright 2006 Kyle Hailey

enq: CI - contention

Cross Instance not OPS lock. invoke actions in background processes

checkpoints log switches instance is shut down

Page 52: Lock 09 Enqueues

#.52

Copyright 2006 Kyle Hailey

CI – Cross InstanceId2 Meaning (parameter3)1 Pass in Parameters2 Invoke the call in background process3 Foreground has not returned yet4 Used to allocate the CI call5 Used to queue up interested clients

Id2 Meaning (parameter3)1 Pass in Parameters2 Invoke the call in background process3 Foreground has not returned yet4 Used to allocate the CI call5 Used to queue up interested clients

Id1 Meaning (parameter2)0 Flush buffers for reuse as new class1 LGWR checkpointing and Hot Backup2 DBWR synchronization of SGA with control file3 Log file add/drop/rename notification4 Write buffer for CR read5 Test Call6 Invalidate KCK cache in all instances7 Alter rollback segment optimal8 Signal Query Servers/coordinator9 Create Remote Parallel Query Server10 Set Global Partitions11 Stop Disk Writes12 Drop Sort Segments13 Release unused space from Sort Segments14 Instance Recovery for Parallel operation Group15 Validate parallel slave Lock Value16 Check Transaction State Objects

Id1 Meaning (parameter2)0 Flush buffers for reuse as new class1 LGWR checkpointing and Hot Backup2 DBWR synchronization of SGA with control file3 Log file add/drop/rename notification4 Write buffer for CR read5 Test Call6 Invalidate KCK cache in all instances7 Alter rollback segment optimal8 Signal Query Servers/coordinator9 Create Remote Parallel Query Server10 Set Global Partitions11 Stop Disk Writes12 Drop Sort Segments13 Release unused space from Sort Segments14 Instance Recovery for Parallel operation Group15 Validate parallel slave Lock Value16 Check Transaction State Objects

Page 53: Lock 09 Enqueues

#.53

Copyright 2006 Kyle Hailey

CI Locksselectselect substr(sql_text,0,20) sql_text,substr(sql_text,0,20) sql_text, p2,p3,p2,p3, CURRENT_OBJ# || ' ' || name objCURRENT_OBJ# || ' ' || name obj ,CURRENT_FILE# file#,CURRENT_FILE# file# ,CURRENT_BLOCK# block#,CURRENT_BLOCK# block# from v$active_session_history ash,from v$active_session_history ash, obj$ o,obj$ o, v$sqltext sqlv$sqltext sql wherewhere event like 'enq: CI%'event like 'enq: CI%' and o.obj# (+)= ash.current_obj#and o.obj# (+)= ash.current_obj# and sql.sql_id (+)= ash.sql_idand sql.sql_id (+)= ash.sql_id order by sample_time;order by sample_time;

SQL_TEXT P2 P3 OBJ FILE# BLOCK#SQL_TEXT P2 P3 OBJ FILE# BLOCK#

-------------------- ----- ------- ------------ ------ --------------------------- ----- ------- ------------ ------ -------

INSERT INTO TOTO1 VA 1 5 54225 TOTO1 6 682721INSERT INTO TOTO1 VA 1 5 54225 TOTO1 6 682721

Page 54: Lock 09 Enqueues

#.54

Copyright 2006 Kyle Hailey

CI Locks

SQL_TEXT P2 P3---------------------------------------- --- --alter table XXXXX drop partition YYYYY 1 5

ID1 = 2 " LGWR checkpointing and Hot Backup "

ID2 = 5. "Used to queue up interested clients"

SQL_TEXT P2 P3---------------------------------------- --- --alter table XXXXX drop partition YYYYY 1 5

ID1 = 2 " LGWR checkpointing and Hot Backup "

ID2 = 5. "Used to queue up interested clients"

If p2=1 and p3=5, then contention on blocks beingIf p2=1 and p3=5, then contention on blocks being

checkpointed, try raising fast_start_mttr_targetcheckpointed, try raising fast_start_mttr_target

alter system set fast_start_mttr_target=600 scope=both;alter system set fast_start_mttr_target=600 scope=both;

Page 55: Lock 09 Enqueues

#.55

Copyright 2006 Kyle Hailey

enq: CU - contention

cursor bind

From Jonathan Lewis: Protects the variable definitions while the

cursor is being optimized.8i one CU per bind 9i one CU per cursor

Page 56: Lock 09 Enqueues

#.56

Copyright 2006 Kyle Hailey

enq: HW - contention

DataData

EmptyEmptyHigh Water MarkHigh Water Mark

HeaderHeader

TableTable

Wait Event Parameter2 Parameter3enq: HW - contention table space # block

Session 1Session 1

Session 2Session 2

Session 3Session 3

Page 57: Lock 09 Enqueues

#.57

Copyright 2006 Kyle Hailey

HW Use Freelists

Cause multiple jumps in High Water Mark

Pre-Allocate Extents Alter table XXXX allocate extent;

Hidden Parameter _bump_highwater_mark_count alter session set "_bump_highwater_mark_count"=100;

Not supported

ASSM Automatic segment space management

Partition Object

Page 58: Lock 09 Enqueues

#.58

Copyright 2006 Kyle Hailey

HW Further Investigation

EVENT SQL_ID OBJ FILE# BLOCK#EVENT SQL_ID OBJ FILE# BLOCK#

-------------------- ------------- ----------- ----- -------------------------- ------------- ----------- ----- ------

enq: HW - contention 49ch3jfkncnsp 53410 T1_I1 13 29734enq: HW - contention 49ch3jfkncnsp 53410 T1_I1 13 29734

selectselect event,event, sql_id,sql_id, CURRENT_OBJ# || ' ' || name objCURRENT_OBJ# || ' ' || name obj ,CURRENT_FILE# file#,CURRENT_FILE# file# ,CURRENT_BLOCK# block#,CURRENT_BLOCK# block# from v$active_session_history ash,from v$active_session_history ash, obj$ oobj$ o wherewhere event like 'enq: HW%'event like 'enq: HW%' and o.obj# (+)= ash.current_obj#and o.obj# (+)= ash.current_obj#

order by sample_timeorder by sample_time;;

Page 59: Lock 09 Enqueues

#.59

Copyright 2006 Kyle Hailey

enq: KO - fast object checkpoint

Used when checking the cache for blocks from a table for PQO direct read

Page 60: Lock 09 Enqueues

#.60

Copyright 2006 Kyle Hailey

enq: RO - fast object reuse

Drop or Truncate a tableWait for DBWR to clean cache

SolutionSmaller cacheTune DBWR using smaller MTTRUse GTTTruncate/drop less often

SELECT TARGET_MTTR, ESTIMATED_MTTR, SELECT TARGET_MTTR, ESTIMATED_MTTR,

CKPT_BLOCK_WRITES CKPT_BLOCK_WRITES FROM V$INSTANCE_RECOVERYFROM V$INSTANCE_RECOVERY

alter system set fast_start_mttr_target=17 scope=both;alter system set fast_start_mttr_target=17 scope=both;

Page 61: Lock 09 Enqueues

#.61

Copyright 2006 Kyle Hailey

enq: ST - contention

Space Transaction management lock Space Transaction Lock Allocating/deallocating extents Dictionary Managed Tablespaces Id1, Id2 Combinations:

Always 0, Always 0.

SolutionGot to Locally Managed Tablespaces

Page 62: Lock 09 Enqueues

#.62

Copyright 2006 Kyle Hailey

Other Resources

@?/rdbms/admin/utllockt@?/rdbms/admin/utllockt

WAITING_SESSION LOCK_TYPE MODE_REQUESTED MODE_HELD LOCK_ID1 LOCK_ID2WAITING_SESSION LOCK_TYPE MODE_REQUESTED MODE_HELD LOCK_ID1 LOCK_ID2

--------------- ----------- -------------- --------- --------- ----------------------- ----------- -------------- --------- --------- --------

144 None144 None

139 Transaction Share Exclusive 131113 7507139 Transaction Share Exclusive 131113 7507

Page 63: Lock 09 Enqueues

#.63

Copyright 2006 Kyle Hailey

Blocking Sessions

Page 64: Lock 09 Enqueues

#.64

Copyright 2006 Kyle Hailey

Summary : Internal Locks CI – Cross Instance CU – Cursor Bind HW – High Water

Look at object and SQL use LMT, freelists, pre-allocate extents

KO – fast object checkpoint PQO

RO – reusable object Reduce cache, tune DBWR, use GTT

SQ – Sequence Lock logon/logoff problem

ST - Space Transaction only one per database used for space allocations uet, fet Find object use LMT