146 int *relid_subplan_map,
157 List *source_stepids,
162 Expr *clause,
Expr *partkey,
int partkeyidx,
163 bool *clause_is_not_null,
206 Expr *expr,
int stateidx,
234 int *relid_subplan_map;
254 foreach(lc, subpaths)
279 appinfo = root->append_rel_array[prel->
relid];
285 if (prel == parentrel)
300 relid_subplan_map[pathrel->
relid] =
i;
311 foreach(lc, allpartrelids)
324 if (pinfolist !=
NIL)
326 prunerelinfos =
lappend(prunerelinfos, pinfolist);
327 allmatchedsubplans =
bms_join(matchedsubplans,
332 pfree(relid_subplan_map);
338 if (prunerelinfos ==
NIL)
411 foreach(lc, allpartrelids)
416 if (targetpart == currtarget)
420 lfirst(lc) = currpartrelids;
421 return allpartrelids;
425 return lappend(allpartrelids, partrelids);
450 int *relid_subplan_map,
455 bool doruntimeprune =
false;
456 int *relid_subpart_map;
479 List *initial_pruning_steps;
480 List *exec_pruning_steps;
492 Assert(rti < root->simple_rel_array_size);
493 relid_subpart_map[rti] =
i++;
502 targetpart = subpart;
527 partprunequal = prunequal;
536 partprunequal = (
List *)
573 initial_pruning_steps = context.
steps;
575 initial_pruning_steps =
NIL;
593 exec_pruning_steps = context.
steps;
603 exec_pruning_steps =
NIL;
608 exec_pruning_steps =
NIL;
612 if (initial_pruning_steps || exec_pruning_steps)
613 doruntimeprune =
true;
623 pinfolist =
lappend(pinfolist, pinfo);
629 pfree(relid_subpart_map);
641 foreach(lc, pinfolist)
646 int nparts = subpart->
nparts;
657 subplan_map = (
int *)
palloc(nparts *
sizeof(
int));
658 memset(subplan_map, -1, nparts *
sizeof(
int));
659 subpart_map = (
int *)
palloc(nparts *
sizeof(
int));
660 memset(subpart_map, -1, nparts *
sizeof(
int));
662 present_parts = NULL;
673 subplan_map[
i] = subplanidx = relid_subplan_map[partrel->
relid] - 1;
674 subpart_map[
i] = subpartidx = relid_subpart_map[partrel->
relid] - 1;
683 else if (subpartidx >= 0)
697 pinfo->subplan_map = subplan_map;
698 pinfo->subpart_map = subpart_map;
699 pinfo->relid_map = relid_map;
702 pfree(relid_subpart_map);
704 *matchedsubplans = subplansfound;
766 Assert(rel->part_scheme != NULL);
788 pruning_steps = gcontext.
steps;
791 if (pruning_steps ==
NIL)
795 context.
strategy = rel->part_scheme->strategy;
796 context.
partnatts = rel->part_scheme->partnatts;
800 context.
partsupfunc = rel->part_scheme->partsupfunc;
852 foreach(lc, pruning_steps)
858 case T_PartitionPruneStepOp:
864 case T_PartitionPruneStepCombine:
865 results[step->step_id] =
872 elog(
ERROR,
"invalid pruning step type: %d",
882 final_result = results[num_steps - 1];
883 Assert(final_result != NULL);
891 Assert(i < context->boundinfo->nindexes);
977 bool generate_opsteps =
false;
1000 memset(keyclauses, 0,
sizeof(keyclauses));
1001 foreach(lc, clauses)
1012 (((
Const *) clause)->constisnull ||
1033 bool all_args_contradictory =
true;
1049 bool arg_contradictory;
1058 if (arg_contradictory)
1064 all_args_contradictory =
false;
1066 if (argsteps !=
NIL)
1097 if (all_args_contradictory)
1103 if (arg_stepids !=
NIL)
1109 result =
lappend(result, step);
1136 if (argsteps !=
NIL)
1156 bool clause_is_not_null =
false;
1162 &clause_is_not_null,
1163 &pc, &clause_steps))
1177 generate_opsteps =
true;
1178 keyclauses[
i] =
lappend(keyclauses[
i], pc);
1182 if (!clause_is_not_null)
1189 keyclauses[
i] !=
NIL)
1268 false,
NIL,
NIL, nullkeys);
1269 result =
lappend(result, step);
1271 else if (generate_opsteps)
1286 result =
lappend(result, step);
1308 result =
lappend(result,
final);
1338 opstep->
exprs = exprs;
1356 List *source_stepids,
1402 memset(btree_clauses, 0,
sizeof(btree_clauses));
1403 memset(hash_clauses, 0,
sizeof(hash_clauses));
1406 List *clauselist = keyclauses[
i];
1407 bool consider_next_key =
true;
1426 foreach(lc, clauselist)
1455 consider_next_key =
false;
1460 elog(
ERROR,
"invalid clause for hash partitioning");
1466 elog(
ERROR,
"invalid partition strategy: %c",
1476 if (!consider_next_key)
1511 foreach(lc, btree_clauses[strat])
1520 bool prefix_valid =
true;
1521 bool pk_has_clauses;
1555 for (keyno = 0; keyno < pc->
keyno; keyno++)
1557 pk_has_clauses =
false;
1567 if (eqpc->
keyno == keyno)
1569 prefix =
lappend(prefix, eqpc);
1570 pk_has_clauses =
true;
1592 if (lepc->
keyno == keyno)
1594 prefix =
lappend(prefix, lepc);
1595 pk_has_clauses =
true;
1618 if (gepc->
keyno == keyno)
1620 prefix =
lappend(prefix, gepc);
1621 pk_has_clauses =
true;
1636 if (!pk_has_clauses)
1638 prefix_valid =
false;
1683 if (eq_clauses !=
NIL)
1697 pc =
llast(eq_clauses);
1704 last_keyno = pc->
keyno;
1705 foreach(lc, eq_clauses)
1708 if (pc->
keyno == last_keyno)
1750 elog(
ERROR,
"invalid partition strategy: %c",
1767 #define PartCollMatchesExprColl(partcoll, exprcoll) \
1768 ((partcoll) == InvalidOid || (partcoll) == (exprcoll))
1803 Expr *clause,
Expr *partkey,
int partkeyidx,
1805 List **clause_steps)
1824 partclause->
keyno = partkeyidx;
1826 partclause->
opno = BooleanEqualOperator;
1828 partclause->
expr = expr;
1849 bool is_opne_listp =
false;
1858 opno = opclause->
opno;
1861 if (
equal(leftop, partkey))
1863 else if (
equal(rightop, partkey))
1904 &op_strategy, &op_lefttype,
1917 &op_strategy, &op_lefttype,
1920 is_opne_listp =
true;
2050 op_righttype, op_righttype,
2055 elog(
ERROR,
"invalid partition strategy: %c",
2069 partclause->
keyno = partkeyidx;
2073 partclause->
opno = negator;
2079 partclause->
opno = opno;
2083 partclause->
expr = expr;
2084 partclause->
cmpfn = cmpfn;
2094 Oid saop_coll = saop->inputcollid;
2105 if (!
equal(leftop, partkey) ||
2133 &lefttype, &righttype);
2203 if (
op_volatile(saop_op) != PROVOLATILE_IMMUTABLE)
2236 if (arr->constisnull)
2241 &elemlen, &elembyval, &elemalign);
2244 elemlen, elembyval, elemalign,
2245 &elem_values, &elem_nulls,
2247 for (
i = 0;
i < num_elems;
i++)
2264 arr->constcollid, elemlen,
2265 elem_values[
i],
false, elembyval);
2266 elem_exprs =
lappend(elem_exprs, elem_expr);
2278 if (arrexpr->multidims)
2297 foreach(lc1, elem_exprs)
2304 elem_clauses =
lappend(elem_clauses, elem_clause);
2318 else if (*clause_steps ==
NIL)
2353 return boolmatchstatus;
2382 Expr *step_lastexpr,
2388 Assert(step_nullkeys == NULL ||
2434 Expr *step_lastexpr,
2453 if (cur_keyno < step_lastkeyno - 1)
2468 if (pc->
keyno > cur_keyno)
2480 if (pc->
keyno == cur_keyno)
2532 Assert(context->
rel->part_scheme->strategy
2535 context->
rel->part_scheme->partnatts);
2548 step_exprs1 =
lappend(step_exprs1, step_lastexpr);
2553 step_cmpfns1 =
lappend_oid(step_cmpfns1, step_lastcmpfn);
2556 step_opstrategy, step_op_is_ne,
2557 step_exprs1, step_cmpfns1,
2559 result =
lappend(result, step);
2596 int *partindices = boundinfo->
indexes;
2601 int greatest_modulus;
2619 for (
i = 0;
i < partnatts;
i++)
2625 greatest_modulus = boundinfo->
nindexes;
2626 if (partindices[rowHash % greatest_modulus] >= 0)
2677 bool inclusive =
false;
2710 maxoff = boundinfo->
ndatums - 1;
2738 if (off >= 0 && is_equal)
2771 if (off >= 0 && is_equal)
2791 if (!is_equal || !inclusive)
2809 if (off > boundinfo->
ndatums - 1)
2823 if (off >= 0 && is_equal && !inclusive)
2839 elog(
ERROR,
"invalid strategy number %d", opstrategy);
2843 Assert(minoff >= 0 && maxoff >= 0);
2886 int *partindices = boundinfo->
indexes;
2891 bool inclusive =
false;
2894 Assert(nvalues <= partnatts);
2920 if (partindices[minoff] < 0)
2922 if (partindices[maxoff] < 0)
2926 Assert(partindices[minoff] >= 0 &&
2927 partindices[maxoff] >= 0);
2937 if (nvalues < partnatts)
2950 if (off >= 0 && is_equal)
2952 if (nvalues == partnatts)
2960 int saved_off = off;
2981 boundinfo->
datums[off - 1],
2982 boundinfo->
kind[off - 1],
2993 boundinfo->
kind[off],
3004 if (boundinfo->
kind[off][nvalues] ==
3015 while (off < boundinfo->ndatums - 1)
3021 boundinfo->
datums[off + 1],
3022 boundinfo->
kind[off + 1],
3033 boundinfo->
kind[off],
3043 Assert(minoff >= 0 && maxoff >= 0);
3086 if (is_equal && nvalues < partnatts)
3101 while (off >= 1 && off < boundinfo->ndatums - 1)
3106 nextoff = inclusive ? off - 1 : off + 1;
3110 boundinfo->
datums[nextoff],
3111 boundinfo->
kind[nextoff],
3123 boundinfo->
kind[off],
3126 minoff = inclusive ? off : off + 1;
3162 if (is_equal && nvalues < partnatts)
3164 while (off >= 1 && off < boundinfo->ndatums - 1)
3169 nextoff = inclusive ? off + 1 : off - 1;
3172 boundinfo->
datums[nextoff],
3173 boundinfo->
kind[nextoff],
3185 boundinfo->
kind[off],
3188 maxoff = inclusive ? off + 1 : off;
3200 else if (!is_equal || inclusive)
3216 elog(
ERROR,
"invalid strategy number %d", opstrategy);
3220 Assert(minoff >= 0 && minoff <= boundinfo->ndatums);
3221 Assert(maxoff >= 0 && maxoff <= boundinfo->ndatums);
3229 if (minoff < boundinfo->ndatums && partindices[minoff] < 0)
3231 int lastkey = nvalues - 1;
3233 if (boundinfo->
kind[minoff][lastkey] ==
3248 if (maxoff >= 1 && partindices[maxoff] < 0)
3250 int lastkey = nvalues - 1;
3252 if (boundinfo->
kind[maxoff - 1][lastkey] ==
3260 Assert(minoff >= 0 && maxoff >= 0);
3261 if (minoff <= maxoff)
3320 foreach(lc2, step->
exprs)
3326 execparamids =
bms_join(execparamids,
3331 return execparamids;
3367 for (keyno = 0; keyno < context->
partnatts; keyno++)
3473 elog(
ERROR,
"unexpected partition strategy: %d",
3529 elog(
ERROR,
"invalid pruning combine step argument");
3530 step_result = step_results[step_id];
3531 Assert(step_result != NULL);
3553 elog(
ERROR,
"invalid pruning combine step argument");
3554 step_result = step_results[step_id];
3555 Assert(step_result != NULL);
3609 if (!IsBuiltinBooleanOpfamily(partopfamily))
3621 leftop = btest->
arg;
3625 if (
equal(leftop, partkey))
3644 if (
equal(leftop, partkey))
3645 *outconst = is_not_clause ?
3674 Expr *expr,
int stateidx,
3682 *
value = con->constvalue;
3683 *isnull = con->constisnull;
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
#define DatumGetArrayTypeP(X)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
int bms_next_member(const Bitmapset *a, int prevbit)
int bms_num_members(const Bitmapset *a)
bool bms_is_member(int x, const Bitmapset *a)
Bitmapset * bms_make_singleton(int x)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Bitmapset * bms_del_member(Bitmapset *a, int x)
Bitmapset * bms_copy(const Bitmapset *a)
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
static Datum values[MAXATTR]
#define OidIsValid(objectId)
bool contain_volatile_functions(Node *clause)
bool enable_partition_pruning
elog(ERROR, "%s: %s", p2, msg)
bool equal(const void *a, const void *b)
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
#define HASHEXTENDED_PROC
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
List * lappend_int(List *list, int datum)
List * lappend_oid(List *list, Oid datum)
List * list_copy(const List *oldlist)
void list_free(List *list)
List * list_concat(List *list1, const List *list2)
List * list_concat_copy(const List *list1, const List *list2)
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
char op_volatile(Oid opno)
bool op_in_opfamily(Oid opno, Oid opfamily)
Oid get_negator(Oid opno)
Oid get_commutator(Oid opno)
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Node * makeBoolConst(bool value, bool isnull)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
static bool is_andclause(const void *clause)
static Expr * get_notclausearg(const void *notclause)
static bool is_orclause(const void *clause)
static Node * get_rightop(const void *clause)
#define expression_tree_walker(n, w, c)
static bool is_notclause(const void *clause)
static Node * get_leftop(const void *clause)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
@ PARTITION_STRATEGY_HASH
@ PARTITION_STRATEGY_LIST
@ PARTITION_STRATEGY_RANGE
@ PARTITION_RANGE_DATUM_MAXVALUE
@ PARTITION_RANGE_DATUM_MINVALUE
int32 partition_rbound_datum_cmp(FmgrInfo *partsupfunc, Oid *partcollation, Datum *rb_datums, PartitionRangeDatumKind *rb_kind, Datum *tuple_datums, int n_tuple_datums)
uint64 compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *values, bool *isnull)
int partition_range_datum_bsearch(FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, int nvalues, Datum *values, bool *is_equal)
int partition_list_bsearch(FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, Datum value, bool *is_equal)
#define partition_bound_has_default(bi)
#define partition_bound_accepts_nulls(bi)
int make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
@ PARTCLAUSE_MATCH_CONTRADICT
@ PARTCLAUSE_MATCH_CLAUSE
@ PARTCLAUSE_MATCH_NULLNESS
static List * add_part_relids(List *allpartrelids, Bitmapset *partrelids)
static List * get_steps_using_prefix(GeneratePruningStepsContext *context, StrategyNumber step_opstrategy, bool step_op_is_ne, Expr *step_lastexpr, Oid step_lastcmpfn, int step_lastkeyno, Bitmapset *step_nullkeys, List *prefix)
static PartitionPruneStep * gen_prune_step_combine(GeneratePruningStepsContext *context, List *source_stepids, PartitionPruneCombineOp combineOp)
static List * make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *prunequal, Bitmapset *partrelids, int *relid_subplan_map, Bitmapset **matchedsubplans)
static Bitmapset * get_partkey_exec_paramids(List *steps)
Bitmapset * get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
struct PruneStepResult PruneStepResult
static PartClauseMatchStatus match_clause_to_partition_key(GeneratePruningStepsContext *context, Expr *clause, Expr *partkey, int partkeyidx, bool *clause_is_not_null, PartClauseInfo **pc, List **clause_steps)
Bitmapset * prune_append_rel_partitions(RelOptInfo *rel)
static List * get_steps_using_prefix_recurse(GeneratePruningStepsContext *context, StrategyNumber step_opstrategy, bool step_op_is_ne, Expr *step_lastexpr, Oid step_lastcmpfn, int step_lastkeyno, Bitmapset *step_nullkeys, List *prefix, ListCell *start, List *step_exprs, List *step_cmpfns)
static PruneStepResult * get_matching_list_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, Datum value, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys)
static PruneStepResult * get_matching_hash_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, Datum *values, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys)
static List * gen_partprune_steps_internal(GeneratePruningStepsContext *context, List *clauses)
static void gen_partprune_steps(RelOptInfo *rel, List *clauses, PartClauseTarget target, GeneratePruningStepsContext *context)
static void partkey_datum_from_expr(PartitionPruneContext *context, Expr *expr, int stateidx, Datum *value, bool *isnull)
static PartClauseMatchStatus match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey, Expr **outconst)
static List * gen_prune_steps_from_opexps(GeneratePruningStepsContext *context, List **keyclauses, Bitmapset *nullkeys)
static PruneStepResult * get_matching_range_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, Datum *values, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys)
#define PartCollMatchesExprColl(partcoll, exprcoll)
static PruneStepResult * perform_pruning_base_step(PartitionPruneContext *context, PartitionPruneStepOp *opstep)
static PruneStepResult * perform_pruning_combine_step(PartitionPruneContext *context, PartitionPruneStepCombine *cstep, PruneStepResult **step_results)
struct GeneratePruningStepsContext GeneratePruningStepsContext
static Bitmapset * pull_exec_paramids(Expr *expr)
static PartitionPruneStep * gen_prune_step_op(GeneratePruningStepsContext *context, StrategyNumber opstrategy, bool op_is_ne, List *exprs, List *cmpfns, Bitmapset *nullkeys)
struct PartClauseInfo PartClauseInfo
static bool pull_exec_paramids_walker(Node *node, Bitmapset **context)
#define PruneCxtStateIdx(partnatts, step_id, keyno)
#define IS_PARTITIONED_REL(rel)
#define planner_rt_fetch(rti, root)
@ RELOPT_OTHER_MEMBER_REL
#define PARTITION_MAX_KEYS
static int list_length(const List *l)
#define list_make1_oid(x1)
#define for_each_cell(cell, lst, initcell)
static ListCell * list_head(const List *l)
static ListCell * lnext(const List *l, const ListCell *c)
@ PARTPRUNE_COMBINE_INTERSECT
@ PARTPRUNE_COMBINE_UNION
void check_stack_depth(void)
static bool DatumGetBool(Datum X)
bool predicate_refuted_by(List *predicate_list, List *clause_list, bool weak)
Node * negate_clause(Node *node)
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
#define HTMaxStrategyNumber
#define BTGreaterStrategyNumber
#define BTMaxStrategyNumber
#define HTEqualStrategyNumber
#define BTLessStrategyNumber
#define BTEqualStrategyNumber
#define BTLessEqualStrategyNumber
#define BTGreaterEqualStrategyNumber
BoolTestType booltesttype
NullTestType nulltesttype
PartitionRangeDatumKind ** kind
ExprContext * exprcontext
PartitionBoundInfo boundinfo
Bitmapset * other_subplans
Bitmapset * root_parent_relids
PartitionPruneCombineOp combineOp
StrategyNumber opstrategy
struct FmgrInfo * partsupfunc
Bitmapset * present_parts
List * initial_pruning_steps
List * exec_pruning_steps
ExprContext * ps_ExprContext
int simple_rel_array_size
Bitmapset * bound_offsets
bool contain_var_clause(Node *node)