diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c index c83991c93c..92b74077e9 100644 --- a/src/backend/executor/execPartition.c +++ b/src/backend/executor/execPartition.c @@ -50,7 +50,7 @@ static char *ExecBuildSlotPartitionKeyDescription(Relation rel, static List *adjust_partition_tlist(List *tlist, TupleConversionMap *map); static void find_subplans_for_params_recurse(PartitionPruneState *prunestate, PartitionPruningData *pprune, - bool allparams, + bool initial_prune, Bitmapset **validsubplans); @@ -1345,18 +1345,16 @@ adjust_partition_tlist(List *tlist, TupleConversionMap *map) * pruning to be performed for values which are only determined during * execution, we must make an additional pruning attempt during execution. * - * Here we support pruning using both external and exec Params. The main - * difference between these that we need to concern ourselves with is the - * time when the values of the Params are known. External Param values are - * known at any time of execution, including executor startup, but exec Param - * values are only known when the executor is running. + * Here we support pruning using any non-volatile expression. This allows + * pruning to be performed using both external and exec Params, along with + * stable functions calls and expressions. Any expression containing an exec + * Param must not be evaluated until during execution. For everything else, we + * can perform pruning during executor startup. * - * For external Params we may be able to prune away unneeded partitions - * during executor startup. This has the added benefit of not having to - * initialize the unneeded subnodes at all. This is useful as it can save - * quite a bit of effort during executor startup. + * Having the ability to prune away unneeded subnodes during executor startup + * has the added benefit of not having to initialize the unneeded subnodes at + * all. * - * For exec Params, we must delay pruning until the executor is running. * * Functions: * @@ -1369,19 +1367,20 @@ adjust_partition_tlist(List *tlist, TupleConversionMap *map) * planner's partition prune function into subnode indexes. * * ExecFindInitialMatchingSubPlans: - * Returns indexes of matching subnodes utilizing only external Params - * to eliminate subnodes. The function must only be called during - * executor startup for the given node before the subnodes themselves - * are initialized. Subnodes which are found not to match by this - * function must not be included in the node's list of subnodes as this - * function performs a remap of the partition index to subplan index map - * and the newly created map provides indexes only for subnodes which - * remain after calling this function. + * Returns indexes of matching subnodes. Partition pruning is attempted + * without any evaluation of expressions containing exec Params. This + * function must only be called during executor startup for the given + * node before the subnodes themselves are initialized. Subnodes which + * are found not to match by this function must not be included in the + * node's list of subnodes as this function performs a remap of the + * partition index to subplan index map and the newly created map + * provides indexes only for subnodes which remain after calling this + * function. * * ExecFindMatchingSubPlans: - * Returns indexes of matching subnodes utilizing all Params to eliminate - * subnodes which can't possibly contain matching tuples. This function - * can only be called while the executor is running. + * Returns indexes of matching subnodes evaluating all possible + * expressions. This function can only be called while the executor is + * running. *------------------------------------------------------------------------- */ @@ -1416,8 +1415,8 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo) */ prunestate->partprunedata = prunedata; prunestate->num_partprunedata = list_length(partitionpruneinfo); - prunestate->extparams = NULL; prunestate->execparams = NULL; + prunestate->do_initial_prune = false; /* may be set below */ /* * Create a sub memory context which we'll use when making calls to the @@ -1444,7 +1443,7 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo) int partnatts; int n_steps; - pprune->present_parts = bms_copy(pinfo->present_parts); + pprune->present_parts = pinfo->present_parts; pprune->subnode_map = palloc(sizeof(int) * pinfo->nparts); /* @@ -1476,9 +1475,14 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo) context->nparts = pinfo->nparts; context->boundinfo = partition_bounds_copy(partdesc->boundinfo, partkey); context->planstate = planstate; - context->safeparams = NULL; /* empty for now */ context->exprstates = palloc0(sizeof(ExprState *) * n_steps * partnatts); + /* + * Use the hasexecparam. This is not modified anywhere, so we just + * borrow the planner's copy. + */ + context->exprhasexecparam = pinfo->hasexecparam; + /* Initialize expression states for each expression */ foreach(lc2, pinfo->pruning_steps) { @@ -1511,31 +1515,25 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo) } pprune->pruning_steps = pinfo->pruning_steps; - pprune->extparams = bms_copy(pinfo->extparams); - pprune->allparams = bms_union(pinfo->extparams, pinfo->execparams); + pprune->execparams = bms_copy(pinfo->execparams); + pprune->do_initial_prune = pinfo->do_initial_prune; + pprune->do_exec_prune = pinfo->do_exec_prune; /* - * Accumulate the paramids which match the partitioned keys of all - * partitioned tables. + * Accumulate the exec paramids which match the partitioned keys of + * all partitioned tables. */ - prunestate->extparams = bms_add_members(prunestate->extparams, - pinfo->extparams); - prunestate->execparams = bms_add_members(prunestate->execparams, pinfo->execparams); + /* Record if an initial prune would be useful at any level */ + prunestate->do_initial_prune |= pinfo->do_initial_prune; + relation_close(rel, NoLock); i++; } - /* - * Cache the union of the paramids of both types. This saves having to - * recalculate it everytime we need to know what they are. - */ - prunestate->allparams = bms_union(prunestate->extparams, - prunestate->execparams); - return prunestate; } @@ -1543,9 +1541,9 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo) * ExecFindInitialMatchingSubPlans * Determine which subset of subplan nodes we need to initialize based * on the details stored in 'prunestate'. Here we only determine the - * matching partitions using values known during plan startup, which is - * only external Params. Exec Params will be unknown at this time. We - * must delay pruning using exec Params until the actual executor run. + * matching partitions using values known during plan startup, which + * excludes attempting to evaulate any expressions containing exec + * Params. * * It is expected that callers of this function do so only once during their * init plan. The caller must only initialize the subnodes which are returned @@ -1554,8 +1552,6 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo) * return its matching subnode indexes assuming that the caller discarded * the original non-matching subnodes. * - * This function must only be called if 'prunestate' has any extparams. - * * 'nsubnodes' must be passed as the total number of unpruned subnodes. */ Bitmapset * @@ -1565,11 +1561,7 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes) MemoryContext oldcontext; Bitmapset *result = NULL; - /* - * Ensure there's actually external params, or we've not been called - * already. - */ - Assert(!bms_is_empty(prunestate->extparams)); + Assert(prunestate->do_initial_prune); pprune = prunestate->partprunedata; @@ -1579,8 +1571,8 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes) */ oldcontext = MemoryContextSwitchTo(prunestate->prune_context); - /* Determine which subnodes match the external params */ - find_subplans_for_params_recurse(prunestate, pprune, false, &result); + /* Perform pruning without using exec params */ + find_subplans_for_params_recurse(prunestate, pprune, true, &result); MemoryContextSwitchTo(oldcontext); @@ -1589,18 +1581,6 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes) MemoryContextReset(prunestate->prune_context); - /* - * Record that partition pruning has been performed for external params. - * These are not required again afterwards, and nullifying them helps - * ensure nothing accidentally calls this function twice on the same - * PartitionPruneState. - * - * (Note we keep prunestate->allparams, because we do use that one - * repeatedly in ExecFindMatchingSubPlans). - */ - bms_free(prunestate->extparams); - prunestate->extparams = NULL; - /* * If any subnodes were pruned, we must re-sequence the subnode indexes so * that ExecFindMatchingSubPlans properly returns the indexes from the @@ -1644,7 +1624,6 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes) * for. It seems easier to build a fresh one, rather than trying * to update the existing one. */ - bms_free(pprune->present_parts); pprune->present_parts = NULL; for (j = 0; j < nparts; j++) @@ -1669,6 +1648,41 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes) } } + /* + * Now we must determine which sub-partitioned tables still have + * unpruned partitions. The easiest way to do this is to simply loop + * over each PartitionPruningData again checking if there are any + * 'present_parts' in the sub-partitioned table. We needn't bother + * doing this if there are no sub-partitioned tables. + */ + if (prunestate->num_partprunedata > 1) + { + for (i = 0; i < prunestate->num_partprunedata; i++) + { + int nparts; + int j; + + pprune = &prunestate->partprunedata[i]; + nparts = pprune->context.nparts; + + for (j = 0; j < nparts; j++) + { + int subidx = pprune->subpart_map[j]; + + if (subidx >= 0) + { + PartitionPruningData *subprune; + + subprune = &prunestate->partprunedata[subidx]; + + if (!bms_is_empty(subprune->present_parts)) + pprune->present_parts = + bms_add_member(pprune->present_parts, j); + } + } + } + } + pfree(new_subnode_indexes); } @@ -1697,7 +1711,7 @@ ExecFindMatchingSubPlans(PartitionPruneState *prunestate) */ oldcontext = MemoryContextSwitchTo(prunestate->prune_context); - find_subplans_for_params_recurse(prunestate, pprune, true, &result); + find_subplans_for_params_recurse(prunestate, pprune, false, &result); MemoryContextSwitchTo(oldcontext); @@ -1717,43 +1731,34 @@ ExecFindMatchingSubPlans(PartitionPruneState *prunestate) static void find_subplans_for_params_recurse(PartitionPruneState *prunestate, PartitionPruningData *pprune, - bool allparams, + bool initial_prune, Bitmapset **validsubplans) { PartitionPruneContext *context = &pprune->context; Bitmapset *partset; - Bitmapset *pruneparams; int i; /* Guard against stack overflow due to overly deep partition hierarchy. */ check_stack_depth(); - /* - * Use only external params unless we've been asked to also use exec - * params too. - */ - if (allparams) - pruneparams = pprune->allparams; - else - pruneparams = pprune->extparams; - - /* - * We only need to determine the matching partitions if there are any - * params matching the partition key at this level. If there are no - * matching params, then we can simply return all subnodes which belong to - * this parent partition. The planner should have already determined - * these to be the minimum possible set. We must still recursively visit - * any subpartitioned tables as we may find their partition keys match - * some Params at their level. - */ - if (!bms_is_empty(pruneparams)) + /* Only prune if pruning would be useful at this level. */ + if ((initial_prune && pprune->do_initial_prune) || + (!initial_prune && pprune->do_exec_prune)) { - context->safeparams = pruneparams; + /* Set whether we can evaluate exec params or not */ + context->evalexecparams = !initial_prune; + partset = get_matching_partitions(context, pprune->pruning_steps); } else + { + /* + * If no pruning is to be done, just include all partitions at this + * level. + */ partset = pprune->present_parts; + } /* Translate partset into subnode indexes */ i = -1; @@ -1769,7 +1774,7 @@ find_subplans_for_params_recurse(PartitionPruneState *prunestate, if (partidx != -1) find_subplans_for_params_recurse(prunestate, &prunestate->partprunedata[partidx], - allparams, validsubplans); + initial_prune, validsubplans); else { /* diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c index 6bc3e470bf..707a3e0e4b 100644 --- a/src/backend/executor/nodeAppend.c +++ b/src/backend/executor/nodeAppend.c @@ -138,11 +138,8 @@ ExecInitAppend(Append *node, EState *estate, int eflags) prunestate = ExecSetupPartitionPruneState(&appendstate->ps, node->part_prune_infos); - /* - * When there are external params matching the partition key we may be - * able to prune away Append subplans now. - */ - if (!bms_is_empty(prunestate->extparams)) + /* Perform an initial partition prune, if required. */ + if (prunestate->do_initial_prune) { /* Determine which subplans match the external params */ validsubplans = ExecFindInitialMatchingSubPlans(prunestate, diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 7c045a7afe..90c8d4a028 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -2175,9 +2175,12 @@ _copyPartitionPruneInfo(const PartitionPruneInfo *from) COPY_NODE_FIELD(pruning_steps); COPY_BITMAPSET_FIELD(present_parts); COPY_SCALAR_FIELD(nparts); + COPY_SCALAR_FIELD(nexprs); COPY_POINTER_FIELD(subnode_map, from->nparts * sizeof(int)); COPY_POINTER_FIELD(subpart_map, from->nparts * sizeof(int)); - COPY_BITMAPSET_FIELD(extparams); + COPY_POINTER_FIELD(hasexecparam, from->nexprs * sizeof(bool)); + COPY_SCALAR_FIELD(do_initial_prune); + COPY_SCALAR_FIELD(do_exec_prune); COPY_BITMAPSET_FIELD(execparams); return newnode; diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 610f9edaf5..9f6fb7c55f 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -1742,6 +1742,7 @@ _outPartitionPruneInfo(StringInfo str, const PartitionPruneInfo *node) WRITE_NODE_FIELD(pruning_steps); WRITE_BITMAPSET_FIELD(present_parts); WRITE_INT_FIELD(nparts); + WRITE_INT_FIELD(nexprs); appendStringInfoString(str, " :subnode_map"); for (i = 0; i < node->nparts; i++) @@ -1751,7 +1752,12 @@ _outPartitionPruneInfo(StringInfo str, const PartitionPruneInfo *node) for (i = 0; i < node->nparts; i++) appendStringInfo(str, " %d", node->subpart_map[i]); - WRITE_BITMAPSET_FIELD(extparams); + appendStringInfoString(str, " :hasexecparam"); + for (i = 0; i < node->nexprs; i++) + appendStringInfo(str, " %s", booltostr(node->hasexecparam[i])); + + WRITE_BOOL_FIELD(do_initial_prune); + WRITE_BOOL_FIELD(do_exec_prune); WRITE_BITMAPSET_FIELD(execparams); } diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 2826cec2f8..a0335ccd76 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -1363,9 +1363,12 @@ _readPartitionPruneInfo(void) READ_NODE_FIELD(pruning_steps); READ_BITMAPSET_FIELD(present_parts); READ_INT_FIELD(nparts); + READ_INT_FIELD(nexprs); READ_INT_ARRAY(subnode_map, local_node->nparts); READ_INT_ARRAY(subpart_map, local_node->nparts); - READ_BITMAPSET_FIELD(extparams); + READ_BOOL_ARRAY(hasexecparam, local_node->nexprs); + READ_BOOL_FIELD(do_initial_prune); + READ_BOOL_FIELD(do_exec_prune); READ_BITMAPSET_FIELD(execparams); READ_DONE(); diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c index 58ec2a684d..2561c6b835 100644 --- a/src/backend/partitioning/partprune.c +++ b/src/backend/partitioning/partprune.c @@ -53,10 +53,12 @@ #include "optimizer/planner.h" #include "optimizer/predtest.h" #include "optimizer/prep.h" +#include "optimizer/var.h" #include "partitioning/partprune.h" #include "partitioning/partbounds.h" #include "rewrite/rewriteManip.h" #include "utils/lsyscache.h" +#include "utils/rel.h" /* @@ -115,6 +117,14 @@ typedef struct PruneStepResult bool scan_null; /* Scan the partition for NULL values? */ } PruneStepResult; +/* + * expression_tree_walker context struct for gathering paramids of params + * matching the partition key. + */ +typedef struct PullParamContext +{ + Bitmapset *params; +} PullParamContext; static List *gen_partprune_steps(RelOptInfo *rel, List *clauses, bool *contradictory); @@ -162,7 +172,10 @@ static PruneStepResult *get_matching_list_bounds(PartitionPruneContext *context, static PruneStepResult *get_matching_range_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, Datum *values, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys); -static bool pull_partkey_params(PartitionPruneInfo *pinfo, List *steps); +static Bitmapset *pull_exec_paramids(Expr *expr); +static bool pull_exec_paramids_walker(Node *node, PullParamContext * context); +static bool analyze_partkey_exprs(PartitionPruneInfo *pinfo, List *steps, + int partnatts); static PruneStepResult *perform_pruning_base_step(PartitionPruneContext *context, PartitionPruneStepOp *opstep); static PruneStepResult *perform_pruning_combine_step(PartitionPruneContext *context, @@ -197,7 +210,7 @@ make_partition_pruneinfo(PlannerInfo *root, List *partition_rels, int *relid_subnode_map; int *relid_subpart_map; int i; - bool gotparam = false; + bool doruntimeprune = false; /* * Allocate two arrays to store the 1-based indexes of the 'subpaths' and @@ -238,6 +251,7 @@ make_partition_pruneinfo(PlannerInfo *root, List *partition_rels, RangeTblEntry *rte; Bitmapset *present_parts; int nparts = subpart->nparts; + int partnatts = subpart->part_scheme->partnatts; int *subnode_map; int *subpart_map; List *partprunequal; @@ -320,17 +334,13 @@ make_partition_pruneinfo(PlannerInfo *root, List *partition_rels, pinfo->pruning_steps = pruning_steps; pinfo->present_parts = present_parts; pinfo->nparts = nparts; - pinfo->extparams = NULL; + pinfo->nexprs = list_length(pruning_steps) * partnatts; pinfo->execparams = NULL; pinfo->subnode_map = subnode_map; pinfo->subpart_map = subpart_map; - /* - * Extract Params matching partition key and record if we got any. - * We'll not bother enabling run-time pruning if no params matched the - * partition key at any level of partitioning. - */ - gotparam |= pull_partkey_params(pinfo, pruning_steps); + /* Determine which pruning types should be enabled at this level */ + doruntimeprune |= analyze_partkey_exprs(pinfo, pruning_steps, partnatts); pinfolist = lappend(pinfolist, pinfo); } @@ -338,14 +348,10 @@ make_partition_pruneinfo(PlannerInfo *root, List *partition_rels, pfree(relid_subnode_map); pfree(relid_subpart_map); - if (gotparam) + if (doruntimeprune) return pinfolist; - /* - * If no Params were found to match the partition key on any of the - * partitioned relations then there's no point doing any run-time - * partition pruning. - */ + /* No run-time pruning required. */ return NIL; } @@ -444,9 +450,10 @@ prune_append_rel_partitions(RelOptInfo *rel) context.boundinfo = rel->boundinfo; /* Not valid when being called from the planner */ + context.evalexecparams = false; context.planstate = NULL; - context.safeparams = NULL; context.exprstates = NULL; + context.exprhasexecparam = NULL; /* Actual pruning happens here. */ partindexes = get_matching_partitions(&context, pruning_steps); @@ -1478,6 +1485,10 @@ match_clause_to_partition_key(RelOptInfo *rel, if (contain_volatile_functions((Node *) expr)) return PARTCLAUSE_UNSUPPORTED; + /* We can't prune using an expression with Vars. */ + if (contain_var_clause((Node *) expr)) + return PARTCLAUSE_UNSUPPORTED; + /* * Determine the input types of the operator we're considering. * @@ -1655,7 +1666,7 @@ match_clause_to_partition_key(RelOptInfo *rel, return PARTCLAUSE_UNSUPPORTED; } else - return PARTCLAUSE_UNSUPPORTED; /* no useful negator */ + return PARTCLAUSE_UNSUPPORTED; /* no useful negator */ } /* @@ -2683,54 +2694,96 @@ get_matching_range_bounds(PartitionPruneContext *context, } /* - * pull_partkey_params - * Loop through each pruning step and record each external and exec - * Params being compared to the partition keys. + * pull_exec_paramids + * Returns a Bitmapset containing the paramids of each Param with + * paramkind = PARAM_EXEC in 'expr'. + */ +static Bitmapset * +pull_exec_paramids(Expr *expr) +{ + PullParamContext context; + + context.params = NULL; + + pull_exec_paramids_walker((Node *) expr, &context); + + return context.params; +} + +static bool +pull_exec_paramids_walker(Node *node, PullParamContext * context) +{ + if (node == NULL) + return false; + if (IsA(node, Param)) + { + Param *param = (Param *) node; + + if (param->paramkind == PARAM_EXEC) + context->params = bms_add_member(context->params, param->paramid); + return false; + } + return expression_tree_walker(node, pull_exec_paramids_walker, + (void *) context); +} + +/* + * analyze_partkey_exprs + * Loop through each pruning steps recording which one are comparing exec + * params to the partition key. + * + * Returns true if run-time partition pruning should be attempted at this + * level. */ static bool -pull_partkey_params(PartitionPruneInfo *pinfo, List *steps) +analyze_partkey_exprs(PartitionPruneInfo *pinfo, List *steps, int partnatts) { ListCell *lc; - bool gotone = false; + bool doruntimeprune = false; + + pinfo->hasexecparam = palloc0(sizeof(bool) * pinfo->nexprs); + pinfo->do_initial_prune = false; + pinfo->do_exec_prune = false; foreach(lc, steps) { - PartitionPruneStepOp *stepop = lfirst(lc); + PartitionPruneStepOp *step = lfirst(lc); ListCell *lc2; + int keyno; - if (!IsA(stepop, PartitionPruneStepOp)) + if (!IsA(step, PartitionPruneStepOp)) continue; - foreach(lc2, stepop->exprs) + keyno = 0; + foreach(lc2, step->exprs) { Expr *expr = lfirst(lc2); - if (IsA(expr, Param)) + if (!IsA(expr, Const)) { - Param *param = (Param *) expr; - - switch (param->paramkind) - { - case PARAM_EXTERN: - pinfo->extparams = bms_add_member(pinfo->extparams, - param->paramid); - break; - case PARAM_EXEC: - pinfo->execparams = bms_add_member(pinfo->execparams, - param->paramid); - break; + Bitmapset *execparams = pull_exec_paramids(expr); + bool hasexecparams; + int stateidx = PruneCxtStateIdx(partnatts, + step->step.step_id, + keyno); + + hasexecparams = !bms_is_empty(execparams); + pinfo->hasexecparam[stateidx] = hasexecparams; + pinfo->execparams = bms_add_members(pinfo->execparams, + execparams); + + if (!hasexecparams) + pinfo->do_initial_prune = true; + else + pinfo->do_exec_prune = true; - default: - elog(ERROR, "unrecognized paramkind: %d", - (int) param->paramkind); - break; - } - gotone = true; + doruntimeprune = true; } + keyno++; } } - return gotone; + return doruntimeprune; } /* @@ -3031,37 +3084,40 @@ static bool partkey_datum_from_expr(PartitionPruneContext *context, Expr *expr, int stateidx, Datum *value) { - switch (nodeTag(expr)) + if (IsA(expr, Const)) { - case T_Const: - *value = ((Const *) expr)->constvalue; - return true; - - case T_Param: - - /* - * When being called from the executor we may be able to evaluate - * the Param's value. - */ - if (context->planstate && - bms_is_member(((Param *) expr)->paramid, context->safeparams)) - { - ExprState *exprstate; - ExprContext *ectx; - bool isNull; + *value = ((Const *) expr)->constvalue; + return true; + } + else + { + /* + * When called from the executor we'll have a valid planstate so we + * may be able to evaluate the expression which could not be folded to + * a Const during planning. Since run-time pruning can occur both + * during initialization of the executor or while it's running, we + * must be careful here only to attempt to evaluate expressions + * containing exec params when the executor is running. + */ + if (context->planstate && + (context->evalexecparams || + !context->exprhasexecparam[stateidx])) + { + ExprState *exprstate; + ExprContext *ectx; + bool isNull; - exprstate = context->exprstates[stateidx]; - ectx = context->planstate->ps_ExprContext; - *value = ExecEvalExprSwitchContext(exprstate, ectx, &isNull); - if (isNull) - return false; + /* Exprs with volatile functions shouldn't make it here */ + Assert(!contain_volatile_functions((Node *) expr)); - return true; - } - break; + exprstate = context->exprstates[stateidx]; + ectx = context->planstate->ps_ExprContext; + *value = ExecEvalExprSwitchContext(exprstate, ectx, &isNull); + if (isNull) + return false; - default: - break; + return true; + } } return false; diff --git a/src/include/executor/execPartition.h b/src/include/executor/execPartition.h index fc6e9574e3..ed940a8a36 100644 --- a/src/include/executor/execPartition.h +++ b/src/include/executor/execPartition.h @@ -133,9 +133,12 @@ typedef struct PartitionTupleRouting * the partition pruning code. * pruning_steps Contains a list of PartitionPruneStep used to * perform the actual pruning. - * extparams Contains paramids of external params found + * execparams Contains paramids of exec params found * matching partition keys in 'pruning_steps'. - * allparams As 'extparams' but also including exec params. + * do_initial_prune true if pruning should be performed during + * executor startup. + * do_exec_prune true if pruning should be performed during + * executor run. *----------------------- */ typedef struct PartitionPruningData @@ -145,8 +148,9 @@ typedef struct PartitionPruningData Bitmapset *present_parts; PartitionPruneContext context; List *pruning_steps; - Bitmapset *extparams; - Bitmapset *allparams; + Bitmapset *execparams; + bool do_initial_prune; + bool do_exec_prune; } PartitionPruningData; /*----------------------- @@ -163,24 +167,22 @@ typedef struct PartitionPruningData * partitioned relation. First element contains the * details for the target partitioned table. * num_partprunedata Number of items in 'partprunedata' array. + * do_initial_prune true if pruning should be performed during executor + * startup. * prune_context A memory context which can be used to call the query * planner's partition prune functions. - * extparams All PARAM_EXTERN paramids which were found to match a + * execparams All PARAM_EXEC paramids which were found to match a * partition key in each of the contained * PartitionPruningData structs. - * execparams As above but for PARAM_EXEC. - * allparams Union of 'extparams' and 'execparams', saved to avoid - * recalculation. *----------------------- */ typedef struct PartitionPruneState { PartitionPruningData *partprunedata; int num_partprunedata; + bool do_initial_prune; MemoryContext prune_context; - Bitmapset *extparams; Bitmapset *execparams; - Bitmapset *allparams; } PartitionPruneState; extern PartitionTupleRouting *ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index f90aa7b2a1..e4c366b58f 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1598,9 +1598,16 @@ typedef struct PartitionPruneInfo Bitmapset *present_parts; /* Indexes of all partitions which subnodes * are present for. */ int nparts; /* The length of the following two arrays */ + int nexprs; /* Size of hasexecparam array */ int *subnode_map; /* subnode index by partition id, or -1 */ int *subpart_map; /* subpart index by partition id, or -1 */ - Bitmapset *extparams; /* All external paramids seen in prunesteps */ + bool *hasexecparam; /* true if corresponding pruning_step has an + * exec Param in the Expr being compared to + * the partition key. */ + bool do_initial_prune; /* true if pruning should be performed + * during executor startup. */ + bool do_exec_prune; /* true if pruning should be performed during + * executor run. */ Bitmapset *execparams; /* All exec paramids seen in prunesteps */ } PartitionPruneInfo; diff --git a/src/include/partitioning/partprune.h b/src/include/partitioning/partprune.h index 3d114b4c71..028d4cecb6 100644 --- a/src/include/partitioning/partprune.h +++ b/src/include/partitioning/partprune.h @@ -36,6 +36,9 @@ typedef struct PartitionPruneContext /* Number of partitions */ int nparts; + /* true if it's safe to evaulate exec params */ + bool evalexecparams; + /* Partition boundary info */ PartitionBoundInfo boundinfo; @@ -45,18 +48,15 @@ typedef struct PartitionPruneContext */ PlanState *planstate; - /* - * Parameters that are safe to be used for partition pruning. execparams - * are not safe to use until the executor is running. - */ - Bitmapset *safeparams; - /* * Array of ExprStates, indexed as per PruneCtxStateIdx; one for each * partkey in each pruning step. Allocated if planstate is non-NULL, * otherwise NULL. */ ExprState **exprstates; + + /* true if corresponding 'exprstate' expression contains an exec param */ + bool *exprhasexecparam; } PartitionPruneContext; #define PruneCxtStateIdx(partnatts, step_id, keyno) \ diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out index cf331e79c1..64b4e933d6 100644 --- a/src/test/regress/expected/partition_prune.out +++ b/src/test/regress/expected/partition_prune.out @@ -1835,6 +1835,54 @@ fetch backward all from cur; (2 rows) commit; +begin; +-- Test run-time pruning using stable functions +create function list_part_fn(int) returns int as $$ begin return $1; end;$$ language plpgsql stable; +-- Ensure pruning works using a stable function containing no Vars +explain (analyze, costs off, summary off, timing off) select * from list_part where a = list_part_fn(1); + QUERY PLAN +------------------------------------------------------ + Append (actual rows=1 loops=1) + Subplans Removed: 3 + -> Seq Scan on list_part1 (actual rows=1 loops=1) + Filter: (a = list_part_fn(1)) +(4 rows) + +-- Ensure pruning does not take place when the function contains a Var parameter +explain (analyze, costs off, summary off, timing off) select * from list_part where a = list_part_fn(a); + QUERY PLAN +------------------------------------------------------ + Append (actual rows=4 loops=1) + -> Seq Scan on list_part1 (actual rows=1 loops=1) + Filter: (a = list_part_fn(a)) + -> Seq Scan on list_part2 (actual rows=1 loops=1) + Filter: (a = list_part_fn(a)) + -> Seq Scan on list_part3 (actual rows=1 loops=1) + Filter: (a = list_part_fn(a)) + -> Seq Scan on list_part4 (actual rows=1 loops=1) + Filter: (a = list_part_fn(a)) +(9 rows) + +-- Ensure pruning does not take place when the expression contains a Var. +explain (analyze, costs off, summary off, timing off) select * from list_part where a = list_part_fn(1) + a; + QUERY PLAN +------------------------------------------------------ + Append (actual rows=0 loops=1) + -> Seq Scan on list_part1 (actual rows=0 loops=1) + Filter: (a = (list_part_fn(1) + a)) + Rows Removed by Filter: 1 + -> Seq Scan on list_part2 (actual rows=0 loops=1) + Filter: (a = (list_part_fn(1) + a)) + Rows Removed by Filter: 1 + -> Seq Scan on list_part3 (actual rows=0 loops=1) + Filter: (a = (list_part_fn(1) + a)) + Rows Removed by Filter: 1 + -> Seq Scan on list_part4 (actual rows=0 loops=1) + Filter: (a = (list_part_fn(1) + a)) + Rows Removed by Filter: 1 +(13 rows) + +rollback; drop table list_part; -- Parallel append -- Suppress the number of loops each parallel node runs for. This is because @@ -2079,6 +2127,40 @@ select explain_parallel_append('select avg(ab.a) from ab inner join lprt_a a on Index Cond: (a = a.a) (27 rows) +-- Ensure the same partitions are pruned when we make the nested loop +-- parameter an Expr rather than a plain Param. +select explain_parallel_append('select avg(ab.a) from ab inner join lprt_a a on ab.a = a.a + 0 where a.a in(0, 0, 1)'); + explain_parallel_append +--------------------------------------------------------------------------------------------------- + Finalize Aggregate (actual rows=1 loops=1) + -> Gather (actual rows=2 loops=1) + Workers Planned: 1 + Workers Launched: 1 + -> Partial Aggregate (actual rows=1 loops=2) + -> Nested Loop (actual rows=0 loops=2) + -> Parallel Seq Scan on lprt_a a (actual rows=51 loops=N) + Filter: (a = ANY ('{0,0,1}'::integer[])) + -> Append (actual rows=0 loops=102) + -> Index Scan using ab_a1_b1_a_idx on ab_a1_b1 (actual rows=0 loops=2) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a1_b2_a_idx on ab_a1_b2 (actual rows=0 loops=2) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a1_b3_a_idx on ab_a1_b3 (actual rows=0 loops=2) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a2_b1_a_idx on ab_a2_b1 (never executed) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a2_b2_a_idx on ab_a2_b2 (never executed) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a2_b3_a_idx on ab_a2_b3 (never executed) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a3_b1_a_idx on ab_a3_b1 (never executed) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a3_b2_a_idx on ab_a3_b2 (never executed) + Index Cond: (a = (a.a + 0)) + -> Index Scan using ab_a3_b3_a_idx on ab_a3_b3 (never executed) + Index Cond: (a = (a.a + 0)) +(27 rows) + insert into lprt_a values(3),(3); select explain_parallel_append('select avg(ab.a) from ab inner join lprt_a a on ab.a = a.a where a.a in(1, 0, 3)'); explain_parallel_append diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sql index 1464f4dcd9..b6681fa44c 100644 --- a/src/test/regress/sql/partition_prune.sql +++ b/src/test/regress/sql/partition_prune.sql @@ -396,6 +396,22 @@ fetch backward all from cur; commit; +begin; + +-- Test run-time pruning using stable functions +create function list_part_fn(int) returns int as $$ begin return $1; end;$$ language plpgsql stable; + +-- Ensure pruning works using a stable function containing no Vars +explain (analyze, costs off, summary off, timing off) select * from list_part where a = list_part_fn(1); + +-- Ensure pruning does not take place when the function contains a Var parameter +explain (analyze, costs off, summary off, timing off) select * from list_part where a = list_part_fn(a); + +-- Ensure pruning does not take place when the expression contains a Var. +explain (analyze, costs off, summary off, timing off) select * from list_part where a = list_part_fn(1) + a; + +rollback; + drop table list_part; -- Parallel append @@ -486,6 +502,10 @@ set enable_mergejoin = 0; select explain_parallel_append('select avg(ab.a) from ab inner join lprt_a a on ab.a = a.a where a.a in(0, 0, 1)'); +-- Ensure the same partitions are pruned when we make the nested loop +-- parameter an Expr rather than a plain Param. +select explain_parallel_append('select avg(ab.a) from ab inner join lprt_a a on ab.a = a.a + 0 where a.a in(0, 0, 1)'); + insert into lprt_a values(3),(3); select explain_parallel_append('select avg(ab.a) from ab inner join lprt_a a on ab.a = a.a where a.a in(1, 0, 3)');