database system implementationjarulraj/courses/4420-s19/slides/10... · a data structure that...

137
DATABASE SYSTEM IMPLEMENTATION GT 4420/6422 // SPRING 2019 // @JOY_ARULRAJ LECTURE #9: INDEX LOCKING & LATCHING

Upload: others

Post on 27-Apr-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

DATABASE SYSTEM IMPLEMENTATION

GT 4420/6422 // SPRING 2019 // @JOY_ARULRAJ

LECTURE #9: INDEX LOCKING & LATCHING

Page 2: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

ANATOMY OF A DATABASE SYSTEM

Connection Manager + Admission Control

Query Parser

Query Optimizer

Query Executor

Lock Manager (Concurrency Control)

Access Methods (or Indexes)

Buffer Pool Manager

Log Manager

Memory Manager + Disk Manager

Networking Manager

2

QueryTransactional

Storage Manager

Query Processor

Shared Utilities

Process Manager

Source: Anatomy of a Database System

Page 3: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

TODAY'S AGENDA

Index Locks vs. LatchesLatch ImplementationsIndex Latching (Physical)Index Locking (Logical)

3

Page 4: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

DATABASE INDEX

A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space.→ Example: Glossary in a tech-book

Indexes are used to quickly locate data without having to search every row in a table every time a table is accessed.

4

Page 5: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

DATA STRUCTURES

Order Preserving Indexes→ A tree-like structure that maintains keys in some sorted

order.→ Supports all possible predicates with O(log n) searches.→ Example: AGE > 20 AND AGE < 30Hashing Indexes→ An associative array that maps a hash of the key to a

particular record.→ Only supports equality predicates with O(1) searches.→ Example: AGE = 20

5

Page 6: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

DATA STRUCTURES

Order Preserving Indexes→ A tree-like structure that maintains keys in some sorted

order.→ Supports all possible predicates with O(log n) searches.→ Example: AGE > 20 AND AGE < 30Hashing Indexes→ An associative array that maps a hash of the key to a

particular record.→ Only supports equality predicates with O(1) searches.→ Example: AGE = 20

6

Page 7: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

B-TREE VS. B+TREE

The original B-tree from 1972 stored keys + values in all nodes in the tree.→ More memory efficient since each key only appears once

in the tree.

A B+tree only stores values in leaf nodes. Inner nodes only guide the search process.→ Easier to manage concurrent index access when the

values are only in the leaf nodes.

7

Reference: Differences betwee B-Tree and B+Tree

Page 8: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

OBSERVATION

We already know how to use locks to protect objects in the database.

But we have to treat indexes differently because the physical structure can change as long as the logical contents are consistent.

8

Page 9: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

SIMPLE EXAMPLE

9

A20 22

Page 10: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

SIMPLE EXAMPLE

10

A20 22

Txn #1: Read ‘22’

Page 11: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

SIMPLE EXAMPLE

11

A20 22 Txn #2: Insert ‘21’Txn #1: Read ‘22’

Page 12: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

SIMPLE EXAMPLE

12

A20 22

B20 22 C

Txn #2: Insert ‘21’Txn #1: Read ‘22’

Page 13: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

SIMPLE EXAMPLE

13

A20 22

B20 22 C

Txn #2: Insert ‘21’21

21 22

Txn #1: Read ‘22’

Page 14: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

SIMPLE EXAMPLE

14

A20 22

B20 22 C

Txn #2: Insert ‘21’21

Txn #1: Read ‘22’

21 22

Txn #1: Read ‘22’

Page 15: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LOCKS VS. LATCHES

Locks→ Protects the index’s logical contents from other txns.→ Held for txn duration.→ Need to be able to rollback changes.

Latches→ Protects the critical sections of the index’s internal data

structure from other threads.→ Held for operation duration.→ Do not need to be able to rollback changes.

15

A SURVEY OF B-TREE LOCKING TECHNIQUESTODS 2010

Page 16: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Blocking OS MutexTest-and-Set SpinlockQueue-based SpinlockReader-Writer Locks

16

