南大通用GBase8s数据库中的Partition Page解析

142 阅读17分钟

原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。

这篇文章我们来探讨GBase8s数据库中一个关键的数据存储结构——Partition Page(分区页)。

概述

在每个dbspace的第一个chunk中,chunk free列表后面的页面标记tblspace tblspace(分区表)的开始。

表(或索引)有不分片和分片两种。对于不分片的情况,这个表(或索引)就只有一个片(partition);对于分片的情况,这个表(或索引)有多个片(partition)。

1个片(也叫“分片”),就是1个partition,也就是1个tblspace。这个tblspace的元信息保存在tblspace tblspace页中。而 tblspace tblspace管理着所有分片的元信息,就像表管理着多行数据一样,所以称之为tblspace tblspace,也就是管理表空间的表空间。

tblspace tblspace的布局如图:

1.png

 

通过设置onconfig文件中的TBLTBLFIRST和TBLTBLNEXT配置参数,可以设定为tblspace tblspace配置第一个和后续扩展数据块的大小。默认值0指示数据库服务器根据初始dbspace块的大小来确定适当的数据块大小。

系统中的每个tblspace都有一个tblspace tblspace页面来描述它。为了简洁起见,我们经常将这种特殊的页面类型称为partition page。

Partition Number (Partnum)

2.png 前12bit: dbspace号

后20bit: 在tblspace tblspace中的逻辑页号

Partnum(另一个4字节的十六进制代码)的目的是将GBase8s 引导到tblspace-tblspace中该表的分区页。partnum的高12bit表示哪个dbspace包含该表。GBase8s使用该12bit的值来定位目标dbspace的tblspace-tblspace。tblspace tblspace中的一个页面描述了目标表。GBase8s使用partnum的低位20 bit 来定位正确的分区页面,其中包含一个逻辑页码,也就是在tblspace  tblspace中的第几个页。

Partition Page布局

整体介绍

3.png

每个分区页面都使用以下5 slot来描述dbspace中一个表的结构、位置和内容:

  • 插槽1包含92字节的通用表信息,包括分区编号。

  • 插槽2包含通过数据库名称、表所有者、表名称和NLS排序规则序列(如果有)标识分区的信息。

  • 插槽3包含表中每个特殊列的描述性条目,表示Blob和VARCHAR类型。

  • slot4: 包含此表中存在的每个索引键的键描述符条目。因此,当访问特定的tblspace时,诸如“非法key描述符:部分太多或太长”之类的错误指的是该表分区页上的插槽4出现问题,而不是索引本身出现问题。由于删除索引需要使用其key描述符,因此错误的key描述符有时可能需要GBase 8s技术支持的干预才能修复。

  • 插槽5包含扩展数据块信息。此插槽中的每个8字节条目包括:

    • tblspace中该数据块的第一页的逻辑页码(4字节)
    • extent到dbspace的页面偏移量(4字节)

该插槽还包括一个条目,其中包括分配的下一个数据块的逻辑页码。

表也可以有第六个插槽,但它不会出现在该表的主分区页上。第六个插槽用于描述由于就地更改表而导致的不同版本的扩展数据块。此插槽显示在tblspace tblspace中的一个单独页面上。

准备数据和元数据

drop database if exists hyqdb;
create database hyqdb with log;
create table t1(c1 int primary key, c2 char(10), c3 varchar(10));
create index i1_t1 on t1(c2, c3);
insert into t1 values(1, '001', 'v001');
insert into t1 values(2, '002', 'v002');

找到partition partition page

使用 oncheck -pt database_name:table_name 可以找到一个表相关的所有partition,每个partition都有自己的信息。

对于t1而言,是不分区的表,同时有主键索引和另一个索引。

因此,有3个partition:

  1. 数据所在的partition;
  2. 主键索引所在的partition;
  3. 另一个索引所在的partition。
[root@node2 ids]# oncheck -pt hyqdb:t1


