百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

PostgreSQL技术内幕17:PG分区表(pg数据库表分区)

liuian 2025-07-06 14:04 58 浏览

0.简介

本文主要介绍PG中分区表的概念,产生分区表技术的原因,使用方式和其内部实现原理,旨在能对PG分区表技术有一个系统的说明。

1.概念介绍

分区表是数据库用于管理大量数据的一种技术,它允许将一个大表分割成多个小表,这些小表在物理上是独立的,但在逻辑上作为一个整体被查询和更新。分区表的主要优势在于提高查询性能,特别是当查询集中在少数几个分区时。此外,分区表还可以简化数据的批量删除和加载,以及将不常用的数据迁移到成本较低的存储介质上实现冷热分离。

1)主表/父表/Master Table:该表是创建子表的模板。它是一个正常的普通表,但正常情况下它并不储存任何数据。

2)子表/分区表/Child Table/Partition Table:这些表继承并属于一个主表。子表中存储所有的数据。主表与分区表属于一对多的关系,也就是说,一个主表包含多个分区表,而一个分区表只从属于一个主表

2.分区表技术产生的背景

在使用数据库过程中,随着时间的推移,每张表数据量会不断增加,造成查询速度越来越慢,在分区表之前有很多查询的技术去优化它,比如添加特殊的索引,将磁盘分区(把日志文件放到单独的磁盘分区),调整参数等等。这些优化技术都能对查询性能做出或多或少的提升,但其并没有对于表特点以及局部性的原理进行合理应用,因为对于很多应用来说,许多历史数据对于查询可能并没有太多用处,或者是某一列是特定值时是更为关系的数据,如果能够将不常用数据进行隐藏,就能大大提高查询速度,分区表就是为了解决这个问题而产生的。比如可以按照时间作为分区键进行分区将新老数据分离。

3.分区类型及使用方式

PG 10以后支持三种分区,以下都使用主流的使用方式声明式分区(还有表继承)进行说明:

1)范围(Range)分区

CREATE TABLE students (grade INTEGER) PARTITION BY RANGE(grade);
CREATE TABLE stu_fail PARTITION OF students FOR VALUES FROM (MINVALUE) TO (60);
CREATE TABLE stu_pass PARTITION OF students FOR VALUES FROM (60) TO (MAXVALUE);
 \d+  students
                                 Table "public.students"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
 grade  | integer |           |          |         | plain   |              |
Partition key: RANGE (grade)
Partitions: stu_fail FOR VALUES FROM (MINVALUE) TO (60),
            stu_pass FOR VALUES FROM (60) TO (MAXVALUE)

\d+ stu_fail
                                 Table "public.stu_fail"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
 grade  | integer |           |          |         | plain   |              |
Partition of: students FOR VALUES FROM (MINVALUE) TO (60)
Partition constraint: ((grade IS NOT NULL) AND (grade < 60))

可以看出,其中最大值是小于关系,不是小于等于关系。

2)列表(List)分区

列表分区明确指定根据某字段的某个具体值进行分区,默认分区(可选值)保存不属于任何指定分区的列表值。

CREATE TABLE students (status character varying(30)) PARTITION BY LIST(status);
CREATE TABLE stu_active PARTITION OF students FOR VALUES IN ('ACTIVE');
CREATE TABLE stu_exp PARTITION OF students FOR VALUES IN ('EXPIRED');
CREATE TABLE stu_others PARTITION OF students DEFAULT;

\d+  students
                                         Table "public.students"
 Column |         Type          | Collation | Nullable | Default | Storage  | Stats target | Description
--------+-----------------------+-----------+----------+---------+----------+--------------+-------------
 status | character varying(30) |           |          |         | extended |              |
Partition key: LIST (status)
Partitions: stu_active FOR VALUES IN ('ACTIVE'),
            stu_exp FOR VALUES IN ('EXPIRED'),
            stu_others DEFAULT

\d+  stu_others;
                                        Table "public.stu_others"
 Column |         Type          | Collation | Nullable | Default | Storage  | Stats target | Description
--------+-----------------------+-----------+----------+---------+----------+--------------+-------------
 status | character varying(30) |           |          |         | extended |              |
Partition of: students DEFAULT
Partition constraint: (NOT ((status IS NOT NULL) AND ((status)::text = ANY (ARRAY['ACTIVE'::character varying(30), 'EXPIRED'::character varying(30)]))))

3)哈希(Hash)分区

