怎么理解PostgreSQL的分区表
本篇内容主要讲解“怎么理解PostgreSQL的分区表”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解PostgreSQL的分区表”吧!
在PG中,分区表通过"继承"的方式实现,这里就会存在一个问题,就是在插入数据时,PG如何确定数据应该插入到哪个目标分区?在PG中,通过函数ExecPrepareTupleRouting为路由待插入的元组做准备,主要的目的是确定元组所在的分区。
一、数据结构
ModifyTable
ModifyTable Node
通过插入、更新或删除,将子计划生成的行应用到结果表。
typedef struct ModifyTable
{
Plan plan;
CmdType operation;
bool canSetTag;
Index nominalRelation;
Index rootRelation;
bool partColsUpdated;
List *resultRelations;
int resultRelIndex;
int rootResultRelIndex;
List *plans;
List *withCheckOptionLists;
List *returningLists;
List *fdwPrivLists;
Bitmapset *fdwDirectModifyPlans;
List *rowMarks;
int epqParam;
OnConflictAction onConflictAction;
List *arbiterIndexes;
List *onConflictSet;
Node *onConflictWhere;
Index exclRelRTI;
List *exclRelTlist;
} ModifyTable;
ResultRelInfo
ResultRelInfo结构体
每当更新一个现有的关系时,我们必须更新关系上的索引,也许还需要触发触发器。ResultRelInfo保存关于结果关系所需的所有信息,包括索引。
typedef struct ResultRelInfo
{
NodeTag type;
//RTE索引
Index ri_RangeTableIndex;
//结果/目标relation的描述符
Relation ri_RelationDesc;
//目标关系中索引数目
int ri_NumIndices;
//索引的关系描述符数组(索引视为一个relation)
RelationPtr ri_IndexRelationDescs;
//索引的键/属性数组
IndexInfo **ri_IndexRelationInfo;
//触发的索引
TriggerDesc *ri_TrigDesc;
//触发器函数(缓存)
FmgrInfo *ri_TrigFunctions;
//WHEN表达式状态的触发器数组
ExprState **ri_TrigWhenExprs;
//可选的触发器运行期度量器
Instrumentation *ri_TrigInstrument;
//FDW回调函数
struct FdwRoutine *ri_FdwRoutine;
//可用于存储FDW的私有状态
void *ri_FdwState;
//直接更新FDW时为T
bool ri_usesFdwDirectModify;
//WithCheckOption链表
List *ri_WithCheckOptions;
//WithCheckOption表达式链表
List *ri_WithCheckOptionExprs;
//约束检查表达式状态数组
ExprState **ri_ConstraintExprs;
//用于从元组中删除junk属性
JunkFilter *ri_junkFilter;
//RETURNING表达式链表
List *ri_returningList;
//用于计算RETURNING链表
ProjectionInfo *ri_projectReturning;
//用于检查冲突的仲裁器索引的列表
List *ri_onConflictArbiterIndexes;
//ON CONFLICT解析状态
OnConflictSetState *ri_onConflict;
//分区检查表达式链表
List *ri_PartitionCheck;
//分区检查表达式状态
ExprState *ri_PartitionCheckExpr;
//分区root根表描述符
Relation ri_PartitionRoot;
//额外的分区元组路由信息
struct PartitionRoutingInfo *ri_PartitionInfo;
} ResultRelInfo;
PartitionRoutingInfo
PartitionRoutingInfo结构体
分区路由信息,用于将元组路由到表分区的结果关系信息。
typedef struct PartitionRoutingInfo
{
TupleConversionMap *pi_RootToPartitionMap;
TupleConversionMap *pi_PartitionToRootMap;
TupleTableSlot *pi_PartitionTupleSlot;
} PartitionRoutingInfo;
TupleConversionMap
TupleConversionMap结构体,用于存储元组转换映射信息.
typedef struct TupleConversionMap
{
TupleDesc indesc;
TupleDesc outdesc;
AttrNumber *attrMap;
Datum *invalues;
bool *inisnull; //是否为NULL标记数组
Datum *outvalues;
bool *outisnull; //null标记
} TupleConversionMap;
二、源码解读
ExecPrepareTupleRouting函数确定要插入slot中的tuple所属的分区,同时修改mtstate和estate等相关信息,为后续实际的插入作准备。
static TupleTableSlot *
ExecPrepareTupleRouting(ModifyTableState *mtstate,
EState *estate,
PartitionTupleRouting *proute,
ResultRelInfo *targetRelInfo,
TupleTableSlot *slot)
{
ModifyTable *node;//ModifyTable节点
int partidx;//分区索引
ResultRelInfo *partrel;//ResultRelInfo结构体指针(数组)
HeapTuple tuple;//元组
partidx = ExecFindPartition(targetRelInfo,
proute->partition_dispatch_info,
slot,
estate);
Assert(partidx >= 0 && partidx < proute->num_partitions);
partrel = proute->partitions[partidx];
if (partrel == NULL)
partrel = ExecInitPartitionInfo(mtstate, targetRelInfo,
proute, estate,
partidx);
if (!partrel->ri_PartitionReadyForRouting)
{
//验证分区是否可用于INSERT
CheckValidResultRel(partrel, CMD_INSERT);
//设置将元组路由到分区所需的信息。
ExecInitRoutingInfo(mtstate, estate, proute, partrel, partidx);
}
estate->es_result_relation_info = partrel;
//从给定的slot中获取heap tuple
tuple = ExecMaterializeSlot(slot);
if (mtstate->mt_transition_capture != NULL)
{
if (partrel->ri_TrigDesc &&
partrel->ri_TrigDesc->trig_insert_before_row)
{
mtstate->mt_transition_capture->tcs_original_insert_tuple = NULL;
mtstate->mt_transition_capture->tcs_map =
TupConvMapForLeaf(proute, targetRelInfo, partidx);
}
else
{
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);
}
ConvertPartitionTupleSlot(proute->parent_child_tupconv_maps[partidx],
tuple,
proute->partition_tuple_slot,
&slot);
//如为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;
}
HeapTuple
ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
{
Assert(slot != NULL);
Assert(!TTS_EMPTY(slot));
//物化元组,以便slot“拥有”它(如要求)。
if (materialize)
slot->tts_ops->materialize(slot);
if (slot->tts_ops->get_heap_tuple == NULL)
{
if (shouldFree)
*shouldFree = true;
return slot->tts_ops->copy_heap_tuple(slot);//返回slot拷贝
}
else
{
if (shouldFree)
*shouldFree = false;
return slot->tts_ops->get_heap_tuple(slot);//直接返回slot
}
}
三、跟踪分析
测试脚本如下
-- Hash Partition
drop table if exists t_hash_partition;
create table t_hash_partition (c1 int not null,c2 varchar(40),c3 varchar(40)) partition by hash(c1);
create table t_hash_partition_1 partition of t_hash_partition for values with (modulus 6,remainder 0);
create table t_hash_partition_2 partition of t_hash_partition for values with (modulus 6,remainder 1);
create table t_hash_partition_3 partition of t_hash_partition for values with (modulus 6,remainder 2);
create table t_hash_partition_4 partition of t_hash_partition for values with (modulus 6,remainder 3);
create table t_hash_partition_5 partition of t_hash_partition for values with (modulus 6,remainder 4);
create table t_hash_partition_6 partition of t_hash_partition for values with (modulus 6,remainder 5);
-- delete from t_hash_partition where c1 = 0;
insert into t_hash_partition(c1,c2,c3) VALUES(0,'HASH0','HAHS0');
启动gdb,设置断点,进入ExecPrepareTupleRouting
(gdb) b ExecPrepareTupleRouting
Breakpoint 1 at 0x710b1e: file nodeModifyTable.c, line 1712.
(gdb) c
Continuing.
Breakpoint 1, ExecPrepareTupleRouting (mtstate=0x1e4de60, estate=0x1e4daf8, proute=0x1e4eb48, targetRelInfo=0x1e4dd48,
slot=0x1e4e4e0) at nodeModifyTable.c:1712
1712 partidx = ExecFindPartition(targetRelInfo,
查看函数调用栈
ExecPrepareTupleRouting在ExecModifyTable Node中被调用,为后续的插入作准备.
(gdb) bt
#0 ExecPrepareTupleRouting (mtstate=0x1e4de60, estate=0x1e4daf8, proute=0x1e4eb48, targetRelInfo=0x1e4dd48, slot=0x1e4e4e0)
at nodeModifyTable.c:1712
#1 0x0000000000711602 in ExecModifyTable (pstate=0x1e4de60) at nodeModifyTable.c:2157
#2 0x00000000006e4c30 in ExecProcNodeFirst (node=0x1e4de60) at execProcnode.c:445
#3 0x00000000006d9974 in ExecProcNode (node=0x1e4de60) at ../../../class="lazy" data-src/include/executor/executor.h:237
#4 0x00000000006dc22d in ExecutePlan (estate=0x1e4daf8, planstate=0x1e4de60, use_parallel_mode=false,
operation=CMD_INSERT, sendTuples=false, numberTuples=0, direction=ForwardScanDirection, dest=0x1e67e90,
execute_once=true) at execMain.c:1723
#5 0x00000000006d9f5c in standard_ExecutorRun (queryDesc=0x1e39d68, direction=ForwardScanDirection, count=0,
execute_once=true) at execMain.c:364
#6 0x00000000006d9d7f in ExecutorRun (queryDesc=0x1e39d68, direction=ForwardScanDirection, count=0, execute_once=true)
at execMain.c:307
#7 0x00000000008cbdb3 in ProcessQuery (plan=0x1e67d18,
sourceText=0x1d60ec8 "insert into t_hash_partition(c1,c2,c3) VALUES(0,'HASH0','HAHS0');", params=0x0, queryEnv=0x0,
dest=0x1e67e90, completionTag=0x7ffdcf148b20 "") at pquery.c:161
#8 0x00000000008cd6f9 in PortalRunMulti (portal=0x1dc6538, isTopLevel=true, setHoldSnapshot=false, dest=0x1e67e90,
altdest=0x1e67e90, completionTag=0x7ffdcf148b20 "") at pquery.c:1286
#9 0x00000000008cccb9 in PortalRun (portal=0x1dc6538, count=9223372036854775807, isTopLevel=true, run_once=true,
dest=0x1e67e90, altdest=0x1e67e90, completionTag=0x7ffdcf148b20 "") at pquery.c:799
#10 0x00000000008c6b1e in exec_simple_query (
query_string=0x1d60ec8 "insert into t_hash_partition(c1,c2,c3) VALUES(0,'HASH0','HAHS0');") at postgres.c:1145
#11 0x00000000008cae70 in PostgresMain (argc=1, argv=0x1d8aba8, dbname=0x1d8aa10 "testdb", username=0x1d5dba8 "xdb")
at postgres.c:4182
找到该元组所在的分区
(gdb) n
1716 Assert(partidx >= 0 && partidx < proute->num_partitions);
(gdb) p partidx
$1 = 2
获取与所选分区对应的ResultRelInfo;如果还没有,则初始化
(gdb) n
1722 partrel = proute->partitions[partidx];
(gdb)
1723 if (partrel == NULL)
(gdb) p *partrel
Cannot access memory at address 0x0
(gdb) n
1724 partrel = ExecInitPartitionInfo(mtstate, targetRelInfo,
初始化后的partrel
(gdb) p *partrel
$2 = {type = T_ResultRelInfo, ri_RangeTableIndex = 1, ri_RelationDesc = 0x1e7c940, ri_NumIndices = 0,
ri_IndexRelationDescs = 0x0, ri_IndexRelationInfo = 0x0, ri_TrigDesc = 0x0, ri_TrigFunctions = 0x0,
ri_TrigWhenExprs = 0x0, ri_TrigInstrument = 0x0, ri_FdwRoutine = 0x0, ri_FdwState = 0x0, ri_usesFdwDirectModify = false,
ri_WithCheckOptions = 0x0, ri_WithCheckOptionExprs = 0x0, ri_ConstraintExprs = 0x0, ri_junkFilter = 0x0,
ri_returningList = 0x0, ri_projectReturning = 0x0, ri_onConflictArbiterIndexes = 0x0, ri_onConflict = 0x0,
ri_PartitionCheck = 0x1e4f538, ri_PartitionCheckExpr = 0x0, ri_PartitionRoot = 0x1e7c2f8,
ri_PartitionReadyForRouting = true}
目标分区描述符-->t_hash_partition_3
(gdb) p *partrel->ri_RelationDesc
$3 = {rd_node = {spcNode = 1663, dbNode = 16402, relNode = 16995}, rd_smgr = 0x1e34510, rd_refcnt = 1, rd_backend = -1,
rd_islocaltemp = false, rd_isnailed = false, rd_isvalid = true, rd_indexvalid = 0 '\000', rd_statvalid = false,
rd_createSubid = 0, rd_newRelfilenodeSubid = 0, rd_rel = 0x1e7c1e0, rd_att = 0x1e7cb58, rd_id = 16995, rd_lockInfo = {
lockRelId = {relId = 16995, dbId = 16402}}, rd_rules = 0x0, rd_rulescxt = 0x0, trigdesc = 0x0, rd_rsdesc = 0x0,
rd_fkeylist = 0x0, rd_fkeyvalid = false, rd_partkeycxt = 0x0, rd_partkey = 0x0, rd_pdcxt = 0x0, rd_partdesc = 0x0,
rd_partcheck = 0x1e7aa30, rd_indexlist = 0x0, rd_oidindex = 0, rd_pkindex = 0, rd_replidindex = 0, rd_statlist = 0x0,
rd_indexattr = 0x0, rd_projindexattr = 0x0, rd_keyattr = 0x0, rd_pkattr = 0x0, rd_idattr = 0x0, rd_projidx = 0x0,
rd_pubactions = 0x0, rd_options = 0x0, rd_index = 0x0, rd_indextuple = 0x0, rd_amhandler = 0, rd_indexcxt = 0x0,
rd_amroutine = 0x0, rd_opfamily = 0x0, rd_opcintype = 0x0, rd_support = 0x0, rd_supportinfo = 0x0, rd_indoption = 0x0,
rd_indexprs = 0x0, rd_indpred = 0x0, rd_exclops = 0x0, rd_exclprocs = 0x0, rd_exclstrats = 0x0, rd_amcache = 0x0,
rd_indcollation = 0x0, rd_fdwroutine = 0x0, rd_toastoid = 0, pgstat_info = 0x1de40b0}
------------------
testdb=# select oid,relname from pg_class where oid=16995;
oid | relname
-------+--------------------
16995 | t_hash_partition_3
(1 row)
-----------------
该分区是可路由的
(gdb) p partrel->ri_PartitionReadyForRouting
$4 = true
设置estate变量(让它看起来像是插入到分区中)/物化tuple
(gdb) n
1751 estate->es_result_relation_info = partrel;
(gdb)
1754 tuple = ExecMaterializeSlot(slot);
(gdb)
1760 if (mtstate->mt_transition_capture != NULL)
(gdb) p tuple
$5 = (HeapTuple) 0x1e4f4e0
(gdb) p *tuple
$6 = {t_len = 40, t_self = {ip_blkid = {bi_hi = 65535, bi_lo = 65535}, ip_posid = 0}, t_tableOid = 0, t_data = 0x1e4f4f8}
(gdb)
(gdb) p *tuple->t_data
$7 = {t_choice = {t_heap = {t_xmin = 160, t_xmax = 4294967295, t_field3 = {t_cid = 2249, t_xvac = 2249}}, t_datum = {
datum_len_ = 160, datum_typmod = -1, datum_typeid = 2249}}, t_ctid = {ip_blkid = {bi_hi = 65535, bi_lo = 65535},
ip_posid = 0}, t_infomask2 = 3, t_infomask = 2, t_hoff = 24 '\030', t_bits = 0x1e4f50f ""}
mtstate->mt_transition_capture 为NULL,无需处理相关信息
(gdb) p mtstate->mt_transition_capture
$8 = (struct TransitionCaptureState *) 0x0
1783 if (mtstate->mt_oc_transition_capture != NULL)
(gdb)
如需要,转换元组
1792 ConvertPartitionTupleSlot(proute->parent_child_tupconv_maps[partidx],
(gdb)
1798 Assert(mtstate != NULL);
(gdb)
1799 node = (ModifyTable *) mtstate->ps.plan;
(gdb) p *mtstate
$9 = {ps = {type = T_ModifyTableState, plan = 0x1e59838, state = 0x1e4daf8, ExecProcNode = 0x711056 <ExecModifyTable>,
ExecProcNodeReal = 0x711056 <ExecModifyTable>, instrument = 0x0, worker_instrument = 0x0, worker_jit_instrument = 0x0,
qual = 0x0, lefttree = 0x0, righttree = 0x0, initPlan = 0x0, subPlan = 0x0, chgParam = 0x0,
ps_ResultTupleSlot = 0x1e4ede8, ps_ExprContext = 0x0, ps_ProjInfo = 0x0, scandesc = 0x0}, operation = CMD_INSERT,
canSetTag = true, mt_done = false, mt_plans = 0x1e4e078, mt_nplans = 1, mt_whichplan = 0, resultRelInfo = 0x1e4dd48,
rootResultRelInfo = 0x0, mt_arowmarks = 0x1e4e098, mt_epqstate = {estate = 0x0, planstate = 0x0, origslot = 0x1e4e4e0,
plan = 0x1e59588, arowMarks = 0x0, epqParam = 0}, fireBSTriggers = false, mt_existing = 0x0, mt_excludedtlist = 0x0,
mt_conflproj = 0x0, mt_partition_tuple_routing = 0x1e4eb48, mt_transition_capture = 0x0, mt_oc_transition_capture = 0x0,
mt_per_subplan_tupconv_maps = 0x0}
返回slot,完成调用
(gdb) n
1800 if (node->onConflictAction == ONCONFLICT_UPDATE)
(gdb)
1810 return slot;
(gdb)
1811 }
到此,相信大家对“怎么理解PostgreSQL的分区表”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341