(http://www.dbsnake.net/library-cache-lock-and-pin.html)

Posted: December 16, 2011 | Author: Cui Hua | Filed under: Oracle | Tags: library cache lock and pin | 3Comments »

可能有很多朋友从来就没有搞清楚过到底什么是library cache lock和library cache pin,它们到底是enqueue还是latch?它们的作用是什么?

这里我尝试对上述问题做一番解释,这些解释可能是有问题的,因为里面包含了我的一些猜测。

最近连续写了一些基于我的猜测、没有确凿证据的文章,这也许不太合适。

我们通常说的library cache locklibrary cache pinenqueue,不是latch,它们是两种DDL lock。但需要我们注意的是,在11gR1之前,Oracle中又存在名为library cache locklibrary cache pinlatch

是不是感觉很混乱?没关系,我们一点一点往下看。很抱歉,我这里引用了大量英文,因为我觉得如果翻译出来就失去了原先的味道。

1、  作为enqueuelibrary cache locklibrary cache pin的作用是什么?

Both library cache lock and library cache pin are provided to access objects in the library cache.Library cache lock manages concurrency between processes, whereas library cache pin manages cache coherence. In order to access an object in library cache, a process must first lock the library cache object handle, and then pin the object data heap itself. Requests for both library cache lock and library cache pin will wait until granted. This is a possible source of contention, because there is no NOWAIT request mode.

By acquiring a library cache lock on the library cache object handle, a process can prevent other processes from accessing the object, or even finding out what type it is. It can even maintain a dependency on an object without preventing other processes from accessing the object. Acquiring a library cache lock is also the only way to locate an object in cache—a process locates and locks an object in a single operation.

If the process wants to actually examine or modify the object, then it must acquire a library cache pin on the object data heap itself (after acquiring a library cache lock on the library cache object handle).Pinning the object causes information about the object and its data heaps to be loaded into memory if they were not already there. This information is guaranteed to remain in memory at least until the pin is released. Locks and pins are externalized in X$KGLLK and X$KGLPN, respectively.

2、  作为enqueuelibrary cache locklibrary cache pin有哪几种lock mode

a)         Library cache lock有三种lock mode,分别是share、exclusive和null。A process acquires a share library cache lock if it intends only to read the object. For example, it wants to reference the object during compilation. A process acquires an exclusive library cache lock if it intends to create or modify the object. For example, it wants to drop the object from the database. Null library cache locks are a special case. They are acquired on objects that are to be executed like child cursor, procedure, function, package, or type body. You can use them to maintain an interest on an object for a long period of time (session persistency), and to detect if the object becomes invalid. You can break null library cache lock at any time. This is used as a mechanism to notify a session that an executable object is no longer valid. If a null library cache lock is broken, and thus the object is invalidated, then it is an indication to the user who was holding the null library cache lock that the object needs to be recompiled. A null library cache lock is acquired during the parse phase of SQL statement execution and is held as long as the shared SQL area for that statement remains in the shared pool. A null library cache lock does not prevent any DDL operation, and can be broken to allow conflicting DDL operations, hence the term “breakable parse lock.” A null library cache lock on an object is broken when there is an exclusive library cache pin on the object.

b)         Library cache pin有两种lock mode,分别是share和exclusive。 When a process pins an object data heap that is not in memory, the process can determine whether the data heap is to be loaded in the PGA or SGA. An object must be pinned in Exclusive mode if it is to be modified. However, the process first will always pin the object in Share mode, examine it for errors and security checks, and then, if necessary, (such as needing modification) pin it in Exclusive mode. An object is never pinned in Exclusive mode if only read access is required.This is because all dependent transient objects (cursors) are invalidated (null locks broken) when an object is unpinned from Exclusive mode. The effect would be unnecessary recompilation and reparsing of all dependent packages, procedures, and functions.

3、  作为latchlibrary cache locklibrary cache pin的作用是什么?

这是一个很纠结的问题,既然已经有了作为enqueue的library cache lock和library cache pin,为什么在11gR1以前,Oracle里还有同名latch,而且明显这些同名latch是在被使用:

Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.5.0

Connected as ipra

SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

————————————————– ———- ———- ———- ———- ————– —————-

