【openGauss5.0.0版本】分区表新增的特性 - 示例

openGauss 5.0.0 版本中,List和range分区新增功能如下:

  1. List分区键最大数由1扩展为16列:

    create table tp16 (
    c1 int,
    c2 int,
    c3 int,
    c4 int,
    c5 int,
    c6 int,
    c7 int,
    c8 int,
    c9 int,
    c10 int,
    c11 int,
    c12 int,
    c13 int,
    c14 int,
    c15 int,
    c16 int
    )
    partition by list (
    c1,
    c2,
    c3,
    c4,
    c5,
    c6,
    c7,
    c8,
    c9,
    c10,
    c11,
    c12,
    c13,
    c14,
    c15,
    c16
    )
    (
    partition p1 values ((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p2 values ((2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p3 values ((3,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p4 values ((4,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p5 values ((5,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p6 values ((6,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p7 values ((7,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p8 values ((8,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p9 values ((9,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p10 values ((10,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p11 values ((11,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p12 values ((12,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p13 values ((13,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p14 values ((14,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p15 values ((15,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)),
    partition p16 values ((16,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16))
    );
    

    查看表结构:

    testdb001=# \d+ tp16
                             Table "public.tp16"
     Column |  Type   | Modifiers | Storage | Stats target | Description
    --------+---------+-----------+---------+--------------+-------------
     c1     | integer |           | plain   |              |
     c2     | integer |           | plain   |              |
     c3     | integer |           | plain   |              |
     c4     | integer |           | plain   |              |
     c5     | integer |           | plain   |              |
     c6     | integer |           | plain   |              |
     c7     | integer |           | plain   |              |
     c8     | integer |           | plain   |              |
     c9     | integer |           | plain   |              |
     c10    | integer |           | plain   |              |
     c11    | integer |           | plain   |              |
     c12    | integer |           | plain   |              |
     c13    | integer |           | plain   |              |
     c14    | integer |           | plain   |              |
     c15    | integer |           | plain   |              |
     c16    | integer |           | plain   |              |
    Partition By LIST(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16)
    Number of partitions: 16 (View pg_partition to check each partition range.)
    Has OIDs: no
    Options: orientation=row, compression=no
    

    查看分区信息SQL语句:

    select relname,parttype,parentid,relfilenode,partkey,interval,boundaries,partitionno from pg_partition;
    

    分区信息查看结果:

    relname | parttype | parentid | relfilenode |                partkey                 | interval |                  boundaries                   | partitionno
    
    ---------+----------+----------+-------------+----------------------------------------+----------+-----------------------------------------------+------------
    -
     tp16    | r        |    49349 |           0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |          |                                               |         -16
     p1      | p        |    49349 |       49353 |                                        |          | {"{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           1
     p2      | p        |    49349 |       49354 |                                        |          | {"{2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           2
     p3      | p        |    49349 |       49355 |                                        |          | {"{3,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           3
     p4      | p        |    49349 |       49356 |                                        |          | {"{4,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           4
     p5      | p        |    49349 |       49357 |                                        |          | {"{5,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           5
     p6      | p        |    49349 |       49358 |                                        |          | {"{6,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           6
     p7      | p        |    49349 |       49359 |                                        |          | {"{7,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           7
     p8      | p        |    49349 |       49360 |                                        |          | {"{8,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           8
     p9      | p        |    49349 |       49361 |                                        |          | {"{9,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"}  |           9
     p10     | p        |    49349 |       49362 |                                        |          | {"{10,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"} |          10
     p11     | p        |    49349 |       49363 |                                        |          | {"{11,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"} |          11
     p12     | p        |    49349 |       49364 |                                        |          | {"{12,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"} |          12
     p13     | p        |    49349 |       49365 |                                        |          | {"{13,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"} |          13
     p14     | p        |    49349 |       49366 |                                        |          | {"{14,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"} |          14
     p15     | p        |    49349 |       49367 |                                        |          | {"{15,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"} |          15
     p16     | p        |    49349 |       49368 |                                        |          | {"{16,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}"} |          16
    (17 rows)
    
    
  2. RANGE 分区键最大数由4扩展为16列
    LIST分区和RANGE分区都是一种将数据根据特定条件进行分区的技术,但是它们之间存在一些关键区别,主要区别在于存储值、分区键和数据分布。在选择使用哪种分区技术时,需要根据具体的需求和场景进行权衡和选择。
    间隔分区是在范围分区的基础上,增加了间隔值“PARTITION BY RANGE (partition_key)”的定义。
    从 openGauss 5.0.0 开始, RANGE 分区键最大数由4扩展为16列,列举如下:
    创建表SQL语句:

    CREATE TABLE tp_r16 (
    c1 int,
    c2 int,
    c3 int,
    c4 int,
    c5 int,
    c6 int,
    c7 int,
    c8 int,
    c9 int,
    c10 int,
    c11 int,
    c12 int,
    c13 int,
    c14 int,
    c15 int,
    c16 int,
    c17 int,
    PRIMARY KEY (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16)
    )
    PARTITION BY RANGE (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16) (
      PARTITION p0 VALUES LESS THAN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
      PARTITION p1 VALUES LESS THAN (2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
      PARTITION p2 VALUES LESS THAN (3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
      PARTITION p3 VALUES LESS THAN (MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE)
    );
    

    查看表结构:

    testdb001=# \d+ tp_r16
                            Table "public.tp_r16"
     Column |  Type   | Modifiers | Storage | Stats target | Description
    --------+---------+-----------+---------+--------------+-------------
     c1     | integer | not null  | plain   |              |
     c2     | integer | not null  | plain   |              |
     c3     | integer | not null  | plain   |              |
     c4     | integer | not null  | plain   |              |
     c5     | integer | not null  | plain   |              |
     c6     | integer | not null  | plain   |              |
     c7     | integer | not null  | plain   |              |
     c8     | integer | not null  | plain   |              |
     c9     | integer | not null  | plain   |              |
     c10    | integer | not null  | plain   |              |
     c11    | integer | not null  | plain   |              |
     c12    | integer | not null  | plain   |              |
     c13    | integer | not null  | plain   |              |
     c14    | integer | not null  | plain   |              |
     c15    | integer | not null  | plain   |              |
     c16    | integer | not null  | plain   |              |
     c17    | integer |           | plain   |              |
    Indexes:
        "tp_r16_pkey" PRIMARY KEY, btree (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16) LOCAL TABLESPACE pg_default
    Partition By RANGE(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16)
    Number of partitions: 4 (View pg_partition to check each partition range.)
    Has OIDs: no
    Options: orientation=row, compression=no
    

    查看分区情况:

    testdb001=# select relname,parttype,parentid,relfilenode,partkey,interval,boundaries,partitionno from pg_partition where parentid=49369;
     relname | parttype | parentid | relfilenode |                partkey                 | interval |                                    boundaries
                           | partitionno
    ---------+----------+----------+-------------+----------------------------------------+----------+------------------------------------------------------------
    -----------------------+-------------
     tp_r16  | r        |    49369 |           0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |          |
                           |          -4
     p0      | p        |    49369 |       49373 |                                        |          | {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
                           |           1
     p1      | p        |    49369 |       49374 |                                        |          | {2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
                           |           2
     p2      | p        |    49369 |       49375 |                                        |          | {3,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
                           |           3
     p3      | p        |    49369 |       49376 |                                        |          | {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NUL
    L,NULL,NULL,NULL,NULL} |           4
    (5 rows)
    

    如果超过17列就会报错:

    testdb001=# CREATE TABLE tp_r17 (
    testdb001(# c1 int,
    testdb001(# c2 int,
    testdb001(# c3 int,
    testdb001(# c4 int,
    testdb001(# c5 int,
    testdb001(# c6 int,
    testdb001(# c7 int,
    testdb001(# c8 int,
    testdb001(# c9 int,
    testdb001(# c10 int,
    testdb001(# c11 int,
    testdb001(# c12 int,
    testdb001(# c13 int,
    testdb001(# c14 int,
    testdb001(# c15 int,
    testdb001(# c16 int,
    testdb001(# c17 int,
    testdb001(# c18 int,
    testdb001(# PRIMARY KEY (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17)
    testdb001(# )
    testdb001-# PARTITION BY RANGE (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17) (
    testdb001(#   PARTITION p0 VALUES LESS THAN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),
    testdb001(#   PARTITION p1 VALUES LESS THAN (2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),
    testdb001(#   PARTITION p2 VALUES LESS THAN (3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),
    testdb001(#   PARTITION p3 VALUES LESS THAN (MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE, MAXVALUE)
    testdb001(# );
    ERROR:  too many partition keys for partitioned table
    HINT:  Partittion key columns can not be more than 16
    testdb001=#
    
  3. 基于范围分区的自动扩展分区
    openGauss 中提供了一种自动扩展分区的分区表建表语法,可以自定义按日期进行分区,而无需预定义创建表分区定义,系统可以自行创建系统分区,并命名为 sys_p1, sys_p2, …。注意:sys_p1等是超过范围规定的部分数据将会存储到以sys_pxxx的方式命名的分区中。
    建表SQL:

    create table tp_ri (id int, c date)
    PARTITION BY RANGE (c)
    INTERVAL ('1 MONTH')
    (
    PARTITION START VALUES LESS THAN('2023-01-01'),
    PARTITION LATER VALUES LESS THAN('2024-12-31')
    );
    

    查看表结构:

    openGauss=# \d+ tp_ri
                                        Table "public.tp_ri"
     Column |              Type              | Modifiers | Storage | Stats target | Description
    --------+--------------------------------+-----------+---------+--------------+-------------
     id     | integer                        |           | plain   |              |
     c      | timestamp(0) without time zone |           | plain   |              |
    Partition By RANGE(c) INTERVAL('1 MONTH')
    Number of partitions: 3 (View pg_partition to check each partition range.)
    Has OIDs: no
    Options: orientation=row, compression=no
    

    查看分区信息:

    openGauss=# select relname,parttype,parentid,relfilenode,partkey,interval,boundaries,partitionno from pg_partition where parentid=49342;
     relname | parttype | parentid | relfilenode | partkey |  interval   |       boundaries        | partitionno
    ---------+----------+----------+-------------+---------+-------------+-------------------------+-------------
     tp_ri   | r        |    49342 |           0 | 2       | {"1 MONTH"} |                         |          -3
     start   | p        |    49342 |       49346 |         |             | {2023-01-01}            |           1
     later   | p        |    49342 |       49347 |         |             | {2024-12-31}            |           2
     sys_p1  | p        |    49342 |       49348 |         |             | {"2025-04-30 00:00:00"} |           3
    (4 rows)
    

    插入测试数据:

    insert into tp_ri values (1,'2023-01-01'),(2,'2023-02-01'),(3,'2023-03-01'),(4,'2024-01-01'),(5,'2024-04-01'),(13,'2025-04-01');
    

    再次查看该分区表的信息:

    openGauss=# select relname,parttype,parentid,relfilenode,partkey,interval,boundaries,partitionno from pg_partition where parentid=49342;
     relname | parttype | parentid | relfilenode | partkey |  interval   |       boundaries        | partitionno
    ---------+----------+----------+-------------+---------+-------------+-------------------------+-------------
     tp_ri   | r        |    49342 |           0 | 2       | {"1 MONTH"} |                         |          -3
     start   | p        |    49342 |       49346 |         |             | {2023-01-01}            |           1
     later   | p        |    49342 |       49347 |         |             | {2024-12-31}            |           2
     sys_p1  | p        |    49342 |       49348 |         |             | {"2025-04-30 00:00:00"} |           3
    (4 rows)
    

    多出了分区名称sys_p1的分区,自动扩展出来的分区,且当前该分区存储的数据为“2025-04-01”。

你可能感兴趣的:(openGauss,openGauss)