Database Systems Memory Management Guide
Database Systems Memory Management Guide
Systems
Memory & Disk
Management
15-445/645 FALL 2024 PROF. ANDY PAVLO
LAST CLASS
Problem #1: How the DBMS represents the
database in files on disk.
DATABASE STORAGE
Spatial Control:
→ Where to write pages on disk.
→ The goal is to keep pages that are used together often as
physically close together as possible on disk.
Temporal Control:
→ When to read pages into memory, and when to write them
to disk.
→ The goal is to minimize the number of stalls from having
to read data from disk.
DISK-ORIENTED DBMS
Get Page #2 Execution
Pointer to Page #2
Engine
Buffer Pool
Directory Header
2 Frames
Memory
Database File
1 2 3 4 5 … Pages
Disk
5-445/645 (Fall 2024)
DISK-ORIENTED DBMS
Get Page #2 Execution
Pointer to Page #2
Engine
Buffer Pool
Directory Header
2 Frames
Memory
Database File
1 2 3 4 5 … Pages
Disk
5-445/645 (Fall 2024)
TODAY'S AGENDA
Buffer Pool Manager
Why MMAP Will Murder Your DBMS
Disk I/O Scheduling
Replacement Policies
Other Memory Pools
On-Disk File
5-445/645 (Fall 2024)
On-Disk File
5-445/645 (Fall 2024)
frame4
→ Pin/Reference Counter
→ Access Tracking Information
page1 page2 page3 page4
On-Disk File
5-445/645 (Fall 2024)
frame4
→ Pin/Reference Counter
→ Access Tracking Information
page1 page2 page3 page4
On-Disk File
5-445/645 (Fall 2024)
frame4
→ Pin/Reference Counter
→ Access Tracking Information
page1 page2 page3 page4
On-Disk File
5-445/645 (Fall 2024)
Latches:
→ Protects the critical sections of the DBMS's internal data
structure from other threads. ←Mutex
→ Held for operation duration.
→ Do not need to be able to rollback changes.
[Link]
Goals:
→ Correctness
→ Accuracy
→ Speed
→ Meta-data overhead
LEAST-RECENTLY USED
Maintain a single timestamp of when
Disk Pages
each page was last accessed. When the
DBMS needs to evict a page, select the page0
one with the oldest timestamp.
→ Keep the pages in sorted order to reduce Q1 page1
the search time on eviction.
page2
LEAST-RECENTLY USED
Maintain a single timestamp of when
Disk Pages
each page was last accessed. When the
DBMS needs to evict a page, select the page0
one with the oldest timestamp.
→ Keep the pages in sorted order to reduce Q1 page1
the search time on eviction.
page2
LEAST-RECENTLY USED
Maintain a single timestamp of when
Disk Pages
each page was last accessed. When the
DBMS needs to evict a page, select the page0
one with the oldest timestamp.
→ Keep the pages in sorted order to reduce Q1 page1
the search time on eviction.
page2
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page2
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=1
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page2
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page2
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page2
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page2
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page5
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=1 ref=0
Organize pages in a circular buffer page4 page5
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=1
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page5
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page5
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
CLOCK
Approximation of LRU that does not
need a separate timestamp per page. ref=0
→ Each page has a reference bit. page1
→ When a page is accessed, set its bit to 1.
ref=0 ref=0
Organize pages in a circular buffer page4 page5
with a "clock hand" that sweeps over
pages in order:
→ As the hand visits each page, check if its page3
bit is set to 1.
→ If yes, set to zero. If no, then evict. ref=0
OBSERVATION
LRU + CLOCK replacement policies are susceptible
to sequential flooding.
→ A query performs a sequential scan that reads every page in
a table one or more times (e.g., blocked nested-loop joins).
→ This pollutes the buffer pool with pages that are read once
and then never again.
SEQUENTIAL FLOODING
page1
page0 page3
page4
page5
5-445/645 (Fall 2024)
SEQUENTIAL FLOODING
page1
page0 page3
page4
page5
5-445/645 (Fall 2024)
SEQUENTIAL FLOODING
page1
page0 Q2 page3
page1 page4
page2 page5
5-445/645 (Fall 2024)
SEQUENTIAL FLOODING
page1
page3 Q2 page3
page1 page4
page2 page5
5-445/645 (Fall 2024)
SEQUENTIAL FLOODING
page3 Q2 page3
page1 page4
page2 page5
5-445/645 (Fall 2024)
SEQUENTIAL FLOODING
page3 Q2 page3
page1 page4
page2 page5
5-445/645 (Fall 2024)
page3
HEAD Young List HEAD Old List
page4
page4 page5 page9 page3 page6 page2 page8
Newest←Oldest page5
page3
HEAD Young List HEAD Old List
page4
page4 page5 page9 page3 page6 page2 page8
Newest←Oldest page5
page3
HEAD Young List HEAD Old List
page4
page4 page5 page9 page3 page6 page2 page8
Newest←Oldest page5
page3
HEAD Young List HEAD Old List
page4
page4 page5 page9 page3 page1 page6 page2
Newest←Oldest page5
page3
HEAD Young List HEAD Old List
page4
page4 page5 page9 page3 page1 page6 page2
Newest←Oldest page5
page3
HEAD Young List HEAD Old List
page4
page4 page5 page9 page3 page1 page6 page2
Newest←Oldest page5
page3
HEAD Young List HEAD Old List
page4
page1 page4 page5 page9 page1
page3 page6 page2
Newest←Oldest page5
index-page0
Q1 INSERT INTO A VALUES (id++)
index-page1 index-page4
MIN id MAX
5-445/645 (Fall 2024)
index-page0
Q1 INSERT INTO A VALUES (id++)
index-page1 index-page4
MIN id MAX
5-445/645 (Fall 2024)
index-page0
Q1 INSERT INTO A VALUES (id++)
index-page1 index-page4
Q2 SELECT * FROM A WHERE id = ?
index-page2 index-page3 index-page5 index-page6
MIN id MAX
5-445/645 (Fall 2024)
index-page0
Q1 INSERT INTO A VALUES (id++)
index-page1 index-page4
Q2 SELECT * FROM A WHERE id = ?
index-page2 index-page3 index-page5 index-page6
MIN id MAX
5-445/645 (Fall 2024)
DIRTY PAGES
Fast Path: If a page in the buffer pool is not dirty,
then the DBMS can simply "drop" it.
BACKGROUND WRITING
The DBMS can periodically walk through the page
table and write dirty pages to disk.
OBSERVATION
OS/hardware tries to maximize disk bandwidth by
reordering and batching I/O requests.
But they do not know which I/O requests are more
important than others.
OS PAGE CACHE
Most disk operations go through the
OS API. Unless the DBMS tells it not DBMS
to, the OS maintains its own User-space read(...)
filesystem cache (aka page cache, Kernel-space
buffer cache). Filesystem
OS PAGE CACHE
Most disk operations go through the
OS API. Unless the DBMS tells it not DBMS
to, the OS maintains its own User-space read(...)
filesystem cache (aka page cache, Kernel-space
buffer cache). Filesystem
29
OS PAGE CACHE
Most disk operations go through the
OS API. Unless the DBMS tells it not DBMS
to, the OS maintains its own User-space read(...)
filesystem cache (aka page cache, Kernel-space
buffer cache). Filesystem
FSYNC PROBLEMS
If the DBMS calls fwrite, what happens?
FSYNC PROBLEMS
If the DBMS calls fwrite, what happens?
PRE-FETCHING
The DBMS can also prefetch pages Disk Pages
based on a query plan.
→ Examples: Sequential vs. Index Scans Q1 page0
page3
page4
page5
PRE-FETCHING
The DBMS can also prefetch pages Disk Pages
based on a query plan.
→ Examples: Sequential vs. Index Scans Q1 page0
page0 page3
page4
page5
PRE-FETCHING
The DBMS can also prefetch pages Disk Pages
based on a query plan.
→ Examples: Sequential vs. Index Scans page0
page0 page3
page1 page4
page5
PRE-FETCHING
The DBMS can also prefetch pages Disk Pages
based on a query plan.
→ Examples: Sequential vs. Index Scans page0
page0 page3
page1 page4
page5
PRE-FETCHING
The DBMS can also prefetch pages Disk Pages
based on a query plan.
→ Examples: Sequential vs. Index Scans page0
page3 page3
page1 page4
page2 page5
PRE-FETCHING
The DBMS can also prefetch pages Disk Pages
based on a query plan.
→ Examples: Sequential vs. Index Scans page0
page3 page3
page1 page4
page2 page5
PRE-FETCHING
The DBMS can also prefetch pages Disk Pages
based on a query plan.
→ Examples: Sequential vs. Index Scans page0
page3 page3
page4 page4
page5 Q1 page5
PRE-FETCHING
Disk Pages
Q1 SELECT * FROM A
index-page0
WHERE val BETWEEN 100 AND 250
index-page1
index-page3
index-page4
index-page5
PRE-FETCHING
index-page0
Disk Pages
index-page1 index-page4 index-page0
index-page2 index-page3 index-page5 index-page6
index-page1
0 99 100 199 200 299 300 399
index-page3
index-page4
index-page5
PRE-FETCHING
index-page0
Disk Pages
index-page1 index-page4
Q1 index-page0
index-page2 index-page3 index-page5 index-page6
index-page1
0 99 100 199 200 299 300 399
index-page0 index-page3
index-page4
index-page5
PRE-FETCHING
index-page0
Disk Pages
index-page1 index-page4 index-page0
index-page2 index-page3 index-page5 index-page6
Q1 index-page1
0 99 100 199 200 299 300 399
index-page0 index-page3
index-page1 index-page4
index-page5
PRE-FETCHING
index-page0
Disk Pages
index-page1 index-page4 index-page0
index-page2 index-page3 index-page5 index-page6
Q1 index-page1
0 99 100 199 200 299 300 399
index-page0 index-page3
index-page1 index-page4
index-page5
SCAN SHARING
Allow multiple queries to attach to a single cursor
that scans a table.
→ Also called synchronized scans.
→ This is different from result caching.
Examples:
→ Fully supported in DB2, MSSQL, Teradata, and Postgres.
→ Oracle only supports cursor sharing for identical queries.
SCAN SHARING
Allow multiple queries to attach to a single cursor
that scans a table.
→ Also called synchronized scans.
→ This is different from result caching.
Examples:
→ Fully supported in DB2, MSSQL, Teradata, and Postgres.
→ Oracle only supports cursor sharing for identical queries.
SCAN SHARING
Allow multiple queries to attach to a single cursor
that scans a table.
→ Also called synchronized scans.
→ This is different from result caching.
Examples:
→ Fully supported in DB2, MSSQL, Teradata, and Postgres.
→ Oracle only supports cursor sharing for identical queries.
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
Q1 page0
page1
page3
page4
page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
Q1 page0
page1
page0 page3
page4
page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
page1
page0 page3
page1 page4
page2 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
page1
page0 Q1 page3
page1 page4
page2 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
page1
page3 Q1 page3
page1 page4
page2 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
Q2 page0
Q2 SELECT AVG(val) FROM A
page1
page3 Q1 page3
page1 page4
page2 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
Q2 SELECT AVG(val) FROM A
page1
page3 Q2 Q1 page3
page1 page4
page2 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
Q2 SELECT AVG(val) FROM A
page1
page3 page3
page4 page4
page5 Q2 Q1 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
Q2 page0
Q2 SELECT AVG(val) FROM A
page1
page3 page3
page4 page4
page5 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
Q2 SELECT AVG(val) FROM A
page1
page0 page3
page1 page4
page2 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
Q2 SELECT AVG(val) FROM A LIMIT 100
page1
page0 page3
page1 page4
page2 page5
SCAN SHARING
Disk Pages
Q1 SELECT SUM(val) FROM A
page0
Q2 SELECT AVG(val) FROM A LIMIT 100
page1
page0 page3
page1 page4
page2 page5
CONCLUSION
The DBMS can almost always manage memory
better than the OS.
NEXT CLASS
Hash Tables
PROJECT #1
You will build the first component of
your storage manager.
→ LRU-K Replacement Policy
→ Disk Scheduler
→ Buffer Pool Manager Instance
Due Date:
Sunday Sept 29th @ 11:59pm
5-445/645 (Fall 2024)
General Hints:
→ Your LRUKReplacer needs to check the "pinned" status of
a Page.
→ If there are no pages touched since last sweep, then return
the lowest page id.
Disk Scheduler
page0
page1
It's up to you to decide how you want page2
to batch, reorder, and issue read/write
requests to the local disk.
Make sure it is thread-safe!
Disk Scheduler
→ Use whatever data structure you want page6 page0
for the page table. page2 page1
page4 page2
Make sure you get the order of
operations correct when pinning!
THINGS TO NOTE
Do not change any file other than the six that you
must hand in. Other changes will not be graded.
CODE QUALITY
We will automatically check whether you are
writing good code.
→ Google C++ Style Guide
→ Doxygen Javadoc Style
EXTRA CREDIT
Gradescope Leaderboard runs your code with a
specialized in-memory version of BusTub.
The top 20 fastest implementations in the class will
receive extra credit for this assignment.
→ #1: 50% bonus points
→ #2–10: 25% bonus points
→ #11–20: 10% bonus points
Student with the most bonus points at the end of
the semester will receive a BusTub schwag!
PLAGIARISM WARNING
The homework and projects must be your own
original work. They are not group assignments.
You may not copy source code from other people
or the web.