2.4 When Bytes Become a Record

A record stored in a page is just a contiguous byte buffer. The record buffer holds no information about where the customer number ends and the name begins. Even though the records in a page have no column delimiters, the customer information such as number and name can be decoded by consulting the catalog records. Each time the database reads a row, it looks at the catalog, cuts the bytes into columns, and turns them into values. This is called decoding. Conversely, turning values into bytes and writing them to a page is called encoding.

Let us check this in PostgreSQL. The following query reads, from the catalog pg_attribute, in what order and how many bytes each column of the customer table is stored.

SELECT  attnum, attname, atttypid::regtype AS type, attlen, attalign
FROM    pg_attribute
WHERE   attrelid = 'customer'::regclass AND attnum > 0
ORDER   BY attnum;
attnumattnametypeattlenattalign
1idinteger4i
2nametext-1i
3phonetext-1i
4gradesmallint2s

attnum is the position of the column within the row, and attlen is the column's size in bytes. -1 means a variable length that differs from value to value. attalign indicates at what byte boundary a value must be positioned (i is 4 bytes, s is 2 bytes). These four pieces of information are enough to turn a lump of bytes back into a record.

Figure 2-2. Decoding a row's bytes into columns with catalog information

block-beta
  columns 16
  t["one row in a heap page (PostgreSQL)"]:16
  th["tuple header"]:2 id["id · EA 03 00 00"]:3 nm["name · 15 | 9 B UTF-8"]:4 ph["phone · len | 13 B"]:5 gr["grade · 03 00"]:2
  o0["data start"]:2 o1["+4 · 4 B"]:3 o2["+14 · 10 B"]:4 o3["+28 · 14 B"]:5 o4["2 B"]:2
  tn["tuple header: xact info · data offset · natts = 4 · null bitmap?"]:16
  t2["catalog (pg_attribute) -> decoded value"]:16
  k1["1 id · integer · len 4"]:5 v1["EA 03 00 00 -> 0x000003EA"]:6 x1["1002"]:5
  k2["2 name · text · len -1"]:5 v2["15 = 10 bytes incl. header"]:6 x2["'김민수'"]:5
  k3["3 phone · text · len -1"]:5 v3["14 bytes incl. header"]:6 x3["phone number"]:5
  k4["4 grade · smallint · len 2"]:5 v4["03 00 (2-byte aligned)"]:6 x4["3"]:5
  class t,t2,tn plain
  class th meta
  class id,nm,ph,gr data
  class o0,o1,o2,o3,o4 muted
  class k1,k2,k3,k4 meta
  class v1,v2,v3,v4 slot
  class x1,x2,x3,x4 hot

The upper part of the diagram lays out one row stored in a page in byte order. The lower part shows the catalog information used for decoding (the order, name, type, and length from pg_attribute) and the value interpreted from each span. The row itself carries no column boundaries; every boundary line is computed from catalog information. The offsets (+4, +14, +28) are measured with the start of the data as 0.

Step 1: Read the tuple header. At the very front of the row is the tuple header. Along with information about the transaction that created the row, the header records from which byte the actual data starts, how many columns are stored in the row, and whether there is a NULL bitmap marking the columns that are NULL. A NULL column takes no bytes, so during decoding the bitmap is consulted to skip that column. In this example all four columns have values, so no column is skipped.

Step 2: Read id. From here on, columns are read in catalog order. id is an integer, so 4 bytes, EA 03 00 00, are read from the start of the data. An x86 CPU is little-endian, placing the least significant byte first, so this is interpreted as 0x03EA, that is, 1002.

Step 3: Read name. name is variable length (attlen = -1), so the first byte is examined first. For short values of 126 bytes or fewer, PostgreSQL attaches a 1-byte length header, and 0x15 means "10 bytes including the header." This is because the length is stored shifted left by one bit, and the last bit marks the short format (0x15 is binary 0001 0101: the upper seven bits are 10 and the last bit is 1). Reading the remaining 9 bytes, excluding the header, as UTF-8 gives '김민수'. So name occupies 10 bytes, from offset 4 through 13.

Step 4: Read phone. phone is handled the same way: the length header is read first, followed by 14 bytes including the header. This span runs from offset 14 through 27.

Step 5: Read grade. The last column, grade, is a smallint with attalign s, so 2 bytes are read at a 2-byte boundary. Offset 28 is a multiple of 2, so 03 00 is read directly, and interpreted as little-endian it becomes 3.

INSERT runs this process in reverse, encoding values into bytes with the same catalog information and writing them to the page.

Knowing this structure also explains why the ADD COLUMN in the next section can finish instantly. Adding a column leaves the bytes of existing rows as they are, and the column count recorded in their headers remains the old one. The database doing the decoding sees that "this row has only 4 columns but the catalog has 5," and fills in the fifth value with the default stored in the catalog. No page was modified; only the catalog was.

Row formats by DBMS

How a row is represented as bytes differs by product, but the principle is the same: "cut the bytes according to the order and types the catalog defines."

  • MySQL (InnoDB): The list of variable-length column lengths and the NULL bitmap are gathered at the front of the row, followed by the row header and the column values. A row in a clustered index also contains hidden columns such as the transaction ID and the rollback pointer.
  • SQL Server (Azure SQL): Fixed-length columns are gathered first, followed in order by the NULL bitmap, the list of end positions of the variable-length columns, and the variable-length values. This is why the column order in the catalog can differ from the byte order within the row.
  • Oracle: After the row header, each column is written as "length + value" in sequence. Trailing consecutive NULL columns at the end of the row are not stored at all, so a row can be shorter than the column count.

You do not normally see these bytes directly, but with a tool that shows pages as they are, such as PostgreSQL's pageinspect extension, you can check the bytes in the figure above yourself.