From 777d935e398d2fa32532b3e55c8d79853ee134b0 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Tue, 20 Sep 2022 15:28:40 +0800 Subject: [PATCH 01/12] fix #62414445 btree: Support parallel index scans. #62414492 add index cost model in optimizer add smp thread cots add executor support for parallel index scans Please enter the commit message for your changes. Lines starting --- src/common/backend/utils/misc/guc/guc_sql.cpp | 14 + src/gausskernel/optimizer/path/costsize.cpp | 7 +- src/gausskernel/optimizer/path/indxpath.cpp | 36 +- src/gausskernel/optimizer/util/pathnode.cpp | 4 +- src/gausskernel/process/stream/streamCore.cpp | 63 ++++ src/gausskernel/runtime/executor/execMain.cpp | 2 + .../runtime/executor/nodeIndexscan.cpp | 27 +- .../storage/access/hbstore/hbindex_am.cpp | 15 +- .../storage/access/index/indexam.cpp | 54 ++- .../storage/access/nbtree/nbtree.cpp | 250 ++++++++++++++ .../storage/access/nbtree/nbtsearch.cpp | 308 +++++++++++++++--- .../storage/access/nbtree/nbtutils.cpp | 4 + src/include/access/genam.h | 5 +- src/include/access/hbindex_am.h | 4 +- src/include/access/nbtree.h | 16 +- src/include/access/relscan.h | 10 + src/include/c.h | 2 + src/include/distributelayer/streamCore.h | 22 ++ src/include/executor/node/nodeIndexscan.h | 1 + src/include/knl/knl_thread.h | 10 + src/include/optimizer/pathnode.h | 2 +- .../regress/input/parallel_index_scan.source | 35 ++ .../regress/output/parallel_index_scan.source | 92 ++++++ 23 files changed, 917 insertions(+), 66 deletions(-) create mode 100644 src/test/regress/input/parallel_index_scan.source create mode 100644 src/test/regress/output/parallel_index_scan.source diff --git a/src/common/backend/utils/misc/guc/guc_sql.cpp b/src/common/backend/utils/misc/guc/guc_sql.cpp index 8a989d820..79d21e17e 100755 --- a/src/common/backend/utils/misc/guc/guc_sql.cpp +++ b/src/common/backend/utils/misc/guc/guc_sql.cpp @@ -2584,6 +2584,20 @@ static void InitSqlConfigureNamesReal() NULL, NULL, NULL}, + {{"smp_thread_cost", + PGC_USERSET, + NODE_ALL, + QUERY_TUNING_COST, + gettext_noop("Sets the planner's estimate of the cost of a " + "smp thread cost."), + NULL}, + &u_sess->opt_cxt.smp_thread_cost, + DEFAULT_SMP_THREAD_COST, + 0, + DBL_MAX, + NULL, + NULL, + NULL}, {{"cpu_tuple_cost", PGC_USERSET, NODE_ALL, diff --git a/src/gausskernel/optimizer/path/costsize.cpp b/src/gausskernel/optimizer/path/costsize.cpp index caa2c27c0..a263fb486 100755 --- a/src/gausskernel/optimizer/path/costsize.cpp +++ b/src/gausskernel/optimizer/path/costsize.cpp @@ -998,6 +998,8 @@ void cost_index(IndexPath* path, PlannerInfo* root, double loop_count) double pages_fetched; bool ispartitionedindex = path->indexinfo->rel->isPartitionedTable; bool disable_path = false; + int dop = SET_DOP(path->path.dop); + if (enable_parametrized_path(root, baserel, (Path*)path) || (!u_sess->attr.attr_sql.enable_indexscan && !indexonly) || (!u_sess->attr.attr_sql.enable_indexonlyscan && indexonly)) { @@ -1219,7 +1221,7 @@ void cost_index(IndexPath* path, PlannerInfo* root, double loop_count) */ csquared = indexCorrelation * indexCorrelation; - run_cost += max_IO_cost + csquared * (min_IO_cost - max_IO_cost); + run_cost += (max_IO_cost + csquared * (min_IO_cost - max_IO_cost)) / dop; ereport(DEBUG2, (errmodule(MOD_OPT), @@ -1253,7 +1255,8 @@ void cost_index(IndexPath* path, PlannerInfo* root, double loop_count) else cpu_per_tuple = u_sess->attr.attr_sql.cpu_tuple_cost + qpqual_cost.per_tuple; - run_cost += cpu_per_tuple * tuples_fetched; + run_cost += u_sess->opt_cxt.smp_thread_cost * (dop - 1); + run_cost += cpu_per_tuple * tuples_fetched / dop; ereport(DEBUG2, (errmodule(MOD_OPT), diff --git a/src/gausskernel/optimizer/path/indxpath.cpp b/src/gausskernel/optimizer/path/indxpath.cpp index d1646ac7f..339101eae 100755 --- a/src/gausskernel/optimizer/path/indxpath.cpp +++ b/src/gausskernel/optimizer/path/indxpath.cpp @@ -914,7 +914,8 @@ static List* build_index_paths(PlannerInfo* root, RelOptInfo* rel, IndexOptInfo* bool index_is_ordered = false; bool index_only_scan = false; int indexcol; - + bool can_parallel = IS_STREAM_PLAN && (u_sess->opt_cxt.query_dop > 1) && (ST_BITMAPSCAN != scantype) && + (!rel->isPartitionedTable); /* * Check that index supports the desired scan type(s) */ @@ -1072,6 +1073,22 @@ static List* build_index_paths(PlannerInfo* root, RelOptInfo* rel, IndexOptInfo* upper_params, loop_count); result = lappend(result, ipath); + if (can_parallel) { + ipath = create_index_path(root, + index, + index_clauses, + clause_columns, + NIL, + NIL, + useful_pathkeys, + index_is_ordered ? ForwardScanDirection : NoMovementScanDirection, + index_only_scan, + outer_relids, + upper_params, + loop_count, + u_sess->opt_cxt.query_dop); + result = lappend(result, ipath); + } } /* @@ -1097,6 +1114,23 @@ static List* build_index_paths(PlannerInfo* root, RelOptInfo* rel, IndexOptInfo* upper_params, loop_count); result = lappend(result, ipath); + + if (can_parallel) { + ipath = create_index_path(root, + index, + index_clauses, + clause_columns, + NIL, + NIL, + useful_pathkeys, + BackwardScanDirection, + index_only_scan, + outer_relids, + upper_params, + loop_count, + u_sess->opt_cxt.query_dop); + result = lappend(result, ipath); + } } } diff --git a/src/gausskernel/optimizer/util/pathnode.cpp b/src/gausskernel/optimizer/util/pathnode.cpp index 969bdcd74..8a5a43f98 100755 --- a/src/gausskernel/optimizer/util/pathnode.cpp +++ b/src/gausskernel/optimizer/util/pathnode.cpp @@ -2356,7 +2356,7 @@ bool is_pwj_path(Path* pwjpath) */ IndexPath* create_index_path(PlannerInfo* root, IndexOptInfo* index, List* indexclauses, List* indexclausecols, List* indexorderbys, List* indexorderbycols, List* pathkeys, ScanDirection indexscandir, bool indexonly, - Relids required_outer, Bitmapset *upper_params, double loop_count) + Relids required_outer, Bitmapset *upper_params, double loop_count, int dop) { IndexPath* pathnode = makeNode(IndexPath); RelOptInfo* rel = index->rel; @@ -2370,7 +2370,7 @@ IndexPath* create_index_path(PlannerInfo* root, IndexOptInfo* index, List* index pathnode->path.pathtarget = rel->reltarget; pathnode->path.param_info = get_baserel_parampathinfo(root, rel, required_outer, upper_params); pathnode->path.pathkeys = pathkeys; - + pathnode->path.dop = dop; /* Convert clauses to indexquals the executor can handle */ expand_indexqual_conditions(index, indexclauses, indexclausecols, &indexquals, &indexqualcols); diff --git a/src/gausskernel/process/stream/streamCore.cpp b/src/gausskernel/process/stream/streamCore.cpp index 375a888d8..d5352986d 100755 --- a/src/gausskernel/process/stream/streamCore.cpp +++ b/src/gausskernel/process/stream/streamCore.cpp @@ -56,6 +56,7 @@ #include "utils/combocid.h" #include "vecexecutor/vecstream.h" #include "access/hash.h" +#include "access/nbtree.h" #include "pgstat.h" #include "tcop/tcopprot.h" #include "distributelayer/streamCore.h" @@ -1904,6 +1905,68 @@ void StreamNodeGroup::MarkRecursiveVfdInvalid() } } +void StreamNodeGroup::BuildStreamDesc(const uint64& queryId, Plan* node) +{ + StreamKey streamKey; + streamKey.queryId = queryId; + streamKey.planNodeId = node->plan_node_id; + + void* parallelDesc = NULL; + + switch (nodeTag(node)) { + case T_IndexScan: + parallelDesc = palloc0(sizeof(ParallelIndexScanDescData)); + ((ParallelIndexScanDescData*)parallelDesc)->ps_indexid = ((IndexScan*)node)->indexid; + ((ParallelIndexScanDescData*)parallelDesc)->ps_relid = ((IndexScan*)node)->scan.scanrelid; + ((ParallelIndexScanDescData*)parallelDesc)->ps_btpscan = btbuildparallelscan(); + break; + default: + break; + } + + if (!parallelDesc) { + return; + } + + m_streamDesc.insert({streamKey, parallelDesc}); +} + +void StreamNodeGroup::DestroyStreamDesc(const uint64& queryId, Plan* node) +{ + StreamKey streamKey; + streamKey.queryId = queryId; + streamKey.planNodeId = node->plan_node_id; + + std::unordered_map::iterator iter; + + switch (nodeTag(node)) { + case T_IndexScan: + iter = m_streamDesc.find(streamKey); + if (m_streamDesc.end() == iter) { + return; + } + if (iter->second) { + if (((ParallelIndexScanDescData*)iter->second)->ps_btpscan) { + delete ((ParallelIndexScanDescData*)iter->second)->ps_btpscan; + } + pfree(iter->second); + } + m_streamDesc.erase(streamKey); + break; + default: + break; + } +} + +void* StreamNodeGroup::GetParalleDesc(const uint64& queryId, const uint64& planNodeId) +{ + StreamKey key; + key.queryId = queryId; + key.planNodeId = planNodeId; + std::unordered_map::iterator iter = m_streamDesc.find(key); + return (m_streamDesc.end() == iter) ? NULL : iter->second; +} + #ifndef ENABLE_MULTIPLE_NODES bool InitStreamObject(PlannedStmt* planStmt) { diff --git a/src/gausskernel/runtime/executor/execMain.cpp b/src/gausskernel/runtime/executor/execMain.cpp index a47d07511..89ad637bb 100755 --- a/src/gausskernel/runtime/executor/execMain.cpp +++ b/src/gausskernel/runtime/executor/execMain.cpp @@ -1522,6 +1522,7 @@ void InitPlan(QueryDesc *queryDesc, int eflags) (IS_SPQ_COORDINATOR && list_nth_int(plannedstmt->subplan_ids, i - 1) != 0) || #endif plannedstmt->planTree->plan_node_id == list_nth_int(plannedstmt->subplan_ids, i - 1))) { + estate->es_under_subplan = true; subplanstate = ExecInitNode(subplan, estate, sp_eflags); @@ -1552,6 +1553,7 @@ void InitPlan(QueryDesc *queryDesc, int eflags) plan->initPlan = plannedstmt->initPlan; estate->es_subplan_ids = plannedstmt->subplan_ids; } + planstate = ExecInitNode(plan, estate, eflags); if (estate->pruningResult) { diff --git a/src/gausskernel/runtime/executor/nodeIndexscan.cpp b/src/gausskernel/runtime/executor/nodeIndexscan.cpp index 0d5684ea1..fc5ccd305 100644 --- a/src/gausskernel/runtime/executor/nodeIndexscan.cpp +++ b/src/gausskernel/runtime/executor/nodeIndexscan.cpp @@ -259,6 +259,8 @@ void ExecReScanIndexScan(IndexScanState* node) scan_handler_idx_rescan( node->iss_ScanDesc, node->iss_ScanKeys, node->iss_NumScanKeys, node->iss_OrderByKeys, node->iss_NumOrderByKeys); + scan_handler_idx_rescan_parallel(node->iss_ScanDesc); + ExecScanReScan(&node->ss); } @@ -463,8 +465,12 @@ void ExecEndIndexScan(IndexScanState* node) /* * close the index relation (no-op if we didn't open it) */ - if (index_scan_desc) + if (index_scan_desc) { scan_handler_idx_endscan(index_scan_desc); + if (WorkerThreadAmI() && node->ss.ps.plan->dop > 1) { + u_sess->stream_cxt.global_obj->DestroyStreamDesc(node->ss.ps.state->es_plannedstmt->queryId, node->ss.ps.plan); + } + } /* * close the index relation (no-op if we didn't open it) @@ -599,14 +605,25 @@ void ExecInitIndexRelation(IndexScanState* node, EState* estate, int eflags) } } + ParallelIndexScanDescData *paralleDesc = NULL; + if (u_sess->stream_cxt.global_obj && node->ss.ps.plan->dop > 1) { + if (WorkerThreadAmI()) { + u_sess->stream_cxt.global_obj->BuildStreamDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan); + } + paralleDesc = (ParallelIndexScanDescData*)u_sess->stream_cxt.global_obj->GetParalleDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan->plan_node_id); + if (WorkerThreadAmI()) + scan_handler_idx_parallelscan_initialize(current_relation, index_state->iss_RelationDesc, paralleDesc); + } + /* Initialize scan descriptor for partitioned table */ - index_state->iss_ScanDesc = scan_handler_idx_beginscan(index_state->ss.ss_currentPartition, - index_state->iss_CurrentIndexPartition, + index_state->iss_ScanDesc = scan_handler_idx_beginscan(current_relation, + index_state->iss_RelationDesc, scanSnap, index_state->iss_NumScanKeys, index_state->iss_NumOrderByKeys, - (ScanState*)index_state); - } + (ScanState*)index_state, + paralleDesc); + } } } else { /* diff --git a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp index 20de90970..1eac6115a 100644 --- a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp +++ b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp @@ -545,15 +545,20 @@ static HeapTuple cross_level_index_getnext(IndexScanDesc scan, ScanDirection dir * ------------------------------------------------------------------------ */ -IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state) +IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state, + ParallelIndexScanDesc pscan) { if (unlikely(RELATION_OWN_BUCKET(heap_relation))) { return hbkt_idx_beginscan(heap_relation, index_relation, snapshot, nkeys, norderbys, scan_state); } else { - return index_beginscan(heap_relation, index_relation, snapshot, nkeys, norderbys, scan_state); + return index_beginscan(heap_relation, index_relation, snapshot, nkeys, norderbys, scan_state, pscan); } } +void scan_handler_idx_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan) { + index_parallelscan_initialize(heap_relation, index_relation, p_index_scan); +} + IndexScanDesc scan_handler_idx_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state) { if (unlikely(RELATION_OWN_BUCKET(indexRelation))) { @@ -574,6 +579,12 @@ void scan_handler_idx_rescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey } } +void scan_handler_idx_rescan_parallel(IndexScanDesc scan) +{ + Assert(scan != NULL); + index_rescan_parallel(scan); +} + void scan_handler_idx_rescan_local(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys) { Assert(scan != NULL); diff --git a/src/gausskernel/storage/access/index/indexam.cpp b/src/gausskernel/storage/access/index/indexam.cpp index 999398d2b..377ec244b 100644 --- a/src/gausskernel/storage/access/index/indexam.cpp +++ b/src/gausskernel/storage/access/index/indexam.cpp @@ -83,6 +83,7 @@ #include "storage/predicate.h" #include "storage/procarray.h" #include "storage/smgr/smgr.h" +#include "access/nbtree.h" #include "access/ustore/knl_uvisibility.h" #include "access/ustore/knl_uscan.h" #include "utils/snapmgr.h" @@ -143,7 +144,8 @@ } \ } while (0) -static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot); +static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot + ,ParallelIndexScanDesc pscan = NULL); /* ---------------- * index_open - open an index relation by relation OID @@ -246,11 +248,12 @@ bool index_insert(Relation index_relation, Datum *values, const bool *isnull, It * Caller must be holding suitable locks on the heap and the index. */ IndexScanDesc index_beginscan( - Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state) + Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state, + ParallelIndexScanDesc pscan) { IndexScanDesc scan; - scan = index_beginscan_internal(index_relation, nkeys, norderbys, snapshot); + scan = index_beginscan_internal(index_relation, nkeys, norderbys, snapshot, pscan); /* * Save additional parameters into the scandesc. Everything else was set @@ -295,7 +298,8 @@ IndexScanDesc index_beginscan_bitmap(Relation index_relation, Snapshot snapshot, /* * index_beginscan_internal --- common code for index_beginscan variants */ -static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot) +static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, + ParallelIndexScanDesc pscan) { IndexScanDesc scan; FmgrInfo *procedure = NULL; @@ -324,6 +328,9 @@ static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys scan->spq_scan = NULL; #endif + /* Initialize information for parallel scan. */ + scan->parallel_scan = pscan; + return scan; } @@ -371,6 +378,18 @@ void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, } } +/* ---------------- + * index_rescan_parallel - reset parallel index scan variables + * ---------------- + */ + +void index_rescan_parallel(IndexScanDesc scan) +{ + if (scan->parallel_scan) { + btparallelrescan(scan); + } +} + /* ---------------- * index_endscan - end a scan * ---------------- @@ -476,6 +495,33 @@ void index_restrpos(IndexScanDesc scan) } +/* + * index_parallelscan_initialize - initialize parallel scan + * + * We initialize both the ParallelIndexScanDesc proper and the AM-specific + * information which follows it. + * + * This function calls access method specific initialization routine to + * initialize am specific information. Call this just once in the leader + * process; then, individual workers attach via index_beginscan_parallel. + */ +void +index_parallelscan_initialize(Relation heap_relation, Relation index_relation, + ParallelIndexScanDesc target) +{ + if (!target) { + return; + } + Size offset; + + RELATION_CHECKS; + + target->ps_relid = RelationGetRelid(heap_relation); + target->ps_indexid = RelationGetRelid(index_relation); + + btinitparallelscan(target->ps_btpscan); +} + /* ---------------- * index_getnext_tid - get the next TID from a scan * diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp index 9c120610b..825f2f639 100644 --- a/src/gausskernel/storage/access/nbtree/nbtree.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp @@ -17,6 +17,9 @@ * * ------------------------------------------------------------------------- */ +#include +#include + #include "postgres.h" #include "knl/knl_variable.h" #include "access/nbtree.h" @@ -25,6 +28,7 @@ #include "access/xlog.h" #include "catalog/index.h" #include "commands/vacuum.h" +#include "pgstat.h" #include "storage/indexfsm.h" #include "storage/ipc.h" #include "storage/lmgr.h" @@ -50,6 +54,46 @@ typedef struct { MemoryContext pagedelcontext; } BTVacState; +/* + * BTPARALLEL_NOT_INITIALIZED indicates that the scan has not started. + * + * BTPARALLEL_ADVANCING indicates that some process is advancing the scan to + * a new page; others must wait. + * + * BTPARALLEL_IDLE indicates that no backend is currently advancing the scan + * to a new page; some process can start doing that. + * + * BTPARALLEL_DONE indicates that the scan is complete (including error exit). + * We reach this state once for every distinct combination of array keys. + */ +typedef enum +{ + BTPARALLEL_NOT_INITIALIZED, + BTPARALLEL_ADVANCING, + BTPARALLEL_IDLE, + BTPARALLEL_DONE +} BTPS_State; + +/* + * BTParallelScanDescData contains btree specific shared information required + * for parallel scan. + */ +typedef struct BTParallelScanDescData +{ + BlockNumber btps_scanPage; /* latest or next page to be scanned */ + BTPS_State btps_pageStatus;/* indicates whether next page is available + * for scan. see above for possible states of + * parallel scan. */ + int btps_arrayKeyCount; /* count indicating number of array + * scan keys processed by parallel + * scan */ + + std::mutex btps_mutex; /* protects above variables */ + std::condition_variable btps_cv; /* used to synchronize parallel scan */ +} BTParallelScanDescData; + +typedef struct BTParallelScanDescData *BTParallelScanDesc; + static void btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state, BTCycleId cycleid); static void btvacuumpage(BTVacState *vstate, BlockNumber blkno, BlockNumber orig_blkno); @@ -480,6 +524,7 @@ IndexScanDesc btbeginscan_internal(Relation rel, int nkeys, int norderbys) so->arrayContext = NULL; so->killedItems = NULL; /* until needed */ so->numKilled = 0; + so->arrayKeyCount = 0; /* * We don't know yet whether the scan will be index-only, so we do not @@ -703,6 +748,201 @@ void btrestrpos_internal(IndexScanDesc scan) } } + +/* + * btinitparallelscan -- initialize BTParallelScanDesc for parallel btree scan + */ +void +btinitparallelscan(void *target) +{ + BTParallelScanDesc bt_target = (BTParallelScanDesc) target; + + std::unique_lock lck(bt_target->btps_mutex); + bt_target->btps_scanPage = InvalidBlockNumber; + bt_target->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; + bt_target->btps_arrayKeyCount = 0; +} + +/* + * btparallelrescan() -- reset parallel scan + */ +void +btparallelrescan(IndexScanDesc scan) +{ + BTParallelScanDesc btscan; + ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + + Assert(parallel_scan); + + btscan = (BTParallelScanDesc) parallel_scan->ps_btpscan; + + /* + * In theory, we don't need to acquire the spinlock here, because there + * shouldn't be any other workers running at this point, but we do so for + * consistency. + */ + std::unique_lock lck(btscan->btps_mutex); + btscan->btps_scanPage = InvalidBlockNumber; + btscan->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; + btscan->btps_arrayKeyCount = 0; +} + +/* + * _bt_parallel_seize() -- Begin the process of advancing the scan to a new + * page. Other scans must wait until we call bt_parallel_release() or + * bt_parallel_done(). + * + * The return value is true if we successfully seized the scan and false + * if we did not. The latter case occurs if no pages remain for the current + * set of scankeys. + * + * If the return value is true, *pageno returns the next or current page + * of the scan (depending on the scan direction). An invalid block number + * means the scan hasn't yet started, and P_NONE means we've reached the end. + * The first time a participating process reaches the last page, it will return + * true and set *pageno to P_NONE; after that, further attempts to seize the + * scan will return false. + * + * Callers should ignore the value of pageno if the return value is false. + */ +bool +_bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) +{ + BTScanOpaque so = (BTScanOpaque) scan->opaque; + BTPS_State pageStatus; + bool exit_loop = false; + bool status = true; + ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + BTParallelScanDesc btscan; + + *pageno = P_NONE; + + btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + + while (1) + { + std::unique_lock lck(btscan->btps_mutex); + pageStatus = btscan->btps_pageStatus; + + if (so->arrayKeyCount < btscan->btps_arrayKeyCount) + { + /* Parallel scan has already advanced to a new set of scankeys. */ + status = false; + } + else if (pageStatus == BTPARALLEL_DONE) + { + /* + * We're done with this set of scankeys. This may be the end, or + * there could be more sets to try. + */ + status = false; + } + else if (pageStatus != BTPARALLEL_ADVANCING) + { + /* + * We have successfully seized control of the scan for the purpose + * of advancing it to a new page! + */ + btscan->btps_pageStatus = BTPARALLEL_ADVANCING; + *pageno = btscan->btps_scanPage; + exit_loop = true; + } + if (exit_loop || !status) + break; + + btscan->btps_cv.wait(lck); + } + + return status; +} + +/* + * _bt_parallel_release() -- Complete the process of advancing the scan to a + * new page. We now have the new value btps_scanPage; some other backend + * can now begin advancing the scan. + */ +void +_bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) +{ + ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + BTParallelScanDesc btscan; + + btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + + { + std::unique_lock lck(btscan->btps_mutex); + btscan->btps_scanPage = scan_page; + btscan->btps_pageStatus = BTPARALLEL_IDLE; + } + btscan->btps_cv.notify_one(); +} + +/* + * _bt_parallel_done() -- Mark the parallel scan as complete. + * + * When there are no pages left to scan, this function should be called to + * notify other workers. Otherwise, they might wait forever for the scan to + * advance to the next page. + */ +void +_bt_parallel_done(IndexScanDesc scan) +{ + BTScanOpaque so = (BTScanOpaque) scan->opaque; + ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + BTParallelScanDesc btscan; + bool status_changed = false; + + /* Do nothing, for non-parallel scans */ + if (parallel_scan == NULL) + return; + + btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + + /* + * Mark the parallel scan as done for this combination of scan keys, + * unless some other process already did so. See also + * _bt_advance_array_keys. + */ + { + std::unique_lock lck(btscan->btps_mutex); + if (so->arrayKeyCount >= btscan->btps_arrayKeyCount + && btscan->btps_pageStatus != BTPARALLEL_DONE) { + btscan->btps_pageStatus = BTPARALLEL_DONE; + status_changed = true; + } + } + + /* wake up all the workers associated with this parallel scan */ + if (status_changed) + btscan->btps_cv.notify_all(); +} + +/* + * _bt_parallel_advance_array_keys() -- Advances the parallel scan for array + * keys. + * + * Updates the count of array keys processed for both local and parallel + * scans. + */ +void +_bt_parallel_advance_array_keys(IndexScanDesc scan) +{ + BTScanOpaque so = (BTScanOpaque) scan->opaque; + ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + BTParallelScanDesc btscan; + + btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + + so->arrayKeyCount++; + std::unique_lock lck(btscan->btps_mutex); + if (btscan->btps_pageStatus == BTPARALLEL_DONE) + { + btscan->btps_scanPage = InvalidBlockNumber; + btscan->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; + btscan->btps_arrayKeyCount++; + } +} + /* * Bulk deletion of all index entries pointing to a set of heap tuples. * The set of target tuples is specified via a callback routine that tells @@ -1396,3 +1636,13 @@ static BTVacuumPosting btree_vacuum_posting(BTVacState *vac_state, IndexTuple po *num_remaining = num_live; return vacposting; } + +/* + * btestimateparallelscan -- estimate storage for BTParallelScanDescData + */ +void* +btbuildparallelscan(void) +{ + void *bt_pscan = (void*)new BTParallelScanDescData; + return bt_pscan; +} diff --git a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp index 1d59776cf..6f5beffc8 100644 --- a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp @@ -35,9 +35,12 @@ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber off static void _bt_saveitem(BTScanOpaque so, int itemIndex, OffsetNumber offnum, IndexTuple itup, Oid partOid, int2 bucketid); static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir); +static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir); +static bool _bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, + ScanDirection dir); static bool _bt_endpoint(IndexScanDesc scan, ScanDirection dir); static void _bt_check_natts_correct(const Relation index, bool heapkeyspace, Page page, OffsetNumber offnum); - +static inline void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir); static int btree_setup_posting_items(BTScanOpaque so, int itemIndex, OffsetNumber offnum, ItemPointer heapTid, IndexTuple itup); static void btree_save_posting_item(BTScanOpaque so, int itemIndex, OffsetNumber offnum, ItemPointer heapTid, @@ -592,8 +595,10 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) ScanKeyData notnullkeys[INDEX_MAX_KEYS]; int keysCount = 0; int i; + bool status = true; StrategyNumber strat_total; BTScanPosItem *currItem = NULL; + BlockNumber blkno; pgstat_count_index_scan(rel); @@ -610,6 +615,30 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) if (!so->qual_ok) return false; + /* + * For parallel scans, get the starting page from shared state. If the + * scan has not started, proceed to find out first leaf page in the usual + * way while keeping other participating processes waiting. If the scan + * has already begun, use the page number from the shared structure. + */ + if (scan->parallel_scan != NULL) + { + status = _bt_parallel_seize(scan, &blkno); + if (!status) + return false; + else if (blkno == P_NONE) + { + _bt_parallel_done(scan); + return false; + } + else if (blkno != InvalidBlockNumber) + { + if (!_bt_parallel_readpage(scan, blkno, dir)) + return false; + goto readcomplete; + } + } + /* ---------- * Examine the scan keys to discover where we need to start the scan. * @@ -775,7 +804,19 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) * there. */ if (keysCount == 0) - return _bt_endpoint(scan, dir); + { + bool match; + + match = _bt_endpoint(scan, dir); + + if (!match) + { + /* No match, so mark (parallel) scan finished */ + _bt_parallel_done(scan); + } + + return match; + } /* * We want to start the scan somewhere within the index. Set up an @@ -802,7 +843,10 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) ScanKey subkey = (ScanKey)DatumGetPointer(cur->sk_argument); Assert(subkey->sk_flags & SK_ROW_MEMBER); if (subkey->sk_flags & SK_ISNULL) + { + _bt_parallel_done(scan); return false; + } inskey.scankeys[i] = *subkey; /* @@ -1003,21 +1047,19 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) * because nothing finer to lock exists. */ PredicateLockRelation(rel, scan->xs_snapshot); + + /* + * mark parallel scan as done, so that all the workers can finish + * their scan + */ + _bt_parallel_done(scan); return false; } else PredicateLockPage(rel, BufferGetBlockNumber(buf), scan->xs_snapshot); - /* initialize moreLeft/moreRight appropriately for scan direction */ - if (ScanDirectionIsForward(dir)) { - so->currPos.moreLeft = false; - so->currPos.moreRight = true; - } else { - so->currPos.moreLeft = true; - so->currPos.moreRight = false; - } - so->numKilled = 0; /* just paranoia */ - so->markItemIndex = -1; /* ditto */ + _bt_initialize_more_data(so, dir); + { /* position to the precise item on the page */ int posting_off = 0; offnum = _bt_binsrch(rel, &inskey, buf, &posting_off); @@ -1057,7 +1099,9 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) /* Drop the lock, but not pin, on the current page */ LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); + } +readcomplete: /* OK, itemIndex says what to return */ currItem = &so->currPos.items[so->currPos.itemIndex]; scan->xs_ctup.t_self = currItem->heapTid; @@ -1149,6 +1193,10 @@ bool _bt_next(IndexScanDesc scan, ScanDirection dir) * moreLeft or moreRight (as appropriate) is cleared if _bt_checkkeys reports * that there can be no more matching tuples in the current scan direction. * + * In the case of a parallel scan, caller must have called _bt_parallel_seize + * prior to calling this function; this function will invoke + * _bt_parallel_release before returning. + * * Returns true if any matching items found on the page, false if none. */ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum) @@ -1175,6 +1223,16 @@ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber off page = BufferGetPage(so->currPos.buf); opaque = (BTPageOpaqueInternal)PageGetSpecialPointer(page); + + /* allow next page be processed by parallel worker */ + if (scan->parallel_scan) + { + if (ScanDirectionIsForward(dir)) + _bt_parallel_release(scan, opaque->btpo_next); + else + _bt_parallel_release(scan, BufferGetBlockNumber(so->currPos.buf)); + } + minoff = P_FIRSTDATAKEY(opaque); maxoff = PageGetMaxOffsetNumber(page); @@ -1314,8 +1372,8 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) { BTScanOpaque so = (BTScanOpaque)scan->opaque; Relation rel; - Page page; - BTPageOpaqueInternal opaque; + BlockNumber blkno = InvalidBlockNumber; + bool status; /* we must have the buffer pinned and locked */ Assert(BufferIsValid(so->currPos.buf)); @@ -1347,52 +1405,148 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) rel = scan->indexRelation; if (ScanDirectionIsForward(dir)) { + /* release the previous buffer, if pinned */ + _bt_relbuf(rel, so->currPos.buf); + so->currPos.buf = InvalidBuffer; + /* Walk right to the next page with data */ - /* We must rely on the previously saved nextPage link! */ - BlockNumber blkno = so->currPos.nextPage; + if (scan->parallel_scan != NULL) + { + /* + * Seize the scan to get the next block number; if the scan has + * ended already, bail out. + */ + status = _bt_parallel_seize(scan, &blkno); + if (!status) + { + return false; + } + } + else + { + /* Not parallel, so use the previously-saved nextPage link. */ + blkno = so->currPos.nextPage; + } /* Remember we left a page with data */ so->currPos.moreLeft = true; - for (;;) { - /* release the previous buffer */ - _bt_relbuf(rel, so->currPos.buf); - so->currPos.buf = InvalidBuffer; - /* if we're at end of scan, give up */ - if (blkno == P_NONE || !so->currPos.moreRight) + } else { + /* Remember we left a page with data */ + so->currPos.moreRight = true; + + if (scan->parallel_scan != NULL) + { + /* + * Seize the scan to get the current block number; if the scan has + * ended already, bail out. + */ + status = _bt_parallel_seize(scan, &blkno); + if (!status) + { + so->currPos.buf = InvalidBuffer; return false; + } + } + } + + if (!_bt_readnextpage(scan, blkno, dir)) + return false; + + return true; +} + +/* + * _bt_readnextpage() -- Read next page containing valid data for scan + * + * On success exit, so->currPos is updated to contain data from the next + * interesting page. Caller is responsible to release lock and pin on + * buffer on success. We return true to indicate success. + * + * If there are no more matching records in the given direction, we drop all + * locks and pins, set so->currPos.buf to InvalidBuffer, and return false. + */ +static bool +_bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) +{ + BTScanOpaque so = (BTScanOpaque) scan->opaque; + Relation rel; + Page page; + BTPageOpaqueInternal opaque; + bool status; + + rel = scan->indexRelation; + + if (ScanDirectionIsForward(dir)) + { + for (;;) + { + /* + * if we're at end of scan, give up and mark parallel scan as + * done, so that all the workers can finish their scan + */ + if (blkno == P_NONE || !so->currPos.moreRight) + { + _bt_parallel_done(scan); + return false; + } /* check for interrupts while we're not holding any buffer lock */ CHECK_FOR_INTERRUPTS(); /* step right one page */ so->currPos.buf = _bt_getbuf(rel, blkno, BT_READ); - /* check for deleted page */ page = BufferGetPage(so->currPos.buf); - opaque = (BTPageOpaqueInternal)PageGetSpecialPointer(page); - if (!P_IGNORE(opaque)) { + opaque = BTPageGetOpaqueInternal(page); + /* check for deleted page */ + if (!P_IGNORE(opaque)) + { PredicateLockPage(rel, blkno, scan->xs_snapshot); /* see if there are any matches on this page */ /* note that this will clear moreRight if we can stop */ if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque))) break; } - /* nope, keep going */ - blkno = opaque->btpo_next; - } - } else { - /* Remember we left a page with data */ - so->currPos.moreRight = true; + else if (scan->parallel_scan != NULL) + { + /* allow next page be processed by parallel worker */ + _bt_parallel_release(scan, opaque->btpo_next); + } + /* release the previous buffer */ + _bt_relbuf(rel, so->currPos.buf); + so->currPos.buf = InvalidBuffer; + + /* nope, keep going */ + if (scan->parallel_scan != NULL) + { + status = _bt_parallel_seize(scan, &blkno); + if (!status) + { + return false; + } + } + else + { + blkno = opaque->btpo_next; + } + } + } + else + { /* * Walk left to the next page with data. This is much more complex * than the walk-right case because of the possibility that the page * to our left splits while we are in flight to it, plus the * possibility that the page we were on gets deleted after we leave - * it. See nbtree/README for details. + * it. See nbtree/README for details. */ - for (;;) { + + for (;;) + { /* Done if we know there are no matching keys to the left */ - if (!so->currPos.moreLeft) { + if (!so->currPos.moreLeft) + { _bt_relbuf(rel, so->currPos.buf); + _bt_parallel_done(scan); so->currPos.buf = InvalidBuffer; return false; } @@ -1404,7 +1558,10 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) /* if we're physically at end of index, return failure */ if (so->currPos.buf == InvalidBuffer) + { + _bt_parallel_done(scan); return false; + } /* * Okay, we managed to move left to a non-deleted page. Done if @@ -1412,20 +1569,65 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) * and do it all again. */ page = BufferGetPage(so->currPos.buf); - opaque = (BTPageOpaqueInternal)PageGetSpecialPointer(page); - if (!P_IGNORE(opaque)) { + opaque = BTPageGetOpaqueInternal(page); + if (!P_IGNORE(opaque)) + { PredicateLockPage(rel, BufferGetBlockNumber(so->currPos.buf), scan->xs_snapshot); /* see if there are any matches on this page */ /* note that this will clear moreLeft if we can stop */ if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page))) break; } + else if (scan->parallel_scan != NULL) + { + /* allow next page be processed by parallel worker */ + _bt_parallel_release(scan, BufferGetBlockNumber(so->currPos.buf)); + } + + /* + * For parallel scans, get the last page scanned as it is quite + * possible that by the time we try to seize the scan, some other + * worker has already advanced the scan to a different page. We + * must continue based on the latest page scanned by any worker. + */ + if (scan->parallel_scan != NULL) + { + _bt_relbuf(rel, so->currPos.buf); + status = _bt_parallel_seize(scan, &blkno); + if (!status) + { + so->currPos.buf = InvalidBuffer; + return false; + } + so->currPos.buf = _bt_getbuf(rel, blkno, BT_READ); + } } } return true; } +/* + * _bt_parallel_readpage() -- Read current page containing valid data for scan + * + * On success, release lock and maybe pin on buffer. We return true to + * indicate success. + */ +static bool +_bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) +{ + BTScanOpaque so = (BTScanOpaque) scan->opaque; + + _bt_initialize_more_data(so, dir); + + if (!_bt_readnextpage(scan, blkno, dir)) + return false; + + /* Drop the lock, but not pin, on the new page */ + LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); + return true; +} + /* * _bt_walk_left() -- step left one page, if possible * @@ -1667,16 +1869,7 @@ static bool _bt_endpoint(IndexScanDesc scan, ScanDirection dir) /* remember which buffer we have pinned */ so->currPos.buf = buf; - /* initialize moreLeft/moreRight appropriately for scan direction */ - if (ScanDirectionIsForward(dir)) { - so->currPos.moreLeft = false; - so->currPos.moreRight = true; - } else { - so->currPos.moreLeft = true; - so->currPos.moreRight = false; - } - so->numKilled = 0; /* just paranoia */ - so->markItemIndex = -1; /* ditto */ + _bt_initialize_more_data(so, dir); /* * Now load data from the first page of the scan. @@ -1889,3 +2082,26 @@ static inline void btree_save_posting_item(BTScanOpaque so, int item_idx, Offset if (so->currTuples) curr_item->tupleOffset = tuple_offset; } + +/* + * _bt_initialize_more_data() -- initialize moreLeft/moreRight appropriately + * for scan direction + */ +static inline void +_bt_initialize_more_data(BTScanOpaque so, ScanDirection dir) +{ + /* initialize moreLeft/moreRight appropriately for scan direction */ + if (ScanDirectionIsForward(dir)) + { + so->currPos.moreLeft = false; + so->currPos.moreRight = true; + } + else + { + so->currPos.moreLeft = true; + so->currPos.moreRight = false; + } + so->numKilled = 0; /* just paranoia */ + so->markItemIndex = -1; /* ditto */ +} + diff --git a/src/gausskernel/storage/access/nbtree/nbtutils.cpp b/src/gausskernel/storage/access/nbtree/nbtutils.cpp index 93d3ffba9..1c477b6b0 100644 --- a/src/gausskernel/storage/access/nbtree/nbtutils.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtutils.cpp @@ -554,6 +554,10 @@ bool _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir) } } + /* advance parallel scan */ + if (scan->parallel_scan != NULL) + _bt_parallel_advance_array_keys(scan); + return found; } diff --git a/src/include/access/genam.h b/src/include/access/genam.h index fbf04d175..ac48b2f58 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -86,6 +86,7 @@ typedef bool (*IndexBulkDeleteCallback)(ItemPointer itemptr, void* state, Oid pa typedef struct IndexScanDescData* IndexScanDesc; typedef struct SysScanDescData* SysScanDesc; struct ScanState; +typedef struct ParallelIndexScanDescData *ParallelIndexScanDesc; /* * Enumeration specifying the type of uniqueness check to perform in @@ -133,9 +134,11 @@ extern bool index_insert(Relation indexRelation, Datum* values, const bool* isnu Relation heapRelation, IndexUniqueCheck checkUnique); extern IndexScanDesc index_beginscan( - Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state=NULL); + Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state=NULL, ParallelIndexScanDesc pscan=NULL); +extern void index_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan); extern IndexScanDesc index_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state=NULL); extern void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys); +extern void index_rescan_parallel(IndexScanDesc scan); extern void index_endscan(IndexScanDesc scan); extern void index_markpos(IndexScanDesc scan); extern void index_restrpos(IndexScanDesc scan); diff --git a/src/include/access/hbindex_am.h b/src/include/access/hbindex_am.h index 6de8943e2..8aa2765bd 100644 --- a/src/include/access/hbindex_am.h +++ b/src/include/access/hbindex_am.h @@ -49,9 +49,11 @@ extern bool hbkt_idx_bitmapscan_switch_bucket(IndexScanDesc scan, int targetSlot extern bool cbi_scan_need_fix_hbkt_rel(IndexScanDesc scan, int2 bucketid = InvalidBktId); extern bool cbi_scan_fix_hbkt_rel(HBktIdxScanDesc hpScan); extern IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, - int nkeys, int norderbys, ScanState* scan_state = NULL); + int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); + extern void scan_handler_idx_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan); extern IndexScanDesc scan_handler_idx_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state); extern void scan_handler_idx_rescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys); +extern void scan_handler_idx_rescan_parallel(IndexScanDesc scan); extern void scan_handler_idx_rescan_local(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys); extern void scan_handler_idx_endscan(IndexScanDesc scan); extern void scan_handler_idx_markpos(IndexScanDesc scan); diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h index c34fad5c0..395d07001 100644 --- a/src/include/access/nbtree.h +++ b/src/include/access/nbtree.h @@ -107,6 +107,7 @@ typedef struct UBTPageOpaqueData { } UBTPageOpaqueData; typedef UBTPageOpaqueData* UBTPageOpaque; +#define BTPageGetOpaqueInternal(page) ((BTPageOpaqueInternal) PageGetSpecialPointer(page)) typedef struct BTDedupIntervalData { OffsetNumber base_off; @@ -961,6 +962,8 @@ typedef struct BTScanOpaqueData { ScanKey arrayKeyData; /* modified copy of scan->keyData */ int numArrayKeys; /* number of equality-type array keys (-1 if * there are any unsatisfiable array keys) */ + int arrayKeyCount; /* count indicating number of array scan keys + * processed */ BTArrayKeyInfo* arrayKeys; /* info about each equality-type array key */ MemoryContext arrayContext; /* scan-lifespan context for array data */ @@ -1282,6 +1285,8 @@ extern Datum btbuild(PG_FUNCTION_ARGS); extern Datum btbuildempty(PG_FUNCTION_ARGS); extern Datum btinsert(PG_FUNCTION_ARGS); extern Datum btbeginscan(PG_FUNCTION_ARGS); +extern void* btbuildparallelscan(void); +extern void btinitparallelscan(void *target); extern Datum btgettuple(PG_FUNCTION_ARGS); extern Datum btgetbitmap(PG_FUNCTION_ARGS); extern Datum cbtreegetbitmap(PG_FUNCTION_ARGS); @@ -1294,6 +1299,16 @@ extern Datum btvacuumcleanup(PG_FUNCTION_ARGS); extern Datum btcanreturn(PG_FUNCTION_ARGS); extern Datum btoptions(PG_FUNCTION_ARGS); +extern void btparallelrescan(IndexScanDesc scan); +extern void btinitparallelscan(void *target); +/* + * prototypes for internal functions in nbtree.c + */ +extern bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno); +extern void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page); +extern void _bt_parallel_done(IndexScanDesc scan); +extern void _bt_parallel_advance_array_keys(IndexScanDesc scan); + extern inline IndexBuildResult *btbuild_internal(Relation heap, Relation index, IndexInfo *index_info); extern inline void btbuildempty_internal(Relation index); @@ -1326,7 +1341,6 @@ extern inline IndexBuildResult *btmerge_internal(Relation dstIdxRel, List *srcId * thought, we are not going to implement them right now. */ extern Datum btmerge(PG_FUNCTION_ARGS); - /* * prototypes for functions in nbtinsert.c */ diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h index d3f40dd24..878f657fd 100644 --- a/src/include/access/relscan.h +++ b/src/include/access/relscan.h @@ -170,6 +170,8 @@ typedef struct IndexScanDescData { SPQScanDesc spq_scan; #endif IndexFetchTableData *xs_heapfetch; + /* parallel index scan information, in global variables */ + struct ParallelIndexScanDescData *parallel_scan; /* put decompressed heap tuple data into xs_ctbuf_hdr be careful! when malloc memory should give extra mem for *xs_ctbuf_hdr. t_bits which is varlength arr */ @@ -177,6 +179,14 @@ typedef struct IndexScanDescData { /* DO NOT add any other members here. xs_ctbuf_hdr must be the last one. */ } IndexScanDescData; +/* Generic structure for parallel scans */ +typedef struct ParallelIndexScanDescData +{ + Oid ps_relid; + Oid ps_indexid; + void* ps_btpscan; +} ParallelIndexScanDescData; + #define SizeofIndexScanDescData (offsetof(IndexScanDescData, xs_ctbuf_hdr) + SizeofHeapTupleHeader) /* Get partition heap oid for bitmap index scan */ diff --git a/src/include/c.h b/src/include/c.h index 468768a34..5c02faa24 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -722,6 +722,8 @@ typedef struct pathData { * True iff pointer is properly aligned to point to the given type. */ #define PointerIsAligned(pointer, type) (((intptr_t)(pointer) % (sizeof(type))) == 0) +#define OffsetToPointer(base, offset) \ + ((void *)((char *) base + offset)) #define OidIsValid(objectId) ((bool)((objectId) != InvalidOid)) diff --git a/src/include/distributelayer/streamCore.h b/src/include/distributelayer/streamCore.h index a117a3032..b4dacb7e4 100755 --- a/src/include/distributelayer/streamCore.h +++ b/src/include/distributelayer/streamCore.h @@ -28,6 +28,7 @@ #define SRC_INCLUDE_DISTRIBUTELAYER_STREAMCORE_H_ #include +#include #include "postgres.h" #include "knl/knl_variable.h" @@ -442,6 +443,12 @@ public: /* Send stop signal to all stream threads in node group. */ void SigStreamThreadClose(); + void BuildStreamDesc(const uint64& queryId, Plan* node); + + void* GetParalleDesc(const uint64& queryId, const uint64& planNodeId); + + void DestroyStreamDesc(const uint64& queryId, Plan* node); + struct PortalData *m_portal; #endif /* Mark recursive vfd is invalid before aborting transaction. */ @@ -519,6 +526,21 @@ private: /* Mark Stream query quit status. */ StreamObjStatus m_quitStatus; #endif + struct KeyHash { + std::size_t operator()(const StreamKey& k) const + { + return std::hash()(k.queryId) ^ + (std::hash()(k.planNodeId) << 1); + } + }; + + struct KeyEqual { + bool operator()(const StreamKey& lhs, const StreamKey& rhs) const + { + return lhs.queryId == rhs.queryId && lhs.planNodeId == rhs.planNodeId; + } + }; + std::unordered_map m_streamDesc; }; extern bool IsThreadProcessStreamRecursive(); diff --git a/src/include/executor/node/nodeIndexscan.h b/src/include/executor/node/nodeIndexscan.h index 0fdbf67a5..c3441dafc 100644 --- a/src/include/executor/node/nodeIndexscan.h +++ b/src/include/executor/node/nodeIndexscan.h @@ -14,6 +14,7 @@ #ifndef NODEINDEXSCAN_H #define NODEINDEXSCAN_H +#include "executor/exec/execStream.h" #include "nodes/execnodes.h" extern IndexScanState* ExecInitIndexScan(IndexScan* node, EState* estate, int eflags); diff --git a/src/include/knl/knl_thread.h b/src/include/knl/knl_thread.h index 6b214e360..7bd3bbf18 100755 --- a/src/include/knl/knl_thread.h +++ b/src/include/knl/knl_thread.h @@ -3642,6 +3642,16 @@ inline bool StreamTopConsumerAmI() return (t_thrd.subrole == TOP_CONSUMER); } +inline bool WorkerThreadAmI() +{ + return (t_thrd.role == WORKER || t_thrd.role == THREADPOOL_WORKER); +} + +inline bool StreamWorkerThreadAmI() +{ + return (t_thrd.role == STREAM_WORKER); +} + inline bool WLMThreadAmI() { return (t_thrd.role == WLM_WORKER || t_thrd.role == WLM_MONITOR || diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h index c9e5af177..91dee74f2 100644 --- a/src/include/optimizer/pathnode.h +++ b/src/include/optimizer/pathnode.h @@ -61,7 +61,7 @@ extern Path *create_tsstorescan_path(PlannerInfo * root,RelOptInfo * rel, int do #endif /* ENABLE_MULTIPLE_NODES */ extern IndexPath* create_index_path(PlannerInfo* root, IndexOptInfo* index, List* indexclauses, List* indexclausecols, List* indexorderbys, List* indexorderbycols, List* pathkeys, ScanDirection indexscandir, bool indexonly, - Relids required_outer, Bitmapset *upper_params, double loop_count); + Relids required_outer, Bitmapset *upper_params, double loop_count, int dop = 1); extern Path* build_seqScanPath_by_indexScanPath(PlannerInfo* root, Path* index_path); extern bool CheckBitmapQualIsGlobalIndex(Path* bitmapqual); extern bool CheckBitmapHeapPathContainGlobalOrLocal(Path* bitmapqual); diff --git a/src/test/regress/input/parallel_index_scan.source b/src/test/regress/input/parallel_index_scan.source new file mode 100644 index 000000000..99a20da5e --- /dev/null +++ b/src/test/regress/input/parallel_index_scan.source @@ -0,0 +1,35 @@ +drop schema if exists test_parallel_index_scan cascade; +create schema test_parallel_index_scan; +set current_schema='test_parallel_index_scan'; +-- create test table and index +DROP TABLE IF EXISTS parallel_index_01; +CREATE TABLE parallel_index_01(a int, b int); +INSERT INTO parallel_index_01 VALUES (generate_series(1, 1000000), generate_series(1,1000000)); +CREATE INDEX index_parallel_index_01 ON parallel_index_01(a); +SET enable_seqscan = OFF; +SET enable_bitmapscan = OFF; +SET enable_indexonlyscan = OFF; +SET query_dop = 2; +-- encourage optimizer to choose parallel path +SET smp_thread_cost = 0; + +-- parallel index scan in equality case +SELECT * FROM parallel_index_01 WHERE a=100; +SELECT * FROM parallel_index_01 WHERE a=100 AND a=10000; +SELECT * FROM parallel_index_01 WHERE a=100 AND b=100; +SELECT * FROM parallel_index_01 WHERE a=100 AND b=200; + +-- parallel index scan in scope case +EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a<10000; +SELECT COUNT(b) FROM parallel_index_01 WHERE a<10000; +EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a>10000 AND a<20000; +SELECT COUNT(b) FROM parallel_index_01 WHERE a>10000 AND a<20000; +EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; +SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; + +--cleanup env +reset enable_seqscan; +reset enable_bitmapscan; +reset enable_indexonlyscan; +reset query_dop; +reset smp_thread_cost; diff --git a/src/test/regress/output/parallel_index_scan.source b/src/test/regress/output/parallel_index_scan.source new file mode 100644 index 000000000..7501a417f --- /dev/null +++ b/src/test/regress/output/parallel_index_scan.source @@ -0,0 +1,92 @@ +drop schema if exists test_parallel_index_scan cascade; +NOTICE: schema "test_parallel_index_scan" does not exist, skipping +create schema test_parallel_index_scan; +set current_schema='test_parallel_index_scan'; +-- create test table and index +DROP TABLE IF EXISTS parallel_index_01; +NOTICE: table "parallel_index_01" does not exist, skipping +CREATE TABLE parallel_index_01(a int, b int); +INSERT INTO parallel_index_01 VALUES (generate_series(1, 1000000), generate_series(1,1000000)); +CREATE INDEX index_parallel_index_01 ON parallel_index_01(a); +SET enable_seqscan = OFF; +SET enable_bitmapscan = OFF; +SET enable_indexonlyscan = OFF; +SET query_dop = 2; +-- encourage optimizer to choose parallel path +SET smp_thread_cost = 0; +-- parallel index scan in equality case +SELECT * FROM parallel_index_01 WHERE a=100; + a | b +-----+----- + 100 | 100 +(1 row) + +SELECT * FROM parallel_index_01 WHERE a=100 AND a=10000; + a | b +---+--- +(0 rows) + +SELECT * FROM parallel_index_01 WHERE a=100 AND b=100; + a | b +-----+----- + 100 | 100 +(1 row) + +SELECT * FROM parallel_index_01 WHERE a=100 AND b=200; + a | b +---+--- +(0 rows) + +-- parallel index scan in scope case +EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a<10000; + QUERY PLAN +--------------------------------------------------------------------- + Streaming(type: LOCAL GATHER dop: 1/2) + -> Index Scan using index_parallel_index_01 on parallel_index_01 + Index Cond: (a < 10000) +(3 rows) + +SELECT COUNT(b) FROM parallel_index_01 WHERE a<10000; + count +------- + 9999 +(1 row) + +EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a>10000 AND a<20000; + QUERY PLAN +--------------------------------------------------------------------- + Streaming(type: LOCAL GATHER dop: 1/2) + -> Index Scan using index_parallel_index_01 on parallel_index_01 + Index Cond: ((a > 10000) AND (a < 20000)) +(3 rows) + +SELECT COUNT(b) FROM parallel_index_01 WHERE a>10000 AND a<20000; + count +------- + 9999 +(1 row) + +EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; + QUERY PLAN +--------------------------------------------------------------------------- + Sort + Sort Key: a + -> Streaming(type: LOCAL GATHER dop: 1/2) + -> Index Scan using index_parallel_index_01 on parallel_index_01 + Index Cond: (a = ANY ('{1000,10000,100000}'::integer[])) +(5 rows) + +SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; + a | b +--------+-------- + 1000 | 1000 + 10000 | 10000 + 100000 | 100000 +(3 rows) + +--cleanup env +reset enable_seqscan; +reset enable_bitmapscan; +reset enable_indexonlyscan; +reset query_dop; +reset smp_thread_cost; From d2441fa591746b569a0ddc3c6aaa6ac68788dd37 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Thu, 20 Oct 2022 15:37:30 +0800 Subject: [PATCH 02/12] fix #62649744 fix parallel indexscan backward coredump --- .../runtime/executor/nodeIndexscan.cpp | 26 ++++++------ .../storage/access/nbtree/nbtree.cpp | 4 +- .../storage/access/nbtree/nbtsearch.cpp | 41 +++++++++++++++++-- src/include/access/nbtree.h | 1 + 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/gausskernel/runtime/executor/nodeIndexscan.cpp b/src/gausskernel/runtime/executor/nodeIndexscan.cpp index fc5ccd305..2f37aaea6 100644 --- a/src/gausskernel/runtime/executor/nodeIndexscan.cpp +++ b/src/gausskernel/runtime/executor/nodeIndexscan.cpp @@ -605,24 +605,13 @@ void ExecInitIndexRelation(IndexScanState* node, EState* estate, int eflags) } } - ParallelIndexScanDescData *paralleDesc = NULL; - if (u_sess->stream_cxt.global_obj && node->ss.ps.plan->dop > 1) { - if (WorkerThreadAmI()) { - u_sess->stream_cxt.global_obj->BuildStreamDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan); - } - paralleDesc = (ParallelIndexScanDescData*)u_sess->stream_cxt.global_obj->GetParalleDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan->plan_node_id); - if (WorkerThreadAmI()) - scan_handler_idx_parallelscan_initialize(current_relation, index_state->iss_RelationDesc, paralleDesc); - } - /* Initialize scan descriptor for partitioned table */ index_state->iss_ScanDesc = scan_handler_idx_beginscan(current_relation, index_state->iss_RelationDesc, scanSnap, index_state->iss_NumScanKeys, index_state->iss_NumOrderByKeys, - (ScanState*)index_state, - paralleDesc); + (ScanState*)index_state); } } } else { @@ -644,6 +633,16 @@ void ExecInitIndexRelation(IndexScanState* node, EState* estate, int eflags) } } + ParallelIndexScanDescData *paralleDesc = NULL; + if (u_sess->stream_cxt.global_obj && node->ss.ps.plan->dop > 1) { + if (WorkerThreadAmI()) { + u_sess->stream_cxt.global_obj->BuildStreamDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan); + } + paralleDesc = (ParallelIndexScanDescData*)u_sess->stream_cxt.global_obj->GetParalleDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan->plan_node_id); + if (WorkerThreadAmI()) + scan_handler_idx_parallelscan_initialize(current_relation, index_state->iss_RelationDesc, paralleDesc); + } + /* * Initialize scan descriptor. */ @@ -652,7 +651,8 @@ void ExecInitIndexRelation(IndexScanState* node, EState* estate, int eflags) scanSnap, index_state->iss_NumScanKeys, index_state->iss_NumOrderByKeys, - (ScanState*)index_state); + (ScanState*)index_state, + paralleDesc); } return; diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp index 825f2f639..9026288f7 100644 --- a/src/gausskernel/storage/access/nbtree/nbtree.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp @@ -818,10 +818,10 @@ _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) *pageno = P_NONE; btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); - + + std::unique_lock lck(btscan->btps_mutex); while (1) { - std::unique_lock lck(btscan->btps_mutex); pageStatus = btscan->btps_pageStatus; if (so->arrayKeyCount < btscan->btps_arrayKeyCount) diff --git a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp index 6f5beffc8..dcf5bb5a4 100644 --- a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp @@ -1243,6 +1243,12 @@ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber off */ so->currPos.nextPage = opaque->btpo_next; + /* + * We note the buffer's block number so that we can release the pin later. + * This allows us to re-read the buffer if it is needed again for hinting. + */ + so->currPos.currPage = BufferGetBlockNumber(so->currPos.buf); + /* initialize tuple workspace to empty */ so->currPos.nextTupleOffset = 0; @@ -1404,9 +1410,10 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) rel = scan->indexRelation; - if (ScanDirectionIsForward(dir)) { - /* release the previous buffer, if pinned */ - _bt_relbuf(rel, so->currPos.buf); + /* release the previous buffer, if pinned */ + _bt_relbuf(rel, so->currPos.buf); + + if (ScanDirectionIsForward(dir)) { so->currPos.buf = InvalidBuffer; /* Walk right to the next page with data */ @@ -1448,6 +1455,11 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) return false; } } + else + { + /* Not parallel, so just use our own notion of the current page */ + blkno = so->currPos.currPage; + } } if (!_bt_readnextpage(scan, blkno, dir)) @@ -1532,12 +1544,35 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) } else { + /* + * Should only happen in parallel cases, when some other backend + * advanced the scan. + */ + + so->currPos.currPage = blkno; + so->currPos.buf = _bt_getbuf(rel, so->currPos.currPage, BT_READ); + /* * Walk left to the next page with data. This is much more complex * than the walk-right case because of the possibility that the page * to our left splits while we are in flight to it, plus the * possibility that the page we were on gets deleted after we leave * it. See nbtree/README for details. + * + * It might be possible to rearrange this code to have less overhead + * in pinning and locking, but that would require capturing the left + * pointer when the page is initially read, and using it here, along + * with big changes to _bt_walk_left() and the code below. It is not + * clear whether this would be a win, since if the page immediately to + * the left splits after we read this page and before we step left, we + * would need to visit more pages than with the current code. + * + * Note that if we change the code so that we drop the pin for a scan + * which uses a non-MVCC snapshot, we will need to modify the code for + * walking left, to allow for the possibility that a referenced page + * has been deleted. As long as the buffer is pinned or the snapshot + * is MVCC the page cannot move past the half-dead state to fully + * deleted. */ for (;;) diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h index 395d07001..a8b2c16e0 100644 --- a/src/include/access/nbtree.h +++ b/src/include/access/nbtree.h @@ -906,6 +906,7 @@ typedef struct BTScanPosData { Buffer buf; /* if valid, the buffer is pinned */ BlockNumber nextPage; /* page's right link when we scanned it */ + BlockNumber currPage; /* page referenced by items array */ TransactionId xid_base; From ce4e503946ec9ccfbb44ca39210617a20c063646 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Fri, 19 Jul 2024 16:27:41 +0800 Subject: [PATCH 03/12] remove test case --- .../regress/input/parallel_index_scan.source | 35 ------- .../regress/output/parallel_index_scan.source | 92 ------------------- 2 files changed, 127 deletions(-) delete mode 100644 src/test/regress/input/parallel_index_scan.source delete mode 100644 src/test/regress/output/parallel_index_scan.source diff --git a/src/test/regress/input/parallel_index_scan.source b/src/test/regress/input/parallel_index_scan.source deleted file mode 100644 index 99a20da5e..000000000 --- a/src/test/regress/input/parallel_index_scan.source +++ /dev/null @@ -1,35 +0,0 @@ -drop schema if exists test_parallel_index_scan cascade; -create schema test_parallel_index_scan; -set current_schema='test_parallel_index_scan'; --- create test table and index -DROP TABLE IF EXISTS parallel_index_01; -CREATE TABLE parallel_index_01(a int, b int); -INSERT INTO parallel_index_01 VALUES (generate_series(1, 1000000), generate_series(1,1000000)); -CREATE INDEX index_parallel_index_01 ON parallel_index_01(a); -SET enable_seqscan = OFF; -SET enable_bitmapscan = OFF; -SET enable_indexonlyscan = OFF; -SET query_dop = 2; --- encourage optimizer to choose parallel path -SET smp_thread_cost = 0; - --- parallel index scan in equality case -SELECT * FROM parallel_index_01 WHERE a=100; -SELECT * FROM parallel_index_01 WHERE a=100 AND a=10000; -SELECT * FROM parallel_index_01 WHERE a=100 AND b=100; -SELECT * FROM parallel_index_01 WHERE a=100 AND b=200; - --- parallel index scan in scope case -EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a<10000; -SELECT COUNT(b) FROM parallel_index_01 WHERE a<10000; -EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a>10000 AND a<20000; -SELECT COUNT(b) FROM parallel_index_01 WHERE a>10000 AND a<20000; -EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; -SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; - ---cleanup env -reset enable_seqscan; -reset enable_bitmapscan; -reset enable_indexonlyscan; -reset query_dop; -reset smp_thread_cost; diff --git a/src/test/regress/output/parallel_index_scan.source b/src/test/regress/output/parallel_index_scan.source deleted file mode 100644 index 7501a417f..000000000 --- a/src/test/regress/output/parallel_index_scan.source +++ /dev/null @@ -1,92 +0,0 @@ -drop schema if exists test_parallel_index_scan cascade; -NOTICE: schema "test_parallel_index_scan" does not exist, skipping -create schema test_parallel_index_scan; -set current_schema='test_parallel_index_scan'; --- create test table and index -DROP TABLE IF EXISTS parallel_index_01; -NOTICE: table "parallel_index_01" does not exist, skipping -CREATE TABLE parallel_index_01(a int, b int); -INSERT INTO parallel_index_01 VALUES (generate_series(1, 1000000), generate_series(1,1000000)); -CREATE INDEX index_parallel_index_01 ON parallel_index_01(a); -SET enable_seqscan = OFF; -SET enable_bitmapscan = OFF; -SET enable_indexonlyscan = OFF; -SET query_dop = 2; --- encourage optimizer to choose parallel path -SET smp_thread_cost = 0; --- parallel index scan in equality case -SELECT * FROM parallel_index_01 WHERE a=100; - a | b ------+----- - 100 | 100 -(1 row) - -SELECT * FROM parallel_index_01 WHERE a=100 AND a=10000; - a | b ----+--- -(0 rows) - -SELECT * FROM parallel_index_01 WHERE a=100 AND b=100; - a | b ------+----- - 100 | 100 -(1 row) - -SELECT * FROM parallel_index_01 WHERE a=100 AND b=200; - a | b ----+--- -(0 rows) - --- parallel index scan in scope case -EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a<10000; - QUERY PLAN ---------------------------------------------------------------------- - Streaming(type: LOCAL GATHER dop: 1/2) - -> Index Scan using index_parallel_index_01 on parallel_index_01 - Index Cond: (a < 10000) -(3 rows) - -SELECT COUNT(b) FROM parallel_index_01 WHERE a<10000; - count -------- - 9999 -(1 row) - -EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a>10000 AND a<20000; - QUERY PLAN ---------------------------------------------------------------------- - Streaming(type: LOCAL GATHER dop: 1/2) - -> Index Scan using index_parallel_index_01 on parallel_index_01 - Index Cond: ((a > 10000) AND (a < 20000)) -(3 rows) - -SELECT COUNT(b) FROM parallel_index_01 WHERE a>10000 AND a<20000; - count -------- - 9999 -(1 row) - -EXPLAIN (COSTS OFF) SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; - QUERY PLAN ---------------------------------------------------------------------------- - Sort - Sort Key: a - -> Streaming(type: LOCAL GATHER dop: 1/2) - -> Index Scan using index_parallel_index_01 on parallel_index_01 - Index Cond: (a = ANY ('{1000,10000,100000}'::integer[])) -(5 rows) - -SELECT * FROM parallel_index_01 WHERE a in (1000,10000,100000) ORDER BY a; - a | b ---------+-------- - 1000 | 1000 - 10000 | 10000 - 100000 | 100000 -(3 rows) - ---cleanup env -reset enable_seqscan; -reset enable_bitmapscan; -reset enable_indexonlyscan; -reset query_dop; -reset smp_thread_cost; From c601b78a54bc502e6c18a85aa08dfddeb2f5b303 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Wed, 24 Jul 2024 10:41:29 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=86=B2=E7=AA=81=EF=BC=8C=E5=90=88=E5=B9=B6=E6=97=B6=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=96=8F=E6=BC=8F=EF=BC=8C=E4=BB=A3=E7=A0=81=E5=90=88?= =?UTF-8?q?=E9=94=99=E4=BA=86=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gausskernel/runtime/executor/nodeIndexscan.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gausskernel/runtime/executor/nodeIndexscan.cpp b/src/gausskernel/runtime/executor/nodeIndexscan.cpp index 2f37aaea6..71fe2ea91 100644 --- a/src/gausskernel/runtime/executor/nodeIndexscan.cpp +++ b/src/gausskernel/runtime/executor/nodeIndexscan.cpp @@ -606,13 +606,13 @@ void ExecInitIndexRelation(IndexScanState* node, EState* estate, int eflags) } /* Initialize scan descriptor for partitioned table */ - index_state->iss_ScanDesc = scan_handler_idx_beginscan(current_relation, - index_state->iss_RelationDesc, + index_state->iss_ScanDesc = scan_handler_idx_beginscan(index_state->ss.ss_currentPartition, + index_state->iss_CurrentIndexPartition, scanSnap, index_state->iss_NumScanKeys, index_state->iss_NumOrderByKeys, (ScanState*)index_state); - } + } } } else { /* From 3312e14cd494c01eb17d858a637dc883f69d7c7e Mon Sep 17 00:00:00 2001 From: zhaosen Date: Fri, 26 Jul 2024 13:44:23 +0800 Subject: [PATCH 05/12] fix test case plan_hint_iud --- .../runtime/executor/nodeIndexscan.cpp | 18 +++++++++--------- src/test/regress/expected/plan_hint_iud.out | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/gausskernel/runtime/executor/nodeIndexscan.cpp b/src/gausskernel/runtime/executor/nodeIndexscan.cpp index 71fe2ea91..3668857d9 100644 --- a/src/gausskernel/runtime/executor/nodeIndexscan.cpp +++ b/src/gausskernel/runtime/executor/nodeIndexscan.cpp @@ -633,15 +633,15 @@ void ExecInitIndexRelation(IndexScanState* node, EState* estate, int eflags) } } - ParallelIndexScanDescData *paralleDesc = NULL; - if (u_sess->stream_cxt.global_obj && node->ss.ps.plan->dop > 1) { - if (WorkerThreadAmI()) { - u_sess->stream_cxt.global_obj->BuildStreamDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan); - } - paralleDesc = (ParallelIndexScanDescData*)u_sess->stream_cxt.global_obj->GetParalleDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan->plan_node_id); - if (WorkerThreadAmI()) - scan_handler_idx_parallelscan_initialize(current_relation, index_state->iss_RelationDesc, paralleDesc); - } + ParallelIndexScanDescData *paralleDesc = NULL; + if (u_sess->stream_cxt.global_obj && node->ss.ps.plan->dop > 1) { + if (WorkerThreadAmI()) { + u_sess->stream_cxt.global_obj->BuildStreamDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan); + } + paralleDesc = (ParallelIndexScanDescData*)u_sess->stream_cxt.global_obj->GetParalleDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan->plan_node_id); + if (WorkerThreadAmI()) + scan_handler_idx_parallelscan_initialize(current_relation, index_state->iss_RelationDesc, paralleDesc); + } /* * Initialize scan descriptor. diff --git a/src/test/regress/expected/plan_hint_iud.out b/src/test/regress/expected/plan_hint_iud.out index 25e775a6f..09443698d 100755 --- a/src/test/regress/expected/plan_hint_iud.out +++ b/src/test/regress/expected/plan_hint_iud.out @@ -579,15 +579,18 @@ deallocate all; --- Set :EXP merge /*+ set(query_dop 1008) */ into t1 using t2 on t1.c1 = t2.c1 when matched then update set t1.c2 = t2.c2 when not matched then insert values (t2.c1, t2.c2); - QUERY PLAN ----------------------------------------------------- + QUERY PLAN +---------------------------------------------------------------------- Merge on t1 - -> Nested Loop Left Join - -> Streaming(type: LOCAL GATHER dop: 1/8) - -> Seq Scan on t2 - -> Index Scan using t1_pkey on t1 - Index Cond: (c1 = t2.c1) -(6 rows) + -> Streaming(type: LOCAL GATHER dop: 1/8) + -> Nested Loop Left Join + Join Filter: (t1.c1 = t2.c1) + -> Streaming(type: LOCAL REDISTRIBUTE dop: 8/8) + -> Seq Scan on t2 + -> Materialize + -> Streaming(type: LOCAL REDISTRIBUTE dop: 8/8) + -> Seq Scan on t1 +(9 rows) --- Plancache prepare merge_g as merge /*+ use_gplan */ into t1 using t2 on t1.c1 = t2.c1 and t1.c1 = $1 when matched then update set t1.c2 = t2.c2 when not matched then insert values (t2.c1, t2.c2); From dcb24b7bbc2e8b1ecdf503844ef9f9a90ac11864 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Sun, 28 Jul 2024 17:10:03 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=81=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E4=B8=BALWlock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../storage/access/nbtree/nbtree.cpp | 30 ++++++++----------- src/gausskernel/storage/lmgr/lwlocknames.txt | 1 + 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp index 9026288f7..376964a5f 100644 --- a/src/gausskernel/storage/access/nbtree/nbtree.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp @@ -17,8 +17,6 @@ * * ------------------------------------------------------------------------- */ -#include -#include #include "postgres.h" #include "knl/knl_variable.h" @@ -87,9 +85,6 @@ typedef struct BTParallelScanDescData int btps_arrayKeyCount; /* count indicating number of array * scan keys processed by parallel * scan */ - - std::mutex btps_mutex; /* protects above variables */ - std::condition_variable btps_cv; /* used to synchronize parallel scan */ } BTParallelScanDescData; typedef struct BTParallelScanDescData *BTParallelScanDesc; @@ -757,10 +752,11 @@ btinitparallelscan(void *target) { BTParallelScanDesc bt_target = (BTParallelScanDesc) target; - std::unique_lock lck(bt_target->btps_mutex); + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); bt_target->btps_scanPage = InvalidBlockNumber; bt_target->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; bt_target->btps_arrayKeyCount = 0; + LWLockRelease(ParallelIndexScanLock); } /* @@ -781,10 +777,11 @@ btparallelrescan(IndexScanDesc scan) * shouldn't be any other workers running at this point, but we do so for * consistency. */ - std::unique_lock lck(btscan->btps_mutex); + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); btscan->btps_scanPage = InvalidBlockNumber; btscan->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; btscan->btps_arrayKeyCount = 0; + LWLockRelease(ParallelIndexScanLock); } /* @@ -819,9 +816,9 @@ _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); - std::unique_lock lck(btscan->btps_mutex); while (1) { + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); pageStatus = btscan->btps_pageStatus; if (so->arrayKeyCount < btscan->btps_arrayKeyCount) @@ -847,10 +844,9 @@ _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) *pageno = btscan->btps_scanPage; exit_loop = true; } + LWLockRelease(ParallelIndexScanLock); if (exit_loop || !status) break; - - btscan->btps_cv.wait(lck); } return status; @@ -870,11 +866,11 @@ _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); { - std::unique_lock lck(btscan->btps_mutex); + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); btscan->btps_scanPage = scan_page; btscan->btps_pageStatus = BTPARALLEL_IDLE; + LWLockRelease(ParallelIndexScanLock); } - btscan->btps_cv.notify_one(); } /* @@ -904,17 +900,14 @@ _bt_parallel_done(IndexScanDesc scan) * _bt_advance_array_keys. */ { - std::unique_lock lck(btscan->btps_mutex); + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); if (so->arrayKeyCount >= btscan->btps_arrayKeyCount && btscan->btps_pageStatus != BTPARALLEL_DONE) { btscan->btps_pageStatus = BTPARALLEL_DONE; status_changed = true; } + LWLockRelease(ParallelIndexScanLock); } - - /* wake up all the workers associated with this parallel scan */ - if (status_changed) - btscan->btps_cv.notify_all(); } /* @@ -934,13 +927,14 @@ _bt_parallel_advance_array_keys(IndexScanDesc scan) btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); so->arrayKeyCount++; - std::unique_lock lck(btscan->btps_mutex); + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); if (btscan->btps_pageStatus == BTPARALLEL_DONE) { btscan->btps_scanPage = InvalidBlockNumber; btscan->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; btscan->btps_arrayKeyCount++; } + LWLockRelease(ParallelIndexScanLock); } /* diff --git a/src/gausskernel/storage/lmgr/lwlocknames.txt b/src/gausskernel/storage/lmgr/lwlocknames.txt index 9934949c9..ebf15340a 100755 --- a/src/gausskernel/storage/lmgr/lwlocknames.txt +++ b/src/gausskernel/storage/lmgr/lwlocknames.txt @@ -144,3 +144,4 @@ RedoTruncateLock 135 ExrtoRecycleResidualUndoLock 137 ShareInputScanLock 138 +ParallelIndexScanLock 139 \ No newline at end of file From 00f2b2f012f08b9274fa6ec2739d040917b10626 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Thu, 1 Aug 2024 16:36:01 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=9C=AA=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/include/knl/knl_thread.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/include/knl/knl_thread.h b/src/include/knl/knl_thread.h index 7bd3bbf18..dda189e2f 100755 --- a/src/include/knl/knl_thread.h +++ b/src/include/knl/knl_thread.h @@ -3647,11 +3647,6 @@ inline bool WorkerThreadAmI() return (t_thrd.role == WORKER || t_thrd.role == THREADPOOL_WORKER); } -inline bool StreamWorkerThreadAmI() -{ - return (t_thrd.role == STREAM_WORKER); -} - inline bool WLMThreadAmI() { return (t_thrd.role == WLM_WORKER || t_thrd.role == WLM_MONITOR || From 74820dfb3c6613422f5a7f868f2691fc78fce7e0 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Mon, 5 Aug 2024 10:02:40 +0800 Subject: [PATCH 08/12] clean code && remove guc parameter smp_thread_cost --- 0001-clean-code-jin.patch | 323 ++++++++++++++++++ src/common/backend/utils/misc/guc/guc_sql.cpp | 14 - src/common/backend/utils/sort/tuplesort.cpp | 2 - src/gausskernel/optimizer/path/costsize.cpp | 12 +- .../runtime/executor/nodeIndexscan.cpp | 9 +- .../storage/access/hbstore/hbindex_am.cpp | 8 +- .../storage/access/index/indexam.cpp | 13 +- .../storage/access/nbtree/nbtree.cpp | 98 +++--- .../storage/access/nbtree/nbtsearch.cpp | 144 +++----- src/include/access/genam.h | 7 +- src/include/access/hbindex_am.h | 3 +- src/include/access/nbtree.h | 4 +- src/include/access/relscan.h | 5 +- src/include/c.h | 2 +- .../executor/node/nodeBitmapHeapscan.h | 2 + 15 files changed, 456 insertions(+), 190 deletions(-) create mode 100644 0001-clean-code-jin.patch diff --git a/0001-clean-code-jin.patch b/0001-clean-code-jin.patch new file mode 100644 index 000000000..1b8c9f96c --- /dev/null +++ b/0001-clean-code-jin.patch @@ -0,0 +1,323 @@ +From d12721addbe02a005ba0e8733c45e78aba88964b Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?=E8=B5=B5=E9=87=91?= +Date: Fri, 9 Aug 2024 11:58:48 +0800 +Subject: [PATCH] clean code jin + +--- + src/gausskernel/process/stream/streamCore.cpp | 6 ++--- + .../storage/access/hbstore/hbindex_am.cpp | 2 +- + .../storage/access/index/indexam.cpp | 8 +++---- + .../storage/access/nbtree/nbtree.cpp | 22 +++++++++---------- + .../storage/access/nbtree/nbtsearch.cpp | 19 ++++++++-------- + .../storage/access/nbtree/nbtutils.cpp | 2 +- + src/include/access/genam.h | 4 ++-- + src/include/access/nbtree.h | 2 +- + src/include/access/relscan.h | 4 ++-- + 9 files changed, 34 insertions(+), 35 deletions(-) + +diff --git a/src/gausskernel/process/stream/streamCore.cpp b/src/gausskernel/process/stream/streamCore.cpp +index d5352986d..25fbd65d3 100755 +--- a/src/gausskernel/process/stream/streamCore.cpp ++++ b/src/gausskernel/process/stream/streamCore.cpp +@@ -1918,7 +1918,7 @@ void StreamNodeGroup::BuildStreamDesc(const uint64& queryId, Plan* node) + parallelDesc = palloc0(sizeof(ParallelIndexScanDescData)); + ((ParallelIndexScanDescData*)parallelDesc)->ps_indexid = ((IndexScan*)node)->indexid; + ((ParallelIndexScanDescData*)parallelDesc)->ps_relid = ((IndexScan*)node)->scan.scanrelid; +- ((ParallelIndexScanDescData*)parallelDesc)->ps_btpscan = btbuildparallelscan(); ++ ((ParallelIndexScanDescData*)parallelDesc)->psBtpscan = Btbuildparallelscan(); + break; + default: + break; +@@ -1946,8 +1946,8 @@ void StreamNodeGroup::DestroyStreamDesc(const uint64& queryId, Plan* node) + return; + } + if (iter->second) { +- if (((ParallelIndexScanDescData*)iter->second)->ps_btpscan) { +- delete ((ParallelIndexScanDescData*)iter->second)->ps_btpscan; ++ if (((ParallelIndexScanDescData*)iter->second)->psBtpscan) { ++ delete ((ParallelIndexScanDescData*)iter->second)->psBtpscan; + } + pfree(iter->second); + } +diff --git a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp +index 16ce1ca84..8113a673c 100644 +--- a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp ++++ b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp +@@ -584,7 +584,7 @@ void scan_handler_idx_rescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey + void scan_handler_idx_rescan_parallel(IndexScanDesc scan) + { + Assert(scan != NULL); +- index_rescan_parallel(scan); ++ IndexRescanParallel(scan); + } + + void scan_handler_idx_rescan_local(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys) +diff --git a/src/gausskernel/storage/access/index/indexam.cpp b/src/gausskernel/storage/access/index/indexam.cpp +index 7007d1473..b85522a6c 100644 +--- a/src/gausskernel/storage/access/index/indexam.cpp ++++ b/src/gausskernel/storage/access/index/indexam.cpp +@@ -329,7 +329,7 @@ static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys + #endif + + /* Initialize information for parallel scan. */ +- scan->parallel_scan = pscan; ++ scan->parallelScan = pscan; + + return scan; + } +@@ -383,9 +383,9 @@ void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, + * ---------------- + */ + +-void index_rescan_parallel(IndexScanDesc scan) ++void IndexRescanParallel(IndexScanDesc scan) + { +- if (scan->parallel_scan) { ++ if (scan->parallelScan) { + btparallelrescan(scan); + } + } +@@ -518,7 +518,7 @@ void index_parallelscan_initialize(Relation heap_relation, Relation index_relati + target->ps_relid = RelationGetRelid(heap_relation); + target->ps_indexid = RelationGetRelid(index_relation); + +- btinitparallelscan(target->ps_btpscan); ++ btinitparallelscan(target->psBtpscan); + } + + /* ---------------- +diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp +index 183fe7af7..cea5cccab 100644 +--- a/src/gausskernel/storage/access/nbtree/nbtree.cpp ++++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp +@@ -762,11 +762,11 @@ void Btinitparallelscan(void *target) + void btparallelrescan(IndexScanDesc scan) + { + BTParallelScanDesc btscan; +- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; ++ ParallelIndexScanDesc parallel_scan = scan->parallelScan; + + Assert(parallel_scan); + +- btscan = (BTParallelScanDesc) parallel_scan->ps_btpscan; ++ btscan = (BTParallelScanDesc) parallel_scan->psBtpscan; + + /* + * In theory, we don't need to acquire the spinlock here, because there +@@ -804,12 +804,12 @@ bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) + BTPS_State pageStatus; + bool exitLoop = false; + bool status = true; +- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; ++ ParallelIndexScanDesc parallel_scan = scan->parallelScan; + BTParallelScanDesc btscan; + + *pageno = P_NONE; + +- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); ++ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); + + while (1) { + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); +@@ -849,10 +849,10 @@ bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) + */ + void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) + { +- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; ++ ParallelIndexScanDesc parallel_scan = scan->parallelScan; + BTParallelScanDesc btscan; + +- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); ++ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); + + { + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); +@@ -872,7 +872,7 @@ void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) + void _bt_parallel_done(IndexScanDesc scan) + { + BTScanOpaque so = (BTScanOpaque) scan->opaque; +- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; ++ ParallelIndexScanDesc parallel_scan = scan->parallelScan; + BTParallelScanDesc btscan; + bool statusChanged = false; + +@@ -881,7 +881,7 @@ void _bt_parallel_done(IndexScanDesc scan) + return; + } + +- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); ++ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); + + /* + * Mark the parallel scan as done for this combination of scan keys, +@@ -909,10 +909,10 @@ void _bt_parallel_done(IndexScanDesc scan) + void _bt_parallel_advance_array_keys(IndexScanDesc scan) + { + BTScanOpaque so = (BTScanOpaque) scan->opaque; +- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; ++ ParallelIndexScanDesc parallel_scan = scan->parallelScan; + BTParallelScanDesc btscan; + +- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); ++ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); + + so->arrayKeyCount++; + LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); +@@ -1621,7 +1621,7 @@ static BTVacuumPosting btree_vacuum_posting(BTVacState *vac_state, IndexTuple po + /* + * btestimateparallelscan -- estimate storage for BTParallelScanDescData + */ +-void* btbuildparallelscan(void) ++void* Btbuildparallelscan(void) + { + void *btPscan = new BTParallelScanDescData; + return btPscan; +diff --git a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp +index d2e16c848..3b91e0957 100644 +--- a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp ++++ b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp +@@ -620,7 +620,7 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) + * way while keeping other participating processes waiting. If the scan + * has already begun, use the page number from the shared structure. + */ +- if (scan->parallel_scan != NULL) { ++ if (scan->parallelScan != NULL) { + status = _bt_parallel_seize(scan, &blkno); + if (!status) { + return false; +@@ -1215,7 +1215,7 @@ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber off + opaque = (BTPageOpaqueInternal)PageGetSpecialPointer(page); + + /* allow next page be processed by parallel worker */ +- if (scan->parallel_scan) { ++ if (scan->parallelScan) { + if (ScanDirectionIsForward(dir)) + _bt_parallel_release(scan, opaque->btpo_next); + else +@@ -1406,7 +1406,7 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) + so->currPos.buf = InvalidBuffer; + + /* Walk right to the next page with data */ +- if (scan->parallel_scan != NULL) { ++ if (scan->parallelScan != NULL) { + /* + * Seize the scan to get the next block number; if the scan has + * ended already, bail out. +@@ -1424,7 +1424,7 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) + } else { + /* Remember we left a page with data */ + so->currPos.moreRight = true; +- if (scan->parallel_scan != NULL) { ++ if (scan->parallelScan != NULL) { + /* + * Seize the scan to get the current block number; if the scan has + * ended already, bail out. +@@ -1492,7 +1492,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, + if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque))) { + break; + } +- } else if (scan->parallel_scan != NULL) { ++ } else if (scan->parallelScan != NULL) { + /* allow next page be processed by parallel worker */ + _bt_parallel_release(scan, opaque->btpo_next); + } +@@ -1502,7 +1502,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, + so->currPos.buf = InvalidBuffer; + + /* nope, keep going */ +- if (scan->parallel_scan != NULL) { ++ if (scan->parallelScan != NULL) { + status = _bt_parallel_seize(scan, &blkno); + if (!status) { + return false; +@@ -1577,7 +1577,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, + if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page))) { + break; + } +- } else if (scan->parallel_scan != NULL) { ++ } else if (scan->parallelScan != NULL) { + /* allow next page be processed by parallel worker */ + _bt_parallel_release(scan, BufferGetBlockNumber(so->currPos.buf)); + } +@@ -1588,11 +1588,10 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, + * worker has already advanced the scan to a different page. We + * must continue based on the latest page scanned by any worker. + */ +- if (scan->parallel_scan != NULL) { ++ if (scan->parallelScan != NULL) { + _bt_relbuf(rel, so->currPos.buf); + status = _bt_parallel_seize(scan, &blkno); +- if (!status) +- { ++ if (!status) { + so->currPos.buf = InvalidBuffer; + return false; + } +diff --git a/src/gausskernel/storage/access/nbtree/nbtutils.cpp b/src/gausskernel/storage/access/nbtree/nbtutils.cpp +index 1c477b6b0..616da34cc 100644 +--- a/src/gausskernel/storage/access/nbtree/nbtutils.cpp ++++ b/src/gausskernel/storage/access/nbtree/nbtutils.cpp +@@ -555,7 +555,7 @@ bool _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir) + } + + /* advance parallel scan */ +- if (scan->parallel_scan != NULL) ++ if (scan->parallelScan != NULL) + _bt_parallel_advance_array_keys(scan); + + return found; +diff --git a/src/include/access/genam.h b/src/include/access/genam.h +index b14da2568..99044ac07 100644 +--- a/src/include/access/genam.h ++++ b/src/include/access/genam.h +@@ -136,10 +136,10 @@ extern bool index_insert(Relation indexRelation, Datum* values, const bool* isnu + extern IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, + int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); + extern void index_parallelscan_initialize(Relation heap_relation, +- Relation index_relation, ParallelIndexScanDesc p_index_scan); ++ Relation index_relation, ParallelIndexScanDesc pIndexScan); + extern IndexScanDesc index_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state=NULL); + extern void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys); +-extern void index_rescan_parallel(IndexScanDesc scan); ++extern void IndexRescanParallel(IndexScanDesc scan); + extern void index_endscan(IndexScanDesc scan); + extern void index_markpos(IndexScanDesc scan); + extern void index_restrpos(IndexScanDesc scan); +diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h +index e0e5afa38..bb6d1e83c 100644 +--- a/src/include/access/nbtree.h ++++ b/src/include/access/nbtree.h +@@ -1286,7 +1286,7 @@ extern Datum btbuild(PG_FUNCTION_ARGS); + extern Datum btbuildempty(PG_FUNCTION_ARGS); + extern Datum btinsert(PG_FUNCTION_ARGS); + extern Datum btbeginscan(PG_FUNCTION_ARGS); +-extern void* btbuildparallelscan(void); ++extern void* Btbuildparallelscan(void); + extern void Btinitparallelscan(void *target); + extern Datum btgettuple(PG_FUNCTION_ARGS); + extern Datum btgetbitmap(PG_FUNCTION_ARGS); +diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h +index cb40b95df..cc67b1be8 100644 +--- a/src/include/access/relscan.h ++++ b/src/include/access/relscan.h +@@ -171,7 +171,7 @@ typedef struct IndexScanDescData { + #endif + IndexFetchTableData *xs_heapfetch; + /* parallel index scan information, in global variables */ +- struct ParallelIndexScanDescData *parallel_scan; ++ struct ParallelIndexScanDescData *parallelScan; + /* put decompressed heap tuple data into xs_ctbuf_hdr be careful! when malloc memory should give extra mem for + *xs_ctbuf_hdr. t_bits which is varlength arr + */ +@@ -183,7 +183,7 @@ typedef struct IndexScanDescData { + typedef struct ParallelIndexScanDescData { + Oid ps_relid; + Oid ps_indexid; +- void* ps_btpscan; ++ void* psBtpscan; + } ParallelIndexScanDescData; + + #define SizeofIndexScanDescData (offsetof(IndexScanDescData, xs_ctbuf_hdr) + SizeofHeapTupleHeader) +-- +2.39.1.windows.1 + diff --git a/src/common/backend/utils/misc/guc/guc_sql.cpp b/src/common/backend/utils/misc/guc/guc_sql.cpp index 79d21e17e..8a989d820 100755 --- a/src/common/backend/utils/misc/guc/guc_sql.cpp +++ b/src/common/backend/utils/misc/guc/guc_sql.cpp @@ -2584,20 +2584,6 @@ static void InitSqlConfigureNamesReal() NULL, NULL, NULL}, - {{"smp_thread_cost", - PGC_USERSET, - NODE_ALL, - QUERY_TUNING_COST, - gettext_noop("Sets the planner's estimate of the cost of a " - "smp thread cost."), - NULL}, - &u_sess->opt_cxt.smp_thread_cost, - DEFAULT_SMP_THREAD_COST, - 0, - DBL_MAX, - NULL, - NULL, - NULL}, {{"cpu_tuple_cost", PGC_USERSET, NODE_ALL, diff --git a/src/common/backend/utils/sort/tuplesort.cpp b/src/common/backend/utils/sort/tuplesort.cpp index c687adfdc..718290103 100644 --- a/src/common/backend/utils/sort/tuplesort.cpp +++ b/src/common/backend/utils/sort/tuplesort.cpp @@ -1968,8 +1968,6 @@ static bool tuplesort_gettuple_common(Tuplesortstate* state, bool forward, SortT case TSS_SORTEDONTAPE: Assert(forward || state->randomAccess); - Assert(state->slabAllocatorUsed); - /* * The slot that held the tuple that we returned in previous * gettuple call can now be reused. diff --git a/src/gausskernel/optimizer/path/costsize.cpp b/src/gausskernel/optimizer/path/costsize.cpp index a263fb486..0ea00e50e 100755 --- a/src/gausskernel/optimizer/path/costsize.cpp +++ b/src/gausskernel/optimizer/path/costsize.cpp @@ -1221,7 +1221,11 @@ void cost_index(IndexPath* path, PlannerInfo* root, double loop_count) */ csquared = indexCorrelation * indexCorrelation; - run_cost += (max_IO_cost + csquared * (min_IO_cost - max_IO_cost)) / dop; + if (dop == 0) { + run_cost += (max_IO_cost + csquared * (min_IO_cost - max_IO_cost)); + } else { + run_cost += (max_IO_cost + csquared * (min_IO_cost - max_IO_cost)) / dop; + } ereport(DEBUG2, (errmodule(MOD_OPT), @@ -1256,7 +1260,11 @@ void cost_index(IndexPath* path, PlannerInfo* root, double loop_count) cpu_per_tuple = u_sess->attr.attr_sql.cpu_tuple_cost + qpqual_cost.per_tuple; run_cost += u_sess->opt_cxt.smp_thread_cost * (dop - 1); - run_cost += cpu_per_tuple * tuples_fetched / dop; + if (dop == 0) { + run_cost += cpu_per_tuple * tuples_fetched; + } else { + run_cost += cpu_per_tuple * tuples_fetched / dop; + } ereport(DEBUG2, (errmodule(MOD_OPT), diff --git a/src/gausskernel/runtime/executor/nodeIndexscan.cpp b/src/gausskernel/runtime/executor/nodeIndexscan.cpp index 3668857d9..b3c134a1e 100644 --- a/src/gausskernel/runtime/executor/nodeIndexscan.cpp +++ b/src/gausskernel/runtime/executor/nodeIndexscan.cpp @@ -468,7 +468,8 @@ void ExecEndIndexScan(IndexScanState* node) if (index_scan_desc) { scan_handler_idx_endscan(index_scan_desc); if (WorkerThreadAmI() && node->ss.ps.plan->dop > 1) { - u_sess->stream_cxt.global_obj->DestroyStreamDesc(node->ss.ps.state->es_plannedstmt->queryId, node->ss.ps.plan); + u_sess->stream_cxt.global_obj->DestroyStreamDesc( + node->ss.ps.state->es_plannedstmt->queryId, node->ss.ps.plan); } } @@ -636,9 +637,11 @@ void ExecInitIndexRelation(IndexScanState* node, EState* estate, int eflags) ParallelIndexScanDescData *paralleDesc = NULL; if (u_sess->stream_cxt.global_obj && node->ss.ps.plan->dop > 1) { if (WorkerThreadAmI()) { - u_sess->stream_cxt.global_obj->BuildStreamDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan); + u_sess->stream_cxt.global_obj->BuildStreamDesc( + estate->es_plannedstmt->queryId, index_state->ss.ps.plan); } - paralleDesc = (ParallelIndexScanDescData*)u_sess->stream_cxt.global_obj->GetParalleDesc(estate->es_plannedstmt->queryId, index_state->ss.ps.plan->plan_node_id); + paralleDesc = (ParallelIndexScanDescData*)u_sess->stream_cxt.global_obj->GetParalleDesc( + estate->es_plannedstmt->queryId, index_state->ss.ps.plan->plan_node_id); if (WorkerThreadAmI()) scan_handler_idx_parallelscan_initialize(current_relation, index_state->iss_RelationDesc, paralleDesc); } diff --git a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp index 1eac6115a..16ce1ca84 100644 --- a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp +++ b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp @@ -545,8 +545,8 @@ static HeapTuple cross_level_index_getnext(IndexScanDesc scan, ScanDirection dir * ------------------------------------------------------------------------ */ -IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state, - ParallelIndexScanDesc pscan) +IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, + int nkeys, int norderbys, ScanState* scan_state, ParallelIndexScanDesc pscan) { if (unlikely(RELATION_OWN_BUCKET(heap_relation))) { return hbkt_idx_beginscan(heap_relation, index_relation, snapshot, nkeys, norderbys, scan_state); @@ -555,7 +555,9 @@ IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_ } } -void scan_handler_idx_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan) { +void scan_handler_idx_parallelscan_initialize(Relation heap_relation, + Relation index_relation, ParallelIndexScanDesc p_index_scan) +{ index_parallelscan_initialize(heap_relation, index_relation, p_index_scan); } diff --git a/src/gausskernel/storage/access/index/indexam.cpp b/src/gausskernel/storage/access/index/indexam.cpp index 377ec244b..7007d1473 100644 --- a/src/gausskernel/storage/access/index/indexam.cpp +++ b/src/gausskernel/storage/access/index/indexam.cpp @@ -144,8 +144,8 @@ } \ } while (0) -static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot - ,ParallelIndexScanDesc pscan = NULL); +static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, + ParallelIndexScanDesc pscan = NULL); /* ---------------- * index_open - open an index relation by relation OID @@ -298,8 +298,8 @@ IndexScanDesc index_beginscan_bitmap(Relation index_relation, Snapshot snapshot, /* * index_beginscan_internal --- common code for index_beginscan variants */ -static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, - ParallelIndexScanDesc pscan) +static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, + ParallelIndexScanDesc pscan) { IndexScanDesc scan; FmgrInfo *procedure = NULL; @@ -505,9 +505,8 @@ void index_restrpos(IndexScanDesc scan) * initialize am specific information. Call this just once in the leader * process; then, individual workers attach via index_beginscan_parallel. */ -void -index_parallelscan_initialize(Relation heap_relation, Relation index_relation, - ParallelIndexScanDesc target) +void index_parallelscan_initialize(Relation heap_relation, Relation index_relation, + ParallelIndexScanDesc target) { if (!target) { return; diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp index 376964a5f..183fe7af7 100644 --- a/src/gausskernel/storage/access/nbtree/nbtree.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp @@ -64,25 +64,23 @@ typedef struct { * BTPARALLEL_DONE indicates that the scan is complete (including error exit). * We reach this state once for every distinct combination of array keys. */ -typedef enum -{ +typedef enum BTPS_State { BTPARALLEL_NOT_INITIALIZED, BTPARALLEL_ADVANCING, BTPARALLEL_IDLE, BTPARALLEL_DONE -} BTPS_State; +}; /* * BTParallelScanDescData contains btree specific shared information required * for parallel scan. */ -typedef struct BTParallelScanDescData -{ +typedef struct BTParallelScanDescData { BlockNumber btps_scanPage; /* latest or next page to be scanned */ - BTPS_State btps_pageStatus;/* indicates whether next page is available + BTPS_State btpsPageStatus;/* indicates whether next page is available * for scan. see above for possible states of * parallel scan. */ - int btps_arrayKeyCount; /* count indicating number of array + int btpsArrayKeyCount; /* count indicating number of array * scan keys processed by parallel * scan */ } BTParallelScanDescData; @@ -745,25 +743,23 @@ void btrestrpos_internal(IndexScanDesc scan) /* - * btinitparallelscan -- initialize BTParallelScanDesc for parallel btree scan + * Btinitparallelscan -- initialize BTParallelScanDesc for parallel btree scan */ -void -btinitparallelscan(void *target) +void Btinitparallelscan(void *target) { - BTParallelScanDesc bt_target = (BTParallelScanDesc) target; + BTParallelScanDesc btTarget = (BTParallelScanDesc) target; LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); - bt_target->btps_scanPage = InvalidBlockNumber; - bt_target->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; - bt_target->btps_arrayKeyCount = 0; + btTarget->btps_scanPage = InvalidBlockNumber; + btTarget->btpsPageStatus = BTPARALLEL_NOT_INITIALIZED; + btTarget->btpsArrayKeyCount = 0; LWLockRelease(ParallelIndexScanLock); } /* * btparallelrescan() -- reset parallel scan */ -void -btparallelrescan(IndexScanDesc scan) +void btparallelrescan(IndexScanDesc scan) { BTParallelScanDesc btscan; ParallelIndexScanDesc parallel_scan = scan->parallel_scan; @@ -779,8 +775,8 @@ btparallelrescan(IndexScanDesc scan) */ LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); btscan->btps_scanPage = InvalidBlockNumber; - btscan->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; - btscan->btps_arrayKeyCount = 0; + btscan->btpsPageStatus = BTPARALLEL_NOT_INITIALIZED; + btscan->btpsArrayKeyCount = 0; LWLockRelease(ParallelIndexScanLock); } @@ -802,12 +798,11 @@ btparallelrescan(IndexScanDesc scan) * * Callers should ignore the value of pageno if the return value is false. */ -bool -_bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) +bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) { BTScanOpaque so = (BTScanOpaque) scan->opaque; BTPS_State pageStatus; - bool exit_loop = false; + bool exitLoop = false; bool status = true; ParallelIndexScanDesc parallel_scan = scan->parallel_scan; BTParallelScanDesc btscan; @@ -816,37 +811,32 @@ _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); - while (1) - { + while (1) { LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); - pageStatus = btscan->btps_pageStatus; + pageStatus = btscan->btpsPageStatus; - if (so->arrayKeyCount < btscan->btps_arrayKeyCount) - { + if (so->arrayKeyCount < btscan->btpsArrayKeyCount) { /* Parallel scan has already advanced to a new set of scankeys. */ status = false; - } - else if (pageStatus == BTPARALLEL_DONE) - { + } else if (pageStatus == BTPARALLEL_DONE) { /* * We're done with this set of scankeys. This may be the end, or * there could be more sets to try. */ status = false; - } - else if (pageStatus != BTPARALLEL_ADVANCING) - { + } else if (pageStatus != BTPARALLEL_ADVANCING) { /* * We have successfully seized control of the scan for the purpose * of advancing it to a new page! */ - btscan->btps_pageStatus = BTPARALLEL_ADVANCING; + btscan->btpsPageStatus = BTPARALLEL_ADVANCING; *pageno = btscan->btps_scanPage; - exit_loop = true; + exitLoop = true; } LWLockRelease(ParallelIndexScanLock); - if (exit_loop || !status) + if (exitLoop || !status) { break; + } } return status; @@ -857,8 +847,7 @@ _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) * new page. We now have the new value btps_scanPage; some other backend * can now begin advancing the scan. */ -void -_bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) +void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) { ParallelIndexScanDesc parallel_scan = scan->parallel_scan; BTParallelScanDesc btscan; @@ -868,7 +857,7 @@ _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) { LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); btscan->btps_scanPage = scan_page; - btscan->btps_pageStatus = BTPARALLEL_IDLE; + btscan->btpsPageStatus = BTPARALLEL_IDLE; LWLockRelease(ParallelIndexScanLock); } } @@ -880,17 +869,17 @@ _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) * notify other workers. Otherwise, they might wait forever for the scan to * advance to the next page. */ -void -_bt_parallel_done(IndexScanDesc scan) +void _bt_parallel_done(IndexScanDesc scan) { BTScanOpaque so = (BTScanOpaque) scan->opaque; ParallelIndexScanDesc parallel_scan = scan->parallel_scan; BTParallelScanDesc btscan; - bool status_changed = false; + bool statusChanged = false; /* Do nothing, for non-parallel scans */ - if (parallel_scan == NULL) + if (parallel_scan == NULL) { return; + } btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); @@ -901,10 +890,10 @@ _bt_parallel_done(IndexScanDesc scan) */ { LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); - if (so->arrayKeyCount >= btscan->btps_arrayKeyCount - && btscan->btps_pageStatus != BTPARALLEL_DONE) { - btscan->btps_pageStatus = BTPARALLEL_DONE; - status_changed = true; + if (so->arrayKeyCount >= btscan->btpsArrayKeyCount + && btscan->btpsPageStatus != BTPARALLEL_DONE) { + btscan->btpsPageStatus = BTPARALLEL_DONE; + statusChanged = true; } LWLockRelease(ParallelIndexScanLock); } @@ -917,8 +906,7 @@ _bt_parallel_done(IndexScanDesc scan) * Updates the count of array keys processed for both local and parallel * scans. */ -void -_bt_parallel_advance_array_keys(IndexScanDesc scan) +void _bt_parallel_advance_array_keys(IndexScanDesc scan) { BTScanOpaque so = (BTScanOpaque) scan->opaque; ParallelIndexScanDesc parallel_scan = scan->parallel_scan; @@ -928,11 +916,10 @@ _bt_parallel_advance_array_keys(IndexScanDesc scan) so->arrayKeyCount++; LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); - if (btscan->btps_pageStatus == BTPARALLEL_DONE) - { + if (btscan->btpsPageStatus == BTPARALLEL_DONE) { btscan->btps_scanPage = InvalidBlockNumber; - btscan->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED; - btscan->btps_arrayKeyCount++; + btscan->btpsPageStatus = BTPARALLEL_NOT_INITIALIZED; + btscan->btpsArrayKeyCount++; } LWLockRelease(ParallelIndexScanLock); } @@ -1634,9 +1621,8 @@ static BTVacuumPosting btree_vacuum_posting(BTVacState *vac_state, IndexTuple po /* * btestimateparallelscan -- estimate storage for BTParallelScanDescData */ -void* -btbuildparallelscan(void) +void* btbuildparallelscan(void) { - void *bt_pscan = (void*)new BTParallelScanDescData; - return bt_pscan; + void *btPscan = new BTParallelScanDescData; + return btPscan; } diff --git a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp index dcf5bb5a4..d2e16c848 100644 --- a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp @@ -36,8 +36,7 @@ static void _bt_saveitem(BTScanOpaque so, int itemIndex, OffsetNumber offnum, In int2 bucketid); static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir); static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir); -static bool _bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, - ScanDirection dir); +static bool _bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir); static bool _bt_endpoint(IndexScanDesc scan, ScanDirection dir); static void _bt_check_natts_correct(const Relation index, bool heapkeyspace, Page page, OffsetNumber offnum); static inline void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir); @@ -621,20 +620,17 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) * way while keeping other participating processes waiting. If the scan * has already begun, use the page number from the shared structure. */ - if (scan->parallel_scan != NULL) - { + if (scan->parallel_scan != NULL) { status = _bt_parallel_seize(scan, &blkno); - if (!status) + if (!status) { return false; - else if (blkno == P_NONE) - { + } else if (blkno == P_NONE) { _bt_parallel_done(scan); return false; - } - else if (blkno != InvalidBlockNumber) - { - if (!_bt_parallel_readpage(scan, blkno, dir)) + } else if (blkno != InvalidBlockNumber) { + if (!_bt_parallel_readpage(scan, blkno, dir)) { return false; + } goto readcomplete; } } @@ -803,18 +799,13 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) * the tree. Walk down that edge to the first or last key, and scan from * there. */ - if (keysCount == 0) - { + if (keysCount == 0) { bool match; - match = _bt_endpoint(scan, dir); - - if (!match) - { + if (!match) { /* No match, so mark (parallel) scan finished */ _bt_parallel_done(scan); } - return match; } @@ -842,8 +833,7 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) */ ScanKey subkey = (ScanKey)DatumGetPointer(cur->sk_argument); Assert(subkey->sk_flags & SK_ROW_MEMBER); - if (subkey->sk_flags & SK_ISNULL) - { + if (subkey->sk_flags & SK_ISNULL) { _bt_parallel_done(scan); return false; } @@ -1225,8 +1215,7 @@ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber off opaque = (BTPageOpaqueInternal)PageGetSpecialPointer(page); /* allow next page be processed by parallel worker */ - if (scan->parallel_scan) - { + if (scan->parallel_scan) { if (ScanDirectionIsForward(dir)) _bt_parallel_release(scan, opaque->btpo_next); else @@ -1411,59 +1400,49 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) rel = scan->indexRelation; /* release the previous buffer, if pinned */ - _bt_relbuf(rel, so->currPos.buf); + _bt_relbuf(rel, so->currPos.buf); - if (ScanDirectionIsForward(dir)) { + if (ScanDirectionIsForward(dir)) { so->currPos.buf = InvalidBuffer; /* Walk right to the next page with data */ - if (scan->parallel_scan != NULL) - { + if (scan->parallel_scan != NULL) { /* * Seize the scan to get the next block number; if the scan has * ended already, bail out. */ status = _bt_parallel_seize(scan, &blkno); - if (!status) - { + if (!status) { return false; } - } - else - { + } else { /* Not parallel, so use the previously-saved nextPage link. */ blkno = so->currPos.nextPage; } - /* Remember we left a page with data */ so->currPos.moreLeft = true; - } else { /* Remember we left a page with data */ so->currPos.moreRight = true; - - if (scan->parallel_scan != NULL) - { + if (scan->parallel_scan != NULL) { /* * Seize the scan to get the current block number; if the scan has * ended already, bail out. */ status = _bt_parallel_seize(scan, &blkno); - if (!status) - { + if (!status) { so->currPos.buf = InvalidBuffer; return false; } - } - else - { + } else { /* Not parallel, so just use our own notion of the current page */ blkno = so->currPos.currPage; } } - if (!_bt_readnextpage(scan, blkno, dir)) + if (!_bt_readnextpage(scan, blkno, dir)) { return false; + } return true; } @@ -1478,8 +1457,8 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) * If there are no more matching records in the given direction, we drop all * locks and pins, set so->currPos.buf to InvalidBuffer, and return false. */ -static bool -_bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) +static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, + ScanDirection dir) { BTScanOpaque so = (BTScanOpaque) scan->opaque; Relation rel; @@ -1489,16 +1468,13 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) rel = scan->indexRelation; - if (ScanDirectionIsForward(dir)) - { - for (;;) - { + if (ScanDirectionIsForward(dir)) { + for (;;) { /* * if we're at end of scan, give up and mark parallel scan as * done, so that all the workers can finish their scan */ - if (blkno == P_NONE || !so->currPos.moreRight) - { + if (blkno == P_NONE || !so->currPos.moreRight) { _bt_parallel_done(scan); return false; } @@ -1509,16 +1485,14 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) page = BufferGetPage(so->currPos.buf); opaque = BTPageGetOpaqueInternal(page); /* check for deleted page */ - if (!P_IGNORE(opaque)) - { + if (!P_IGNORE(opaque)) { PredicateLockPage(rel, blkno, scan->xs_snapshot); /* see if there are any matches on this page */ /* note that this will clear moreRight if we can stop */ - if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque))) + if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque))) { break; - } - else if (scan->parallel_scan != NULL) - { + } + } else if (scan->parallel_scan != NULL) { /* allow next page be processed by parallel worker */ _bt_parallel_release(scan, opaque->btpo_next); } @@ -1528,22 +1502,16 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) so->currPos.buf = InvalidBuffer; /* nope, keep going */ - if (scan->parallel_scan != NULL) - { + if (scan->parallel_scan != NULL) { status = _bt_parallel_seize(scan, &blkno); - if (!status) - { + if (!status) { return false; } - } - else - { + } else { blkno = opaque->btpo_next; } } - } - else - { + } else { /* * Should only happen in parallel cases, when some other backend * advanced the scan. @@ -1575,11 +1543,9 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) * deleted. */ - for (;;) - { + for (;;) { /* Done if we know there are no matching keys to the left */ - if (!so->currPos.moreLeft) - { + if (!so->currPos.moreLeft) { _bt_relbuf(rel, so->currPos.buf); _bt_parallel_done(scan); so->currPos.buf = InvalidBuffer; @@ -1587,13 +1553,12 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) } /* Step to next physical page */ - Buffer temp = so->currPos.buf; - so->currPos.buf = InvalidBuffer; + Buffer temp = so->currPos.buf; + so->currPos.buf = InvalidBuffer; so->currPos.buf = _bt_walk_left(rel, temp); /* if we're physically at end of index, return failure */ - if (so->currPos.buf == InvalidBuffer) - { + if (so->currPos.buf == InvalidBuffer) { _bt_parallel_done(scan); return false; } @@ -1605,16 +1570,14 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) */ page = BufferGetPage(so->currPos.buf); opaque = BTPageGetOpaqueInternal(page); - if (!P_IGNORE(opaque)) - { + if (!P_IGNORE(opaque)) { PredicateLockPage(rel, BufferGetBlockNumber(so->currPos.buf), scan->xs_snapshot); /* see if there are any matches on this page */ /* note that this will clear moreLeft if we can stop */ - if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page))) + if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page))) { break; - } - else if (scan->parallel_scan != NULL) - { + } + } else if (scan->parallel_scan != NULL) { /* allow next page be processed by parallel worker */ _bt_parallel_release(scan, BufferGetBlockNumber(so->currPos.buf)); } @@ -1625,8 +1588,7 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) * worker has already advanced the scan to a different page. We * must continue based on the latest page scanned by any worker. */ - if (scan->parallel_scan != NULL) - { + if (scan->parallel_scan != NULL) { _bt_relbuf(rel, so->currPos.buf); status = _bt_parallel_seize(scan, &blkno); if (!status) @@ -1648,16 +1610,16 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) * On success, release lock and maybe pin on buffer. We return true to * indicate success. */ -static bool -_bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) +static bool _bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir) { BTScanOpaque so = (BTScanOpaque) scan->opaque; _bt_initialize_more_data(so, dir); - if (!_bt_readnextpage(scan, blkno, dir)) + if (!_bt_readnextpage(scan, blkno, dir)) { return false; - + } + /* Drop the lock, but not pin, on the new page */ LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); return true; @@ -2122,17 +2084,13 @@ static inline void btree_save_posting_item(BTScanOpaque so, int item_idx, Offset * _bt_initialize_more_data() -- initialize moreLeft/moreRight appropriately * for scan direction */ -static inline void -_bt_initialize_more_data(BTScanOpaque so, ScanDirection dir) +static inline void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir) { /* initialize moreLeft/moreRight appropriately for scan direction */ - if (ScanDirectionIsForward(dir)) - { + if (ScanDirectionIsForward(dir)) { so->currPos.moreLeft = false; so->currPos.moreRight = true; - } - else - { + } else { so->currPos.moreLeft = true; so->currPos.moreRight = false; } diff --git a/src/include/access/genam.h b/src/include/access/genam.h index ac48b2f58..b14da2568 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -133,9 +133,10 @@ extern void index_delete(Relation index_relation, Datum* values, const bool* isn extern bool index_insert(Relation indexRelation, Datum* values, const bool* isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique); -extern IndexScanDesc index_beginscan( - Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state=NULL, ParallelIndexScanDesc pscan=NULL); -extern void index_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan); +extern IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, + int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); +extern void index_parallelscan_initialize(Relation heap_relation, + Relation index_relation, ParallelIndexScanDesc p_index_scan); extern IndexScanDesc index_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state=NULL); extern void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys); extern void index_rescan_parallel(IndexScanDesc scan); diff --git a/src/include/access/hbindex_am.h b/src/include/access/hbindex_am.h index 8aa2765bd..29a45fd24 100644 --- a/src/include/access/hbindex_am.h +++ b/src/include/access/hbindex_am.h @@ -50,7 +50,8 @@ extern bool cbi_scan_need_fix_hbkt_rel(IndexScanDesc scan, int2 bucketid = Inval extern bool cbi_scan_fix_hbkt_rel(HBktIdxScanDesc hpScan); extern IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); - extern void scan_handler_idx_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan); +extern void scan_handler_idx_parallelscan_initialize(Relation heap_relation, + Relation index_relation, ParallelIndexScanDesc p_index_scan); extern IndexScanDesc scan_handler_idx_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state); extern void scan_handler_idx_rescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys); extern void scan_handler_idx_rescan_parallel(IndexScanDesc scan); diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h index a8b2c16e0..e0e5afa38 100644 --- a/src/include/access/nbtree.h +++ b/src/include/access/nbtree.h @@ -1287,7 +1287,7 @@ extern Datum btbuildempty(PG_FUNCTION_ARGS); extern Datum btinsert(PG_FUNCTION_ARGS); extern Datum btbeginscan(PG_FUNCTION_ARGS); extern void* btbuildparallelscan(void); -extern void btinitparallelscan(void *target); +extern void Btinitparallelscan(void *target); extern Datum btgettuple(PG_FUNCTION_ARGS); extern Datum btgetbitmap(PG_FUNCTION_ARGS); extern Datum cbtreegetbitmap(PG_FUNCTION_ARGS); @@ -1301,7 +1301,7 @@ extern Datum btcanreturn(PG_FUNCTION_ARGS); extern Datum btoptions(PG_FUNCTION_ARGS); extern void btparallelrescan(IndexScanDesc scan); -extern void btinitparallelscan(void *target); +extern void Btinitparallelscan(void *target); /* * prototypes for internal functions in nbtree.c */ diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h index 878f657fd..cb40b95df 100644 --- a/src/include/access/relscan.h +++ b/src/include/access/relscan.h @@ -180,12 +180,11 @@ typedef struct IndexScanDescData { } IndexScanDescData; /* Generic structure for parallel scans */ -typedef struct ParallelIndexScanDescData -{ +typedef struct ParallelIndexScanDescData { Oid ps_relid; Oid ps_indexid; void* ps_btpscan; -} ParallelIndexScanDescData; +} ParallelIndexScanDescData; #define SizeofIndexScanDescData (offsetof(IndexScanDescData, xs_ctbuf_hdr) + SizeofHeapTupleHeader) diff --git a/src/include/c.h b/src/include/c.h index 5c02faa24..5b6a61990 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -723,7 +723,7 @@ typedef struct pathData { */ #define PointerIsAligned(pointer, type) (((intptr_t)(pointer) % (sizeof(type))) == 0) #define OffsetToPointer(base, offset) \ - ((void *)((char *) base + offset)) + ((void *)((char *)(base) + (offset))) #define OidIsValid(objectId) ((bool)((objectId) != InvalidOid)) diff --git a/src/include/executor/node/nodeBitmapHeapscan.h b/src/include/executor/node/nodeBitmapHeapscan.h index 0b0f1a411..9f6560123 100644 --- a/src/include/executor/node/nodeBitmapHeapscan.h +++ b/src/include/executor/node/nodeBitmapHeapscan.h @@ -29,4 +29,6 @@ extern int TableScanBitmapNextTargetRel(TableScanDesc scan, BitmapHeapScanState extern TupleTableSlot* ExecBitmapHeapScan(PlanState* state); extern void ExecInitPartitionForBitmapHeapScan(BitmapHeapScanState* scanstate, EState* estate); +#define BITMAP_PREFETCH_PAGE_RATIO 2 + #endif /* NODEBITMAPHEAPSCAN_H */ From d0f3f71289b7db30b5ffe27f9ed2fba8fbc2663e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=87=91?= Date: Fri, 9 Aug 2024 11:58:48 +0800 Subject: [PATCH 09/12] clean code jin --- src/gausskernel/process/stream/streamCore.cpp | 6 ++--- .../storage/access/hbstore/hbindex_am.cpp | 2 +- .../storage/access/index/indexam.cpp | 8 +++---- .../storage/access/nbtree/nbtree.cpp | 22 +++++++++--------- .../storage/access/nbtree/nbtsearch.cpp | 23 +++++++++---------- .../storage/access/nbtree/nbtutils.cpp | 2 +- src/include/access/genam.h | 4 ++-- src/include/access/nbtree.h | 2 +- src/include/access/relscan.h | 4 ++-- 9 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/gausskernel/process/stream/streamCore.cpp b/src/gausskernel/process/stream/streamCore.cpp index d5352986d..25fbd65d3 100755 --- a/src/gausskernel/process/stream/streamCore.cpp +++ b/src/gausskernel/process/stream/streamCore.cpp @@ -1918,7 +1918,7 @@ void StreamNodeGroup::BuildStreamDesc(const uint64& queryId, Plan* node) parallelDesc = palloc0(sizeof(ParallelIndexScanDescData)); ((ParallelIndexScanDescData*)parallelDesc)->ps_indexid = ((IndexScan*)node)->indexid; ((ParallelIndexScanDescData*)parallelDesc)->ps_relid = ((IndexScan*)node)->scan.scanrelid; - ((ParallelIndexScanDescData*)parallelDesc)->ps_btpscan = btbuildparallelscan(); + ((ParallelIndexScanDescData*)parallelDesc)->psBtpscan = Btbuildparallelscan(); break; default: break; @@ -1946,8 +1946,8 @@ void StreamNodeGroup::DestroyStreamDesc(const uint64& queryId, Plan* node) return; } if (iter->second) { - if (((ParallelIndexScanDescData*)iter->second)->ps_btpscan) { - delete ((ParallelIndexScanDescData*)iter->second)->ps_btpscan; + if (((ParallelIndexScanDescData*)iter->second)->psBtpscan) { + delete ((ParallelIndexScanDescData*)iter->second)->psBtpscan; } pfree(iter->second); } diff --git a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp index 16ce1ca84..8113a673c 100644 --- a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp +++ b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp @@ -584,7 +584,7 @@ void scan_handler_idx_rescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey void scan_handler_idx_rescan_parallel(IndexScanDesc scan) { Assert(scan != NULL); - index_rescan_parallel(scan); + IndexRescanParallel(scan); } void scan_handler_idx_rescan_local(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys) diff --git a/src/gausskernel/storage/access/index/indexam.cpp b/src/gausskernel/storage/access/index/indexam.cpp index 7007d1473..1a2a7d167 100644 --- a/src/gausskernel/storage/access/index/indexam.cpp +++ b/src/gausskernel/storage/access/index/indexam.cpp @@ -329,7 +329,7 @@ static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys #endif /* Initialize information for parallel scan. */ - scan->parallel_scan = pscan; + scan->parallelScan = pscan; return scan; } @@ -383,9 +383,9 @@ void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, * ---------------- */ -void index_rescan_parallel(IndexScanDesc scan) +void IndexRescanParallel(IndexScanDesc scan) { - if (scan->parallel_scan) { + if (scan->parallelScan) { btparallelrescan(scan); } } @@ -518,7 +518,7 @@ void index_parallelscan_initialize(Relation heap_relation, Relation index_relati target->ps_relid = RelationGetRelid(heap_relation); target->ps_indexid = RelationGetRelid(index_relation); - btinitparallelscan(target->ps_btpscan); + Btinitparallelscan(target->psBtpscan); } /* ---------------- diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp index 183fe7af7..cea5cccab 100644 --- a/src/gausskernel/storage/access/nbtree/nbtree.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp @@ -762,11 +762,11 @@ void Btinitparallelscan(void *target) void btparallelrescan(IndexScanDesc scan) { BTParallelScanDesc btscan; - ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + ParallelIndexScanDesc parallel_scan = scan->parallelScan; Assert(parallel_scan); - btscan = (BTParallelScanDesc) parallel_scan->ps_btpscan; + btscan = (BTParallelScanDesc) parallel_scan->psBtpscan; /* * In theory, we don't need to acquire the spinlock here, because there @@ -804,12 +804,12 @@ bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) BTPS_State pageStatus; bool exitLoop = false; bool status = true; - ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + ParallelIndexScanDesc parallel_scan = scan->parallelScan; BTParallelScanDesc btscan; *pageno = P_NONE; - btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); while (1) { LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); @@ -849,10 +849,10 @@ bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) */ void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) { - ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + ParallelIndexScanDesc parallel_scan = scan->parallelScan; BTParallelScanDesc btscan; - btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); { LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); @@ -872,7 +872,7 @@ void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) void _bt_parallel_done(IndexScanDesc scan) { BTScanOpaque so = (BTScanOpaque) scan->opaque; - ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + ParallelIndexScanDesc parallel_scan = scan->parallelScan; BTParallelScanDesc btscan; bool statusChanged = false; @@ -881,7 +881,7 @@ void _bt_parallel_done(IndexScanDesc scan) return; } - btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); /* * Mark the parallel scan as done for this combination of scan keys, @@ -909,10 +909,10 @@ void _bt_parallel_done(IndexScanDesc scan) void _bt_parallel_advance_array_keys(IndexScanDesc scan) { BTScanOpaque so = (BTScanOpaque) scan->opaque; - ParallelIndexScanDesc parallel_scan = scan->parallel_scan; + ParallelIndexScanDesc parallel_scan = scan->parallelScan; BTParallelScanDesc btscan; - btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); + btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); so->arrayKeyCount++; LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); @@ -1621,7 +1621,7 @@ static BTVacuumPosting btree_vacuum_posting(BTVacState *vac_state, IndexTuple po /* * btestimateparallelscan -- estimate storage for BTParallelScanDescData */ -void* btbuildparallelscan(void) +void* Btbuildparallelscan(void) { void *btPscan = new BTParallelScanDescData; return btPscan; diff --git a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp index d2e16c848..92392b4df 100644 --- a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp @@ -620,7 +620,7 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) * way while keeping other participating processes waiting. If the scan * has already begun, use the page number from the shared structure. */ - if (scan->parallel_scan != NULL) { + if (scan->parallelScan != NULL) { status = _bt_parallel_seize(scan, &blkno); if (!status) { return false; @@ -1215,7 +1215,7 @@ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber off opaque = (BTPageOpaqueInternal)PageGetSpecialPointer(page); /* allow next page be processed by parallel worker */ - if (scan->parallel_scan) { + if (scan->parallelScan) { if (ScanDirectionIsForward(dir)) _bt_parallel_release(scan, opaque->btpo_next); else @@ -1406,7 +1406,7 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) so->currPos.buf = InvalidBuffer; /* Walk right to the next page with data */ - if (scan->parallel_scan != NULL) { + if (scan->parallelScan != NULL) { /* * Seize the scan to get the next block number; if the scan has * ended already, bail out. @@ -1424,7 +1424,7 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) } else { /* Remember we left a page with data */ so->currPos.moreRight = true; - if (scan->parallel_scan != NULL) { + if (scan->parallelScan != NULL) { /* * Seize the scan to get the current block number; if the scan has * ended already, bail out. @@ -1492,7 +1492,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque))) { break; } - } else if (scan->parallel_scan != NULL) { + } else if (scan->parallelScan != NULL) { /* allow next page be processed by parallel worker */ _bt_parallel_release(scan, opaque->btpo_next); } @@ -1502,7 +1502,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, so->currPos.buf = InvalidBuffer; /* nope, keep going */ - if (scan->parallel_scan != NULL) { + if (scan->parallelScan != NULL) { status = _bt_parallel_seize(scan, &blkno); if (!status) { return false; @@ -1553,7 +1553,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, } /* Step to next physical page */ - Buffer temp = so->currPos.buf; + Buffer temp = so->currPos.buf; so->currPos.buf = InvalidBuffer; so->currPos.buf = _bt_walk_left(rel, temp); @@ -1576,8 +1576,8 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, /* note that this will clear moreLeft if we can stop */ if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page))) { break; - } - } else if (scan->parallel_scan != NULL) { + } + } else if (scan->parallelScan != NULL) { /* allow next page be processed by parallel worker */ _bt_parallel_release(scan, BufferGetBlockNumber(so->currPos.buf)); } @@ -1588,11 +1588,10 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, * worker has already advanced the scan to a different page. We * must continue based on the latest page scanned by any worker. */ - if (scan->parallel_scan != NULL) { + if (scan->parallelScan != NULL) { _bt_relbuf(rel, so->currPos.buf); status = _bt_parallel_seize(scan, &blkno); - if (!status) - { + if (!status) { so->currPos.buf = InvalidBuffer; return false; } diff --git a/src/gausskernel/storage/access/nbtree/nbtutils.cpp b/src/gausskernel/storage/access/nbtree/nbtutils.cpp index 1c477b6b0..616da34cc 100644 --- a/src/gausskernel/storage/access/nbtree/nbtutils.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtutils.cpp @@ -555,7 +555,7 @@ bool _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir) } /* advance parallel scan */ - if (scan->parallel_scan != NULL) + if (scan->parallelScan != NULL) _bt_parallel_advance_array_keys(scan); return found; diff --git a/src/include/access/genam.h b/src/include/access/genam.h index b14da2568..99044ac07 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -136,10 +136,10 @@ extern bool index_insert(Relation indexRelation, Datum* values, const bool* isnu extern IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); extern void index_parallelscan_initialize(Relation heap_relation, - Relation index_relation, ParallelIndexScanDesc p_index_scan); + Relation index_relation, ParallelIndexScanDesc pIndexScan); extern IndexScanDesc index_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state=NULL); extern void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys); -extern void index_rescan_parallel(IndexScanDesc scan); +extern void IndexRescanParallel(IndexScanDesc scan); extern void index_endscan(IndexScanDesc scan); extern void index_markpos(IndexScanDesc scan); extern void index_restrpos(IndexScanDesc scan); diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h index e0e5afa38..bb6d1e83c 100644 --- a/src/include/access/nbtree.h +++ b/src/include/access/nbtree.h @@ -1286,7 +1286,7 @@ extern Datum btbuild(PG_FUNCTION_ARGS); extern Datum btbuildempty(PG_FUNCTION_ARGS); extern Datum btinsert(PG_FUNCTION_ARGS); extern Datum btbeginscan(PG_FUNCTION_ARGS); -extern void* btbuildparallelscan(void); +extern void* Btbuildparallelscan(void); extern void Btinitparallelscan(void *target); extern Datum btgettuple(PG_FUNCTION_ARGS); extern Datum btgetbitmap(PG_FUNCTION_ARGS); diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h index cb40b95df..cc67b1be8 100644 --- a/src/include/access/relscan.h +++ b/src/include/access/relscan.h @@ -171,7 +171,7 @@ typedef struct IndexScanDescData { #endif IndexFetchTableData *xs_heapfetch; /* parallel index scan information, in global variables */ - struct ParallelIndexScanDescData *parallel_scan; + struct ParallelIndexScanDescData *parallelScan; /* put decompressed heap tuple data into xs_ctbuf_hdr be careful! when malloc memory should give extra mem for *xs_ctbuf_hdr. t_bits which is varlength arr */ @@ -183,7 +183,7 @@ typedef struct IndexScanDescData { typedef struct ParallelIndexScanDescData { Oid ps_relid; Oid ps_indexid; - void* ps_btpscan; + void* psBtpscan; } ParallelIndexScanDescData; #define SizeofIndexScanDescData (offsetof(IndexScanDescData, xs_ctbuf_hdr) + SizeofHeapTupleHeader) From d66fc9d1823b8fe752b935ff1689df73d612cff6 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Fri, 9 Aug 2024 14:19:50 +0800 Subject: [PATCH 10/12] clean code by zhaosen --- src/gausskernel/storage/access/nbtree/nbtsearch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp index 92392b4df..e07865b9d 100644 --- a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp @@ -597,6 +597,7 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) bool status = true; StrategyNumber strat_total; BTScanPosItem *currItem = NULL; + bool match = false; BlockNumber blkno; pgstat_count_index_scan(rel); @@ -800,7 +801,6 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) * there. */ if (keysCount == 0) { - bool match; match = _bt_endpoint(scan, dir); if (!match) { /* No match, so mark (parallel) scan finished */ From cf050ccb7b12ec8131cef1a0fa5ad69325bb23e0 Mon Sep 17 00:00:00 2001 From: zhaosen Date: Fri, 9 Aug 2024 14:38:42 +0800 Subject: [PATCH 11/12] clean code by zhaosen --- 0001-clean-code-jin.patch | 323 ------------------ .../storage/access/hbstore/hbindex_am.cpp | 4 +- .../storage/access/index/indexam.cpp | 7 +- .../storage/access/nbtree/nbtree.cpp | 3 +- src/include/access/genam.h | 4 +- src/include/access/hbindex_am.h | 4 +- 6 files changed, 10 insertions(+), 335 deletions(-) delete mode 100644 0001-clean-code-jin.patch diff --git a/0001-clean-code-jin.patch b/0001-clean-code-jin.patch deleted file mode 100644 index 1b8c9f96c..000000000 --- a/0001-clean-code-jin.patch +++ /dev/null @@ -1,323 +0,0 @@ -From d12721addbe02a005ba0e8733c45e78aba88964b Mon Sep 17 00:00:00 2001 -From: =?UTF-8?q?=E8=B5=B5=E9=87=91?= -Date: Fri, 9 Aug 2024 11:58:48 +0800 -Subject: [PATCH] clean code jin - ---- - src/gausskernel/process/stream/streamCore.cpp | 6 ++--- - .../storage/access/hbstore/hbindex_am.cpp | 2 +- - .../storage/access/index/indexam.cpp | 8 +++---- - .../storage/access/nbtree/nbtree.cpp | 22 +++++++++---------- - .../storage/access/nbtree/nbtsearch.cpp | 19 ++++++++-------- - .../storage/access/nbtree/nbtutils.cpp | 2 +- - src/include/access/genam.h | 4 ++-- - src/include/access/nbtree.h | 2 +- - src/include/access/relscan.h | 4 ++-- - 9 files changed, 34 insertions(+), 35 deletions(-) - -diff --git a/src/gausskernel/process/stream/streamCore.cpp b/src/gausskernel/process/stream/streamCore.cpp -index d5352986d..25fbd65d3 100755 ---- a/src/gausskernel/process/stream/streamCore.cpp -+++ b/src/gausskernel/process/stream/streamCore.cpp -@@ -1918,7 +1918,7 @@ void StreamNodeGroup::BuildStreamDesc(const uint64& queryId, Plan* node) - parallelDesc = palloc0(sizeof(ParallelIndexScanDescData)); - ((ParallelIndexScanDescData*)parallelDesc)->ps_indexid = ((IndexScan*)node)->indexid; - ((ParallelIndexScanDescData*)parallelDesc)->ps_relid = ((IndexScan*)node)->scan.scanrelid; -- ((ParallelIndexScanDescData*)parallelDesc)->ps_btpscan = btbuildparallelscan(); -+ ((ParallelIndexScanDescData*)parallelDesc)->psBtpscan = Btbuildparallelscan(); - break; - default: - break; -@@ -1946,8 +1946,8 @@ void StreamNodeGroup::DestroyStreamDesc(const uint64& queryId, Plan* node) - return; - } - if (iter->second) { -- if (((ParallelIndexScanDescData*)iter->second)->ps_btpscan) { -- delete ((ParallelIndexScanDescData*)iter->second)->ps_btpscan; -+ if (((ParallelIndexScanDescData*)iter->second)->psBtpscan) { -+ delete ((ParallelIndexScanDescData*)iter->second)->psBtpscan; - } - pfree(iter->second); - } -diff --git a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp -index 16ce1ca84..8113a673c 100644 ---- a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp -+++ b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp -@@ -584,7 +584,7 @@ void scan_handler_idx_rescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey - void scan_handler_idx_rescan_parallel(IndexScanDesc scan) - { - Assert(scan != NULL); -- index_rescan_parallel(scan); -+ IndexRescanParallel(scan); - } - - void scan_handler_idx_rescan_local(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys) -diff --git a/src/gausskernel/storage/access/index/indexam.cpp b/src/gausskernel/storage/access/index/indexam.cpp -index 7007d1473..b85522a6c 100644 ---- a/src/gausskernel/storage/access/index/indexam.cpp -+++ b/src/gausskernel/storage/access/index/indexam.cpp -@@ -329,7 +329,7 @@ static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys - #endif - - /* Initialize information for parallel scan. */ -- scan->parallel_scan = pscan; -+ scan->parallelScan = pscan; - - return scan; - } -@@ -383,9 +383,9 @@ void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, - * ---------------- - */ - --void index_rescan_parallel(IndexScanDesc scan) -+void IndexRescanParallel(IndexScanDesc scan) - { -- if (scan->parallel_scan) { -+ if (scan->parallelScan) { - btparallelrescan(scan); - } - } -@@ -518,7 +518,7 @@ void index_parallelscan_initialize(Relation heap_relation, Relation index_relati - target->ps_relid = RelationGetRelid(heap_relation); - target->ps_indexid = RelationGetRelid(index_relation); - -- btinitparallelscan(target->ps_btpscan); -+ btinitparallelscan(target->psBtpscan); - } - - /* ---------------- -diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp -index 183fe7af7..cea5cccab 100644 ---- a/src/gausskernel/storage/access/nbtree/nbtree.cpp -+++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp -@@ -762,11 +762,11 @@ void Btinitparallelscan(void *target) - void btparallelrescan(IndexScanDesc scan) - { - BTParallelScanDesc btscan; -- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; -+ ParallelIndexScanDesc parallel_scan = scan->parallelScan; - - Assert(parallel_scan); - -- btscan = (BTParallelScanDesc) parallel_scan->ps_btpscan; -+ btscan = (BTParallelScanDesc) parallel_scan->psBtpscan; - - /* - * In theory, we don't need to acquire the spinlock here, because there -@@ -804,12 +804,12 @@ bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) - BTPS_State pageStatus; - bool exitLoop = false; - bool status = true; -- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; -+ ParallelIndexScanDesc parallel_scan = scan->parallelScan; - BTParallelScanDesc btscan; - - *pageno = P_NONE; - -- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); -+ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); - - while (1) { - LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); -@@ -849,10 +849,10 @@ bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno) - */ - void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) - { -- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; -+ ParallelIndexScanDesc parallel_scan = scan->parallelScan; - BTParallelScanDesc btscan; - -- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); -+ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); - - { - LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); -@@ -872,7 +872,7 @@ void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page) - void _bt_parallel_done(IndexScanDesc scan) - { - BTScanOpaque so = (BTScanOpaque) scan->opaque; -- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; -+ ParallelIndexScanDesc parallel_scan = scan->parallelScan; - BTParallelScanDesc btscan; - bool statusChanged = false; - -@@ -881,7 +881,7 @@ void _bt_parallel_done(IndexScanDesc scan) - return; - } - -- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); -+ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); - - /* - * Mark the parallel scan as done for this combination of scan keys, -@@ -909,10 +909,10 @@ void _bt_parallel_done(IndexScanDesc scan) - void _bt_parallel_advance_array_keys(IndexScanDesc scan) - { - BTScanOpaque so = (BTScanOpaque) scan->opaque; -- ParallelIndexScanDesc parallel_scan = scan->parallel_scan; -+ ParallelIndexScanDesc parallel_scan = scan->parallelScan; - BTParallelScanDesc btscan; - -- btscan = (BTParallelScanDesc) (parallel_scan->ps_btpscan); -+ btscan = (BTParallelScanDesc) (parallel_scan->psBtpscan); - - so->arrayKeyCount++; - LWLockAcquire(ParallelIndexScanLock, LW_EXCLUSIVE); -@@ -1621,7 +1621,7 @@ static BTVacuumPosting btree_vacuum_posting(BTVacState *vac_state, IndexTuple po - /* - * btestimateparallelscan -- estimate storage for BTParallelScanDescData - */ --void* btbuildparallelscan(void) -+void* Btbuildparallelscan(void) - { - void *btPscan = new BTParallelScanDescData; - return btPscan; -diff --git a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp -index d2e16c848..3b91e0957 100644 ---- a/src/gausskernel/storage/access/nbtree/nbtsearch.cpp -+++ b/src/gausskernel/storage/access/nbtree/nbtsearch.cpp -@@ -620,7 +620,7 @@ bool _bt_first(IndexScanDesc scan, ScanDirection dir) - * way while keeping other participating processes waiting. If the scan - * has already begun, use the page number from the shared structure. - */ -- if (scan->parallel_scan != NULL) { -+ if (scan->parallelScan != NULL) { - status = _bt_parallel_seize(scan, &blkno); - if (!status) { - return false; -@@ -1215,7 +1215,7 @@ static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber off - opaque = (BTPageOpaqueInternal)PageGetSpecialPointer(page); - - /* allow next page be processed by parallel worker */ -- if (scan->parallel_scan) { -+ if (scan->parallelScan) { - if (ScanDirectionIsForward(dir)) - _bt_parallel_release(scan, opaque->btpo_next); - else -@@ -1406,7 +1406,7 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) - so->currPos.buf = InvalidBuffer; - - /* Walk right to the next page with data */ -- if (scan->parallel_scan != NULL) { -+ if (scan->parallelScan != NULL) { - /* - * Seize the scan to get the next block number; if the scan has - * ended already, bail out. -@@ -1424,7 +1424,7 @@ static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir) - } else { - /* Remember we left a page with data */ - so->currPos.moreRight = true; -- if (scan->parallel_scan != NULL) { -+ if (scan->parallelScan != NULL) { - /* - * Seize the scan to get the current block number; if the scan has - * ended already, bail out. -@@ -1492,7 +1492,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, - if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque))) { - break; - } -- } else if (scan->parallel_scan != NULL) { -+ } else if (scan->parallelScan != NULL) { - /* allow next page be processed by parallel worker */ - _bt_parallel_release(scan, opaque->btpo_next); - } -@@ -1502,7 +1502,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, - so->currPos.buf = InvalidBuffer; - - /* nope, keep going */ -- if (scan->parallel_scan != NULL) { -+ if (scan->parallelScan != NULL) { - status = _bt_parallel_seize(scan, &blkno); - if (!status) { - return false; -@@ -1577,7 +1577,7 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, - if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page))) { - break; - } -- } else if (scan->parallel_scan != NULL) { -+ } else if (scan->parallelScan != NULL) { - /* allow next page be processed by parallel worker */ - _bt_parallel_release(scan, BufferGetBlockNumber(so->currPos.buf)); - } -@@ -1588,11 +1588,10 @@ static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, - * worker has already advanced the scan to a different page. We - * must continue based on the latest page scanned by any worker. - */ -- if (scan->parallel_scan != NULL) { -+ if (scan->parallelScan != NULL) { - _bt_relbuf(rel, so->currPos.buf); - status = _bt_parallel_seize(scan, &blkno); -- if (!status) -- { -+ if (!status) { - so->currPos.buf = InvalidBuffer; - return false; - } -diff --git a/src/gausskernel/storage/access/nbtree/nbtutils.cpp b/src/gausskernel/storage/access/nbtree/nbtutils.cpp -index 1c477b6b0..616da34cc 100644 ---- a/src/gausskernel/storage/access/nbtree/nbtutils.cpp -+++ b/src/gausskernel/storage/access/nbtree/nbtutils.cpp -@@ -555,7 +555,7 @@ bool _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir) - } - - /* advance parallel scan */ -- if (scan->parallel_scan != NULL) -+ if (scan->parallelScan != NULL) - _bt_parallel_advance_array_keys(scan); - - return found; -diff --git a/src/include/access/genam.h b/src/include/access/genam.h -index b14da2568..99044ac07 100644 ---- a/src/include/access/genam.h -+++ b/src/include/access/genam.h -@@ -136,10 +136,10 @@ extern bool index_insert(Relation indexRelation, Datum* values, const bool* isnu - extern IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, - int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); - extern void index_parallelscan_initialize(Relation heap_relation, -- Relation index_relation, ParallelIndexScanDesc p_index_scan); -+ Relation index_relation, ParallelIndexScanDesc pIndexScan); - extern IndexScanDesc index_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state=NULL); - extern void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys); --extern void index_rescan_parallel(IndexScanDesc scan); -+extern void IndexRescanParallel(IndexScanDesc scan); - extern void index_endscan(IndexScanDesc scan); - extern void index_markpos(IndexScanDesc scan); - extern void index_restrpos(IndexScanDesc scan); -diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h -index e0e5afa38..bb6d1e83c 100644 ---- a/src/include/access/nbtree.h -+++ b/src/include/access/nbtree.h -@@ -1286,7 +1286,7 @@ extern Datum btbuild(PG_FUNCTION_ARGS); - extern Datum btbuildempty(PG_FUNCTION_ARGS); - extern Datum btinsert(PG_FUNCTION_ARGS); - extern Datum btbeginscan(PG_FUNCTION_ARGS); --extern void* btbuildparallelscan(void); -+extern void* Btbuildparallelscan(void); - extern void Btinitparallelscan(void *target); - extern Datum btgettuple(PG_FUNCTION_ARGS); - extern Datum btgetbitmap(PG_FUNCTION_ARGS); -diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h -index cb40b95df..cc67b1be8 100644 ---- a/src/include/access/relscan.h -+++ b/src/include/access/relscan.h -@@ -171,7 +171,7 @@ typedef struct IndexScanDescData { - #endif - IndexFetchTableData *xs_heapfetch; - /* parallel index scan information, in global variables */ -- struct ParallelIndexScanDescData *parallel_scan; -+ struct ParallelIndexScanDescData *parallelScan; - /* put decompressed heap tuple data into xs_ctbuf_hdr be careful! when malloc memory should give extra mem for - *xs_ctbuf_hdr. t_bits which is varlength arr - */ -@@ -183,7 +183,7 @@ typedef struct IndexScanDescData { - typedef struct ParallelIndexScanDescData { - Oid ps_relid; - Oid ps_indexid; -- void* ps_btpscan; -+ void* psBtpscan; - } ParallelIndexScanDescData; - - #define SizeofIndexScanDescData (offsetof(IndexScanDescData, xs_ctbuf_hdr) + SizeofHeapTupleHeader) --- -2.39.1.windows.1 - diff --git a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp index 8113a673c..9f3ab4a03 100644 --- a/src/gausskernel/storage/access/hbstore/hbindex_am.cpp +++ b/src/gausskernel/storage/access/hbstore/hbindex_am.cpp @@ -545,7 +545,7 @@ static HeapTuple cross_level_index_getnext(IndexScanDesc scan, ScanDirection dir * ------------------------------------------------------------------------ */ -IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, +IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state, ParallelIndexScanDesc pscan) { if (unlikely(RELATION_OWN_BUCKET(heap_relation))) { @@ -555,7 +555,7 @@ IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_ } } -void scan_handler_idx_parallelscan_initialize(Relation heap_relation, +void scan_handler_idx_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan) { index_parallelscan_initialize(heap_relation, index_relation, p_index_scan); diff --git a/src/gausskernel/storage/access/index/indexam.cpp b/src/gausskernel/storage/access/index/indexam.cpp index 1a2a7d167..b291c09a9 100644 --- a/src/gausskernel/storage/access/index/indexam.cpp +++ b/src/gausskernel/storage/access/index/indexam.cpp @@ -144,7 +144,7 @@ } \ } while (0) -static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, +static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, ParallelIndexScanDesc pscan = NULL); /* ---------------- @@ -298,7 +298,7 @@ IndexScanDesc index_beginscan_bitmap(Relation index_relation, Snapshot snapshot, /* * index_beginscan_internal --- common code for index_beginscan variants */ -static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, +static IndexScanDesc index_beginscan_internal(Relation index_relation, int nkeys, int norderbys, Snapshot snapshot, ParallelIndexScanDesc pscan) { IndexScanDesc scan; @@ -511,8 +511,7 @@ void index_parallelscan_initialize(Relation heap_relation, Relation index_relati if (!target) { return; } - Size offset; - + Size offset; RELATION_CHECKS; target->ps_relid = RelationGetRelid(heap_relation); diff --git a/src/gausskernel/storage/access/nbtree/nbtree.cpp b/src/gausskernel/storage/access/nbtree/nbtree.cpp index cea5cccab..f4fc03c24 100644 --- a/src/gausskernel/storage/access/nbtree/nbtree.cpp +++ b/src/gausskernel/storage/access/nbtree/nbtree.cpp @@ -17,7 +17,6 @@ * * ------------------------------------------------------------------------- */ - #include "postgres.h" #include "knl/knl_variable.h" #include "access/nbtree.h" @@ -80,7 +79,7 @@ typedef struct BTParallelScanDescData { BTPS_State btpsPageStatus;/* indicates whether next page is available * for scan. see above for possible states of * parallel scan. */ - int btpsArrayKeyCount; /* count indicating number of array + int btpsArrayKeyCount; /* count indicating number of array * scan keys processed by parallel * scan */ } BTParallelScanDescData; diff --git a/src/include/access/genam.h b/src/include/access/genam.h index 99044ac07..42af09b01 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -133,9 +133,9 @@ extern void index_delete(Relation index_relation, Datum* values, const bool* isn extern bool index_insert(Relation indexRelation, Datum* values, const bool* isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique); -extern IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, +extern IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); -extern void index_parallelscan_initialize(Relation heap_relation, +extern void index_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc pIndexScan); extern IndexScanDesc index_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state=NULL); extern void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys); diff --git a/src/include/access/hbindex_am.h b/src/include/access/hbindex_am.h index 29a45fd24..cd4a173bf 100644 --- a/src/include/access/hbindex_am.h +++ b/src/include/access/hbindex_am.h @@ -48,9 +48,9 @@ static inline bool hbkt_idx_need_switch_bkt(IndexScanDesc scan, int targetSlot) extern bool hbkt_idx_bitmapscan_switch_bucket(IndexScanDesc scan, int targetSlot); extern bool cbi_scan_need_fix_hbkt_rel(IndexScanDesc scan, int2 bucketid = InvalidBktId); extern bool cbi_scan_fix_hbkt_rel(HBktIdxScanDesc hpScan); -extern IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, +extern IndexScanDesc scan_handler_idx_beginscan(Relation heap_relation, Relation index_relation, Snapshot snapshot, int nkeys, int norderbys, ScanState* scan_state = NULL, ParallelIndexScanDesc pscan = NULL); -extern void scan_handler_idx_parallelscan_initialize(Relation heap_relation, +extern void scan_handler_idx_parallelscan_initialize(Relation heap_relation, Relation index_relation, ParallelIndexScanDesc p_index_scan); extern IndexScanDesc scan_handler_idx_beginscan_bitmap(Relation indexRelation, Snapshot snapshot, int nkeys, ScanState* scan_state); extern void scan_handler_idx_rescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys); From ee0023f8a620ec3263556dc0f504076eace4f886 Mon Sep 17 00:00:00 2001 From: "jin.zhao" Date: Thu, 15 Aug 2024 18:01:19 +0800 Subject: [PATCH 12/12] streamDesc use normal hash table --- src/gausskernel/process/stream/streamCore.cpp | 50 +++++++++++++------ src/include/distributelayer/streamCore.h | 24 +++------ .../executor/node/nodeBitmapHeapscan.h | 2 - 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/gausskernel/process/stream/streamCore.cpp b/src/gausskernel/process/stream/streamCore.cpp index 25fbd65d3..e39dcebe4 100755 --- a/src/gausskernel/process/stream/streamCore.cpp +++ b/src/gausskernel/process/stream/streamCore.cpp @@ -74,6 +74,7 @@ MemoryContext StreamNodeGroup::m_memoryGlobalCxt = NULL; pthread_mutex_t StreamNodeGroup::m_streamNodeGroupLock; HTAB* StreamNodeGroup::m_streamNodeGroupTbl = NULL; HTAB* StreamNodeGroup::m_streamConnectSyncTbl = NULL; +HTAB* StreamNodeGroup::m_streamDescHashTbl = NULL; pthread_mutex_t StreamNodeGroup::m_streamConnectSyncLock; static void ConsumerNodeSyncUpMessage(RecursiveUnionController* controller, int step, StreamState* node); @@ -439,6 +440,15 @@ void StreamNodeGroup::StartUp() hash_create("stream connect sync hash", 256, &nodectl, HASH_ELEM | HASH_FUNCTION | HASH_SHRCTX); pthread_mutex_init(&m_streamConnectSyncLock, NULL); + rc = memset_s(&nodectl, sizeof(nodectl), 0, sizeof(nodectl)); + securec_check(rc, "\0", "\0"); + nodectl.keysize = sizeof(StreamKey); + nodectl.entrysize = sizeof(StreamDescElement); + nodectl.hash = tag_hash; + nodectl.hcxt = m_memoryGlobalCxt; + + m_streamDescHashTbl = + hash_create("stream desc hash", STREAM_DESC_HASH_NUMBER, &nodectl, HASH_ELEM | HASH_FUNCTION | HASH_SHRCTX); } /* @@ -1908,6 +1918,7 @@ void StreamNodeGroup::MarkRecursiveVfdInvalid() void StreamNodeGroup::BuildStreamDesc(const uint64& queryId, Plan* node) { StreamKey streamKey; + memset_s(&streamKey, sizeof(streamKey), 0, sizeof(streamKey)); streamKey.queryId = queryId; streamKey.planNodeId = node->plan_node_id; @@ -1927,31 +1938,34 @@ void StreamNodeGroup::BuildStreamDesc(const uint64& queryId, Plan* node) if (!parallelDesc) { return; } - - m_streamDesc.insert({streamKey, parallelDesc}); + bool found = false; + StreamDescElement* element = (StreamDescElement*)hash_search(m_streamDescHashTbl, &streamKey, HASH_ENTER, &found); + if (found != false) { + ereport(ERROR, (errcode(ERRCODE_SYSTEM_ERROR), errmsg("streamKey of stream nodegroup id is duplicated"))); + } + element->key = streamKey; + element->parallelDesc = (ParallelIndexScanDescData*)parallelDesc; } void StreamNodeGroup::DestroyStreamDesc(const uint64& queryId, Plan* node) { StreamKey streamKey; + memset_s(&streamKey, sizeof(streamKey), 0, sizeof(streamKey)); streamKey.queryId = queryId; streamKey.planNodeId = node->plan_node_id; - - std::unordered_map::iterator iter; + bool found = false; + StreamDescElement* element = NULL; switch (nodeTag(node)) { case T_IndexScan: - iter = m_streamDesc.find(streamKey); - if (m_streamDesc.end() == iter) { - return; - } - if (iter->second) { - if (((ParallelIndexScanDescData*)iter->second)->psBtpscan) { - delete ((ParallelIndexScanDescData*)iter->second)->psBtpscan; + element = (StreamDescElement*)hash_search(m_streamDescHashTbl, &streamKey, HASH_FIND, &found); + if (found == true) { + if (((ParallelIndexScanDescData*)element->parallelDesc)->psBtpscan) { + delete ((ParallelIndexScanDescData*)element->parallelDesc)->psBtpscan; } - pfree(iter->second); + pfree(element->parallelDesc); + (StreamDescElement*)hash_search(m_streamDescHashTbl, &streamKey, HASH_REMOVE, NULL); } - m_streamDesc.erase(streamKey); break; default: break; @@ -1961,10 +1975,16 @@ void StreamNodeGroup::DestroyStreamDesc(const uint64& queryId, Plan* node) void* StreamNodeGroup::GetParalleDesc(const uint64& queryId, const uint64& planNodeId) { StreamKey key; + memset_s(&key, sizeof(key), 0, sizeof(key)); key.queryId = queryId; key.planNodeId = planNodeId; - std::unordered_map::iterator iter = m_streamDesc.find(key); - return (m_streamDesc.end() == iter) ? NULL : iter->second; + bool found = false; + StreamDescElement* element = (StreamDescElement*)hash_search(m_streamDescHashTbl, &key, HASH_FIND, &found); + if (found == false) { + return NULL; + } else { + return element->parallelDesc; + } } #ifndef ENABLE_MULTIPLE_NODES diff --git a/src/include/distributelayer/streamCore.h b/src/include/distributelayer/streamCore.h index b4dacb7e4..d7f191780 100755 --- a/src/include/distributelayer/streamCore.h +++ b/src/include/distributelayer/streamCore.h @@ -28,7 +28,6 @@ #define SRC_INCLUDE_DISTRIBUTELAYER_STREAMCORE_H_ #include -#include #include "postgres.h" #include "knl/knl_variable.h" @@ -59,6 +58,8 @@ #define TupleVectorMaxSize 100 +#define STREAM_DESC_HASH_NUMBER 256 + #define IS_STREAM_PORTAL (!StreamThreadAmI() && portal->streamInfo.streamGroup != NULL) struct StreamState; @@ -104,6 +105,11 @@ typedef struct { uint64 key; } StreamConnectSyncElement; +typedef struct { + StreamKey key; + ParallelIndexScanDescData* parallelDesc; +} StreamDescElement; + enum StreamObjType { STREAM_PRODUCER, STREAM_CONSUMER, @@ -526,21 +532,7 @@ private: /* Mark Stream query quit status. */ StreamObjStatus m_quitStatus; #endif - struct KeyHash { - std::size_t operator()(const StreamKey& k) const - { - return std::hash()(k.queryId) ^ - (std::hash()(k.planNodeId) << 1); - } - }; - - struct KeyEqual { - bool operator()(const StreamKey& lhs, const StreamKey& rhs) const - { - return lhs.queryId == rhs.queryId && lhs.planNodeId == rhs.planNodeId; - } - }; - std::unordered_map m_streamDesc; + static HTAB* m_streamDescHashTbl; }; extern bool IsThreadProcessStreamRecursive(); diff --git a/src/include/executor/node/nodeBitmapHeapscan.h b/src/include/executor/node/nodeBitmapHeapscan.h index 9f6560123..0b0f1a411 100644 --- a/src/include/executor/node/nodeBitmapHeapscan.h +++ b/src/include/executor/node/nodeBitmapHeapscan.h @@ -29,6 +29,4 @@ extern int TableScanBitmapNextTargetRel(TableScanDesc scan, BitmapHeapScanState extern TupleTableSlot* ExecBitmapHeapScan(PlanState* state); extern void ExecInitPartitionForBitmapHeapScan(BitmapHeapScanState* scanstate, EState* estate); -#define BITMAP_PREFETCH_PAGE_RATIO 2 - #endif /* NODEBITMAPHEAPSCAN_H */