Analyze Library Cache Pin and 4021 Error in Oracle

Analyze Library Cache Pin and 4021 Error in Oracle

 

From Metalink
PURPOSE

-------
 
This article discuss the library cache locks and pins and how to analyze
locking problems in this area.
 

SCOPE & APPLICATION
-------------------
 
Support analysts, dba's, ..
 
Title:  How to analyze ORA-04021 errors?
========================================
 
CONTENTS
========
 
1.   Why does it occur?
2.   Which views can be used to detect library cache locking problems?
3.   How to find out why an ORA-4021 occurs?
4.   What type of objects can be locked in a dictionary cache?
5.   which lock mode is required for which type of action?
6.   common library cache locking situations
7.   Unusual library cache locking problems
 
 
1.   Why does it occur?
-----------------------
 
An Oracle instance has a library cache that contains the description of different types of objects e.g. cursors, indexes, tables, views, procedures, ... Those objects cannot be changed when they are used. They are locked by a mechanism based on library locks and pins. A session that need to use an object will first acquire a library lock in a certain mode (null, shared or exclusive) on the object, in order to prevent other sessions from accessing the same object (e.g. exclusive lock when recompiling a package or view) or to maintain the object definition for a long time. Locking an object is sometimes referred as the job to locate it in the library cache and lock it in a certain mode.
If the session wants to modify or examine the object, it must acquire after the lock also a pin in a certain mode (again null, shared or exclusive).
Each SQL statement that want to use/modify objects that are locked or pinned and whose lock/pin mode is incompatible with the requested mode, will wait on events like 'library cache pin' or 'library cache lock' until a timeout occurs. The timeout normally occurs after 5 minutes and the SQL statement then ends with an ORA-4021. If a deadlock is detected, an ORA-4020 is given back.
 
ORA-04021 timeout occurred while waiting to lock object %s%s%s%s%s".
Cause:  While trying to lock a library object, a time-out occurred.
Action: Retry the operation later.
 
ORA-04020 deadlock detected while trying to lock object %s%s%s%s%s
Cause:  While trying to lock a library object, a deadlock is detected.
Action: Retry the operation later.(see Note 166924.1)
 
2.   Which views can be used to detect library locking problems?
----------------------------------------------------------------
Different views can be used to detect pin/locks:
DBA_KGLLOCK : one row for each lock or pin of the instance
-KGLLKUSE  session address
-KGLLKHDL  Pin/Lock handle
-KGLLKMOD/KGLLKREQ  Holding/requested mode
0           no lock/pin held
1           null mode
2           share mode
3           exclusive mode
-KGLLKTYPE Pin/Lock
(created via the $ORACLE_HOME/rdbms/admin/catblock.sql)
 
V$ACCESS : one row for each object locked by any user
-SID       session sid
-OWNER     username
-OBJECT    object name
-TYPE      object type
 
V$DB_OBJECT_CACHE : one row for each object in the library cache
-OWNER         object owner
-NAME          object name or cursor text
-TYPE          object type
-LOCKS         number of locks on this object
-PINS          number of pins on this object
 
DBA_DDL_LOCKS  : one row for each object that is locked (exception made of the cursors)
-SESSION_ID
-OWNER
-NAME
-TYPE
-MODE_HELD
-MODE_REQUESTED
 
V$SESSION_WAIT : each session waiting on a library cache pin or lock is blocked by some other session
-p1 = object address
-p2 = lock/pin address
 
 
3.   How to find out why an ORA-4021 occurs?
--------------------------------------------
When you execute the statement that generates the ORA-4021, it is possible during the delay of 5 minutes to detect the reason for the blocking situation.
Following query can be used to find the blocking and waiting sessions:
FYI: You need to run the script called "catblock.sql" first.
===  This script can be found in:  $ORACLE_HOME/rdbms/admin/catblock.sql
select /*+ ordered */ w1.sid  waiting_session,
        h1.sid  holding_session,
        w.kgllktype lock_or_pin,
        w.kgllkhdl address,
        decode(h.kgllkmod,  0, 'None', 1, 'Null', 2, 'Share', 3, 'Exclusive',
           'Unknown') mode_held,
        decode(w.kgllkreq,  0, 'None', 1, 'Null', 2, 'Share', 3, 'Exclusive',
           'Unknown') mode_requested
  from dba_kgllock w, dba_kgllock h, v$session w1, v$session h1
 where
  (((h.kgllkmod != 0) and (h.kgllkmod != 1)
     and ((h.kgllkreq = 0) or (h.kgllkreq = 1)))
   and
     (((w.kgllkmod = 0) or (w.kgllkmod= 1))
     and ((w.kgllkreq != 0) and (w.kgllkreq != 1))))
  and  w.kgllktype  =  h.kgllktype
  and  w.kgllkhdl   =  h.kgllkhdl
  and  w.kgllkuse     =   w1.saddr
  and  h.kgllkuse     =   h1.saddr
