您好,登錄后才能下訂單哦!
這篇文章主要介紹“PostgreSQL中獲取Tuple的分區(qū)鍵值函數(shù)是什么”,在日常操作中,相信很多人在PostgreSQL中獲取Tuple的分區(qū)鍵值函數(shù)是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL中獲取Tuple的分區(qū)鍵值函數(shù)是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
ModifyTable
通過插入、更新或刪除,將子計劃生成的行應(yīng)用到結(jié)果表。
/* ---------------- * ModifyTable node - * Apply rows produced by subplan(s) to result table(s), * by inserting, updating, or deleting. * 通過插入、更新或刪除,將子計劃生成的行應(yīng)用到結(jié)果表。 * * If the originally named target table is a partitioned table, both * nominalRelation and rootRelation contain the RT index of the partition * root, which is not otherwise mentioned in the plan. Otherwise rootRelation * is zero. However, nominalRelation will always be set, as it's the rel that * EXPLAIN should claim is the INSERT/UPDATE/DELETE target. * 如果最初命名的目標表是分區(qū)表,則nominalRelation和rootRelation都包含分區(qū)根的RT索引,計劃中沒有另外提到這個索引。 * 否則,根關(guān)系為零。但是,總是會設(shè)置名義關(guān)系,nominalRelation因為EXPLAIN應(yīng)該聲明的rel是INSERT/UPDATE/DELETE目標關(guān)系。 * * Note that rowMarks and epqParam are presumed to be valid for all the * subplan(s); they can't contain any info that varies across subplans. * 注意,rowMarks和epqParam被假定對所有子計劃有效; * 它們不能包含任何在子計劃中變化的信息。 * ---------------- */ typedef struct ModifyTable { Plan plan; CmdType operation; /* 操作類型;INSERT, UPDATE, or DELETE */ bool canSetTag; /* 是否需要設(shè)置tag?do we set the command tag/es_processed? */ Index nominalRelation; /* 用于EXPLAIN的父RT索引;Parent RT index for use of EXPLAIN */ Index rootRelation; /* 根Root RT索引(如目標為分區(qū)表);Root RT index, if target is partitioned */ bool partColsUpdated; /* 更新了層次結(jié)構(gòu)中的分區(qū)關(guān)鍵字;some part key in hierarchy updated */ List *resultRelations; /* RT索引的整型鏈表;integer list of RT indexes */ int resultRelIndex; /* 計劃鏈表中第一個resultRel的索引;index of first resultRel in plan's list */ int rootResultRelIndex; /* 分區(qū)表根索引;index of the partitioned table root */ List *plans; /* 生成源數(shù)據(jù)的計劃鏈表;plan(s) producing source data */ List *withCheckOptionLists; /* 每一個目標表均具備的WCO鏈表;per-target-table WCO lists */ List *returningLists; /* 每一個目標表均具備的RETURNING鏈表;per-target-table RETURNING tlists */ List *fdwPrivLists; /* 每一個目標表的FDW私有數(shù)據(jù)鏈表;per-target-table FDW private data lists */ Bitmapset *fdwDirectModifyPlans; /* FDW DM計劃索引位圖;indices of FDW DM plans */ List *rowMarks; /* rowMarks鏈表;PlanRowMarks (non-locking only) */ int epqParam; /* EvalPlanQual再解析使用的參數(shù)ID;ID of Param for EvalPlanQual re-eval */ OnConflictAction onConflictAction; /* ON CONFLICT action */ List *arbiterIndexes; /* 沖突仲裁器索引表;List of ON CONFLICT arbiter index OIDs */ List *onConflictSet; /* SET for INSERT ON CONFLICT DO UPDATE */ Node *onConflictWhere; /* WHERE for ON CONFLICT UPDATE */ Index exclRelRTI; /* RTI of the EXCLUDED pseudo relation */ List *exclRelTlist; /* 已排除偽關(guān)系的投影列鏈表;tlist of the EXCLUDED pseudo relation */ } ModifyTable;
ResultRelInfo
ResultRelInfo結(jié)構(gòu)體
每當更新一個現(xiàn)有的關(guān)系時,我們必須更新關(guān)系上的索引,也許還需要觸發(fā)觸發(fā)器。ResultRelInfo保存關(guān)于結(jié)果關(guān)系所需的所有信息,包括索引。
/* * ResultRelInfo * ResultRelInfo結(jié)構(gòu)體 * * Whenever we update an existing relation, we have to update indexes on the * relation, and perhaps also fire triggers. ResultRelInfo holds all the * information needed about a result relation, including indexes. * 每當更新一個現(xiàn)有的關(guān)系時,我們必須更新關(guān)系上的索引,也許還需要觸發(fā)觸發(fā)器。 * ResultRelInfo保存關(guān)于結(jié)果關(guān)系所需的所有信息,包括索引。 * * Normally, a ResultRelInfo refers to a table that is in the query's * range table; then ri_RangeTableIndex is the RT index and ri_RelationDesc * is just a copy of the relevant es_relations[] entry. But sometimes, * in ResultRelInfos used only for triggers, ri_RangeTableIndex is zero * and ri_RelationDesc is a separately-opened relcache pointer that needs * to be separately closed. See ExecGetTriggerResultRel. * 通常,ResultRelInfo是指查詢范圍表中的表; * ri_RangeTableIndex是RT索引,而ri_RelationDesc只是相關(guān)es_relations[]條目的副本。 * 但有時,在只用于觸發(fā)器的ResultRelInfos中,ri_RangeTableIndex為零(NULL), * 而ri_RelationDesc是一個需要單獨關(guān)閉單獨打開的relcache指針。 * 具體可參考ExecGetTriggerResultRel結(jié)構(gòu)體。 */ typedef struct ResultRelInfo { NodeTag type; /* result relation's range table index, or 0 if not in range table */ //RTE索引 Index ri_RangeTableIndex; /* relation descriptor for result relation */ //結(jié)果/目標relation的描述符 Relation ri_RelationDesc; /* # of indices existing on result relation */ //目標關(guān)系中索引數(shù)目 int ri_NumIndices; /* array of relation descriptors for indices */ //索引的關(guān)系描述符數(shù)組(索引視為一個relation) RelationPtr ri_IndexRelationDescs; /* array of key/attr info for indices */ //索引的鍵/屬性數(shù)組 IndexInfo **ri_IndexRelationInfo; /* triggers to be fired, if any */ //觸發(fā)的索引 TriggerDesc *ri_TrigDesc; /* cached lookup info for trigger functions */ //觸發(fā)器函數(shù)(緩存) FmgrInfo *ri_TrigFunctions; /* array of trigger WHEN expr states */ //WHEN表達式狀態(tài)的觸發(fā)器數(shù)組 ExprState **ri_TrigWhenExprs; /* optional runtime measurements for triggers */ //可選的觸發(fā)器運行期度量器 Instrumentation *ri_TrigInstrument; /* FDW callback functions, if foreign table */ //FDW回調(diào)函數(shù) struct FdwRoutine *ri_FdwRoutine; /* available to save private state of FDW */ //可用于存儲FDW的私有狀態(tài) void *ri_FdwState; /* true when modifying foreign table directly */ //直接更新FDW時為T bool ri_usesFdwDirectModify; /* list of WithCheckOption's to be checked */ //WithCheckOption鏈表 List *ri_WithCheckOptions; /* list of WithCheckOption expr states */ //WithCheckOption表達式鏈表 List *ri_WithCheckOptionExprs; /* array of constraint-checking expr states */ //約束檢查表達式狀態(tài)數(shù)組 ExprState **ri_ConstraintExprs; /* for removing junk attributes from tuples */ //用于從元組中刪除junk屬性 JunkFilter *ri_junkFilter; /* list of RETURNING expressions */ //RETURNING表達式鏈表 List *ri_returningList; /* for computing a RETURNING list */ //用于計算RETURNING鏈表 ProjectionInfo *ri_projectReturning; /* list of arbiter indexes to use to check conflicts */ //用于檢查沖突的仲裁器索引的列表 List *ri_onConflictArbiterIndexes; /* ON CONFLICT evaluation state */ //ON CONFLICT解析狀態(tài) OnConflictSetState *ri_onConflict; /* partition check expression */ //分區(qū)檢查表達式鏈表 List *ri_PartitionCheck; /* partition check expression state */ //分區(qū)檢查表達式狀態(tài) ExprState *ri_PartitionCheckExpr; /* relation descriptor for root partitioned table */ //分區(qū)root根表描述符 Relation ri_PartitionRoot; /* Additional information specific to partition tuple routing */ //額外的分區(qū)元組路由信息 struct PartitionRoutingInfo *ri_PartitionInfo; } ResultRelInfo;
PartitionRoutingInfo
PartitionRoutingInfo結(jié)構(gòu)體
分區(qū)路由信息,用于將元組路由到表分區(qū)的結(jié)果關(guān)系信息。
/* * PartitionRoutingInfo * PartitionRoutingInfo - 分區(qū)路由信息 * * Additional result relation information specific to routing tuples to a * table partition. * 用于將元組路由到表分區(qū)的結(jié)果關(guān)系信息。 */ typedef struct PartitionRoutingInfo { /* * Map for converting tuples in root partitioned table format into * partition format, or NULL if no conversion is required. * 映射,用于將根分區(qū)表格式的元組轉(zhuǎn)換為分區(qū)格式,如果不需要轉(zhuǎn)換,則轉(zhuǎn)換為NULL。 */ TupleConversionMap *pi_RootToPartitionMap; /* * Map for converting tuples in partition format into the root partitioned * table format, or NULL if no conversion is required. * 映射,用于將分區(qū)格式的元組轉(zhuǎn)換為根分區(qū)表格式,如果不需要轉(zhuǎn)換,則轉(zhuǎn)換為NULL。 */ TupleConversionMap *pi_PartitionToRootMap; /* * Slot to store tuples in partition format, or NULL when no translation * is required between root and partition. * 以分區(qū)格式存儲元組的slot.在根分區(qū)和分區(qū)之間不需要轉(zhuǎn)換時為NULL。 */ TupleTableSlot *pi_PartitionTupleSlot; } PartitionRoutingInfo;
TupleConversionMap
TupleConversionMap結(jié)構(gòu)體,用于存儲元組轉(zhuǎn)換映射信息.
typedef struct TupleConversionMap { TupleDesc indesc; /* 源行類型的描述符;tupdesc for source rowtype */ TupleDesc outdesc; /* 結(jié)果行類型的描述符;tupdesc for result rowtype */ AttrNumber *attrMap; /* 輸入字段的索引信息,0表示NULL;indexes of input fields, or 0 for null */ Datum *invalues; /* 析構(gòu)源數(shù)據(jù)的工作空間;workspace for deconstructing source */ bool *inisnull; //是否為NULL標記數(shù)組 Datum *outvalues; /* 構(gòu)造結(jié)果的工作空間;workspace for constructing result */ bool *outisnull; //null標記 } TupleConversionMap;
FormPartitionKeyDatum函數(shù)獲取Tuple的分區(qū)鍵值,返回鍵值values[]數(shù)組和是否為null標記isnull[]數(shù)組.
/* ---------------- * FormPartitionKeyDatum * Construct values[] and isnull[] arrays for the partition key * of a tuple. * 構(gòu)造values[]數(shù)組和isnull[]數(shù)組 * * pd Partition dispatch object of the partitioned table * pd 分區(qū)表的分區(qū)分發(fā)器(dispatch)對象 * * slot Heap tuple from which to extract partition key * slot 從其中提前分區(qū)鍵的heap tuple * * estate executor state for evaluating any partition key * expressions (must be non-NULL) * estate 解析分區(qū)鍵表達式(必須非NULL)的執(zhí)行器狀態(tài) * * values Array of partition key Datums (output area) * 分區(qū)鍵Datums數(shù)組(輸出參數(shù)) * isnull Array of is-null indicators (output area) * is-null標記數(shù)組(輸出參數(shù)) * * the ecxt_scantuple slot of estate's per-tuple expr context must point to * the heap tuple passed in. * estate的per-tuple上下文的ecxt_scantuple必須指向傳入的heap tuple * ---------------- */ static void FormPartitionKeyDatum(PartitionDispatch pd, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull) { ListCell *partexpr_item; int i; if (pd->key->partexprs != NIL && pd->keystate == NIL) { /* Check caller has set up context correctly */ //檢查調(diào)用者是否已正確配置內(nèi)存上下文 Assert(estate != NULL && GetPerTupleExprContext(estate)->ecxt_scantuple == slot); /* First time through, set up expression evaluation state */ //第一次進入,配置表達式解析器狀態(tài) pd->keystate = ExecPrepareExprList(pd->key->partexprs, estate); } partexpr_item = list_head(pd->keystate);//獲取分區(qū)鍵表達式狀態(tài) for (i = 0; i < pd->key->partnatts; i++)//循環(huán)遍歷分區(qū)鍵 { AttrNumber keycol = pd->key->partattrs[i];//分區(qū)鍵屬性編號 Datum datum;// typedef uintptr_t Datum;sizeof(Datum) == sizeof(void *) == 4 or 8 bool isNull;//是否null if (keycol != 0)//編號不為0 { /* Plain column; get the value directly from the heap tuple */ //扁平列,直接從堆元組中提取值 datum = slot_getattr(slot, keycol, &isNull); } else { /* Expression; need to evaluate it */ //表達式,需要解析 if (partexpr_item == NULL)//分區(qū)鍵表達式狀態(tài)為NULL,報錯 elog(ERROR, "wrong number of partition key expressions"); //獲取表達式值 datum = ExecEvalExprSwitchContext((ExprState *) lfirst(partexpr_item), GetPerTupleExprContext(estate), &isNull); //切換至下一個 partexpr_item = lnext(partexpr_item); } values[i] = datum;//賦值 isnull[i] = isNull; } if (partexpr_item != NULL)//參數(shù)設(shè)置有誤?報錯 elog(ERROR, "wrong number of partition key expressions"); } /* * slot_getattr - fetch one attribute of the slot's contents. * slot_getattr - 提取slot中的某個屬性值 */ static inline Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull) { AssertArg(attnum > 0); if (attnum > slot->tts_nvalid) slot_getsomeattrs(slot, attnum); *isnull = slot->tts_isnull[attnum - 1]; return slot->tts_values[attnum - 1]; } /* * This function forces the entries of the slot's Datum/isnull arrays to be * valid at least up through the attnum'th entry. * 這個函數(shù)強制slot的Datum/isnull數(shù)組的條目至少在attnum的第一個條目上是有效的。 */ static inline void slot_getsomeattrs(TupleTableSlot *slot, int attnum) { if (slot->tts_nvalid < attnum) slot_getsomeattrs_int(slot, attnum); } /* * slot_getsomeattrs_int - workhorse for slot_getsomeattrs() * slot_getsomeattrs_int - slot_getsomeattrs()函數(shù)的實際實現(xiàn) */ void slot_getsomeattrs_int(TupleTableSlot *slot, int attnum) { /* Check for caller errors */ //檢查調(diào)用者輸入?yún)?shù)是否有誤 Assert(slot->tts_nvalid < attnum); /* slot_getsomeattr checked */ Assert(attnum > 0); //attnum參數(shù)判斷 if (unlikely(attnum > slot->tts_tupleDescriptor->natts)) elog(ERROR, "invalid attribute number %d", attnum); /* Fetch as many attributes as possible from the underlying tuple. */ //從元組中獲取盡可能多的屬性。 slot->tts_ops->getsomeattrs(slot, attnum); /* * If the underlying tuple doesn't have enough attributes, tuple descriptor * must have the missing attributes. * 如果底層元組沒有足夠的屬性,那么元組描述符必須具有缺少的屬性。 */ if (unlikely(slot->tts_nvalid < attnum)) { slot_getmissingattrs(slot, slot->tts_nvalid, attnum); slot->tts_nvalid = attnum; } }
測試腳本如下
-- 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); insert into t_hash_partition(c1,c2,c3) VALUES(20,'HASH0','HAHS0');
啟動gdb,設(shè)置斷點
(gdb) b FormPartitionKeyDatum Breakpoint 5 at 0x6e30d2: file execPartition.c, line 1087. (gdb) b slot_getattr Breakpoint 6 at 0x489d9b: file heaptuple.c, line 1510. (gdb) c Continuing. Breakpoint 5, FormPartitionKeyDatum (pd=0x2e1bfa0, slot=0x2e1b8a0, estate=0x2e1aeb8, values=0x7fff4e2407a0, isnull=0x7fff4e240780) at execPartition.c:1087 1087 if (pd->key->partexprs != NIL && pd->keystate == NIL)
循環(huán),根據(jù)分區(qū)鍵獲取相應(yīng)的鍵值
1087 if (pd->key->partexprs != NIL && pd->keystate == NIL) (gdb) n 1097 partexpr_item = list_head(pd->keystate); (gdb) 1098 for (i = 0; i < pd->key->partnatts; i++) (gdb) 1100 AttrNumber keycol = pd->key->partattrs[i]; (gdb) 1104 if (keycol != 0) (gdb) 1107 datum = slot_getattr(slot, keycol, &isNull);
進入函數(shù)slot_getattr
(gdb) step Breakpoint 6, slot_getattr (slot=0x2e1b8a0, attnum=1, isnull=0x7fff4e240735) at heaptuple.c:1510 1510 HeapTuple tuple = slot->tts_tuple;
獲取結(jié)果,分區(qū)鍵值為20
... (gdb) p *isnull $31 = false (gdb) p slot->tts_values[attnum - 1] $32 = 20
返回到FormPartitionKeyDatum函數(shù)中
(gdb) n 1593 } (gdb) FormPartitionKeyDatum (pd=0x2e1bfa0, slot=0x2e1b8a0, estate=0x2e1aeb8, values=0x7fff4e2407a0, isnull=0x7fff4e240780) at execPartition.c:1119 1119 values[i] = datum;
完成調(diào)用
1119 values[i] = datum; (gdb) n 1120 isnull[i] = isNull; (gdb) 1098 for (i = 0; i < pd->key->partnatts; i++) (gdb) 1123 if (partexpr_item != NULL) (gdb) 1125 } (gdb) ExecFindPartition (resultRelInfo=0x2e1b108, pd=0x2e1c5b8, slot=0x2e1b8a0, estate=0x2e1aeb8) at execPartition.c:282 282 if (partdesc->nparts == 0)
到此,關(guān)于“PostgreSQL中獲取Tuple的分區(qū)鍵值函數(shù)是什么”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。