Professional Documents
Culture Documents
Understanding Locks and Enqueues
Understanding Locks and Enqueues
Introduction
Senior Oracle DBA at ProQuest Company Working with Oracle since version 6 Present regularly at the local, national and international level Member of the OakTable Network since 2001
Disclaimer
At least one statement in this presentation is incorrect, but it may be this one.
Agenda
What is an Enqueue?
Difference between locks, enqueues, and latches
What is an Enqueue?
An Enqueue protects shared resources from concurrent, incompatible accesses
Prevent two sessions from writing the same row of a table at the same time Facilitates enforcement of parent/child locking for referential integrity Prevent two sessions from updating the definition of a table (drop/add column, etc) at the same time
CF Controlfile DM Database Mount MR Media Recovery RS ?? (not documented in list of enqueue names for 10g or 11g) RT Redo Thread TM DML TO Temporary Table Object TS Temporary Segment or Tablespace TX Transaction UL User Lock
TM Enqueue
The TM or DML enqueue is taken at the table level, by any session doing DDL or DML.
DDL takes TM enqueue in X (exclusive) mode, which prevents other sessions doing any DML or DDL while the table definition is changing DML takes TM enqueue in S (shared) mode, which allows for concurrent DML on the same table, but locks out DDL.
TM Enqueue (p. 2)
Is used when foreign key relationships are enforced Locking strategy is much more aggressive in the case when indexes on the child tables foreign key columns are missing. Missing indexes on child table foreign key columns is a common cause of waits and deadlocks.
TM Enqueue (p.3)
One exception is direct load insert. In the case of direct load insert, a session will take the TM enqueue in X mode. This prevents any other DML from taking place while the direct load is happening, in addition to blocking all DDL. TM enqueues may be disabled
At the instance level, via dml_locks=0 (If RAC, all instances must have same value of dml_locks) On a per table basis, with alter table disable locks;
TM Enqueue (p. 4)
There are two different wait scenarios youll likely encounter with the TM enqueue. X mode waiting on X mode being held.
This is likely to be a session holding X mode during direct load insert, while another session attempts to initiate a direct load into the same table.
TX Enqueue
The TX or Transaction Enqueue is at the heart of Oracles row locking mechanism. The TX enqueue points to an undo segment header, slot within that header, and wrap number. Before images of any changes are stored in the body of that undo segment, and point to that segment header/slot/wrap.
TX Enqueue (p. 2)
When a transaction commits, the only action thats guaranteed to happen is that the undo segment slot is marked committed, and the TX enqueue is released. Many different wait scenarios may be observed, each with a different root cause.
TX Enqueue (p. 3)
In every (known) case, the TX enqueue will be held in X mode. If a session is waiting to acquire a TX enqueue in X mode, its simple row-level locking or PK/UK enforement on an existing row. If a session is waiting to acquire TX enqueue in S mode, there are several possibilities.
ITL slot shortage
TX Enqueue (p.4)
If a session is waiting to acquire TX enqueue in S mode, there are several possibilities. (cont.)
Bitmap index locking Is the segment an IOT? If so, its simple row-level locking On insert of duplicate, uncommitted PK or UK Very rarely, freelist contention (no longer relevant if youre using ASSM)
Monitoring Enqueues
V$LOCK
List of current enqueues, as well as sessions holding and waiting on enqueues Shows TYPE of enqueue, mode held, mode waited, as well as ID1 and ID2 that help interpret information about the enqueue
V$SESSION_WAIT
Can be used to quickly identify waiting session
V$SESSION
Starting with 10g, V$SESSION has extended information, including:
Wait information from V$SESSION_WAIT And especially, BLOCKING_INSTANCE and BLOCKING_SESSION
Row-level Locking
The first DML in a session where a transaction does not already exist will implicitly create a transaction.
An undo segment number, slot, and wrap will be assigned TX enqueue (state object) will be instantiated
When a row to be modified is identified, session will take an entry in the ITL of the data block, assign it to the transaction
USN/SLOT/WRAP will be written to ITL slot, reserving that slot for the current transaction Lock will be taken on the row, by setting the lock byte in the row directory to point to the current transactions ITL slot
Demo Time!
Row locking demo If time, do more demos
Questions?
Comments? Criticisms?
More Information
Oracle Documentation
http://tahiti.oracle.com/
Oracle MetaLink
http://metalink.oracle.com/
OakTable Website
http://oaktable.net/
Jonathan Lewis
http://jonathanlewis.wordpress.com/ http://www.jlcomp.demon.co.uk/