Information Technology Reference
In-Depth Information
A latch on a buffered page is implemented using a semaphore pointed to from
the buffer control block of the page. Semaphores are the standard method for mutual
exclusion of concurrent processes accessing shared main-memory structures. A
semaphore is a main-memory structure that contains the mode of the semaphore
(free, shared, exclusive), the number of process threads holding the semaphore (0
for free mode, 1 for exclusive mode, 1 for shared mode), and a pointer to the list
of process threads waiting for access to the semaphore. A read latch is implemented
with a shared semaphore and a write latch with an exclusive semaphore.
Before each reference to page p produced by an SQL query, the database
management system automatically generates the pair of calls
fix-and-read-latch .p/ fix .p/& read-latch .p/,
if the page is only read during this fixing, or
fix-and-write-latch .p/ fix .p/& write-latch .p/,
if p is to be updated during this fixing. The latch and fixing is freed by generating
the pair of calls
unlatch-and-unfix .p/ unlatch .p/& unfix .p/.
Actually, the pair of fix and latch calls and the pair of unlatch and unfix calls are
most efficiently implemented as single calls, because we never need to hold fixed
pages unlatched.
When a process or thread asks for a read latch onto a page that is currently write-
latched by another process or thread or for a write latch onto a page that is currently
read- or write-latched by another process or thread, the thread requesting for the
latch is put to sleep, waiting for the other threads to release their latches. We assume
that at any time, a process thread can be waiting for at most one semaphore and
hence at most one latch.
A general policy is that no thread should hold a latch for a long time and that
a thread may hold only a small constant amount of latches at any given time. This
is because no wait-for graph is maintained for latches to detect possible deadlocks,
and thus the latching protocol followed by the threads needs to prevent deadlocks.
In most cases a thread only holds one latch at a time to protect its database action.
When a process wishes to read a tuple from a data page p, it calls
fix-and-read-latch .p/, copies the tuple from the buffer frame of page p,andthen
calls unlatch-and-unfix .p/. When a process wishes to update a tuple on a data
page p, it calls fix-and-write-latch .p/, updates the tuple in the buffer frame of
page p, writes a log record for the update, stamps the LSN of this log record to the
P AGE -LSN of p, and then calls unlatch-and-unfix .p/.
When traversing a chained database structure, a process thread needs to keep
latches on two pages at a time in order to ensure that a correct path is traversed even
if there are other process threads running simultaneously that may modify the pages.
Example 2.4 Assume that pages p 1 ;:::;p n form an unidirectional chain so that the
header of p i contains the page identifier of p iC1 ,for1 i<n, and the header of p n
Search WWH ::




Custom Search