What is the difference between a lock and a latch in the context of concurrent access to a database?

前端 未结 8 1643
别跟我提以往
别跟我提以往 2021-01-30 06:46

I am trying to understand a paper on concurrent B-tree, in which the author mentioned latch vs lock, and how latches do not need a \"Lock Manager\". I have been trying to figure

相关标签:
8条回答
  • 2021-01-30 07:34

    It really depends on your DBMS, but here's a good explanation for Oracle.

    http://www.dba-oracle.com/t_lru_latches.htm

    Latches are like locks for RAM memory structures to prevent concurrent access and ensure serial execution of kernel code. The LRU (least recently used) latches are used when seeking, adding, or removing a buffer from the buffer cache, an action that can only be done by one process at a time.

    0 讨论(0)
  • 2021-01-30 07:39

    According to paper Architecture of a Database System p223.

    Latches differ from locks in a number of ways:

    • Locks are kept in the lock table and located via hash tables; latches reside in memory near the resources they protect, and are accessed via direct addressing.

    • In a strict 2PL implementation, locks are subject to the strict 2PL protocol. Latches may be acquired or dropped during a transaction based on special-case internal logic.

    • Lock acquisition is entirely driven by data access, and hence the order and lifetime of lock acquisitions is largely in the hands of applications and the query optimizer. Latches are acquired by specialized code inside the DBMS, and the DBMS internal code issues latch requests and released strategically.

    • Locks are allowed to produce deadlock, and lock deadlocks are detected and resolved via transactional restart. Latch deadlock must be avoided; the occurrence of a latch deadlock represents a bug in the DBMS code.

    • Latches are implemented using an atomic hardware instruction or, in rare cases, where this is not available, via mutual exclusion in the OS kernel.

    • Latch calls take at most a few dozen CPU cycles whereas lock requests take hundreds of CPU cycles.

    • The lock manager tracks all the locks held by a transaction and automatically releases the locks in case the transaction throws an exception, but internal DBMS routines that manipulate latches must carefully track them and include manual cleanup as part of their exception handling.

    • Latches are not tracked and so cannot be automatically released if the task faults.

    0 讨论(0)
提交回复
热议问题