The Internals of PostgreSQL中对于存储的描述http://www.interdb.jp/pg/pgsql01.html#_1.2.
每个表或索引都作为一个page数组存储于物理数据文件中(page大小默认为8k,编译时可以指定),单个的表数据文件中包含多个page页(默认单个表文件最大1G,超过1G后会新建同名+“.1”后缀的数据文件,依次累加,同样在编译时可以指定单个文件的最大值)。page页的结构如下图所示。
而对于page页中存储的行数据Tuple,其结构为下图所示。
相关数据结构
// src/include/storage/bufpage.h
typedef struct PageHeaderData
{
PageXLogRecPtr pd_lsn;
uint16 pd_checksum;
uint16 pd_flags;
LocationIndex pd_lower;
LocationIndex pd_upper;
LocationIndex pd_special;
uint16 pd_pagesize_version;
TransactionId pd_prune_xid;
ItemIdData pd_linp[FLEXIBLE_ARRAY_MEMBER];
} PageHeaderData;
// src/include/storage/itemid.h
typedef struct ItemIdData
{
unsigned lp_off:15,
lp_flags:2,
lp_len:15;
} ItemIdData;
#define LP_UNUSED 0
#define LP_NORMAL 1
#define LP_REDIRECT 2
#define LP_DEAD 3
// src/include/access/htup_details.h
struct HeapTupleHeaderData
{
union
{
HeapTupleFields t_heap;
DatumTupleFields t_datum;
} t_choice;
ItemPointerData t_ctid;
#define FIELDNO_HEAPTUPLEHEADERDATA_INFOMASK2 2
uint16 t_infomask2;
#define FIELDNO_HEAPTUPLEHEADERDATA_INFOMASK 3
uint16 t_infomask;
#define FIELDNO_HEAPTUPLEHEADERDATA_HOFF 4
uint8 t_hoff;
#define FIELDNO_HEAPTUPLEHEADERDATA_BITS 5
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER];
};
// src/include/access/htup.h
typedef struct HeapTupleData
{
uint32 t_len;
ItemPointerData t_self;
Oid t_tableOid;
#define FIELDNO_HEAPTUPLEDATA_DATA 3
HeapTupleHeader t_data;
} HeapTupleData;