Source: Anastasia Ailamaki

Page 17: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

COMPARE-AND-SWAP INSTRUCTION

Atomic instruction that compares contents of a memory location M to a given value V→ If values are equal, installs new given value V’ in M→ Otherwise operation fails

17

M__sync_bool_compare_and_swap(&M, 20, 30)20

Compare Value

AddressNew

Value

Page 18: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

COMPARE-AND-SWAP INSTRUCTION

Atomic instruction that compares contents of a memory location M to a given value V→ If values are equal, installs new given value V’ in M→ Otherwise operation fails

18

M__sync_bool_compare_and_swap(&M, 20, 30)30

Compare Value

AddressNew

Value

Page 19: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #1: Blocking OS Mutex→ Simple to use→ Non-scalable (about 25ns per lock/unlock invocation)→ Example: std::mutex

19

Page 20: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #1: Blocking OS Mutex→ Simple to use→ Non-scalable (about 25ns per lock/unlock invocation)→ Example: std::mutex

20

pthread_mutex_t

Page 21: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #1: Blocking OS Mutex→ Simple to use→ Non-scalable (about 25ns per lock/unlock invocation)→ Example: std::mutex

21

std::mutex m;⋮

m.lock();// Do something special...m.unlock();

pthread_mutex_t

Page 22: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #1: Blocking OS Mutex→ Simple to use→ Non-scalable (about 25ns per lock/unlock invocation)→ Example: std::mutex

22

std::mutex m;⋮

m.lock();// Do something special...m.unlock();

pthread_mutex_t

Page 23: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #1: Blocking OS Mutex→ Simple to use→ Non-scalable (about 25ns per lock/unlock invocation)→ Example: std::mutex

23

std::mutex m;⋮

m.lock();// Do something special...m.unlock();

pthread_mutex_t

Page 24: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #2: Test-and-Set Spinlock (TAS)→ Very efficient (single instruction to lock/unlock)→ Not OS-dependent (unlike mutex)→ Non-scalable, not cache friendly, busy-waiting→ Example: std::atomic<T>

24

Page 25: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #2: Test-and-Set Spinlock (TAS)→ Very efficient (single instruction to lock/unlock)→ Not OS-dependent (unlike mutex)→ Non-scalable, not cache friendly, busy-waiting→ Example: std::atomic<T>

25

std::atomic_flag latch;⋮

while (latch.test_and_set(…)) {// Yield? Abort? Retry?

}

Page 26: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #2: Test-and-Set Spinlock (TAS)→ Very efficient (single instruction to lock/unlock)→ Not OS-dependent (unlike mutex)→ Non-scalable, not cache friendly, busy-waiting→ Example: std::atomic<T>

26

std::atomic_flag latch;⋮

while (latch.test_and_set(…)) {// Yield? Abort? Retry?

}

std::atomic<bool>

Page 27: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #2: Test-and-Set Spinlock (TAS)→ Very efficient (single instruction to lock/unlock)→ Not OS-dependent (unlike mutex)→ Non-scalable, not cache friendly, busy-waiting→ Example: std::atomic<T>

27

std::atomic_flag latch;⋮

while (latch.test_and_set(…)) {// Yield? Abort? Retry?

}

std::atomic<bool>

Page 28: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #2: Test-and-Set Spinlock (TAS)→ Very efficient (single instruction to lock/unlock)→ Not OS-dependent (unlike mutex)→ Non-scalable, not cache friendly, busy-waiting→ Example: std::atomic<T>

28

std::atomic_flag latch;⋮

while (latch.test_and_set(…)) {// Yield? Abort? Retry?

}

std::atomic<bool>

Page 29: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

29

Page 30: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

30

Mellor-Crummey and Scott

Page 31: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

31

next

Base Latch

Mellor-Crummey and Scott

Page 32: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

32

next

Base Latch

CPU1

Mellor-Crummey and Scott

Page 33: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

33

next

Base Latch

next

CPU1 Latch

CPU1

Mellor-Crummey and Scott

Page 34: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

34

next

Base Latch

next

