Upload
haneesha-muddasani
View
216
Download
0
Embed Size (px)
Citation preview
7/29/2019 lt22-bufferMgr
1/13
Database Systems & Applications
Lecture 22
Buffer Manager
7/29/2019 lt22-bufferMgr
2/13
Buffer Management
Query Optimizationand Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
You are here
Data must be in RAM for DBMS to operate on it!
Buffer Mgr hides the fact that not all data is in RAM
7/29/2019 lt22-bufferMgr
3/13
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 999
7/29/2019 lt22-bufferMgr
4/13
Requesting a page
22
MAIN MEMORY
DISK
disk page
free frames
BUFFER POOL
1 2 3 22 90
Higher level DBMS
component
I need
page 3
Disk Mgr
Buf Mgr
I need
page 33
3
If requests can be predicted (e.g., sequential scans) pages can
bepre-fetchedseveral pages at a time!
7/29/2019 lt22-bufferMgr
5/13
Releasing a page
22
MAIN MEMORY
DISK
disk page
free frames
BUFFER POOL
1 2 3 22 90
Higher level DBMS
component
I read page 3
and Im donewith it
Disk Mgr
Buf Mgr
3
7/29/2019 lt22-bufferMgr
6/13
Releasing a page
22
MAIN MEMORY
DISK
disk page
free frames
BUFFER POOL
1 2 3 22 90
Higher level DBMS
component
I wrote on page
3 and Im donewith it
Disk Mgr
Buf Mgr
3
3
3
7/29/2019 lt22-bufferMgr
7/13
More on Buffer Management
Requestor of page must eventually unpin it, andindicate whether page has been modified:
dirtybit is used for this.
Page in pool may be requested many times, apin countis used.
To pin a page, pin_count++
A page is a candidate for replacement iffpin count== 0(unpinned)
CC & recovery may entail additional I/O when aframe is chosen for replacement.
Write-Ahead Log protocol; more later!
7/29/2019 lt22-bufferMgr
8/13
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.
If no page in the BP has pin count 0 ??????
7/29/2019 lt22-bufferMgr
9/13
Buffer Replacement Policy
Frame is chosen for replacement by areplacement policy:
Least-recently-used (LRU), MRU, Clock, etc.
Policy can have big impact on # of I/Os;
depends on the access pattern.
7/29/2019 lt22-bufferMgr
10/13
LRU Replacement Policy Least Recently Used (LRU)
This can be implemented using a queue of pointers to frames withpin count 0.
A frame is added at the end of the queue when it became thecandidate for replacement (when its pin count goes to 0)
The page chosen for the replacement is the one in the frame at the
head of the queue. Works well for repeated accesses to popular pages
Problems?
Problem: Sequential flooding LRU + repeated sequential scans.
# buffer frames < # pages in file means each page request causes anI/O.
Idea: MRU better in this scenario?
7/29/2019 lt22-bufferMgr
11/13
2114
LRU causes sequential flooding in a sequential
scan
MAIN MEMORY
BUFFER POOL
1 2 3 4
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!!!
7/29/2019 lt22-bufferMgr
12/13
Clock Replacement Policy
An approximation of LRU
Arrange frames into a cycle, store one reference bitperframe
Can think of this as the 2nd chance bit
When pin count reduces to 0, turn on ref. bit
A(1)
B(p)
C(1)
D(1)
7/29/2019 lt22-bufferMgr
13/13
32 6
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 1
1 2 3 4
1
I needpage 5
4
Frame 2
Frame 3
Frame 4
5
ref
Higher level DBMScomponent
Buf Mgr5
6
I needpage 6