You are on page 1of 13

Database Systems & Applications

Lecture 22 Buffer Manager

Buffer Management
Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management

You are here

DB

Data must be in RAM for DBMS to operate on it! Buffer Mgr hides the fact that not all data is in RAM

Buffer Management in a DBMS


Page Requests from Higher Levels
BUFFER POOL disk page

free frame
MAIN MEMORY DISK

DB
4 5 6 7 8 9

choice of frame dictated by replacement policy

10 11 12 13 14 15 16 999

Buffer pool information table contains:

<frame#, pageid, pin_count, dirty>

Requesting a page
Higher level DBMS component

I need page 3

Buf Mgr disk page

BUFFER POOL 22

I need page 3

3 free frames

MAIN MEMORY

Disk Mgr
DISK

22

90

If requests can be predicted (e.g., sequential scans) pages can be pre-fetched several pages at a time!

Releasing a page
Higher level DBMS component

I read page 3 and Im done with it

Buf Mgr disk page

BUFFER POOL 22 3 free frames MAIN MEMORY

Disk Mgr
DISK

22

90

Releasing a page
Higher level DBMS component

I wrote on page 3 and Im done with it

Buf Mgr disk page

BUFFER POOL 22 3 free frames MAIN MEMORY

Disk Mgr
DISK

3 3

22

90

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!

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 ??????

Buffer Replacement Policy


Frame is chosen for replacement by a replacement policy:
Least-recently-used (LRU), MRU, Clock, etc.

Policy can have big impact on # of I/Os; depends on the access pattern.

LRU Replacement Policy


Least Recently Used (LRU)
This can be implemented using a queue of pointers to frames with pin count 0. A frame is added at the end of the queue when it became the candidate 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 an I/O. Idea: MRU better in this scenario?

LRU causes sequential flooding in a sequential scan


Higher level DBMS component

I need page 1 I need page 1

I need page 2 I need page 2!!!


BUFFER POOL

I need page 3

I need page 4

Buf Mgr

1 4

1 2

Disk Mgr
MAIN MEMORY DISK 1 2 3 4

Clock Replacement Policy


A(1) D(1) An approximation of LRU Arrange frames into a cycle, store one reference bit per frame
Can think of this as the 2nd chance bit

B(p)

C(1)

When pin count reduces to 0, turn on ref. bit

Clock Replacement Policy


Higher level DBMS component

Frame 1

I need page 5

I need page 6

Frame 4

Frame 2

Frame 3 ref

Buf Mgr

1 5

6 3

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;

You might also like