Professional Documents
Culture Documents
Understanding Locks and Enqueues
Understanding Locks and Enqueues
By Mark J. Bobak
November 20th, 2008
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
• Characteristics of Enqueues
• Most Common Types of Enqueues
– TM – Table Modification Enqueue
– TX – Transaction Enqueue
• Wait Scenarios of TM and TX enqueues
• Anatomy of a row-level Lock
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
What is an Enqueue? (p.2)
• Enqueues differ from latches:
– Latches are the Database’s version of a mutex
– Whereas latches (mostly) provide for mutually
exclusive access, enqueues allow for shared access, if
mode is compatible
– Enqueues allow for enqueueing, that is, sessions
waiting for access to an enqueue will queue in a line
– In the case of latches, sessions waiting for access
must spin or sleep, and there’s no guarantee who will
get the latch next
What is an Enqueue? (p.3)
• An Enqueue has three lists:
– Holder: List of sessions that currently hold the
enqueue, and in what mode
– Waiter: List of sessions that are waiting to acquire
the enqueue in a mode incompatible with what’s
being held
– Converter: List of sessions that are currently holding
the enqueue in one mode, and waiting to convert
their lock to a more restrictive mode, incompatible
with a mode already held by some other session
Ok, but what’s an Enqueue, really??
• An enqueue is an element in an array of
enqueues that are allocated at SGA creation
time. (Visible in X$KSQEQ, number of
elements controlled by _enqueue_locks)
• It’s a state object, like so many other
components in the SGA. (process state
objects, session state objects, enqueue state
objects, etc.)
Most Common Enqueue Types
• List of most common enqueues
– 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
Most Common Enqueues
• Though there are many common enqueues,
usually only TM and TX create problems for
applications and developers.
• TM – DML, or table enqueue
• TX – Transaction enqueue
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 table’s
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.