Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PostgreSQL Source Code interpretation (192)-query # 108 (sort # 1-ExecInitSort)

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This section introduces the implementation of sorting, the implementation function of sorting is ExecSort, similar to the aggregate function, there is also an initialization process ExecInitSort, this section mainly introduces the implementation of this function.

The following is the plan tree for the test SQL to perform sorting:

"select bh,c1 from t_sort order by t_sort" "psql" 2019-05-17 15 testdb 1242.494 CST, "xdb", "testdb", 1519, "[local]", 5cde5e9e.5efji15, "SELECT", 2019-05-17 15:11:26 CST,3/10,0,LOG,00000, "plan:" "{PLANNEDSTMT: commandType 1: queryId 0: hasReturning false: hasModifyingCTE false: canSetTag true: transientPlan false: dependsOnRole false: jitFlags 0: planTree {SORT: startup_cost 14142.82: total_cost 14392.82: plan_rows 100000: plan_width 66: parallel_aware false: parallel_safe true: plan_node_id 0: targetlist (...): qual: lefttree {SEQSCAN: startup_cost: total_cost 1736.00: plan_rows 100000: plan_width 66: parallel_aware false: parallel_safe true: plan_node_id 1: targetlist (. ): qual: lefttree: righttree: initPlan: extParam (b): allParam (b): scanrelid 1}: righttree: initPlan: extParam (b): allParam (b): numCols 1: sortColIdx 3: sortOperators 2990: collations 0 : nullsFirst false}: rtable (...): resultRelations: nonleafResultRelations: rootResultRelations: subplans: rewindPlanIDs (b): rowMarks: relationOids (o 278567): invalItems: paramExecTypes: utilityStmt: stmt_location 0: stmt_len 40} " , "select bh,c1 from t_sort order by t_sort. "psql" I. data structure

SortState

Sort run-time status information

/ *-* SortState information * sort runtime status information *-* / typedef struct SortState {/ / base class ScanState ss; / * its first field is NodeTag * / / do I need to randomly access the sorted output? Bool randomAccess; / * need random access to sort output? * / is there a boundary in the result set? Bool bounded; / * is the result set bounded? * / / if there is a boundary, how many tuples are needed? Int64 bound; / * if bounded, how many tuples are needed * / / have you finished sorting? Bool sort_Done; / * sort completed yet? * / do you use bounded values? What is the bounded value used by bool bounded_Done; / * value of bounded we did the sort with * / /? Is the private status of int64 bound_Done; / * value of bound we did the sort with * / / tuplesort.c void * tuplesortstate; / * private state of tuplesort.c * / / worker? Bool am_worker; / * are we a worker? * / / each worker corresponds to an entry SharedSortInfo * shared_info; / * one entry per worker * /} SortState / *-* Shared memory container for per-worker sort information * the number of shared memory containers for per-worker sorting information *-* / typedef struct SharedSortInfo {/ / worker? Int num_workers; / / sorting mechanism TuplesortInstrumentation server [flex _ ARRAY_MEMBER];} SharedSortInfo

TuplesortInstrumentation

Report the data structure of sorted statistics.

/ * Data structures for reporting sort statistics. Note that * TuplesortInstrumentation can't contain any pointers because we * sometimes put it in shared memory. * report the data structure of sorted statistics. * Note that TuplesortInstrumentation cannot contain pointers because sometimes the structure is placed in shared memory. * / typedef enum {SORT_TYPE_STILL_IN_PROGRESS = 0 SORT_TYPE_STILL_IN_PROGRESS / still in sorting SORT_TYPE_TOP_N_HEAPSORT,//TOP N heap sort SORT_TYPE_QUICKSORT,// quick sort SORT_TYPE_EXTERNAL_SORT,// external sort SORT_TYPE_EXTERNAL_MERGE// external sort merge} TuplesortMethod / / sorting method typedef enum {SORT_SPACE_TYPE_DISK,// needs to use disk SORT_SPACE_TYPE_MEMORY// use memory} TuplesortSpaceType;typedef struct TuplesortInstrumentation {/ / sort algorithm TuplesortMethod sortMethod; / * sort algorithm used * / / sort using space type TuplesortSpaceType spaceType; / * type of space spaceUsed represents * / / Space consumption (in K) long spaceUsed / * space consumption, in kB * /} TuplesortInstrumentation; II. Source code interpretation

ExecInitSort creates run-time information for sorting nodes and initializes the user subtree, which is relatively simple.

/ *-* ExecInitSort * * Creates the run-time state information for the sort node * produced by the planner and initializes its outer subtree. *-* / SortState * ExecInitSort (Sort * node, EState * estate, int eflags) {SortState * sortstate;// sort run time information SO1_printf ("ExecInitSort:% s\ n", "initializing sort node") / * * create state structure * create runtime status node structure * / sortstate = makeNode (SortState); sortstate- > ss.ps.plan = (Plan *) node; sortstate- > ss.ps.state = estate; sortstate- > ss.ps.ExecProcNode = ExecSort; / * * We must have random access to the sort output to do backward scan or * mark/restore. We also prefer to materialize the sort output if we * might be called on to rewind and replay it many times. * Random access is required for sorting output for performing back-end search or marking / storage. * at the same time, if it is possible to use rewind or replay for many times, it will tend to materialize the sorted output * / sortstate- > randomAccess = (eflags & (EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK))! = 0; sortstate- > bounded = false / / there is no boundary for the result set sortstate- > sort_Done = false;// unfinished sort sortstate- > tuplesortstate = NULL;// tuple sort status: NULL / * * Miscellaneous initialization * perform various initialization * * Sort nodes don't initialize their ExprContexts because they never call * ExecQual or ExecProject. * sorting nodes do not need to initialize ExprContexts, because sorting does not call ExecQual/ExecProject * / * initialize child nodes * initialize child nodes * * We shield the child node from the need to support REWIND, BACKWARD, or * MARK/RESTORE. * Child nodes do not need to support REWIND, BACKWARD, MARK/RESTORE * / eflags & = ~ (EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK); / / external (left) child nodes are often scan nodes, such as SeqScan outerPlanState (sortstate) = ExecInitNode (outerPlan (node), estate, eflags); / * Initialize scan slot and type. * initialize scan slot and type * / ExecCreateScanSlotFromOuterPlan (estate, & sortstate- > ss); / * Initialize return slot and type. No need to initialize projection info * because this node doesn't do projections. * initialization returns slot and type. * there is no need to initialize projection information because sorting nodes do not need projection. * / ExecInitResultTupleSlotTL (estate, & sortstate- > ss.ps); sortstate- > ss.ps.ps_ProjInfo = NULL; SO1_printf ("ExecInitSort:% s\ n", "sort node initialized"); return sortstate } / *-* ExecCreateSlotFromOuterPlan *-* / voidExecCreateScanSlotFromOuterPlan (EState * estate, ScanState * scanstate) {PlanState * outerPlan; TupleDesc tupDesc; outerPlan = outerPlanState (scanstate); tupDesc = ExecGetResultType (outerPlan); ExecInitScanTupleSlot (estate, scanstate, tupDesc) } / *-* ExecInitScanTupleSlot *-* / voidExecInitScanTupleSlot (EState * estate, ScanState * scanstate, TupleDesc tupledesc) {scanstate- > ss_ScanTupleSlot = ExecAllocTableSlot (& estate- > es_tupleTable, tupledesc); scanstate- > ps.scandesc = tupledesc;} III. Tracking analysis

N/A

IV. Reference materials

N/A

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report