/
 
The result looks like:
 
WAITING_SESSION HOLDING_SESSION LOCK ADDRESS  MODE_HELD MODE_REQU
--------------- --------------- ---- -------- --------- ---------
             16              12 Pin  03FA2270 Share     Exclusive
 
The object that is locked can be found with v$object_dependency and should be the same as the one mentioned in the ORA-4021 error message.e.g.
select to_name from v$object_dependency where to_address = '03FA2270';
should give:
TO_NAME
-------------
DBMS_PIPE
 
You can find which library objects are used by each session via following queries, e.g.
a. for the blocked session:
 
select distinct kglnaobj from x$kgllk  where
kgllkuse in (select saddr from v$session where sid = 16);
 
b. for the blocking session
 
select distinct kglnaobj from x$kgllk  where
kgllkuse in (select saddr from v$session where sid = 12);
 
One of those objects can be the cursor or statement that each session is executing/trying to execute.
 
You can also use the $ORACLE_HOME/rdbms/admin/utldtree.sql utility to find out how the dependency tree looks like and which objects are dependent on e.g. DBMS_PIPE. One of those objects will be the sql statement of the holding session. A variant script on utldtree.sql stands in Note 139594.1 and gives which objects an object depends on.
 
4.   What type of objects can be locked in a dictionary cache?
---------------------------------------------------------
 
Following list give all objects that can be locked in the library cache
(Oracle8i).
- CURSOR
- INDEX
- TABLE
- CLUSTER
- VIEW
- SYNONYM
- SEQUENCE
- PROCEDURE
- FUNCTION
- PACKAGE
- PACKAGE BODY
- TRIGGER
- TYPE
- TYPE BODY
- OBJECT
- USER
- DBLINK
- PIPE
- TABLE PARTITION
- INDEX PARTITION
- LOB
- LIBRARY
- DIRECTORY
- QUEUE
- INDEX-ORGANIZED TABLE
- REPLICATION OBJECT GROUP
- REPLICATION PROPAGATOR
 
5.   Which lock/pin mode is required for which type of action?
----------------------------------------------------------
 
All DDL operations require a exclusive lock and pin on the object that need to be processed: e.g. when a package is recompiled, when a grant is given on an object, a truncate on a table, ...
 
All usage of objects require a null lock and shared pin on those objects: e.g. when a view is used, when a procedure is executed, ... It is also applicable for all dependent objects (e.g. view based on another view, package using another package or view).
 
The DDL cursors themselves require a null lock (localisation job in library cache) and an exclusive pin when executing. Query and DML cursors require a null lock and shared pin when executing.
 
The locks/pins are held during the duration of the SQL statement, and released at the end of it.
 
 
6.   Common library cache locking situations
--------------------------------------------
 
If using DBMS_SQL for DDL or 'execute immediate', be sure the DDL does not affect objects currently running or that you are directly dependent on.  Eg: grant to the procedure that is running will result in a deadlock.
You may not issue a DDL on a object that is being used, e.g. somebody is executing a long running package and you want to recreate it. It mostly result in a timeout.
 
7.   Unusual library cache locking problems
-------------------------------------------
 
a. When the shared pool is too small, the initjvm can give an ORA-4021 (see Note 152915.1)
 
b. ORA-4021 during dbms_utility.compile_schema. If one (or more) objects to be compiled were executed within the same PLSQL block prior to the invocation of compile_schema, an ORA-4021 will occur (see Note 1070137.6)
c. ORA-4021 can occur when attempting to modify a PL/SQL object if a PL/SQL client (such as Oracle Forms) has a 'library cache pin' lock on the same object.
Note:  This affects only 9i databases.  (see Note 223140.1 or Bug 2166890)
 
RELATED DOCUMENTS
-----------------
 
Note 1054939.6 COMPILATION OF PACKAGE IS HANGING ON LIBRARY CACHE LOCK AND LIBRARY CACHE
Note 122793.1  HOW TO FIND THE SESSION HOLDING A LIBRARY CACHE LOCK
Note 152915.1  Unable to Add JServer to the Database, ORA-04021
Note 1070137.6 V8.X ORA-4021 WHEN COMPILING PACKAGE WITH COMPILE_SCHEMA
Note 139594.1  Script To List Recursive Dependency Between Objects
Note 223140.1  Hangs and ORA-4021 errors possible when using PL/SQL clients with Oracle9I, Bug 2166890.
Bug 2166890    Hang / ORA-4021 possible using PLSQL clients
Note 166924.1  Ora-04020 Deadlocks Most Common Causes

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章