CPU1 Latch

CPU1

Mellor-Crummey and Scott

Page 35: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

35

next

Base Latch

next

CPU1 Latch

CPU1 CPU2

Mellor-Crummey and Scott

Page 36: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

36

next

Base Latch

next

CPU1 Latch

CPU1 CPU2

Mellor-Crummey and Scott

Page 37: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

37

next

Base Latch

next

CPU1 Latch

CPU1 CPU2

Mellor-Crummey and Scott

Page 38: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

38

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2

Mellor-Crummey and Scott

Page 39: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

39

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

Mellor-Crummey and Scott

Page 40: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

40

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

Mellor-Crummey and Scott

Page 41: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ With TAS, all threads are polling a single shared latch variable→ With MCS, each thread is polling on predecessor in queue→ Example: std::atomic<Latch*>

41

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

Mellor-Crummey and Scott

Page 42: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

42

Page 43: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

43

read write

Latch

=0=0

=0=0

Page 44: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

44

read write

Latch

=0=0

=0=0

Page 45: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

45

read write

Latch

=0=0

=0=0

Page 46: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

46

read write

Latch

=0=0

=0=0

=1

Page 47: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

47

read write

Latch

=0=0

=0=0

=1

Page 48: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

48

read write

Latch

=0=0

=0=0

=1=2

Page 49: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

49

read write

Latch

=0=0

=0=0

=1=2

Page 50: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

50

read write

Latch

=0=0

=0=0

=1=2=1

Page 51: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

51

read write

Latch

=0=0

=0=0

=1=2=1

Page 52: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

52

read write

Latch

=0=0

=0=0

=1=2=1=1

Page 53: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH CRABBING

Acquire and release latches on B+Tree nodes when traversing the data structure.

A thread can release latch on a parent node if its child node considered safe.→ Any node that won’t split or merge when updated.→ Not full (on insertion)→ More than half-full (on deletion)

53

Page 54: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LATCH CRABBING

Search: Start at root and go down; repeatedly,→ Acquire read (R) latch on child→ Then unlock the parent node.

Insert/Delete: Start at root and go down, obtaining write (W) latches as needed.Once child is locked, check if it is safe:→ If child is safe, release all locks on ancestors.

54

Page 55: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

55

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 56: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

56

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 57: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

57

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

Page 58: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

58

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

R

Page 59: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

59

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

R

We can release the latch on A as soon as we acquire the latch for C.

Page 60: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

60

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

We can release the latch on A as soon as we acquire the latch for C.

Page 61: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

61

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

R

We can release the latch on A as soon as we acquire the latch for C.

Page 62: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #1: SEARCH 23

62

A

B

D G

20

10 35

6 12 23 38 44

C

E FR

We can release the latch on A as soon as we acquire the latch for C.

Page 63: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

63

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 64: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

64

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

Page 65: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

65

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

Page 66: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

66

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

We may need to coalesce C, so we can’t release the latch on A.

Page 67: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

67

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

W

We may need to coalesce C, so we can’t release the latch on A.

Page 68: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

68

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

W

We may need to coalesce C, so we can’t release the latch on A.G will not merge with F, so we can release latches on A and C.

Page 69: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

69

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We may need to coalesce C, so we can’t release the latch on A.G will not merge with F, so we can release latches on A and C.

Page 70: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #2: DELETE 44

70

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We may need to coalesce C, so we can’t release the latch on A.G will not merge with F, so we can release latches on A and C.

X

Page 71: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

71

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 72: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

72

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

Page 73: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

73

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

Page 74: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

74

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.

Page 75: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

75

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

C has room if its child has to split, so we can release the latch on A.

Page 76: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

76

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.G has to split, so we can’t release the latch on C.

Page 77: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

77

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.G has to split, so we can’t release the latch on C.

H44

Page 78: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

78

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.G has to split, so we can’t release the latch on C.

H4440

44

Page 79: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #3: INSERT 40

79

A

B

D G

20

10 35

6 12 23 38 44

C

E F

