Home > Articles

This chapter is from the book

Latch and Mutex Scenarios

Along with these generic methods of associating latch waits with SQLs and segments, there are diagnostic techniques specific to certain types of latch contention. We look at these as we discuss specific latch/mutex wait scenarios in the sections that follow.

Library Cache Mutex Waits

The library cache is the part of the shared pool in which cached definitions of SQL, PL/SQL, and Java classes are held. Modifications to the library cache are protected by library cache mutexes. Prior to Oracle Database 10g Release 2, they were protected by library cache latches.

Oracle maintains a cache of SQL statements in the shared pool. If a matching SQL is found in the shared pool, then most of the overhead of parsing a statement can be avoided. Such a parse is called a soft parse. If no matching SQL is found, a hard parse must be performed.

The most common reason to acquire a library cache mutex in exclusive mode is to add a new entry to the cache. This happens, for instance, when we parse a new SQL statement. Oracle looks for a matching entry in the cache, and if one is not found (a “miss”), it acquires the relevant mutex and inserts the new entry. Failing to obtain the mutex will result in a library cache: mutex X wait.

The most common cause of library cache mutex contention is excessive hard parsing caused by a failure to use bind variables in application code. For example, in the following Java snippet, a SQL statement object is created, executed, and discarded:

Statement s=oracleConnection.createStatement();
s.execute("UPDATE sh.customers SET cust_valid = 'Y'"+
          " WHERE cust_id = 1");
s.close();

If your application does nothing but execute a single SQL, then this code is probably okay. But it’s common for a SQL statement to be executed more than once, selecting or modifying different rows with each execution. This next Java snippet issues an UPDATE statement once for every customer ID held in the custIdList array:

1  for (int custId : custIdList) {
2       Statement stmt = oracleConnection.createStatement();
3       stmt.execute("UPDATE sh.customers SET cust_valid = 'Y'"
4                   + " WHERE cust_id = " + custId);
5        stmt.close();
6  }

The loop starting on line 1 iterates through an array of CUST_ID values. We create a statement object (line 2) and then construct and execute an UPDATE statement once for each customer in the list. We concatenate the custId from the list into the SQL string on line 3.

This code will work, of course, but each UPDATE statement will need to be parsed as well as executed. This parse overhead can be significant. Furthermore, because each SQL is unique (it includes the hardcoded custId), we’re unlikely to find a matching SQL in the shared pool. Therefore, a hard parse—one in which no matching SQL is found in the shared pool—will be required.

The next code snippet shows the bind variable technique in Java. The SQL statement is created as a PreparedStatement and includes a bind variable—identified as :custId—which acts as a placeholder for the parameters to the SQL. The variable is assigned a value on line 5 prior to each execution on line 6:

1  PreparedStatement stmt = oracleConnection.prepareStatement(
2        "UPDATE sh.customers SET cust_valid = 'Y'"
3      + " WHERE cust_id = :custId");
4  for (int custId : custIdList) {
5       stmt.setInt(1, custId);
6       stmt.execute();
7  }

Using the bind variable technique radically reduces the parse overhead of SQL execution, and in particular, it reduces the amount of library cache mutex contention.

To identify the SQLs that are causing the most hard parses, we need to find those SQLs that are identical other than for the values of literals. These SQLs will show up in V$SQL as SQLs with the same value for FORCE_MATCHING_SIGNATURE, as shown in Listing 16.5.

Listing 16.5 Finding SQLs That Are Not Using Bind Variables

SQL> WITH force_matches AS
  2         (SELECT force_matching_signature,
  3                 COUNT( * )  matches,
  4                 MAX(sql_id || child_number) max_sql_child,
  5                 DENSE_RANK() OVER (ORDER BY COUNT( * ) DESC)
  6                    ranking
  7          FROM v$sql
  8          WHERE force_matching_signature <> 0
  9            AND parsing_schema_name <> 'SYS'
 10          GROUP BY force_matching_signature
 11          HAVING COUNT( * ) > 5)
 12  SELECT sql_id,     matches, parsing_schema_name schema, sql_text
 13    FROM       v$sql JOIN force_matches
 14      ON (sql_id || child_number = max_sql_child)
 15  WHERE ranking <= 10
 16  ORDER BY matches DESC;


