105
Index Locking & Latching @Andy_Pavlo // 15-721 // Spring 2018 ADVANCED DATABASE SYSTEMS Lecture #07

Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

Index Locking & Latching

@Andy_Pavlo // 15-721 // Spring 2018

ADVANCEDDATABASE SYSTEMS

Le

ctu

re #

07

Page 2: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

TODAY'S AGENDA

Index Locks vs. Latches

Latch Implementations

Index Latching (Logical)

Index Locking (Physical)

2

Page 3: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

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

3

Page 4: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

Hashing Indexes→ An associative array that maps a hash of the key to a

particular record.→ Only supports equality predicates with O(1) searches.

4

Page 5: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

5

Page 6: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

6

Page 7: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

SIMPLE EXAMPLE

7

A20 22

Page 8: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

SIMPLE EXAMPLE

7

A20 22

Txn #1: Read ‘22’

Page 9: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

SIMPLE EXAMPLE

7

A20 22 Txn #2: Insert ‘21’

Txn #1: Read ‘22’

Page 10: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

SIMPLE EXAMPLE

7

A20 22

B20 22 C

Txn #2: Insert ‘21’

Txn #1: Read ‘22’

Page 11: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

SIMPLE EXAMPLE

7

A20 22

B20 22 C

Txn #2: Insert ‘21’21

21 22

Txn #1: Read ‘22’

Page 12: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

SIMPLE EXAMPLE

7

A20 22

B20 22 C

Txn #2: Insert ‘21’21

Txn #1: Read ‘22’

21 22

Txn #1: Read ‘22’

Page 13: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

LOCKS VS. L ATCHES

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.

8

A SURVEY OF B-TREE LOCKING TECHNIQUESTODS 2010

Page 14: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

10

Page 15: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Blocking OS Mutex

Test-and-Set Spinlock

Queue-based Spinlock

Reader-Writer Locks

11

Source: Anastasia Ailamaki

Page 16: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

COMPARE-AND-SWAP

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

12

M__sync_bool_compare_and_swap(&M, 20, 30)20

Compare Value

AddressNew

Value

Page 17: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

COMPARE-AND-SWAP

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

12

M__sync_bool_compare_and_swap(&M, 20, 30)30

Compare Value

AddressNew

Value

Page 18: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

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

13

Page 19: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

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

13

pthread_mutex_t

Page 20: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

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

13

std::mutex m;⋮

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

pthread_mutex_t

Page 21: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

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

14

Page 22: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

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

14

std::atomic_flag latch;⋮

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

}

std::atomic<bool>

Page 23: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

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

14

std::atomic_flag latch;⋮

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

}

std::atomic<bool>

Page 24: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

Page 25: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

Page 26: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

CPU1

Page 27: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

CPU1

Page 28: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

CPU1 CPU2

Page 29: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

CPU1 CPU2

Page 30: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2

Page 31: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

Page 32: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

Page 33: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

Page 34: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH 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

16

read write

Latch

=0

=0

=0

=0

Page 35: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH 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

16

read write

Latch

=0

=0

=0

=0

=1

Page 36: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH 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

16

read write

Latch

=0

=0

=0

=0

=1=2

Page 37: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH 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

16

read write

Latch

=0

=0

=0

=0

=1=2

=1

Page 38: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH 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

16

read write

Latch

=0

=0

=0

=0

=1=2

=1=1

Page 39: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH 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)

17

Page 40: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

L ATCH 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.

18

Page 41: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 42: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

Page 43: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #1: SEARCH 23

19

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 44: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #1: SEARCH 23

19

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 45: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #1: SEARCH 23

19

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 46: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 47: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

Page 48: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #2: DELETE 44

20

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 49: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #2: DELETE 44

20

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 50: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #2: DELETE 44

20

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 51: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #2: DELETE 44

20

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 52: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 53: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

Page 54: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

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 55: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

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 56: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

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 57: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

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 58: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

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 59: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #3: INSERT 40

21

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 60: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

OBSERVATION

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

22

Delete 44

A20

W

Insert 40

A20

W

Page 61: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

BET TER L ATCH 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.

23

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

Page 62: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

Page 63: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #4: DELETE 44

24

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 64: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #4: DELETE 44

24

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 65: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #4: DELETE 44

24

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 66: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #4: DELETE 44

24

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 67: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

EXAMPLE #4: DELETE 44

24

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 68: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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…

25

Page 69: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsR

Page 70: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 existsR

R

Page 71: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

R

Page 72: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

R!

Page 73: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

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 74: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

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 75: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

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

25

W

Page 76: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

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

Txn #1: Insert 25

25

Page 77: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

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

Txn #1: Insert 25

25

W

Page 78: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 79: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

R

Page 80: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

R R

Page 81: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

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 82: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

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 83: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

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 84: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

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 85: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

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 86: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PROBLEM SCENARIO #2

27

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 87: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

28

Page 88: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

INDEX LOCKS

29

Lock Table

txn1

Xtxn2

Stxn3

S • • •

txn3

Stxn2

Stxn4

S • • •

txn4

IXtxn6

Xtxn5

S • • •

Page 89: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

INDEX LOCKING SCHEMES

Predicate Locks

Key-Value Locks

Gap Locks

Key-Range Locks

Hierarchical Locking

30

Page 90: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

31

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

Page 91: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PREDICATE LOCKS

32

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

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

Records in Table "account"

Page 92: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PREDICATE LOCKS

32

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

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

name='Biggie'

Records in Table "account"

Page 93: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

PREDICATE LOCKS

32

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 94: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

KEY-VALUE LOCKS

Locks that cover a single key value.

Need “virtual keys” for non-existent values.

33

10 12 14 16

B+Tree Leaf NodeKey

[14, 14]

Page 95: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

34

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

B+Tree Leaf Node

Page 96: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

34

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

B+Tree Leaf Node

Gap(14, 16)

Page 97: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

35

Page 98: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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)

36

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

B+Tree Leaf Node

Next Key [14, 16)

Page 99: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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)

36

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

B+Tree Leaf Node

Prior Key (12, 14]

Page 100: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

HIERARCHICAL LOCKING

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

37

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

B+Tree Leaf Node

Page 101: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

HIERARCHICAL LOCKING

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

37

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

B+Tree Leaf NodeIX

[10, 16)

Page 102: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

HIERARCHICAL LOCKING

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

37

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

B+Tree Leaf NodeIX

[10, 16)

[14, 16)X

Page 103: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

HIERARCHICAL LOCKING

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

37

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

B+Tree Leaf NodeIX

[10, 16)

[14, 16)X

IX [12, 12]X

Page 104: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

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.

Peloton currently does not support serializable isolation with range scans.

38

Page 105: Index Locking & Latching · A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to

CMU 15-721 (Spring 2018)

NEXT CL ASS

Index Key Representation

Memory Allocation & Garbage Collection

T-Trees (1980s / TimesTen)

Bw-Tree (Hekaton)

Concurrent Skip Lists (MemSQL)

39