Understanding Locks and Enqueues How to Approach Common Blocking Lock Scenarios By Mark J. Bobak November 20 th, 2008.

Slides:



Advertisements
Similar presentations
Rollback Segments Nilendu Misra (MAR99)
Advertisements

The Architecture of Oracle
Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
Enqueue Waits : Locks. #.2 Copyright 2006 Kyle Hailey Wait Tree - Locks Waits Disk I/O Library Cache Enqueue Undo TX 6 Row Lock TX 4 ITL Lock HW Lock.
Enqueue Waits : Locks. #.2 Copyright 2006 Kyle Hailey Locks REDO Lib Cache Buffer Cache IO Locks Network.
1 © 2006 Julian Dyke Supplemental Logging Julian Dyke Independent Consultant juliandyke.com Web Version.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
17 Copyright © 2005, Oracle. All rights reserved. Performing Flashback.
Tuning Database Locks & Latches Hamid R. Minoui Fritz Companies Inc. NoCOUG May 16, 2001.
1 Chapter 16 Latch and Mutex Contention. 2 Architecture Overview of Latches Protect Oracle’s SGA Prevent two processes from updating same area of SGA.
Oracle Architecture. Instances and Databases (1/2)
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
More on transactions…. Dealing with concurrency (OR: how to handle the pressure!) Locking Timestamp ordering Multiversion protocols Optimistic protocols.
1 - Oracle Server Architecture Overview
Harvard University Oracle Database Administration Session 2 System Level.
Locking and concurrency. Overview In multi-user systems, several users may update the same information concurrently – i.e. at the same time. Locking allows.
Harvard University Oracle Database Administration Session 5 Data Storage.
9 Copyright © 2009, Oracle. All rights reserved. Managing Data Concurrency.
10 Copyright © 2009, Oracle. All rights reserved. Managing Undo Data.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
Backup Concepts. Introduction Backup and recovery procedures protect your database against data loss and reconstruct the data, should loss occur. The.
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
Database Administration TableSpace & Data File Management
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
I Copyright © 2004, Oracle. All rights reserved. Introduction.
Extents, segments and blocks in detail. Database structure Database Table spaces Segment Extent Oracle block O/S block Data file logical physical.
Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 3.
1 RAC Internals Julian Dyke Independent Consultant Web Version juliandyke.com © 2007 Julian Dyke.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
1 Oracle Architectural Components. 1-2 Objectives Listing the structures involved in connecting a user to an Oracle server Listing the stages in processing.
Copyright © Oracle Corporation, All rights reserved. 1 Oracle Architectural Components.
An Oracle server:  Is a database management system that provides an open, comprehensive, integrated approach to information management.  Consists.
Transaction control and isolation levels in Oracle
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan Introduction to Concurrency Control Different types.
Database structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
Introduction to Oracle. Oracle History 1979 Oracle Release client/server relational database 1989 Oracle Oracle 8 (object relational) 1999.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
7 Copyright © 2005, Oracle. All rights reserved. Managing Undo Data.
Database structure and space Management. Segments The level of logical database storage above an extent is called a segment. A segment is a set of extents.
Objectives Database triggers and syntax
IT Database Administration Section 09. Backup and Recovery Backup: The available options Full Consistent (cold) Backup Database shutdown, all files.
D Copyright © Oracle Corporation, All rights reserved. Loading Data into a Database.
Oracle 10g Database Administrator: Implementation and Administration Chapter 5 Basic Storage Concepts and Settings.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 1Oracle9i DBA II: Backup/Recovery and Network Administration 1 Chapter 1 Backup and Recovery Overview MSCD642 Backup and Recovery.
Chapter 5 Index and Clustering
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Preface 1Performance Tuning Methodology: A Review Course Structure 1-2 Lesson Objective 1-3 Concepts 1-4 Determining the Worst Bottleneck 1-5 Understanding.
Module 11: Managing Transactions and Locks
IT Database Administration Section 05. Rollback Segments: An Introduction  What are they?  Space within a database used to store “before” images.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Chapter 21 SGA Architecture and Wait Event Summarized & Presented by Yeon JongHeum IDS Lab., Seoul National University.
Concurrency Control Managing Hierarchies of Database Elements (18.6)
Database structure and space Management
Enqueue Structures.
Oracle Working Mechanism Okcan Yasin Saygili
Isolation Levels Understanding Transaction Temper Tantrums
Instant Add Columns in MySQL
Kyle Hailey Enqueue Waits : Locks Kyle Hailey
Concurrency Control WXES 2103 Database.
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Isolation Levels Understanding Transaction Temper Tantrums
Presentation transcript:

Understanding Locks and Enqueues How to Approach Common Blocking Lock Scenarios By Mark J. Bobak November 20 th, 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. 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 you’ll 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. ‘SSX’ mode waiting on ‘SX’ mode being held. – This is due to unindexed foreign keys

TX Enqueue The TX or Transaction Enqueue is at the heart of Oracle’s 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 that’s 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, it’s 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, it’s simple row-level locking – On insert of duplicate, uncommitted PK or UK – Very rarely, freelist contention (no longer relevant if you’re 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

Monitoring Enqueues (p. 2) If you’re still not on at least 10g, there are couple of other options – catblock.sql, available in ?/rdbms/admin/catblock.sql will create DBA_BLOCKERS, DBA_WAITERS, etc As of 10g, still distributed, but, appears to be out of date? – Steve Adams’ locking scripts, available on his website,

Monitoring Enqueues (p. 3) Looking at V$LOCK, the TYPE column identifies the type of enqueue, i.e., TM, TX, UL, etc ID1 and ID2 may carry additional information, but are context-sensitive with respect to the enqueue TYPE For TM enqueue, ID1 is the OBJECT_ID of the object being locked, which can be referenced in DBA_OBJECTS, and ID2 is always 0 For TX Enqueue, ID1 and ID2 hold the undo segment number, slot number, and wrap.

Monitoring Enqueues (p. 4) For TX enqueue, ID1 and ID2 must be decoded using this formula: – trunc(id1/power(2,16)) USN – bitand(id1,to_number('ffff','xxxx'))+0 SLOT – id2 SEQ/WRAP

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 transaction’s ITL slot

Demo Time! Row locking demo If time, do more demos

Questions? Comments? Criticisms?

More Information Oracle Documentation – Oracle MetaLink – OakTable Website – Jonathan Lewis – – Steve Adam’s Website – Tom Kyte’s AskTom –