Prev: advanced-sql Next: database-storage-ii
This course focuses on disk-oriented DBMSes, so in-memory databases are out.
Volatile Devices:
Non-Volatile Devices:
There are some devices called non-volatile memory
that
are as fast as RAM but persistent. Intel’s Optane is an example of
this.
The DBMS will do manipulations in memory and then ship it out to disk.
The DBMS maintains a buffer pool that moves pages between memory and disk. The DBMS also has an execution engine that will execute queries. It will ask the buffer pool for a specific page, and the buffer pool will take care of bringing that page into memory and giving the execution engine a pointer to the page in memory.
The DBMS must support databases that exceed the amount of memory available. Also, since reading/writing to disk is expensive, it must be managed well.
The high-level design goal is similar to virtual memory. You can use
mmap
for this, by mapping the contents of a file in a
process’ address space, but if the OS gets a page fault, this blocks the
process.
mmap
should not be used for DBMS’ if writingmmap
can advise the OS with the following:
madvise
: Tell the OS when you are planning on reading
certain pages.mlock
: Tells the OS to not swap memory ranges to
disk.msync
: Tells the OS to flush memory ranges to
disk.The DBMS will write files on disk (normally in some binary fashion). Only the DBMS knows how to decipher their contents. The DBMS’s storage manager is responsible for managing the database’s files.
The DBMS organizes the database across fixed-size blocks called pages. Pages may contain data (metadata, tuples, indexes, etc). Most systems will not mix these types, and some will require that they are self-contained, meaning all information needed to read the page is on the page itself.
Each page is given a unique identifier. The DBMS generally has an indirection layer that maps a page id to a file path and offset. The storage manager will turn a page number into a file and offset to find the page.
Most DBMS’ use fixed-size pages to avoid engineering overhead.
There are three types of pages:
The storage device generally guarantees atomic writes of hardware pages – if the database’s page size is larger than the hardware’s page size, care needs to be taken to ensure that data gets written out safely, since the program can crash partway through writing a database page to disk.
The database heap can find the location of the page a DBMS wants on disk in a few ways:
Every page includes a header that records metadata about the page’s contents:
There are two main ways to laying out data in pages:
Slotted Pages: - The page maps slots to offsets - The header keeps track of the number of used slots and the offset of the starting location of the last used slot and a slot array, which keeps track of the location of the start of each tuple. - To add a tuple, the slot array will grow from the beginning to the end, and the data of the tuples will grow from the end to the beginning. The page is considered full when the slot array and the tuple data meet.
Log-Structured: - Stores records to file of how the database was modified (insert, update, deletes) - To read a record, the DBMS scans the log file backwards and “recreates” the tuple. - Fast writes, potentially slow reads. - Works well on append-only storage, because the DBMS cannot go back and update the data. - To avoid long reads, the DBMS can have indexes to allow it to jump to specific locations in the log, and also compact it.
Header: Contains metadata about the tuple. - Visibility information for the DBMS’ concurrency control protocol - Bit Map for NULL values.
Data: Data for attributes - Attributes are stored in the order that you specify them when you create the table. - Most DBMS’s do not allow a tuple to exceed the size of a page.
Unique Identifier: Identifier for tuple - Each tuple has a unique identifier. - Commonly (page_id + offset) - An application cannot rely on these ids to mean anything.
Prev: advanced-sql Next: database-storage-ii