TBLspace Report for hyqdb:root.t1

    Physical Address               1:11862   //随后会用这个地址
    Creation date                   09/13/2024 11:14:56
    TBLspace Flags                  902        Row Locking
                                             TBLspace contains VARCHARS
                                              TBLspace use 4 bit bit-maps
    Maximum row size                26
    Number of special columns      1
    Number of keys                 0
    Number of extents              1
   Current serial value            1
    Current SERIAL8 value          1
    Current BIGSERIAL value        1
    Current REFID value            1
    Pagesize (k)                   2
    First extent size              8
    Next extent size               8
    Number of pages allocated      8
    Number of pages used           2
    Number of data pages           1
    Number of rows                 2
    Partition partnum              1049253  //随后会用这个值
    Partition lockid                1049253

    Extents
         Logical Page     Physical Page        Size Physical Pages
                   0           1:13496           8          8

                 Index  100_1 fragment partition rootdbs in DBspace rootdbs

    Physical Address               1:11863   // 展示主键索引时使用
    Creation date                   09/13/2024 11:14:56
    TBLspace Flags                  802        Row Locking
                                              TBLspace use 4 bit bit-maps
    Maximum row size                26
    Number of special columns      0
    Number of keys                 1
    Number of extents              1
    Current serial value           1
    Current SERIAL8 value          1
    Current BIGSERIAL value        1
    Current REFID value            1
    Pagesize (k)                   2
    First extent size              4
    Next extent size               4
    Number of pages allocated      4
    Number of pages used           2
    Number of data pages           0
    Number of rows                 0
    Partition partnum               1049254
    Partition lockid                1049253

    Extents
         Logical Page     Physical Page        Size Physical Pages
                   0            1:6421           4          4

                 Index i1_t1 fragment partition rootdbs in DBspace rootdbs

    Physical Address                1:11864  // 展示索引i1_t1 时使用
    Creation date                   09/13/2024 11:14:56
    TBLspace Flags                  802        Row Locking
                                             TBLspace use 4 bit bit-maps
    Maximum row size                26
    Number of special columns      0
    Number of keys                 1
    Number of extents              1
    Current serial value           1
    Current SERIAL8 value          1
    Current BIGSERIAL value        1
    Current REFID value            1
    Pagesize (k)                   2
    First extent size              7
    Next extent size               7
    Number of pages allocated      7
    Number of pages used           2
    Number of data pages           0
    Number of rows                 0
    Partition partnum               1049255
    Partition lockid                1049253

    Extents
         Logical Page     Physical Page        Size Physical Pages
                   0           1:13489           7          7

除了使用库名:表名之外,还可以使用表的partition num:

oncheck –pt 1049253

展示的内容和刚才完全一样。

Slot 1: The Partition Structure

上一节输出的输出内容由分区页的partition结构(槽1)和extent结构(槽5)组成。本节介绍partition结构(在slot1中)的内容。

Partition结构存储在每个分区页的第一个槽中,并保存有关相应表的一般信息。您可以通过运行以下命令来查看任何表的分区结构:

您可能会将存储在分区结构中的一些常规tblspace信息识别为表统计信息,这些信息也存储在系统目录中供优化器使用。事实上,在一些UPDATE STATISTICS操作期间,表的分区页中的一些信息被复制到系统目录中,而其他信息必须通过读取tblspace页本身来收集。尽管随着表的增长和变化,系统目录信息可能会过时,但分区页上的信息应该始终是准确的。然而,优化器代码旨在跨GBase8s服务器进行移植,因此它不知道如何访问分区页面。就优化器而言,唯一可用的表统计信息存储在系统目录中。

随后逐个介绍Partition结构的内容。

Physical Address (decimal:decimal)

这个是这个partition partition page所在的物理位置。

可以通过oncheck –pP 展示这个页的内容。

表数据

[root@node2 ids]# oncheck -pP 1 11862
addr             stamp    chksum nslots flag type         frptr frcnt next     prev
1:11862          5942545  831c    5      2    PARTN        216    1808  0        0
        slot ptr   len   flag
        1    24    136    0
        2    160   24     0
        3    184   12     0
        4    196   0      0
        5    196   20     0
