how oracle locking works -...

15
11/15/2012 1 How Oracle Locking Works Arup Nanda Longtime Oracle DBA Agenda What this is about? Learning How Oracle Locking Works Understanding locking behavior Tuning locking operations Tools SQL*Plus 2 How Oracle Locking Works

Upload: vodat

Post on 15-May-2019

216 views

Category:

Documents


0 download

TRANSCRIPT

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

11/15/2012

2

3

About Me• Oracle DBA for 16 years and counting

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

• Brought up the Global Database Group at Starwood 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

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

11/15/2012

4

Records Getting Inserted

How Oracle Locking Works7

2nd Record

How Oracle Locking Works8

Records get inserted from

tail end

11/15/2012

5

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”

How Oracle Locking Works9

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

How Oracle Locking Works10

11/15/2012

6

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

How Oracle Locking Works11

ITL Shortage

• ITL can’t 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

11/15/2012

7

Transaction

• A transaction is a block which is ended by a commit or a rollback

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

11/15/2012

8

Checking Transactions

• Local Transaction ID

– dbms_transaction.local_transaction_id

How Oracle Locking Works15

Undo Segment#

Slot# in that segment

Sequence# in that slot

loctxnid.sql

Transaction Table

• A memory structure that holds all the active transactions in the instance

• Exposed through X$KTCXB

• Shown as V$TRANSACTION

• Different Across Instances in RAC

How Oracle Locking Works16

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 a Session, join with V$SESSION

• Columns to join:

How Oracle Locking Works18

V$TRANSAC

TION

V$SESSIO

N

ADDR TADDR

SES_ADDR SADDR

OR

Sesstxn.sql

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

11/15/2012

11

ITL Record

How Oracle Locking Works21

Transaction ID,

corresponding to

V$TRANSACTION

Undo Segment

Information

Flag – locked, etc.

Number of rows

locked

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 - Share

5 - Sub Share Exclusive

6 - Exclusive

Lockobj.sql

11/15/2012

12

jj

How Oracle Locking Works23

Obj.sql

Blocking Session

• To find out the session that holds the lock this session 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

11/15/2012

13

Locked Row• Checking for the row information

How Oracle Locking Works25

To get the object information:

Rowwait.sql

Dobjid.sql

Row from RowID

How Oracle Locking Works26

This is the row on which the lock is being requested

Note:

DATA_OBJECT_ID;

not OBJECT_ID

Row_id.sql

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

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 is space; 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