Oracle SGA consists of numerous data structures such as Buffer Cache, Shared pool, Library cache, Dictionary cache, redo log buffer etc. Many oracle processes need to access these SGA data structures concurrently. Though these SGA components are shared, only one process must be able to modify it at any particular point in time. Oracle achieves this by protecting the SGA data structures with latches.
Latches do not allow multiple processes to inspect the protected data structures simultaneously. They provide only exclusive access. A latch is a kind of lock that can be very quickly acquired and freed.
When do Oracle Process obtain a latch?
Oracle sessions need to update or read from the SGA for almost all operations. For instance,
- When a session performs a physical read, it must modify a free block in the buffer cache and adjust the Buffer Cache LRU Chain.
- When a session reads a block from the SGA, it will modify the LRU chain.
- When a new SQL statement is parsed, it will be added to the library cache.
- As changes are made to blocks, entries are placed in the redo buffer.
- The DBWr and redo log writer writes buffers from buffer to disks.
Latches prevent any of these operations from colliding and possible corrupting the same area of SGA.
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.
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.
Latches Vs Locks
Request Queuing: Request for locks are queued and serviced in order, whereas there is no request queuing in latches the process requests for latch until it succeeds. Latches are serviced in random.
Management: Locks management is complex because of their support for queuing and concurrency. Latch management is simple, essentially just a single location in memory representing the state of a latch.
Because latches are very efficient Oracle uses latch to protect the data structures that are expected to be accessed briefly and intermittently.