通过对每个分区使用取模和余数来创建hash分区,modulus指定了对N取模,而remainder指定了除完后的余数。

CREATE TABLE students (id INTEGER) PARTITION BY HASH(id);
CREATE TABLE stu_part1 PARTITION OF students FOR VALUES WITH (modulus 3, remainder 0);
CREATE TABLE stu_part2 PARTITION OF students FOR VALUES WITH (modulus 3, remainder 1);
CREATE TABLE stu_part3 PARTITION OF students FOR VALUES WITH (modulus 3, remainder 2);

\d+ students;
                                 Table "public.students"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
 id     | integer |           |          |         | plain   |              |
Partition key: HASH (id)
Partitions: stu_part1 FOR VALUES WITH (modulus 3, remainder 0),
            stu_part2 FOR VALUES WITH (modulus 3, remainder 1),
            stu_part3 FOR VALUES WITH (modulus 3, remainder 2)

\d+ stu_part1;
                                 Table "public.stu_part1"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
 id     | integer |           |          |         | plain   |              |
Partition of: students FOR VALUES WITH (modulus 3, remainder 0)
Partition constraint: satisfies_hash_partition('16439'::oid, 3, 0, id)

PG分区还支持创建子分区:LIST-LIST,LIST-RANGE,LIST-HASH,RANGE-RANGE,RANGE-LIST,RANGE-HASH,HASH-HASH,HASH-LIST和HASH-RANGE;以及和普通表之间互相转换,DETACH PARTITION可以将分区表转换为普通表,而attach partition可以将普通表附加到分区表上。

4.实现原理

4.1 分区表创建

分区表创建相对简单,对PG来说实际是一张逻辑表对应多张物理表,下面简单看创建时其分区表相关的调用流程。

--> transformPartitionBound  
                    --> RelationGetPartitionKey
                    --> get_partition_strategy
                    --> transformPartitionBoundValue
                    --> transformPartitionRangeBounds
                        --> validateInfiniteBounds
                --> check_new_partition_bound
                --> StorePartitionBound // Update pg_class tuple of rel to store the partition bound and set relispartition to true
                --> StoreCatalogInheritance // 向系统表pg_inherits插入信息
                // 处理stmt->partspec
                --> transformPartitionSpec
                --> ComputePartitionAttrs
                --> StorePartitionKey // 向pg_partitioned_table中插入分区键等信息

4.2 分区表查询

分区表查询是要根据条件查询一定数量的子表然后进行返回,其主要分为三步:

1)识别分区表并找到所有的分区子表

/*
 * expand_inherited_tables
 *    Expand each rangetable entry that represents an inheritance set
 *    into an "append relation".  At the conclusion of this process,
 *    the "inh" flag is set in all and only those RTEs that are append
 *    relation parents.
 */
void
expand_inherited_tables(PlannerInfo *root)
{
  Index    nrtes;
  Index    rti;
  ListCell   *rl;

  /*
   * expand_inherited_rtentry may add RTEs to parse->rtable. The function is
   * expected to recursively handle any RTEs that it creates with inh=true.
   * So just scan as far as the original end of the rtable list.
   */
  nrtes = list_length(root->parse->rtable);
  rl = list_head(root->parse->rtable);
  for (rti = 1; rti <= nrtes; rti++)
  {
    RangeTblEntry *rte = (RangeTblEntry *) lfirst(rl);

    expand_inherited_rtentry(root, rte, rti);
    rl = lnext(rl);
  }
}

2)根据约束条件识别需要查询的分区,也就是分区裁剪,只读取需要的分区;

prune_append_rel_partitions
 *    Process rel's baserestrictinfo and make use of quals which can be
 *    evaluated during query planning in order to determine the minimum set
 *    of partitions which must be scanned to satisfy these quals.  Returns
 *    the matching partitions in the form of a Relids set containing the
 *    partitions' RT indexes.
 *
 * Callers must ensure that 'rel' is a partitioned table.
 */
