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上的提升,这就是原因。

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...