5.2 Tuple Structure
Heap tuples in table pages are categorized into two types: standard data tuples and TOAST tuples. This section describes the structure of standard data tuples.
A heap tuple comprises three parts: the HeapTupleHeaderData structure defined in htup_details.h, a NULL bitmap, and the user data (Figure 5.2).
Figure 5.2. Tuple structure.
While the HeapTupleHeaderData structure contains several fields, the following four are essential for understanding concurrency control:
- t_xmin: Stores the txid of the transaction that inserted the tuple.
- t_xmax: Stores the txid of the transaction that deleted or updated the tuple. If the tuple has not been deleted or updated, t_xmax is set to 0 (Invalid).
- t_cid: Stores the command ID (cid), representing the number of SQL commands executed before the current command within the same transaction, starting from 0.
For example, in a transaction block containing three INSERT commands: ‘BEGIN;INSERT;INSERT;INSERT;COMMIT’, the tuple inserted by the first command has a t_cid of 0, the second a t_cid of 1, and the third a t_cid of 2. - t_ctid: Stores the tuple identifier (tid), which points either to the tuple itself or to a newer version. As described in Section 1.3, the tid identifies a tuple’s physical location within a table.
When a tuple is updated, its t_ctid is updated to point to the new version of the tuple; otherwise, it points to itself.