slot    1:
    0: a5  2 10  0   2  9  0  0 1a  0   0  0  1  0  0   0   %...............
   16:  1  0   0  8 30 ae e3 66  1   0  0  0   8  0  0   0   ....0.cf........
   32:  8  0   0  0  8   0  0  0   1  0  0   0  0  0   0  0   ................
   48: ff ff ff ff a5  2 10  0   1  0  0   0  0  0   0  0   ....%...........
   64:  0  0   0  0  0   0  0  0  0  0   0  0  0   0  0  0    ................
   80:  0  0   0  0  0   0  0  0   0  0  0   0  1  0   0  0   ................
   96:  0  0   0  0  1   0  0  0   1  0  0   0  0  0   0  0   ................
  112:  1  0   0  0 89  0   0  0 80 d0 77  0   0  0  0   0   .........Pw.....
  128:  0  0   0  0  0   0  0  0                           ................
slot    2:
    0: 68 79 71 64 62  0 72 6f 6f 74  0 74 31  0 65 6e    hyqdb.root.t1.en
   16: 5f 55 53 2e 38 31 39  0                           _US.819.........
slot    3:
    0:  e  0   0  0  0   0  a  0   0  0  0   5               ................
slot   4:
slot   5:
   0:   0  0  0   0  0  1   0  0 34 b8  0   0  0  8   0  0   ........48......
  16:   0  0  0   0                                        ................

主键索引

[root@node2 ids]# oncheck -pP 1 11863
addr             stamp    chksum nslots flag type         frptr frcnt next     prev
1:11863          5942445  82a1    5      2    PARTN        244    1780  0        0
        slot ptr   len   flag
        1    24    136    0
        2    160   28     0
        3    188   0      0
        4    208   36     0
        5    188   20     0
slot    1:
    0: a6  2 10  0   2  8  0  0 1a  0   0  0  0  0  1   0   &...............
   16:  1  0   0  8 30 ae e3 66  1   0  0  0   4  0  0   0   ....0.cf........
   32:  4  0   0  0  4   0  0  0   2  0  0   0  0  0   0  0   ................
   48: ff ff ff ff a5  2 10  0   1  0  0   0  0  0   0  0   ....%...........
   64:  0  0   0  0  0   0  0  0   0  0  0   0  0  0   0  0   ................
   80:  0  0   0  0  0   0  0  0  0  0   0  0  1   0  0  0    ................
   96:  0  0   0  0  1   0  0  0   1  0  0   0  0  0   0  0   ................
  112:  1  0   0  0 89  0   0  0 74 d2 77  0   0  0  0   0   ........tRw.....
  128:  0  0   0  0  0   0  0  0                           ................
slot    2:
    0: 68 79 71 64 62  0 72 6f 6f 74  0 20 31 30 30 5f   hyqdb.root. 100_
   16: 31  0 65 6e 5f 55 53 2e 38 31 39  0               1.en_US.819.....
slot    3:
slot   4:
   0: 98   2  4  1   1  0  4   0  0  0   0  0 a5  2 10   0   ............%...
  16:   1  0  0   0  0  0   0  0  4   0  2  0   0  0  0   0   ................
  32:   0  0  0   0                                        ................
slot   5:
   0:   0  0  0   0  0  1   0  0 19 15  0   0  0  4   0  0   ................
  16:   0  0  0   0                                        ................

索引i1_t1

[root@node2 ids]# oncheck -pP 1 11864
addr             stamp    chksum nslots flag type         frptr frcnt next     prev
1:11864          5942518  82f5    5      2    PARTN        260    1764  0        0
        slot ptr   len   flag
        1    24    136    0
        2    160   28     0
        3    188   0      0
        4    208   52     0
        5    188   20     0
slot    1:
    0: a7  2 10  0   2  8  0  0 1a  0   0  0  0  0  1   0   '...............
   16:  1  0   0  8 30 ae e3 66  1   0  0  0   7  0  0   0   ....0.cf........
   32:  7  0   0  0  7   0  0  0   2  0  0   0  0  0   0  0   ................
   48: ff ff ff ff a5  2 10  0   1  0  0   0  0  0   0  0   ....%...........
   64:  0  0   0  0  0   0  0  0   0  0  0   0  0  0   0  0   ................
   80:  0  0   0  0  0   0  0  0   0  0  0   0  1  0   0  0   ................
   96:  0  0   0  0  1   0  0  0   1  0  0   0  0  0   0  0   ................
  112:  1  0   0  0 89  0   0  0 50  0 78   0  0  0   0  0   ........P.x.....
  128:  0  0   0  0  0   0  0  0                           ................
