Database System Implementation CSE 507 Buffer Managers Some slides adapted from Prof Chris Re Stanford.
Buffer Managers Buffer manager: A module in a database intelligently shuffles data from main memory to disk. It is transparent to higher levels of DBMS operation
Buffer Managers Summary Page Requests from Higher Levels READ WRITE BUFFER POOL disk page free frame INPUT OUTUPT MAIN MEMORY DISK DB choice of frame dictated by replacement policy Data must be in RAM for DBMS to operate on it! Table of <frame#, pageid> pairs is maintained
When a bucket/page is requested If the requested page/bucket in the buffer pool No need to go back to the disk! If not? Choose a frame to replace. If there is a free frame, use it! Terminology: We pin a page (means it’s in use) If not? We need to choose a page to remove! What would be good strategy? --- Replacement policy
A simple strategy A page is dirty, if its contents have been changed after writing Buffer Manager keeps a dirty bit Say we choose to evict P If P is dirty, we write it to disk What if no page is dirty? Or multiple pages are dirty?
Review of Some strategies from OS-- LRU Order pages by the time of last accessed Always replace the least recently accessed P5, P2, P8, P4, P1, P9, P6, P3, P7 Access P6 P6, P5, P2, P8, P4, P1, P9, P3, P7
Some strategies from OS– Clock algorithm Instead we maintain a “last used clock” Think of buckets ordered 1…N around a clock “The hand” sweeps around Buckets keep a “ref bit” set to 1 or 0. Whenever a bucket is fetched in its “ref bit” is set to 1. Similarly it is set to “1” whenever it is referenced. The buffer manager’s “hand” looks for the first 0 for replacement. Whenever it passes by a “1” it is set to “0”.
Some strategies from OS– MRU algorithm Most Recently Used. Are you kidding me? Why would you ever want to use this? Hint: Consider the inner loop behavior of a nested loop join. Material adapted from Prof Chris Re Stanford.
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks --- Page Fault Counter = 0 --- ---
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=1 Page Fault Counter = 1 --- Page fault for Block 1 of R !! Which one would be replaced? ---
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=1 Page Fault Counter = 2 Block1 from S T=1 Page fault for Block 1 of S !! Which one would be replaced? ---
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=1 Page Fault Counter = 2 Assuming each page has k records in it Block1 from S T=k Page request for Block 1 of join !!! Output block T=k
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=k^2 - k At this stage block 1 of R joined with bock 1 of S Page Fault Counter = 2 Block1 from S T=k^2 Note that the last used timestamps depend on how the use is counted. Output block T=k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=k^2 - k At this stage block 1 of R joined with bock 1 of S Page Fault Counter = 2 Block1 from S T=k^2 So instead of exact number the key thing to follow is: last used of R < that of S Output block T=k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=k^2 -k Page Fault Counter = 3 Page Fault Counter = 0 Block1 from S T=k^2 Page fault for Block 2 of S !! Which one would be replaced? Output block T=k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time Which one would MRU have replaced?? Would it have prevented any unnecessary page fault Block2 from S T=k^2 +1 Page Fault Counter = 3 Block1 from S T=k^2 Output block T=k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=k^2 +1 Page Fault Counter = 3 Page fault for Block 1 of R !! Which one would be replaced? Block1 from S T=k^2 Output block T=k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=k^2 +1 Page Fault Counter = 4 Block1 from R T=k^2 +2 Output block T=k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For LRU – Assume 3 buffers; R=2 S = 3 blocks Last used time Block2 from S T=k^2+1 +k Page Fault Counter = 4 Block1 from R T=k^2+2 Output block T=k^2 + 1+k
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For LRU – Assume 3 buffers; R=2 S = 3 blocks Last used time Block2 from S T=k^2+1+k^2 Page Fault Counter = 4 block 1 of R joined with block 2 of S Block1 from R T=k^2+2 + k^2 -k Output block T=2k^2 + 1
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=2k^2+1 Page Fault Counter = 5 Block1 from R Page fault for Block 3 of S !! Which one would be replaced? T=2k^2 + 2 - k Output block T=2k^2 + 1
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time Which one would MRU have replaced?? Would it have prevented any unnecessary page fault Block2 from S T=2k^2+1 Page Fault Counter = 5 Block3 from S T= 2k^2 + 2 Output block T=2k^2 + 1
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=2k^2+1 Page Fault Counter = 6 Block3 from S Page fault for Block 1 of R !! Which one would be replaced? T= 2k^2 + 2 Output block T=2k^2 + 1
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=2k^2+3 Page Fault Counter = 6 Block3 from S Page fault for Block 1 of R !! Which one would be replaced? T= 2k^2 + 2 Output block T=2k^2 + 1
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=2k^2+3 Page Fault Counter = 6 Block3 from S T= 2k^2 + 2 + k Output block T=2k^2 + 2 + k
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=2k^2+3 +k^2 -k Page Fault Counter = 6 By now block 1 of R joined with block 3 of S Block3 from S T= 2k^2 + 2 + k^2 Output block T=2k^2 + 2 + k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block1 from R T=3k^2+3 - k Page Fault Counter = 7 Page fault for Block 2 of R !! Which one would be replaced? Block3 from S T= 3k^2 + 2 Output block T=3k^2 + 2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from R T=3k^2+ 3 Page Fault Counter = 8 Page fault for Block 1 of S !! Which one would be replaced? Block3 from S T= 3k^2 + 2 Output block T=3k^2 + 2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from R T=3k^2+ 3 Page Fault Counter = 8 Block1 from S T= 3k^2 + 4 Output block T=3k^2 + 2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from R T=3k^2+ 3 Page Fault Counter = 8 Block1 from S T= 3k^2 + 4 +k Output block T=3k^2 + 4 +k
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from R T=3k^2+ 3 + k^2 - k Page Fault Counter = 8 Block 2 of R joined with block 1 of S Block1 from S T= 3k^2 + 4 +k^2 Output block T=3k^2 + 4 +k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from R T=4k^2+ 3 - k Page Fault Counter = 9 Page fault for Block 2 of S !! Which one would be replaced? Block1 from S T= 4k^2 + 4 Output block T=4k^2 + 4
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Which one would MRU have replaced?? Would it have prevented any unnecessary page fault Last used time Block2 from S T=4k^2+ 5 Page Fault Counter = 9 Block1 from S T= 4k^2 + 4 Output block T=4k^2 + 4
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=4k^2+ 5 Page Fault Counter = 10 Page fault for Block 2 of R !! Which one would be replaced? Block1 from S T= 4k^2 + 4 Output block T=4k^2 + 4
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=4k^2+ 5 Page Fault Counter = 10 Block2 from R T= 4k^2 + 6 Output block T=4k^2 + 4
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=4k^2+ 5 +k Page Fault Counter = 10 Block2 from R T= 4k^2 + 6 Output block T=4k^2 + 5 + k
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=4k^2+ 5 +k^2 Page Fault Counter = 10 Block 2 of R joined with block 2 of S Block2 from R T= 4k^2 + 6 + k^2 -k Output block T=4k^2 + 5 + k^2
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S Page fault for Block 3 of S !! Which one would be replaced? T=5k^2+ 5 Page Fault Counter = 11 Block2 from R T= 5k^2 + 6 - k Output block T=5k^2 + 5
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time Which one would MRU have replaced?? Would it have prevented any unnecessary page fault Block2 from S T=5k^2+ 5 Page Fault Counter = 11 Block3 from S T= 5k^2 + 6 Output block T=5k^2 + 5
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Last used time LRU – Assume 3 buffers; R=2 S = 3 blocks Block2 from S T=5k^2+ 5 Page Fault Counter = 12 Page fault for Block 2 of R !! Which one would be replaced? Block3 from S T= 5k^2 + 6 Output block T=5k^2 + 5
LRU vs MRU --- Case on Nested Loop Join How would LRU and MRU perform on this Nested join algorithm? For each block in Relation R For each block in Relation S Test the join condition specified on each pair of records End For Compare with: Cost from estimated models of nested loop. Any surprises? MRU algorithm, For MRU--Care needs to be taken to prevent infinite page faults. May need to avoid replacing the page just brought it. Page Fault Counter = 12
Database Buffer managers can be much smarter these!
Domain Separation Technique Separate pages into statically assigned domains If page of type X is needed, then allocate in pool X LRU in each Domain If none are available in current domain (all pages have IO going on them), then borrow from another domain. 1: Root 2: Internal Nodes 3: Leaves
Pros and Cons of Domain Separation Pro: Big observation. Not all pages are the same! Con 1: Concept of domain is static Replacement should depend on how page is used Importance of a page depends on which algo is using it. E.g., a page of a relation in a sequential scan v. a nested join Con 2: No priorities among domains Index pages more frequently used than data pages. But this would allow index page to be over-written by another index page.
Separate Buffer pool for each relation == Working set “New” Algorithm Two Key Observations “The priority of a page is not a property of the page; in contrast, it is a property of the relation to which that page belongs.” Each relation has its own Working Set Separate Buffer pool for each relation == Working set
“New” Algorithm: Resident Set of Pages Free Pages List The resident sets are linked in a priority list. There is also a global free list at the top. Increasing importance of pages MRU, Relation 1 MRU, Relation 2 Each relation is assigned: Resident Set of Pages. Internally MRU is used for each relation.
“New” Algorithm: Resident Set of Pages Free Pages List Increasing importance of pages MRU, Relation 1 MRU, Relation 2 Intuition: - If near the top, then unlikely to be reused. - If near the bottom then pages are protected.
“New” Algorithm When a page fault occurs: Search is initiated from the top of the priority list. 2. Each relation is entitled to few buffers which are exempt from replacement consideration. Ordering among relations can be adjusted. A relation is place near top if its pages are unlikely to be re-used. Else it is protected at bottom.
Pros and Cons of “New” algorithm “New” algorithm could track locality of queries through relations. Advantages of MRU are limited to some cases. Ordering the relations is not trivial. Heuristic based (one could imagine some stats) Extensions to multi-user environment was not clear.
Hot Set Algorithm A hotset is a set of pages that an operation will loop over many times. Key Idea: If a query is given a buffer large enough to hold the hot sets. Then its processing would be efficient On the other hand, If a query is given less than it hot sets. It will have many page faults.
Nested Loop Join Hotset Hot Set Algorithm A hotset is a set of pages that an operation will loop over many times. Nested Loop Join Hotset = 1 + #blocks of S . Why? LRU # Page Faults Hot point # Buffers
Nested Loop Join Hotset Hot Set Algorithm A hotset is a set of pages that an operation will loop over many times. Nested Loop Join Hotset = 1 + #blocks of S . Why? LRU # Page Faults MRU Hot point Model was tied to LRU MRU may be better! # Buffers
DBMIN– Buffer management System
Query Locality Set Model (QLSM) Database access methods are predictable handful of typical algorithms So, define a handful of reference access methods.
Query Locality Set Model --- Nested Loop For instance, consider an index nested loop join with an index on the joining attribute. Two locality sets of this query: Index and data pages of the inner relation Sequential scan of the outer relation.
Reference Model --- Sequential Sequential References: Scanning a relation. Straight Sequential (SS). Only one access without repetition. How many pages should we allocate? Clustered Sequential (CS). Think Merge Join on a equality join What should we keep together? Looping Sequential (CR). Think Nested loop Join. What replacement policy on inner?
Reference Model --- Sequential Sequential References: Scanning a relation. Straight Sequential (SS). Only one access without repetition. How many pages should we allocate? (one) Clustered Sequential (CS). Think Merge Join What should we keep together? (records of a “cluster”, i.e., they have the same value) Looping Sequential (CR). Nested loop Join. What replacement policy on inner? (MRU)
Reference Model --- Hierarchical Straight Hierarchical: B+Tree Probe (X = 10) Hierarchical + Straight Sequential (X >= 10) (traversing the tree followed by a scan through leaves) Looping Hierarchical: Index-Nested Loop Join Inner relation has the index. Examples of Random References? Data pages are retrieved in random order.
DBMIN– Buffer management using QLSM Buffers are managed on a per file instance Active instances of the same file are given different buffer pools – In fact they may use different replacement policies! Files share pages via global table. Set of buffered pages associated with a file instance is called its locality set.
DBMIN– Buffer management using QLSM Replacement Policy (RP) of a buffer simply moves to Global Free list (not actual eviction!). Notation used: N : The total number of buffer frames available in system. Lij : The maximum number of buffers that can be allocated to a file instance j of query i. Rij : the #buffers allocated to file instance j of query i.
DBMIN– In case of Page Fault Case I: page in both global table and locality set of requesting process: Simply update stats (of as used by RP policy) and return.
DBMIN– In case of Page Fault Case II: Page in memory, but not in locality set If page has an owner, then simply return it Otherwise, page is allocated to requestor’s Locality Set. R is incremented by one. If R > L then a page is released to the global free list. Case III: Page not in memory. Bring it in, proceed as in case II.
DBMIN – #buffers and RP in QLSM Straight Sequential References: Locality set size: 1 ; new page overwrites the current. Clustered Sequential References: Locality set size: #recs in largest cluster/bfr ; LRU or FIFO Looping Sequential References: Locality set size: size of smaller relation; MRU
DBMIN – #buffers and RP in QLSM Random References: Use Yao’s formula to estimate the number of pages b in case of k random accesses. Locality set size between 1 and b. Residual value of a page = (k-b)/b; if < threshold then LS=1 Straight Hierarchical References: Locality set size: 1 ; new page overwrites the current.
DBMIN – #buffers and RP in QLSM Looping Hierarchical References: Index is repeatedly scanned Consider a tree with height h and fanout f. Level i would have f^i pages. If bi is #pages accessed according to Yao’s formula. Size of locality set= (1 + 𝑖=1 𝑗 𝑏 𝑖 ) + 1 Here, j is largest i such that (k-bi)/bi > a threshold Can use LIFO for this or something like domain separation for this.
DBMIN – #buffers and RP in QLSM For k references on a file with n records with m blocks #disk access (Yao’s formula) (approximately) = 𝑚 ∗ 1 − 𝑖=1 𝑡𝑜 𝑘 𝑛𝑑 −𝑖+1 𝑛−𝑖+1 ; 𝐻𝑒𝑟𝑒, 𝑑=1− 1 𝑚 ;