Pages, tuples, and column storage
This page describes the physical format Amphi uses to store pages, tuples, and row values. The format is documented to make storage costs and wide-table limits transparent; applications do not need to construct these bytes manually.
Heap page
A heap page uses a slotted-page layout:
+----------------+------------------+----------------+-------------------+
| page header | line pointers | free space | tuples |
| 32 bytes | 4 bytes each | | |
+----------------+------------------+----------------+-------------------+
0 lower upper page size
The page header is 32 bytes. Each line pointer is 4 bytes and identifies a tuple by a stable slot. A tuple can move during compaction without changing that slot. Tuples are aligned to 8-byte units.
Tuple and row body
Each tuple has a 32-byte physical header followed by the encoded row body:
+-------------------+----------------------+
| tuple header | row body |
| 32 bytes | RowCodec |
+-------------------+----------------------+
The row body is:
[n_columns u16]
[null bitmap]
[optional external bitmap]
[fixed region]
[variable offsets]
[variable region]
The column count uses 2 bytes. The null bitmap uses 1 bit per column. Variable columns have one 2-byte offset containing the end of their value in the variable region. Fixed-width columns are packed separately in schema order.
For example, with id int4, name varchar(5), and code char(5), values 7,
'AB', and 'X' are conceptually encoded as:
+----------+------+------------+----------------+----------------------+
| columns | null | fixed | var offsets | variable region |
+----------+------+------------+----------------+----------------------+
| 03 00 | 00 | id = 7 | 02 00 06 00 | A B X _ _ _ |
+----------+------+------------+----------------+----------------------+
The offsets are ends, not (start, length) pairs:
name: starts at 0 and ends at 2
code: starts at 2 and ends at 6
var offsets = [2, 6]
var region = "ABX "
The null bitmap distinguishes NULL from an empty string, whose start and end
offsets can be equal.
varchar(n) and char(n)
Both types use the variable-width text storage format. There is no additional
per-value length byte for varchar(n) or char(n); the physical length comes
from the variable-region offsets.
The semantic limit is stored in the column catalog metadata:
varchar without a limit : type_mod = -1
varchar(5) : type_mod = 5
char(5) : type_mod = -6
varchar(n) rejects values longer than n. char(n) pads shorter values with
spaces and rejects values longer than n. Text is stored as UTF-8: validation
counts Unicode characters, while offsets count physical bytes.
Column limit and overhead
The current structural limit is 32,767 columns per table. The column counter is
a u16, but its top bit is reserved to indicate the presence of the external
bitmap.
The control overhead is:
fixed column : 1 null-bitmap bit + fixed value bytes
variable column : 1 null-bitmap bit + 2-byte offset + value bytes
For example, 100 variable columns use 13 null-bitmap bytes and 200 offset bytes, in addition to the 2-byte counter and the values. Wide rows can hit tuple or page size limits before reaching the structural column-count limit.