slot    2:
    0: 68 79 71 64 62  0 72 6f 6f 74  0 69 31 5f 74 31   hyqdb.root.i1_t1
   16:  0 65 6e 5f 55 53 2e 38 31 39  0 49               .en_US.819.I....
slot    3:
slot    4:
    0:  1  2   0  1  2  0 16  0   0  0  0  0 a5  2 10  0    ............%...
   16:  1  0   0  0  4   0  0  0   a  0  0   0  0  0   0  0   ................
   32:  0  0   0  0  e   0  0  0   c  0  5   0  0  0   0  0   ................
   48:  0  0   0  0                                        ................
slot    5:
    0:  0   0  0  0   0  1  0  0 34 b1  0  0   0  7  0   0   ........41......
  16:   0  0  0   0                                        ................

Creation date

这是创建此表的日期和时间。

TBLSpace Flags (decimal)

这些标志的操作方式与页面标志、dbspace标志和块标志相同,在逻辑OR的帮助下凝结成一个整数。各个tblspace标志具有以下值和含义:

0x0001 页级锁定
0x0002 行级锁定
0x0004 Tblspace是Bundlespace(在线安全)
0x0008 标记为DDR复制的分区
0x0010 分区已删除(仅共享内存)
0x0020 系统定义的临时表
0x0040 用户定义的临时表
0x0080 用于排序的Tblspace
0x0100 包含VARCHAR0x0200 包含BLOB空间BLOB0x0400 包含驻留在分区中的BLOB0x0800 需要4位位图
0x1000 包含optical BLOB0x2000 系统运行所需的分区-不要删除
0x4000 临时表用于特殊功能-不要更新
0x8000 Partition is being appended to

Maximum row size (decimal)

对于具有固定行长的表,该值仅是以字节为单位的行大小。只有当tblspace包含VARCHAR列时,才需要最大行大小的概念。回想一下,VARCHAR列定义了最小和最大大小,以字符为单位,相当于字节。VARCHAR列的最大大小,加上2个字节的开销(用于存储VARCHAR数据的实际大小),再加上模式中其他列的大小,就得到了这个最大行大小。

Number of special columns (decimal)

Blob和VARCHAR列被认为是特殊的,仅适用于GBase8s的列类型。

Number of keys (decimal)

为表定义的索引总数。复合索引可以由几列组成,但仍然只有一个索引键。

Number of extents (decimal)

分配给表的单独数据块的数量。GBase8s管理员的扩展区连接和加倍,以及良好的tblspace管理,往往会使这个数字保持在较低的水平。随着tblspace的扩展数据块数量的增加,不仅数据会不可预测地分散(顺序读取的性能问题),而且分区页上的扩展数据槽也会增加。数据块插槽在空间用完之前只能包含这么多条目。由于分区页上的其他插槽(如插槽4)也可以动态增长,因此无法以任何精度发布最大数量的tblspace扩展数据块。经验证据表明,2K页面上的最大区数约为190。

Current serial value (decimal)

tblspace只能包含一个serial类型的列。如果存在,则这是用于插入的下一个值。如果表中没有serial列,则此值保持为1。

serial相关类型包含:serial, serial8, bigserial。

First extent size (decimal)

这是配置的EXTENT SIZE,以GBase8s的页面为单位。这里的单位可能有点令人困惑,因为通过SQL,可以指定以千字节为单位的EXTENT SIZE。

默认的EXTENT SIZE为8页,与页面大小无关。最小范围大小为4页。

Next extent size (decimal)

这是配置的NEXT SIZE,也是以GBase8s 页面为单位。请注意,在SQL级别,一个以千字节为单位指定NEXT SIZE,因此与First区段大小一样,这里的值最初是SQL中NEXT SIZE子句使用的数字的一半或四分之一。

由于数据块大小加倍,下一个数据块大小可能会随着时间的推移而增加。除非Table被故意改变,否则尺寸永远不会减小。下一个数据块大小的默认值为8页。最小范围大小为4页。

Number of pages allocated (decimal)

