diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index baed981..639451a 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -26,9 +26,9 @@ static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo); -static void printtup(TupleTableSlot *slot, DestReceiver *self); -static void printtup_20(TupleTableSlot *slot, DestReceiver *self); -static void printtup_internal_20(TupleTableSlot *slot, DestReceiver *self); +static bool printtup(TupleTableSlot *slot, DestReceiver *self); +static bool printtup_20(TupleTableSlot *slot, DestReceiver *self); +static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self); static void printtup_shutdown(DestReceiver *self); static void printtup_destroy(DestReceiver *self); @@ -299,7 +299,7 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs) * printtup --- print a tuple in protocol 3.0 * ---------------- */ -static void +static bool printtup(TupleTableSlot *slot, DestReceiver *self) { TupleDesc typeinfo = slot->tts_tupleDescriptor; @@ -376,13 +376,15 @@ printtup(TupleTableSlot *slot, DestReceiver *self) /* Return to caller's context, and flush row's temporary memory */ MemoryContextSwitchTo(oldcontext); MemoryContextReset(myState->tmpcontext); + + return true; } /* ---------------- * printtup_20 --- print a tuple in protocol 2.0 * ---------------- */ -static void +static bool printtup_20(TupleTableSlot *slot, DestReceiver *self) { TupleDesc typeinfo = slot->tts_tupleDescriptor; @@ -452,6 +454,8 @@ printtup_20(TupleTableSlot *slot, DestReceiver *self) /* Return to caller's context, and flush row's temporary memory */ MemoryContextSwitchTo(oldcontext); MemoryContextReset(myState->tmpcontext); + + return true; } /* ---------------- @@ -528,7 +532,7 @@ debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo) * debugtup - print one tuple for an interactive backend * ---------------- */ -void +bool debugtup(TupleTableSlot *slot, DestReceiver *self) { TupleDesc typeinfo = slot->tts_tupleDescriptor; @@ -553,6 +557,8 @@ debugtup(TupleTableSlot *slot, DestReceiver *self) printatt((unsigned) i + 1, typeinfo->attrs[i], value); } printf("\t----\n"); + + return true; } /* ---------------- @@ -564,7 +570,7 @@ debugtup(TupleTableSlot *slot, DestReceiver *self) * This is largely same as printtup_20, except we use binary formatting. * ---------------- */ -static void +static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self) { TupleDesc typeinfo = slot->tts_tupleDescriptor; @@ -636,4 +642,6 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self) /* Return to caller's context, and flush row's temporary memory */ MemoryContextSwitchTo(oldcontext); MemoryContextReset(myState->tmpcontext); + + return true; } diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index f409aa7..42d4a44 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -4411,7 +4411,7 @@ copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo) /* * copy_dest_receive --- receive one tuple */ -static void +static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self) { DR_copy *myState = (DR_copy *) self; @@ -4423,6 +4423,8 @@ copy_dest_receive(TupleTableSlot *slot, DestReceiver *self) /* And send the data */ CopyOneRowTo(cstate, InvalidOid, slot->tts_values, slot->tts_isnull); myState->processed++; + + return true; } /* diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c index 41183f6..418b0f6 100644 --- a/src/backend/commands/createas.c +++ b/src/backend/commands/createas.c @@ -62,7 +62,7 @@ typedef struct static ObjectAddress CreateAsReladdr = {InvalidOid, InvalidOid, 0}; static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo); -static void intorel_receive(TupleTableSlot *slot, DestReceiver *self); +static bool intorel_receive(TupleTableSlot *slot, DestReceiver *self); static void intorel_shutdown(DestReceiver *self); static void intorel_destroy(DestReceiver *self); @@ -482,7 +482,7 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo) /* * intorel_receive --- receive one tuple */ -static void +static bool intorel_receive(TupleTableSlot *slot, DestReceiver *self) { DR_intorel *myState = (DR_intorel *) self; @@ -507,6 +507,8 @@ intorel_receive(TupleTableSlot *slot, DestReceiver *self) myState->bistate); /* We know this is a newly created relation, so there are no indexes */ + + return true; } /* diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index f0d9e94..4481505 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_Gather: 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_Gather: + pname = sname = "Gather"; + break; case T_IndexScan: pname = sname = "Index Scan"; break; @@ -1003,6 +1007,7 @@ ExplainNode(PlanState *planstate, List *ancestors, { case T_SeqScan: case T_SampleScan: + case T_Gather: case T_BitmapHeapScan: case T_TidScan: case T_SubqueryScan: @@ -1276,6 +1281,14 @@ ExplainNode(PlanState *planstate, List *ancestors, show_instrumentation_count("Rows Removed by Filter", 1, planstate, es); break; + case T_Gather: + show_scan_qual(plan->qual, "Filter", planstate, ancestors, es); + if (plan->qual) + show_instrumentation_count("Rows Removed by Filter", 1, + planstate, es); + ExplainPropertyInteger("Number of Workers", + ((Gather *) plan)->num_workers, es); + break; case T_FunctionScan: if (es->verbose) { @@ -2335,6 +2348,7 @@ ExplainTargetRel(Plan *plan, Index rti, ExplainState *es) { case T_SeqScan: case T_SampleScan: + case T_Gather: case T_IndexScan: case T_IndexOnlyScan: case T_BitmapHeapScan: diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index 5492e59..750a59c 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -56,7 +56,7 @@ typedef struct static int matview_maintenance_depth = 0; static void transientrel_startup(DestReceiver *self, int operation, TupleDesc typeinfo); -static void transientrel_receive(TupleTableSlot *slot, DestReceiver *self); +static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self); static void transientrel_shutdown(DestReceiver *self); static void transientrel_destroy(DestReceiver *self); static void refresh_matview_datafill(DestReceiver *dest, Query *query, @@ -422,7 +422,7 @@ transientrel_startup(DestReceiver *self, int operation, TupleDesc typeinfo) /* * transientrel_receive --- receive one tuple */ -static void +static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self) { DR_transientrel *myState = (DR_transientrel *) self; @@ -441,6 +441,8 @@ transientrel_receive(TupleTableSlot *slot, DestReceiver *self) myState->bistate); /* We know this is a newly created relation, so there are no indexes */ + + return true; } /* diff --git a/src/backend/executor/Makefile b/src/backend/executor/Makefile index f5e1e1a..51edd4c 100644 --- a/src/backend/executor/Makefile +++ b/src/backend/executor/Makefile @@ -17,8 +17,8 @@ OBJS = execAmi.o execCurrent.o execGrouping.o execIndexing.o execJunk.o \ execScan.o execTuples.o \ execUtils.o functions.o instrument.o nodeAppend.o nodeAgg.o \ nodeBitmapAnd.o nodeBitmapOr.o \ - nodeBitmapHeapscan.o nodeBitmapIndexscan.o nodeCustom.o nodeHash.o \ - nodeHashjoin.o nodeIndexscan.o nodeIndexonlyscan.o \ + nodeBitmapHeapscan.o nodeBitmapIndexscan.o nodeCustom.o nodeGather.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 \ diff --git a/src/backend/executor/execAmi.c b/src/backend/executor/execAmi.c index 93e1e9a..163650c 100644 --- a/src/backend/executor/execAmi.c +++ b/src/backend/executor/execAmi.c @@ -24,6 +24,7 @@ #include "executor/nodeCustom.h" #include "executor/nodeForeignscan.h" #include "executor/nodeFunctionscan.h" +#include "executor/nodeGather.h" #include "executor/nodeGroup.h" #include "executor/nodeGroup.h" #include "executor/nodeHash.h" @@ -160,6 +161,10 @@ ExecReScan(PlanState *node) ExecReScanSampleScan((SampleScanState *) node); break; + case T_GatherState: + ExecReScanGather((GatherState *) node); + break; + case T_IndexScanState: ExecReScanIndexScan((IndexScanState *) node); break; @@ -467,6 +472,9 @@ ExecSupportsBackwardScan(Plan *node) /* Simplify life for tablesample methods by disallowing this */ return false; + case T_Gather: + return false; + case T_IndexScan: return IndexSupportsBackwardScan(((IndexScan *) node)->indexid) && TargetListSupportsBackwardScan(node->targetlist); diff --git a/src/backend/executor/execCurrent.c b/src/backend/executor/execCurrent.c index bcd287f..fd89204 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_GatherState: case T_IndexScanState: case T_IndexOnlyScanState: case T_BitmapHeapScanState: diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 85ff46b..1dc2ad7 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -348,6 +348,14 @@ standard_ExecutorRun(QueryDesc *queryDesc, dest); /* + * Accumulate stats and shutdown parallel workers before stopping node. + * Though we already accumulate this information when last tuple is + * fetched from parallel workers, this is to cover cases when we don't + * fetch all tuples from a node such as for Limit node. + */ + (void) ExecShutdownNode(queryDesc->planstate); + + /* * shutdown tuple receiver, if we started it */ if (sendTuples) @@ -1581,7 +1589,15 @@ ExecutePlan(EState *estate, * practice, this is probably always the case at this point.) */ if (sendTuples) - (*dest->receiveSlot) (slot, dest); + { + /* + * If we are not able to send the tuple, we assume the destination + * has closed and no more tuples can be sent. If that's the case, + * end the loop. + */ + if (!((*dest->receiveSlot) (slot, dest))) + break; + } /* * Count tuples processed, if this is a SELECT. (For other operation diff --git a/src/backend/executor/execProcnode.c b/src/backend/executor/execProcnode.c index 03c2feb..578e28b 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/nodeGather.h" #include "executor/nodeRecursiveunion.h" #include "executor/nodeResult.h" #include "executor/nodeSamplescan.h" @@ -113,6 +114,7 @@ #include "executor/nodeValuesscan.h" #include "executor/nodeWindowAgg.h" #include "executor/nodeWorktablescan.h" +#include "nodes/nodeFuncs.h" #include "miscadmin.h" @@ -196,6 +198,11 @@ ExecInitNode(Plan *node, EState *estate, int eflags) estate, eflags); break; + case T_Gather: + result = (PlanState *) ExecInitGather((Gather *) node, + estate, eflags); + break; + case T_IndexScan: result = (PlanState *) ExecInitIndexScan((IndexScan *) node, estate, eflags); @@ -416,6 +423,10 @@ ExecProcNode(PlanState *node) result = ExecSampleScan((SampleScanState *) node); break; + case T_GatherState: + result = ExecGather((GatherState *) node); + break; + case T_IndexScanState: result = ExecIndexScan((IndexScanState *) node); break; @@ -658,6 +669,10 @@ ExecEndNode(PlanState *node) ExecEndSampleScan((SampleScanState *) node); break; + case T_GatherState: + ExecEndGather((GatherState *) node); + break; + case T_IndexScanState: ExecEndIndexScan((IndexScanState *) node); break; @@ -769,3 +784,30 @@ ExecEndNode(PlanState *node) break; } } + +/* + * ExecShutdownNode + * + * Recursively shutdown and accumulate the stats for all the Gather nodes + * in a plan state tree. + */ +bool +ExecShutdownNode(PlanState *node) +{ + if (node == NULL) + return false; + + switch (nodeTag(node)) + { + case T_GatherState: + { + ExecShutdownGather((GatherState *) node); + return true; + } + break; + default: + break; + } + + return planstate_tree_walker(node, ExecShutdownNode, NULL); +} diff --git a/src/backend/executor/execTuples.c b/src/backend/executor/execTuples.c index a05d8b1..d5619bd 100644 --- a/src/backend/executor/execTuples.c +++ b/src/backend/executor/execTuples.c @@ -1313,7 +1313,7 @@ do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull) ExecStoreVirtualTuple(slot); /* send the tuple to the receiver */ - (*tstate->dest->receiveSlot) (slot, tstate->dest); + (void) (*tstate->dest->receiveSlot) (slot, tstate->dest); /* clean up */ ExecClearTuple(slot); diff --git a/src/backend/executor/functions.c b/src/backend/executor/functions.c index 812a610..863bd64 100644 --- a/src/backend/executor/functions.c +++ b/src/backend/executor/functions.c @@ -167,7 +167,7 @@ static Datum postquel_get_single_result(TupleTableSlot *slot, static void sql_exec_error_callback(void *arg); static void ShutdownSQLFunction(Datum arg); static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo); -static void sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self); +static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self); static void sqlfunction_shutdown(DestReceiver *self); static void sqlfunction_destroy(DestReceiver *self); @@ -1903,7 +1903,7 @@ sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo) /* * sqlfunction_receive --- receive one tuple */ -static void +static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self) { DR_sqlfunction *myState = (DR_sqlfunction *) self; @@ -1913,6 +1913,8 @@ sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self) /* Store the filtered tuple into the tuplestore */ tuplestore_puttupleslot(myState->tstore, slot); + + return true; } /* diff --git a/src/backend/executor/nodeGather.c b/src/backend/executor/nodeGather.c new file mode 100644 index 0000000..6ec3c86 --- /dev/null +++ b/src/backend/executor/nodeGather.c @@ -0,0 +1,309 @@ +/*------------------------------------------------------------------------- + * + * nodeGather.c + * Support routines for scanning a relation via multiple workers. + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/executor/nodeGather.c + * + *------------------------------------------------------------------------- + */ +/* + * INTERFACE ROUTINES + * ExecGather scans a relation using worker backends. + * ExecInitGather creates and initializes a Gather node. + * ExecEndGather releases any storage allocated. + * ExecReScanGather Re-initialize the workers and rescans a relation via them. + */ +#include "postgres.h" + +#include "access/relscan.h" +#include "executor/execdebug.h" +#include "executor/execParallel.h" +#include "executor/nodeGather.h" +#include "executor/nodeSubplan.h" +#include "utils/rel.h" + + +static TupleTableSlot *gather_getnext(GatherState *gatherstate); + + +/* ---------------------------------------------------------------- + * ExecInitGather + * ---------------------------------------------------------------- + */ +GatherState * +ExecInitGather(Gather *node, EState *estate, int eflags) +{ + GatherState *gatherstate; + + /* Gather node doesn't have innerPlan node. */ + Assert(innerPlan(node) == NULL); + + /* + * create state structure + */ + gatherstate = makeNode(GatherState); + gatherstate->ps.plan = (Plan *) node; + gatherstate->ps.state = estate; + gatherstate->any_worker_launched = false; + + /* + * Miscellaneous initialization + * + * create expression context for node + */ + ExecAssignExprContext(estate, &gatherstate->ps); + + /* + * initialize child expressions + */ + gatherstate->ps.targetlist = (List *) + ExecInitExpr((Expr *) node->plan.targetlist, + (PlanState *) gatherstate); + gatherstate->ps.qual = (List *) + ExecInitExpr((Expr *) node->plan.qual, + (PlanState *) gatherstate); + + /* + * tuple table initialization + */ + ExecInitResultTupleSlot(estate, &gatherstate->ps); + + /* + * now initialize outer plan + */ + outerPlanState(gatherstate) = ExecInitNode(outerPlan(node), estate, eflags); + + + gatherstate->ps.ps_TupFromTlist = false; + + /* + * Initialize result tuple type and projection info. + */ + ExecAssignResultTypeFromTL(&gatherstate->ps); + ExecAssignProjectionInfo(&gatherstate->ps, NULL); + + return gatherstate; +} + +/* ---------------------------------------------------------------- + * ExecGather(node) + * + * Scans the relation via multiple workers and returns + * the next qualifying tuple. + * ---------------------------------------------------------------- + */ +TupleTableSlot * +ExecGather(GatherState *node) +{ + int i; + TupleTableSlot *slot; + + /* + * Initialize the parallel context and workers on first execution. We do + * this on first execution rather than during node initialization, as it + * needs to allocate large dynamic segement, so it is better to do if it + * is really needed. + */ + if (!node->pei->pcxt) + { + EState *estate = node->ps.state; + bool any_worker_launched = false; + + /* Initialize the workers required to execute Gather node. */ + node->pei = ExecInitParallelPlan(node->ps.lefttree, + 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 + * there are no more workers available, then the Gather node will just + * scan locally. + */ + LaunchParallelWorkers(node->pei->pcxt); + + node->funnel = CreateTupleQueueFunnel(); + + for (i = 0; i < node->pei->pcxt->nworkers; ++i) + { + if (node->pei->pcxt->worker[i].bgwhandle) + { + shm_mq_set_handle((node->pei->tqueue)[i], node->pei->pcxt->worker[i].bgwhandle); + RegisterTupleQueueOnFunnel(node->funnel, (node->pei->tqueue)[i]); + any_worker_launched = true; + } + } + + if (any_worker_launched) + node->any_worker_launched = true; + } + + slot = gather_getnext(node); + + if (TupIsNull(slot)) + { + /* + * Destroy the parallel context once we complete fetching all the + * tuples, this will ensure that if in the same statement we need to + * have Gather node for multiple parts of statement, it won't + * accumulate lot of dsm segments and workers can be made available to + * use by other parts of statement. + */ + ExecShutdownGather(node); + } + return slot; +} + +/* ---------------------------------------------------------------- + * ExecEndGather + * + * frees any storage allocated through C routines. + * ---------------------------------------------------------------- + */ +void +ExecEndGather(GatherState *node) +{ + /* + * Free the exprcontext + */ + ExecFreeExprContext(&node->ps); + + /* + * clean out the tuple table + */ + ExecClearTuple(node->ps.ps_ResultTupleSlot); + + ExecEndNode(outerPlanState(node)); + + ExecShutdownGather(node); +} + +/* + * gather_getnext + * + * Get the next tuple from shared memory queue. This function + * is reponsible for fetching tuples from all the queues associated + * with worker backends used in Gather node execution and if there is + * no data available from queues or no worker is available, it does + * fetch the data from local node. + */ +TupleTableSlot * +gather_getnext(GatherState *gatherstate) +{ + PlanState *outerPlan; + TupleTableSlot *outerTupleSlot; + TupleTableSlot *slot; + HeapTuple tup; + + /* + * We can use projection info of Gather for the tuples received from + * worker backends as currently for all cases worker backends sends the + * projected tuple as required by Gather node. + */ + slot = gatherstate->ps.ps_ProjInfo->pi_slot; + + while ((!gatherstate->all_workers_done && + gatherstate->any_worker_launched) || + !gatherstate->local_scan_done) + { + if (!gatherstate->all_workers_done && gatherstate->any_worker_launched) + { + /* wait only if local scan is done */ + tup = TupleQueueFunnelNext(gatherstate->funnel, + !gatherstate->local_scan_done, + &gatherstate->all_workers_done); + + if (HeapTupleIsValid(tup)) + { + ExecStoreTuple(tup, /* tuple to store */ + slot, /* slot to store in */ + InvalidBuffer, /* buffer associated with this + * tuple */ + true); /* pfree this pointer if not from heap */ + + return slot; + } + } + if (!gatherstate->local_scan_done) + { + outerPlan = outerPlanState(gatherstate); + + outerTupleSlot = ExecProcNode(outerPlan); + + if (!TupIsNull(outerTupleSlot)) + return outerTupleSlot; + + gatherstate->local_scan_done = true; + } + } + + return ExecClearTuple(slot); +} + +/* ---------------------------------------------------------------- + * ExecShutdownGather + * + * Destroy the setup for parallel workers. Collect all the + * stats after workers are stopped, else some work done by + * workers won't be accounted. + * ---------------------------------------------------------------- + */ +void +ExecShutdownGather(GatherState *node) +{ + if (node->pei == NULL || node->pei->pcxt == NULL) + return; + + /* + * Ensure all workers have finished before destroying the parallel context + * to ensure a clean exit. + */ + if (node->any_worker_launched) + { + DestroyTupleQueueFunnel(node->funnel); + node->funnel = NULL; + } + + ExecParallelFinish(node->pei); + + /* destroy parallel context. */ + DestroyParallelContext(node->pei->pcxt); + node->pei->pcxt = NULL; + + node->any_worker_launched = false; + node->all_workers_done = false; + node->local_scan_done = false; +} + +/* ---------------------------------------------------------------- + * Join Support + * ---------------------------------------------------------------- + */ + +/* ---------------------------------------------------------------- + * ExecReScanGather + * + * Re-initialize the workers and rescans a relation via them. + * ---------------------------------------------------------------- + */ +void +ExecReScanGather(GatherState *node) +{ + /* + * Re-initialize the parallel context and workers to perform rescan of + * relation. We want to gracefully shutdown all the workers so that they + * should be able to propagate any error or other information to master + * backend before dying. + */ + ExecShutdownGather(node); + + ExecReScan(node->ps.lefttree); +} diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c index 300401e..a60f228 100644 --- a/src/backend/executor/spi.c +++ b/src/backend/executor/spi.c @@ -1774,7 +1774,7 @@ spi_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo) * store tuple retrieved by Executor into SPITupleTable * of current SPI procedure */ -void +bool spi_printtup(TupleTableSlot *slot, DestReceiver *self) { SPITupleTable *tuptable; @@ -1809,6 +1809,8 @@ spi_printtup(TupleTableSlot *slot, DestReceiver *self) (tuptable->free)--; MemoryContextSwitchTo(oldcxt); + + return true; } /* diff --git a/src/backend/executor/tqueue.c b/src/backend/executor/tqueue.c index 67143d3..28ba49f 100644 --- a/src/backend/executor/tqueue.c +++ b/src/backend/executor/tqueue.c @@ -41,14 +41,24 @@ struct TupleQueueFunnel /* * Receive a tuple. */ -static void +static bool tqueueReceiveSlot(TupleTableSlot *slot, DestReceiver *self) { TQueueDestReceiver *tqueue = (TQueueDestReceiver *) self; HeapTuple tuple; + shm_mq_result result; tuple = ExecMaterializeSlot(slot); - shm_mq_send(tqueue->handle, tuple->t_len, tuple->t_data, false); + result = shm_mq_send(tqueue->handle, tuple->t_len, tuple->t_data, false); + + if (result == SHM_MQ_DETACHED) + return false; + else if (result != SHM_MQ_SUCCESS) + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("unable to send tuples"))); + + return true; } /* diff --git a/src/backend/executor/tstoreReceiver.c b/src/backend/executor/tstoreReceiver.c index c1fdeb7..b0862ae 100644 --- a/src/backend/executor/tstoreReceiver.c +++ b/src/backend/executor/tstoreReceiver.c @@ -37,8 +37,8 @@ typedef struct } TStoreState; -static void tstoreReceiveSlot_notoast(TupleTableSlot *slot, DestReceiver *self); -static void tstoreReceiveSlot_detoast(TupleTableSlot *slot, DestReceiver *self); +static bool tstoreReceiveSlot_notoast(TupleTableSlot *slot, DestReceiver *self); +static bool tstoreReceiveSlot_detoast(TupleTableSlot *slot, DestReceiver *self); /* @@ -90,19 +90,21 @@ tstoreStartupReceiver(DestReceiver *self, int operation, TupleDesc typeinfo) * Receive a tuple from the executor and store it in the tuplestore. * This is for the easy case where we don't have to detoast. */ -static void +static bool tstoreReceiveSlot_notoast(TupleTableSlot *slot, DestReceiver *self) { TStoreState *myState = (TStoreState *) self; tuplestore_puttupleslot(myState->tstore, slot); + + return true; } /* * Receive a tuple from the executor and store it in the tuplestore. * This is for the case where we have to detoast any toasted values. */ -static void +static bool tstoreReceiveSlot_detoast(TupleTableSlot *slot, DestReceiver *self) { TStoreState *myState = (TStoreState *) self; @@ -152,6 +154,8 @@ tstoreReceiveSlot_detoast(TupleTableSlot *slot, DestReceiver *self) /* And release any temporary detoasted values */ for (i = 0; i < nfree; i++) pfree(DatumGetPointer(myState->tofree[i])); + + return true; } /* diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 4b4ddec..085e6bd 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -314,6 +314,27 @@ _copyBitmapOr(const BitmapOr *from) return newnode; } +/* + * _copyGather + */ +static Gather * +_copyGather(const Gather *from) +{ + Gather *newnode = makeNode(Gather); + + /* + * copy node superclass fields + */ + CopyPlanFields((const Plan *) from, (Plan *) newnode); + + /* + * copy remainder of node + */ + COPY_SCALAR_FIELD(num_workers); + + return newnode; +} + /* * CopyScanFields @@ -4235,6 +4256,9 @@ copyObject(const void *from) case T_Scan: retval = _copyScan(from); break; + case T_Gather: + retval = _copyGather(from); + break; case T_SeqScan: retval = _copySeqScan(from); break; diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index ee9c360..0b5600e 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -433,6 +433,16 @@ _outBitmapOr(StringInfo str, const BitmapOr *node) } static void +_outGather(StringInfo str, const Gather *node) +{ + WRITE_NODE_TYPE("GATHER"); + + _outPlanInfo(str, (const Plan *) node); + + WRITE_UINT_FIELD(num_workers); +} + +static void _outScan(StringInfo str, const Scan *node) { WRITE_NODE_TYPE("SCAN"); @@ -3000,6 +3010,9 @@ _outNode(StringInfo str, const void *obj) case T_BitmapOr: _outBitmapOr(str, obj); break; + case T_Gather: + _outGather(str, obj); + break; case T_Scan: _outScan(str, obj); break; diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index d107d76..1b61fd9 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -11,6 +11,8 @@ * cpu_tuple_cost Cost of typical CPU time to process a tuple * cpu_index_tuple_cost Cost of typical CPU time to process an index tuple * cpu_operator_cost Cost of CPU time to execute an operator or function + * parallel_tuple_cost Cost of CPU time to pass a tuple from worker to master backend + * parallel_setup_cost Cost of setting up shared memory for parallelism * * We expect that the kernel will typically do some amount of read-ahead * optimization; this in conjunction with seek costs means that seq_page_cost @@ -102,11 +104,15 @@ double random_page_cost = DEFAULT_RANDOM_PAGE_COST; double cpu_tuple_cost = DEFAULT_CPU_TUPLE_COST; double cpu_index_tuple_cost = DEFAULT_CPU_INDEX_TUPLE_COST; double cpu_operator_cost = DEFAULT_CPU_OPERATOR_COST; +double parallel_tuple_cost = DEFAULT_PARALLEL_TUPLE_COST; +double parallel_setup_cost = DEFAULT_PARALLEL_SETUP_COST; int effective_cache_size = DEFAULT_EFFECTIVE_CACHE_SIZE; Cost disable_cost = 1.0e10; +int max_parallel_degree = 0; + bool enable_seqscan = true; bool enable_indexscan = true; bool enable_indexonlyscan = true; @@ -290,6 +296,38 @@ cost_samplescan(Path *path, PlannerInfo *root, } /* + * cost_gather + * Determines and returns the cost of gather path. + * + * 'rel' is the relation to be operated upon + * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL + */ +void +cost_gather(GatherPath *path, PlannerInfo *root, + RelOptInfo *rel, ParamPathInfo *param_info) +{ + Cost startup_cost = 0; + Cost run_cost = 0; + + /* Mark the path with the correct row estimate */ + if (param_info) + path->path.rows = param_info->ppi_rows; + else + path->path.rows = rel->rows; + + startup_cost = path->subpath->startup_cost; + + run_cost = path->subpath->total_cost - path->subpath->startup_cost; + + /* Parallel setup and communication cost. */ + startup_cost += parallel_setup_cost; + run_cost += parallel_tuple_cost * rel->tuples; + + path->path.startup_cost = startup_cost; + path->path.total_cost = (startup_cost + run_cost); +} + +/* * cost_index * Determines and returns the cost of scanning a relation using an index. * diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 404c6f5..6a1c7fc 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 Gather *create_gather_plan(PlannerInfo *root, + GatherPath *best_path); static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly); static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root, @@ -104,6 +106,9 @@ 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 Gather *make_gather(List *qptlist, List *qpqual, + Index scanrelid, int nworkers, + Plan *subplan); static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig, List *indexorderby, List *indexorderbyorig, @@ -273,6 +278,10 @@ create_plan_recurse(PlannerInfo *root, Path *best_path) plan = create_unique_plan(root, (UniquePath *) best_path); break; + case T_Gather: + plan = (Plan *) create_gather_plan(root, + (GatherPath *) best_path); + break; default: elog(ERROR, "unrecognized node type: %d", (int) best_path->pathtype); @@ -560,6 +569,7 @@ disuse_physical_tlist(PlannerInfo *root, Plan *plan, Path *path) { case T_SeqScan: case T_SampleScan: + case T_Gather: case T_IndexScan: case T_IndexOnlyScan: case T_BitmapHeapScan: @@ -1194,6 +1204,62 @@ create_samplescan_plan(PlannerInfo *root, Path *best_path, } /* + * create_gather_plan + * + * Returns a gather plan for the base relation scanned by + * 'best_path'. + */ +static Gather * +create_gather_plan(PlannerInfo *root, GatherPath *best_path) +{ + Gather *gather_plan; + Plan *subplan; + List *tlist; + RelOptInfo *rel = best_path->path.parent; + Index scan_relid = best_path->path.parent->relid; + + /* + * For table scans, rather than using the relation targetlist (which is + * only those Vars actually needed by the query), we prefer to generate a + * tlist containing all Vars in order. This will allow the executor to + * optimize away projection of the table tuples, if possible. (Note that + * planner.c may replace the tlist we generate here, forcing projection to + * occur.) + */ + if (use_physical_tlist(root, rel)) + { + tlist = build_physical_tlist(root, rel); + /* if fail because of dropped cols, use regular method */ + if (tlist == NIL) + tlist = build_path_tlist(root, &best_path->path); + } + else + { + tlist = build_path_tlist(root, &best_path->path); + } + + subplan = create_plan_recurse(root, best_path->subpath); + + /* + * quals for subplan and top level plan are same as either all the quals + * are pushed to subplan (partialseqscan plan) or parallel plan won't be + * choosen. + */ + gather_plan = make_gather(tlist, + subplan->qual, + scan_relid, + best_path->num_workers, + subplan); + + copy_path_costsize(&gather_plan->plan, &best_path->path); + + /* use parallel mode for parallel plans. */ + root->glob->parallelModeNeeded = true; + + return gather_plan; +} + +/* * create_indexscan_plan * Returns an indexscan plan for the base relation scanned by 'best_path' * with restriction clauses 'scan_clauses' and targetlist 'tlist'. @@ -3462,6 +3528,26 @@ make_samplescan(List *qptlist, return node; } +static Gather * +make_gather(List *qptlist, + List *qpqual, + Index scanrelid, + int nworkers, + Plan *subplan) +{ + Gather *node = makeNode(Gather); + Plan *plan = &node->plan; + + /* cost should be inserted by caller */ + plan->targetlist = qptlist; + plan->qual = qpqual; + plan->lefttree = subplan; + plan->righttree = NULL; + node->num_workers = nworkers; + + return node; +} + static IndexScan * make_indexscan(List *qptlist, List *qpqual, diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c index 3c81697..9ea4007 100644 --- a/src/backend/optimizer/plan/setrefs.c +++ b/src/backend/optimizer/plan/setrefs.c @@ -470,6 +470,25 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset) fix_scan_expr(root, (Node *) splan->tablesample, rtoffset); } break; + case T_Gather: + { + Gather *splan = (Gather *) plan; + + /* + * target list for leftree of gather plan should be same as + * for gather scan as both nodes need to produce same + * projection. We don't want to do this assignment after + * fixing references as that will be done separately for + * lefttree node. + */ + splan->plan.lefttree->targetlist = splan->plan.targetlist; + + splan->plan.targetlist = + fix_scan_list(root, splan->plan.targetlist, rtoffset); + splan->plan.qual = + fix_scan_list(root, splan->plan.qual, rtoffset); + } + break; case T_IndexScan: { IndexScan *splan = (IndexScan *) plan; diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c index d0bc412..78f3ce1 100644 --- a/src/backend/optimizer/plan/subselect.c +++ b/src/backend/optimizer/plan/subselect.c @@ -2243,6 +2243,10 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params, context.paramids = bms_add_members(context.paramids, scan_params); break; + case T_Gather: + context.paramids = bms_add_members(context.paramids, scan_params); + break; + case T_IndexScan: finalize_primnode((Node *) ((IndexScan *) plan)->indexqual, &context); diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index 4336ca1..e3f3539 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -732,6 +732,32 @@ create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer } /* + * create_gather_path + * + * Creates a path corresponding to a gather scan, returning the + * pathnode. + */ +GatherPath * +create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, + Relids required_outer, int nworkers) +{ + GatherPath *pathnode = makeNode(GatherPath); + + pathnode->path.pathtype = T_Gather; + pathnode->path.parent = rel; + pathnode->path.param_info = get_baserel_parampathinfo(root, rel, + required_outer); + pathnode->path.pathkeys = NIL; /* Gather has unordered result */ + + pathnode->subpath = subpath; + pathnode->num_workers = nworkers; + + cost_gather(pathnode, root, rel, pathnode->path.param_info); + + return pathnode; +} + +/* * create_index_path * Creates a path node for an index scan. * diff --git a/src/backend/tcop/dest.c b/src/backend/tcop/dest.c index d645751..bed1ef2 100644 --- a/src/backend/tcop/dest.c +++ b/src/backend/tcop/dest.c @@ -45,9 +45,10 @@ * dummy DestReceiver functions * ---------------- */ -static void +static bool donothingReceive(TupleTableSlot *slot, DestReceiver *self) { + return true; } static void diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c index 0df86a2..5eab231 100644 --- a/src/backend/tcop/pquery.c +++ b/src/backend/tcop/pquery.c @@ -1117,7 +1117,13 @@ RunFromStore(Portal portal, ScanDirection direction, long count, if (!ok) break; - (*dest->receiveSlot) (slot, dest); + /* + * If we are not able to send the tuple, we assume the destination + * has closed and no more tuples can be sent. If that's the case, + * end the loop. + */ + if (!((*dest->receiveSlot) (slot, dest))) + break; ExecClearTuple(slot); diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 17053af..4b03e9e 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -2535,6 +2535,16 @@ static struct config_int ConfigureNamesInt[] = }, { + {"max_parallel_degree", PGC_SUSET, RESOURCES_ASYNCHRONOUS, + gettext_noop("Sets the maximum number of simultaneously running backend worker processes."), + NULL + }, + &max_parallel_degree, + 0, 0, MAX_BACKENDS, + NULL, NULL, NULL + }, + + { {"autovacuum_work_mem", PGC_SIGHUP, RESOURCES_MEM, gettext_noop("Sets the maximum memory to be used by each autovacuum worker process."), NULL, @@ -2711,6 +2721,26 @@ static struct config_real ConfigureNamesReal[] = DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX, NULL, NULL, NULL }, + { + {"parallel_tuple_cost", PGC_USERSET, QUERY_TUNING_COST, + gettext_noop("Sets the planner's estimate of the cost of " + "passing each tuple (row) from worker to master backend."), + NULL + }, + ¶llel_tuple_cost, + DEFAULT_PARALLEL_TUPLE_COST, 0, DBL_MAX, + NULL, NULL, NULL + }, + { + {"parallel_setup_cost", PGC_USERSET, QUERY_TUNING_COST, + gettext_noop("Sets the planner's estimate of the cost of " + "setting up environment (shared memory) for parallelism."), + NULL + }, + ¶llel_setup_cost, + DEFAULT_PARALLEL_SETUP_COST, 0, DBL_MAX, + NULL, NULL, NULL + }, { {"cursor_tuple_fraction", PGC_USERSET, QUERY_TUNING_OTHER, diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 8c65287..e7f1250 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -164,6 +164,7 @@ #effective_io_concurrency = 1 # 1-1000; 0 disables prefetching #max_worker_processes = 8 +#max_parallel_degree = 0 # max number of worker backend subprocesses #------------------------------------------------------------------------------ @@ -290,6 +291,8 @@ #cpu_tuple_cost = 0.01 # same scale as above #cpu_index_tuple_cost = 0.005 # same scale as above #cpu_operator_cost = 0.0025 # same scale as above +#parallel_tuple_cost = 0.1 # same scale as above +#parallel_setup_cost = 1000.0 # same scale as above #effective_cache_size = 4GB # - Genetic Query Optimizer - diff --git a/src/include/access/printtup.h b/src/include/access/printtup.h index 46c4148..92ec882 100644 --- a/src/include/access/printtup.h +++ b/src/include/access/printtup.h @@ -25,11 +25,11 @@ extern void SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, extern void debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo); -extern void debugtup(TupleTableSlot *slot, DestReceiver *self); +extern bool debugtup(TupleTableSlot *slot, DestReceiver *self); /* XXX these are really in executor/spi.c */ extern void spi_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo); -extern void spi_printtup(TupleTableSlot *slot, DestReceiver *self); +extern bool spi_printtup(TupleTableSlot *slot, DestReceiver *self); #endif /* PRINTTUP_H */ diff --git a/src/include/executor/execParallel.h b/src/include/executor/execParallel.h index 4fc797a..fd1142f 100644 --- a/src/include/executor/execParallel.h +++ b/src/include/executor/execParallel.h @@ -13,21 +13,18 @@ #ifndef EXECPARALLEL_H #define EXECPARALLEL_H -#include "access/parallel.h" #include "nodes/execnodes.h" -#include "nodes/parsenodes.h" -#include "nodes/plannodes.h" typedef struct SharedExecutorInstrumentation SharedExecutorInstrumentation; typedef struct ParallelExecutorInfo { - PlanState *planstate; + PlanState *planstate; ParallelContext *pcxt; BufferUsage *buffer_usage; SharedExecutorInstrumentation *instrumentation; shm_mq_handle **tqueue; -} ParallelExecutorInfo; +} ParallelExecutorInfo; extern ParallelExecutorInfo *ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers); diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index 226f905..4f77692 100644 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -225,6 +225,7 @@ extern PlanState *ExecInitNode(Plan *node, EState *estate, int eflags); extern TupleTableSlot *ExecProcNode(PlanState *node); extern Node *MultiExecProcNode(PlanState *node); extern void ExecEndNode(PlanState *node); +extern bool ExecShutdownNode(PlanState *node); /* * prototypes from functions in execQual.c diff --git a/src/include/executor/nodeGather.h b/src/include/executor/nodeGather.h new file mode 100644 index 0000000..9e5d8fc --- /dev/null +++ b/src/include/executor/nodeGather.h @@ -0,0 +1,25 @@ +/*------------------------------------------------------------------------- + * + * nodeGather.h + * prototypes for nodeGather.c + * + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/executor/nodeGather.h + * + *------------------------------------------------------------------------- + */ +#ifndef NODEGATHER_H +#define NODEGATHER_H + +#include "nodes/execnodes.h" + +extern GatherState *ExecInitGather(Gather *node, EState *estate, int eflags); +extern TupleTableSlot *ExecGather(GatherState *node); +extern void ExecEndGather(GatherState *node); +extern void ExecShutdownGather(GatherState *node); +extern void ExecReScanGather(GatherState *node); + +#endif /* NODEGATHER_H */ diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 4ae2f3e..a823f29 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -16,7 +16,9 @@ #include "access/genam.h" #include "access/heapam.h" +#include "access/parallel.h" #include "executor/instrument.h" +#include "executor/tqueue.h" #include "lib/pairingheap.h" #include "nodes/params.h" #include "nodes/plannodes.h" @@ -1049,6 +1051,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 */ @@ -1221,6 +1230,29 @@ typedef struct BitmapOrState int nplans; /* number of input plans */ } BitmapOrState; +/* ---------------- + * GatherState extends PlanState by storing additional information + * related to parallel workers. + * + * pei parallel execution info for managing generic state information + * required for parallelism. + * funnel maintains the runtime information about queue's used to + * receive data from parallel workers. + * any_worker_launched indicates that workers are launched. + * all_workers_done indicates that all the data from workers has been received. + * local_scan_done indicates that local scan is compleleted. + * ---------------- + */ +typedef struct GatherState +{ + PlanState ps; /* its first field is NodeTag */ + struct ParallelExecutorInfo *pei; + TupleQueueFunnel *funnel; + bool any_worker_launched; + bool all_workers_done; + bool local_scan_done; +} GatherState; + /* ---------------------------------------------------------------- * Scan State Information * ---------------------------------------------------------------- diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 274480e..c014532 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_Gather, T_IndexScan, T_IndexOnlyScan, T_BitmapIndexScan, @@ -99,6 +100,7 @@ typedef enum NodeTag T_ScanState, T_SeqScanState, T_SampleScanState, + T_GatherState, T_IndexScanState, T_IndexOnlyScanState, T_BitmapIndexScanState, @@ -223,6 +225,7 @@ typedef enum NodeTag T_IndexOptInfo, T_ParamPathInfo, T_Path, + T_GatherPath, T_IndexPath, T_BitmapHeapPath, T_BitmapAndPath, diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h index 1e2d2bb..844e0f8 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -269,6 +269,16 @@ typedef struct BitmapOr List *bitmapplans; } BitmapOr; +/* ------------ + * Gather node + * ------------ + */ +typedef struct Gather +{ + Plan plan; + int num_workers; +} Gather; + /* * ========== * Scan nodes diff --git a/src/include/nodes/relation.h b/src/include/nodes/relation.h index 961b5d1..20192c9 100644 --- a/src/include/nodes/relation.h +++ b/src/include/nodes/relation.h @@ -763,6 +763,13 @@ typedef struct Path /* pathkeys is a List of PathKey nodes; see above */ } Path; +typedef struct GatherPath +{ + Path path; + Path *subpath; /* path for each worker */ + int num_workers; +} GatherPath; + /* Macro for extracting a path's parameterization relids; beware double eval */ #define PATH_REQ_OUTER(path) \ ((path)->param_info ? (path)->param_info->ppi_req_outer : (Relids) NULL) diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h index dd43e45..b36fbc1 100644 --- a/src/include/optimizer/cost.h +++ b/src/include/optimizer/cost.h @@ -26,6 +26,13 @@ #define DEFAULT_CPU_TUPLE_COST 0.01 #define DEFAULT_CPU_INDEX_TUPLE_COST 0.005 #define DEFAULT_CPU_OPERATOR_COST 0.0025 +#define DEFAULT_PARALLEL_TUPLE_COST 0.1 +/* + * XXX - We have kept reasonably high value for default parallel + * setup cost. In future we might want to change this value based + * on results. + */ +#define DEFAULT_PARALLEL_SETUP_COST 1000.0 #define DEFAULT_EFFECTIVE_CACHE_SIZE 524288 /* measured in pages */ @@ -48,8 +55,11 @@ extern PGDLLIMPORT double random_page_cost; extern PGDLLIMPORT double cpu_tuple_cost; extern PGDLLIMPORT double cpu_index_tuple_cost; extern PGDLLIMPORT double cpu_operator_cost; +extern PGDLLIMPORT double parallel_tuple_cost; +extern PGDLLIMPORT double parallel_setup_cost; extern PGDLLIMPORT int effective_cache_size; extern Cost disable_cost; +extern int max_parallel_degree; extern bool enable_seqscan; extern bool enable_indexscan; extern bool enable_indexonlyscan; @@ -70,6 +80,8 @@ 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_gather(GatherPath *path, PlannerInfo *root, + RelOptInfo *baserel, ParamPathInfo *param_info); 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 161644c..cc00ba5 100644 --- a/src/include/optimizer/pathnode.h +++ b/src/include/optimizer/pathnode.h @@ -34,6 +34,9 @@ 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 GatherPath *create_gather_path(PlannerInfo *root, + RelOptInfo *rel, Path *subpath, Relids required_outer, + int nworkers); extern IndexPath *create_index_path(PlannerInfo *root, IndexOptInfo *index, List *indexclauses, diff --git a/src/include/tcop/dest.h b/src/include/tcop/dest.h index b560672..91acd60 100644 --- a/src/include/tcop/dest.h +++ b/src/include/tcop/dest.h @@ -104,7 +104,9 @@ typedef enum * pointers that the executor must call. * * Note: the receiveSlot routine must be passed a slot containing a TupleDesc - * identical to the one given to the rStartup routine. + * identical to the one given to the rStartup routine. It returns bool where + * a "true" value means "continue processing" and a "false" value means + * "stop early, just as if we'd reached the end of the scan". * ---------------- */ typedef struct _DestReceiver DestReceiver; @@ -112,7 +114,7 @@ typedef struct _DestReceiver DestReceiver; struct _DestReceiver { /* Called for each tuple to be output: */ - void (*receiveSlot) (TupleTableSlot *slot, + bool (*receiveSlot) (TupleTableSlot *slot, DestReceiver *self); /* Per-executor-run initialization and shutdown: */ void (*rStartup) (DestReceiver *self, diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 0e149ea..feb821b 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -707,6 +707,9 @@ FunctionParameterMode FunctionScan FunctionScanPerFuncState FunctionScanState +Gather +GatherPath +GatherState FuzzyAttrMatchState GBT_NUMKEY GBT_NUMKEY_R @@ -1195,6 +1198,7 @@ OverrideSearchPath OverrideStackEntry PACE_HEADER PACL +ParallelExecutorInfo PATH PBOOL PCtxtHandle