PostgreSQL 逻辑复制下分区表的 INSERT:从 publisher 到 leaf 的完整时序


 TL;DR
主要源码路径: - ~/cwork/postgresql/src/backend/replication/pgoutput/pgoutput.c - ~/cwork/postgresql/src/backend/replication/logical/proto.c - ~/cwork/postg…
难度 中等

本文是 PostgreSQL 逻辑复制与分区表:从 publisher 到 partition leaf 的全链路 的深度细化篇。

那一篇讲了 apply worker 大致怎么处理分区表 INSERT。本文把每条 message、每个函数调用、每次 attr map 翻译、每个 relkind 检查全部画到源码级。

主要源码路径:

  • ~/cwork/postgresql/src/backend/replication/pgoutput/pgoutput.c
  • ~/cwork/postgresql/src/backend/replication/logical/proto.c
  • ~/cwork/postgresql/src/backend/replication/logical/worker.c
  • ~/cwork/postgresql/src/backend/replication/logical/relation.c
  • ~/cwork/postgresql/src/backend/executor/execPartition.c
  • ~/cwork/postgresql/src/backend/executor/execReplication.c
  • ~/cwork/babelfish_extensions/contrib/babelfishpg_tsql/src/pltsql_partition.c

一、整条链路的”分层”

分区表 INSERT 在逻辑复制里要经过三个进程边界:

flowchart LR subgraph pub["publisher 进程"] P1["client INSERT INTO orders_p_2024_h2"]:::pub P2["heap_insert 写 heap 页"]:::pub P3["WAL: HEAP_INSERT record"]:::pub P4["logical decoding<br/>ReorderBuffer 排序"]:::pub P5["pgoutput_change<br/>(决定以 leaf OID 还是 root OID 上报)"]:::pub P1 --> P2 --> P3 --> P4 --> P5 end subgraph wire["wire protocol"] W1["LOGICAL_REP_MSG_RELATION<br/>(schema 缓存)"] W2["LOGICAL_REP_MSG_BEGIN"] W3["LOGICAL_REP_MSG_INSERT<br/>relid + 新元组"] W4["LOGICAL_REP_MSG_COMMIT"] end subgraph sub["subscriber apply worker 进程"] S1["apply_dispatch → apply_handle_insert"]:::sub S2["logicalrep_rel_open(remoteid)"]:::sub S3["relkind 检查<br/>(partitioned table?)"]:::sub S4["apply_handle_tuple_routing"]:::sub S5["ExecFindPartition<br/>(FormPartitionKeyDatum + bsearch)"]:::sub S6["attr map 翻译<br/>(execute_attr_map_slot)"]:::sub S7["CheckSubscriptionRelkind(leaf)"]:::sub S8["ExecSimpleRelationInsert<br/>→ heap_insert → WAL"]:::sub S1 --> S2 --> S3 --> S4 --> S5 --> S6 --> S7 --> S8 end P5 --> W1 --> W2 --> W3 --> W4 --> S1

二、publisher 端:pgoutput_change 怎么发分区表 INSERT

2.1 入口:pgoutput_change

src/backend/replication/pgoutput/pgoutput.c:1482:

// src/backend/replication/pgoutput/pgoutput.c:1508-1523
  relentry = get_rel_sync_entry(data, relation);

  /* First check the table filter */
  switch (action)
  {
    case REORDER_BUFFER_CHANGE_INSERT:
      if (!relentry->pubactions.pubinsert)
        return;
      break;
    case REORDER_BUFFER_CHANGE_UPDATE:
      if (!relentry->pubactions.pubupdate)
        return;
      break;
    case REORDER_BUFFER_CHANGE_DELETE:
      if (!relentry->pubactions.pubdelete)
        return;

发消息本体(src/backend/replication/pgoutput/pgoutput.c:1604):

// src/backend/replication/pgoutput/pgoutput.c:1604-1615-1614
  maybe_send_schema(ctx, change, relation, relentry);

  OutputPluginPrepareWrite(ctx, true);

  /* Send the data */
  switch (action)
  {
    case REORDER_BUFFER_CHANGE_INSERT:
      logicalrep_write_insert(ctx->out, xid, targetrel, new_slot,
                  data->binary, relentry->columns,
                  relentry->include_gencols_type);
      break;

写完收尾(src/backend/replication/pgoutput/pgoutput.c:1630):

// src/backend/replication/pgoutput/pgoutput.c:1630-1630
  OutputPluginWrite(ctx, true);

PG 18 里发 schema 的函数叫 maybe_send_schema(src/backend/replication/pgoutput/pgoutput.c:725)→ send_relation_and_attrs(src/backend/replication/pgoutput/pgoutput.c:796),没有 PG 17 之前的 pgoutput_send_relation。OutputPluginPrepareWrite / OutputPluginWrite 本身还在(src/backend/replication/logical/logical.c:787 和 src/backend/replication/logical/logical.c:800),但它们背后的回调类型在 PG 18 已经换成 LogicalOutputPluginWriterPrepareWrite / LogicalOutputPluginWriterWrite(src/include/replication/logical.h:19-25)。

2.2 关键:get_rel_sync_entry 的祖先选择

src/backend/replication/pgoutput/pgoutput.c:2055:

// src/backend/replication/pgoutput/pgoutput.c:2295-2306
        else
        {
          /* Same ancestor level, has to be the same OID. */
          Assert(publish_as_relid == pub_relid);
        }

        /* Track publications for this ancestor. */
        rel_publications = lappend(rel_publications, pub);
      }
    }

    entry->publish_as_relid = publish_as_relid;