SQL_ID           MATCHES SCHEMA
------------- ---------- --------------------
SQL_TEXT
------------------------------------------------------------------
gzxu5hs6sk4s9      13911 OPSG
select max(data) from log_data where id=717.91

The query reveals that there were 13,911 instances of a SQL statement that was identical except for the value of a variable—a variable that could have been represented by a bind variable.

Ideally, applications should make use of bind variables whenever possible by changing the application code, as outlined earlier in this section. However, it’s not always easy or possible to rewrite an application to use bind variables. Therefore, Oracle provides a mechanism for imposing bind variables transparently; when the parameter CURSOR_SHARING is set to FORCE or SIMILAR, then Oracle can replace a statement such as this:

SELECT MAX(data) FROM log_data WHERE id=717.91

with a statement like this:

SELECT MAX(data) FROM log_data WHERE id=:"SYS_B_0"

Oracle will then substitute the appropriate values into the system-generated bind variables (value 717.91 would be assigned in the previous example), and the library cache miss will be avoided. As we saw earlier, this behavior reduces parse overhead—since Oracle can retrieve the already parsed version from the shared pool—and it also reduces mutex contention, since Oracle doesn’t have to acquire the mutex in exclusive mode if the matching SQL is found.

Library Cache Pin

The library cache pin wait is not strictly a latch or mutex wait, but it often shows up in similar circumstances. A library cache pin is required whenever an object in the library cache is to be executed, parsed, or reparsed. The library cache pin is acquired in exclusive mode if, for instance, the execution plan for a SQL statement needs to be changed or a PL/SQL package is modified or recompiled. The library cache pin is acquired in exclusive mode by the sessions executing the object.

The session wanting to modify the object will attempt to acquire the library cache pin in exclusive mode; sessions executing the object will be holding a shared library cache pin.

Excessive waits on the library cache pin may suggest that PL/SQL packages are being recompiled during periods of heavy concurrent execution. If possible, schedule recompilation during maintenance windows.

Shared Pool Latch

The primary purpose of shared pool latches is to control access to the shared pool memory map. Sessions that are looking for free space in the shared pool for a new SQL statement or PL/SQL package will need to acquire shared pool latches, and many Oracle internal operations (resizing the shared pool for instance) will acquire these latches as well.

Excessive hard parsing—the primary cause of library cache mutex contention—generally results in shared pool latch contention as well, because the constant allocation of “one-off” SQL statements will fragment the shared pool and require continual deallocation of old statements. This will show up as waits for the latch: shared pool event.

Shared pool fragmentation has other deleterious side effects, including ORA-4031 errors (“unable to allocate x bytes of shared memory”) and excessive shared pool memory consumption. Over the years, a variety of techniques have been employed to combat this fragmentation:

  • Some sites flush the shared pool periodically using the ALTER SYSTEM FLUSH SHARED_POOL command.
  • Setting a minimum size for the shared pool when using automatic SGA memory management is almost always a good idea but particularly if shared pool latch contention is present. Using automatic SGA memory management can exacerbate fragmentation issues, since the memory management algorithms are not always able to predict or measure the degree of fragmentation that will result from continual resizing.
  • Pinning large but infrequently executed PL/SQL packages in the shared pool—using DBMS_SHARED_POOL—might help reduce fragmentation by preventing large objects moving in and out of memory.
  • The SHARED_POOL_RESERVED_SIZE parameter controls the amount of shared pool reserved for large memory allocations. In some instances, increasing the size of this parameter may reduce pressure on the shared pool latch by reducing the amount of time it takes to find large, contiguous chunks of memory

Cache Buffers Chains Latch

A cache buffer chain (CBC) is a doubly linked list of buffer headers pointing to buffers in the buffer cache that hash to a common value. There are a number of CBC latches, each latch protecting multiple cache buffer chains.

When a session needs to access a buffer in the buffer cache, it must acquire a CBC latch on the “chain” that contains that buffer. Contention for this latch results in waits for the latch: cache buffers chains event.

