PostgreSQL的可变长类型的内部定义

这部分还不是特别了解,仅供探讨,欢迎指正错误。

首先看源代码 src/include/c.h,这里有一个结构定义:
struct varlena
{
    char        vl_len_[4];        /* Do not touch this field directly! */
    char        vl_dat[1];
};

紧跟着这个定义之后,我们会发现:
typedef struct varlena bytea;
typedef struct varlena text;
typedef struct varlena BpChar;    /* blank-padded char, ie SQL char(n) */
typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */

可以看出,实际有四个类型使用这个结构定义。

文档中提到字符类型时,有这样一段描述:
There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. Whilecharacter(n)has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in factcharacter(n)is usually the slowest of the three because of its additional storage costs. In most situationstextorcharacter varyingshould be used instead.

通过上边的定义,我们知道为什么会这样说。

此外,长度字段前两位是标志,后30位才是长度,长度上限略小于1G的限制就是来源于此。
此类定义常见于 PG 各部分,比如块内偏移量与其他定义共用32位空间,实际它只有15位,因此限制整个块大小。以前有朋友问过,为什么PG最大支持32K的块,而更大的块能带来I/O上的提升,这就是原因。

转载于:https://my.oschina.net/quanzl/blog/135478

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值