publish_as_relid 的计算(src/backend/replication/pgoutput/pgoutput.c:2207 一带):

// src/backend/replication/pgoutput/pgoutput.c:2207-2217
      if (pub->alltables)
      {
        publish = true;
        if (pub->pubviaroot && am_partition)
        {
          List	   *ancestors = get_partition_ancestors(relid);

          pub_relid = llast_oid(ancestors);
          ancestor_level = list_length(ancestors);
        }
      }

祖先选择靠 GetTopMostAncestorInPublication(调用点 src/backend/replication/pgoutput/pgoutput.c:2235,实现在 src/backend/catalog/pg_publication.c:355)——不是沿 pg_partition_ancestors 自己循环,而是问 publication “它包含了哪些祖先”。

结论:

pubviaroot publish_as_relid INSERT 消息里 relid 字段
false(默认) leaf OID leaf OID
true 父表 OID 父表 OID + 后续需 attr map 翻译

注:pubviaroot = false 是默认设置,但 PG 14+ 起对”挂父表到 subscription”的官方推荐是 pubviaroot = true。否则 subscriber 端必须事先建好所有 leaf 镜像。

2.3 决定 leaf 还是 root 后,调 logicalrep_write_insert

src/backend/replication/logical/proto.c:403:

// src/backend/replication/logical/proto.c:402-420
void
logicalrep_write_insert(StringInfo out, TransactionId xid, Relation rel,
            TupleTableSlot *newslot, bool binary,
            Bitmapset *columns,
            PublishGencolsType include_gencols_type)
{
  pq_sendbyte(out, LOGICAL_REP_MSG_INSERT);

  /* transaction ID (if not valid, we're not streaming) */
  if (TransactionIdIsValid(xid))
    pq_sendint32(out, xid);

  /* use Oid as relation identifier */
  pq_sendint32(out, RelationGetRelid(rel));

  pq_sendbyte(out, 'N');		/* new tuple follows */
  logicalrep_write_tuple(out, rel, newslot, binary, columns,
               include_gencols_type);
}

PG 18 的 logicalrep_write_insert 签名比 PG 14 多了 xid、newslot、binary、columns、include_gencols_type 五个参数——写的是已经填好的 slot,不是 relation。

rel 是从 get_rel_sync_entry 里拿到的 publish_as_relid——可能是 leaf 也可能是父表。如果父表,写出的列顺序就是父表的列顺序(subscriber 端 apply_handle_insert 拿这个 slot 做 attr map 翻译)。

2.4 wire 上的消息顺序

同一个事务的多个 change 按 commit_lsn 排序:

BEGIN                                (LOGICAL_REP_MSG_BEGIN 'B')
  RELATION <schema>                  (LOGICAL_REP_MSG_RELATION 'R', 仅 schema 变更或首次)
  INSERT <relid=orders> <data>       (LOGICAL_REP_MSG_INSERT 'I')
  INSERT <relid=orders> <data>       (LOGICAL_REP_MSG_INSERT 'I')
  UPDATE <relid=orders> <data>       (LOGICAL_REP_MSG_UPDATE 'U')
  ...
COMMIT <commit_lsn>                 (LOGICAL_REP_MSG_COMMIT 'C')

消息类型枚举在 src/include/replication/logicalproto.h:57('B' / 'C' / 'I' / 'U' / 'R' …)。

每个 INSERT 消息体结构(src/backend/replication/logical/proto.c:428 的 logicalrep_read_insert 反序列化):

// src/include/replication/logicalproto.h:84-92
typedef struct LogicalRepTupleData
{
  /* Array of StringInfos, one per column; some may be unused */
  StringInfoData *colvalues;
  /* Array of markers for null/unchanged/text/binary, one per column */
  char	   *colstatus;
  /* Length of above arrays */
  int			ncols;
} LogicalRepTupleData;

PG 18 已经没有 inherited_columns 这个字段了——它在 PG 15 被删掉了。现在的 LogicalRepTupleData 只有 colvalues / colstatus / ncols 三样,colstatus[i] 取 LOGICALREP_COLUMN_NULL / UNCHANGED / TEXT / BINARY(src/include/replication/logicalproto.h:96-99)四个值之一。也就是说”哪些列是继承来的”这个信息在 PG 18 的 wire protocol 里不再单独编码——未发送的列由 subscriber 端按默认值补齐。


三、subscriber 端:apply_handle_insert 收到 INSERT

3.1 主入口:apply_handle_insert

src/backend/replication/logical/worker.c:2405:

// src/backend/replication/logical/worker.c:2421-2438
  if (is_skipping_changes() ||
    handle_streamed_transaction(LOGICAL_REP_MSG_INSERT, s))
    return;

  begin_replication_step();

  relid = logicalrep_read_insert(s, &newtup);
  rel = logicalrep_rel_open(relid, RowExclusiveLock);
  if (!should_apply_changes_for_rel(rel))
  {
    /*
     * The relation can't become interesting in the middle of the
     * transaction so it's safe to unlock it.
     */
    logicalrep_rel_close(rel, RowExclusiveLock);
    end_replication_step();
    return;
  }

取 slot 与填默认值(src/backend/replication/logical/worker.c:2454):

// src/backend/replication/logical/worker.c:2454-2462
  remoteslot = ExecInitExtraTupleSlot(estate,
                    RelationGetDescr(rel->localrel),
                    &TTSOpsVirtual);

  /* Process and store remote tuple in the slot */
  oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
  slot_store_data(remoteslot, rel, &newtup);
  slot_fill_defaults(rel, estate, remoteslot);
  MemoryContextSwitchTo(oldctx);