library cache                                               5    9221760       1608        800           2596           76766

library cache lock                                          6   13548247        582          6              0                0

library cache lock allocation                               3     208273          0          0              0                0

library cache pin                                           6    4207462        193          0              2                0

library cache hash chains                                   9          0          0          0              0                0

library cache pin allocation                                3      57276          0          0              0                0

library cache load lock                                     5      24848          0          0              1                0

7 rows selected

从结果里我们可以看到,对于10.2.0.5而言,Oracle存在7种跟library cache相关的latch,除了library cache hash chains latch之外,其他的跟library cache相关的latch,Oracle都有使用。

那么library cache lock latch、library cache pin latch以及大家最耳熟能详的library cache latch等等,这些latch是做什么用的呢?

也许我们可以从下面的一段文字中找到答案:

The library cache latches serialize access to the objects in the library cache. Access to library cache objects always occurs through library cache locks. Because locking an object is not an atomic instruction, a library cache latch is acquired before the library cache lock request and is released after it. For most operations, the library cache latches are used, and therefore they can become a point of contention.

If an object is not in memory, then a library cache lock cannot be acquired on it. In order to prevent multiple processes to request the load of the same object simultaneously, another latch must be acquired before the load request. This is the library cache load lock latch. The library cache load lock latch is taken and held until a library cache load lock is allocated, then the latch is released. Loading of the object is performed under the library cache load lock and not under the library cache load lock latch as it may take quite a long time.

这里提到了几点值得我们关注:

a)        Oracle使用上述library cache latches(包括library cache latch、library cache lock latch、library cache pin latch、library cache pin allocation latch、library cache load lock latch)的目的是控制并发访问library cache object所需要的相关的enqueue或者是为了控制并发访问library cache中的相关的内存结构,比如用相关的library cache lock latch控制并发获得library cache lock。这里我猜测Oraclelibrary cache lock latch控制并发获得library cache lock,用library cache pin latch控制并发获得library cache pin,用library cache load lock latch控制并发获得library cache load locklibrary cache latch去控制并发访问library cache object handle中的某些结构,如library cache object handle中的flag中的special status flag (special status flags are protected by the library cache latch. Examples of these flags indicate that: The object is valid; The object is authorized; The object has compilation errors)。

b)         Library cache load lock是另外一种enqueue。The session tries to find the library cache load lock for the database object so that it can load the object. The library cache load lock is always obtained in Exclusive mode, so that no other process can load the same object. If the library cache load lock is busy the session will wait on this event until the lock becomes available.

好了,现在我们来验证一下,还是上述10.2.0.5的环境,我将上述sql(select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’)马上再执行一遍,这是软解析,必然要获得library cache lock,不需要获得library cache load lock,所以对应的latch应该表现为library cache lock latch的gets增加,library cache load lock latch的gets不变:

SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

————————————————– ———- ———- ———- ———- ————– —————-

library cache                                               5    9222166       1608        800           2596           76766

library cache lock                                          6   13548760        582          6              0                0

library cache lock allocation                               3     208287          0          0              0                0

library cache pin                                           6    4207656        193          0              2                0

library cache hash chains                                   9          0          0          0              0                0

library cache pin allocation                                3      57278          0          0              0                0

library cache load lock                                     5      24848          0          0              1                0

7 rows selected

从结果里我们可以看到,library cache lock latch的gets从13548247递增到了13548760,library cache pin latch的gets从4207462递增到了4207656,但library cache load lock latch的gets还是保持24848不变。

现在我们来让library cache load lock latch的gets发生变化,这是非常容易的事情,我们只需要执行一个需要硬解析的sql就可以了:

SQL> select * from scott.emp_temp;

EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO  ISINSPECT

—– ———- ——— —– ———– ——— ——— —— ———-

SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

————————————————– ———- ———- ———- ———- ————– —————-

library cache                                               5    9223549       1608        800           2596           76766

library cache lock                                          6   13550296        582          6              0                0

library cache lock allocation                               3     208348          0          0              0                0

library cache pin                                           6    4208118        193          0              2                0

library cache hash chains                                   9          0          0          0              0                0

library cache pin allocation                                3      57294          0          0              0                0

