博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL Page页结构解析(2)- 页头和行数据指针
阅读量:2497 次
发布时间:2019-05-11

本文共 4592 字,大约阅读时间需要 15 分钟。

本文简单介绍了PG数据页Page中存储的原始内容以及如何阅读它们,包括页头PageHeader和行数据指针ItemId(Line Pointer)。

一、测试数据

-- 创建一张表,插入几行数据

drop table if exists t_page;

create table t_page (id int,c1 char(8),c2 varchar(16));

insert into t_page values(1,'1','a');

insert into t_page values(2,'2','b');

insert into t_page values(3,'3','c');

insert into t_page values(4,'4','d');

-- 获取该表对应的数据文件

testdb=# select pg_relation_filepath('t_page');

pg_relation_filepath

----------------------

base/16477/24801

(1 row)

-- Dump数据文件中的数据

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801

00000000  01 00 00 00 88 20 2a 12  00 00 00 00 28 00 60 1f  |..... *.....(.`.|

00000010  00 20 04 20 00 00 00 00  d8 9f 4e 00 b0 9f 4e 00  |. . ......N...N.|

00000020  88 9f 4e 00 60 9f 4e 00  00 00 00 00 00 00 00 00  |..N.`.N.........|

00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

*

00001f60  e5 1b 18 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00001f70  04 00 03 00 02 08 18 00  04 00 00 00 13 34 20 20  |.............4  |

00001f80  20 20 20 20 20 05 64 00  e4 1b 18 00 00 00 00 00  |    .d.........|

00001f90  00 00 00 00 00 00 00 00  03 00 03 00 02 08 18 00  |................|

00001fa0  03 00 00 00 13 33 20 20  20 20 20 20 20 05 63 00  |.....3      .c.|

00001fb0  e3 1b 18 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00001fc0  02 00 03 00 02 08 18 00  02 00 00 00 13 32 20 20  |.............2  |

00001fd0  20 20 20 20 20 05 62 00  e2 1b 18 00 00 00 00 00  |    .b.........|

00001fe0  00 00 00 00 00 00 00 00  01 00 03 00 02 08 18 00  |................|

00001ff0  01 00 00 00 13 31 20 20  20 20 20 20 20 05 61 00  |.....1      .a.|

00002000

二、PageHeader

上一节提到过PageHeaderData,其数据结构如下:

typedef struct PageHeaderData

{

/* XXX LSN is member of *any* block, not only page-organized ones */

PageXLogRecPtr pd_lsn; /* LSN: next byte after last byte of xlog

* record for last change to this page */

uint16 pd_checksum; /* checksum */

uint16 pd_flags; /* flag bits, see below */

LocationIndex pd_lower; /* offset to start of free space */

LocationIndex pd_upper; /* offset to end of free space */

LocationIndex pd_special; /* offset to start of special space */

uint16 pd_pagesize_version;

TransactionId pd_prune_xid; /* oldest prunable XID, or zero if none */

ItemIdData pd_linp[1]; /* beginning of line pointer array */

}  PageHeaderData;

下面根据数据文件中的数据使用hexdump查看并逐个进行解析。

pd_lsn(8bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 0 -n 8

00000000  01 00 00 00 88 20 2a 12                          |..... *.|

00000008

数据文件的8个Bytes存储的是LSN,其中最开始的4个Bytes是TimelineID,在这里是\x0000 0001(即数字1),后面的4个Bytes是\x122a2088,组合起来LSN为1/122A2088

注意:

A、0000000&0000008是hexdump工具的输出,不是数据内容

B、X86使用小端模式,阅读字节码时注意高低位变换

pd_checksum(2bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 8 -n 2

00000008  00 00                                            |..|

0000000a

checksum为\x0000

pd_flags(2bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 10 -n 2

0000000a  00 00                                            |..|

0000000c

flags为\x0000

pd_lower(2bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 12 -n 2

0000000c  28 00                                            |(.|

0000000e

lower为\x0028,十进制值为40

pd_upper(2bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 14 -n 2

0000000e  60 1f                                            |`.|

00000010

[xdb@localhost utf8db]$ echo $((0x1f60))

8032

upper为\x1f60,十进制为8032

pd_special(2bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 16 -n 2

00000010  00 20                                            |. |

00000012

Special Space为\x2000,十进制值为8192

pd_pagesize_version(2bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 18 -n 2

00000012  04 20                                            |. |

00000014

pagesize_version为\x2004,十进制为8196(即版本4)

pd_prune_xid(4bytes)

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 20 -n 4

00000014  00 00 00 00                                      |....|

00000018

prune_xid为\x0000,即0

三、ItemIds

PageHeaderData之后是ItemId数组,每个元素占用的空间为4Bytes,数据结构:

typedef struct ItemIdData

{

unsigned lp_off:15,/* offset to tuple (from start of page) */

               lp_flags:2,/* state of item pointer, see below */

              lp_len:15;/* byte length of tuple */

} ItemIdData;

typedef ItemIdData* ItemId;

lp_off

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 24 -n 2

00000018  d8 9f                                            |..|

0000001a

取低15位

[xdb@localhost utf8db]$ echo $((0x9fd8 & ~$((1<<15))))

8152

表示第1个Item(tuple)从8152开始

lp_len

[xdb@localhost utf8db]$ hexdump -C $PGDATA/base/16477/24801 -s 26 -n 2

0000001a  4e 00                                            |N.|

0000001c

取高15位

[xdb@localhost utf8db]$ echo $((0x004e >> 1))

39

表示第1个Item(tuple)的大小为39

lp_flags

取第17-16位,01,即1

四、小结

以上简单介绍了如何阅读Raw Datafile,包括页头和数据行指针信息,有兴趣的同学可在此基础上实现自己的“pageinspect"。下一节将介绍数据行信息。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2374922/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/6906/viewspace-2374922/

你可能感兴趣的文章
免费的Bootstrap等待页面的应用模板
查看>>
JS DOM操作(四) Window.docunment对象——操作内容
查看>>
machine learning 之 Recommender Systems
查看>>
新房装修三大空鼓解决方法 为家居装修做好前奏
查看>>
vue.js路由vue-router
查看>>
小程序丨页面去掉转发按钮
查看>>
centos6.4安装flashcache
查看>>
树、二叉树基础
查看>>
指针数组,数组指针,指针函数,函数指针,二级指针详解
查看>>
判断浏览器类型和版本
查看>>
七, 表查询 一
查看>>
ContentProvider备份短信,以xml文件存储
查看>>
file-loader及url-loader的使用
查看>>
熟悉常用的HBase操作,编写MapReduce作业
查看>>
关于clipboard插件的使用问题
查看>>
Pycharm使用中背景颜色和更改项目的Python版本
查看>>
关情纸尾-----UIKit基础-简述KVC和KVO
查看>>
[Poj]1905——二分
查看>>
Makefile 使用总结
查看>>
控制反转
查看>>