注意几个细节:

  1. should_apply_changes_for_rel(src/backend/replication/logical/worker.c:460):检查 pg_subscription_rel.srsubstate 是否允许 apply。状态机里 READY 或 SYNCDONE 且 lsn <= remote_final_lsn 才允许。否则直接丢弃,不报错——这是 DML 在 tablesync 阶段不重放的保证。
  2. slot_store_data:把从 wire 解出来的 LogicalRepTupleData 转成 TupleTableSlot。如果 rel 是父表,slot 用的是父表的 TupleDesc(列顺序是父表的列顺序,包括列继承的列)。
  3. slot_fill_defaults:补齐订阅端默认值(如果 publisher 用了 DEFAULT column sync)。

3.2 create_edata_for_relation:构造 ModifyTableState

虽然 apply_handle_insert 是顺序处理的,但 apply_handle_tuple_routing 要求一个 ModifyTableState。实现见 src/backend/replication/logical/worker.c:644:

// src/backend/replication/logical/worker.c:652-674
  edata = (ApplyExecutionData *) palloc0(sizeof(ApplyExecutionData));
  edata->targetRel = rel;

  edata->estate = estate = CreateExecutorState();

  rte = makeNode(RangeTblEntry);
  rte->rtekind = RTE_RELATION;
  rte->relid = RelationGetRelid(rel->localrel);
  rte->relkind = rel->localrel->rd_rel->relkind;
  rte->rellockmode = AccessShareLock;

  addRTEPermissionInfo(&perminfos, rte);

  ExecInitRangeTable(estate, list_make1(rte), perminfos,
             bms_make_singleton(1));

  edata->targetRelInfo = resultRelInfo = makeNode(ResultRelInfo);

  /*
   * Use Relation opened by logicalrep_rel_open() instead of opening it
   * again.
   */
  InitResultRelInfo(resultRelInfo, rel->localrel, 1, NULL, 0);

PG 18 的 create_edata_for_relation 比 PG 14 多了 RangeTblEntry + perminfos(addRTEPermissionInfo)这一段——PG 16 引入的并行 apply 需要按权限信息建 RTE。

3.3 关键代码点:父表 vs leaf 分支

src/backend/replication/logical/worker.c:2465:

// src/backend/replication/logical/worker.c:2464-2475
  /* For a partitioned table, insert the tuple into a partition. */
  if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
    apply_handle_tuple_routing(edata,
                   remoteslot, NULL, CMD_INSERT);
  else
  {
    ResultRelInfo *relinfo = edata->targetRelInfo;

    ExecOpenIndices(relinfo, false);
    apply_handle_insert_internal(edata, relinfo, remoteslot);
    ExecCloseIndices(relinfo);
  }

只有父表才走 tuple routing。Leaf 走直接 apply_handle_insert_internal(普通路径)。


四、apply_handle_tuple_routing 详解

src/backend/replication/logical/worker.c:3004:

// src/backend/replication/logical/worker.c:3023-3041
  /* ModifyTableState is needed for ExecFindPartition(). */
  edata->mtstate = mtstate = makeNode(ModifyTableState);
  mtstate->ps.plan = NULL;
  mtstate->ps.state = estate;
  mtstate->operation = operation;
  mtstate->resultRelInfo = relinfo;

  /* ... as is PartitionTupleRouting. */
  edata->proute = proute = ExecSetupPartitionTupleRouting(estate, parentrel);

  /*
   * Find the partition to which the "search tuple" belongs.
   */
  Assert(remoteslot != NULL);
  oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
  partrelinfo = ExecFindPartition(mtstate, relinfo, proute,
                  remoteslot, estate);
  Assert(partrelinfo != NULL);
  partrel = partrelinfo->ri_RelationDesc;

二次校验(src/backend/replication/logical/worker.c:3048):

// src/backend/replication/logical/worker.c:3043-3050
  /*
   * Check for supported relkind.  We need this since partitions might be of
   * unsupported relkinds; and the set of partitions can change, so checking
   * at CREATE/ALTER SUBSCRIPTION would be insufficient.
   */
  CheckSubscriptionTargetRel(RelationGetRelid(partrel),
                 get_namespace_name(RelationGetNamespace(partrel)),
                 RelationGetRelationName(partrel));

attr map 翻译(src/backend/replication/logical/worker.c:3057):

// src/backend/replication/logical/worker.c:3052-3072
  /*
   * To perform any of the operations below, the tuple must match the
   * partition's rowtype. Convert if needed or just copy, using a dedicated
   * slot to store the tuple in any case.
   */
  remoteslot_part = partrelinfo->ri_PartitionTupleSlot;
  if (remoteslot_part == NULL)
    remoteslot_part = table_slot_create(partrel, &estate->es_tupleTable);
  map = ExecGetRootToChildMap(partrelinfo, estate);
  if (map != NULL)
  {
    attrmap = map->attrMap;
    remoteslot_part = execute_attr_map_slot(attrmap, remoteslot,
                        remoteslot_part);
  }
  else
  {
    remoteslot_part = ExecCopySlot(remoteslot_part, remoteslot);
    slot_getallattrs(remoteslot_part);
  }
  MemoryContextSwitchTo(oldctx);

PG 18 的 leaf 二次校验走的是 CheckSubscriptionTargetRel(RelationGetRelid(partrel), ...)(src/backend/replication/logical/worker.c:3048),它内部转调 CheckSubscriptionRelkind(src/backend/executor/execReplication.c:953)——PG 17 及以前是直接调 CheckSubscriptionRelkind(partrel->rd_rel->relkind, ...)。

