How Oracle Locking Works Pres

Embed Size (px)

Citation preview

  • 7/28/2019 How Oracle Locking Works Pres

    1/15

    11/15/2012

    1

    How Oracle Locking

    WorksArup Nanda

    Longtime Oracle DBA

    Agenda

    What this is about?

    Learning How Oracle Locking Works

    Understanding locking behavior

    Tuning locking operations

    Tools

    SQL*Plus

    2How Oracle Locking Works

  • 7/28/2019 How Oracle Locking Works Pres

    2/15

    11/15/2012

    2

    3

    About Me Oracle DBA for 16 years and counting

    Speak at conferences, write articles, 4books, provide trainings, security audits

    Brought up the Global Database Group atStarwood Hotels, in White Plains, NY

    Blog: arup.blogspot.com

    Locking

    How Oracle Locking Works4

    Table

    Row1

    Row2

    Row3

    Session1 Session2

    has updated

    the row

    wants to

    update the

    row

  • 7/28/2019 How Oracle Locking Works Pres

    3/15

    11/15/2012

    3

    Lock Location

    There is no central locking facility

    Oracle puts the lock for a row in the block itself

    In slots called ITL Entry

    How Oracle Locking Works5

    Empty Block

    How Oracle Locking Works6

  • 7/28/2019 How Oracle Locking Works Pres

    4/15

    11/15/2012

    4

    Records Getting Inserted

    How Oracle Locking Works7

    2nd Record

    How Oracle Locking Works8

    Records get inserted from

    tail end

  • 7/28/2019 How Oracle Locking Works Pres

    5/15

    11/15/2012

    5

    Locking

    Session 1 locks row 1

    It puts that information in theheader of that block

    The transaction is interested inthat row, hence InterestedTransaction

    How Oracle Locking Works9

    Locking #2

    Session 2 locks row 2

    It also puts an interestedtransaction entry

    Now there is a list of interestedtransactions known as ITL

    How Oracle Locking Works10

  • 7/28/2019 How Oracle Locking Works Pres

    6/15

    11/15/2012

    6

    ITLs Continued

    Each ITL slot takes 24 bytes

    The total number of ITL slots can grow, as long asthere is room in the block

    Cant exceed 50% of the block

    Max ITL is 255

    How Oracle Locking Works11

    ITL Shortage

    ITL cant grow when the block is full

    The session will wait with an event

    To avoid it

    Have plenty of room in the block

    Increased PCTFREE, etc.

    MINIMIZE_RECORDS_PER_BLOCK

    Have a larger INITRANS

    How Oracle Locking Works12

  • 7/28/2019 How Oracle Locking Works Pres

    7/15

    11/15/2012

    7

    Transaction

    A transactionis a blockwhich isended by acommit or arollback

    How Oracle Locking Works13

    Statement 1

    Statement 2

    Commit;

    Statement 3

    Statement 4

    Rollback;

    Statement 5

    Transaction 1

    Transaction 2

    Transaction not ended yet

    Transaction ID

    How Oracle Locking Works14

    Block

    Changed by

    Transaction

    Undo

    SegmentRedo Buffer

    pre postpre post

  • 7/28/2019 How Oracle Locking Works Pres

    8/15

    11/15/2012

    8

    Checking Transactions

    Local Transaction ID dbms_transaction.local_transaction_id

    How Oracle Locking Works15

    UndoSegment#

    Slot# in thatsegment

    Sequence# inthat slot

    loctxnid.sql

    Transaction Table

    A memory structure that holds all the activetransactions in the instance

    Exposed through X$KTCXB

    Shown as V$TRANSACTION

    Different Across Instances in RAC

    How Oracle Locking Works16

  • 7/28/2019 How Oracle Locking Works Pres

    9/15

    11/15/2012

    9

    A Different Session

    All the transactions in the instance

    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

    How Oracle Locking Works17

    Vtxn.sql

    Txn and Session

    To Know Active Txn of aSession, join withV$SESSION

    Columns to join:

    How Oracle Locking Works18

    V$TRANSAC

    TION

    V$SESSIO

    NADDR TADDR

    SES_ADDR SADDR

    OR

    Sesstxn.sql

  • 7/28/2019 How Oracle Locking Works Pres

    10/15

    11/15/2012

    10

    Commit

    How Oracle Locking Works19

    Block

    Changed by

    Transaction

    Undo

    SegmentRedo Buffer

    prepre post

    flushCommit

    Marker

    Checking ITL

    Getting a file and block

    Getting a block dump

    Creates a tracefile with the dump of the block

    How Oracle Locking Works20

    Dump.sql

    Block.sql

  • 7/28/2019 How Oracle Locking Works Pres

    11/15

    11/15/2012

    11

    ITL Record

    How Oracle Locking Works21

    Transaction ID,

    corresponding to

    V$TRANSACTION

    Undo Segment

    Information

    Flag locked, etc.

    Number of rowslocked

    V$LOCKED_OBJECT Shows you the object

    locked

    How Oracle Locking Works22

    V$TRANSACTION

    0 - None

    1 - Null

    2 - Row Share

    3 - Row Exclusive

    4 - Share5 - Sub Share Exclusive

    6 - Exclusive

    Lockobj.sql

  • 7/28/2019 How Oracle Locking Works Pres

    12/15

    11/15/2012

    12

    jj

    How Oracle Locking Works23

    Obj.sql

    Blocking Session

    To find out the session that holds the lock thissession is asking for

    How Oracle Locking Works24

    The SID of the session holding

    the lock

    The Instance of the other session

    holding the lock

    How long it has been waiting

    Blocking.sql

  • 7/28/2019 How Oracle Locking Works Pres

    13/15

  • 7/28/2019 How Oracle Locking Works Pres

    14/15

    11/15/2012

    14

    ITL Shortage

    Query

    Output

    How Oracle Locking Works27

    STATISTIC_NAME

    Historical

    AWR Repository

    Stats of Interest ITL_WAITS_TOTAL

    ITL_WAITS_DELTA

    ROW_LOCK_WAITS_TOTAL

    ROW_LOCK_WAITS_DELTA

    How Oracle Locking Works28

  • 7/28/2019 How Oracle Locking Works Pres

    15/15

    11/15/2012

    15

    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 isspace; otherwise txn waits with ITL waits

    ITL entry shows undo information

    ITL is not updated as a part of commit

    How Oracle Locking Works29

    Thank You!

    Download Scripts:

    Blog:Tweeter:

    30