Commit 6630ccad authored by Tom Lane's avatar Tom Lane

Restructure creation of run-time pruning steps.

Previously, gen_partprune_steps() always built executor pruning steps
using all suitable clauses, including those containing PARAM_EXEC
Params.  This meant that the pruning steps were only completely safe
for executor run-time (scan start) pruning.  To prune at executor
startup, we had to ignore the steps involving exec Params.  But this
doesn't really work in general, since there may be logic changes
needed as well --- for example, pruning according to the last operator's
btree strategy is the wrong thing if we're not applying that operator.
The rules embodied in gen_partprune_steps() and its minions are
sufficiently complicated that tracking their incremental effects in
other logic seems quite impractical.

Short of a complete redesign, the only safe fix seems to be to run
gen_partprune_steps() twice, once to create executor startup pruning
steps and then again for run-time pruning steps.  We can save a few
cycles however by noting during the first scan whether we rejected
any clauses because they involved exec Params --- if not, we don't
need to do the second scan.

In support of this, refactor the internal APIs in partprune.c to make
more use of passing information in the GeneratePruningStepsContext
struct, rather than as separate arguments.

This is, I hope, the last piece of our response to a bug report from
Alan Jackson.  Back-patch to v11 where this code came in.

Discussion: https://postgr.es/m/FAD28A83-AC73-489E-A058-2681FA31D648@tvsquared.com
parent 05685897
...@@ -183,6 +183,11 @@ static char *ExecBuildSlotPartitionKeyDescription(Relation rel, ...@@ -183,6 +183,11 @@ static char *ExecBuildSlotPartitionKeyDescription(Relation rel,
bool *isnull, bool *isnull,
int maxfieldlen); int maxfieldlen);
static List *adjust_partition_tlist(List *tlist, TupleConversionMap *map); static List *adjust_partition_tlist(List *tlist, TupleConversionMap *map);
static void ExecInitPruningContext(PartitionPruneContext *context,
List *pruning_steps,
PartitionDesc partdesc,
PartitionKey partkey,
PlanState *planstate);
static void find_matching_subplans_recurse(PartitionPruningData *prunedata, static void find_matching_subplans_recurse(PartitionPruningData *prunedata,
PartitionedRelPruningData *pprune, PartitionedRelPruningData *pprune,
bool initial_prune, bool initial_prune,
...@@ -1614,16 +1619,9 @@ ExecCreatePartitionPruneState(PlanState *planstate, ...@@ -1614,16 +1619,9 @@ ExecCreatePartitionPruneState(PlanState *planstate,
{ {
PartitionedRelPruneInfo *pinfo = lfirst_node(PartitionedRelPruneInfo, lc2); PartitionedRelPruneInfo *pinfo = lfirst_node(PartitionedRelPruneInfo, lc2);
PartitionedRelPruningData *pprune = &prunedata->partrelprunedata[j]; PartitionedRelPruningData *pprune = &prunedata->partrelprunedata[j];
PartitionPruneContext *context = &pprune->context;
Relation partrel; Relation partrel;
PartitionDesc partdesc; PartitionDesc partdesc;
PartitionKey partkey; PartitionKey partkey;
int partnatts;
int n_steps;
ListCell *lc3;
/* present_parts is also subject to later modification */
pprune->present_parts = bms_copy(pinfo->present_parts);
/* /*
* We can rely on the copies of the partitioned table's partition * We can rely on the copies of the partitioned table's partition
...@@ -1643,6 +1641,7 @@ ExecCreatePartitionPruneState(PlanState *planstate, ...@@ -1643,6 +1641,7 @@ ExecCreatePartitionPruneState(PlanState *planstate,
* However, new partitions may have been added. * However, new partitions may have been added.
*/ */
Assert(partdesc->nparts >= pinfo->nparts); Assert(partdesc->nparts >= pinfo->nparts);
pprune->nparts = partdesc->nparts;
pprune->subplan_map = palloc(sizeof(int) * partdesc->nparts); pprune->subplan_map = palloc(sizeof(int) * partdesc->nparts);
if (partdesc->nparts == pinfo->nparts) if (partdesc->nparts == pinfo->nparts)
{ {
...@@ -1700,11 +1699,65 @@ ExecCreatePartitionPruneState(PlanState *planstate, ...@@ -1700,11 +1699,65 @@ ExecCreatePartitionPruneState(PlanState *planstate,
Assert(pd_idx == pinfo->nparts); Assert(pd_idx == pinfo->nparts);
} }
n_steps = list_length(pinfo->pruning_steps); /* present_parts is also subject to later modification */
pprune->present_parts = bms_copy(pinfo->present_parts);
/*
* Initialize pruning contexts as needed.
*/
pprune->initial_pruning_steps = pinfo->initial_pruning_steps;
if (pinfo->initial_pruning_steps)
{
ExecInitPruningContext(&pprune->initial_context,
pinfo->initial_pruning_steps,
partdesc, partkey, planstate);
/* Record whether initial pruning is needed at any level */
prunestate->do_initial_prune = true;
}
pprune->exec_pruning_steps = pinfo->exec_pruning_steps;
if (pinfo->exec_pruning_steps)
{
ExecInitPruningContext(&pprune->exec_context,
pinfo->exec_pruning_steps,
partdesc, partkey, planstate);
/* Record whether exec pruning is needed at any level */
prunestate->do_exec_prune = true;
}
/*
* Accumulate the IDs of all PARAM_EXEC Params affecting the
* partitioning decisions at this plan node.
*/
prunestate->execparamids = bms_add_members(prunestate->execparamids,
pinfo->execparamids);
j++;
}
i++;
}
return prunestate;
}
/*
* Initialize a PartitionPruneContext for the given list of pruning steps.
*/
static void
ExecInitPruningContext(PartitionPruneContext *context,
List *pruning_steps,
PartitionDesc partdesc,
PartitionKey partkey,
PlanState *planstate)
{
int n_steps;
int partnatts;
ListCell *lc;
n_steps = list_length(pruning_steps);
context->strategy = partkey->strategy; context->strategy = partkey->strategy;
context->partnatts = partnatts = partkey->partnatts; context->partnatts = partnatts = partkey->partnatts;
context->nparts = pinfo->nparts; context->nparts = partdesc->nparts;
context->boundinfo = partdesc->boundinfo; context->boundinfo = partdesc->boundinfo;
context->partcollation = partkey->partcollation; context->partcollation = partkey->partcollation;
context->partsupfunc = partkey->partsupfunc; context->partsupfunc = partkey->partsupfunc;
...@@ -1719,10 +1772,10 @@ ExecCreatePartitionPruneState(PlanState *planstate, ...@@ -1719,10 +1772,10 @@ ExecCreatePartitionPruneState(PlanState *planstate,
/* Initialize expression state for each expression we need */ /* Initialize expression state for each expression we need */
context->exprstates = (ExprState **) context->exprstates = (ExprState **)
palloc0(sizeof(ExprState *) * n_steps * partnatts); palloc0(sizeof(ExprState *) * n_steps * partnatts);
foreach(lc3, pinfo->pruning_steps) foreach(lc, pruning_steps)
{ {
PartitionPruneStepOp *step = (PartitionPruneStepOp *) lfirst(lc3); PartitionPruneStepOp *step = (PartitionPruneStepOp *) lfirst(lc);
ListCell *lc4; ListCell *lc2;
int keyno; int keyno;
/* not needed for other step kinds */ /* not needed for other step kinds */
...@@ -1732,9 +1785,9 @@ ExecCreatePartitionPruneState(PlanState *planstate, ...@@ -1732,9 +1785,9 @@ ExecCreatePartitionPruneState(PlanState *planstate,
Assert(list_length(step->exprs) <= partnatts); Assert(list_length(step->exprs) <= partnatts);
keyno = 0; keyno = 0;
foreach(lc4, step->exprs) foreach(lc2, step->exprs)
{ {
Expr *expr = (Expr *) lfirst(lc4); Expr *expr = (Expr *) lfirst(lc2);
/* not needed for Consts */ /* not needed for Consts */
if (!IsA(expr, Const)) if (!IsA(expr, Const))
...@@ -1749,31 +1802,6 @@ ExecCreatePartitionPruneState(PlanState *planstate, ...@@ -1749,31 +1802,6 @@ ExecCreatePartitionPruneState(PlanState *planstate,
keyno++; keyno++;
} }
} }
/* Array is not modified at runtime, so just point to plan's copy */
context->exprhasexecparam = pinfo->hasexecparam;
pprune->pruning_steps = pinfo->pruning_steps;
pprune->do_initial_prune = pinfo->do_initial_prune;
pprune->do_exec_prune = pinfo->do_exec_prune;
/* Record if pruning would be useful at any level */
prunestate->do_initial_prune |= pinfo->do_initial_prune;
prunestate->do_exec_prune |= pinfo->do_exec_prune;
/*
* Accumulate the IDs of all PARAM_EXEC Params affecting the
* partitioning decisions at this plan node.
*/
prunestate->execparamids = bms_add_members(prunestate->execparamids,
pinfo->execparamids);
j++;
}
i++;
}
return prunestate;
} }
/* /*
...@@ -1824,7 +1852,8 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubplans) ...@@ -1824,7 +1852,8 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubplans)
find_matching_subplans_recurse(prunedata, pprune, true, &result); find_matching_subplans_recurse(prunedata, pprune, true, &result);
/* Expression eval may have used space in node's ps_ExprContext too */ /* Expression eval may have used space in node's ps_ExprContext too */
ResetExprContext(pprune->context.planstate->ps_ExprContext); if (pprune->initial_pruning_steps)
ResetExprContext(pprune->initial_context.planstate->ps_ExprContext);
} }
/* Add in any subplans that partition pruning didn't account for */ /* Add in any subplans that partition pruning didn't account for */
...@@ -1888,7 +1917,7 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubplans) ...@@ -1888,7 +1917,7 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubplans)
for (j = prunedata->num_partrelprunedata - 1; j >= 0; j--) for (j = prunedata->num_partrelprunedata - 1; j >= 0; j--)
{ {
PartitionedRelPruningData *pprune = &prunedata->partrelprunedata[j]; PartitionedRelPruningData *pprune = &prunedata->partrelprunedata[j];
int nparts = pprune->context.nparts; int nparts = pprune->nparts;
int k; int k;
/* We just rebuild present_parts from scratch */ /* We just rebuild present_parts from scratch */
...@@ -1993,7 +2022,8 @@ ExecFindMatchingSubPlans(PartitionPruneState *prunestate) ...@@ -1993,7 +2022,8 @@ ExecFindMatchingSubPlans(PartitionPruneState *prunestate)
find_matching_subplans_recurse(prunedata, pprune, false, &result); find_matching_subplans_recurse(prunedata, pprune, false, &result);
/* Expression eval may have used space in node's ps_ExprContext too */ /* Expression eval may have used space in node's ps_ExprContext too */
ResetExprContext(pprune->context.planstate->ps_ExprContext); if (pprune->exec_pruning_steps)
ResetExprContext(pprune->exec_context.planstate->ps_ExprContext);
} }
/* Add in any subplans that partition pruning didn't account for */ /* Add in any subplans that partition pruning didn't account for */
...@@ -2029,15 +2059,15 @@ find_matching_subplans_recurse(PartitionPruningData *prunedata, ...@@ -2029,15 +2059,15 @@ find_matching_subplans_recurse(PartitionPruningData *prunedata,
check_stack_depth(); check_stack_depth();
/* Only prune if pruning would be useful at this level. */ /* Only prune if pruning would be useful at this level. */
if (initial_prune ? pprune->do_initial_prune : pprune->do_exec_prune) if (initial_prune && pprune->initial_pruning_steps)
{ {
PartitionPruneContext *context = &pprune->context; partset = get_matching_partitions(&pprune->initial_context,
pprune->initial_pruning_steps);
/* Set whether we can evaluate PARAM_EXEC Params or not */ }
context->evalexecparams = !initial_prune; else if (!initial_prune && pprune->exec_pruning_steps)
{
partset = get_matching_partitions(context, partset = get_matching_partitions(&pprune->exec_context,
pprune->pruning_steps); pprune->exec_pruning_steps);
} }
else else
{ {
......
...@@ -1198,16 +1198,13 @@ _copyPartitionedRelPruneInfo(const PartitionedRelPruneInfo *from) ...@@ -1198,16 +1198,13 @@ _copyPartitionedRelPruneInfo(const PartitionedRelPruneInfo *from)
PartitionedRelPruneInfo *newnode = makeNode(PartitionedRelPruneInfo); PartitionedRelPruneInfo *newnode = makeNode(PartitionedRelPruneInfo);
COPY_SCALAR_FIELD(rtindex); COPY_SCALAR_FIELD(rtindex);
COPY_NODE_FIELD(pruning_steps);
COPY_BITMAPSET_FIELD(present_parts); COPY_BITMAPSET_FIELD(present_parts);
COPY_SCALAR_FIELD(nparts); COPY_SCALAR_FIELD(nparts);
COPY_SCALAR_FIELD(nexprs);
COPY_POINTER_FIELD(subplan_map, from->nparts * sizeof(int)); COPY_POINTER_FIELD(subplan_map, from->nparts * sizeof(int));
COPY_POINTER_FIELD(subpart_map, from->nparts * sizeof(int)); COPY_POINTER_FIELD(subpart_map, from->nparts * sizeof(int));
COPY_POINTER_FIELD(relid_map, from->nparts * sizeof(Oid)); COPY_POINTER_FIELD(relid_map, from->nparts * sizeof(Oid));
COPY_POINTER_FIELD(hasexecparam, from->nexprs * sizeof(bool)); COPY_NODE_FIELD(initial_pruning_steps);
COPY_SCALAR_FIELD(do_initial_prune); COPY_NODE_FIELD(exec_pruning_steps);
COPY_SCALAR_FIELD(do_exec_prune);
COPY_BITMAPSET_FIELD(execparamids); COPY_BITMAPSET_FIELD(execparamids);
return newnode; return newnode;
......
...@@ -948,16 +948,13 @@ _outPartitionedRelPruneInfo(StringInfo str, const PartitionedRelPruneInfo *node) ...@@ -948,16 +948,13 @@ _outPartitionedRelPruneInfo(StringInfo str, const PartitionedRelPruneInfo *node)
WRITE_NODE_TYPE("PARTITIONEDRELPRUNEINFO"); WRITE_NODE_TYPE("PARTITIONEDRELPRUNEINFO");
WRITE_UINT_FIELD(rtindex); WRITE_UINT_FIELD(rtindex);
WRITE_NODE_FIELD(pruning_steps);
WRITE_BITMAPSET_FIELD(present_parts); WRITE_BITMAPSET_FIELD(present_parts);
WRITE_INT_FIELD(nparts); WRITE_INT_FIELD(nparts);
WRITE_INT_FIELD(nexprs);
WRITE_INT_ARRAY(subplan_map, node->nparts); WRITE_INT_ARRAY(subplan_map, node->nparts);
WRITE_INT_ARRAY(subpart_map, node->nparts); WRITE_INT_ARRAY(subpart_map, node->nparts);
WRITE_OID_ARRAY(relid_map, node->nparts); WRITE_OID_ARRAY(relid_map, node->nparts);
WRITE_BOOL_ARRAY(hasexecparam, node->nexprs); WRITE_NODE_FIELD(initial_pruning_steps);
WRITE_BOOL_FIELD(do_initial_prune); WRITE_NODE_FIELD(exec_pruning_steps);
WRITE_BOOL_FIELD(do_exec_prune);
WRITE_BITMAPSET_FIELD(execparamids); WRITE_BITMAPSET_FIELD(execparamids);
} }
......
...@@ -2388,16 +2388,13 @@ _readPartitionedRelPruneInfo(void) ...@@ -2388,16 +2388,13 @@ _readPartitionedRelPruneInfo(void)
READ_LOCALS(PartitionedRelPruneInfo); READ_LOCALS(PartitionedRelPruneInfo);
READ_UINT_FIELD(rtindex); READ_UINT_FIELD(rtindex);
READ_NODE_FIELD(pruning_steps);
READ_BITMAPSET_FIELD(present_parts); READ_BITMAPSET_FIELD(present_parts);
READ_INT_FIELD(nparts); READ_INT_FIELD(nparts);
READ_INT_FIELD(nexprs);
READ_INT_ARRAY(subplan_map, local_node->nparts); READ_INT_ARRAY(subplan_map, local_node->nparts);
READ_INT_ARRAY(subpart_map, local_node->nparts); READ_INT_ARRAY(subpart_map, local_node->nparts);
READ_OID_ARRAY(relid_map, local_node->nparts); READ_OID_ARRAY(relid_map, local_node->nparts);
READ_BOOL_ARRAY(hasexecparam, local_node->nexprs); READ_NODE_FIELD(initial_pruning_steps);
READ_BOOL_FIELD(do_initial_prune); READ_NODE_FIELD(exec_pruning_steps);
READ_BOOL_FIELD(do_exec_prune);
READ_BITMAPSET_FIELD(execparamids); READ_BITMAPSET_FIELD(execparamids);
READ_DONE(); READ_DONE();
......
...@@ -84,22 +84,43 @@ typedef enum PartClauseMatchStatus ...@@ -84,22 +84,43 @@ typedef enum PartClauseMatchStatus
PARTCLAUSE_UNSUPPORTED PARTCLAUSE_UNSUPPORTED
} PartClauseMatchStatus; } PartClauseMatchStatus;
/*
* PartClauseTarget
* Identifies which qual clauses we can use for generating pruning steps
*/
typedef enum PartClauseTarget
{
PARTTARGET_PLANNER, /* want to prune during planning */
PARTTARGET_INITIAL, /* want to prune during executor startup */
PARTTARGET_EXEC /* want to prune during each plan node scan */
} PartClauseTarget;
/* /*
* GeneratePruningStepsContext * GeneratePruningStepsContext
* Information about the current state of generation of "pruning steps" * Information about the current state of generation of "pruning steps"
* for a given set of clauses * for a given set of clauses
* *
* gen_partprune_steps() initializes an instance of this struct, which is used * gen_partprune_steps() initializes and returns an instance of this struct.
* throughout the step generation process. *
* Note that has_mutable_op, has_mutable_arg, and has_exec_param are set if
* we found any potentially-useful-for-pruning clause having those properties,
* whether or not we actually used the clause in the steps list. This
* definition allows us to skip the PARTTARGET_EXEC pass in some cases.
*/ */
typedef struct GeneratePruningStepsContext typedef struct GeneratePruningStepsContext
{ {
/* Input data: */ /* Copies of input arguments for gen_partprune_steps: */
bool forplanner; /* true when generating steps to be used RelOptInfo *rel; /* the partitioned relation */
* during query planning */ PartClauseTarget target; /* use-case we're generating steps for */
/* Working state and result data: */ /* Result data: */
List *steps; /* list of PartitionPruneSteps */
bool has_mutable_op; /* clauses include any stable operators */
bool has_mutable_arg; /* clauses include any mutable comparison
* values, *other than* exec params */
bool has_exec_param; /* clauses include any PARAM_EXEC params */
bool contradictory; /* clauses were proven self-contradictory */
/* Working state: */
int next_step_id; int next_step_id;
List *steps; /* output, list of PartitionPruneSteps */
} GeneratePruningStepsContext; } GeneratePruningStepsContext;
/* The result of performing one PartitionPruneStep */ /* The result of performing one PartitionPruneStep */
...@@ -121,22 +142,20 @@ static List *make_partitionedrel_pruneinfo(PlannerInfo *root, ...@@ -121,22 +142,20 @@ static List *make_partitionedrel_pruneinfo(PlannerInfo *root,
int *relid_subplan_map, int *relid_subplan_map,
List *partitioned_rels, List *prunequal, List *partitioned_rels, List *prunequal,
Bitmapset **matchedsubplans); Bitmapset **matchedsubplans);
static List *gen_partprune_steps(RelOptInfo *rel, List *clauses, static void gen_partprune_steps(RelOptInfo *rel, List *clauses,
bool forplanner, bool *contradictory); PartClauseTarget target,
GeneratePruningStepsContext *context);
static List *gen_partprune_steps_internal(GeneratePruningStepsContext *context, static List *gen_partprune_steps_internal(GeneratePruningStepsContext *context,
RelOptInfo *rel, List *clauses, List *clauses);
bool *contradictory);
static PartitionPruneStep *gen_prune_step_op(GeneratePruningStepsContext *context, static PartitionPruneStep *gen_prune_step_op(GeneratePruningStepsContext *context,
StrategyNumber opstrategy, bool op_is_ne, StrategyNumber opstrategy, bool op_is_ne,
List *exprs, List *cmpfns, Bitmapset *nullkeys); List *exprs, List *cmpfns, Bitmapset *nullkeys);
static PartitionPruneStep *gen_prune_step_combine(GeneratePruningStepsContext *context, static PartitionPruneStep *gen_prune_step_combine(GeneratePruningStepsContext *context,
List *source_stepids, List *source_stepids,
PartitionPruneCombineOp combineOp); PartitionPruneCombineOp combineOp);
static PartitionPruneStep *gen_prune_steps_from_opexps(PartitionScheme part_scheme, static PartitionPruneStep *gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
GeneratePruningStepsContext *context,
List **keyclauses, Bitmapset *nullkeys); List **keyclauses, Bitmapset *nullkeys);
static PartClauseMatchStatus match_clause_to_partition_key(RelOptInfo *rel, static PartClauseMatchStatus match_clause_to_partition_key(GeneratePruningStepsContext *context,
GeneratePruningStepsContext *context,
Expr *clause, Expr *partkey, int partkeyidx, Expr *clause, Expr *partkey, int partkeyidx,
bool *clause_is_not_null, bool *clause_is_not_null,
PartClauseInfo **pc, List **clause_steps); PartClauseInfo **pc, List **clause_steps);
...@@ -169,8 +188,7 @@ static PruneStepResult *get_matching_range_bounds(PartitionPruneContext *context ...@@ -169,8 +188,7 @@ static PruneStepResult *get_matching_range_bounds(PartitionPruneContext *context
FmgrInfo *partsupfunc, Bitmapset *nullkeys); FmgrInfo *partsupfunc, Bitmapset *nullkeys);
static Bitmapset *pull_exec_paramids(Expr *expr); static Bitmapset *pull_exec_paramids(Expr *expr);
static bool pull_exec_paramids_walker(Node *node, Bitmapset **context); static bool pull_exec_paramids_walker(Node *node, Bitmapset **context);
static bool analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps, static Bitmapset *get_partkey_exec_paramids(List *steps);
int partnatts);
static PruneStepResult *perform_pruning_base_step(PartitionPruneContext *context, static PruneStepResult *perform_pruning_base_step(PartitionPruneContext *context,
PartitionPruneStepOp *opstep); PartitionPruneStepOp *opstep);
static PruneStepResult *perform_pruning_combine_step(PartitionPruneContext *context, static PruneStepResult *perform_pruning_combine_step(PartitionPruneContext *context,
...@@ -178,7 +196,7 @@ static PruneStepResult *perform_pruning_combine_step(PartitionPruneContext *cont ...@@ -178,7 +196,7 @@ static PruneStepResult *perform_pruning_combine_step(PartitionPruneContext *cont
PruneStepResult **step_results); PruneStepResult **step_results);
static bool match_boolean_partition_clause(Oid partopfamily, Expr *clause, static bool match_boolean_partition_clause(Oid partopfamily, Expr *clause,
Expr *partkey, Expr **outconst); Expr *partkey, Expr **outconst);
static bool partkey_datum_from_expr(PartitionPruneContext *context, static void partkey_datum_from_expr(PartitionPruneContext *context,
Expr *expr, int stateidx, Expr *expr, int stateidx,
Datum *value, bool *isnull); Datum *value, bool *isnull);
...@@ -347,10 +365,11 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, ...@@ -347,10 +365,11 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
Index rti = lfirst_int(lc); Index rti = lfirst_int(lc);
RelOptInfo *subpart = find_base_rel(root, rti); RelOptInfo *subpart = find_base_rel(root, rti);
PartitionedRelPruneInfo *pinfo; PartitionedRelPruneInfo *pinfo;
int partnatts = subpart->part_scheme->partnatts;
List *partprunequal; List *partprunequal;
List *pruning_steps; List *initial_pruning_steps;
bool contradictory; List *exec_pruning_steps;
Bitmapset *execparamids;
GeneratePruningStepsContext context;
/* /*
* Fill the mapping array. * Fill the mapping array.
...@@ -415,15 +434,16 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, ...@@ -415,15 +434,16 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
} }
/* /*
* Convert pruning qual to pruning steps. Since these steps will be * Convert pruning qual to pruning steps. We may need to do this
* used in the executor, we can pass 'forplanner' as false to allow * twice, once to obtain executor startup pruning steps, and once for
* steps to be generated that are unsafe for evaluation during * executor per-scan pruning steps. This first pass creates startup
* planning, e.g. evaluation of stable functions. * pruning steps and detects whether there's any possibly-useful quals
* that would require per-scan pruning.
*/ */
pruning_steps = gen_partprune_steps(subpart, partprunequal, false, gen_partprune_steps(subpart, partprunequal, PARTTARGET_INITIAL,
&contradictory); &context);
if (contradictory) if (context.contradictory)
{ {
/* /*
* This shouldn't happen as the planner should have detected this * This shouldn't happen as the planner should have detected this
...@@ -437,20 +457,63 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, ...@@ -437,20 +457,63 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
return NIL; return NIL;
} }
/*
* If no mutable operators or expressions appear in usable pruning
* clauses, then there's no point in running startup pruning, because
* plan-time pruning should have pruned everything prunable.
*/
if (context.has_mutable_op || context.has_mutable_arg)
initial_pruning_steps = context.steps;
else
initial_pruning_steps = NIL;
/*
* If no exec Params appear in potentially-usable pruning clauses,
* then there's no point in even thinking about per-scan pruning.
*/
if (context.has_exec_param)
{
/* ... OK, we'd better think about it */
gen_partprune_steps(subpart, partprunequal, PARTTARGET_EXEC,
&context);
if (context.contradictory)
{
/* As above, skip run-time pruning if anything fishy happens */
return NIL;
}
exec_pruning_steps = context.steps;
/*
* Detect which exec Params actually got used; the fact that some
* were in available clauses doesn't mean we actually used them.
* Skip per-scan pruning if there are none.
*/
execparamids = get_partkey_exec_paramids(exec_pruning_steps);
if (bms_is_empty(execparamids))
exec_pruning_steps = NIL;
}
else
{
/* No exec Params anywhere, so forget about scan-time pruning */
exec_pruning_steps = NIL;
execparamids = NULL;
}
if (initial_pruning_steps || exec_pruning_steps)
doruntimeprune = true;
/* Begin constructing the PartitionedRelPruneInfo for this rel */ /* Begin constructing the PartitionedRelPruneInfo for this rel */
pinfo = makeNode(PartitionedRelPruneInfo); pinfo = makeNode(PartitionedRelPruneInfo);
pinfo->rtindex = rti; pinfo->rtindex = rti;
pinfo->pruning_steps = pruning_steps; pinfo->initial_pruning_steps = initial_pruning_steps;
pinfo->exec_pruning_steps = exec_pruning_steps;
pinfo->execparamids = execparamids;
/* Remaining fields will be filled in the next loop */ /* Remaining fields will be filled in the next loop */
pinfolist = lappend(pinfolist, pinfo); pinfolist = lappend(pinfolist, pinfo);
/*
* Determine which pruning types should be enabled at this level. This
* also records paramids relevant to pruning steps in 'pinfo'.
*/
doruntimeprune |= analyze_partkey_exprs(pinfo, pruning_steps,
partnatts);
} }
if (!doruntimeprune) if (!doruntimeprune)
...@@ -532,37 +595,25 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, ...@@ -532,37 +595,25 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
/* /*
* gen_partprune_steps * gen_partprune_steps
* Process 'clauses' (a rel's baserestrictinfo list of clauses) and return * Process 'clauses' (typically a rel's baserestrictinfo list of clauses)
* a list of "partition pruning steps". * and create a list of "partition pruning steps".
* *
* 'forplanner' must be true when generating steps to be evaluated during * 'target' tells whether to generate pruning steps for planning (use
* query planning, false when generating steps to be used at run-time. * immutable clauses only), or for executor startup (use any allowable
* * clause except ones containing PARAM_EXEC Params), or for executor
* The result generated with forplanner=false includes all clauses that * per-scan pruning (use any allowable clause).
* are selected with forplanner=true, because in some cases we need a *
* combination of clauses to prune successfully. For example, if we * 'context' is an output argument that receives the steps list as well as
* are partitioning on a hash of columns A and B, and we have clauses * some subsidiary flags; see the GeneratePruningStepsContext typedef.
* "WHERE A=constant AND B=nonconstant", we can't do anything at plan
* time even though the first clause would be evaluable then. And we
* must include the first clause when called with forplanner=false,
* or we'll fail to prune at run-time either. This does mean that when
* called with forplanner=false, we may return steps that don't actually
* need to be executed at runtime; it's left to analyze_partkey_exprs()
* to (re)discover that.
*
* If the clauses in the input list are contradictory or there is a
* pseudo-constant "false", *contradictory is set to true upon return,
* else it's set false.
*/ */
static List * static void
gen_partprune_steps(RelOptInfo *rel, List *clauses, bool forplanner, gen_partprune_steps(RelOptInfo *rel, List *clauses, PartClauseTarget target,
bool *contradictory) GeneratePruningStepsContext *context)
{ {
GeneratePruningStepsContext context; /* Initialize all output values to zero/false/NULL */
memset(context, 0, sizeof(GeneratePruningStepsContext));
context.forplanner = forplanner; context->rel = rel;
context.next_step_id = 0; context->target = target;
context.steps = NIL;
/* /*
* For sub-partitioned tables there's a corner case where if the * For sub-partitioned tables there's a corner case where if the
...@@ -593,9 +644,7 @@ gen_partprune_steps(RelOptInfo *rel, List *clauses, bool forplanner, ...@@ -593,9 +644,7 @@ gen_partprune_steps(RelOptInfo *rel, List *clauses, bool forplanner,
} }
/* Down into the rabbit-hole. */ /* Down into the rabbit-hole. */
(void) gen_partprune_steps_internal(&context, rel, clauses, contradictory); (void) gen_partprune_steps_internal(context, clauses);
return context.steps;
} }
/* /*
...@@ -613,7 +662,7 @@ prune_append_rel_partitions(RelOptInfo *rel) ...@@ -613,7 +662,7 @@ prune_append_rel_partitions(RelOptInfo *rel)
{ {
List *clauses = rel->baserestrictinfo; List *clauses = rel->baserestrictinfo;
List *pruning_steps; List *pruning_steps;
bool contradictory; GeneratePruningStepsContext gcontext;
PartitionPruneContext context; PartitionPruneContext context;
Assert(rel->part_scheme != NULL); Assert(rel->part_scheme != NULL);
...@@ -630,15 +679,19 @@ prune_append_rel_partitions(RelOptInfo *rel) ...@@ -630,15 +679,19 @@ prune_append_rel_partitions(RelOptInfo *rel)
return bms_add_range(NULL, 0, rel->nparts - 1); return bms_add_range(NULL, 0, rel->nparts - 1);
/* /*
* Process clauses. If the clauses are found to be contradictory, we can * Process clauses to extract pruning steps that are usable at plan time.
* return the empty set. Pass 'forplanner' as true to indicate to the * If the clauses are found to be contradictory, we can return the empty
* pruning code that we only want pruning steps that can be evaluated * set.
* during planning.
*/ */
pruning_steps = gen_partprune_steps(rel, clauses, true, gen_partprune_steps(rel, clauses, PARTTARGET_PLANNER,
&contradictory); &gcontext);
if (contradictory) if (gcontext.contradictory)
return NULL; return NULL;
pruning_steps = gcontext.steps;
/* If there's nothing usable, return all partitions */
if (pruning_steps == NIL)
return bms_add_range(NULL, 0, rel->nparts - 1);
/* Set up PartitionPruneContext */ /* Set up PartitionPruneContext */
context.strategy = rel->part_scheme->strategy; context.strategy = rel->part_scheme->strategy;
...@@ -655,8 +708,6 @@ prune_append_rel_partitions(RelOptInfo *rel) ...@@ -655,8 +708,6 @@ prune_append_rel_partitions(RelOptInfo *rel)
/* These are not valid when being called from the planner */ /* These are not valid when being called from the planner */
context.planstate = NULL; context.planstate = NULL;
context.exprstates = NULL; context.exprstates = NULL;
context.exprhasexecparam = NULL;
context.evalexecparams = false;
/* Actual pruning happens here. */ /* Actual pruning happens here. */
return get_matching_partitions(&context, pruning_steps); return get_matching_partitions(&context, pruning_steps);
...@@ -667,7 +718,7 @@ prune_append_rel_partitions(RelOptInfo *rel) ...@@ -667,7 +718,7 @@ prune_append_rel_partitions(RelOptInfo *rel)
* Determine partitions that survive partition pruning * Determine partitions that survive partition pruning
* *
* Note: context->planstate must be set to a valid PlanState when the * Note: context->planstate must be set to a valid PlanState when the
* pruning_steps were generated with 'forplanner' = false. * pruning_steps were generated with a target other than PARTTARGET_PLANNER.
* *
* Returns a Bitmapset of the RelOptInfo->part_rels indexes of the surviving * Returns a Bitmapset of the RelOptInfo->part_rels indexes of the surviving
* partitions. * partitions.
...@@ -786,16 +837,15 @@ get_matching_partitions(PartitionPruneContext *context, List *pruning_steps) ...@@ -786,16 +837,15 @@ get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
* even across recursive calls. * even across recursive calls.
* *
* If we find clauses that are mutually contradictory, or a pseudoconstant * If we find clauses that are mutually contradictory, or a pseudoconstant
* clause that contains false, we set *contradictory to true and return NIL * clause that contains false, we set context->contradictory to true and
* (that is, no pruning steps). Caller should consider all partitions as * return NIL (that is, no pruning steps). Caller should consider all
* pruned in that case. Otherwise, *contradictory is set to false. * partitions as pruned in that case.
*/ */
static List * static List *
gen_partprune_steps_internal(GeneratePruningStepsContext *context, gen_partprune_steps_internal(GeneratePruningStepsContext *context,
RelOptInfo *rel, List *clauses, List *clauses)
bool *contradictory)
{ {
PartitionScheme part_scheme = rel->part_scheme; PartitionScheme part_scheme = context->rel->part_scheme;
List *keyclauses[PARTITION_MAX_KEYS]; List *keyclauses[PARTITION_MAX_KEYS];
Bitmapset *nullkeys = NULL, Bitmapset *nullkeys = NULL,
*notnullkeys = NULL; *notnullkeys = NULL;
...@@ -803,8 +853,6 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -803,8 +853,6 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
List *result = NIL; List *result = NIL;
ListCell *lc; ListCell *lc;
*contradictory = false;
memset(keyclauses, 0, sizeof(keyclauses)); memset(keyclauses, 0, sizeof(keyclauses));
foreach(lc, clauses) foreach(lc, clauses)
{ {
...@@ -820,7 +868,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -820,7 +868,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
(((Const *) clause)->constisnull || (((Const *) clause)->constisnull ||
!DatumGetBool(((Const *) clause)->constvalue))) !DatumGetBool(((Const *) clause)->constvalue)))
{ {
*contradictory = true; context->contradictory = true;
return NIL; return NIL;
} }
...@@ -841,6 +889,12 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -841,6 +889,12 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
bool all_args_contradictory = true; bool all_args_contradictory = true;
ListCell *lc1; ListCell *lc1;
/*
* We can share the outer context area with the recursive
* call, but contradictory had better not be true yet.
*/
Assert(!context->contradictory);
/* /*
* Get pruning step for each arg. If we get contradictory for * Get pruning step for each arg. If we get contradictory for
* all args, it means the OR expression is false as a whole. * all args, it means the OR expression is false as a whole.
...@@ -851,11 +905,18 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -851,11 +905,18 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
bool arg_contradictory; bool arg_contradictory;
List *argsteps; List *argsteps;
argsteps = argsteps = gen_partprune_steps_internal(context,
gen_partprune_steps_internal(context, rel, list_make1(arg));
list_make1(arg), arg_contradictory = context->contradictory;
&arg_contradictory); /* Keep context->contradictory clear till we're done */
if (!arg_contradictory) context->contradictory = false;
if (arg_contradictory)
{
/* Just ignore self-contradictory arguments. */
continue;
}
else
all_args_contradictory = false; all_args_contradictory = false;
if (argsteps != NIL) if (argsteps != NIL)
...@@ -869,34 +930,28 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -869,34 +930,28 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
else else
{ {
/* /*
* No steps either means that arg_contradictory is * The arg didn't contain a clause matching this
* true or the arg didn't contain a clause matching * partition key. We cannot prune using such an arg.
* this partition key. * To indicate that to the pruning code, we must
* construct a dummy PartitionPruneStepCombine whose
* source_stepids is set to an empty List.
* *
* In case of the latter, we cannot prune using such
* an arg. To indicate that to the pruning code, we
* must construct a dummy PartitionPruneStepCombine
* whose source_stepids is set to an empty List.
* However, if we can prove using constraint exclusion * However, if we can prove using constraint exclusion
* that the clause refutes the table's partition * that the clause refutes the table's partition
* constraint (if it's sub-partitioned), we need not * constraint (if it's sub-partitioned), we need not
* bother with that. That is, we effectively ignore * bother with that. That is, we effectively ignore
* this OR arm. * this OR arm.
*/ */
List *partconstr = rel->partition_qual; List *partconstr = context->rel->partition_qual;
PartitionPruneStep *orstep; PartitionPruneStep *orstep;
/* Just ignore this argument. */
if (arg_contradictory)
continue;
if (partconstr) if (partconstr)
{ {
partconstr = (List *) partconstr = (List *)
expression_planner((Expr *) partconstr); expression_planner((Expr *) partconstr);
if (rel->relid != 1) if (context->rel->relid != 1)
ChangeVarNodes((Node *) partconstr, 1, ChangeVarNodes((Node *) partconstr, 1,
rel->relid, 0); context->rel->relid, 0);
if (predicate_refuted_by(partconstr, if (predicate_refuted_by(partconstr,
list_make1(arg), list_make1(arg),
false)) false))
...@@ -909,11 +964,12 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -909,11 +964,12 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
} }
} }
*contradictory = all_args_contradictory; /* If all the OR arms are contradictory, we can stop */
if (all_args_contradictory)
/* Check if any contradicting clauses were found */ {
if (*contradictory) context->contradictory = true;
return NIL; return NIL;
}
if (arg_stepids != NIL) if (arg_stepids != NIL)
{ {
...@@ -937,9 +993,10 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -937,9 +993,10 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
* recurse and later combine the component partitions sets * recurse and later combine the component partitions sets
* using a combine step. * using a combine step.
*/ */
argsteps = gen_partprune_steps_internal(context, rel, args, argsteps = gen_partprune_steps_internal(context, args);
contradictory);
if (*contradictory) /* If any AND arm is contradictory, we can stop immediately */
if (context->contradictory)
return NIL; return NIL;
foreach(lc1, argsteps) foreach(lc1, argsteps)
...@@ -969,17 +1026,16 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -969,17 +1026,16 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
} }
/* /*
* Must be a clause for which we can check if one of its args matches * See if we can match this clause to any of the partition keys.
* the partition key.
*/ */
for (i = 0; i < part_scheme->partnatts; i++) for (i = 0; i < part_scheme->partnatts; i++)
{ {
Expr *partkey = linitial(rel->partexprs[i]); Expr *partkey = linitial(context->rel->partexprs[i]);
bool clause_is_not_null = false; bool clause_is_not_null = false;
PartClauseInfo *pc = NULL; PartClauseInfo *pc = NULL;
List *clause_steps = NIL; List *clause_steps = NIL;
switch (match_clause_to_partition_key(rel, context, switch (match_clause_to_partition_key(context,
clause, partkey, i, clause, partkey, i,
&clause_is_not_null, &clause_is_not_null,
&pc, &clause_steps)) &pc, &clause_steps))
...@@ -993,7 +1049,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -993,7 +1049,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
*/ */
if (bms_is_member(i, nullkeys)) if (bms_is_member(i, nullkeys))
{ {
*contradictory = true; context->contradictory = true;
return NIL; return NIL;
} }
generate_opsteps = true; generate_opsteps = true;
...@@ -1006,7 +1062,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -1006,7 +1062,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
/* check for conflicting IS NOT NULL */ /* check for conflicting IS NOT NULL */
if (bms_is_member(i, notnullkeys)) if (bms_is_member(i, notnullkeys))
{ {
*contradictory = true; context->contradictory = true;
return NIL; return NIL;
} }
nullkeys = bms_add_member(nullkeys, i); nullkeys = bms_add_member(nullkeys, i);
...@@ -1016,7 +1072,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -1016,7 +1072,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
/* check for conflicting IS NULL */ /* check for conflicting IS NULL */
if (bms_is_member(i, nullkeys)) if (bms_is_member(i, nullkeys))
{ {
*contradictory = true; context->contradictory = true;
return NIL; return NIL;
} }
notnullkeys = bms_add_member(notnullkeys, i); notnullkeys = bms_add_member(notnullkeys, i);
...@@ -1030,7 +1086,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -1030,7 +1086,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
case PARTCLAUSE_MATCH_CONTRADICT: case PARTCLAUSE_MATCH_CONTRADICT:
/* We've nothing more to do if a contradiction was found. */ /* We've nothing more to do if a contradiction was found. */
*contradictory = true; context->contradictory = true;
return NIL; return NIL;
case PARTCLAUSE_NOMATCH: case PARTCLAUSE_NOMATCH:
...@@ -1091,8 +1147,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, ...@@ -1091,8 +1147,7 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
PartitionPruneStep *step; PartitionPruneStep *step;
/* Strategy 2 */ /* Strategy 2 */
step = gen_prune_steps_from_opexps(part_scheme, context, step = gen_prune_steps_from_opexps(context, keyclauses, nullkeys);
keyclauses, nullkeys);
if (step != NULL) if (step != NULL)
result = lappend(result, step); result = lappend(result, step);
} }
...@@ -1201,15 +1256,15 @@ gen_prune_step_combine(GeneratePruningStepsContext *context, ...@@ -1201,15 +1256,15 @@ gen_prune_step_combine(GeneratePruningStepsContext *context,
* found for any subsequent keys; see specific notes below. * found for any subsequent keys; see specific notes below.
*/ */
static PartitionPruneStep * static PartitionPruneStep *
gen_prune_steps_from_opexps(PartitionScheme part_scheme, gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
GeneratePruningStepsContext *context,
List **keyclauses, Bitmapset *nullkeys) List **keyclauses, Bitmapset *nullkeys)
{ {
ListCell *lc; PartitionScheme part_scheme = context->rel->part_scheme;
List *opsteps = NIL; List *opsteps = NIL;
List *btree_clauses[BTMaxStrategyNumber + 1], List *btree_clauses[BTMaxStrategyNumber + 1],
*hash_clauses[HTMaxStrategyNumber + 1]; *hash_clauses[HTMaxStrategyNumber + 1];
int i; int i;
ListCell *lc;
memset(btree_clauses, 0, sizeof(btree_clauses)); memset(btree_clauses, 0, sizeof(btree_clauses));
memset(hash_clauses, 0, sizeof(hash_clauses)); memset(hash_clauses, 0, sizeof(hash_clauses));
...@@ -1563,13 +1618,12 @@ gen_prune_steps_from_opexps(PartitionScheme part_scheme, ...@@ -1563,13 +1618,12 @@ gen_prune_steps_from_opexps(PartitionScheme part_scheme,
* Output arguments: none set. * Output arguments: none set.
*/ */
static PartClauseMatchStatus static PartClauseMatchStatus
match_clause_to_partition_key(RelOptInfo *rel, match_clause_to_partition_key(GeneratePruningStepsContext *context,
GeneratePruningStepsContext *context,
Expr *clause, Expr *partkey, int partkeyidx, Expr *clause, Expr *partkey, int partkeyidx,
bool *clause_is_not_null, PartClauseInfo **pc, bool *clause_is_not_null, PartClauseInfo **pc,
List **clause_steps) List **clause_steps)
{ {
PartitionScheme part_scheme = rel->part_scheme; PartitionScheme part_scheme = context->rel->part_scheme;
Oid partopfamily = part_scheme->partopfamily[partkeyidx], Oid partopfamily = part_scheme->partopfamily[partkeyidx],
partcoll = part_scheme->partcollation[partkeyidx]; partcoll = part_scheme->partcollation[partkeyidx];
Expr *expr; Expr *expr;
...@@ -1588,7 +1642,7 @@ match_clause_to_partition_key(RelOptInfo *rel, ...@@ -1588,7 +1642,7 @@ match_clause_to_partition_key(RelOptInfo *rel,
partclause->op_is_ne = false; partclause->op_is_ne = false;
partclause->expr = expr; partclause->expr = expr;
/* We know that expr is of Boolean type. */ /* We know that expr is of Boolean type. */
partclause->cmpfn = rel->part_scheme->partsupfunc[partkeyidx].fn_oid; partclause->cmpfn = part_scheme->partsupfunc[partkeyidx].fn_oid;
partclause->op_strategy = InvalidStrategy; partclause->op_strategy = InvalidStrategy;
*pc = partclause; *pc = partclause;
...@@ -1646,59 +1700,6 @@ match_clause_to_partition_key(RelOptInfo *rel, ...@@ -1646,59 +1700,6 @@ match_clause_to_partition_key(RelOptInfo *rel,
if (!PartCollMatchesExprColl(partcoll, opclause->inputcollid)) if (!PartCollMatchesExprColl(partcoll, opclause->inputcollid))
return PARTCLAUSE_NOMATCH; return PARTCLAUSE_NOMATCH;
/*
* Matched with this key. Now check various properties of the clause
* to see if it's sane to use it for pruning. In most of these cases,
* we can return UNSUPPORTED because the same failure would occur no
* matter which partkey it's matched to. (In particular, now that
* we've successfully matched one side of the opclause to a partkey,
* there is no chance that matching the other side to another partkey
* will produce a usable result, since that'd mean there are Vars on
* both sides.)
*/
if (context->forplanner)
{
/*
* When pruning in the planner, we only support pruning using
* comparisons to constants. Immutable subexpressions will have
* been folded to constants already, and we cannot prune on the
* basis of anything that's not immutable.
*/
if (!IsA(expr, Const))
return PARTCLAUSE_UNSUPPORTED;
/*
* Also, the comparison operator itself must be immutable.
*/
if (op_volatile(opno) != PROVOLATILE_IMMUTABLE)
return PARTCLAUSE_UNSUPPORTED;
}
else
{
/*
* Otherwise, non-consts are allowed, but we can't prune using an
* expression that contains Vars.
*/
if (contain_var_clause((Node *) expr))
return PARTCLAUSE_UNSUPPORTED;
/*
* And we must reject anything containing a volatile function.
* Stable functions are OK though. (We need not check this for
* the comparison operator itself: anything that belongs to a
* partitioning operator family must be at least stable.)
*/
if (contain_volatile_functions((Node *) expr))
return PARTCLAUSE_UNSUPPORTED;
}
/*
* Only allow strict operators. This will guarantee nulls are
* filtered.
*/
if (!op_strict(opno))
return PARTCLAUSE_UNSUPPORTED;
/* /*
* See if the operator is relevant to the partitioning opfamily. * See if the operator is relevant to the partitioning opfamily.
* *
...@@ -1739,6 +1740,95 @@ match_clause_to_partition_key(RelOptInfo *rel, ...@@ -1739,6 +1740,95 @@ match_clause_to_partition_key(RelOptInfo *rel,
return PARTCLAUSE_NOMATCH; return PARTCLAUSE_NOMATCH;
} }
/*
* Only allow strict operators. This will guarantee nulls are
* filtered. (This test is likely useless, since btree and hash
* comparison operators are generally strict.)
*/
if (!op_strict(opno))
return PARTCLAUSE_UNSUPPORTED;
/*
* OK, we have a match to the partition key and a suitable operator.
* Examine the other argument to see if it's usable for pruning.
*
* In most of these cases, we can return UNSUPPORTED because the same
* failure would occur no matter which partkey it's matched to. (In
* particular, now that we've successfully matched one side of the
* opclause to a partkey, there is no chance that matching the other
* side to another partkey will produce a usable result, since that'd
* mean there are Vars on both sides.)
*
* Also, if we reject an argument for a target-dependent reason, set
* appropriate fields of *context to report that. We postpone these
* tests until after matching the partkey and the operator, so as to
* reduce the odds of setting the context fields for clauses that do
* not end up contributing to pruning steps.
*
* First, check for non-Const argument. (We assume that any immutable
* subexpression will have been folded to a Const already.)
*/
if (!IsA(expr, Const))
{
Bitmapset *paramids;
/*
* When pruning in the planner, we only support pruning using
* comparisons to constants. We cannot prune on the basis of
* anything that's not immutable. (Note that has_mutable_arg and
* has_exec_param do not get set for this target value.)
*/
if (context->target == PARTTARGET_PLANNER)
return PARTCLAUSE_UNSUPPORTED;
/*
* We can never prune using an expression that contains Vars.
*/
if (contain_var_clause((Node *) expr))
return PARTCLAUSE_UNSUPPORTED;
/*
* And we must reject anything containing a volatile function.
* Stable functions are OK though.
*/
if (contain_volatile_functions((Node *) expr))
return PARTCLAUSE_UNSUPPORTED;
/*
* See if there are any exec Params. If so, we can only use this
* expression during per-scan pruning.
*/
paramids = pull_exec_paramids(expr);
if (!bms_is_empty(paramids))
{
context->has_exec_param = true;
if (context->target != PARTTARGET_EXEC)
return PARTCLAUSE_UNSUPPORTED;
}
else
{
/* It's potentially usable, but mutable */
context->has_mutable_arg = true;
}
}
/*
* Check whether the comparison operator itself is immutable. (We
* assume anything that's in a btree or hash opclass is at least
* stable, but we need to check for immutability.)
*/
if (op_volatile(opno) != PROVOLATILE_IMMUTABLE)
{
context->has_mutable_op = true;
/*
* When pruning in the planner, we cannot prune with mutable
* operators.
*/
if (context->target == PARTTARGET_PLANNER)
return PARTCLAUSE_UNSUPPORTED;
}
/* /*
* Now find the procedure to use, based on the types. If the clause's * Now find the procedure to use, based on the types. If the clause's
* other argument is of the same type as the partitioning opclass's * other argument is of the same type as the partitioning opclass's
...@@ -1822,7 +1912,6 @@ match_clause_to_partition_key(RelOptInfo *rel, ...@@ -1822,7 +1912,6 @@ match_clause_to_partition_key(RelOptInfo *rel,
List *elem_exprs, List *elem_exprs,
*elem_clauses; *elem_clauses;
ListCell *lc1; ListCell *lc1;
bool contradictory;
if (IsA(leftop, RelabelType)) if (IsA(leftop, RelabelType))
leftop = ((RelabelType *) leftop)->arg; leftop = ((RelabelType *) leftop)->arg;
...@@ -1833,90 +1922,121 @@ match_clause_to_partition_key(RelOptInfo *rel, ...@@ -1833,90 +1922,121 @@ match_clause_to_partition_key(RelOptInfo *rel,
return PARTCLAUSE_NOMATCH; return PARTCLAUSE_NOMATCH;
/* /*
* Matched with this key. Check various properties of the clause to * See if the operator is relevant to the partitioning opfamily.
* see if it can sanely be used for partition pruning (this is *
* identical to the logic for a plain OpExpr). * In case of NOT IN (..), we get a '<>', which we handle if list
* partitioning is in use and we're able to confirm that it's negator
* is a btree equality operator belonging to the partitioning operator
* family. As above, report NOMATCH for non-matching operator.
*/ */
if (context->forplanner) if (!op_in_opfamily(saop_op, partopfamily))
{
Oid negator;
if (part_scheme->strategy != PARTITION_STRATEGY_LIST)
return PARTCLAUSE_NOMATCH;
negator = get_negator(saop_op);
if (OidIsValid(negator) && op_in_opfamily(negator, partopfamily))
{ {
int strategy;
Oid lefttype,
righttype;
get_op_opfamily_properties(negator, partopfamily,
false, &strategy,
&lefttype, &righttype);
if (strategy != BTEqualStrategyNumber)
return PARTCLAUSE_NOMATCH;
}
else
return PARTCLAUSE_NOMATCH; /* no useful negator */
}
/* /*
* When pruning in the planner, we only support pruning using * Only allow strict operators. This will guarantee nulls are
* comparisons to constants. Immutable subexpressions will have * filtered. (This test is likely useless, since btree and hash
* been folded to constants already, and we cannot prune on the * comparison operators are generally strict.)
* basis of anything that's not immutable.
*/ */
if (!IsA(rightop, Const)) if (!op_strict(saop_op))
return PARTCLAUSE_UNSUPPORTED; return PARTCLAUSE_UNSUPPORTED;
/* /*
* Also, the comparison operator itself must be immutable. * OK, we have a match to the partition key and a suitable operator.
* Examine the array argument to see if it's usable for pruning. This
* is identical to the logic for a plain OpExpr.
*/ */
if (op_volatile(saop_op) != PROVOLATILE_IMMUTABLE) if (!IsA(rightop, Const))
return PARTCLAUSE_UNSUPPORTED;
}
else
{ {
Bitmapset *paramids;
/* /*
* Otherwise, non-consts are allowed, but we can't prune using an * When pruning in the planner, we only support pruning using
* expression that contains Vars. * comparisons to constants. We cannot prune on the basis of
* anything that's not immutable. (Note that has_mutable_arg and
* has_exec_param do not get set for this target value.)
*/
if (context->target == PARTTARGET_PLANNER)
return PARTCLAUSE_UNSUPPORTED;
/*
* We can never prune using an expression that contains Vars.
*/ */
if (contain_var_clause((Node *) rightop)) if (contain_var_clause((Node *) rightop))
return PARTCLAUSE_UNSUPPORTED; return PARTCLAUSE_UNSUPPORTED;
/* /*
* And we must reject anything containing a volatile function. * And we must reject anything containing a volatile function.
* Stable functions are OK though. (We need not check this for * Stable functions are OK though.
* the comparison operator itself: anything that belongs to a
* partitioning operator family must be at least stable.)
*/ */
if (contain_volatile_functions((Node *) rightop)) if (contain_volatile_functions((Node *) rightop))
return PARTCLAUSE_UNSUPPORTED; return PARTCLAUSE_UNSUPPORTED;
}
/* /*
* Only allow strict operators. This will guarantee nulls are * See if there are any exec Params. If so, we can only use this
* filtered. * expression during per-scan pruning.
*/ */
if (!op_strict(saop_op)) paramids = pull_exec_paramids(rightop);
if (!bms_is_empty(paramids))
{
context->has_exec_param = true;
if (context->target != PARTTARGET_EXEC)
return PARTCLAUSE_UNSUPPORTED; return PARTCLAUSE_UNSUPPORTED;
}
else
{
/* It's potentially usable, but mutable */
context->has_mutable_arg = true;
}
}
/* /*
* In case of NOT IN (..), we get a '<>', which we handle if list * Check whether the comparison operator itself is immutable. (We
* partitioning is in use and we're able to confirm that it's negator * assume anything that's in a btree or hash opclass is at least
* is a btree equality operator belonging to the partitioning operator * stable, but we need to check for immutability.)
* family. As above, report NOMATCH for non-matching operator.
*/ */
if (!op_in_opfamily(saop_op, partopfamily)) if (op_volatile(saop_op) != PROVOLATILE_IMMUTABLE)
{
Oid negator;
if (part_scheme->strategy != PARTITION_STRATEGY_LIST)
return PARTCLAUSE_NOMATCH;
negator = get_negator(saop_op);
if (OidIsValid(negator) && op_in_opfamily(negator, partopfamily))
{ {
int strategy; context->has_mutable_op = true;
Oid lefttype,
righttype;
get_op_opfamily_properties(negator, partopfamily, /*
false, &strategy, * When pruning in the planner, we cannot prune with mutable
&lefttype, &righttype); * operators.
if (strategy != BTEqualStrategyNumber) */
return PARTCLAUSE_NOMATCH; if (context->target == PARTTARGET_PLANNER)
} return PARTCLAUSE_UNSUPPORTED;
else
return PARTCLAUSE_NOMATCH; /* no useful negator */
} }
/* /*
* First generate a list of Const nodes, one for each array element * Examine the contents of the array argument.
* (excepting nulls).
*/ */
elem_exprs = NIL; elem_exprs = NIL;
if (IsA(rightop, Const)) if (IsA(rightop, Const))
{ {
/*
* For a constant array, convert the elements to a list of Const
* nodes, one for each array element (excepting nulls).
*/
Const *arr = (Const *) rightop; Const *arr = (Const *) rightop;
ArrayType *arrval = DatumGetArrayTypeP(arr->constvalue); ArrayType *arrval = DatumGetArrayTypeP(arr->constvalue);
int16 elemlen; int16 elemlen;
...@@ -1968,6 +2088,9 @@ match_clause_to_partition_key(RelOptInfo *rel, ...@@ -1968,6 +2088,9 @@ match_clause_to_partition_key(RelOptInfo *rel,
if (arrexpr->multidims) if (arrexpr->multidims)
return PARTCLAUSE_UNSUPPORTED; return PARTCLAUSE_UNSUPPORTED;
/*
* Otherwise, we can just use the list of element values.
*/
elem_exprs = arrexpr->elements; elem_exprs = arrexpr->elements;
} }
else else
...@@ -2000,10 +2123,8 @@ match_clause_to_partition_key(RelOptInfo *rel, ...@@ -2000,10 +2123,8 @@ match_clause_to_partition_key(RelOptInfo *rel,
elem_clauses = list_make1(makeBoolExpr(OR_EXPR, elem_clauses, -1)); elem_clauses = list_make1(makeBoolExpr(OR_EXPR, elem_clauses, -1));
/* Finally, generate steps */ /* Finally, generate steps */
*clause_steps = *clause_steps = gen_partprune_steps_internal(context, elem_clauses);
gen_partprune_steps_internal(context, rel, elem_clauses, if (context->contradictory)
&contradictory);
if (contradictory)
return PARTCLAUSE_MATCH_CONTRADICT; return PARTCLAUSE_MATCH_CONTRADICT;
else if (*clause_steps == NIL) else if (*clause_steps == NIL)
return PARTCLAUSE_UNSUPPORTED; /* step generation failed */ return PARTCLAUSE_UNSUPPORTED; /* step generation failed */
...@@ -2985,89 +3106,38 @@ pull_exec_paramids_walker(Node *node, Bitmapset **context) ...@@ -2985,89 +3106,38 @@ pull_exec_paramids_walker(Node *node, Bitmapset **context)
} }
/* /*
* analyze_partkey_exprs * get_partkey_exec_paramids
* Loop through all pruning steps and identify which ones require * Loop through given pruning steps and find out which exec Params
* executor startup-time or executor run-time pruning. * are used.
*
* Returns true if any executor partition pruning should be attempted at this
* level. Also fills fields of *pinfo to record how to process each step.
* *
* Note: when this is called, not much of *pinfo is valid; but that's OK * Returns a Bitmapset of Param IDs.
* since we only use it as an output area.
*/ */
static bool static Bitmapset *
analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps, get_partkey_exec_paramids(List *steps)
int partnatts)
{ {
bool doruntimeprune = false; Bitmapset *execparamids = NULL;
ListCell *lc; ListCell *lc;
/*
* Steps require run-time pruning if they contain EXEC_PARAM Params.
* Otherwise, if their expressions aren't simple Consts or they involve
* non-immutable comparison operators, they require startup-time pruning.
* (Otherwise, the pruning would have been done at plan time.)
*
* Notice that what we actually check for mutability is the comparison
* functions, not the original operators. This relies on the support
* functions of the btree or hash opfamily being marked consistently with
* the operators.
*/
pinfo->nexprs = list_length(steps) * partnatts;
pinfo->hasexecparam = (bool *) palloc0(sizeof(bool) * pinfo->nexprs);
pinfo->do_initial_prune = false;
pinfo->do_exec_prune = false;
pinfo->execparamids = NULL;
foreach(lc, steps) foreach(lc, steps)
{ {
PartitionPruneStepOp *step = (PartitionPruneStepOp *) lfirst(lc); PartitionPruneStepOp *step = (PartitionPruneStepOp *) lfirst(lc);
ListCell *lc2; ListCell *lc2;
ListCell *lc3;
int keyno;
if (!IsA(step, PartitionPruneStepOp)) if (!IsA(step, PartitionPruneStepOp))
continue; continue;
keyno = 0; foreach(lc2, step->exprs)
Assert(list_length(step->exprs) == list_length(step->cmpfns));
forboth(lc2, step->exprs, lc3, step->cmpfns)
{ {
Expr *expr = lfirst(lc2); Expr *expr = lfirst(lc2);
Oid fnoid = lfirst_oid(lc3);
/* We can be quick for plain Consts */
if (!IsA(expr, Const)) if (!IsA(expr, Const))
{ execparamids = bms_join(execparamids,
Bitmapset *execparamids = pull_exec_paramids(expr); pull_exec_paramids(expr));
bool hasexecparams;
int stateidx = PruneCxtStateIdx(partnatts,
step->step.step_id,
keyno);
Assert(stateidx < pinfo->nexprs);
hasexecparams = !bms_is_empty(execparamids);
pinfo->hasexecparam[stateidx] = hasexecparams;
pinfo->execparamids = bms_join(pinfo->execparamids,
execparamids);
if (hasexecparams)
pinfo->do_exec_prune = true;
else
pinfo->do_initial_prune = true;
doruntimeprune = true;
}
else if (func_volatile(fnoid) != PROVOLATILE_IMMUTABLE)
{
/* No exec params here, but must do initial pruning */
pinfo->do_initial_prune = true;
doruntimeprune = true;
}
keyno++;
} }
} }
return doruntimeprune; return execparamids;
} }
/* /*
...@@ -3125,19 +3195,18 @@ perform_pruning_base_step(PartitionPruneContext *context, ...@@ -3125,19 +3195,18 @@ perform_pruning_base_step(PartitionPruneContext *context,
Expr *expr; Expr *expr;
Datum datum; Datum datum;
bool isnull; bool isnull;
Oid cmpfn;
expr = lfirst(lc1); expr = lfirst(lc1);
stateidx = PruneCxtStateIdx(context->partnatts, stateidx = PruneCxtStateIdx(context->partnatts,
opstep->step.step_id, keyno); opstep->step.step_id, keyno);
if (partkey_datum_from_expr(context, expr, stateidx, partkey_datum_from_expr(context, expr, stateidx,
&datum, &isnull)) &datum, &isnull);
{
Oid cmpfn;
/* /*
* Since we only allow strict operators in pruning steps, any * Since we only allow strict operators in pruning steps, any
* null-valued comparison value must cause the comparison to * null-valued comparison value must cause the comparison to fail,
* fail, so that no partitions could match. * so that no partitions could match.
*/ */
if (isnull) if (isnull)
{ {
...@@ -3157,10 +3226,10 @@ perform_pruning_base_step(PartitionPruneContext *context, ...@@ -3157,10 +3226,10 @@ perform_pruning_base_step(PartitionPruneContext *context,
if (cmpfn != context->stepcmpfuncs[stateidx].fn_oid) if (cmpfn != context->stepcmpfuncs[stateidx].fn_oid)
{ {
/* /*
* If the needed support function is the same one cached * If the needed support function is the same one cached in
* in the relation's partition key, copy the cached * the relation's partition key, copy the cached FmgrInfo.
* FmgrInfo. Otherwise (i.e., when we have a cross-type * Otherwise (i.e., when we have a cross-type comparison), an
* comparison), an actual lookup is required. * actual lookup is required.
*/ */
if (cmpfn == context->partsupfunc[keyno].fn_oid) if (cmpfn == context->partsupfunc[keyno].fn_oid)
fmgr_info_copy(&context->stepcmpfuncs[stateidx], fmgr_info_copy(&context->stepcmpfuncs[stateidx],
...@@ -3173,7 +3242,6 @@ perform_pruning_base_step(PartitionPruneContext *context, ...@@ -3173,7 +3242,6 @@ perform_pruning_base_step(PartitionPruneContext *context,
values[keyno] = datum; values[keyno] = datum;
nvalues++; nvalues++;
}
lc1 = lnext(lc1); lc1 = lnext(lc1);
lc2 = lnext(lc2); lc2 = lnext(lc2);
...@@ -3392,16 +3460,17 @@ match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey, ...@@ -3392,16 +3460,17 @@ match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey,
* partkey_datum_from_expr * partkey_datum_from_expr
* Evaluate expression for potential partition pruning * Evaluate expression for potential partition pruning
* *
* Evaluate 'expr', whose ExprState is stateidx of the context exprstate * Evaluate 'expr'; set *value and *isnull to the resulting Datum and nullflag.
* array; set *value and *isnull to the resulting Datum and nullflag. *
* Return true if evaluation was possible, otherwise false. * If expr isn't a Const, its ExprState is in stateidx of the context
* exprstate array.
* *
* Note that the evaluated result may be in the per-tuple memory context of * Note that the evaluated result may be in the per-tuple memory context of
* context->planstate->ps_ExprContext, and we may have leaked other memory * context->planstate->ps_ExprContext, and we may have leaked other memory
* there too. This memory must be recovered by resetting that ExprContext * there too. This memory must be recovered by resetting that ExprContext
* after we're done with the pruning operation (see execPartition.c). * after we're done with the pruning operation (see execPartition.c).
*/ */
static bool static void
partkey_datum_from_expr(PartitionPruneContext *context, partkey_datum_from_expr(PartitionPruneContext *context,
Expr *expr, int stateidx, Expr *expr, int stateidx,
Datum *value, bool *isnull) Datum *value, bool *isnull)
...@@ -3413,35 +3482,20 @@ partkey_datum_from_expr(PartitionPruneContext *context, ...@@ -3413,35 +3482,20 @@ partkey_datum_from_expr(PartitionPruneContext *context,
*value = con->constvalue; *value = con->constvalue;
*isnull = con->constisnull; *isnull = con->constisnull;
return true;
} }
else else
{ {
ExprState *exprstate;
ExprContext *ectx;
/* /*
* We should never see a non-Const in a step unless we're running in * We should never see a non-Const in a step unless we're running in
* the executor. * the executor.
*/ */
Assert(context->planstate != NULL); Assert(context->planstate != NULL);
/*
* When called from the executor we'll have a valid planstate so we
* may be able to evaluate an 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 to evaluate expressions containing PARAM_EXEC
* Params only when told it's OK.
*/
if (context->evalexecparams || !context->exprhasexecparam[stateidx])
{
ExprState *exprstate;
ExprContext *ectx;
exprstate = context->exprstates[stateidx]; exprstate = context->exprstates[stateidx];
ectx = context->planstate->ps_ExprContext; ectx = context->planstate->ps_ExprContext;
*value = ExecEvalExprSwitchContext(exprstate, ectx, isnull); *value = ExecEvalExprSwitchContext(exprstate, ectx, isnull);
return true;
}
} }
return false;
} }
...@@ -58,28 +58,30 @@ typedef struct PartitionRoutingInfo ...@@ -58,28 +58,30 @@ typedef struct PartitionRoutingInfo
* PartitionedRelPruneInfo (see plannodes.h); though note that here, * PartitionedRelPruneInfo (see plannodes.h); though note that here,
* subpart_map contains indexes into PartitionPruningData.partrelprunedata[]. * subpart_map contains indexes into PartitionPruningData.partrelprunedata[].
* *
* nparts Length of subplan_map[] and subpart_map[].
* subplan_map Subplan index by partition index, or -1. * subplan_map Subplan index by partition index, or -1.
* subpart_map Subpart index by partition index, or -1. * subpart_map Subpart index by partition index, or -1.
* present_parts A Bitmapset of the partition indexes that we * present_parts A Bitmapset of the partition indexes that we
* have subplans or subparts for. * have subplans or subparts for.
* context Contains the context details required to call * initial_pruning_steps List of PartitionPruneSteps used to
* the partition pruning code. * perform executor startup pruning.
* pruning_steps List of PartitionPruneSteps used to * exec_pruning_steps List of PartitionPruneSteps used to
* perform the actual pruning. * perform per-scan pruning.
* do_initial_prune true if pruning should be performed during * initial_context If initial_pruning_steps isn't NIL, contains
* executor startup (for this partitioning level). * the details needed to execute those steps.
* do_exec_prune true if pruning should be performed during * exec_context If exec_pruning_steps isn't NIL, contains
* executor run (for this partitioning level). * the details needed to execute those steps.
*/ */
typedef struct PartitionedRelPruningData typedef struct PartitionedRelPruningData
{ {
int nparts;
int *subplan_map; int *subplan_map;
int *subpart_map; int *subpart_map;
Bitmapset *present_parts; Bitmapset *present_parts;
PartitionPruneContext context; List *initial_pruning_steps;
List *pruning_steps; List *exec_pruning_steps;
bool do_initial_prune; PartitionPruneContext initial_context;
bool do_exec_prune; PartitionPruneContext exec_context;
} PartitionedRelPruningData; } PartitionedRelPruningData;
/* /*
......
...@@ -1109,21 +1109,23 @@ typedef struct PartitionedRelPruneInfo ...@@ -1109,21 +1109,23 @@ typedef struct PartitionedRelPruneInfo
{ {
NodeTag type; NodeTag type;
Index rtindex; /* RT index of partition rel for this level */ Index rtindex; /* RT index of partition rel for this level */
List *pruning_steps; /* List of PartitionPruneStep, see below */
Bitmapset *present_parts; /* Indexes of all partitions which subplans or Bitmapset *present_parts; /* Indexes of all partitions which subplans or
* subparts are present for. */ * subparts are present for */
int nparts; /* Length of subplan_map[] and subpart_map[] */ int nparts; /* Length of the following arrays: */
int nexprs; /* Length of hasexecparam[] */
int *subplan_map; /* subplan index by partition index, or -1 */ int *subplan_map; /* subplan index by partition index, or -1 */
int *subpart_map; /* subpart index by partition index, or -1 */ int *subpart_map; /* subpart index by partition index, or -1 */
Oid *relid_map; /* relation OID by partition index, or 0 */ Oid *relid_map; /* relation OID by partition index, or 0 */
bool *hasexecparam; /* true if corresponding pruning_step contains
* any PARAM_EXEC Params. */ /*
bool do_initial_prune; /* true if pruning should be performed * initial_pruning_steps shows how to prune during executor startup (i.e.,
* during executor startup. */ * without use of any PARAM_EXEC Params); it is NIL if no startup pruning
bool do_exec_prune; /* true if pruning should be performed during * is required. exec_pruning_steps shows how to prune with PARAM_EXEC
* executor run. */ * Params; it is NIL if no per-scan pruning is required.
Bitmapset *execparamids; /* All PARAM_EXEC Param IDs in pruning_steps */ */
List *initial_pruning_steps; /* List of PartitionPruneStep */
List *exec_pruning_steps; /* List of PartitionPruneStep */
Bitmapset *execparamids; /* All PARAM_EXEC Param IDs in
* exec_pruning_steps */
} PartitionedRelPruneInfo; } PartitionedRelPruneInfo;
/* /*
......
...@@ -44,10 +44,6 @@ struct RelOptInfo; ...@@ -44,10 +44,6 @@ struct RelOptInfo;
* exprstates Array of ExprStates, indexed as per PruneCtxStateIdx; one * exprstates Array of ExprStates, indexed as per PruneCtxStateIdx; one
* for each partition key in each pruning step. Allocated if * for each partition key in each pruning step. Allocated if
* planstate is non-NULL, otherwise NULL. * planstate is non-NULL, otherwise NULL.
* exprhasexecparam Array of bools, each true if corresponding 'exprstate'
* expression contains any PARAM_EXEC Params. (Can be NULL
* if planstate is NULL.)
* evalexecparams True if it's safe to evaluate PARAM_EXEC Params.
*/ */
typedef struct PartitionPruneContext typedef struct PartitionPruneContext
{ {
...@@ -61,8 +57,6 @@ typedef struct PartitionPruneContext ...@@ -61,8 +57,6 @@ typedef struct PartitionPruneContext
MemoryContext ppccontext; MemoryContext ppccontext;
PlanState *planstate; PlanState *planstate;
ExprState **exprstates; ExprState **exprstates;
bool *exprhasexecparam;
bool evalexecparams;
} PartitionPruneContext; } PartitionPruneContext;
/* /*
......
...@@ -3149,6 +3149,45 @@ select * from mc3p where a < 3 and abs(b) = 1; ...@@ -3149,6 +3149,45 @@ select * from mc3p where a < 3 and abs(b) = 1;
Filter: ((a < 3) AND (abs(b) = 1)) Filter: ((a < 3) AND (abs(b) = 1))
(7 rows) (7 rows)
--
-- Check that pruning with composite range partitioning works correctly when
-- a combination of runtime parameters is specified, not all of whose values
-- are available at the same time
--
set plan_cache_mode = force_generic_plan;
prepare ps1 as
select * from mc3p where a = $1 and abs(b) < (select 3);
explain (analyze, costs off, summary off, timing off)
execute ps1(1);
QUERY PLAN
-------------------------------------------------
Append (actual rows=1 loops=1)
InitPlan 1 (returns $0)
-> Result (actual rows=1 loops=1)
Subplans Removed: 2
-> Seq Scan on mc3p1 (actual rows=1 loops=1)
Filter: ((a = $1) AND (abs(b) < $0))
(6 rows)
deallocate ps1;
prepare ps2 as
select * from mc3p where a <= $1 and abs(b) < (select 3);
explain (analyze, costs off, summary off, timing off)
execute ps2(1);
QUERY PLAN
-------------------------------------------------
Append (actual rows=2 loops=1)
InitPlan 1 (returns $0)
-> Result (actual rows=1 loops=1)
Subplans Removed: 1
-> Seq Scan on mc3p0 (actual rows=1 loops=1)
Filter: ((a <= $1) AND (abs(b) < $0))
-> Seq Scan on mc3p1 (actual rows=1 loops=1)
Filter: ((a <= $1) AND (abs(b) < $0))
(8 rows)
deallocate ps2;
reset plan_cache_mode;
drop table mc3p; drop table mc3p;
-- Ensure runtime pruning works with initplans params with boolean types -- Ensure runtime pruning works with initplans params with boolean types
create table boolvalues (value bool not null); create table boolvalues (value bool not null);
......
...@@ -809,6 +809,24 @@ insert into mc3p values (0, 1, 1), (1, 1, 1), (2, 1, 1); ...@@ -809,6 +809,24 @@ insert into mc3p values (0, 1, 1), (1, 1, 1), (2, 1, 1);
explain (analyze, costs off, summary off, timing off) explain (analyze, costs off, summary off, timing off)
select * from mc3p where a < 3 and abs(b) = 1; select * from mc3p where a < 3 and abs(b) = 1;
--
-- Check that pruning with composite range partitioning works correctly when
-- a combination of runtime parameters is specified, not all of whose values
-- are available at the same time
--
set plan_cache_mode = force_generic_plan;
prepare ps1 as
select * from mc3p where a = $1 and abs(b) < (select 3);
explain (analyze, costs off, summary off, timing off)
execute ps1(1);
deallocate ps1;
prepare ps2 as
select * from mc3p where a <= $1 and abs(b) < (select 3);
explain (analyze, costs off, summary off, timing off)
execute ps2(1);
deallocate ps2;
reset plan_cache_mode;
drop table mc3p; drop table mc3p;
-- Ensure runtime pruning works with initplans params with boolean types -- Ensure runtime pruning works with initplans params with boolean types
......
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