Professional Documents
Culture Documents
Mechanism Demystified
Pini Dibask, Product Manager for Database Solutions
November 17th 2016
About Me
• My Blog: OracleDBPro.BlogSpot.com
Pini.Dibask@Quest.com
http://Linkedin.com/in/pinidibask
@pini_dibask
2 Confidential
About
• Simplifies IT management
• Driven by innovation
“Spend less time on what you need to do, and more time on what you want to do!”
3 Confidential
Agenda
4 Confidential
Overview of Locks in
Database Management Systems
5 Confidential
Overview of Database Locks
6 Confidential
Oracle Database
Locking Mechanism Concepts
7 Confidential
High Level Overview
8 Confidential
Writer Never Blocks Reader - How?
• Oracle blocks can be modified during execution of DMLs
• Non-Blocking Queries
Oracle
reconstructs
the block using
undo image
9 Confidential
Lock Modes
10 Confidential
DML Locks
11 Confidential
DML Row Locks (“TX Locks”)
12 Confidential
DML Row Locks (“TX Locks”) - Demo
Session #1 Session #2
SQL> CREATE TABLE employees (id NUMBER, name VARCHAR2 (20)); SQL> UPDATE employees SET name = 'Mark' WHERE id = 1;
Table created. 1 row updated.
13 Confidential
Table Locks (“TM Locks”) Cont’d
Table EMPLOYEES
EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID MANAGER_ID DEPARTMENT_ID
Table lock acquired Exclusive row lock (TX) acquired Row being updated
14 Confidential
Table Locks (“TM Locks”)
15 Confidential
DML Locks - Demo
(Session ID #383)
SQL> UPDATE employee
2 SET last_name = 'Jones' SQL> SELECT object_name,
Row of 3 WHERE employee_id = 139; session_id,
oracle_username,
employee_id #139
locked_mode
is locked in 1 row updated.
FROM v$locked_object JOIN dba_objects USING (object_id);
LMODE 6 -
Exclusive (x) OBJECT_NAME SESSION_ID ORACLE_USERNAME LOCKED_MODE
---------------- ------------- ----------------------- -----------------
SQL> SELECT type, lmode EMPLOYEE 383 SALES 3
FROM v$lock
Table EMPLOYEE WHERE sid = 383;
is locked in
TYPE LMODE
LMODE 3 - Row
----- ----------
Exclusive Table TX 6
Lock (RX) TM 3
16 Confidential
DDL Locks
• Share Locks
• Allow data concurrency for similar operations
• Only modified objects are locked - Oracle never locks entire Data Dictionary
17 Confidential
DDL Locks Cont’d
• Problem
• DDL commands (e.g. ALTER TABLE, DROP TABLE) require exclusive locks ( LMODE = 6)
• Solution
• DDL_LOCK_TIMEOUT parameter (available from Oracle 11g)
• Specifies time limit for how long DDL statements will wait in DML lock queue
• Default value is 0
• Can be set at session level (ALTER SESSION) or instance level (ALTER SYSTEM)
18 Confidential
DDL Locks - Demo
Session #1 Session #2
Table already
SQL> UPDATE employee SQL> drop table employee; locked in
SET last_name = 'Jones' drop table employee share mode
WHERE employee_id = 139; *
ERROR at line 1:
1 row updated. ORA-00054: resource busy and acquire with NOWAIT
specified or timeout expired
SQL> alter system set ddl_lock_timeout=10; SQL> drop table employee;
drop table employee
System altered. *
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT
Available
specified or timeout expired
from version Oracle waits
11g 10 seconds
before raising
this error
19 Confidential
How Oracle Manages Locks?
20 Confidential
FIFO Lock Ordering - Demo
SQL> SELECT * FROM employees; SQL> LOCK TABLE employees SQL> UPDATE employees
IN EXCLUSIVE MODE; SET name = 'Daniel'
EMP_ID NAME DEPT_ID WHERE emp_id = 2;
---------- ------------ ---------- (waiting – session is blocked)
1 David 3 (waiting – session is blocked)
2 John 4
SQL> UPDATE employees
SET name = 'Greg'
WHERE emp_id = 1;
1 row updated.
21 Confidential
Manual Data Locks
Example: Transaction needs exclusive access to resource and must not wait for other transactions
22 Confidential
Manual Row Locks - Demo
Session #1 Session #2
SQL> SELECT id, name SQL> UPDATE employees SET name = 'Mark' WHERE id = 1;
FROM employees 1 row updated.
WHERE id = 2 FOR UPDATE;
ID NAME
------------------------------
2 Jason
SQL> UPDATE employees SET name = 'John' WHERE id = 2;
(waiting – session is blocked)
23 Confidential
Manual Table Locks - Demo
• LOCK TABLE IN [ ROW SHARE | ROW EXCLUSIVE | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE ] MODE
24 Confidential
Advanced Locking Scenarios
25 Confidential
Deadlocks
Resource 1 Resource 2
26 Confidential
Deadlocks - Demo
Session #1 Session #2
(Waiting - session
ORA-00060: is blocked)
deadlock detected while waiting for resource (Waiting - session is blocked)
27 Confidential
Deadlocks Cont’d
• Deadlock error (ORA-00060) is also audited in Alert Log
orcl11_ora_3600.trc
28 Confidential
Deadlocks Cont’d
orcl11_ora_3600.trc (cont’d)
Session 584
(sid 584) is
still waiting
with this
statement
The statement
that has been
rolled-back
(“This session”)
29 Confidential
Blocked Inserts
30 Confidential
Blocked Inserts - Demo
Session #1 Session #2
SQL> CREATE TABLE employees SQL> insert into employees values (1, 'David');
(
id NUMBER, (waiting – row already locked by session #1)
name VARCHAR2 (20),
CONSTRAINT pk_id PRIMARY KEY (id)
);
*
Table created.
ERROR at line 1:
SQL> insert into employees values (1, 'John'); ORA-00001:
unique constraint (SALES.PK_ID) violated
1 row created.
SQL> commit;
Commit complete.
31 Confidential
Blocked Inserts - Demo Cont’d
Session #1 Session #2
SQL> CREATE TABLE employees SQL> insert into employees values (1, 'David');
(
id NUMBER, (waiting – row already locked by session #1)
name VARCHAR2 (20),
CONSTRAINT pk_id PRIMARY KEY (id)
);
1 row created.
Table created.
SQL> insert into employees values (1, 'John');
1 row created.
SQL> rollback;
Rollback complete.
32 Confidential
Unindexed Foreign Keys
• Oracle Database places full table lock (LMODE = 4) on child table when:
• Unindexed foreign key column on child table
33 Confidential
Unindexed Foreign Keys Cont’d
Parent Key Primary key of referenced table
60 IT 103 1400
Full table lock acquired Exclusive row lock (TX) acquired Primary key modified
34 Confidential
Indexed Foreign Keys
Parent Key Primary key of referenced table
60 IT 103 1400
Session
deletes 90 Executive 100 1700
a row
280 Event Planning 1700
DMLs are
allowed on Indexed Foreign Key
Table EMPLOYEES (Dependent Child Table) EMPLOYEES
EMPLOYEE__ID LAST_NAME EMAIL HIRE_DATE JOB_ID MANAGER_ID DEPAETMENT_ID
Full table lock acquired Exclusive row lock (TX) acquired Row being deleted
35 Confidential
Monitoring
36 Confidential
Monitoring locks via Oracle Dictionary Views
• V$LOCK/DBA_LOCK - Lists all locks currently held and all requests for a lock
• V$LOCKED_OBJECT - Lists sessions holding locks on what objects and in what mode
37 Confidential
Monitoring locks via Oracle Dictionary Views Cont’d
SQL> SELECT DECODE (blocking_session, null, null, 'BLOCKED') status,
sid,
lmode,
request,
ctime duration,
USER, Real-Time
program, Monitoring
blocking_session,
DECODE (request, 0, NULL, SQL_TEXT) SQL_TEXT
FROM v$lock l
JOIN v$session s USING (sid)
LEFT JOIN v$sqlarea USING (sql_id)
WHERE block = 1 OR request > 0
ORDER BY status
38 Confidential
Monitoring Blocked Sessions via Tools
39 Confidential
Monitoring Blocked Sessions via Tools
40 Confidential
Monitoring Blocked Sessions via Tools
41 Confidential
Monitoring Blocked Sessions via Tools
42 Confidential
Monitoring Blocked Sessions via Tools
43 Confidential
Monitoring Blocked Sessions via Tools
44 Confidential
Summary
• Hold locks as long as you need, but not more than you need
45 Confidential
Q&A
46 Confidential