[转载]library cache pin原理
<P>信息来源:邪恶八进制信息安全团队 </P><P>An Oracle instance has a library cache that contains the description of<BR>different types of objects e.g. cursors, indexes, tables, views, procedures,<BR>... Those objects cannot be changed when they are used. They are locked by a<BR>mechanism based on library locks and pins. A session that need to use an object<BR>will first acquire a library lock in a certain mode (null, shared or exclusive)<BR>on the object, in order to prevent other sessions from accessing the same<BR>object (e.g. exclusive lock when recompiling a package or view) or to maintain<BR>the object definition for a long time. Locking an object is sometimes referred <BR>as the job to locate it in the library cache and lock it in a certain mode. <BR>If the session wants to modify or examine the object, it must acquire after<BR>the lock also a pin in a certain mode (again null, shared or exclusive).<BR><BR>Each SQL statement that want to use/modify objects that are locked or pinned<BR>and whose lock/pin mode is incompatible with the requested mode, will wait<BR>on events like 'library cache pin' or 'library cache lock' until a timeout<BR>occurs. The timeout normally occurs after 5 minutes and the SQL statement<BR>then ends with an ORA-4021. If a deadlock is detected, an ORA-4020 is given<BR>back. <BR><BR><BR>Dealing with slow downs related to "mysterious" library cache pins<BR>and load locks we should look for the reason of the database object<BR>invalidations. They are likely to be triggered by actions causing<BR>changes to "LAST_DDL" attribute of database objects that have other <BR>dependent ones. Typically they are the object maintenance operations -<BR>ALTER, GRANT, REVOKE, replacing views, etc. This behavior is described <BR>in Oracle Server Application Developer's Guide as object dependency <BR>maintenance. <BR><BR>After object invalidation, Oracle tries to recompile the object at the <BR>time of the first access to it. It may be a problem in case when other <BR>sessions have pinned the object to the library cache. It is obvious that <BR>it is more likely to occur with more active users and with more complex <BR>dependencies (eg. many cross-dependent packages or package bodies).<BR>In some cases waiting for object recompilation may even take hours<BR>blocking all the sessions trying to access it. <BR><BR><BR>ORA-04021 timeout occurred while waiting to lock object %s%s%s%s%s". <BR>Cause:While trying to lock a library object, a time-out occurred. <BR>Action: Retry the operation later. <BR><BR>ORA-04020 deadlock detected while trying to lock object %s%s%s%s%s <BR>Cause:While trying to lock a library object, a deadlock is detected. <BR>Action: Retry the operation later. <BR>(see <NOTE.166924.1>) <BR><BR>2. Which views can be used to detect library locking problems? <BR>---------------------------------------------------------------- <BR><BR>Different views can be used to detect pin/locks: <BR><BR>DBA_KGLLOCK : one row for each lock or pin of the instance <BR>-KGLLKUSEsession address <BR>-KGLLKHDLPin/lock handle <BR>-KGLLKMOD/KGLLKREQHolding/requested mode <BR>0 no lock/pin held <BR>1 null mode <BR>2 share mode <BR>3 exclusive mode <BR>-KGLLKTYPE Pin/lock <BR>(created via the $ORACLE_HOME/rdbms/admin/catblock.sql) <BR><BR>V$ACCESS : one row for each object locked by any user <BR>-SID session sid <BR>-OWNER username <BR>-OBJECT object name <BR>-TYPE object type <BR><BR>V$DB_OBJECT_CACHE : one row for each object in the library cache <BR>-OWNER object owner <BR>-NAME object name or cursor text <BR>-TYPE object type <BR>-LOCKS number of locks on this object <BR>-PINS number of pins on this object <BR><BR>DBA_DDL_LOCKS: one row for each object that is locked (exception made of the cursors) <BR>-SESSION_ID <BR>-OWNER <BR>-NAME <BR>-TYPE <BR>-MODE_HELD <BR>-MODE_REQUESTED <BR><BR>V$SESSION_WAIT : each session waiting on a library cache pin or lock is blocked by some other session <BR>-p1 = object address <BR>-p2 = lock/pin address <BR><BR><BR>3. How to find out why an ORA-4021 occurs? <BR>-------------------------------------------- <BR><BR>When you execute the statement that generates the ORA-4021, it is possible<BR>during the delay of 5 minutes to detect the reason for the blocking situation.<BR>Following query can be used to find the blocking and waiting sessions: <BR><BR>FYI: You need to run the script called "catblock.sql" first.<BR>===This script can be found in:$ORACLE_HOME/rdbms/admin/catblock.sql<BR><BR><BR>select /*+ ordered */ w1.sidwaiting_session, <BR> h1.sidholding_session, <BR> w.kgllktype lock_or_pin, <BR> w.kgllkhdl address, <BR> decode(h.kgllkmod,0, 'None', 1, 'Null', 2, 'Share', 3, 'Exclusive', <BR> 'Unknown') mode_held,<BR> decode(w.kgllkreq,0, 'None', 1, 'Null', 2, 'Share', 3, 'Exclusive', <BR> 'Unknown') mode_requested <BR>from dba_kgllock w, dba_kgllock h, v$session w1, v$session h1 <BR>where <BR>(((h.kgllkmod != 0) and (h.kgllkmod != 1) <BR> and ((h.kgllkreq = 0) or (h.kgllkreq = 1))) <BR> and <BR> (((w.kgllkmod = 0) or (w.kgllkmod= 1)) <BR> and ((w.kgllkreq != 0) and (w.kgllkreq != 1)))) <BR>andw.kgllktype =h.kgllktype <BR>andw.kgllkhdl =h.kgllkhdl <BR>andw.kgllkuse = w1.saddr <BR>andh.kgllkuse = h1.saddr <BR>/ <BR><BR>The result looks like: <BR><BR>WAITING_SESSION HOLDING_SESSION lock ADDRESSMODE_HELD MODE_REQU <BR>--------------- --------------- ---- -------- --------- --------- <BR> 16 12 Pin03FA2270 Share Exclusive <BR><BR>The object that is locked can be found with v$object_dependency and<BR>should be the same as the one mentioned in the ORA-4021 error message. <BR>e.g. <BR>select to_name from v$object_dependency where to_address = '03FA2270'; <BR>should give: <BR><BR>TO_NAME <BR>------------- <BR>DBMS_PIPE <BR><BR>You can find which library objects are used by each session via following<BR>queries, e.g.<BR>a. for the blocked session: <BR><BR>select distinct kglnaobj from x$kgllkwhere<BR>kgllkuse in (select saddr from v$session where sid = 16); <BR><BR>b. for the blocking session <BR><BR>select distinct kglnaobj from x$kgllkwhere<BR>kgllkuse in (select saddr from v$session where sid = 12); <BR><BR>One of those objects can be the cursor or statement that each session is<BR>executing/trying to execute. <BR><BR>You can also use the $ORACLE_HOME/rdbms/admin/utldtree.sql utility to find out<BR>how the dependency tree looks like and which objects are dependent on e.g.<BR>DBMS_PIPE. One of those objects will be the sql statement of the holding<BR>session. A variant script on utldtree.sql stands in [NOTE:139594.1] and<BR>gives which objects an object depends on. <BR><BR><BR><BR><BR> Library cache pins are used to manage library cache concurrency. <BR> Pinning an object causes the heaps to be loaded into memory (if not already loaded).<BR> PINS can be acquired in NULL, SHARE or EXCLUSIVE modes and can be considered like a special form of lock. <BR> A wait for a "library cache pin" implies some other session holds that PIN in an incompatible mode. <BR> <BR> P1 = Handle address <BR> P2 = Pin address <BR> P3 = Encoded Mode & Namespace <BR> <BR> ·Handle address<BR> ~~~~~~~~~~~~~~~~ <BR> Use P1RAW rather than P1 <BR> This is the handle of the library cache object which the waiting session wants to acquire a pin on. <BR><BR> <BR>查找library cache对象<BR>~~~~~~~~~~~~~~<BR> The actual object being waited on can be found using <BR> SELECT kglnaown "Owner", kglnaobj "Object" <BR> FROM x$kglob <BR> WHERE kglhdadr='&P1RAW' <BR> ; <BR> ·Pin address<BR> ~~~~~~~~~~~~~<BR> Use P2RAW rather than P2 <BR> This is the address of the PIN itself. <BR> ·Encoded Mode & Namespace <BR> ~~~~~~~~~~~~~~~~~~~~~~~~~<BR> In Oracle 7.0 - 8.1.7 inclusive the value is 10 * Mode + Namespace. <BR> In Oracle 9.0 - 9.2 inclusive the value is 100 * Mode + Namespace. <BR> <BR> Where: <BR> <BR> Mode is the mode in which the pin is wanted. This is a number thus: <BR> o 2 - Share mode <BR> o 3 - Exclusive mode <BR> <BR> Namespace is just the namespace number of the namespace in the library cache in which the required object lives: <BR> o 0 SQL Area <BR> o 1 Table / Procedure / Function / Package Header <BR> o 2 Package Body <BR> o 3 Trigger <BR> o 4 Index <BR> o 5 Cluster <BR> o 6 Object <BR> o 7 Pipe <BR> o 13 Java Source <BR> o 14 Java Resource <BR> o 32 Java Data<BR></P>
页:
[1]