library cache load lock                                     5      24856          0          0              1                0

7 rows selected

由于我们执行了一个需要硬解析的sql,导致Oracle需要获得library cache load lock以便load相关信息到这个sql的子cursor的heap 6中,而要获得library cache load lock,必须先持有library cache load lock latch。从上述结果中我们可以看到,此时library cache load lock latch的gets已经发生了变化,从24848递增到了24856。

接下来我们再来看一看上述library cache latches的子latch情况:

SQL> show parameter cpu_count

NAME                                 TYPE        VALUE

———————————— ———– ——————————

cpu_count                            integer     2

这里cpu的个数为2,显然上述library cache latches的子latch应该为3:

SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch_children where name like ‘library%’;

NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

————————————————– ———- ———- ———- ———- ————– —————-

library cache                                               5    3274551       1301         94            187                0

library cache                                               5    2218356        116         80            933                0

library cache                                               5    3731320        191        626           1476           76766

library cache lock                                          6    5339737        362          3              0                0

library cache lock                                          6    6223353        194          3              0                0

library cache lock                                          6    1987799         26          0              0                0

library cache pin                                           6    1484918        184          0              0                0

library cache pin                                           6     891695          3          0              2                0

library cache pin                                           6    1831837          6          0              0                0

library cache pin allocation                                3      23177          0          0              0                0

library cache pin allocation                                3       8272          0          0              0                0

library cache pin allocation                                3      25849          0          0              0                0

library cache lock allocation                               3      75900          0          0              0                0

library cache lock allocation                               3      28229          0          0              0                0

library cache lock allocation                               3     104237          0          0              0                0

library cache hash chains                                   9          0          0          0              0                0

library cache hash chains                                   9          0          0          0              0                0

library cache hash chains                                   9          0          0          0              0                0

18 rows selected

注意,结果里并没有library cache load lock latch,说明library cache load lock latch没有children,它是一个solitary类型的latch。

从10.2.0.2开始,Oracle将_kks_use_mutex_pin的默认值改成了true,这意味着从10.2.0.2开始,Oracle里将再不会有针对cursor的library cache pin等待,取而代之的是mutex等待,具体表现为cursor: pin *等待,如cursor: pin S wait on X。

这里需要我们了解的是:

a)         从10.2.0.2开始,Oracle只是用mutex替代了针对cursor的library cache pin,这并不代表从10.2.0.2开始Oracle里就没有library cache pin等待了。比如这个例子里的library cache pin等待就发生在10.2.0.4中:http://www.dbsnake.net/solve-library-cache-pin.html

b)         Mutex和latch是互相独立,没有任何关系的:Latches and mutexes are independent mechanisms i.e. a process can hold a latch and a mutex at the same time. In the case of process death, latches are always cleaned up before mutexes. There is no generic mutex deadlock detection (unlike latches). There is no mutex/latch hierarchy.

从11gR1开始,Oracle用mutex替换了library cache latches,并引了一个新的等待事件:library cache: mutex *,我们来看一下这个知识点:

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Connected as nbs

SQL>  select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

NAME                                                                 LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

—————————————————————- ———- ———- ———- ———- ————– —————-

library cache load lock                                                   5          0          0          0             0                0

SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch_children where name like ‘library%’;

NAME                                                                 LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

—————————————————————- ———- ———- ———- ———- ————– —————-

从结果里我们可以看到,在11.2.0.1里,各种library cache latches都没有了,只剩下了library cache load lock latch,而且Oracle还没有使用这个latch,因为gets是0。

转载于:https://www.cnblogs.com/taowang2016/archive/2013/02/21/2920439.html

