Analyze library cache pin and 4021 error in Oracle

Source: Internet
Author: User
Tags sql client



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 ".
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
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 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, 'clusive ',
'Unknown ') mode_held,
Decode (W. kgllkreq, 0, 'none', 1, 'null', 2, 'share', 3, 'clusive ',
'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 shocould 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 ';
Shoshould 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.