The amount of time it takes to access a block in memory is very small, and there are a large number of CBC latches. Nevertheless, cache buffers chains latch contention can become significant on systems with very high logical read rates, especially if these logical reads concentrate on a small number of blocks. Another possible cause is the creation of long buffer hash chains caused by multiple consistent read copies of an individual buffer.

Ironically, cache buffers chains latch contention often occurs on systems that are almost perfectly optimized in every other respect: in order to get the very high logical read rates necessary to induce cache buffers chains contention, the system typically needs to minimize all other forms of contention and waits, such as IO, parsing, and locking.

High logical read rates and the resulting CBC latch contention can, however, be the result of poorly tuned SQL as well. For example, a nested loops join that uses an unselective index may scan the same set of blocks on the inner table many times over. These blocks will then become “hot” and may be the subject of latch contention. Tuning the SQL by creating a more selective index will reduce the redundant logical reads and reduce the latch contention as well as improve the performance of the SQL concerned.

The mapping of cache buffers to cache buffers chains latches is based on an Oracle hashing algorithm, and the number of blocks per latch can vary significantly. If you want to examine the configuration of your cache buffers chains latches, the query in Listing 16.6—which you must run as SYS—will reveal the latch to buffer ratios.

Listing 16.6 Revealing the CBC Latch to Buffer Ratio

SQL> SELECT COUNT(DISTINCT l.addr) cbc_latches,
  2         SUM(COUNT( * )) buffers,
  3         MIN(COUNT( * )) min_buffer_per_latch,
  4         MAX(COUNT( * )) max_buffer_per_latch,
  5         ROUND(AVG(COUNT( * ))) avg_buffer_per_latch
  6  FROM       v$latch_children l
  7       JOIN
  8          x$bh b
  9       ON (l.addr = b.hladdr)
 10  WHERE name = 'cache buffers chains'
 11  GROUP BY l.addr;

 CBC Latch Buffer Cache Min Buffer Max Buffer Avg Buffer
     Count      Buffers  Per Latch  Per Latch  Per Latch
---------- ------------ ---------- ---------- ----------
      8192        89386          3         46         11

On this database, an average of 11 blocks was associated with each latch, but some latches protected as few as 3 or as many as 46 blocks.

The chance that contention for a cache buffers chains latch is a result of two hot blocks being mapped to the same latch is pretty small, and while you can attempt to change the number of latches using undocumented Oracle parameters (such as _db_block_hash_buckets), the chances that you’ll relieve latch contention by doing so are not good.

Each latch exposes its individual statistics into the view V$LATCH_CHILDREN. You can link these latches to the buffers they protect by examining the view X$BH (which, unfortunately, you can only do as the SYS user). The query in Listing 16.7 joins the two tables to identify the segments that are most heavily associated with cache buffers chains latch sleeps.

Listing 16.7 Identifying Segments with High CBC Latch Sleeps

SQL> WITH cbc_latches AS
  2      (SELECT addr, name, sleeps,
  3              rank() over(order by sleeps desc) ranking
  4         FROM v$latch_children
  5        WHERE name = 'cache buffers chains')
  6  SELECT owner, object_name,object_type,
  7         COUNT(distinct l.addr) latches,
  8         SUM(tch) touches
  9    FROM cbc_latches l JOIN x$bh b
 10         ON (l.addr = b.hladdr)
 11    JOIN dba_objects o
 12         ON (b.obj = o.object_id)
 13   WHERE l.ranking <=100
 14   GROUP BY owner, object_name,object_type
 15  ORDER BY sum(tch) DESC;

OWNER        OBJECT_NAME          OBJECT_TYP    LATCHES      TOUCHES
------------ -------------------- ---------- ---------- ------------
OPSG         LOG_DATA             TABLE             103        1,149

This query shows that the top 100 cache buffers chains latches are all associated with the LOG_DATA table and that it is probably the high rates of logical I/O against this table that are the root cause of the cache buffers chains latch contention we are experiencing.

