转载

深入理解shared pool共享池之library cache的library cache pin系列三

背景

  关于library cache相关的LATCH非常多,名称差不多,我相信一些人对这些概念还是有些晕,我之前也有些晕,希望此文可以对这些概念有个更为清晰的理解,本文主要学习library cache pin

前期相关文章


http://blog.itpub.net/9240380/


结论

1,library cache pin是用于并发的一种特殊锁,它不是LATCH
2,library cache pin适用于正在执行存储过程时,不能同时去编译存储过程,后者则会产生此等待事件,目的即防止并发修改存储过程的定义
3,library cache pin的分析可采用如下脚本:
整合下上述的SQL为一个SQL,便于运维使用,直接查出造成library cache pin的原凶
SQL> select sid,serial#,event,v$sql.sql_id,v$sql.sql_text   from dba_kgllock,v$session,v$sql where kgllktype='Pin' and kgllkmod<>0 and dba_kgllock.kgllkuse=v$session.saddr and v$session.sql_id=v$sql.sql_id;


       SID    SERIAL# EVENT                          SQL_ID        SQL_TEXT
---------- ---------- ------------------------------ ------------- --------------------------------------------------
       116        153 SQL*Net message from client    6t5v50dgh7paq SELECT COUNT(1) FROM T_LOCK_TEST




4,如果产生了library cache pin,是基于存储过程对应的library cache bucket,
   同时会在BUCKET中产生PIN OWNER及PIN WAITER,否则会消失
   通过pin waiter及pin owner结合v$session及dba_kgllock可以定位PIN的持有者及等待者


5,所以在业务高峰期间,一实不要执行编译存储过程的工作


6,dba_kgllock不止记录pin也记录lock,关于dba_kgllock含义请见官方手册
7,library cache pin不会在v$lock及dba_dml_locks和dba_waiters,dba_blockers出现相关任何信息   
   可见不同的字典适用于不同的场景,一定要深入理解这些字典的用途      


测试



----oracle version
SQL> select * from v$version where rownum=1;


BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi






----与library cache相关的latch,概念极为容易迷惑人
SQL> select latch#,name from v$latch where lower(name) like '%library cache pin%';


    LATCH# NAME
---------- ------------------------------
       220 library cache pin allocation
       219 library cache pin




---与library cache相关的锁,有控制并发访问lco,lco内容的,控制跨节点访问lco的失效的
SQL> select type,name,id1_tag,id2_tag,is_user,description from v$lock_type where lower(name) like '%library%';


TYPE                 NAME                           ID1_TAG                        ID2_TAG              IS_ DESCRIPTION
-------------------- ------------------------------ ------------------------------ -------------------- --- ----------------------------------------------------------------------
V                    Library Cache Lock 3           hash value                     hash value           NO  Synchronizes accesses to library cache objects
E                    Library Cache Lock 2           hash value                     hash value           NO  Synchronizes accesses to library cache objects
L                    Library Cache Lock 1           hash value                     hash value           NO  Synchronizes accesses to library cache objects
Y                    Library Cache Pin 3            hash value                     hash value           NO  Synchronizes accesses to the contents of library cache objects
G                    Library Cache Pin 2            hash value                     hash value           NO  Synchronizes accesses to the contents of library cache objects
N                    Library Cache Pin 1            hash value                     hash value           NO  Synchronizes accesses to the contents of library cache objects
IV                   Library Cache Invalidation     object #                       time stamp           NO  Synchronizes library cache object invalidations across instances


7 rows selected.


---library cache lock模拟小记
http://blog.itpub.net/9240380/viewspace-759281/,可见是如果某对象正在执行时,会对其对象相引用的对象加library cache lock


SQL> select latch#,name from v$latch_children where lower(name) like '%library cache lock%';


    LATCH# NAME
---------- ------------------------------
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock
       218 library cache lock


---library cache lock的含义,引自官方手册 Oracle? Database Reference 11g Release 2 (11.2)之Descriptions of Wait Events


library cache lock含义
1,用于并发控制对于library cache的访问,在library cache  的library cache handle加锁,这个目的:
   a,防止同一时间只能一个应用访问相同的对象
   b,确保在一个操作的既定时间内,确保相互信赖对象的一致性,即确保依赖对象不能被修改
   c,同时会持一个锁用于library cache  object


library cache lock的参数
handle address 加载的对象地址
lock address 加载的锁地址,它不是正常的哪个锁或LATCH,它是一个状态对象
mode 指定加载对象是以何种模式加载
namespace 命名空间


namespace


The name of the object namespace as it is displayed in V$DB_OBJECT_CACHE view.


name and type


The name or "type" of the enqueue or global lock can be determined by looking at the two high order bytes of P1 or P1RAW. The name is always two characters. Use the following SQL statement to retrieve the lock name.


