PostgreSQL的数据存储(十二)---数据存储

4.3.2       Tuple是否可以跨页面

PostgreSQL uses a fixed page size (commonly 8 kB), and does not allow tuples to span multiple pages. Therefore, it is not possible to store very large field values directly. To overcome this limitation, large field values are compressed and/or broken up into multiple physical rows. This happens transparently to the user, with only small impact on most of the backend code. The technique is affectionately known as TOAST (or “the best thing since sliced bread”).

【翻译】PostgreSQL使用固定页面大小(通常是8字节),并且不允许元组跨越多个页面。因此,直接存储非常大的值是不可能的。为了克服这一限制,大字段值进行压缩和/或分成多个物理行。这对用户透明,对大多数后端代码只有小的影响。该技术是亲切地称为TOAST(或“有史以来最好的事情”)。

以上英文是PG帮助系统给出的描述,中文是简单翻译。对于数据库系统来说,这是处理大数据的最好的方式吗?

 

回到本节标题提出的问题,元组是否可以跨页面?答案是:PG存储的基本单位是tuple(元组,或称为记录),一条完整的tuple是不可以跨页面分开存储的。

 

4.3.3        微对象存储方式

一个tuple,可能由多种数据类型的“列”组成,有的列可能有空值,有的列可能是定长的,有的列可能是变长的。

列数据信息,要存储在物理存储设备,使用时要被读入内存,即内外存要交换数据,如何交换更为合适?

有个概念叫做“内存对齐”,是否有外存对齐?为什么要内存对齐?

 

PG处理元组,涉及读和写。

读数据块到buf,从buf变为数据页(pg知道数据页的格式,所以才能分解数据页上的信息);数据页上存在元组tuple,读出每个tuple,才能得知具体的数据;每个tuple的格式,也在tuple上标明,这样,才能识别tuple中的具体列的信息。这一步,是靠函数完成的。

写数据内容到buf,是先形成tuple(heap_form_tuple->heap_fill_tuple),然后把tuple写到buf,由系统在某个时刻,把脏的buf写到外存。

但是,如何形成tuple呢?不同数据类型的列,在创建表的时候,就已经把列存储相关的信息,写到了系统表(pg_attribute)中,在读取元组中的内容时,要根据系统表存储的信息,进行逐列读取。

PG规定了不同数据类型如何存储,基本原则一:如果是值存储,直接存储值;如果是引用存储,则先存放一个指针,指针指向真正数据所在的位置。可以用PG联机帮助中的话佐证:

typbyval determines whether internal routines pass a value of this type by value or by reference. typbyval had better be false if typlen is not 1, 2, or 4 (or 8 on machines where Datum is 8 bytes). Variable-length types are always passed by reference. Note that typbyval can be false even if the length would allow pass-by-value

PG存储数据类型基本原则二:如果是变长类型,则按变长方式处理,区分了一个页面存储不下的情况(toast方式)、较段的变长类型、以及其他等(查看heap_fill_tuple函数[1])。PG联机帮助中的话:

For a fixed-size type, typlen is the number of bytes in the internal representation of the type. But for a variable-length type, typlen is negative. -1 indicates a “varlena” type (one that has a length word), -2 indicates a null-terminated C string.

PG存储数据类型基本原则三:对于字符串的处理(上段中为“-2”的情况)。

       另外,其他情况的处理,如下:

typalign is the alignment required when storing a value of this type. It applies to storage on disk as well as most representations of the value inside PostgreSQL. When multiple values are stored consecutively, such as in the representation of a complete row on disk, padding is inserted before a datum of this type so that it begins on the specified boundary. The alignment reference is the beginning of the first datum in the sequence.

Possible values are:

c = char alignment, i.e., no alignment needed.

s = short alignment (2 bytes on most machines).

i = int alignment (4 bytes on most machines).

d = double alignment (8 bytes on many machines, but by no means all).

Note

For types used in system tables, it is critical that the size and alignment defined in pg_type agree with the way that the compiler will lay out the column in a structure representing a table row.

这段话是在说,不同类型的存储,是要求有对齐格式的,对齐,是内存中便于操作数据的一种指令加速方式。外存存储的数据,同内存中的格式一致,这样,就不用把外存读入的数据进行转换,存储时,也不用特意从内存格式转换为外存(非对齐)格式,加快了操作的效率,但牺牲了外存的存储空间。



[1] 查看heap_fill_tuple函数,可以清晰了解不同数据类型的存储细节


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值