(转载)library cache lock和library cache pin到底是什么相关推荐

  1. 密码错误频繁登录引发的“library cache lock”或“row cache lock”等待

    密码错误频繁登录引发的"library cache lock"或"row cache lock"等待 对于正常的系统,由于密码的更改,可能存在某些被遗漏的客户端 ...

  2. 并行insert出现library cache lock与cursor: pin S wait on X等待问题记录

    一. 故障现象与紧急处理 开发反馈凌晨5点左右应用出现大量报错 ORA-04021: timeout occurred while waiting to lock object,并且集中出现在inse ...

  3. 经验:Library Cache Lock之异常分析-云和恩墨技术通讯精选

    亲爱的读者朋友: 为了及时共享行业案例,通知共性问题,达成共享和提前预防,我们整理和编辑了<云和恩墨技术通讯>,通过对过去一段时间的知识回顾,故障归纳,以期提供有价值的信息供大家参考.同时 ...

  4. 实战课堂:数据库高Library Cache Lock导致Hang的故障分析

    编辑手记:在现实的生产环境中,DBA可能遭遇到各种各样的异常,或简单.或复杂,但是无一不考验DBA的经验和能力,在『实战课堂』栏目中,我们将整理和分享来自云和恩墨一线的各种案例,以其帮助走在DBA道路 ...

  5. Oracle 11g业务用户更改密码后产生大量library cache lock等待

    DB Version:11.2.0.4+RAC OS Version:Oracle Linux Server 6.7 在我的一个两节点RAC数据库上,alert日志中平均每3秒左右就会产生一条连接超时 ...

  6. 五月数据库技术通讯丨Oracle 12c因新特性引发异常Library Cache Lock等待

    每月关注:35页数据库技术干货,汇总一个月数据库行业热点事件.新的产品特性,包括重要数据库产品发布.警报.更新.新版本.补丁等. 亲爱的读者朋友: 为了及时共享行业案例,通知共性问题,达成共享和提前预 ...

  7. 记一次library cache lock/library cache pin导致的函数编译hang住分析及处理过程

    墨墨导读:业务在进行alter function my_function_name compile时,有两个函数编译无法通过,现象就是会hang住,这里分享处理的整个过程. 一.前言 业务在进行alt ...

  8. oracle library cache lock,【案例】Oracle等待事件library cache lock产生原因和解决办法...

    [案例]Oracle等待事件library cache lock产生原因和解决办法 时间:2016-12-07 18:56   来源:Oracle研究中心   作者:网络   点击: 次 天萃荷净 O ...

  9. oracle library cache lock,【DB】彻底搞清楚library cache lock的成因和解决方法(一)

    问题描述: 接到应用人员的报告,说是在任何对表CSNOZ629926699966的操作都会hang,包括desc CSNOZ629926699966,例如: > sqlplus SQL*Plus ...

最新文章

  1. Django使用已经存有数据的mysql数据库
  2. 百度今晨7:20分“宕机”
  3. (原創) 如何利用copy() algorithm將array輸出到cout? (C/C++) (STL)
  4. win7系统自带截图工具快捷键是什么?怎么设置快捷键
  5. (二)linux内核准备及编译
  6. 计算机导论中的名词解释,计算机导论期末考试试题及答案
  7. php 如何实现关键字查找,php中如何通过关键字查找文件中包含该关键字的所有行内容呢...
  8. Java GC原理简单讲解
  9. set注意点map遍历
  10. 二、bootstrap4基础(flex布局)
  11. php 5.5 sqlserver,thinkphp5 连接sqlserver windows
  12. python数据分析-《Python数据分析与数据化运营》电子版
  13. Why Service Mesh
  14. 深圳市社会医疗保险门诊大病管理办法
  15. 【风光摄影】用滤镜在前期控制完美光比
  16. 添加网站验证,让搜索引擎收录你的网站
  17. 神经网络参数个数计算,神经网络的参数设置
  18. vue+js数据处理,对数组重新构造
  19. 第二章 第二十节 最值求解——刁老师
  20. 中央一号文件力推乡村振兴,VR全景如何构建数字乡村?

热门文章

  1. 旅行照片剪辑--西安篇
  2. 猿人学之访问逻辑罗生门
  3. 域控中将计算机账户移动到特定OU下
  4. 【Algorithm】种子填充算法
  5. JavaScript 实现动物识别专家系统交互演示
  6. Java版本工程行业管理系统源码-专业的工程管理软件-提供一站式服务
  7. 雅虎黑客事件严重 Verizon表示可能放弃收购
  8. mac服务器文件同步软件,[MACOS]使用fswatch和SCP配合实现自动单向实时同步文件
  9. docker启动ssh、xrdp命令
  10. 【VUE】源码分析 - computed计算属性的实现原理