How to Analyze ORA-04021 or ORA-4020 Errors

4
How to analyze ORA-04021 or ORA-4020 errors? [ID 169139.1] Modified 19-AUG-2010 Type TROUBLESHOOTING Status PUBLISHED 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 Rate this document

Transcript of How to Analyze ORA-04021 or ORA-4020 Errors

Page 1: How to Analyze ORA-04021 or ORA-4020 Errors

How to analyze ORA-04021 or ORA-4020 errors? [ID 169139.1]

Modified 19-AUG-2010 Type TROUBLESHOOTING Status PUBLISHED

PURPOSE-------

This article discuss the library cache locks and pins and how to analyzelocking 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 situations7. Unusual library cache locking problems

1. Why does it occur?-----------------------

An Oracle instance has a library cache that contains the description ofdifferent types of objects e.g. cursors, indexes, tables, views, procedures,... Those objects cannot be changed when they are used. They are locked by amechanism based on library locks and pins. A session that need to use an objectwill 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 sameobject (e.g. exclusive lock when recompiling a package or view) or to maintainthe object definition for a long time. Locking an object is sometimes referredas 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 afterthe 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 pinnedand whose lock/pin mode is incompatible with the requested mode, will waiton events like 'library cache pin' or 'library cache lock' until a timeoutoccurs. The timeout normally occurs after 5 minutes and the SQL statementthen ends with an ORA-4021. If a deadlock is detected, an ORA-4020 is givenback.

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%sCause: 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 mode0 no lock/pin held1 null mode2 share mode3 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

Rate this document

Page 2: How to Analyze ORA-04021 or ORA-4020 Errors

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 possibleduring 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_requestedfrom dba_kgllock w, dba_kgllock h, v$session w1, v$session h1where(((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.kgllktypeand w.kgllkhdl = h.kgllkhdland w.kgllkuse = w1.saddrand 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 andshould 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 followingqueries, e.g.a. for the blocked session:

select distinct kglnaobj from x$kgllk wherekgllkuse in (select saddr from v$session where sid = 16);

b. for the blocking session

select distinct kglnaobj from x$kgllk wherekgllkuse in (select saddr from v$session where sid = 12);

One of those objects can be the cursor or statement that each session isexecuting/trying to execute.

You can also use the $ORACLE_HOME/rdbms/admin/utldtree.sql utility to find outhow 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 holdingsession. A variant script on utldtree.sql stands in Note:139594.1 andgives 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

Page 3: How to Analyze ORA-04021 or ORA-4020 Errors

- 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 thatneed 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 anotherview, package using another package or view).

The DDL cursors themselves require a null lock (localisation job in librarycache) and an exclusive pin when executing. Query and DML cursors requiresa null lock and shared pin when executing.

The locks/pins are held during the duration of the SQL statement, andreleased 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 notaffect 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 isexecuting a long running package and you want to recreate it. It mostlyresult 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) objectsto be compiled were executed within the same PLSQL block prior to theinvocation 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/SQLclient (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 CACHENote:122793.1 HOW TO FIND THE SESSION HOLDING A LIBRARY CACHE LOCKNote:152915.1 Unable to Add JServer to the Database, ORA-04021Note:1070137.6 V8.X ORA-4021 WHEN COMPILING PACKAGE WITH COMPILE_SCHEMANote:139594.1 Script To List Recursive Dependency Between ObjectsNote: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 clientsNote:166924.1 Ora-04020 Deadlocks Most Common Causes

Related

Products

Oracle Database Products > Oracle Database > Oracle Database > Oracle Server - Enterprise EditionOracle Database Products > Oracle Database > Oracle Database > Oracle Server - Enterprise Edition

Page 4: How to Analyze ORA-04021 or ORA-4020 Errors

Keywords

LOCKErrors

ORA-4021; ORA-4020; 4021 ERROR; 04021 ERROR; 4020 ERROR

Back to top

Copyright (c) 2007, 2010, Oracle. All rights reserved. Legal Notices and Terms of Use | Privacy Statement