4.1 构造 ModifyTableState + PartitionTupleRouting

src/backend/replication/logical/worker.c:3023:

edata->mtstate = mtstate = makeNode(ModifyTableState);
mtstate->ps.state = estate;
mtstate->operation = operation;
mtstate->resultRelInfo = relinfo;

edata->proute = proute = ExecSetupPartitionTupleRouting(estate, parentrel);

ExecSetupPartitionTupleRouting(src/backend/executor/execPartition.c:218)内部会递归建好所有层级的 PartitionDispatch:

// src/backend/executor/execPartition.c:218-243
ExecSetupPartitionTupleRouting(EState *estate, Relation rel)
{
  PartitionTupleRouting *proute;

  /*
   * Here we attempt to expend as little effort as possible in setting up
   * the PartitionTupleRouting.  Each partition's ResultRelInfo is built on
   * demand, only when we actually need to route a tuple to that partition.
   * The reason for this is that a common case is for INSERT to insert a
   * single tuple into a partitioned table and this must be fast.
   */
  proute = (PartitionTupleRouting *) palloc0(sizeof(PartitionTupleRouting));
  proute->partition_root = rel;
  proute->memcxt = CurrentMemoryContext;
  /* Rest of members initialized by zeroing */

  /*
   * Initialize this table's PartitionDispatch object.  Here we pass in the
   * parent as NULL as we don't need to care about any parent of the target
   * partitioned table.
   */
  ExecInitPartitionDispatchInfo(estate, proute, RelationGetRelid(rel),
                  NULL, 0, NULL);

  return proute;
}

递归入口是 ExecInitPartitionDispatchInfo(src/backend/executor/execPartition.c:1102)。

每次 INSERT 到父表都重新构造一次。这是有意的——PartitionTupleRouting 不重用,proute->partition_root 指向的 Relation 是当前 logicalrep_rel_open 拿到的,跨 message 不能跨事务跨 Relation 复用。

4.2 ExecFindPartition:复用内核 partition routing

src/backend/executor/execPartition.c:265:

partrelinfo = ExecFindPartition(mtstate, relinfo, proute, remoteslot, estate);

这条调用和普通 INSERT(不走逻辑复制)走的是完全相同的代码——FormPartitionKeyDatum(src/backend/executor/execPartition.c:1302)+ get_partition_for_tuple(src/backend/executor/execPartition.c:1399)+ partition_*_bsearch(src/backend/partitioning/partbounds.c):

// src/backend/executor/execPartition.c:1552-1565
        bound_offset = partition_range_datum_bsearch(key->partsupfunc,
                             key->partcollation,
                             boundinfo,
                             key->partnatts,
                             values,
                             &equal);

        /*
         * The bound at bound_offset is less than or equal to the
         * tuple value, so the bound at offset+1 is the upper bound of
         * the partition we're looking for, if there actually exists
         * one.
         */
        part_index = boundinfo->indexes[bound_offset + 1];

4.3 CheckSubscriptionRelkind:leaf 二次校验

src/backend/replication/logical/worker.c:3048:

CheckSubscriptionTargetRel(RelationGetRelid(partrel),
               get_namespace_name(RelationGetNamespace(partrel)),
               RelationGetRelationName(partrel));

src/backend/executor/execReplication.c:953:

// src/backend/executor/execReplication.c:952-962
void
CheckSubscriptionRelkind(char relkind, const char *nspname,
             const char *relname)
{
  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
    ereport(ERROR,
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
         errmsg("cannot use relation \"%s.%s\" as logical replication target",
            nspname, relname),
         errdetail_relkind_not_supported(relkind)));
}

为什么 apply 时再校验一次?

  • DDL 同步到 subscriber 后,leaf 可能被 ALTER TABLE ... SET LOGICAL REPLICA ... 改了 relkind,或者变成 VIEW/FOREIGN TABLE。
  • 中间层 partition 树可能动态变化,relkind 集合在 CREATE/ALTER SUBSCRIPTION 时不可能完整确认。
  • 所以每次落到 leaf 都校验一次——保证 INSERT 永远只写 RELKIND_RELATION 或 RELKIND_PARTITIONED_TABLE。

4.4 attr map 翻译

src/backend/replication/logical/worker.c:3057:

remoteslot_part = partrelinfo->ri_PartitionTupleSlot;
if (remoteslot_part == NULL)
    remoteslot_part = table_slot_create(partrel, &estate->es_tupleTable);
map = ExecGetRootToChildMap(partrelinfo, estate);
if (map != NULL) {
    attrmap = map->attrMap;
    remoteslot_part = execute_attr_map_slot(attrmap, remoteslot, remoteslot_part);
} else {
    remoteslot_part = ExecCopySlot(remoteslot_part, remoteslot);
    slot_getallattrs(remoteslot_part);
}

这一步把”父表 TupleDesc 的 slot”翻译成”子表 TupleDesc 的 slot”。

场景 是否需要 attr map
leaf 和父表列定义完全一致 否(map == NULL,走 ExecCopySlot)
leaf 删除了某列 是(按列名匹配)
leaf 重排列顺序 是
leaf 是不同 schema 否(TupleDesc 不变)
leaf 有额外列(DEFAULT 填充) 否(publisher 没发的列在 leaf 由 slot_fill_defaults 补)

TupleConversionMap 由 ExecGetRootToChildMap 构造——它在 ExecInitPartitionInfo 时按 leaf 的 RelationGetIndexExpressions / attisdropped 等信息计算并缓存。

4.5 apply_handle_insert_internal 实际落盘

