SourceURL:file:///home/cheng/bb/artical/BB系列文章002_PostgreSQL深度探秘_整数.docx
BB系列文章002
PostgreSQL深度探秘:整数
SourceURL:file:///home/cheng/bb/artical/BB系列文章002_PostgreSQL深度探秘_整数.docx
注:本文中使用的导出PostgreSQL表数据的工具pgexp和查看导出结果的工具bb可以在https://gitee.com/cheng719/bb.git下载, 包含了在linux下运行的这些工具和说明文档,如果需要其它平台(例如AIX、HPUX、Solaris等)(未支持这些平台的原因手头没有编译环境),请联系作者abusoft@sina.com。
如果要实现捕获PostgreSQL数据库的变化数据(CDC)以便用来实现数据库的容灾、备份、复用等,需要掌握数据库各种数据类型在磁盘里面实际存储的内容。
[if !supportLists]1. [endif]PostgreSQL整数包含如下几种类型:
整数类型存储长度描述范围
smallint2小整数-32768~+32767
integer4整数-2147483648 to +2147483647
bigint8长整数-9223372036854775808 to +9223372036854775807
smallserial2小自增整数1 to 32767
serial4自增整数1 to 2147483647
bigserial8长自增整数1 to 9223372036854775807
[if !supportLists]2. [endif]建立测试表,并且插入数据:
create table test_int (
f_smallint smallint,
f_int integer,
f_big bigint,
f_smallserial smallserial,
f_serial serial,
f_bigserial bigserial );
insert into test_int values ( 100,101, 102, 103, 104,105 );
insert into test_int values ( 1000,1010, 1020, 1030, 1040,1050 );
insert into test_int values ( 10000,10100, 10200, 10300, 10400,10500 );
[if !supportLists]3. [endif]使用工具pgexp导出表的数据:
$ pgexp -o /tmp/11 -t public.test_int
1118221628[5] Export: bank.public.test_int
1118221628[5] OUT: writedSize= 143 rows= 3 dataSize= 143
Export: rows=3, Length=552
导出了3条记录,结果存放在文件 /tmp/11,文件长度552。
[if !supportLists]4. [endif]使用工具bb查看导出的表的数据:
$ bb -3 /tmp/11
create table bank.public.test_int (
f_smallint int2
, f_int int4
, f_big int8
, f_smallserial smallserial not null
, f_serial serial not null
, f_bigserial bigserial not null
);
insert into bank.public.test_int (f_smallint,f_int,f_big,f_smallserial,f_serial,f_bigserial) values (100,101,102,103,104,105),
(1000,1010,1020,1030,1040,1050),
(10000,10100,10200,10300,10400,10500);
[if !supportLists]5. [endif]使用工具bb查看导出的表的数据的内部表示方法:
$bb -2 /tmp/11
R000 CC [ 1]: 06
c0_0 [ 2]: 0064 .d
c0_1 [ 4]: 0000 0065 ...e
c0_2 [ 8]: 0000 0000 0000 0066 .......f
c0_3 [ 2]: 0067 .g
c0_4 [ 4]: 0000 0068 ...h
c0_5 [ 8]: 0000 0000 0000 0069 .......i
R001 CC [ 1]: 06
c1_0 [ 2]: 03e8 ..
c1_1 [ 4]: 0000 03f2 ....
c1_2 [ 8]: 0000 0000 0000 03fc ........
c1_3 [ 2]: 0406 ..
c1_4 [ 4]: 0000 0410 ....
c1_5 [ 8]: 0000 0000 0000 041a ........
R002 CC [ 1]: 06
c2_0 [ 2]: 2710 '.
c2_1 [ 4]: 0000 2774 ..'t
c2_2 [ 8]: 0000 0000 0000 27d8 ......'.
c2_3 [ 2]: 283c (<
c2_4 [ 4]: 0000 28a0 ..(.
c2_5 [ 8]: 0000 0000 0000 2904 ......).
[if !supportLists]6. [endif]数据类型为 smallint、smallserial 的内部表示:
数值数据库文件内部存储
1000x0064
1030x0067
10000x03e8
10300x0406
100000x2710
103000x283c
即Postgresql存储smallint、smallserial的方法就是按照大头优先( big endian)存储2字节的小整数内存变量到数据库磁盘文件中。
[if !supportLists]7. [endif]数据类型为 integer、serial 的内部表示:
数值数据库文件内部存储
1010x00000065
1040x00000068
10100x000003f2
10400x00000410
101000x00002774
104000x000028a0
即Postgresql存储integer、serial的方法就是按照大头优先( big endian)存储4字节的整数内存变量到数据库磁盘文件中。
[if !supportLists]8. [endif]数据类型为 bigint、bigserial 的内部表示:
数值数据库文件内部存储
1020x0000000000000066
1050x0000000000000069
10200x00000000000003fc
10500x000000000000041a
102000x00000000000027d8
105000x0000000000002904
即Postgresql存储bigint、bigserial的方法就是按照大头优先( big endian)存储8字节的整数内存变量到数据库磁盘文件中。