Relids
prune_append_rel_partitions(RelOptInfo *rel)
{
  Relids    result;
  List     *clauses = rel->baserestrictinfo;
  List     *pruning_steps;
  GeneratePruningStepsContext gcontext;
  PartitionPruneContext context;
  Bitmapset  *partindexes;
  int      i;

  Assert(clauses != NIL);
  Assert(rel->part_scheme != NULL);

  /* If there are no partitions, return the empty set */
  if (rel->nparts == 0)
    return NULL;

  /*
   * Process clauses to extract pruning steps that are usable at plan time.
   * If the clauses are found to be contradictory, we can return the empty
   * set.
   */
  gen_partprune_steps(rel, clauses, PARTTARGET_PLANNER,
            &gcontext);
  if (gcontext.contradictory)
    return NULL;
  pruning_steps = gcontext.steps;

  /* Set up PartitionPruneContext */
  context.strategy = rel->part_scheme->strategy;
  context.partnatts = rel->part_scheme->partnatts;
  context.nparts = rel->nparts;
  context.boundinfo = rel->boundinfo;
  context.partcollation = rel->part_scheme->partcollation;
  context.partsupfunc = rel->part_scheme->partsupfunc;
  context.stepcmpfuncs = (FmgrInfo *) palloc0(sizeof(FmgrInfo) *
                        context.partnatts *
                        list_length(pruning_steps));
  context.ppccontext = CurrentMemoryContext;

  /* These are not valid when being called from the planner */
  context.partrel = NULL;
  context.planstate = NULL;
  context.exprstates = NULL;

  /* Actual pruning happens here. */
  partindexes = get_matching_partitions(&context, pruning_steps);

  /* Add selected partitions' RT indexes to result. */
  i = -1;
  result = NULL;
  while ((i = bms_next_member(partindexes, i)) >= 0)
    result = bms_add_member(result, rel->part_rels[i]->relid);

  return result;
}

3)对结果集执行APPEND,作为最终结果输出,这和其他表append操作一致,使用ExecInitAppend和ExecAppend函数。

/* ----------------------------------------------------------------
 *     ExecAppend
 *
 *    Handles iteration over multiple subplans.
 * ----------------------------------------------------------------
 */
static TupleTableSlot *
ExecAppend(PlanState *pstate)
{
  AppendState *node = castNode(AppendState, pstate);

  if (node->as_whichplan < 0)
  {
    /*
     * If no subplan has been chosen, we must choose one before
     * proceeding.
     */
    if (node->as_whichplan == INVALID_SUBPLAN_INDEX &&
      !node->choose_next_subplan(node))
      return ExecClearTuple(node->ps.ps_ResultTupleSlot);

    /* Nothing to do if there are no matching subplans */
    else if (node->as_whichplan == NO_MATCHING_SUBPLANS)
      return ExecClearTuple(node->ps.ps_ResultTupleSlot);
  }

  for (;;)
  {
    PlanState  *subnode;
    TupleTableSlot *result;

    CHECK_FOR_INTERRUPTS();

    /*
     * figure out which subplan we are currently processing
     */
    Assert(node->as_whichplan >= 0 && node->as_whichplan < node->as_nplans);
    subnode = node->appendplans[node->as_whichplan];

    /*
     * get a tuple from the subplan
     */
    result = ExecProcNode(subnode);

    if (!TupIsNull(result))
    {
      /*
       * If the subplan gave us something then return it as-is. We do
       * NOT make use of the result slot that was set up in
       * ExecInitAppend; there's no need for it.
       */
      return result;
    }

    /* choose new subplan; if none, we're done */
    if (!node->choose_next_subplan(node))
      return ExecClearTuple(node->ps.ps_ResultTupleSlot);
  }
}

4.3 分区表写入

分区表写入分为两个阶段,一个是查找到要写入的分区,然后就是正常去做写入,下面来看查找分区的函数。

/*
 * ExecPrepareTupleRouting --- prepare for routing one tuple
 *
 * Determine the partition in which the tuple in slot is to be inserted,
 * and modify mtstate and estate to prepare for it.
 *
 * Caller must revert the estate changes after executing the insertion!
 * In mtstate, transition capture changes may also need to be reverted.
 *
 * Returns a slot holding the tuple of the partition rowtype.
 */