C has room if its child has to split, so we can release the latch on A.G has to split, so we can’t release the latch on C.

H4440

44

Page 80: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

OBSERVATION

What was the first step that the DBMS took in the two examples that updated the index?

80

Page 81: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

OBSERVATION

What was the first step that the DBMS took in the two examples that updated the index?

81

Delete 44

A20W

Insert 40

A20W

Page 82: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

BETTER LATCH CRABBING

Optimistically assume that the leaf is safe.→ Take R latches as you traverse the tree to reach it and

verify.→ If leaf is not safe, then do previous algorithm.

Also called optimistic lock coupling.

82

CONCURRENCY OF OPERATIONS ON B-TREESActa Informatica 9: 1-21 1977

Page 83: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #4: DELETE 44

83

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 84: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #4: DELETE 44

84

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

Page 85: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #4: DELETE 44

85

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

R

We assume that C is safe, so we can release the latch on A.

Page 86: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #4: DELETE 44

86

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

We assume that C is safe, so we can release the latch on A.

Page 87: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #4: DELETE 44

87

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

We assume that C is safe, so we can release the latch on A.Acquire an exclusive latch on G.

Page 88: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #4: DELETE 44

88

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We assume that C is safe, so we can release the latch on A.Acquire an exclusive latch on G.

Page 89: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

EXAMPLE #4: DELETE 44

89

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We assume that C is safe, so we can release the latch on A.Acquire an exclusive latch on G.

X

Page 90: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

OBSERVATION

Crabbing ensures that txns do not corrupt the internal data structure during modifications.

But because txns release latches on each node as soon as they are finished their operations, we cannot guarantee that phantoms do not occur…

Latches only protect the physical contents of the data structure, not the logical contents.

90

Page 91: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

91

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 92: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

92

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

Page 93: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

93

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsR

Page 94: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

94

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsR

R

Page 95: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

95

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

R

Page 96: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

96

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

R!

Page 97: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

97

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsTxn #2: Insert 25

Page 98: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

98

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsTxn #2: Insert 25

W

Page 99: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

99

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsTxn #2: Insert 25

25W

Page 100: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

100

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsTxn #2: Insert 25Txn #1: Insert 25

25

Page 101: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #1

101

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsTxn #2: Insert 25Txn #1: Insert 25

25W

Page 102: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

102

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 103: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

103

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

Page 104: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

104

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

R

Page 105: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

105

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

R R

Page 106: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

106

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]Txn #2: Insert 21

Page 107: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

107

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]Txn #2: Insert 21

W

W

Page 108: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

108

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]Txn #2: Insert 21

2321

W

W

Page 109: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

109

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]Txn #2: Insert 21

2321

Page 110: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

110

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]Txn #2: Insert 21

2321

Txn #1: Scan [12, 23]

Page 111: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PROBLEM SCENARIO #2

111

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]Txn #2: Insert 21

2321

Txn #1: Scan [12, 23]

R R

Page 112: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

INDEX LOCKS

Need a way to protect the index’s logical contents from other txns to avoid phantoms.

Difference with index latches:→ Locks are held for the entire duration of a txn.→ Only acquired at the leaf nodes.→ Not physically stored in index data structure.

112

Page 113: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

INDEX LOCKS

113

Lock Table

txn1X

txn2S

txn3S • • •

txn3S

txn2S

txn4S • • •

txn4IX

txn6X

txn5S • • •

Page 114: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

INDEX LOCKS

114

Lock Table

txn1X

txn2S

txn3S • • •

txn3S

txn2S

txn4S • • •

txn4IX

txn6X

txn5S • • •

Page 115: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

INDEX LOCKS

115

Lock Table

txn1X

txn2S

txn3S • • •

txn3S

txn2S

txn4S • • •

txn4IX

txn6X

txn5S • • •

Page 116: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

INDEX LOCKING SCHEMES

Predicate LocksKey-Value LocksGap LocksKey-Range LocksHierarchical Locking

116

Page 117: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PREDICATE LOCKS

Proposed locking scheme from System R.→ Shared lock on the predicate in a WHERE clause of a

