06 / 19 · Storage and Execution
Pages, records and the buffer pool
Follow a stored row from its page slot into memory. Estimate page capacity and trace which reads a small buffer pool can avoid.
The shop split orders and lines into separate tables. To display one line, the database still has to locate its bytes and bring them into memory. The storage device transfers blocks, while SQL talks about rows. A page layout connects those units.
Find one record inside a page#
A page is a fixed-size unit of storage managed by the database. A heap table places records on heap pages without requiring the pages to be arranged by a search key. Within one page, records can differ in size. A fixed record number can still address a record if the number selects a slot holding the record’s current offset.
A PostgreSQL heap page has a header, an array of item identifiers, free space and tuple data. The page header occupies 24 bytes. Each item identifier occupies 4 bytes and holds an offset and length. As we add records, the array grows from the front and tuples use space from the other end. A tuple may be moved inside the page during compaction without changing its slot number. A PostgreSQL physical tuple identifier names a page and an item within the page. An update can create a tuple at a new address. An application that needs to refer to the same order across such updates uses a stable key such as order_id. (PostgreSQL 18, §66.6)
A slot points to the tuple
SQLite uses a related division in B-tree pages. Its cell pointer array has two-byte offsets, and the cell contents occupy a separate region. The offset scheme is shared. The layout and organization of the table depend on the engine. (SQLite, §1.6)
Estimate pages from row size#
Take a toy 8192-byte heap page with a 24-byte header. Suppose every tuple occupies 100 bytes after its own overhead and alignment, and every tuple needs one 4-byte slot. Assume we pack pages as tightly as possible and ignore any special space and later version churn. The page has available bytes. Dividing by gives 78 complete tuple-slot pairs. The 78 pairs use 8112 bytes and leave 56 bytes free. One more pair would need 104 bytes.
In this model 78,000 rows use exactly 1000 pages. Those pages occupy bytes. A wider tuple reduces capacity and can increase the number of pages touched by a scan. The estimate is a packed toy layout, not a size prediction for a PostgreSQL table with variable-width values, free-space policy, auxiliary storage or several versions of a row.
Reuse a page in memory#
A buffer pool holds page copies in memory. A page request is a hit if the page is already in a frame. On a miss the engine needs to bring it into a frame. A miss at this layer can be served by another cache, so it is not evidence of physical device I/O. When all frames are occupied, a replacement policy selects an available victim. A frame in use can be pinned so it cannot be evicted. A changed, or dirty, page needs a writeback before its only modified copy can be discarded. (CMU, Buffer Pool Management)
Consider three frames and the access sequence A B A C A D B. Use least recently used replacement (LRU), no pins and clean pages. The first A and B miss. The second A hits. C fills the last frame, and the third A hits. D then evicts B, which has the oldest last access. The final B misses and evicts C. There are five misses and two hits. This trace illustrates one replacement policy. A DBMS can implement a different one.
A sequential scan visits all its table pages even when some visits hit in memory. A search structure changes which pages the query needs to visit in the first place.
Next#
A lookup for one order should not have to inspect every heap page. Indexes and access paths connect a search key to a much smaller set of page and slot references.
References#
- PostgreSQL Global Development Group. Database Page Layout, §66.6. PostgreSQL 18 documentation, on the page header, item identifiers and tuple layout.
- SQLite Project. Database File Format, §1.6. Documents the cell pointer array and B-tree cell contents.
- Andy Pavlo. Buffer Pool Management. CMU 15-445/645, Fall 2024, sections on page frames, pin counts, dirty pages and LRU.