src/backend/replication/logical/worker.c:2496:

// src/backend/replication/logical/worker.c:2499-2514
{
  EState	   *estate = edata->estate;

  /* Caller should have opened indexes already. */
  Assert(relinfo->ri_IndexRelationDescs != NULL ||
       !relinfo->ri_RelationDesc->rd_rel->relhasindex ||
       RelationGetIndexList(relinfo->ri_RelationDesc) == NIL);

  /* Caller will not have done this bit. */
  Assert(relinfo->ri_onConflictArbiterIndexes == NIL);
  InitConflictIndexes(relinfo);

  /* Do the insert. */
  TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_INSERT);
  ExecSimpleRelationInsert(relinfo, estate, remoteslot);
}

InitConflictIndexes 一次性 cache 一组索引(用于后续 ON CONFLICT)。ExecSimpleRelationInsert 内部:

ExecSimpleRelationInsert
   → heap_insert
      → heap_prepare_insert (生成 xl_heap_insert WAL record)
      → RelationPutHeapTuple
         → PageAddItem (buffer pool 写 + WAL flush)
   → insert indexes (btree, gin, gist, etc.)
   → pgstat_report_heap_insert (更新 stat counters)

五、关键不变量

5.1 publisher vs subscriber 的 schema 一致性

relmapentry->attrmap(在 logicalrep_rel_open 里构建,src/backend/replication/logical/relation.c:437 一带):