这是分配给tblspace的扩展数据块中包含的总页数,无论是否使用。

Number of pages used (decimal)

这是tblspace中使用过的最大页数。

Number of data pages (decimal)

这是tblspace中当前正在使用的数据页数。当从数据页中删除所有行时,该页将被释放以在tblspace中重新使用,分区结构的数据页数量元素将减少。

Number of rows (decimal)

这表示tblspace中的行数。

Partition partnum (decimal)

这表示tblspace的分区号。

Partition lockid

过去,当您锁定表时,您实际上是锁定了一个分区号。只要数据库表和分区号之间存在一一对应关系,这就可以工作。但是分区表允许许多分区与一个数据库表相关联。GBase8s 使用这个值lockid来表示所有表片段,而不是将每个表片段的partnum与单个表锁相关联。

Slot 2: partition name

slot    2:
     0: 68 79 71 64 62  0 72 6f 6f 74  0 74 31  0 65 6e    hyqdb.root.t1.en
    16: 5f 55 53 2e 38 31 39  0                           _US.819.........

slot 2中的 PT_NAME是以 '\0'分割的四个信息:

dbsname,

owner,

tabname,

nlscollname.

slot 3: column descriptors

slot    3:
     0e  0   0  0  0   0  a  0   0  0  0   5               ................

逐个字节介绍如下:

e 0  0  0:e是16进制的14,这是小端,代表这一列的行内偏移是14字节。

0  0:标志位

a  0:代表这个varchar字段最大是10个字节

0  0:代表这个varchar字段最小是0个字节

0:标志位

5:基础数据类型(5代表是varchar)

slot 4: key descriptors

主键对应的partition partition page:

slot   4:
    0: 98   2  4  1   1  0  4   0  0  0   0  0 a5  2 10   0   ............%...
   161  0  0   0  0  0   0  0  4   0  2  0   0  0  0   0   ................
   320  0  0   0                                        ................

i1_t1对应的partition partition page:

slot   4:
    01  2  0   1  2  0 16   0  0  0   0  0 a5  2 10   0   ............%...
   161  0  0   0  4  0   0  0  a   0  0  0   0  0  0   0   ................
   320  0  0   0  e  0   0  0  c   0  5  0   0  0  0   0   ................
   480  0  0   0                                       ................

Slot 5: The Extent Slot

使用oncheck -pt hyqdb:t1,展示的内容,针对每个partition的最后部分,就是extent相关信息,以表数据的为例:

Extents
          Logical Page     Physical Page        Size Physical Pages
                    0           1:13496           8          8

这个代表了数据的物理起始位置,及页数。

根据物理地址展示数据部分(注意:需要跳过bitmap页,因此使用1 13497)

[root@node2 ids]# oncheck -pP 1 13497
addr             stamp    chksum nslots flag type         frptr frcnt next     prev
1:13497          5942555  99f9    2      1    DATA         64     1972  0        0
       slot ptr   len    flag
       1     24    20    0
       2     44    20    0
slot   1:
   0:   0  0  0  1 30 30 31 20 20 20 20 20 20 20  0  4    ....001       ..
  16: 76 30 30 31                                       v001............
slot   2:
   0:   0  0  0  2 30 30 32 20 20 20 20 20 20 20  0  4    ....002       ..
  16: 76 30 30 32                                        v002............

Slot 6: Page Versioning   In-Place-Alter时需要

在发生In-Place-Alter的时候,这个slot位会记录数据基于哪个版本的元数据。

所谓In-Place-Alter,是说修改表,但是表里的数据不立刻更新,未来发生变化的时候再更新。此时,数据就会有不同的元数据版本。这个slot就是记录这个版本的。

通过对GBase8s数据库中Partition Page的解析,我们不仅了解了分区页的定义、作用、结构和组成,还掌握了分区页的创建、管理和实际应用方法。分区页作为GBase8s数据库中的关键数据存储结构,对于提高数据库的存储效率、优化查询性能以及简化数据维护具有重要意义。在未来的数据库技术发展中,分区页的应用和优化仍将是研究的重点之一。希望本文能够为数据库从业者和爱好者提供有价值的参考和启发,让我们共同探索数据库技术的无限可能。

原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。