1.2 Internal Structure of a Page

How, then, is the inside of a page organized? If records were put anywhere in the order they arrive, the whole page would have to be scanned every time a record is looked up. So a database page is divided into three main parts.

Figure 1-1. Internal structure of a page

block-beta
  columns 9
  h["header"] s1["slot 1"] s2["slot 2"] s3["slot 3"] f["free space"]:2 r3["row 3"] r2["row 2"] r1["row 1"]
  space g1<["slot array grows -->"]>(right):3 space:1 g2<["<-- rows grow"]>(left):3 space
  class h meta
  class s1,s2,s3 slot
  class f free
  class r1,r2,r3 data

The figure lays out one page from left (the start of the page) to right (the end of the page).

  • header is the page header, placed at the very start of the page.
  • row 1, row 2, and row 3 are records stored in the data area. The first record (row 1) is placed at the very end of the page, and later records pile up in front of it in turn (<-- rows grow).
  • slot 1, slot 2, and slot 3 are the slot area (slot array). It starts right after the header and grows toward the end of the page (slot array grows -->), and each slot points to the location of the record with the same number.
  • free space is space not yet used. The slot area and the data area grow toward it from both sides, and when no space is left, the page is full.

First, the page header. It records the page number, which table's records the page holds, where the empty space begins and ends, and so on. It also holds a value indicating "up to which point in the log the last change to this page has been applied", which is the LSN covered under WAL in Chapter 5.

Second, the data area. Records are filled from the end of the page toward the front.

Third, the slot area. It is a list that records how many records the page has and where each one is. It holds locations (offsets) such as "record 1 is this far from the start of the page" and "record 2 is here". Records are written from the end of the page toward the front, and slots are written from the header side toward the records. When no space is left in the middle, the page is full.

Figure 1-2. Slot and record areas: from address to value

block-beta
  columns 9
  addr["record address = (page 17, slot 2)"]:9
  pg["page 17"]:9
  h["header"] s1["slot 1"] s2["slot 2"] s3["slot 3"] f["free space"]:2 d1["..."] r2["row 2"] d2["..."]
  n1["slot 2: start = 7980"]:4 space:2 n2["row 2: 7980 ~ 8099 (120 bytes)"]:3
  s2 --> r2
  class addr,pg plain
  class h meta
  class s1,s3 slot
  class s2,r2,n1,n2 hot
  class f free
  class d1,d2 muted

The figure shows the process of going from a record's address to its actual value.

  1. record address = (page 17, slot 2) at the top is the address of the record being looked up. A record address consists of two numbers: "which page, which slot".
  2. The database first finds page 17 and reads slot 2 (slot 2) in that page's slot area.
  3. Slot 2 holds the record's start position, 7980 (start = 7980). The arrow from slot 2 to row 2 is the path that follows this position.
  4. Going to byte 7980 of the page and reading 120 bytes (7980 to 8099) yields row 2, which is the record of Kim Cheol-su.

Reading just the header and the slot gives the record's location directly, so there is no need to scan the whole page.

Why keep a separate slot list at all? When a record in a page is deleted, free space appears, and sometimes the remaining records must be pushed toward the end of the page to consolidate it. Even if a record's actual location changes, only the location written in the slot needs to be updated, and from the outside it can still be called "slot 3". That is why a database expresses a record's address as two numbers: "which page, which slot". What an index points to in Chapter 3 is exactly this address.

There is another reason. Records vary in size. Some customers have a three-character name and some a twelve-character name, and some leave the address blank. So row length is variable. For a missing value (NULL), it is common to leave only a short marker meaning "empty here" and use no space. Even with the same column layout, row size varies with the values, so the number of records that fit in a page also varies from page to page. Since a record's location cannot be computed from a fixed interval, each record's location is written separately in a slot.