Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Showing results for 
Search instead for 
Did you mean: 

latch free

Go to solution
Giada Bonfà
Frequent Advisor

latch free

I would like to know what is a latch.

How does it work?
Steve Steel
Honored Contributor

Re: latch free


go to and search on latch


A latch regulates access to a data structure in memory. Latches are generally held for a very short period of time. An example is a memory-to-memory copy of a data page. While the copy takes place, it is important that no other transaction is allowed to alter the content of the data structure. The most commonly latched objects in ALLBASE/SQL are buffer pages, though other data structures are also latched. ALLBASE/SQL latches both data and log pages. For the log buffer, latching is the only mechanism used to regulate concurrent access. Log records are not locked.

Like locks, latches are also represented in shared memory as control blocks, but these are not allocated following a user's request; instead, they are allocated when the DBEnvironment starts up. Latches are more efficient than locks, because deadlocks are not automatically detected in latching. Instead, deadlocks are avoided. Unlike locking, latching does not require the overhead necessary for deadlock detection.

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Honored Contributor

Re: latch free


Latches are lightweight serialization devices used to coordinate multi-user access to shared data structures, objects and files.

Latches are locks that are held for extremely short periods of time, for example the time it takes to modify an in-memory data structure. They are used to protect certain memory structures, such as database block buffer cache or the library cache in the shared pool. Latches are typically requested internally in a "willing to wait" mode. This means that if the latch is not available, the requesting session will sleep for a short period of the time and retry the operation later. Other latches may be requested in an "immediate" mode, meaning that the process will go do something else rather than sit and wait for the latch to become available. Since many requestors may be waiting for a latch at the same time, you may see some processes waiting longer than others. Latches are assigned rather randomly, based on the 'luck of draw', if you will. Whichever session asks for a latch right after it was released will get it. There is no line of latch waiters, just a 'ob' of waiters constantly retrying.

Oracle uses atomic instructions like 'test and set' for operating on latches. Since the instructions to set and free latches are atomic, the operating system itself guarantees that only one process gets it. Since it is only one instruction, it can be quite fast. Latches are held for short periods of time and provide a mechanism for clean-up in case a latch holder 'dies' abnormally while holding it. This cleaning up process would be performed by PMON.

Normally, hard parses require MANY MANY latches - on data structures in the shared pool. They get in each others way.

To summarise:
latches = locks, locks = serialization device, serialization devices = less scalable, slower.

Best regards
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Indira Aramandla
Honored Contributor

Re: latch free

What is a latch : -
Latches are low level serialization mechanisms used to protect shared data structures in the SGA (System Global Area). The implementation of latches is operating system dependent.

A latch is a type of a lock that can be very quickly acquired and freed. Latches are typically used to prevent more than one process from executing the same piece of code at a given time. Associated with each latch is a cleanup procedure that will be called if a process dies while holding the latch. Latches have an associated level that is used to prevent deadlocks. Once a process acquires a latch at a certain level it cannot subsequently acquire a latch at a level that is equal to or less than that level (unless it acquires it nowait).

When do we need to obtain a latch :-

A process acquires a latch when working with a structure in the SGA. It continues to hold the latch for the period of time it works with the structure. The latch is dropped when the
process is finished with the structure. Each latch protects a different set of data, identified by the name of the latch. Oracle uses atomic instructions like "test and set" for operating on latches. Processes waiting to execute a part of code for which a latch has
already been obtained by some other process will wait until the latch is released.

Examples are redo allocation latches, copy latches, archive control latch etc. The basic idea is to block concurrent access to shared data structures. Since the instructions to
set and free latches are atomic, the OS guarantees that only one process gets it. Since it is only one instruction, it is quite fast. Latches are held for short periods of time and provide a mechanism for cleanup in case a holder dies abnormally while holding it. This cleaning is done using the services of PMON

Refer to the attachment for further details and how can we reduce contention for internal latches
Never give up, Keep Trying