static TupleTableSlot *
ExecPrepareTupleRouting(ModifyTableState *mtstate,
            EState *estate,
            PartitionTupleRouting *proute,
            ResultRelInfo *targetRelInfo,
            TupleTableSlot *slot)
{
  ModifyTable *node;
  int      partidx;
  ResultRelInfo *partrel;
  HeapTuple  tuple;

  /*
   * Determine the target partition.  If ExecFindPartition does not find a
   * partition after all, it doesn't return here; otherwise, the returned
   * value is to be used as an index into the arrays for the ResultRelInfo
   * and TupleConversionMap for the partition.
   */
  partidx = ExecFindPartition(targetRelInfo,
                proute->partition_dispatch_info,
                slot,
                estate);
  Assert(partidx >= 0 && partidx < proute->num_partitions);

  /*
   * Get the ResultRelInfo corresponding to the selected partition; if not
   * yet there, initialize it.
   */
  partrel = proute->partitions[partidx];
  if (partrel == NULL)
    partrel = ExecInitPartitionInfo(mtstate, targetRelInfo,
                    proute, estate,
                    partidx);

  /*
   * Check whether the partition is routable if we didn't yet
   *
   * Note: an UPDATE of a partition key invokes an INSERT that moves the
   * tuple to a new partition.  This check would be applied to a subplan
   * partition of such an UPDATE that is chosen as the partition to route
   * the tuple to.  The reason we do this check here rather than in
   * ExecSetupPartitionTupleRouting is to avoid aborting such an UPDATE
   * unnecessarily due to non-routable subplan partitions that may not be
   * chosen for update tuple movement after all.
   */
  if (!partrel->ri_PartitionReadyForRouting)
  {
    /* Verify the partition is a valid target for INSERT. */
    CheckValidResultRel(partrel, CMD_INSERT);

    /* Set up information needed for routing tuples to the partition. */
    ExecInitRoutingInfo(mtstate, estate, proute, partrel, partidx);
  }

  /*
   * Make it look like we are inserting into the partition.
   */
  estate->es_result_relation_info = partrel;

  /* Get the heap tuple out of the given slot. */
  tuple = ExecMaterializeSlot(slot);

  /*
   * If we're capturing transition tuples, we might need to convert from the
   * partition rowtype to parent rowtype.
   */
  if (mtstate->mt_transition_capture != NULL)
  {
    if (partrel->ri_TrigDesc &&
      partrel->ri_TrigDesc->trig_insert_before_row)
    {
      /*
       * If there are any BEFORE triggers on the partition, we'll have
       * to be ready to convert their result back to tuplestore format.
       */
      mtstate->mt_transition_capture->tcs_original_insert_tuple = NULL;
      mtstate->mt_transition_capture->tcs_map =
        TupConvMapForLeaf(proute, targetRelInfo, partidx);
    }
    else
    {
      /*
       * Otherwise, just remember the original unconverted tuple, to
       * avoid a needless round trip conversion.
       */
      mtstate->mt_transition_capture->tcs_original_insert_tuple = tuple;
      mtstate->mt_transition_capture->tcs_map = NULL;
    }
  }
  if (mtstate->mt_oc_transition_capture != NULL)
  {
    mtstate->mt_oc_transition_capture->tcs_map =
      TupConvMapForLeaf(proute, targetRelInfo, partidx);
  }

  /*
   * Convert the tuple, if necessary.
   */
  ConvertPartitionTupleSlot(proute->parent_child_tupconv_maps[partidx],
                tuple,
                proute->partition_tuple_slot,
                &slot);

  /* Initialize information needed to handle ON CONFLICT DO UPDATE. */
  Assert(mtstate != NULL);
  node = (ModifyTable *) mtstate->ps.plan;
  if (node->onConflictAction == ONCONFLICT_UPDATE)
  {
    Assert(mtstate->mt_existing != NULL);
    ExecSetSlotDescriptor(mtstate->mt_existing,
                RelationGetDescr(partrel->ri_RelationDesc));
    Assert(mtstate->mt_conflproj != NULL);
    ExecSetSlotDescriptor(mtstate->mt_conflproj,
                partrel->ri_onConflict->oc_ProjTupdesc);
  }

  return slot;
}

4.4 分区表删除

分区表的删除即为先删除其分区,然后整体删除。

相关推荐

驱动网卡(怎么从新驱动网卡)
驱动网卡(怎么从新驱动网卡)

网卡一般是指为电脑主机提供有线无线网络功能的适配器。而网卡驱动指的就是电脑连接识别这些网卡型号的桥梁。网卡只有打上了网卡驱动才能正常使用。并不是说所有的网卡一插到电脑上面就能进行数据传输了,他都需要里面芯片组的驱动文件才能支持他进行数据传输...

2026-01-30 00:37 liuian

win10更新助手装系统(微软win10更新助手)

1、点击首页“系统升级”的按钮,给出弹框,告诉用户需要上传IMEI码才能使用升级服务。同时给出同意和取消按钮。华为手机助手2、点击同意,则进入到“系统升级”功能华为手机助手华为手机助手3、在检测界面,...