SELECT query.→ Exclusive lock on the predicate in a WHERE clause of any

UPDATE, INSERT, or DELETE query.

Never implemented in any system.

117

THE NOTIONS OF CONSISTENCY AND PREDICATE LOCKS IN A DATABASE SYSTEMCACM 1976

Page 118: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PREDICATE LOCKS

118

SELECT SUM(balance)FROM accountWHERE name = 'Biggie'

INSERT INTO account(name, balance)VALUES ('Biggie', 100);

Records in Table "account"

Page 119: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PREDICATE LOCKS

119

SELECT SUM(balance)FROM accountWHERE name = 'Biggie'

INSERT INTO account(name, balance)VALUES ('Biggie', 100);

Records in Table "account"

Page 120: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PREDICATE LOCKS

120

SELECT SUM(balance)FROM accountWHERE name = 'Biggie'

INSERT INTO account(name, balance)VALUES ('Biggie', 100);

name='Biggie'

Records in Table "account"

Page 121: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PREDICATE LOCKS

121

SELECT SUM(balance)FROM accountWHERE name = 'Biggie'

INSERT INTO account(name, balance)VALUES ('Biggie', 100);

name='Biggie'

name='Biggie'∧balance=100

Records in Table "account"

Page 122: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

KEY-VALUE LOCKS

Locks that cover a single key value.Need “virtual keys” for non-existent values.

122

10 12 14 16

B+Tree Leaf Node

Page 123: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

KEY-VALUE LOCKS

Locks that cover a single key value.Need “virtual keys” for non-existent values.

123

10 12 14 16

B+Tree Leaf NodeKey

[14, 14]

Page 124: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

GAP LOCKS

Each txn acquires a key-value lock on the single key that it wants to access. Then get a gap lock on the next key gap.

124

10 12 14 16

B+Tree Leaf Node

Page 125: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

GAP LOCKS

Each txn acquires a key-value lock on the single key that it wants to access. Then get a gap lock on the next key gap.

125

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Page 126: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

GAP LOCKS

Each txn acquires a key-value lock on the single key that it wants to access. Then get a gap lock on the next key gap.

126

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Gap(14, 16)

Page 127: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

KEY-RANGE LOCKS

A txn takes locks on ranges in the key space.→ Each range is from one key that appears in the relation,

to the next that appears.→ Define lock modes so conflict table will capture

commutativity of the operations available.→ Example: Intention locks (IX, IS)

127

Page 128: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index.→ Need “virtual keys” for artificial values (infinity)

128

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Page 129: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index.→ Need “virtual keys” for artificial values (infinity)

129

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Next Key [14, 16)

Page 130: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index.→ Need “virtual keys” for artificial values (infinity)

130

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Prior Key (12, 14]

Page 131: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

131

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Page 132: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

132

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf NodeIX

[10, 16)

Page 133: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

133

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf NodeIX

[10, 16)

[14, 16)X

Page 134: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

134

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf NodeIX

[10, 16)

[14, 16)XIX [12, 12]X

Page 135: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

LOCK-FREE INDEXES

Possibility #1: No Locks→ Txns don’t acquire locks to access/modify database.→ Still have to use latches to install updates.

Possibility #2: No Latches→ Swap pointers using atomic updates to install changes.→ Still have to use locks to validate txns.

135

Page 136: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

PARTING THOUGHTS

Hierarchical locking essentially provides predicate locking without complications.→ Index locking occurs only in the leaf nodes.→ Latching is to ensure consistent data structure.

Supporting higher levels of Isolation (e.g., serializable isolation) requires the usage of a locking protocol that can avoid phantoms.

136

Page 137: DATABASE SYSTEM IMPLEMENTATIONjarulraj/courses/4420-s19/slides/10... · A data structure that improves the speed of data retrieval operations on a table at the cost of additional

NEXT CLASS

Index Key RepresentationMemory Allocation & Garbage CollectionT-Trees (1980s / TimesTen)Bw-Tree (Hekaton)Concurrent Skip Lists (MemSQL)

137