天天看點

openGauss核心分析(八):執行算子探究

執行引擎位于優化器和存儲引擎之間,負責将資料從存儲引擎讀取出來,根據計劃将資料處理加工傳回給用戶端。執行器接收到的指令就是優化器應對SQL查詢而翻譯出來的關系代數運算符所組成的執行樹,如下圖所示

openGauss核心分析(八):執行算子探究

圖中每一個方塊代表一個具體關系運算代數符,我們稱之為算子,每個算子有統一的接口,從下層的一個或者多個算子獲得輸入,然後将運算結果傳回給上層算子。整個查詢執行過程主要是兩個流,驅動流和資料流。

Ÿ向上的流代表資料流,是指下層算子将資料傳回給上層算子的過程,這是一個從下至上、從葉節點到跟節點的過程。在openGauss中,所有的葉子節點都是表資料掃描算子,這些節點是所有計算的資料源頭。資料從葉子節點,通過逐層計算,然後從根節點傳回給使用者

Ÿ向下的流代表控制流,是指上層算子驅動下層算子執行的過程,這是一個從上至下、由根節點到葉節點的過程。從代碼層面來看,即上層算子會根據需要調用下層算子的函數接口,去擷取下層算子的輸入。驅動流是從根節點逐層傳遞到葉子節點。

執行器的整體目标就是在每一個由優化器建構出來的執行樹上,通過控制流驅動資料流在執行樹上高效的流動,其流動的速度決定了執行器的處理效率。

算子分類

關系資料庫本身是對關系集合Relation的運算操作,執行引擎作為運算的控制邏輯主體也是圍繞着關系運算來實作的,在傳統資料庫實作理論中,算子的分類可以分成以下幾類:

掃描算子(Scan Plan Node)

掃描節點負責從底層資料來源抽取資料,資料來源可能是來自檔案系統,也可能來自網絡(分布式查詢)。一般而言掃描節點都位于執行樹的葉子節點,作為執行樹PlanTree的資料輸入來源。

關鍵特征:輸入資料、葉子節點、表達式過濾

openGauss核心分析(八):執行算子探究

控制算子(Control Plan Node)

控制算子一般不映射代數運算符,通常是為了執行器完成一些特殊的流程引入的算子。

關鍵特征:用于控制資料流程

openGauss核心分析(八):執行算子探究

物化算子(Materialize Plan Node)

物化算子一般指算法要求,在做算子邏輯處理的時候,要求把下層的資料進行緩存處理,因為對于下層算子傳回的資料量不可提前預知,是以需要在算法上考慮資料無法全部放置到記憶體的情況。

關鍵特征:需要掃描所有資料之後才傳回

openGauss核心分析(八):執行算子探究

連接配接算子(Join Plan Node)

這類算子是為了應對資料庫中最常見的關聯操作。

關鍵特征:多個輸入

按照實作方式有3種關聯算子。

openGauss核心分析(八):執行算子探究

按照連接配接類型有6種關聯算子

openGauss核心分析(八):執行算子探究

下面重點分析Seqscan算子的代碼流程。

Seqscan算子

openGauss核心分析(八):執行算子探究

ExecInitSeqScan

ExecInitSeqScan函數初始化SeqScan狀态節點,負責節點狀态結構構造

SeqScanState* ExecInitSeqScan(SeqScan* node, EState* estate, int eflags)

{

……

/*

* create state structure

*/

SeqScanState* scanstate = makeNode(SeqScanState); // SeqScan狀态節點

scanstate->ps.plan = (Plan*)node;

scanstate->ps.state = estate;

scanstate->isPartTbl = node->isPartTbl;

scanstate->currentSlot = 0;

scanstate->partScanDirection = node->partScanDirection;

scanstate->rangeScanInRedis = {false,0,0};

……

/*

* tuple table initialization

*/

InitScanRelation(scanstate, estate, eflags); // 初始化掃描表

……

/*

* initialize scan relation

*/

InitSeqNextMtd(node, scanstate); //設定擷取元組的函數

……

return scanstate;

}

InitSeqNextMtd函數設定擷取元組的函數為SeqNext

static inline void InitSeqNextMtd(SeqScan* node, SeqScanState* scanstate)

{

if (!node->tablesample) {

scanstate->ScanNextMtd = SeqNext;

……

}

ExecSeqScan

ExecutePlan函數循環調用ExecProcNode擷取元組

static void ExecutePlan(EState *estate, PlanState *planstate, CmdType operation, bool sendTuples, long numberTuples,

ScanDirection direction, DestReceiver *dest, JitExec::JitContext* motJitContext)

{

TupleTableSlot *slot = NULL;

long current_tuple_count = 0;//初始化

……

/*

* Loop until we've processed the proper number of tuples from the plan.

*/

for (;;) { //循環調用ExecProcNode

……

if (unlikely(recursive_early_stop)) {

slot = NULL;

} else if (motJitContext && !IS_PGXC_COORDINATOR && JitExec::IsMotCodegenEnabled()) {

// MOT LLVM

int scanEnded = 0;

if (!motFinishedExecution) {

// previous iteration has not signaled end of scan

slot = planstate->ps_ResultTupleSlot;

uint64_t tuplesProcessed = 0;

int rc = JitExec::JitExecQuery(

motJitContext, estate->es_param_list_info, slot, &tuplesProcessed, &scanEnded);

if (scanEnded || (tuplesProcessed == 0) || (rc != 0)) {

// raise flag so that next round we will bail out (current tuple still must be reported to user)

motFinishedExecution = true;

}

} else {

(void)ExecClearTuple(slot);

}

} else {

slot = ExecProcNode(planstate);// 調用ExecProcNode

}

…….

/*

* if the tuple is null, then we assume there is nothing more to

* process so we just end the loop...

*/

if (TupIsNull(slot)) {// 元組為空即中止循環

if(!is_saved_recursive_union_plan_nodeid) {

break;

}

ExecEarlyFreeBody(planstate);

break;

}

……

{

(*dest->receiveSlot)(slot, dest); //簡單select語句調用printtup函數

}

……

/*

* check our tuple count.. if we've processed the proper number then

* quit, else loop again and process more tuples. Zero numberTuples

* means no limit.

*/

current_tuple_count++;// 計數元組數

if (numberTuples == current_tuple_count) {

break;

}

}

……

}

ExecProcNode函數根據nodeTag執行g_execProcFuncTable對應的函數

TupleTableSlot* ExecProcNode(PlanState* node)

{

TupleTableSlot* result = NULL;

……

{

int index = (int)(nodeTag(node))-T_ResultState;

Assert(index >= 0 && index

result = g_execProcFuncTable[index](node);

}

……

return result;

}

ExecProcFuncType g_execProcFuncTable[] = {

ExecResultWrap,

……

ExecSeqScanWrap,

ExecIndexScanWrap,

ExecIndexOnlyScanWrap,

…..

};

ExecSeqScanWrap->ExecSeqScan->ExecScan->ExecScanFetch,ExecScanFetch函數回調SeqNext擷取元組

static TupleTableSlot* ExecScanFetch(ScanState* node, ExecScanAccessMtd access_mtd, ExecScanRecheckMtd recheck_mtd)

{

……

/*

* Run the node-type-specific access method function to get the next tuple

*/

return (*access_mtd)(node); //回調SeqNext

}

ExecEndSeqScan

ExecEndSeqScan完成清理工作。

繼續閱讀