diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 7fb8a14..c76bfb0 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -730,6 +730,7 @@ ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used) { case T_SeqScan: case T_SampleScan: + case T_PartialSeqScan: case T_IndexScan: case T_IndexOnlyScan: case T_BitmapHeapScan: @@ -853,6 +854,9 @@ ExplainNode(PlanState *planstate, List *ancestors, case T_SampleScan: pname = sname = "Sample Scan"; break; + case T_PartialSeqScan: + pname = sname = "Partial Seq Scan"; + break; case T_Gather: pname = sname = "Gather"; break; @@ -1006,6 +1010,7 @@ ExplainNode(PlanState *planstate, List *ancestors, { case T_SeqScan: case T_SampleScan: + case T_PartialSeqScan: case T_BitmapHeapScan: case T_TidScan: case T_SubqueryScan: @@ -1270,6 +1275,7 @@ ExplainNode(PlanState *planstate, List *ancestors, planstate, ancestors, es); /* FALL THRU to print additional fields the same as SeqScan */ case T_SeqScan: + case T_PartialSeqScan: case T_ValuesScan: case T_CteScan: case T_WorkTableScan: @@ -2354,6 +2360,7 @@ ExplainTargetRel(Plan *plan, Index rti, ExplainState *es) { case T_SeqScan: case T_SampleScan: + case T_PartialSeqScan: case T_IndexScan: case T_IndexOnlyScan: case T_BitmapHeapScan: diff --git a/src/backend/executor/Makefile b/src/backend/executor/Makefile index 51edd4c..38a92fe 100644 --- a/src/backend/executor/Makefile +++ b/src/backend/executor/Makefile @@ -21,8 +21,8 @@ OBJS = execAmi.o execCurrent.o execGrouping.o execIndexing.o execJunk.o \ nodeHash.o nodeHashjoin.o nodeIndexscan.o nodeIndexonlyscan.o \ nodeLimit.o nodeLockRows.o \ nodeMaterial.o nodeMergeAppend.o nodeMergejoin.o nodeModifyTable.o \ - nodeNestloop.o nodeFunctionscan.o nodeRecursiveunion.o nodeResult.o \ - nodeSamplescan.o nodeSeqscan.o nodeSetOp.o nodeSort.o nodeUnique.o \ + nodeNestloop.o nodeFunctionscan.o nodePartialSeqscan.o nodeRecursiveunion.o \ + nodeResult.o nodeSamplescan.o nodeSeqscan.o nodeSetOp.o nodeSort.o nodeUnique.o \ nodeValuesscan.o nodeCtescan.o nodeWorktablescan.o \ nodeGroup.o nodeSubplan.o nodeSubqueryscan.o nodeTidscan.o \ nodeForeignscan.o nodeWindowAgg.o tstoreReceiver.o tqueue.o spi.o diff --git a/src/backend/executor/execAmi.c b/src/backend/executor/execAmi.c index 163650c..f2f9c30 100644 --- a/src/backend/executor/execAmi.c +++ b/src/backend/executor/execAmi.c @@ -38,6 +38,7 @@ #include "executor/nodeMergejoin.h" #include "executor/nodeModifyTable.h" #include "executor/nodeNestloop.h" +#include "executor/nodePartialSeqscan.h" #include "executor/nodeRecursiveunion.h" #include "executor/nodeResult.h" #include "executor/nodeSamplescan.h" @@ -161,6 +162,10 @@ ExecReScan(PlanState *node) ExecReScanSampleScan((SampleScanState *) node); break; + case T_PartialSeqScanState: + ExecReScanPartialSeqScan((PartialSeqScanState *) node); + break; + case T_GatherState: ExecReScanGather((GatherState *) node); break; @@ -472,6 +477,7 @@ ExecSupportsBackwardScan(Plan *node) /* Simplify life for tablesample methods by disallowing this */ return false; + case T_PartialSeqScan: case T_Gather: return false; diff --git a/src/backend/executor/execCurrent.c b/src/backend/executor/execCurrent.c index bcd287f..5bd00cc 100644 --- a/src/backend/executor/execCurrent.c +++ b/src/backend/executor/execCurrent.c @@ -262,6 +262,7 @@ search_plan_tree(PlanState *node, Oid table_oid) */ case T_SeqScanState: case T_SampleScanState: + case T_PartialSeqScanState: case T_IndexScanState: case T_IndexOnlyScanState: case T_BitmapHeapScanState: diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c index e6930c1..7db4e41 100644 --- a/src/backend/executor/execParallel.c +++ b/src/backend/executor/execParallel.c @@ -17,6 +17,7 @@ #include "executor/execParallel.h" #include "executor/executor.h" +#include "executor/nodePartialSeqscan.h" #include "executor/tqueue.h" #include "nodes/nodeFuncs.h" #include "optimizer/planmain.h" @@ -59,6 +60,7 @@ struct SharedExecutorInstrumentation typedef struct ExecParallelEstimateContext { ParallelContext *pcxt; + Size pscan_len; int nnodes; } ExecParallelEstimateContext; @@ -67,9 +69,16 @@ typedef struct ExecParallelInitializeDSMContext { ParallelContext *pcxt; SharedExecutorInstrumentation *instrumentation; + Size pscan_len; int nnodes; } ExecParallelInitializeDSMContext; +/* + * This is required for parallel plan execution to fetch the information + * from dsm. + */ +static shm_toc *parallel_shm_toc = NULL; + /* Helper functions that run in the parallel leader. */ static char *ExecSerializePlan(Plan *plan, EState *estate); static bool ExecParallelEstimate(PlanState *node, @@ -158,10 +167,24 @@ ExecParallelEstimate(PlanState *planstate, ExecParallelEstimateContext *e) /* Count this node. */ e->nnodes++; - /* - * XXX. Call estimators for parallel-aware nodes here, when we have - * some. - */ + /* Call estimators for parallel-aware nodes. */ + switch (nodeTag(planstate)) + { + case T_PartialSeqScanState: + { + EState *estate = ((PartialSeqScanState *) planstate)->ss.ps.state; + + e->pscan_len = heap_parallelscan_estimate(estate->es_snapshot); + shm_toc_estimate_chunk(&e->pcxt->estimator, e->pscan_len); + + /* key for partial scan information. */ + shm_toc_estimate_keys(&e->pcxt->estimator, 1); + return true; + } + break; + default: + break; + } return planstate_tree_walker(planstate, ExecParallelEstimate, e); } @@ -177,6 +200,8 @@ static bool ExecParallelInitializeDSM(PlanState *planstate, ExecParallelInitializeDSMContext *d) { + ParallelHeapScanDesc pscan; + if (planstate == NULL) return false; @@ -196,10 +221,29 @@ ExecParallelInitializeDSM(PlanState *planstate, /* Count this node. */ d->nnodes++; - /* - * XXX. Call initializers for parallel-aware plan nodes, when we have - * some. - */ + /* Call initializers for parallel-aware plan nodes. */ + switch (nodeTag(planstate)) + { + case T_PartialSeqScanState: + { + EState *estate = ((PartialSeqScanState *) planstate)->ss.ps.state; + + /* + * Store parallel heap scan descriptor in dynamic shared + * memory. + */ + pscan = shm_toc_allocate(d->pcxt->toc, + d->pscan_len); + heap_parallelscan_initialize(pscan, + ((PartialSeqScanState *) planstate)->ss.ss_currentRelation, + estate->es_snapshot); + shm_toc_insert(d->pcxt->toc, PARALLEL_KEY_SCAN, pscan); + return true; + } + break; + default: + break; + } return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d); } @@ -314,6 +358,7 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers) * a count of how many PlanState nodes there are. */ e.pcxt = pcxt; + e.pscan_len = 0; e.nnodes = 0; ExecParallelEstimate(planstate, &e); @@ -379,6 +424,7 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers) */ d.pcxt = pcxt; d.instrumentation = instrumentation; + d.pscan_len = e.pscan_len; d.nnodes = 0; ExecParallelInitializeDSM(planstate, &d); @@ -531,6 +577,15 @@ ExecParallelReportInstrumentation(PlanState *planstate, } /* + * GetParallelShmToc + */ +shm_toc * +GetParallelShmToc(void) +{ + return parallel_shm_toc; +} + +/* * Main entrypoint for parallel query worker processes. * * We reach this function from ParallelMain, so the setup necessary to create @@ -561,6 +616,8 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc) instrument_options = instrumentation->instrument_options; queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options); + parallel_shm_toc = toc; + /* Prepare to track buffer usage during query execution. */ InstrStartParallelQuery(); diff --git a/src/backend/executor/execProcnode.c b/src/backend/executor/execProcnode.c index 5bc1d48..87b022d 100644 --- a/src/backend/executor/execProcnode.c +++ b/src/backend/executor/execProcnode.c @@ -100,6 +100,7 @@ #include "executor/nodeMergejoin.h" #include "executor/nodeModifyTable.h" #include "executor/nodeNestloop.h" +#include "executor/nodePartialSeqscan.h" #include "executor/nodeGather.h" #include "executor/nodeRecursiveunion.h" #include "executor/nodeResult.h" @@ -309,6 +310,11 @@ ExecInitNode(Plan *node, EState *estate, int eflags) estate, eflags); break; + case T_PartialSeqScan: + result = (PlanState *) ExecInitPartialSeqScan((PartialSeqScan *) node, + estate, eflags); + break; + case T_Gather: result = (PlanState *) ExecInitGather((Gather *) node, estate, eflags); @@ -511,6 +517,10 @@ ExecProcNode(PlanState *node) result = ExecUnique((UniqueState *) node); break; + case T_PartialSeqScanState: + result = ExecPartialSeqScan((PartialSeqScanState *) node); + break; + case T_GatherState: result = ExecGather((GatherState *) node); break; @@ -669,6 +679,10 @@ ExecEndNode(PlanState *node) ExecEndSampleScan((SampleScanState *) node); break; + case T_PartialSeqScanState: + ExecEndPartialSeqScan((PartialSeqScanState *) node); + break; + case T_GatherState: ExecEndGather((GatherState *) node); break; diff --git a/src/backend/executor/nodeGather.c b/src/backend/executor/nodeGather.c index 735dbaa..74b3985 100644 --- a/src/backend/executor/nodeGather.c +++ b/src/backend/executor/nodeGather.c @@ -115,6 +115,8 @@ ExecGather(GatherState *node) estate, ((Gather *) (node->ps.plan))->num_workers); + outerPlanState(node)->toc = node->pei->pcxt->toc; + /* * Register backend workers. If the required number of workers are not * available then we perform the scan with available workers and if diff --git a/src/backend/executor/nodePartialSeqscan.c b/src/backend/executor/nodePartialSeqscan.c new file mode 100644 index 0000000..e4a125a --- /dev/null +++ b/src/backend/executor/nodePartialSeqscan.c @@ -0,0 +1,308 @@ +/*------------------------------------------------------------------------- + * + * nodePartialSeqscan.c + * Support routines for partial sequential scans of relations. + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/executor/nodePartialSeqscan.c + * + *------------------------------------------------------------------------- + */ +/* + * INTERFACE ROUTINES + * ExecPartialSeqScan scans a relation partially. + * PartialSeqNext retrieve next tuple from heap. + * ExecInitPartialSeqScan creates and initializes a partial seqscan node. + * ExecEndPartialSeqScan releases any storage allocated. + */ +#include "postgres.h" + +#include "access/relscan.h" +#include "executor/execdebug.h" +#include "executor/execParallel.h" +#include "executor/nodePartialSeqscan.h" +#include "utils/rel.h" + + + +/* ---------------------------------------------------------------- + * Scan Support + * ---------------------------------------------------------------- + */ + +/* ---------------------------------------------------------------- + * PartialSeqNext + * + * This is a workhorse for ExecPartialSeqScan + * ---------------------------------------------------------------- + */ +static TupleTableSlot * +PartialSeqNext(PartialSeqScanState *node) +{ + HeapTuple tuple; + HeapScanDesc scandesc; + EState *estate; + ScanDirection direction; + TupleTableSlot *slot; + + /* + * get information from the estate and scan state + */ + scandesc = node->ss.ss_currentScanDesc; + estate = node->ss.ps.state; + direction = estate->es_direction; + slot = node->ss.ss_ScanTupleSlot; + + /* + * get the next tuple from the table + */ + tuple = heap_getnext(scandesc, direction); + + /* + * save the tuple and the buffer returned to us by the access methods in + * our scan tuple slot and return the slot. Note: we pass 'false' because + * tuples returned by heap_getnext() are pointers onto disk pages and were + * not created with palloc() and so should not be pfree()'d. Note also + * that ExecStoreTuple will increment the refcount of the buffer; the + * refcount will not be dropped until the tuple table slot is cleared. + */ + if (tuple) + ExecStoreTuple(tuple, /* tuple to store */ + slot, /* slot to store in */ + scandesc->rs_cbuf, /* buffer associated with this + * tuple */ + false); /* don't pfree this pointer */ + else + ExecClearTuple(slot); + + return slot; +} + +/* + * PartialSeqRecheck -- access method routine to recheck a tuple in EvalPlanQual + */ +static bool +PartialSeqRecheck(PartialSeqScanState *node, TupleTableSlot *slot) +{ + /* + * Note that unlike IndexScan, PartialSeqScan never use keys in + * heap_beginscan (and this is very bad) - so, here we do not check are + * keys ok or not. + */ + return true; +} + +/* ---------------------------------------------------------------- + * InitPartialScanRelation + * + * Set up to access the scan relation. + * ---------------------------------------------------------------- + */ +static void +InitPartialScanRelation(PartialSeqScanState *node, EState *estate, int eflags) +{ + Relation currentRelation; + shm_toc *toc; + + /* + * get the relation object id from the relid'th entry in the range table, + * open that relation and acquire appropriate lock on it. + */ + currentRelation = ExecOpenScanRelation(estate, + ((Scan *) node->ss.ps.plan)->scanrelid, + eflags); + + /* + * Parallel scan descriptor is initialized and stored in dynamic shared + * memory segment by master backend and parallel workers retrieve it from + * shared memory. We set 'toc' (place to lookup parallel scan descriptor) + * as retrievied by attaching to dsm for parallel workers whereas master + * backend stores it directly in partial scan state node after + * initializing workers. + */ + toc = GetParallelShmToc(); + if (toc) + node->ss.ps.toc = toc; + + node->ss.ss_currentRelation = currentRelation; + + /* and report the scan tuple slot's rowtype */ + ExecAssignScanType(&node->ss, RelationGetDescr(currentRelation)); +} + +/* ---------------------------------------------------------------- + * ExecInitPartialSeqScan + * ---------------------------------------------------------------- + */ +PartialSeqScanState * +ExecInitPartialSeqScan(PartialSeqScan *node, EState *estate, int eflags) +{ + PartialSeqScanState *scanstate; + + /* + * Once upon a time it was possible to have an outerPlan of a SeqScan, but + * not any more. + */ + Assert(outerPlan(node) == NULL); + Assert(innerPlan(node) == NULL); + + /* + * create state structure + */ + scanstate = makeNode(PartialSeqScanState); + scanstate->ss.ps.plan = (Plan *) node; + scanstate->ss.ps.state = estate; + + /* + * Miscellaneous initialization + * + * create expression context for node + */ + ExecAssignExprContext(estate, &scanstate->ss.ps); + + /* + * initialize child expressions + */ + scanstate->ss.ps.targetlist = (List *) + ExecInitExpr((Expr *) node->plan.targetlist, + (PlanState *) scanstate); + scanstate->ss.ps.qual = (List *) + ExecInitExpr((Expr *) node->plan.qual, + (PlanState *) scanstate); + + /* + * tuple table initialization + */ + ExecInitResultTupleSlot(estate, &scanstate->ss.ps); + ExecInitScanTupleSlot(estate, &scanstate->ss); + + /* + * initialize scan relation + */ + InitPartialScanRelation(scanstate, estate, eflags); + + scanstate->ss.ps.ps_TupFromTlist = false; + + /* + * Initialize result tuple type and projection info. + */ + ExecAssignResultTypeFromTL(&scanstate->ss.ps); + ExecAssignScanProjectionInfo(&scanstate->ss); + + return scanstate; +} + +/* ---------------------------------------------------------------- + * ExecPartialSeqScan(node) + * + * Scans the relation and returns the next qualifying tuple. + * We call the ExecScan() routine and pass it the appropriate + * access method functions. + * ---------------------------------------------------------------- + */ +TupleTableSlot * +ExecPartialSeqScan(PartialSeqScanState *node) +{ + /* + * Initialize the scan on first execution, normally we initialize it + * during ExecutorStart phase, however we need ParallelHeapScanDesc to + * initialize the scan in case of this node and the same is initialized by + * the Funnel node during ExecutorRun phase. + */ + if (!node->scan_initialized) + { + ParallelHeapScanDesc pscan; + + /* + * Parallel scan descriptor is initialized and stored in dynamic + * shared memory segment by master backend, parallel workers and local + * scan by master backend retrieve it from shared memory. If the scan + * descriptor is available on first execution, then we need to + * re-initialize for rescan. + */ + Assert(node->ss.ps.toc); + + pscan = shm_toc_lookup(node->ss.ps.toc, PARALLEL_KEY_SCAN); + + if (!node->ss.ss_currentScanDesc) + { + node->ss.ss_currentScanDesc = + heap_beginscan_parallel(node->ss.ss_currentRelation, pscan); + } + else + { + heap_parallel_rescan(pscan, node->ss.ss_currentScanDesc); + } + + node->scan_initialized = true; + } + + return ExecScan((ScanState *) node, + (ExecScanAccessMtd) PartialSeqNext, + (ExecScanRecheckMtd) PartialSeqRecheck); +} + +/* ---------------------------------------------------------------- + * ExecEndPartialSeqScan + * + * frees any storage allocated through C routines. + * ---------------------------------------------------------------- + */ +void +ExecEndPartialSeqScan(PartialSeqScanState *node) +{ + Relation relation; + HeapScanDesc scanDesc; + + /* + * get information from node + */ + relation = node->ss.ss_currentRelation; + scanDesc = node->ss.ss_currentScanDesc; + + /* + * Free the exprcontext + */ + ExecFreeExprContext(&node->ss.ps); + + /* + * clean out the tuple table + */ + ExecClearTuple(node->ss.ps.ps_ResultTupleSlot); + ExecClearTuple(node->ss.ss_ScanTupleSlot); + + /* + * close heap scan + */ + if (scanDesc) + heap_endscan(scanDesc); + + /* + * close the heap relation. + */ + ExecCloseScanRelation(relation); +} + +/* ---------------------------------------------------------------- + * Join Support + * ---------------------------------------------------------------- + */ + +/* ---------------------------------------------------------------- + * ExecReScanPartialSeqScan + * + * Rescans the relation. + * ---------------------------------------------------------------- + */ +void +ExecReScanPartialSeqScan(PartialSeqScanState *node) +{ + if (node->scan_initialized) + node->scan_initialized = false; + + ExecScanReScan((ScanState *) node); +} diff --git a/src/backend/executor/nodeResult.c b/src/backend/executor/nodeResult.c index 8d3dde0..b348bfd 100644 --- a/src/backend/executor/nodeResult.c +++ b/src/backend/executor/nodeResult.c @@ -75,6 +75,13 @@ ExecResult(ResultState *node) econtext = node->ps.ps_ExprContext; /* + * Result node can be added as a gating node on top of PartialSeqScan + * node, so need to percolate toc information to outer node. + */ + if (node->ps.toc) + outerPlanState(node)->toc = node->ps.toc; + + /* * check constant qualifications like (2 > 1), if not already done */ if (node->rs_checkqual) diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 88dc085..c1b21b4 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -405,6 +405,22 @@ _copySampleScan(const SampleScan *from) } /* + * _copyPartialSeqScan + */ +static PartialSeqScan * +_copyPartialSeqScan(const SeqScan *from) +{ + PartialSeqScan *newnode = makeNode(PartialSeqScan); + + /* + * copy node superclass fields + */ + CopyScanFields((const Scan *) from, (Scan *) newnode); + + return newnode; +} + +/* * _copyIndexScan */ static IndexScan * @@ -4257,6 +4273,9 @@ copyObject(const void *from) case T_Scan: retval = _copyScan(from); break; + case T_PartialSeqScan: + retval = _copyPartialSeqScan(from); + break; case T_Gather: retval = _copyGather(from); break; diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 4645ecb..c69bf08 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -433,6 +433,14 @@ _outBitmapOr(StringInfo str, const BitmapOr *node) } static void +_outPartialSeqScan(StringInfo str, const SeqScan *node) +{ + WRITE_NODE_TYPE("PARTIALSEQSCAN"); + + _outScanInfo(str, (const Scan *) node); +} + +static void _outGather(StringInfo str, const Gather *node) { WRITE_NODE_TYPE("GATHER"); @@ -3011,6 +3019,9 @@ _outNode(StringInfo str, const void *obj) case T_BitmapOr: _outBitmapOr(str, obj); break; + case T_PartialSeqScan: + _outPartialSeqScan(str, obj); + break; case T_Gather: _outGather(str, obj); break; diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 72368ab..3eb5a48 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -1622,6 +1622,19 @@ _readSampleScan(void) } /* + * _readPartialSeqScan + */ +static PartialSeqScan * +_readPartialSeqScan(void) +{ + READ_LOCALS_NO_FIELDS(PartialSeqScan); + + ReadCommonScan(local_node); + + READ_DONE(); +} + +/* * _readIndexScan */ static IndexScan * @@ -2323,6 +2336,8 @@ parseNodeString(void) return_value = _readSeqScan(); else if (MATCH("SAMPLESCAN", 10)) return_value = _readSampleScan(); + else if (MATCH("PARTIALSEQSCAN", 14)) + return_value = _readPartialSeqScan(); else if (MATCH("INDEXSCAN", 9)) return_value = _readIndexScan(); else if (MATCH("INDEXONLYSCAN", 13)) diff --git a/src/backend/optimizer/path/Makefile b/src/backend/optimizer/path/Makefile index 6864a62..6e462b1 100644 --- a/src/backend/optimizer/path/Makefile +++ b/src/backend/optimizer/path/Makefile @@ -13,6 +13,6 @@ top_builddir = ../../../.. include $(top_builddir)/src/Makefile.global OBJS = allpaths.o clausesel.o costsize.o equivclass.o indxpath.o \ - joinpath.o joinrels.o pathkeys.o tidpath.o + joinpath.o joinrels.o pathkeys.o parallelpath.o tidpath.o include $(top_srcdir)/src/backend/common.mk diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 8fc1cfd..c2ae95d 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -477,6 +477,9 @@ set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) /* Consider sequential scan */ add_path(rel, create_seqscan_path(root, rel, required_outer)); + /* Consider parallel scans */ + create_parallelscan_paths(root, rel, required_outer); + /* Consider index scans */ create_index_paths(root, rel); diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 1b61fd9..9b0bb8c 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -296,6 +296,50 @@ cost_samplescan(Path *path, PlannerInfo *root, } /* + * cost_partialseqscan + * Determines and returns the cost of scanning a relation partially. + * + * 'baserel' is the relation to be scanned + * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL + * 'nworkers' are the number of workers among which the work will be + * distributed + */ +void +cost_partialseqscan(Path *path, PlannerInfo *root, + RelOptInfo *baserel, ParamPathInfo *param_info, + int nworkers) +{ + Cost startup_cost = 0; + Cost run_cost = 0; + + cost_seqscan(path, root, baserel, param_info); + + startup_cost = path->startup_cost; + + run_cost = path->total_cost - startup_cost; + + /* + * Account for small cost for communication related to scan + * via the ParallelHeapScanDesc. + */ + run_cost += 0.01; + + /* + * Runtime cost will be equally shared by all workers. + * Here assumption is that disk access cost will also be + * equally shared between workers which is generally true + * unless there are too many workers working on a relatively + * lesser number of blocks. If we come across any such case, + * then we can think of changing the current cost model for + * partial sequiantial scan. + */ + run_cost = run_cost / (nworkers + 1); + + path->startup_cost = startup_cost; + path->total_cost = startup_cost + run_cost; +} + +/* * cost_gather * Determines and returns the cost of gather path. * diff --git a/src/backend/optimizer/path/parallelpath.c b/src/backend/optimizer/path/parallelpath.c new file mode 100644 index 0000000..7dce2a1 --- /dev/null +++ b/src/backend/optimizer/path/parallelpath.c @@ -0,0 +1,90 @@ +/*------------------------------------------------------------------------- + * + * parallelpath.c + * Routines to determine parallel paths for scanning a given relation. + * + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/optimizer/path/parallelpath.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/heapam.h" +#include "optimizer/cost.h" +#include "optimizer/pathnode.h" +#include "optimizer/paths.h" +#include "parser/parsetree.h" +#include "utils/rel.h" + + +/* + * create_parallelscan_paths + * Create paths corresponding to parallel scans of the given rel. + * Currently we only support partial sequential scan. + * + * Candidate paths are added to the rel's pathlist (using add_path). + */ +void +create_parallelscan_paths(PlannerInfo *root, RelOptInfo *rel, + Relids required_outer) +{ + int num_parallel_workers = 0; + int estimated_parallel_workers = 0; + Oid reloid; + Relation relation; + Path *subpath; + + /* + * parallel scan is possible only if user has set parallel_seqscan_degree + * to value greater than 0 and the query is parallel-safe. + */ + if (max_parallel_degree <= 0 || !root->glob->parallelModeOK) + return; + + /* + * There should be atleast a thousand pages to scan for each worker. This + * number is somewhat arbitratry, however we don't want to spawn workers + * to scan smaller relations as that will be costly. + */ + estimated_parallel_workers = rel->pages / 1000; + + if (estimated_parallel_workers <= 0) + return; + + reloid = planner_rt_fetch(rel->relid, root)->relid; + + relation = heap_open(reloid, NoLock); + + /* + * Temporary relations can't be scanned by parallel workers as they are + * visible only to local sessions. + */ + if (RelationUsesLocalBuffers(relation)) + { + heap_close(relation, NoLock); + return; + } + + heap_close(relation, NoLock); + + num_parallel_workers = Min(max_parallel_degree, + estimated_parallel_workers); + + /* + * Create the partial scan path which each worker backend needs to + * execute. + */ + subpath = create_partialseqscan_path(root, rel, required_outer, + num_parallel_workers); + + /* Create the funnel path which master backend needs to execute. */ + add_path(rel, (Path *) create_gather_path(root, rel, subpath, + required_outer, + num_parallel_workers)); +} diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 0ee7392..a334155 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -60,6 +60,8 @@ static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses); static SampleScan *create_samplescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses); +static Scan *create_partialseqscan_plan(PlannerInfo *root, Path *best_path, + List *tlist, List *scan_clauses); static Gather *create_gather_plan(PlannerInfo *root, GatherPath *best_path); static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, @@ -106,6 +108,8 @@ static void copy_plan_costsize(Plan *dest, Plan *src); static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid); static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc); +static PartialSeqScan *make_partialseqscan(List *qptlist, List *qpqual, + Index scanrelid); static Gather *make_gather(List *qptlist, List *qpqual, int nworkers, bool single_copy, Plan *subplan); static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid, @@ -238,6 +242,7 @@ create_plan_recurse(PlannerInfo *root, Path *best_path) { case T_SeqScan: case T_SampleScan: + case T_PartialSeqScan: case T_IndexScan: case T_IndexOnlyScan: case T_BitmapHeapScan: @@ -364,6 +369,13 @@ create_scan_plan(PlannerInfo *root, Path *best_path) scan_clauses); break; + case T_PartialSeqScan: + plan = (Plan *) create_partialseqscan_plan(root, + best_path, + tlist, + scan_clauses); + break; + case T_IndexScan: plan = (Plan *) create_indexscan_plan(root, (IndexPath *) best_path, @@ -568,6 +580,7 @@ disuse_physical_tlist(PlannerInfo *root, Plan *plan, Path *path) { case T_SeqScan: case T_SampleScan: + case T_PartialSeqScan: case T_IndexScan: case T_IndexOnlyScan: case T_BitmapHeapScan: @@ -1110,6 +1123,46 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path) } /* + * create_partialseqscan_plan + * + * Returns a partial seqscan plan for the base relation scanned by + * 'best_path' with restriction clauses 'scan_clauses' and targetlist + * 'tlist'. + */ +static Scan * +create_partialseqscan_plan(PlannerInfo *root, Path *best_path, + List *tlist, List *scan_clauses) +{ + Scan *scan_plan; + Index scan_relid = best_path->parent->relid; + + /* it should be a base rel... */ + Assert(scan_relid > 0); + Assert(best_path->parent->rtekind == RTE_RELATION); + + /* Sort clauses into best execution order */ + scan_clauses = order_qual_clauses(root, scan_clauses); + + /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */ + scan_clauses = extract_actual_clauses(scan_clauses, false); + + /* Replace any outer-relation variables with nestloop params */ + if (best_path->param_info) + { + scan_clauses = (List *) + replace_nestloop_params(root, (Node *) scan_clauses); + } + + scan_plan = (Scan *) make_partialseqscan(tlist, + scan_clauses, + scan_relid); + + copy_path_costsize(&scan_plan->plan, best_path); + + return scan_plan; +} + +/* * create_gather_plan * * Create a Gather plan for 'best_path' and (recursively) plans @@ -4771,6 +4824,24 @@ make_unique(Plan *lefttree, List *distinctList) return node; } +static PartialSeqScan * +make_partialseqscan(List *qptlist, + List *qpqual, + Index scanrelid) +{ + PartialSeqScan *node = makeNode(PartialSeqScan); + Plan *plan = &node->plan; + + /* cost should be inserted by caller */ + plan->targetlist = qptlist; + plan->qual = qpqual; + plan->lefttree = NULL; + plan->righttree = NULL; + node->scanrelid = scanrelid; + + return node; +} + static Gather * make_gather(List *qptlist, List *qpqual, diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c index b1cede2..6a82d4b 100644 --- a/src/backend/optimizer/plan/setrefs.c +++ b/src/backend/optimizer/plan/setrefs.c @@ -447,6 +447,7 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset) switch (nodeTag(plan)) { case T_SeqScan: + case T_PartialSeqScan: { SeqScan *splan = (SeqScan *) plan; diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c index 6b32f85..b85e4f6 100644 --- a/src/backend/optimizer/plan/subselect.c +++ b/src/backend/optimizer/plan/subselect.c @@ -2234,6 +2234,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params, break; case T_SeqScan: + case T_PartialSeqScan: context.paramids = bms_add_members(context.paramids, scan_params); break; diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index 1895a68..68863b9 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -1308,6 +1308,28 @@ create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, } /* + * create_partialseqscan_path + * Creates a path corresponding to a partial sequential scan, returning the + * pathnode. + */ +Path * +create_partialseqscan_path(PlannerInfo *root, RelOptInfo *rel, + Relids required_outer, int nworkers) +{ + Path *pathnode = makeNode(Path); + + pathnode->pathtype = T_PartialSeqScan; + pathnode->parent = rel; + pathnode->param_info = get_baserel_parampathinfo(root, rel, + required_outer); + pathnode->pathkeys = NIL; /* partialseqscan has unordered result */ + + cost_partialseqscan(pathnode, root, rel, pathnode->param_info, nworkers); + + return pathnode; +} + +/* * create_gather_path * * Creates a path corresponding to a gather scan, returning the diff --git a/src/include/executor/execParallel.h b/src/include/executor/execParallel.h index 4fc797a..d171285 100644 --- a/src/include/executor/execParallel.h +++ b/src/include/executor/execParallel.h @@ -18,6 +18,8 @@ #include "nodes/parsenodes.h" #include "nodes/plannodes.h" +#define PARALLEL_KEY_SCAN UINT64CONST(0xE000000000000006) + typedef struct SharedExecutorInstrumentation SharedExecutorInstrumentation; typedef struct ParallelExecutorInfo @@ -32,5 +34,6 @@ typedef struct ParallelExecutorInfo extern ParallelExecutorInfo *ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers); extern void ExecParallelFinish(ParallelExecutorInfo *pei); +extern shm_toc *GetParallelShmToc(void); #endif /* EXECPARALLEL_H */ diff --git a/src/include/executor/nodePartialSeqscan.h b/src/include/executor/nodePartialSeqscan.h new file mode 100644 index 0000000..cec09ad --- /dev/null +++ b/src/include/executor/nodePartialSeqscan.h @@ -0,0 +1,25 @@ +/*------------------------------------------------------------------------- + * + * nodePartialSeqscan.h + * prototypes for nodePartialSeqscan.c + * + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/executor/nodePartialSeqscan.h + * + *------------------------------------------------------------------------- + */ +#ifndef NODEPARTIALSEQSCAN_H +#define NODEPARTIALSEQSCAN_H + +#include "nodes/execnodes.h" + +extern PartialSeqScanState *ExecInitPartialSeqScan(PartialSeqScan *node, + EState *estate, int eflags); +extern TupleTableSlot *ExecPartialSeqScan(PartialSeqScanState *node); +extern void ExecEndPartialSeqScan(PartialSeqScanState *node); +extern void ExecReScanPartialSeqScan(PartialSeqScanState *node); + +#endif /* NODEPARTIALSEQSCAN_H */ diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index b6895f9..9ff8216 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -16,6 +16,7 @@ #include "access/genam.h" #include "access/heapam.h" +#include "access/parallel.h" #include "executor/instrument.h" #include "lib/pairingheap.h" #include "nodes/params.h" @@ -1049,6 +1050,13 @@ typedef struct PlanState Bitmapset *chgParam; /* set of IDs of changed Params */ /* + * At execution time, parallel scan descriptor is initialized and stored + * in dynamic shared memory segment by master backend and parallel workers + * retrieve it from shared memory. + */ + shm_toc *toc; + + /* * Other run-time state needed by most if not all node types. */ TupleTableSlot *ps_ResultTupleSlot; /* slot for my result tuples */ @@ -1273,6 +1281,17 @@ typedef struct SampleScanState } SampleScanState; /* + * PartialSeqScanState extends ScanState by storing additional information + * related to scan. + */ +typedef struct PartialSeqScanState +{ + ScanState ss; /* its first field is NodeTag */ + bool scan_initialized; /* used to determine if the scan is initialized */ +} PartialSeqScanState; + + +/* * These structs store information about index quals that don't have simple * constant right-hand sides. See comments for ExecIndexBuildScanKeys() * for discussion. diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 94bdb7c..d1feab2 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -52,6 +52,7 @@ typedef enum NodeTag T_Scan, T_SeqScan, T_SampleScan, + T_PartialSeqScan, T_IndexScan, T_IndexOnlyScan, T_BitmapIndexScan, @@ -100,6 +101,7 @@ typedef enum NodeTag T_ScanState, T_SeqScanState, T_SampleScanState, + T_PartialSeqScanState, T_IndexScanState, T_IndexOnlyScanState, T_BitmapIndexScanState, diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h index 1f9213c..1e91ce8 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -287,6 +287,12 @@ typedef struct Scan typedef Scan SeqScan; /* ---------------- + * partial sequential scan node + * ---------------- + */ +typedef SeqScan PartialSeqScan; + +/* ---------------- * table sample scan node * ---------------- */ diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h index 25a7303..f08241a 100644 --- a/src/include/optimizer/cost.h +++ b/src/include/optimizer/cost.h @@ -75,6 +75,9 @@ extern void cost_seqscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info); extern void cost_samplescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info); +extern void cost_partialseqscan(Path *path, PlannerInfo *root, + RelOptInfo *baserel, ParamPathInfo *param_info, + int nworkers); extern void cost_index(IndexPath *path, PlannerInfo *root, double loop_count); extern void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h index 7a4940c..cb8ce98 100644 --- a/src/include/optimizer/pathnode.h +++ b/src/include/optimizer/pathnode.h @@ -34,6 +34,8 @@ extern Path *create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer); extern Path *create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer); +extern Path *create_partialseqscan_path(PlannerInfo *root, RelOptInfo *rel, + Relids required_outer, int nworkers); extern IndexPath *create_index_path(PlannerInfo *root, IndexOptInfo *index, List *indexclauses, diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h index 87123a5..e7db9ab 100644 --- a/src/include/optimizer/paths.h +++ b/src/include/optimizer/paths.h @@ -55,6 +55,14 @@ extern void debug_print_rel(PlannerInfo *root, RelOptInfo *rel); #endif /* + * parallelpath.c + * routines to generate parallel scan paths + */ + +extern void create_parallelscan_paths(PlannerInfo *root, RelOptInfo *rel, + Relids required_outer); + +/* * indxpath.c * routines to generate index paths */ diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index feb821b..5492ba0 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -1199,6 +1199,8 @@ OverrideStackEntry PACE_HEADER PACL ParallelExecutorInfo +PartialSeqScan +PartialSeqScanState PATH PBOOL PCtxtHandle