windows11专业版密钥最新(windows11专业版激活码永久)

 Windows11专业版的正版密钥,我们是对windows的激活所必备的工具。该密钥我们可以通过微软商城或者通过计算机的硬件供应商去购买获得。获得了windows11专业版的正版密钥后,我...

手机删过的软件恢复(手机删除过的软件怎么恢复)
手机删过的软件恢复(手机删除过的软件怎么恢复)

操作步骤:1、首先,我们需要先打开手机。然后在许多图标中找到带有[文件管理]文本的图标,然后单击“文件管理”进入页面。2、进入页面后,我们将在顶部看到一行文本:手机,最新信息,文档,视频,图片,音乐,收藏,最后是我们正在寻找的[更多],单击...

2026-01-29 23:55 liuian

一键ghost手动备份系统步骤(一键ghost 备份)

  步骤1、首先把装有一键GHOST装系统的U盘插在电脑上,然后打开电脑马上按F2或DEL键入BIOS界面,然后就选择BOOT打USDHDD模式选择好,然后按F10键保存,电脑就会马上重启。  步骤...

怎么创建局域网(怎么创建局域网打游戏)

  1、购买路由器一台。进入路由器把dhcp功能打开  2、购买一台交换机。从路由器lan端口拉出一条网线查到交换机的任意一个端口上。  3、两台以上电脑。从交换机任意端口拉出网线插到电脑上(电脑设置...

精灵驱动器官方下载(精灵驱动手机版下载)

是的。驱动精灵是一款集驱动管理和硬件检测于一体的、专业级的驱动管理和维护工具。驱动精灵为用户提供驱动备份、恢复、安装、删除、在线更新等实用功能。1、全新驱动精灵2012引擎,大幅提升硬件和驱动辨识能力...

一键还原系统步骤(一键还原系统有哪些)

1、首先需要下载安装一下Windows一键还原程序,在安装程序窗口中,点击“下一步”,弹出“用户许可协议”窗口,选择“我同意该许可协议的条款”,并点击“下一步”。  2、在弹出的“准备安装”窗口中,可...

电脑加速器哪个好(电脑加速器哪款好)

我认为pp加速器最好用,飞速土豆太懒,急速酷六根本不工作。pp加速器什么网页都加速,太任劳任怨了!以上是个人观点,具体性能请自己试。ps:我家电脑性能很好。迅游加速盒子是可以加速电脑的。因为有过之...

任何u盘都可以做启动盘吗(u盘必须做成启动盘才能装系统吗)

是的,需要注意,U盘的大小要在4G以上,最好是8G以上,因为启动盘里面需要装系统,内存小的话,不能用来安装系统。内存卡或者U盘或者移动硬盘都可以用来做启动盘安装系统。普通的U盘就可以,不过最好U盘...

u盘怎么恢复文件(u盘文件恢复的方法)

开360安全卫士,点击上面的“功能大全”。点击文件恢复然后点击“数据”下的“文件恢复”功能。选择驱动接着选择需要恢复的驱动,选择接入的U盘。点击开始扫描选好就点击中间的“开始扫描”,开始扫描U盘数据。...

系统虚拟内存太低怎么办(系统虚拟内存占用过高什么原因)

1.检查系统虚拟内存使用情况,如果发现有大量的空闲内存,可以尝试释放一些不必要的进程,以释放内存空间。2.如果系统虚拟内存使用率较高,可以尝试增加系统虚拟内存的大小,以便更多的应用程序可以使用更多...

剪贴板权限设置方法(剪贴板访问权限)
剪贴板权限设置方法(剪贴板访问权限)

1、首先打开iphone手机,触碰并按住单词或图像直到显示选择选项。2、其次,然后选取“拷贝”或“剪贴板”。3、勾选需要的“权限”,最后选择开启,即可完成苹果剪贴板权限设置。仅参考1.打开苹果手机设置按钮,点击【通用】。2.点击【键盘】,再...

2026-01-29 21:37 liuian

平板系统重装大师(平板重装win系统)

如果你的平板开不了机,但可以连接上电脑,那就能好办,楼主下载安装个平板刷机王到你的个人电脑上,然后连接你的平板,平板刷机王会自动识别你的平板,平板刷机王上有你平板的我刷机包,楼主点击下载一个,下载完成...

联想官网售后服务网点(联想官网售后服务热线)

联想3c服务中心是联想旗下的官方售后,是基于互联网O2O模式开发的全新服务平台。可以为终端用户提供多品牌手机、电脑以及其他3C类产品的维修、保养和保险服务。根据客户需求层次,联想服务针对个人及家庭客户...