Commit d1b7c1ff authored by Robert Haas's avatar Robert Haas

Parallel executor support.

This code provides infrastructure for a parallel leader to start up
parallel workers to execute subtrees of the plan tree being executed
in the master.  User-supplied parameters from ParamListInfo are passed
down, but PARAM_EXEC parameters are not.  Various other constructs,
such as initplans, subplans, and CTEs, are also not currently shared.
Nevertheless, there's enough here to support a basic implementation of
parallel query, and we can lift some of the current restrictions as
needed.

Amit Kapila and Robert Haas
parent 0557dc27
...@@ -13,7 +13,8 @@ top_builddir = ../../.. ...@@ -13,7 +13,8 @@ top_builddir = ../../..
include $(top_builddir)/src/Makefile.global include $(top_builddir)/src/Makefile.global
OBJS = execAmi.o execCurrent.o execGrouping.o execIndexing.o execJunk.o \ OBJS = execAmi.o execCurrent.o execGrouping.o execIndexing.o execJunk.o \
execMain.o execProcnode.o execQual.o execScan.o execTuples.o \ execMain.o execParallel.o execProcnode.o execQual.o \
execScan.o execTuples.o \
execUtils.o functions.o instrument.o nodeAppend.o nodeAgg.o \ execUtils.o functions.o instrument.o nodeAppend.o nodeAgg.o \
nodeBitmapAnd.o nodeBitmapOr.o \ nodeBitmapAnd.o nodeBitmapOr.o \
nodeBitmapHeapscan.o nodeBitmapIndexscan.o nodeCustom.o nodeHash.o \ nodeBitmapHeapscan.o nodeBitmapIndexscan.o nodeCustom.o nodeHash.o \
......
This diff is collapsed.
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
#include "executor/instrument.h" #include "executor/instrument.h"
BufferUsage pgBufferUsage; BufferUsage pgBufferUsage;
static BufferUsage save_pgBufferUsage;
static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add);
static void BufferUsageAccumDiff(BufferUsage *dst, static void BufferUsageAccumDiff(BufferUsage *dst,
const BufferUsage *add, const BufferUsage *sub); const BufferUsage *add, const BufferUsage *sub);
...@@ -47,6 +49,15 @@ InstrAlloc(int n, int instrument_options) ...@@ -47,6 +49,15 @@ InstrAlloc(int n, int instrument_options)
return instr; return instr;
} }
/* Initialize an pre-allocated instrumentation structure. */
void
InstrInit(Instrumentation *instr, int instrument_options)
{
memset(instr, 0, sizeof(Instrumentation));
instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0;
instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0;
}
/* Entry to a plan node */ /* Entry to a plan node */
void void
InstrStartNode(Instrumentation *instr) InstrStartNode(Instrumentation *instr)
...@@ -127,6 +138,73 @@ InstrEndLoop(Instrumentation *instr) ...@@ -127,6 +138,73 @@ InstrEndLoop(Instrumentation *instr)
instr->tuplecount = 0; instr->tuplecount = 0;
} }
/* aggregate instrumentation information */
void
InstrAggNode(Instrumentation *dst, Instrumentation *add)
{
if (!dst->running && add->running)
{
dst->running = true;
dst->firsttuple = add->firsttuple;
}
else if (dst->running && add->running && dst->firsttuple > add->firsttuple)
dst->firsttuple = add->firsttuple;
INSTR_TIME_ADD(dst->counter, add->counter);
dst->tuplecount += add->tuplecount;
dst->startup += add->startup;
dst->total += add->total;
dst->ntuples += add->ntuples;
dst->nloops += add->nloops;
dst->nfiltered1 += add->nfiltered1;
dst->nfiltered2 += add->nfiltered2;
/* Add delta of buffer usage since entry to node's totals */
if (dst->need_bufusage)
BufferUsageAdd(&dst->bufusage, &add->bufusage);
}
/* note current values during parallel executor startup */
void
InstrStartParallelQuery(void)
{
save_pgBufferUsage = pgBufferUsage;
}
/* report usage after parallel executor shutdown */
void
InstrEndParallelQuery(BufferUsage *result)
{
memset(result, 0, sizeof(BufferUsage));
BufferUsageAccumDiff(result, &pgBufferUsage, &save_pgBufferUsage);
}
/* accumulate work done by workers in leader's stats */
void
InstrAccumParallelQuery(BufferUsage *result)
{
BufferUsageAdd(&pgBufferUsage, result);
}
/* dst += add */
static void
BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
{
dst->shared_blks_hit += add->shared_blks_hit;
dst->shared_blks_read += add->shared_blks_read;
dst->shared_blks_dirtied += add->shared_blks_dirtied;
dst->shared_blks_written += add->shared_blks_written;
dst->local_blks_hit += add->local_blks_hit;
dst->local_blks_read += add->local_blks_read;
dst->local_blks_dirtied += add->local_blks_dirtied;
dst->local_blks_written += add->local_blks_written;
dst->temp_blks_read += add->temp_blks_read;
dst->temp_blks_written += add->temp_blks_written;
INSTR_TIME_ADD(dst->blk_read_time, add->blk_read_time);
INSTR_TIME_ADD(dst->blk_write_time, add->blk_write_time);
}
/* dst += add - sub */ /* dst += add - sub */
static void static void
BufferUsageAccumDiff(BufferUsage *dst, BufferUsageAccumDiff(BufferUsage *dst,
......
...@@ -66,7 +66,9 @@ tqueueStartupReceiver(DestReceiver *self, int operation, TupleDesc typeinfo) ...@@ -66,7 +66,9 @@ tqueueStartupReceiver(DestReceiver *self, int operation, TupleDesc typeinfo)
static void static void
tqueueShutdownReceiver(DestReceiver *self) tqueueShutdownReceiver(DestReceiver *self)
{ {
/* do nothing */ TQueueDestReceiver *tqueue = (TQueueDestReceiver *) self;
shm_mq_detach(shm_mq_get_queue(tqueue->handle));
} }
/* /*
......
...@@ -112,6 +112,7 @@ CopyPlanFields(const Plan *from, Plan *newnode) ...@@ -112,6 +112,7 @@ CopyPlanFields(const Plan *from, Plan *newnode)
COPY_SCALAR_FIELD(total_cost); COPY_SCALAR_FIELD(total_cost);
COPY_SCALAR_FIELD(plan_rows); COPY_SCALAR_FIELD(plan_rows);
COPY_SCALAR_FIELD(plan_width); COPY_SCALAR_FIELD(plan_width);
COPY_SCALAR_FIELD(plan_node_id);
COPY_NODE_FIELD(targetlist); COPY_NODE_FIELD(targetlist);
COPY_NODE_FIELD(qual); COPY_NODE_FIELD(qual);
COPY_NODE_FIELD(lefttree); COPY_NODE_FIELD(lefttree);
......
...@@ -271,6 +271,7 @@ _outPlanInfo(StringInfo str, const Plan *node) ...@@ -271,6 +271,7 @@ _outPlanInfo(StringInfo str, const Plan *node)
WRITE_FLOAT_FIELD(total_cost, "%.2f"); WRITE_FLOAT_FIELD(total_cost, "%.2f");
WRITE_FLOAT_FIELD(plan_rows, "%.0f"); WRITE_FLOAT_FIELD(plan_rows, "%.0f");
WRITE_INT_FIELD(plan_width); WRITE_INT_FIELD(plan_width);
WRITE_INT_FIELD(plan_node_id);
WRITE_NODE_FIELD(targetlist); WRITE_NODE_FIELD(targetlist);
WRITE_NODE_FIELD(qual); WRITE_NODE_FIELD(qual);
WRITE_NODE_FIELD(lefttree); WRITE_NODE_FIELD(lefttree);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "postgres.h" #include "postgres.h"
#include "nodes/params.h" #include "nodes/params.h"
#include "storage/shmem.h"
#include "utils/datum.h" #include "utils/datum.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
...@@ -73,3 +74,157 @@ copyParamList(ParamListInfo from) ...@@ -73,3 +74,157 @@ copyParamList(ParamListInfo from)
return retval; return retval;
} }
/*
* Estimate the amount of space required to serialize a ParamListInfo.
*/
Size
EstimateParamListSpace(ParamListInfo paramLI)
{
int i;
Size sz = sizeof(int);
if (paramLI == NULL || paramLI->numParams <= 0)
return sz;
for (i = 0; i < paramLI->numParams; i++)
{
ParamExternData *prm = &paramLI->params[i];
int16 typLen;
bool typByVal;
/* give hook a chance in case parameter is dynamic */
if (!OidIsValid(prm->ptype) && paramLI->paramFetch != NULL)
(*paramLI->paramFetch) (paramLI, i + 1);
sz = add_size(sz, sizeof(Oid)); /* space for type OID */
sz = add_size(sz, sizeof(uint16)); /* space for pflags */
/* space for datum/isnull */
if (OidIsValid(prm->ptype))
get_typlenbyval(prm->ptype, &typLen, &typByVal);
else
{
/* If no type OID, assume by-value, like copyParamList does. */
typLen = sizeof(Datum);
typByVal = true;
}
sz = add_size(sz,
datumEstimateSpace(prm->value, prm->isnull, typByVal, typLen));
}
return sz;
}
/*
* Serialize a paramListInfo structure into caller-provided storage.
*
* We write the number of parameters first, as a 4-byte integer, and then
* write details for each parameter in turn. The details for each parameter
* consist of a 4-byte type OID, 2 bytes of flags, and then the datum as
* serialized by datumSerialize(). The caller is responsible for ensuring
* that there is enough storage to store the number of bytes that will be
* written; use EstimateParamListSpace to find out how many will be needed.
* *start_address is updated to point to the byte immediately following those
* written.
*
* RestoreParamList can be used to recreate a ParamListInfo based on the
* serialized representation; this will be a static, self-contained copy
* just as copyParamList would create.
*/
void
SerializeParamList(ParamListInfo paramLI, char **start_address)
{
int nparams;
int i;
/* Write number of parameters. */
if (paramLI == NULL || paramLI->numParams <= 0)
nparams = 0;
else
nparams = paramLI->numParams;
memcpy(*start_address, &nparams, sizeof(int));
*start_address += sizeof(int);
/* Write each parameter in turn. */
for (i = 0; i < nparams; i++)
{
ParamExternData *prm = &paramLI->params[i];
int16 typLen;
bool typByVal;
/* give hook a chance in case parameter is dynamic */
if (!OidIsValid(prm->ptype) && paramLI->paramFetch != NULL)
(*paramLI->paramFetch) (paramLI, i + 1);
/* Write type OID. */
memcpy(*start_address, &prm->ptype, sizeof(Oid));
*start_address += sizeof(Oid);
/* Write flags. */
memcpy(*start_address, &prm->pflags, sizeof(uint16));
*start_address += sizeof(uint16);
/* Write datum/isnull. */
if (OidIsValid(prm->ptype))
get_typlenbyval(prm->ptype, &typLen, &typByVal);
else
{
/* If no type OID, assume by-value, like copyParamList does. */
typLen = sizeof(Datum);
typByVal = true;
}
datumSerialize(prm->value, prm->isnull, typByVal, typLen,
start_address);
}
}
/*
* Copy a ParamListInfo structure.
*
* The result is allocated in CurrentMemoryContext.
*
* Note: the intent of this function is to make a static, self-contained
* set of parameter values. If dynamic parameter hooks are present, we
* intentionally do not copy them into the result. Rather, we forcibly
* instantiate all available parameter values and copy the datum values.
*/
ParamListInfo
RestoreParamList(char **start_address)
{
ParamListInfo paramLI;
Size size;
int i;
int nparams;
memcpy(&nparams, *start_address, sizeof(int));
*start_address += sizeof(int);
size = offsetof(ParamListInfoData, params) +
nparams * sizeof(ParamExternData);
paramLI = (ParamListInfo) palloc(size);
paramLI->paramFetch = NULL;
paramLI->paramFetchArg = NULL;
paramLI->parserSetup = NULL;
paramLI->parserSetupArg = NULL;
paramLI->numParams = nparams;
for (i = 0; i < nparams; i++)
{
ParamExternData *prm = &paramLI->params[i];
/* Read type OID. */
memcpy(&prm->ptype, *start_address, sizeof(Oid));
*start_address += sizeof(Oid);
/* Read flags. */
memcpy(&prm->pflags, *start_address, sizeof(uint16));
*start_address += sizeof(uint16);
/* Read datum/isnull. */
prm->value = datumRestore(start_address, &prm->isnull);
}
return paramLI;
}
...@@ -1413,6 +1413,7 @@ ReadCommonPlan(Plan *local_node) ...@@ -1413,6 +1413,7 @@ ReadCommonPlan(Plan *local_node)
READ_FLOAT_FIELD(total_cost); READ_FLOAT_FIELD(total_cost);
READ_FLOAT_FIELD(plan_rows); READ_FLOAT_FIELD(plan_rows);
READ_INT_FIELD(plan_width); READ_INT_FIELD(plan_width);
READ_INT_FIELD(plan_node_id);
READ_NODE_FIELD(targetlist); READ_NODE_FIELD(targetlist);
READ_NODE_FIELD(qual); READ_NODE_FIELD(qual);
READ_NODE_FIELD(lefttree); READ_NODE_FIELD(lefttree);
......
...@@ -196,6 +196,7 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams) ...@@ -196,6 +196,7 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
glob->nParamExec = 0; glob->nParamExec = 0;
glob->lastPHId = 0; glob->lastPHId = 0;
glob->lastRowMarkId = 0; glob->lastRowMarkId = 0;
glob->lastPlanNodeId = 0;
glob->transientPlan = false; glob->transientPlan = false;
glob->hasRowSecurity = false; glob->hasRowSecurity = false;
......
...@@ -174,6 +174,8 @@ static bool extract_query_dependencies_walker(Node *node, ...@@ -174,6 +174,8 @@ static bool extract_query_dependencies_walker(Node *node,
* Currently, relations and user-defined functions are the only types of * Currently, relations and user-defined functions are the only types of
* objects that are explicitly tracked this way. * objects that are explicitly tracked this way.
* *
* 7. We assign every plan node in the tree a unique ID.
*
* We also perform one final optimization step, which is to delete * We also perform one final optimization step, which is to delete
* SubqueryScan plan nodes that aren't doing anything useful (ie, have * SubqueryScan plan nodes that aren't doing anything useful (ie, have
* no qual and a no-op targetlist). The reason for doing this last is that * no qual and a no-op targetlist). The reason for doing this last is that
...@@ -436,6 +438,9 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset) ...@@ -436,6 +438,9 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
if (plan == NULL) if (plan == NULL)
return NULL; return NULL;
/* Assign this node a unique ID. */
plan->plan_node_id = root->glob->lastPlanNodeId++;
/* /*
* Plan-type-specific fixes * Plan-type-specific fixes
*/ */
......
...@@ -246,3 +246,121 @@ datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen) ...@@ -246,3 +246,121 @@ datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
} }
return res; return res;
} }
/*-------------------------------------------------------------------------
* datumEstimateSpace
*
* Compute the amount of space that datumSerialize will require for a
* particular Datum.
*-------------------------------------------------------------------------
*/
Size
datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
{
Size sz = sizeof(int);
if (!isnull)
{
/* no need to use add_size, can't overflow */
if (typByVal)
sz += sizeof(Datum);
else
sz += datumGetSize(value, typByVal, typLen);
}
return sz;
}
/*-------------------------------------------------------------------------
* datumSerialize
*
* Serialize a possibly-NULL datum into caller-provided storage.
*
* The format is as follows: first, we write a 4-byte header word, which
* is either the length of a pass-by-reference datum, -1 for a
* pass-by-value datum, or -2 for a NULL. If the value is NULL, nothing
* further is written. If it is pass-by-value, sizeof(Datum) bytes
* follow. Otherwise, the number of bytes indicated by the header word
* follow. The caller is responsible for ensuring that there is enough
* storage to store the number of bytes that will be written; use
* datumEstimateSpace() to find out how many will be needed.
* *start_address is updated to point to the byte immediately following
* those written.
*-------------------------------------------------------------------------
*/
void
datumSerialize(Datum value, bool isnull, bool typByVal, int typLen,
char **start_address)
{
int header;
/* Write header word. */
if (isnull)
header = -2;
else if (typByVal)
header = -1;
else
header = datumGetSize(value, typByVal, typLen);
memcpy(*start_address, &header, sizeof(int));
*start_address += sizeof(int);
/* If not null, write payload bytes. */
if (!isnull)
{
if (typByVal)
{
memcpy(*start_address, &value, sizeof(Datum));
*start_address += sizeof(Datum);
}
else
{
memcpy(*start_address, DatumGetPointer(value), header);
*start_address += header;
}
}
}
/*-------------------------------------------------------------------------
* datumRestore
*
* Restore a possibly-NULL datum previously serialized by datumSerialize.
* *start_address is updated according to the number of bytes consumed.
*-------------------------------------------------------------------------
*/
Datum
datumRestore(char **start_address, bool *isnull)
{
int header;
void *d;
/* Read header word. */
memcpy(&header, *start_address, sizeof(int));
*start_address += sizeof(int);
/* If this datum is NULL, we can stop here. */
if (header == -2)
{
*isnull = true;
return (Datum) 0;
}
/* OK, datum is not null. */
*isnull = false;
/* If this datum is pass-by-value, sizeof(Datum) bytes follow. */
if (header == -1)
{
Datum val;
memcpy(&val, *start_address, sizeof(Datum));
*start_address += sizeof(Datum);
return val;
}
/* Pass-by-reference case; copy indicated number of bytes. */
Assert(header > 0);
d = palloc(header);
memcpy(d, *start_address, header);
*start_address += header;
return PointerGetDatum(d);
}
/*--------------------------------------------------------------------
* execParallel.h
* POSTGRES parallel execution interface
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/include/executor/execParallel.h
*--------------------------------------------------------------------
*/
#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;
ParallelContext *pcxt;
BufferUsage *buffer_usage;
SharedExecutorInstrumentation *instrumentation;
shm_mq_handle **tqueue;
} ParallelExecutorInfo;
extern ParallelExecutorInfo *ExecInitParallelPlan(PlanState *planstate,
EState *estate, int nworkers);
extern void ExecParallelFinish(ParallelExecutorInfo *pei);
#endif /* EXECPARALLEL_H */
...@@ -66,8 +66,13 @@ typedef struct Instrumentation ...@@ -66,8 +66,13 @@ typedef struct Instrumentation
extern PGDLLIMPORT BufferUsage pgBufferUsage; extern PGDLLIMPORT BufferUsage pgBufferUsage;
extern Instrumentation *InstrAlloc(int n, int instrument_options); extern Instrumentation *InstrAlloc(int n, int instrument_options);
extern void InstrInit(Instrumentation *instr, int instrument_options);
extern void InstrStartNode(Instrumentation *instr); extern void InstrStartNode(Instrumentation *instr);
extern void InstrStopNode(Instrumentation *instr, double nTuples); extern void InstrStopNode(Instrumentation *instr, double nTuples);
extern void InstrEndLoop(Instrumentation *instr); extern void InstrEndLoop(Instrumentation *instr);
extern void InstrAggNode(Instrumentation *dst, Instrumentation *add);
extern void InstrStartParallelQuery(void);
extern void InstrEndParallelQuery(BufferUsage *result);
extern void InstrAccumParallelQuery(BufferUsage *result);
#endif /* INSTRUMENT_H */ #endif /* INSTRUMENT_H */
...@@ -102,5 +102,8 @@ typedef struct ParamExecData ...@@ -102,5 +102,8 @@ typedef struct ParamExecData
/* Functions found in src/backend/nodes/params.c */ /* Functions found in src/backend/nodes/params.c */
extern ParamListInfo copyParamList(ParamListInfo from); extern ParamListInfo copyParamList(ParamListInfo from);
extern Size EstimateParamListSpace(ParamListInfo paramLI);
extern void SerializeParamList(ParamListInfo paramLI, char **start_address);
extern ParamListInfo RestoreParamList(char **start_address);
#endif /* PARAMS_H */ #endif /* PARAMS_H */
...@@ -111,6 +111,7 @@ typedef struct Plan ...@@ -111,6 +111,7 @@ typedef struct Plan
/* /*
* Common structural data for all Plan types. * Common structural data for all Plan types.
*/ */
int plan_node_id; /* unique across entire final plan tree */
List *targetlist; /* target list to be computed at this node */ List *targetlist; /* target list to be computed at this node */
List *qual; /* implicitly-ANDed qual conditions */ List *qual; /* implicitly-ANDed qual conditions */
struct Plan *lefttree; /* input plan tree(s) */ struct Plan *lefttree; /* input plan tree(s) */
......
...@@ -99,6 +99,8 @@ typedef struct PlannerGlobal ...@@ -99,6 +99,8 @@ typedef struct PlannerGlobal
Index lastRowMarkId; /* highest PlanRowMark ID assigned */ Index lastRowMarkId; /* highest PlanRowMark ID assigned */
int lastPlanNodeId; /* highest plan node ID assigned */
bool transientPlan; /* redo plan when TransactionXmin changes? */ bool transientPlan; /* redo plan when TransactionXmin changes? */
bool hasRowSecurity; /* row security applied? */ bool hasRowSecurity; /* row security applied? */
......
...@@ -46,4 +46,14 @@ extern Datum datumTransfer(Datum value, bool typByVal, int typLen); ...@@ -46,4 +46,14 @@ extern Datum datumTransfer(Datum value, bool typByVal, int typLen);
extern bool datumIsEqual(Datum value1, Datum value2, extern bool datumIsEqual(Datum value1, Datum value2,
bool typByVal, int typLen); bool typByVal, int typLen);
/*
* Serialize and restore datums so that we can transfer them to parallel
* workers.
*/
extern Size datumEstimateSpace(Datum value, bool isnull, bool typByVal,
int typLen);
extern void datumSerialize(Datum value, bool isnull, bool typByVal,
int typLen, char **start_address);
extern Datum datumRestore(char **start_address, bool *isnull);
#endif /* DATUM_H */ #endif /* DATUM_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment