上次我们简单看了下PostgreSQL中页面的组织结构,介绍了PageHeader和行指针的结构。这次我们来看看元组的结构,也就是tuple的成员变量

Tuple的结构

元组的结构如下

1
2
3
4
5
6
7
typedef struct HeapTupleData
{
uint32 t_len;
ItemPointerData t_self;
Oid t_tableOid;
HeapTupleHeader t_data;
} HeapTupleData;
  • t_len,元组t_data字段的长度

  • t_self,记录元组自己的位置信息,包括所在的block信息,和元组在页面中的offset

  • t_tableOid,元组所在表的oid

  • t_data,元组的元数据信息和具体存储的数据

HeapTupleHeader

存储元组的元数据信息,结构如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct HeapTupleHeaderData
{
union
{
HeapTupleFields t_heap;
DatumTupleFields t_datum;
} t_choice;

ItemPointerData t_ctid;
uint16 t_infomask2;
uint16 t_infomask;
uint8 t_hoff;
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER];
};
  • t_heap,元组事务相关的域

  • t_datum,元组数据相关的域

  • t_cid,HOT元组会指向最新的元组位置,否则指向自己的位置

  • t_infomask2,元组的属性数,和一些标记位

  • t_infomask,元组的标记位。这些标记我们就不在这里介绍了

  • t_hoff,header的整体大小

  • t_bits,NULL值列的数组

t_bits数组之后存放的就是元组的数据

HeapTupleFields

元组事务相关的域

1
2
3
4
5
6
7
8
9
10
11
typedef struct HeapTupleFields
{
TransactionId t_xmin;
TransactionId t_xmax;

union
{
CommandId t_cid;
TransactionId t_xvac;
} t_field3;
} HeapTupleFields;
  • t_xmin,元组插入的事务号

  • t_xmax,元组删除的事务号

  • t_cid,命令id,表示当前事务中执行的修改该元组的命令的id

  • t_xvac,vacuum full操作的事务号

DatumTupleFields

元组数据相关的域

1
2
3
4
5
6
7
typedef struct DatumTupleFields
{
int32 datum_len_; /* varlena header (do not touch directly!) */
int32 datum_typmod; /* -1, or identifier of a record type */
Oid datum_typeid; /* composite type OID, or RECORDOID */

} DatumTupleFields;
  • datum_len_,变长数据类型的长度

  • datum_typmod,数据类型

  • datum_typeid,复合类型的类型oid