// src/backend/replication/logical/relation.c:435-456
    desc = RelationGetDescr(entry->localrel);
    oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
    entry->attrmap = make_attrmap(desc->natts);
    MemoryContextSwitchTo(oldctx);

    /* check and report missing attrs, if any */
    missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
    for (i = 0; i < desc->natts; i++)
    {
      int			attnum;
      Form_pg_attribute attr = TupleDescAttr(desc, i);

      if (attr->attisdropped)
      {
        entry->attrmap->attnums[i] = -1;
        continue;
      }

      attnum = logicalrep_rel_att_by_name(remoterel,
                        NameStr(attr->attname));

      entry->attrmap->attnums[i] = attnum;

这是双层 attr map:

  • 第一层:publisher 的 remoterel.attrmap → 把 publisher 列号映射成”实际发的列位置”。
  • 第二层:subscriber 父表的 relmapentry->attrmap → 把 subscriber 父表列号映射成”wire 上的列位置”。
  • 第三层(如果 publish_as_relid = 父表):在 apply_handle_tuple_routing 内再把父表 slot 翻译成 leaf slot。

5.2 pubviaroot 三种场景对比

场景 pubviaroot 消息里 relid subscriber apply_handle_insert 路径 是否需要 leaf 镜像 是否需要 attr map
只挂 leaf false(默认) leaf OID 走 apply_handle_insert_internal(直接) 是(每 leaf 必须存在) 不需要(slot 就是 leaf)
挂父表 + pubviaroot=true true 父表 OID 走 apply_handle_tuple_routing 不需要(routing 内部 ExecFindPartition 找 leaf) 需要(父→leaf slot 翻译)
挂父表 + pubviaroot=false false leaf OID 走 apply_handle_insert_internal 是 不需要

PG 14+ 推荐第二种——只需要在 subscriber 端建父表,所有 leaf 由 ExecFindPartition 在 subscriber 端动态创建(前提是 ALTER SUBSCRIPTION ... REFRESH PUBLICATION 时 subscriber 的 leaf 也已经建好了;或者 PG 16+ 的 auto-create partition 实验功能)。

5.3 catch-up 期间的行为

DML 在 tablesync 还没完成时已经被 publisher 发出来了——subscriber apply worker 收到怎么办?

src/backend/replication/logical/worker.c:480:

// src/backend/replication/logical/worker.c:479-482
    case WORKERTYPE_APPLY:
      return (rel->state == SUBREL_STATE_READY ||
          (rel->state == SUBREL_STATE_SYNCDONE &&
           rel->statelsn <= remote_final_lsn));
  • state != READY 且 state != SYNCDONE(即 INIT / DATASYNC / CATCHUP):直接 return,丢弃 change,不报错。
  • state == SYNCDONE && statelsn <= remote_final_lsn:允许 apply(catch-up 阶段追上之后)。
  • state == READY:正常 apply。

这就是为什么 subscriber 端的 leaf 在 tablesync 还没跑完时收到的 DML 不会写到 leaf——它们直接被丢弃,由 tablesync worker 的 COPY 把基线数据拉过来之后,再从 remote_lsn 开始 apply。

5.4 列状态标志位:colstatus

PG 18 的 LogicalRepTupleData 没有 inherited_columns 字段(它在 PG 15 被删掉了)。取而代之的是每列一个 colstatus 字节,取值见 src/include/replication/logicalproto.h:96-99:

  • LOGICALREP_COLUMN_NULL('n'):该列为 NULL。
  • LOGICALREP_COLUMN_UNCHANGED('u'):UPDATE 时该列未被修改,wire 上不带值。
  • LOGICALREP_COLUMN_TEXT('t')/ LOGICALREP_COLUMN_BINARY('b'):文本 / 二进制形式。

解析在 logicalrep_read_tuple(src/backend/replication/logical/proto.c:861):

// src/backend/replication/logical/proto.c:882-895
    kind = pq_getmsgbyte(in);
    tuple->colstatus[i] = kind;

    switch (kind)
    {
      case LOGICALREP_COLUMN_NULL:
        /* nothing more to do */
        break;
      case LOGICALREP_COLUMN_UNCHANGED:
        /* we don't receive the value of an unchanged column */
        break;
      case LOGICALREP_COLUMN_TEXT:
      case LOGICALREP_COLUMN_BINARY:
        len = pq_getmsgint(in, 4);	/* read length */

落到 slot 由 slot_store_data(src/backend/replication/logical/worker.c:794)完成——它按 rel->attrmap->attnums[i] 逐列查 colstatus,只有 TEXT / BINARY 才调输入函数填值(src/backend/replication/logical/worker.c:826)。

这在多级嵌套分区里很重要——只有真正被 INSERT 显式赋值的列才在 wire 上出现,其他列由 subscriber 端默认值补齐(slot_fill_defaults,src/backend/replication/logical/worker.c:737)。

这在多级嵌套分区里很重要——只有真正被 INSERT 显式赋值的列才在 wire 上出现,其他列由 subscriber 端默认值补齐。


六、多层嵌套分区的递归

如果 subscriber 是 orders → orders_2024 → orders_2024_cn 的嵌套分区(publish_via_partition_root = true):

apply_handle_tuple_routing
  ├─ edata->proute = ExecSetupPartitionTupleRouting(estate, parentrel = orders)
  │     ├─ pd[0] = PartitionDispatch for orders
  │     │    ├─ pd[0].indexes[0] = 0  (orders_2024 是中间层)
  │     │    ├─ pd[0].indexes[1] = -1 (orders_2024_cn 直接挂在 orders? 不会, 中间层先)
  │     │    └─ 递归: ExecInitPartitionDispatchInfo(orders_2024, ...)
  │     │         └─ pd[1] = PartitionDispatch for orders_2024
  │     │              └─ pd[1].indexes[0] = -1 (orders_2024_cn 是 leaf, 按需建)
  │     └─ 其它 leaf pd[*].indexes[*] 全部 -1
  │
  └─ ExecFindPartition(mtstate, relinfo, proute, remoteslot, estate)
        ├─ dispatch = pd[0] (orders)
        │    ├─ FormPartitionKeyDatum(orders, slot)  → values = [2024-08-15]
        │    ├─ get_partition_for_tuple → partidx = 0 (orders_2024)
        │    └─ is_leaf? 否 → dispatch = pd[pd[0].indexes[0]] = pd[1] (orders_2024)
        │
        ├─ dispatch = pd[1] (orders_2024)
        │    ├─ FormPartitionKeyDatum(orders_2024, slot)  → values = ['CN']
        │    │    (注意: 因为 pd[1].tupmap 处理了父→子的 attr 重映射)
        │    ├─ get_partition_for_tuple → partidx = 0 (orders_2024_cn)
        │    └─ is_leaf? 是 → 拿 leaf rri
        │
        └─ 返回 orders_2024_cn 的 ResultRelInfo

pd->tupmap 在每层之间做 slot 翻译,把父表 schema 的 slot 转成子表 schema 的 slot。这对嵌套分区尤其重要——中间层 partition 可能用不同列(如 region 列只在 orders_2024 中间层存在)。


七、LogicalRepRelMap 缓存层

src/include/replication/logicalrelation.h:19:

// src/include/replication/logicalrelation.h:19-44
typedef struct LogicalRepRelMapEntry
{
  LogicalRepRelation remoterel;	/* key is remoterel.remoteid */

  /*
   * Validity flag -- when false, revalidate all derived info at next
   * logicalrep_rel_open.  (While the localrel is open, we assume our lock
   * on that rel ensures the info remains good.)
   */
  bool		localrelvalid;

  /* Mapping to local relation. */
  Oid			localreloid;	/* local relation id */
  Relation	localrel;		/* relcache entry (NULL when closed) */
  AttrMap    *attrmap;		/* map of local attributes to remote ones */
  bool		updatable;		/* Can apply updates/deletes? */
  bool		idxisreplident; /* is localindexoid the relation's replica
                 * identity or primary key, rather than an
                 * index usable for a REPLICA IDENTITY FULL
                 * remote relation? */
  Oid			localindexoid;	/* which index to use, or InvalidOid if none */

  /* Sync state. */
  char		state;
  XLogRecPtr	statelsn;
} LogicalRepRelMapEntry;

缓存键是 remoterel.remoteid,全局 HTAB *LogicalRepRelMap(src/backend/replication/logical/relation.c:36,创建于 src/backend/replication/logical/relation.c:120)。

7.1 失效场景

  • subscriber 端 ALTER TABLE 改了列定义 → CacheInvalidateRelcache 触发。
  • subscriber 端 relcache flush(任何 DDL)→ 同上。
  • localrel 被 close 后指针失效 → apply worker 用 localrelvalid 标志。

失效回调是 logicalrep_relmap_invalidate_cb(src/backend/replication/logical/relation.c:64),它把对应 entry 的 localrelvalid 置 false(src/backend/replication/logical/relation.c:83)。

logicalrep_relmap_update(src/backend/replication/logical/relation.c:164):

// src/backend/replication/logical/relation.c:177-183
  entry = hash_search(LogicalRepRelMap, &remoterel->remoteid,
            HASH_ENTER, &found);

  if (found)
    logicalrep_relmap_free_entry(entry);

  memset(entry, 0, sizeof(LogicalRepRelMapEntry));

7.2 分区表的 partition map cache

src/backend/replication/logical/relation.c:614:

// src/backend/replication/logical/relation.c:614-615
  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
                  HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);

为每个 leaf OID 缓存一份 LogicalRepPartMapEntry:

// src/backend/replication/logical/relation.c:51-55
typedef struct LogicalRepPartMapEntry
{
  Oid			partoid;		/* LogicalRepPartMap's key */
  LogicalRepRelMapEntry relmapentry;
} LogicalRepPartMapEntry;

logicalrep_partition_open(src/backend/replication/logical/relation.c:634):

// src/backend/replication/logical/relation.c:645-653
  if (LogicalRepPartMap == NULL)
    logicalrep_partmap_init();

  /* Search for existing entry. */
  part_entry = (LogicalRepPartMapEntry *) hash_search(LogicalRepPartMap,
                            &partOid,
                            HASH_ENTER, &found);

  entry = &part_entry->relmapentry;

注意:

  1. partition map 是 cache——localrel 字段每次调用都会强制覆盖(因为旧 Relation 可能被 relcache flush)。
  2. schema (remoterel) 沿用 root 的——因为分区表 schema 来自 publisher 端的父表,所有 leaf 共享同一 schema(列顺序可能不同,但列名一样)。

八、Babelfish T-SQL 模式下的差异

8.1 publisher 端 T-SQL INSERT

INSERT INTO dbo.orders (region, orderdate, amount)
VALUES (N'CN', '2024-08-15', 100.0);

Babelfish 在 TDS 协议层收到 INSERT 请求后,把它翻译成 PG 原生 InsertStmt:

T-SQL: INSERT INTO dbo.orders (...) VALUES (...)
     ↓
Babelfish analyzer
     ↓
PG: INSERT INTO dbo.orders (...) VALUES (...)
     ↓
PG executor 跑 INSERT (跟普通分区表 INSERT 完全一样)

注意:

  • Babelfish 不会因为”T-SQL 是分区表”就在 INSERT 路径上加任何特殊逻辑。
  • dbo.orders 在 Babelfish 下是一个普通 PG 分区表(Babelfish 创建时翻译成 PARTITION BY)。
  • INSERT 走的是纯 PG 原生 INSERT 路径——ExecInsert → ExecFindPartition → heap_insert。
  • 这个 INSERT 会触发普通的 WAL record,然后 pgoutput 在 logical decoding 阶段看到它,按 §2 处理。

8.2 subscriber 端 T-SQL INSERT via logical replication

sequenceDiagram participant TDS as TDS client participant TDSWorker as Babelfish TDS worker participant PG as PG backend participant W as WAL / pgoutput participant Apply as apply worker Note over TDS,W: publisher 端 TDS->>TDSWorker: INSERT INTO dbo.orders (...) VALUES (...) TDSWorker->>PG: 把 T-SQL 翻译成 PG 的 InsertStmt, 走 ProcessUtility PG->>PG: ExecInsert → ExecFindPartition → heap_insert PG->>W: HEAP_INSERT WAL record Note over W,Apply: subscriber 端 W->>Apply: pgoutput 解码, 发 INSERT 消息 Apply->>Apply: apply_handle_insert (分区表 → apply_handle_tuple_routing)

关键观察:

  • Babelfish 不参与 subscriber 端 apply 的代码路径——apply worker 完全跑 PG 原生代码,跟”Babelfish 上有没有分区函数/scheme metadata”无关。
  • 唯一跟 Babelfish metadata 相关的点是:$PARTITION.<func>(col) 之类的元查询(不会出现在 INSERT 热路径上)。
  • 如果 publisher 上 publisher 端插入了 babelfish_partition_function 没记录的分区列值(极少但可能),apply worker 仍然按 PG native 逻辑落到正确 leaf——但 $PARTITION.<func>(col) 返回值在 subscriber / publisher 之间可能不一致。

8.3 Babelfish 测试用例验证

babelfish_extensions/test/JDBC/replication/partition-replication.mix 演示的 INSERT 验证:

-- publisher 端 (TDS):
INSERT INTO partition_replication_t1_int_partition_function (col, val)
VALUES (1000, 'value_in_third_partition');

-- subscriber 端 (TDS): 验证行被同步到对应 leaf
SELECT * FROM partition_replication_t1_int_partition_function WHERE col = 1000;

这个测试覆盖了:

  • ✅ INSERT 能否在 publisher 端正确路由(Babelfish partition function 找到正确 leaf)。
  • ✅ INSERT 能否通过 logical replication 传到 subscriber(pgoutput → apply worker → apply_handle_tuple_routing → ExecFindPartition)。
  • ✅ Subscriber 端 partition function / scheme 是否能”独立路由”同一行到相同 leaf(验证两边 partition_range_datum_bsearch 行为一致)。

九、监控与排错

9.1 监控视图

-- 当前 apply worker 状态
SELECT pid, usesysid, application_name, state, sync_state, sync_priority
  FROM pg_stat_replication
 WHERE application_name LIKE 'sub_%';   -- apply worker

-- apply worker 的当前 message / transaction
SELECT pid, wait_event_type, wait_event, state, query
  FROM pg_stat_activity
 WHERE backend_type = 'logical replication worker';

-- 每个订阅的进度 (LSN)
SELECT subname, received_lsn, latest_end_lsn, last_msg_send_time, last_msg_replay_time
  FROM pg_stat_subscription;

-- 哪个 relation 在 sync / 哪个已经 READY
SELECT srsubid::regclass AS sub,
       srrelid::regclass AS rel,
       srsubstate
  FROM pg_subscription_rel
 ORDER BY srsubid, srrelid;

9.2 常见错误与定位

错误 触发点 排错
no partition of relation "..." found for row ExecFindPartition 内部 subscriber 端的 leaf 与 publisher 端不一致——同步 DDL
cannot use relation "..." as logical replication target CheckSubscriptionRelkind leaf 是 VIEW / FOREIGN TABLE 等不支持的类型
publisher did not send replica identity column expected by ... apply_handle_update 里的 check_relation_updatable UPDATE/DELETE 没 PK / REPLICA IDENTITY
logical replication target relation "..." does not exist logicalrep_rel_open subscriber 端没建表
INSERT 卡住不前进 should_apply_changes_for_rel 返回 false pg_subscription_rel.srsubstate 状态异常

9.3 强制跳过 stale change(适用于”publisher 已经 INSERT 但 subscriber 不该处理”)

-- 临时禁用某表
ALTER SUBSCRIPTION sub_orders DISABLE;
ALTER SUBSCRIPTION sub_orders SET (slot_name = NONE);
ALTER SUBSCRIPTION sub_orders ENABLE;

或者在 publisher 端:

ALTER PUBLICATION pub_orders DROP TABLE orders;
ALTER SUBSCRIPTION sub_orders REFRESH PUBLICATION;  -- 把 pg_subscription_rel 那行置 READY

十、修改指南:要让 apply worker 更”分区表友好”时碰哪些文件

10.1 让 apply worker 自己创建 leaf(PG 16+ 实验)

apply_handle_tuple_routing 现在调用 ExecFindPartition 拿到 leaf rri 时,假设 leaf 已经存在。如果不存在就报错”no partition found”。

PG 16 引入了实验性的”自动建 leaf”功能:

文件 改动
src/backend/executor/execPartition.c ExecFindPartition 拿到 partidx 后,先检查 leaf 是否存在;不存在则直接报 no partition of relation ... found for row(src/backend/executor/execPartition.c:330)——PG 18 不会自动建叶子分区
src/backend/catalog/partition.c 提供”按 PG partitioned_table 的子树建 leaf”的 helper
src/backend/replication/logical/worker.c apply_handle_tuple_routing 在路由前 / 路由后通知 apply worker “建 leaf 的策略”

这一改动需要谨慎——会显著增加 apply worker 的写盘量(建 leaf = DDL),且和”DDL 必须订阅端手工同步”的现状冲突。

10.2 让 HASH 分区也走 routing 优化

get_partition_for_tuple 当前的 HASH 分支是直接索引(O(1)),但 RANGE/LIST 有 last_found_* 缓存。HASH 没有缓存的原因是”哈希本身已经很快”。但如果 partition key 列很多(PG 支持多列 HASH),compute_partition_hash_value 就要逐列算 hash——这时加 last-found 缓存可能反而有效。

10.3 给 Babelfish 模式加 $PARTITION 路由一致性校验

当前 Babelfish 模式下:

  • publisher 端 INSERT 走 Babelfish 的 partition function metadata 找到 leaf。
  • logical replication 走 PG 原生 routing(PG 的 pg_partitioned_table + partition_range_datum_bsearch)。
  • 两套 metadata 必须保持一致——否则 INSERT 在两端落不同 leaf。

改进方向:

  • 在 apply_handle_tuple_routing 落地后,回调一个 hook 让 Babelfish 校验”这条 INSERT 在 Babelfish metadata 下应该落哪个 leaf,和实际落到的是否一致”。
  • 不一致则 ereport(WARNING, ...) 或 pgstat_report_subscription_error。

涉及文件:

  • ~/cwork/babelfish_extensions/contrib/babelfishpg_tsql/src/pltsql_partition.c
  • ~/cwork/postgresql/src/backend/replication/logical/worker.c

十一、总结:分区表 INSERT 在逻辑复制下的关键差异点

维度 普通 INSERT 分区表 INSERT
publisher 端 get_rel_sync_entry 简单 计算祖先 + pubviaroot 决策 publish_as_relid
wire 上 relid leaf OID(leaf = 普通表) leaf OID 或 父表 OID(取决于 pubviaroot)
subscriber apply_handle_insert 直接 apply_handle_insert_internal apply_handle_tuple_routing → ExecFindPartition → apply_handle_insert_internal
元组 slot leaf schema 父表 schema 翻译成 leaf schema
relkind 检查 一次(CheckSubscriptionRelkind 在 logicalrep_rel_open) 两次(父表一次 + leaf 一次)
attr map publisher 列→subscriber 列(一次) publisher 列→subscriber 父表列→leaf 列(两层)
leaf rri 创建 tablesync 时建 apply_handle_tuple_routing 按需建(首次落到该 leaf 时)
嵌套分区 不存在 多次 FormPartitionKeyDatum + ExecFindPartition,每层用不同 PartitionDispatch
性能 单 bsearch 或 index insert 路由 + slot 翻译 + index insert,比普通表多 1–2 μs/tuple
should_apply_changes_for_rel 检查 检查表本身 检查表本身(父表),leaf 不在 pg_subscription_rel 中

PG 17 这一套已经足够成熟——挂法 A(只挂父表 + pubviaroot = true)是部署分区表逻辑复制的最佳实践:

  • publisher 端:1 个 pg_subscription_rel 行(父表)。
  • tablesync 阶段:1 个 tablesync worker。
  • 稳态:1 个 apply worker + 内部按需建 leaf ResultRelInfo。

读到这里你应该清楚一件事:分区表 INSERT 在逻辑复制下走的代码路径比普通表 INSERT 多了 2–3 个步骤,但不增加任何 worker——所有多出来的逻辑都在 apply worker 进程内完成。这就是 PG 逻辑复制能扩展到分区表场景的根本设计。


文章作者: growdu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 growdu !
这篇还发到了
今日头条 drafted
  目录