Finding the segment involved in cache buffers chains contention is a good first step, but where do we go from here? There are a couple of possibilities:

  • If the cache buffers chains contention is associated with an index, then you could consider reimplementing the table as a hash cluster and use a hash key lookup rather than a B-tree index lookup. B-tree indexes often become associated with cache buffers chains contention, because index root and branch blocks tend to be accessed more frequently than index leaf blocks or table blocks. If we use a hash cluster lookup instead, this potential for cache buffers chains latch contention is eliminated. If the B-tree index is part of a nested loops join, then a hash join might similarly relieve pressure on the index blocks.
  • At the risk of belaboring the point, is there any way to reduce the logical I/O rate? Review and tune SQL that accesses the table: perhaps a judicious index or two could reduce the logical I/O demand. Perhaps Oracle client-side caching (CLIENT_RESULT_CACHE_SIZE parameter) or the Oracle server-side result set cache could be used to reduce the logical I/O rate.
  • If there are multiple hot rows within the same hot block, explore options for splitting these rows across multiple blocks. Partitioning the table and its indexes can be an attractive option, especially since it requires no changes to application code.

Other Latch Scenarios

Cache buffers chains latches and library cache mutexes are the most commonly encountered forms of latch/mutex contention. However, other forms of latch contention arise from time to time. Here are some of the other latches that you might encounter:

  • Cache buffers lru chain latch: This latch controls access to the LRU (least recently used) list in the buffer cache. Buffers move up and down this list as they are accessed and, once they reach the cold end of the list, are eventually flushed out of the pool. Contention for this latch is generally associated with cache buffers chains latch contention and will generally respond to a similar resolution. However, while the cache buffers chains latch is most sensitive to hot blocks, the cache buffers lru chains latch is more heavily utilized when new blocks are introduced into the buffer cache.
  • Simulator lru latch: This latch controls access to the “virtual” LRU list that Oracle uses to work out the effect of increasing or decreasing the size of the buffer cache. This information is used to populate the DB_CACHE_ADVICE tables and to perform automatic memory management. Contention for this latch can occur under similar circumstances as for the cache buffers chains and cache buffers lru chains latches and may mask contention for those latches. Setting DB_CACHE_ADVICE to OFF will usually eliminate this contention but may merely shift the contention to the cache buffers chains latch. Note also that contention on this latch was associated with some Oracle bugs in early versions of Oracle 11.
  • Redo allocation latch: This latch serializes entries to the redo log buffers and private “strands,” both of which buffer I/O to the redo logs. This latch—and the related redo copy latch—were often implicated in latch contention issues in earlier versions of Oracle. However, Oracle made significant changes to redo handling in Oracle Database 9i and 10g, parallelizing redo generation, creating multiple independent buffers, and introducing private buffer strands. As a result, redo-related latch contention issues are rarely reported today. You may see some contention for the redo allocation latch when there are very high levels of concurrent DML activity. However, it’s unlikely to dominate overall performance, since such high levels of DML generally create substantial I/O-related waits.
  • Session allocation and process allocation latches: These latches are often involved during the creation of a new session and, in the case of process allocation, associated server process. Contention on these latches will often be seen if there is a very high rate of logon/logoff to the database. Oracle is not really optimized for sessions that connect, issue a single SQL, and then disconnect; performance will usually be better when sessions stay connected and issue multiple SQLs. Using application server connection pools is advisable if you see this sort of contention, and you might see some relief if you configure the database for multithreaded server connections.
  • kks stats latch: This latch seems to be associated with mutex operations—one might speculate that it is involved in maintaining mutex sleep statistics. Some contention on this latch seems to be associated with other mutex contention scenarios. If you see this latch in conjunction with mutex waits, you should probably try resolving the mutex issue first with the hope of curing contention for this latch as well.
  • In-memory undo latch: This latch is associated with Oracle’s relatively new in-memory undo (IMU) structures in which information formerly maintained in rollback (undo) segments is held in memory. Some contention for the in-memory undo latch might be the cost you have to pay for the reduction in redo generation and undo segment I/O that the new algorithm provides. However, some users have suggested turning in-memory undo off by adjusting the undocumented parameter _in_memory_undo or increasing the value of the PROCESSES parameter, which controls the default number of IMU latches.
  • Result cache (RC) latch: This latch protects the creation and deletion of result sets in the result set cache. Contention for the latch occurs if multiple sessions attempt to simultaneously create cached result sets. Result sets in the result set cache should generally be restricted to a relatively small number of infrequently executing SQLs.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020