select chr(bitand(p1,-16777216)/16777215)||
      chr(bitand(p1,16711680)/65535) "Lock"
from v$session_wait
where event = 'DFS enqueue lock acquisition';


---namespace的信息源自v$db_object_cache,也可以直观理解不同的对象类型,当然不完全一致
SQL> select distinct namespace from v$db_object_cache order by 1;


NAMESPACE
----------------------------
BODY
CLUSTER
CURSOR
INDEX
INVALID NAMESPACE
PUB_SUB
RSRC CONSUMER GROUP
RSRC PLAN
RULESET
STORED OUTLINE
SUBSCRIPTION


NAMESPACE
----------------------------
TABLE/PROCEDURE
TRIGGER


13 rows selected.




可见如下前者是latch地址,后者是library cache handle address,到底有何区别呢
SQL> select name,parameter1,parameter2,parameter3,wait_class from v$event_name where lower(name) like '%library cache lock%';


NAME                           PARAMETER1                     PARAMETER2                     PARAMETER3                     WAIT_CLASS
------------------------------ ------------------------------ ------------------------------ ------------------------------ --------------------
latch: library cache lock      address                        number                         tries                          Concurrency
library cache lock             handle address                 lock address                   100*mode+namespace             Concurrency


---为了直观分析,形成测试用例


SQL> select sid from v$mystat where rownum=1;


       SID
----------
       116




SQL> create table t_lock_test(a int,b int);


Table created.




SQL> insert into t_lock_test select level,level from dual connect by level<=1500000;


1500000 rows created.


SQL> commit;


Commit complete.


SQL> insert into t_lock_test select level,level from dual connect by level<=1500000;


1500000 rows created.


SQL> commit;


Commit complete.


SQL> insert into t_lock_test select level,level from dual connect by level<=1500000;


1500000 rows created.


SQL> commit;


Commit complete.


SQL> create or replace procedure proc_hard_parse
  2  as
  3  v_cnt pls_integer;
  4  begin
for i in 1..100000000 loop
  6  select count(1) into v_cnt  from t_lock_test;
  7  end loop;
  8  end;
  9  /


Procedure created.       


SQL> exec proc_hard_parse;




SQL> select sid from v$mystat where rownum=1;


       SID
----------
       121






SQL> alter procedure proc_hard_parse compile;
hang住


--从这里可见library cache pin的p1text可见它不是latch
SQL> select sid,event,p1text,p2text,p3text,wait_class from v$session_wait where sid in (116,121);


       SID EVENT                          P1TEXT                         P2TEXT                         P3TEXT                         WAIT_CLASS
---------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ -------------------------
       116 SQL*Net message from client    driver id                      #bytes                                                        Idle
       121 library cache pin              handle address                 pin address                    100*mode+namespace             Concurrency




SQL> select sid,event,p1,p2,p3 from v$session_wait where sid in (116,121);


       SID EVENT                                  P1         P2         P3
---------- ------------------------------ ---------- ---------- ----------
       116 SQL*Net message from client    1650815232          1          0
       121 library cache pin              2739958280 2663017760        301


可见library cache pin不是锁
SQL> select sid,type,id1,id2,lmode,request from v$lock where sid in (121,116);


no rows selected


通过dba_ddl_locks可以查询出相关的信息,可见在测试的存储过程上加了排它锁,但是通过dba_ddl_locks查不出到底什么原因引发了library cache pin
SQL> select session_id,name,type,mode_held,mode_requested from dba_ddl_locks where session_id in (116,121) and name='PROC_HARD_PARSE';


SESSION_ID NAME                           TYPE                                     MODE_HELD MODE_REQU
---------- ------------------------------ ---------------------------------------- --------- ---------
       116 PROC_HARD_PARSE                Table/Procedure/Type                     Null      None
       121 PROC_HARD_PARSE                Table/Procedure/Type                     Exclusive None






SQL> select count(*) from dba_dml_locks;


  COUNT(*)
----------
         0




SQL> select count(*) from dba_waiters;


  COUNT(*)
----------
         0


SQL> select count(*) from dba_blockers;


  COUNT(*)
----------
         0


---查看与LOCK相关的字典,想办法找到产生这个library cache pin的源头是什么
SQL> select table_name from dict where lower(table_name) like '%lock%';


TABLE_NAME
------------------------------
DBA_KGLLOCK
DBA_LOCK
DBA_LOCK_INTERNAL
DBA_DML_LOCKS
DBA_DDL_LOCKS


SQL> desc dba_kgllock;
 Name              Null?    Type
 ----------------- -------- ------------
 KGLLKUSE                   RAW(8)
 KGLLKHDL                   RAW(8)
 KGLLKMOD                   NUMBER
 KGLLKREQ                   NUMBER
 KGLLKTYPE                  VARCHAR2(4)


 SQL> select kgllkuse,kgllkhdl,kgllkmod,kgllkreq,kgllktype from dba_kgllock where rownum<=1;


