CS4432: Database Systems II Buffer Manager 1. 2 Covered in week 1.

Slides:



Advertisements
Similar presentations
Storing Data: Disk Organization and I/O
Advertisements

Storing Data: Disks and Files CS 186 Spring 2006, Lecture 3 (R&G Chapter 9) Yea, from the table of my memory Ill wipe away all trivial fond records. --
1 Storing Data: Disks and Files Chapter 7. 2 Disks and Files v DBMS stores information on (hard) disks. v This has major implications for DBMS design!
5. Disk, Pages and Buffers Why Not Store Everything in Main Memory
Storing Data: Disks and Files
FILES (AND DISKS).
Buffer Management Notes Adapted from Prof Joe Hellersteins notes
Introduction to Database Systems1 Buffer Management Storage Technology: Topic 2.
Buffer Management The buffer manager reads disk pages into a main memory page as needed. The general idea is to minimize the amount of disk I/O by keeping.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Storing Data: Disks and Files Chapter 7.
Storing Data: Disks and Files
Buffer management.
 Presented By:Payal Gupta  Roll Number:106 (225 in scetion 2)  Professor :Tsau Young Lin.
B+-trees and Hashing. Model of Computation Data stored on disk(s) Minimum transfer unit: page = b bytes or B records (or block) If r is the size of a.
1 Database Buffer Management Yanlei Diao UMass Amherst Feb 20, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Storing Data: Disks and Files Lecture 3 (R&G Chapter 9) “Yea, from the table of my memory I’ll wipe away all trivial fond records.” -- Shakespeare, Hamlet.
Storing Data: Disks and Files Lecture 5 (R&G Chapter 9) “Yea, from the table of my memory I’ll wipe away all trivial fond records.” -- Shakespeare, Hamlet.
The Relational Model (cont’d) Introduction to Disks and Storage CS 186, Spring 2007, Lecture 3 Cow book Section 1.5, Chapter 3 (cont’d) Cow book Chapter.
Storing Data: Disks and Files Lecture 3 (R&G Chapter 9) “Yea, from the table of my memory I’ll wipe away all trivial fond records.” -- Shakespeare, Hamlet.
1.1 CAS CS 460/660 Introduction to Database Systems File Organization Slides from UC Berkeley.
13.6 Representing Block and Record Addresses Ramya Karri CS257 Section 2 ID: 206.
Introduction to Database Systems 1 Storing Data: Disks and Files Chapter 3 “Yea, from the table of my memory I’ll wipe away all trivial fond records.”
Layers of a DBMS Query optimization Execution engine Files and access methods Buffer management Disk space management Query Processor Query execution plan.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Storing Data: Disks and Files Chapter 9.
Database Management 6. course. OS and DBMS DMBS DB OS DBMS DBA USER DDL DML WHISHESWHISHES RULESRULES.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Storing Data: Disks and Files Chapter 7.
Storing Data: Disks and Files Lecture 3 (R&G Chapter 9) “Yea, from the table of my memory I’ll wipe away all trivial fond records.” -- Shakespeare, Hamlet.
Physical Storage Susan B. Davidson University of Pennsylvania CIS330 – Database Management Systems November 20, 2007.
Introduction to Database Systems 1 Storing Data: Disks and Files Chapter 3 “Yea, from the table of my memory I’ll wipe away all trivial fond records.”
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Storing Data: Disks and Files Chapter 7 “ Yea, from the table of my memory I ’ ll wipe away.
1 Storing Data: Disks and Files Chapter 9. 2 Disks and Files  DBMS stores information on (“hard”) disks.  This has major implications for DBMS design!
Bhanu Choudhary CS257 Section 1 ID: 101.  Introduction  Addresses in Client-Server Systems  Logical and Structured Addresses  Pointer Swizzling 
13.6 Representing Block and Record Addresses
“Yea, from the table of my memory I’ll wipe away all trivial fond records.” -- Shakespeare, Hamlet.
Exam I Grades uMax: 96, Min: 37 uMean/Median:66, Std: 18 uDistribution: w>= 90 : 6 w>= 80 : 12 w>= 70 : 9 w>= 60 : 9 w>= 50 : 7 w>= 40 : 11 w>= 30 : 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Storing Data: Disks and Files Content based on Chapter 9 Database Management Systems, (3.
1.1 CAS CS 460/660 Introduction to Database Systems Disks, Buffer Manager.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 7 – Buffer Management.
Representing Block & Record Addresses
CS422 Principles of Database Systems Buffer Management Chengyu Sun California State University, Los Angeles.
Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Storing Data: Disks and Files Chapter 7 Jianping Fan Dept of Computer Science UNC-Charlotte.
1 Storing Data: Disks and Files Chapter 9. 2 Objectives  Memory hierarchy in computer systems  Characteristics of disks and tapes  RAID storage systems.
Database Applications (15-415) DBMS Internals: Part II Lecture 12, February 21, 2016 Mohammad Hammoud.
Announcements Program 1 on web site: due next Friday Today: buffer replacement, record and block formats Next Time: file organizations, start Chapter 14.
Storing Data: Disks and Files Memory Hierarchy Primary Storage: main memory. fast access, expensive. Secondary storage: hard disk. slower access,
The very Essentials of Disk and Buffer Management.
CS222: Principles of Data Management Lecture #4 Catalogs, Buffer Manager, File Organizations Instructor: Chen Li.
CS522 Advanced database Systems
Module 11: File Structure
Storing Data: Disks and Files
Storing Data: Disks and Files
Database Applications (15-415) DBMS Internals: Part II Lecture 11, October 2, 2016 Mohammad Hammoud.
CS222/CS122C: Principles of Data Management Lecture #3 Heap Files, Page Formats, Buffer Manager Instructor: Chen Li.
Database Management Systems (CS 564)
Storing Data: Disks and Files
Lecture 10: Buffer Manager and File Organization
Database Applications (15-415) DBMS Internals: Part II Lecture 13, February 25, 2018 Mohammad Hammoud.
Introduction to Database Systems
Database Applications (15-415) DBMS Internals: Part III Lecture 14, February 27, 2018 Mohammad Hammoud.
Introduction to Database Systems
5. Disk, Pages and Buffers Why Not Store Everything in Main Memory
Storing Data: Disks and Files
CS222/CS122C: Principles of Data Management Lecture #4 Catalogs, File Organizations Instructor: Chen Li.
Database Management Systems (CS 564)
Basics Storing Data on Disks and Files
CS222P: Principles of Data Management Lecture #3 Buffer Manager, PAX
Storing Data: Disks and Files
Lecture 9: Caching and Demand-Paged Virtual Memory
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #03 Row/Column Stores, Heap Files, Buffer Manager, Catalogs Instructor: Chen Li.
Presentation transcript:

CS4432: Database Systems II Buffer Manager 1

2 Covered in week 1

Buffer Manager Higher-level components do not interact with Buffer Manager Buffer Manager manages what blocks should be in memory and for how long Any processing requires the data to be in main memory 3 Disk Storage Manager DB Higher-Level Components (E.g., Query Execution) Buffer Manager Main memory

Buffer Management in a DBMS DB MAIN MEMORY DISK disk page free frame Page Requests from Higher Levels BUFFER POOL choice of frame dictated by replacement policy Buffer Pool information table contains: … 999

Some Terminology 5 Disk Disk block (Disk page) Array called “Buffer Pool” Each entry is called “Frame” Main Memory Empty frame Used frame (has a page) Each entry in the Buffer Pool (Frame) can hold 1 disk block A disk block in memory is usually called “memory page” Buffer Manager Keeps track of: – Which frames are empty – Which disk page exists in which frame Meta Data Information:

Questions  Project 1 How to efficiently find an empty frame? Given a request for Block B1, how to efficiently find whether is exists of not? In which frame? 6 Main Memory Empty frame Used frame (has a page) Naïve Solution Scan the array with each request  O(n)

Questions  Project 1 How to efficiently find an empty frame? Given a request for Block B1, how to efficiently find whether is exists of not? In which frame? 7 Main Memory Empty frame Used frame (has a page) Better Solution (For Q1) Keep a list of the empty frame# {1, 30, 50, …} Better Solution (For Q1) Keep a bitmap of the array size … 0: Empty & 1: Used

Questions  Project 1 How to efficiently find an empty frame? Given a request for Block B1, how to efficiently find whether is exists of not? In which frame? 8 Main Memory Empty frame Used frame (has a page) Better Solution (For Q2) Keep a hash table, given block Id (e.g., B1)  Returns the frame # (if exists)

Requesting A Disk Page 22 MAIN MEMORY DISK disk page free frames BUFFER POOL …… Higher level DBMS component I need page 3 Disk Mgr Buf Mgr I need page * If requests can be predicted (e.g., sequential scans) pages can be pre-fetched several pages at a time!

Pin A Memory Page Pinning a page means not to take from the memory until un- pinned Why to pin a page – Keep it until the transaction completes – Page is important (referenced a lot) – Recovery & Concurrency control (they enforce certain order) – Swizzling pointers refer to it 10 Pin this page Can be a flag (T & F) Can be a counter (0 = unpinned) Can be a flag (T & F) Can be a counter (0 = unpinned)

Releasing Unmodified Page 22 MAIN MEMORY disk page free frames BUFFER POOL Higher level DBMS component I read page 3 and I ’ m done with it Buf Mgr 3 Unpin the page (if you can) since page is not modified  Just claim this frame# in free list No need to write back to disk

Releasing Modified page 22 MAIN MEMORY DISK disk page free frames BUFFER POOL …… Higher level DBMS component I wrote on page 3 and I ’ m done with it Disk Mgr Buf Mgr 3’3’ 3’3’ 3’3’

More on Buffer Management Requestor of page must eventually unpin it, and indicate whether page has been modified: – dirty bit is used for this. Page in pool may be requested many times, – a pin count is used. – To pin a page, pin_count++ – A page is a candidate for replacement iff pin count == 0 (“unpinned”) CC & recovery may entail additional I/O when a frame is chosen for replacement. – Write-Ahead Log protocol; more later! Meta Data Information:

What if the buffer pool is full?... If requested page is not in pool: – Choose a frame for replacement. Only “un-pinned” pages are candidates! – If frame is “dirty”, write it to disk – Read requested page into chosen frame Pin the page and return its address.

Buffer Replacement Policy Frame is chosen for replacement by a replacement policy: – Least-recently-used (LRU) – First-in-First-Out (FIFO), – Clock Policy Policy can have big impact on # of I/O’s; depends on the access pattern. May need additional metadata to be maintained by Buffer Manager

LRU Replacement Policy Least Recently Used (LRU) – for each page in buffer pool, keep track of time when last accessed – replace the frame which has the oldest (earliest) time – very common policy: intuitive and simple Works well for repeated accesses to popular pages Problems: Sequential flooding – LRU + repeated sequential scans. – # buffer frames < # pages in file means each page request causes an I/O. – Expensive  Each access modifies the metadata

LRU causes sequential flooding in a sequential scan MAIN MEMORY BUFFER POOL 1234 Higher level DBMS component I need page 1 Disk Mgr Buf Mgr I need page 2 3 I need page 3 I need page 4 DISK I need page 1 I need page 2…ARG!!!

“Clock” Replacement Policy An approximation of LRU Each frame has – Pin count  If larger than 0, do not touch it – Second chance bit (Ref)  0 or 1 Imagine frames organized into a cycle. A pointer rotates to find a candidate frame to free Frame 1 Frame 2 Frame 3 Frame 4 IF pin-count > 0 Then  Skip IF (pin-count = 0) & (Ref = 1)  Set (Ref = 0) and skip ( second chance) IF (pin-count = 0) & (Ref = 0)  free and re-use

326 “Clock” Replacement Policy do for each page in cycle { if (pincount == 0 && ref bit is on) turn off ref bit; else if (pincount == 0 && ref bit is off) choose this page for replacement; } until a page is chosen; Frame I need page 5 4 Frame 2 Frame 3 Frame 4 5 Ref = 1 Higher level DBMS component Buf Mgr 5 6 I need page 6

Back to The Bigger Picture 20

Relation  File  Blocks Each relation, e.g., R, has a corresponding heap file storing its data Catalog tables in DBMS store metadata information about each heap file – Its block Ids, how many blocks, free spaces 21 Select ID, name, address From R Where … Select ID, name, address From R Where …

Heap File Using a Page Directory The metadata info  directory Each entry in this directory points to a disk page. It contains – Block Id, how many records this block hold – Whether it has free space or not – Whether the free space is contiguous or not – … Data Page 1 Data Page 2 Data Page N Header Page DIRECTORY

Records with Disk Pointers 23

Records with Pointers 24 Block 1 Block 2 Disk It is not common in relational DBs But common in object-oriented & object-relational DBs A data record contains pointers to other addresses on disk – Either in same block – Or in different blocks

25 Pointer Swizzling When a block B1 is moved from disk to main memory – Change all the disk addresses that point to items in B1 into main memory addresses. – Also pointers to other blocks moved to memory can be changed – Need a bit for each address to indicate if it is a disk address or a memory address Why we do that? – Faster to follow memory pointers (only uses a single machine instruction)

26 Example of Swizzling Block 1 Block 2 Disk Main Memory read B1 into main memory swizzled unswizzled Block 1 Block 2 is still on disk

27 Example of Swizzling Block 1 Block 2 Disk Main Memory read B1 into main memory swizzled Block 1 Block 2 read B2 into main memory swizzled

28 Swizzling Policies Automatic Swizzling – As soon as block is brought into memory, swizzle all relevant pointers (if blocks are in memory) Swizzling on Demand – Only swizzle a pointer if and when it is actually followed (its block has to move to memory) No Swizzling – Do not change the pointer in the memory blocks – Depend only on a separate Translation Table

29 Automatic Swizzling When block B is moved to memory 1.Locate all pointers within B – Refer to the schema, which will indicate where addresses are in the records – For index structures, pointers are at known locations 1.Swizzle all pointers that refer to blocks in memory – Change the physical address to main-memory address – Set the swizzle bit = True – Update the Translation Table Physical addressMain-memory address

30 Automatic Swizzling (Cont’d) When block B is moved to memory 3.Pointers referring to blocks still on disk – Leave them un-swizzled for now – Add entry for them in the Translation table with empty main-memory address 4.Check the Translation Table – If any existing pointer points to B, then swizzle it – Update the Translation Table Physical addressMain-memory address Null Null

31 Example: Move of B1 to Memory (Steps 1, 2, 3) Block 1 Block 2 Disk Main Memory read B1 into main memory swizzled unswizzled Block 1 p1 p2 Physical addressMain-memory address P1M1 P2Null M1 p2

32 Example: Move of B2 to Memory (Step 4) Block 1 Block 2 Disk Main Memory read B1 into main memory swizzled Block 1 p1 p2 Physical addressMain-memory address P1M1 P2M2 M1 read B2 into main memory swizzled M2 Block 2

33 Unswizzling: Moving Blocks to Disk When a block is moved from memory back to disk – All pointers must go back to physical (disk) addresses Use Translation Table again Important to have an efficient data structure for the translation table – Either hash tables or indexes

34 Block 1 Block 2 Disk Main Memory read B1 into main memory swizzled Block 1 p1 p2 Physical addressMain-memory address P1M1 P2M2 M1 read B2 into main memory swizzled M2 Block 2 Question: Which Block is Easier to Move out of memory B1 or B2?

35 Main Memory Block 1 Disk Move B1 to disk p1 p2 Physical addressMain-memory address P1M1 P2M2 swizzled Block 1 M1 swizzled M2 Block 2 Use the Translation Table to convert M1 & M2 to P1 & P2 Write B1 to disk Easy Case: Moving Block 1

36 Main Memory swizzled Block 1 Physical addressMain-memory address P1M1 P2M2 M1 swizzled M2 Block 2 Harder Case: Moving Block B2 Approach 1 (Pin Block) A block with incoming pointers should be pinned in the memory buffer In that case, B2 cannot be removed from memory until the incoming pointers are removed

37 Main Memory swizzled Block 1 Physical addressMain-memory address P1M1 P2M2 M1M2 swizzled Block 2 Harder Case: Moving Block B2 Approach 2 (Unswizzle) Check Translation Table All incoming pointers should be unswizzled (back to disk addresses) Update Translation Table Remove B2 from memory p2