understanding oracle locking - proligence oracle locking internals 16. ... where rowid =...
TRANSCRIPT
Understanding Oracle Locking Internals
Arup NandaLongtime Oracle DBA
@ArupNanda
Copyright
IMPORTANTPlease feel free to use the material presented here for
your benefit. All I ask is please attribute the source, which is this material, in your usage.
Understanding Oracle Locking Internals2
Locking
Understanding Oracle Locking Internals3
Table
Row1
Row2
Row3
Session1 Session2
has updated the row
wants to update the row
Understanding Oracle Locking Internals4
Is there a central lock store in Oracle?
Agenda• What this is about?
– How Oracle Locking Works
– Understanding locking behavior
– Tuning locking operations
• Tools– SQL*Plus
5Understanding Oracle Locking Internals
6
About Me• Oracle DBA for 20 years and
counting• Speak at conferences, write articles,
4 books, provide trainings, security audits
• Blog: arup.blogspot.com• Tweeter: @arupnanda• Facebook.com/ArupKNanda
Understanding Oracle Locking Internals
Transaction• A transaction
is a block which is ended by a commit or a rollback
Understanding Oracle Locking Internals7
Statement 1
Statement 2
Commit;
Statement 3
Statement 4
Rollback;
Statement 5
Transaction 1
Transaction 2
Transaction not ended yet
Transaction Data
Understanding Oracle Locking Internals8
Undo Buffer
Buffer CacheRedo Buffer
pre postpre post
Undo Information LocatorSegment#»Slot#»Sequence#
Transaction ID• Checking for Transaction ID in own session
– dbms_transaction.local_transaction_id
Understanding Oracle Locking Internals9
10.25.31749
Undo Segment#
Slot# in that segment
Sequence# in that slot
local.sql
Undo Buffer
Buffer Cache
Redo Buffer
pre
post
pre
post
Transaction Table• A memory structure
• In SGA
• Exposed as X$KTCXB
• Visible as V$TRANSACTION
Understanding Oracle Locking Internals10
Transaction ID Other Relevant Information
10.25.31749 Active/Inactive,Undo Blocks, etc.
10.25.10234
10.25.32345
Undo Buffer
Buffer Cache
Redo Buffer
pre
post
pre
post
Undo Buffer
Buffer Cache
Redo Buffer
pre
post
pre
post
Checking for Txns• All the transactions in the instance
select addr, xidusn, xidslot, xidsqn
from v$transaction;
– ADDR: the address of the transaction – a raw value
– XIDUSN: the undo segment number
– XIDSLOT: the slot#
– XIDSQN: the sequence# or record# inside the slot
Understanding Oracle Locking Internals11
Vtrans.sql
Txn and Session• To Know Active Txns of a
Session, join with V$SESSION
Understanding Oracle Locking Internals12
select sidfrom v$session s, v$transaction twhere t.ses_addr = s.saddr
select sidfrom v$session s, v$transaction twhere t.addr = s.taddr
V$TRANSACTION V$SESSION
ADDR TADDR
SES_ADDR SADDR
OR
Txn2.sql
Txn1.sql
Commit
Understanding Oracle Locking Internals13
Block Changed by Transaction
Undo Segment
Redo Buffer
prepostpre post
flushCommit Marker
Locking
Understanding Oracle Locking Internals14
Table
Row1
Row2
Row3
Session1 Session2
has updated the row
wants to update the row
Lock Location• There is no central locking facility
• Oracle puts the lock for a row in the block itself
• In slots called ITL Entry
Understanding Oracle Locking Internals15
Empty Block
Understanding Oracle Locking Internals16
Records Getting Inserted
Understanding Oracle Locking Internals17
2nd Record
Understanding Oracle Locking Internals18
Records get inserted from tail end
Locking• Session 1 locks row 1
• It puts that information in the header of that block
• The transaction is “interested” in that row, hence “Interested Transaction”
Understanding Oracle Locking Internals19
Locking #2• Session 2 locks row 2
• It also puts an interested transaction entry
• Now there is a “list” of interested transactions known as ITL
Understanding Oracle Locking Internals20
ITLs Continued• Each ITL slot takes 24 bytes
• The total number of ITL slots can grow, as long as there is room in the block
• Can’t exceed 50% of the block
• Max ITL is 255
Understanding Oracle Locking Internals21
Checking ITL• Getting a block dumpalter system dump datafile <DF#> block min <block#> block max <block#>;
• Creates a tracefile with the dump of the block
Understanding Oracle Locking Internals22
Dump.sql
ITL Record
Understanding Oracle Locking Internals23
Itl Xid Uba Flag Lck Scn/Fsc0x01 0x000a.019.00007c05 0x00c00288.1607.0e ---- 1 fsc 0x0000.000000000x02 0x0003.017.00009e24 0x00c00862.190a.0f C--- 0 scn 0x0000.02234e2b
Transaction ID, corresponding to V$TRANSACTION
Undo Segment Information
Flag – locked, etc.
Number of rows locked
Commit and ITL
• ITL is not updated when commit happens– Commits are superfast
• When a new transaction encounters a lock in ITL– it must check the transaction table– If ACTIVE, then it’s actually locked
• ITLs are cleared during cleanout
Understanding Oracle Locking Internals24
V$LOCKED_OBJECT• Shows you the object
locked• XIDUSN• XIDSLOT• XIDSQN• OBJECT_ID• SESSION_ID• ORACLE_USERNAME• OS_USER_NAME• PROCESS• LOCKED_MODE
Understanding Oracle Locking Internals25
V$TRANSACTIONXIDUSNXIDSLOTXIDSQN
0 - None1 - Null2 - Row Share3 - Row Exclusive4 - Share5 - Sub Share Exclusive6 - Exclusive
selectowner object_owner,object_name object_name,session_id oracle_sid,oracle_username db_user,decode(locked_mode,
0, 'None',1, 'Null',2, 'Row Share',3, 'Row Exclusive',4, 'Share',5, 'Sub Share Exclusive',6, 'Exclusive',locked_mode) locked_mode
from v$locked_object lo, dba_objects dowhere (xidusn||'.'||xidslot||'.'||xidsqn) = ('&transid')and do.object_id = lo.object_id
/ Understanding Oracle Locking Internals26
Lobj.sql
Blocking Session• To find out the session that holds the lock this
session is asking forselect
blocking_session,
blocking_instance,
seconds_in_wait
from v$session
where sid = <sid>;
Understanding Oracle Locking Internals27
The SID of the session holding the lock
The Instance of the other session holding the lock
How long it has been waiting
Locked Row• Checking for the row informationselect row_wait_obj#,
row_wait_file#,row_wait_block#,row_wait_row#
from v$sessionwhere sid = <SID>;
ROW_WAIT_OBJ# ROW_WAIT_FILE# ROW_WAIT_BLOCK# ROW_WAIT_ROW#------------- -------------- --------------- -------------
241876 1024 2307623 0
Understanding Oracle Locking Internals28
To get the object information:
select owner, object_type, object_name,data_object_id
from dba_objectsWhere object_id = 241876;
OWNER OBJECT_TYPE OBJECT_NAME DATA_OBJECT_ID----- ----------- ----------- --------------ARUP TABLE T1 241877
Row from RowIDselect * from arup.t1where rowid = dbms_rowid.rowid_create (
rowid_type => 1,object_number => 241877,relative_fno => 1024,block_number => 2307623,row_number => 0
);
COL1 C---- -
1 x
Understanding Oracle Locking Internals29
This is the row on which the lock is being requested
Note: DATA_OBJECT_ID;
not OBJECT_ID
A Useful Scriptselect
object_type,owner||'.'||object_name||':'||nvl(subobject_name,'-')
obj_name,dbms_rowid.rowid_create (
1,o.data_object_id,row_wait_file#,row_wait_block#,row_wait_row#
) row_idfrom v$session s, dba_objects owhere sid = &sidand o.object_id = s.row_wait_obj#/
Understanding Oracle Locking Internals30
Lock1.sqlRowinfo1.sql
ITL Shortage• ITL can’t grow when the block is full
• The session will wait with an event enq: TX - allocate ITL entry
• To avoid it– Have plenty of room in the block
• Increased PCTFREE, etc.
• MINIMIZE_RECORDS_PER_BLOCK
– Have a larger INITRANS
Understanding Oracle Locking Internals31
Finding ITL Shortage• Query
select statistic_name, value
from v$segment_statistics
where object_name = '<Object Name>';
• OutputSTATISTIC_NAME VALUE
--------------------- -----
logical reads 7216
ITL waits 2
…
Understanding Oracle Locking Internals32
STATISTIC_NAME
ITL waitsrow lock waits
Historical• AWR Repositoryselect snap_id, itl_waits_total, itl_waits_deltafrom dba_hist_seg_statwhere obj# = <ObjID>order by snap_id;
• Stats of Interest– ITL_WAITS_TOTAL
– ITL_WAITS_DELTA
– ROW_LOCK_WAITS_TOTAL
– ROW_LOCK_WAITS_DELTA
Understanding Oracle Locking Internals33
Summary• There is no central locking in Oracle• A txn marks the rows locked in the block itself• This is called Interested Transaction List (ITL)• If no ITL slot is available, one is created is there is
space; otherwise txn waits with ITL waits• ITL entry shows undo information• ITL is not updated as a part of commit• Blog: http://arup.blogspot.com/2011/01/how-oracle-
locking-works.html
Understanding Oracle Locking Internals34