KGLLKUSE         KGLLKHDL           KGLLKMOD   KGLLKREQ KGLL
---------------- ---------------- ---------- ---------- ----
00000000A4748578 00000000A3AE6128          1          0 Lock


---转化上述的library cache pin的p1text,即handle address
SQL> select to_char('2739958280','xxxxxxxx') from dual;


TO_CHAR('
---------
 a3507208


经查官方手册可见kgllkuse即会话的地址
SQL> select kgllkuse,kgllkhdl,kgllkmod,kgllkreq,kgllktype from dba_kgllock where kgllkhdl='00000000A3507208';


KGLLKUSE         KGLLKHDL           KGLLKMOD   KGLLKREQ KGLL
---------------- ---------------- ---------- ---------- ----
00000000A4726CC0 00000000A3507208          1          0 Lock
00000000A472D818 00000000A3507208          3          0 Lock
00000000A472D818 00000000A3507208          0          3 Pin --等待pin
00000000A4726CC0 00000000A3507208          2          0 Pin  --持有pin




SQL> select sid,saddr from v$session where sid in (116,121);


       SID SADDR
---------- ----------------
       116 00000000A4726CC0
       121 00000000A472D818


查询持pin的会话及SQL
SQL> select sid,serial#,sql_id from v$session where saddr='00000000A4726CC0';


       SID    SERIAL# SQL_ID
---------- ---------- -------------
       116        153 6t5v50dgh7paq




SQL> col sql_text for a50
SQL> select sql_id,sql_text from v$sql where sql_id='6t5v50dgh7paq';


SQL_ID        SQL_TEXT
------------- --------------------------------------------------
6t5v50dgh7paq SELECT COUNT(1) FROM T_LOCK_TEST




整合下上述的SQL为一个SQL,便于运维使用,直接查出造成library cache pin的原凶
SQL> select sid,serial#,event,v$sql.sql_id,v$sql.sql_text   from dba_kgllock,v$session,v$sql where kgllktype='Pin' and kgllkmod<>0 and dba_kgllock.kgllkuse=v$session.saddr and v$session.sql_id=v$sql.sql_id;


       SID    SERIAL# EVENT                          SQL_ID        SQL_TEXT
---------- ---------- ------------------------------ ------------- --------------------------------------------------
       116        153 SQL*Net message from client    6t5v50dgh7paq SELECT COUNT(1) FROM T_LOCK_TEST


然后与应用人员沟通后,可以运行如下命令,杀死原凶会话即可
alter system kill session '116,153'


--可见产生library cache pin后,是基于存储过程,同时会在bucket的数据结构新增lock owner,pin owners,pin waiters
BUCKET 59215:
  LIBRARY OBJECT HANDLE: handle=a3507208 mtx=0xa3507338(0) lct=22 pct=24 cdp=0
  name=SCOTT.PROC_HARD_PARSE 
  hash=84bc90d61f9452e79e7fe2d54c82e74f timestamp=11-24-2015 02:02:36
  namespace=TABL flags=KGHP/TIM/SML/[02000000]
  kkkk-dddd-llll=0000-001d-20bf lock=X pin=S latch#=9 hpc=0008 hlc=0008  --pin正以共享模式持有,却有会话请求排它模式pin,见下面的pin wqiters
  lwt=0xa35072b0[0xa35072b0,0xa35072b0] ltm=0xa35072c0[0xa35072c0,0xa35072c0]
  pwt=0xa3507278[0x9eba6d50,0x9eba6d50] ptm=0xa3507288[0xa3507288,0xa3507288]
  ref=0xa35072e0[0xa35072e0,0xa35072e0] lnd=0xa35072f8[0xa34de600,0xa34fed28]
    DEPENDENCY REFERENCES:
    reference latch flags
    --------- ----- -------------------
     98c7ce28     2 DEP[01] whr=0 timestamp=11-24-2015 02:02:36
    LOCK OWNERS:
        lock     user  session count mode flags
    -------- -------- -------- ----- ---- ------------------------
    9ec58960 a472d818 a472d818     1 X    [00]
    9eb1fd70 a4726cc0 a4726cc0     1 N    PNC/[400]
    PIN OWNERS:
         pin     user  session     lock count mode mask
    -------- -------- -------- -------- ----- ---- ----   
    9eae0c20 a4726cc0 a4726cc0 9eb1fd70     3 S    0011  --可见PIN正以共享模式持有,这个user对应v$session.saddr即会话的地址,也对应dba_kgllock的KGLLKUSE
    PIN WAITERS:
         pin     user  session     lock count mode mask
    -------- -------- -------- -------- ----- ---- ----
    9eba6d20 a472d818 a472d818        0     0 X    0000   ---可见请求排它模式的PIN
    LIBRARY OBJECT: object=9d949488
    type=PRCD flags=EXS/LOC[0005] pflags=NST[0001] status=VALD load=0
    DEPENDENCIES: count=3 size=16
    dependency#    table reference   handle position flags
    ----------- -------- --------- -------- -------- -------------------
              0 9d94b210  9d94af50 a3a1b6f0        0 DEP[01]
              1 9d94b210  9d94afa8 a351ce38       33 DEP[01]
              2 9d94b210  9d94b000 a3929db0        0 DEP[01]
    READ ONLY DEPENDENCIES: count=1 size=16
    dependency#    table reference   handle flags
    ----------- -------- --------- -------- -------------------
              0 9d9288d0  9d9285e8 a34fa2c8 /ROD[20]
    ACCESSES: count=1 size=16
    dependency# types
    ----------- -----
              1 0009
    TRANSLATIONS: count=1 size=16
    original    final
    -------- --------
    a351ce38 a351ce38
    DATA BLOCKS:
    data#     heap  pointer    status pins change whr alloc(K)  size(K)
    ----- -------- -------- --------- ---- ------ --- -------- --------
        0 a3507148 9d9495a0 I/P/A/-/-    0 NONE   00      0.37     1.09
        2 9d9497a0 97745578 I/-/A/-/-    0 NONE   00     13.27    16.00
        4 9d928540 97b67850 I/P/A/-/-    1 NONE   00      2.26     4.00




解决library cache pin后的存储过程对应的library cache bucket内容


BUCKET 59215:
  LIBRARY OBJECT HANDLE: handle=a3507208 mtx=0xa3507338(0) lct=22 pct=24 cdp=0
  name=SCOTT.PROC_HARD_PARSE 
  hash=84bc90d61f9452e79e7fe2d54c82e74f timestamp=11-24-2015 02:02:36
  namespace=TABL flags=KGHP/TIM/SML/[02000000]
  kkkk-dddd-llll=0000-001d-20bf lock=N pin=0 latch#=9 hpc=0008 hlc=0008 --可见pin=0,可见pin有种状态,即pin=0,pin=s,pin=x(pin=0我估计是null模式的pin)
  lwt=0xa35072b0[0xa35072b0,0xa35072b0] ltm=0xa35072c0[0xa35072c0,0xa35072c0]
  pwt=0xa3507278[0xa3507278,0xa3507278] ptm=0xa3507288[0xa3507288,0xa3507288]
  ref=0xa35072e0[0xa35072e0,0xa35072e0] lnd=0xa35072f8[0xa34de600,0xa34fed28]
    DEPENDENCY REFERENCES:
    reference latch flags
    --------- ----- -------------------
     98c7ce28     2 DEP[01] whr=0 timestamp=11-24-2015 02:02:36
    LOCK OWNERS:
        lock     user  session count mode flags
    -------- -------- -------- ----- ---- ------------------------
    9eb1fd70 a4726cc0 a4726cc0     1 N    [00]
    9ec58960 a472d818 a472d818     0 N    [4044]
    LIBRARY OBJECT: object=9d949488  --而且与出现等待事件library cache pin出现的pin owner及pin waiter也消失了
    type=PRCD flags=EXS/LOC[0005] pflags=NST[0001] status=VALD load=0
    DEPENDENCIES: count=3 size=16
    dependency#    table reference   handle position flags
    ----------- -------- --------- -------- -------- -------------------
              0 9d94b210  9d94af50 a3a1b6f0        0 DEP[01]
              1 9d94b210  9d94afa8 a351ce38       33 DEP[01]
              2 9d94b210  9d94b000 a3929db0        0 DEP[01]
    READ ONLY DEPENDENCIES: count=1 size=16
    dependency#    table reference   handle flags
    ----------- -------- --------- -------- -------------------
              0 9d9288d0  9d9285e8 a34fa2c8 /ROD[20]
    ACCESSES: count=1 size=16
    dependency# types
    ----------- -----
              1 0009
    TRANSLATIONS: count=1 size=16
    original    final
    -------- --------
    a351ce38 a351ce38
    DATA BLOCKS:
    data#     heap  pointer    status pins change whr alloc(K)  size(K)
    ----- -------- -------- --------- ---- ------ --- -------- --------
        0 a3507148 9d9495a0 I/-/A/-/-    0 NONE   00      0.37     1.09
        2 9d9497a0 97745578 I/-/A/-/-    0 NONE   00     13.27    16.00
        4 9d928540 97b67850 I/-/A/-/-    0 NONE   00      2.26     4.00
  BUCKET 59215 total object count=1


正文到此结束
Loading...