145 int *relid_subplan_map,
156 List *source_stepids,
161 Expr *clause,
Expr *partkey,
int partkeyidx,
162 bool *clause_is_not_null,
204 Expr *expr,
int stateidx,
232 int *relid_subplan_map;
249 relid_subplan_map =
palloc0(
sizeof(
int) *
root->simple_rel_array_size);
252 foreach(lc, subpaths)
277 appinfo =
root->append_rel_array[prel->
relid];
283 if (prel == parentrel)
298 relid_subplan_map[pathrel->
relid] =
i;
309 foreach(lc, allpartrelids)
322 if (pinfolist !=
NIL)
324 prunerelinfos =
lappend(prunerelinfos, pinfolist);
325 allmatchedsubplans =
bms_join(matchedsubplans,
330 pfree(relid_subplan_map);
336 if (prunerelinfos ==
NIL)
409 foreach(lc, allpartrelids)
414 if (targetpart == currtarget)
418 lfirst(lc) = currpartrelids;
419 return allpartrelids;
423 return lappend(allpartrelids, partrelids);
448 int *relid_subplan_map,
453 bool doruntimeprune =
false;
454 int *relid_subpart_map;
468 relid_subpart_map =
palloc0(
sizeof(
int) *
root->simple_rel_array_size);
477 List *initial_pruning_steps;
478 List *exec_pruning_steps;
490 Assert(rti < root->simple_rel_array_size);
491 relid_subpart_map[rti] =
i++;
500 targetpart = subpart;
525 partprunequal = prunequal;
534 partprunequal = (
List *)
571 initial_pruning_steps = context.
steps;
573 initial_pruning_steps =
NIL;
591 exec_pruning_steps = context.
steps;
601 exec_pruning_steps =
NIL;
606 exec_pruning_steps =
NIL;
610 if (initial_pruning_steps || exec_pruning_steps)
611 doruntimeprune =
true;
621 pinfolist =
lappend(pinfolist, pinfo);
627 pfree(relid_subpart_map);
639 foreach(lc, pinfolist)
644 int nparts = subpart->
nparts;
648 int *leafpart_rti_map;
656 subplan_map = (
int *)
palloc(nparts *
sizeof(
int));
657 memset(subplan_map, -1, nparts *
sizeof(
int));
658 subpart_map = (
int *)
palloc(nparts *
sizeof(
int));
659 memset(subpart_map, -1, nparts *
sizeof(
int));
661 leafpart_rti_map = (
int *)
palloc0(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;
696 if (partrel->
nparts == -1)
697 leafpart_rti_map[
i] = (int) partrel->
relid;
702 else if (subpartidx >= 0)
716 pinfo->subplan_map = subplan_map;
717 pinfo->subpart_map = subpart_map;
718 pinfo->relid_map = relid_map;
719 pinfo->leafpart_rti_map = leafpart_rti_map;
722 pfree(relid_subpart_map);
724 *matchedsubplans = subplansfound;
786 Assert(rel->part_scheme != NULL);
808 pruning_steps = gcontext.
steps;
811 if (pruning_steps ==
NIL)
815 context.
strategy = rel->part_scheme->strategy;
816 context.
partnatts = rel->part_scheme->partnatts;
820 context.
partsupfunc = rel->part_scheme->partsupfunc;
872 foreach(lc, pruning_steps)
878 case T_PartitionPruneStepOp:
884 case T_PartitionPruneStepCombine:
885 results[step->step_id] =
892 elog(
ERROR,
"invalid pruning step type: %d",
902 final_result = results[num_steps - 1];
903 Assert(final_result != NULL);
911 Assert(i < context->boundinfo->nindexes);
997 bool generate_opsteps =
false;
1020 memset(keyclauses, 0,
sizeof(keyclauses));
1021 foreach(lc, clauses)
1032 (((
Const *) clause)->constisnull ||
1053 bool all_args_contradictory =
true;
1069 bool arg_contradictory;
1078 if (arg_contradictory)
1084 all_args_contradictory =
false;
1086 if (argsteps !=
NIL)
1117 if (all_args_contradictory)
1123 if (arg_stepids !=
NIL)
1129 result =
lappend(result, step);
1156 if (argsteps !=
NIL)
1176 bool clause_is_not_null =
false;
1182 &clause_is_not_null,
1183 &pc, &clause_steps))
1197 generate_opsteps =
true;
1198 keyclauses[
i] =
lappend(keyclauses[
i], pc);
1202 if (!clause_is_not_null)
1209 keyclauses[
i] !=
NIL)
1288 false,
NIL,
NIL, nullkeys);
1289 result =
lappend(result, step);
1291 else if (generate_opsteps)
1306 result =
lappend(result, step);
1328 result =
lappend(result,
final);
1358 opstep->
exprs = exprs;
1376 List *source_stepids,
1422 memset(btree_clauses, 0,
sizeof(btree_clauses));
1423 memset(hash_clauses, 0,
sizeof(hash_clauses));
1426 List *clauselist = keyclauses[
i];
1427 bool consider_next_key =
true;
1446 foreach(lc, clauselist)
1475 consider_next_key =
false;
1480 elog(
ERROR,
"invalid clause for hash partitioning");
1486 elog(
ERROR,
"invalid partition strategy: %c",
1496 if (!consider_next_key)
1531 foreach(lc, btree_clauses[strat])
1540 bool prefix_valid =
true;
1541 bool pk_has_clauses;
1574 for (keyno = 0; keyno < pc->
keyno; keyno++)
1576 pk_has_clauses =
false;
1586 if (eqpc->
keyno == keyno)
1588 prefix =
lappend(prefix, eqpc);
1589 pk_has_clauses =
true;
1611 if (lepc->
keyno == keyno)
1613 prefix =
lappend(prefix, lepc);
1614 pk_has_clauses =
true;
1637 if (gepc->
keyno == keyno)
1639 prefix =
lappend(prefix, gepc);
1640 pk_has_clauses =
true;
1655 if (!pk_has_clauses)
1657 prefix_valid =
false;
1701 if (eq_clauses !=
NIL)
1715 pc =
llast(eq_clauses);
1722 last_keyno = pc->
keyno;
1723 foreach(lc, eq_clauses)
1726 if (pc->
keyno == last_keyno)
1767 elog(
ERROR,
"invalid partition strategy: %c",
1784#define PartCollMatchesExprColl(partcoll, exprcoll) \
1785 ((partcoll) == InvalidOid || (partcoll) == (exprcoll))
1820 Expr *clause,
Expr *partkey,
int partkeyidx,
1822 List **clause_steps)
1877 nulltest->argisrow =
false;
1880 new_clauses =
list_make2(new_booltest, nulltest);
1888 else if (*clause_steps ==
NIL)
1894 partclause->
keyno = partkeyidx;
1896 partclause->
opno = BooleanEqualOperator;
1898 partclause->
expr = expr;
1913 *clause_is_not_null = notclause;
1928 bool is_opne_listp =
false;
1937 opno = opclause->
opno;
1940 if (
equal(leftop, partkey))
1942 else if (
equal(rightop, partkey))
1985 &op_strategy, &op_lefttype,
1999 &op_strategy, &op_lefttype,
2002 is_opne_listp =
true;
2132 op_righttype, op_righttype,
2137 elog(
ERROR,
"invalid partition strategy: %c",
2151 partclause->
keyno = partkeyidx;
2155 partclause->
opno = negator;
2161 partclause->
opno = opno;
2165 partclause->
expr = expr;
2166 partclause->
cmpfn = cmpfn;
2176 Oid saop_coll = saop->inputcollid;
2187 if (!
equal(leftop, partkey) ||
2215 &lefttype, &righttype);
2285 if (
op_volatile(saop_op) != PROVOLATILE_IMMUTABLE)
2318 if (arr->constisnull)
2323 &elemlen, &elembyval, &elemalign);
2326 elemlen, elembyval, elemalign,
2327 &elem_values, &elem_nulls,
2329 for (
i = 0;
i < num_elems;
i++)
2346 arr->constcollid, elemlen,
2347 elem_values[
i],
false, elembyval);
2348 elem_exprs =
lappend(elem_exprs, elem_expr);
2360 if (arrexpr->multidims)
2366 elem_exprs = arrexpr->elements;
2379 foreach(lc1, elem_exprs)
2386 elem_clauses =
lappend(elem_clauses, elem_clause);
2400 else if (*clause_steps ==
NIL)
2435 return boolmatchstatus;
2470 Expr *step_lastexpr,
2476 Assert(step_nullkeys == NULL ||
2528 Expr *step_lastexpr,
2549 if (cur_keyno < final_keyno)
2563 if (pc->
keyno > cur_keyno)
2582 if (pc->
keyno == cur_keyno)
2634 Assert(context->
rel->part_scheme->strategy
2637 context->
rel->part_scheme->partnatts);
2650 step_exprs1 =
lappend(step_exprs1, step_lastexpr);
2655 step_cmpfns1 =
lappend_oid(step_cmpfns1, step_lastcmpfn);
2658 step_opstrategy, step_op_is_ne,
2659 step_exprs1, step_cmpfns1,
2661 result =
lappend(result, step);
2698 int *partindices = boundinfo->
indexes;
2703 int greatest_modulus;
2721 for (
i = 0;
i < partnatts;
i++)
2727 greatest_modulus = boundinfo->
nindexes;
2728 if (partindices[rowHash % greatest_modulus] >= 0)
2779 bool inclusive =
false;
2812 maxoff = boundinfo->
ndatums - 1;
2840 if (off >= 0 && is_equal)
2873 if (off >= 0 && is_equal)
2893 if (!is_equal || !inclusive)
2911 if (off > boundinfo->
ndatums - 1)
2925 if (off >= 0 && is_equal && !inclusive)
2941 elog(
ERROR,
"invalid strategy number %d", opstrategy);
2945 Assert(minoff >= 0 && maxoff >= 0);
2988 int *partindices = boundinfo->
indexes;
2993 bool inclusive =
false;
2996 Assert(nvalues <= partnatts);
3022 if (partindices[minoff] < 0)
3024 if (partindices[maxoff] < 0)
3028 Assert(partindices[minoff] >= 0 &&
3029 partindices[maxoff] >= 0);
3039 if (nvalues < partnatts)
3052 if (off >= 0 && is_equal)
3054 if (nvalues == partnatts)
3062 int saved_off = off;
3083 boundinfo->
datums[off - 1],
3084 boundinfo->
kind[off - 1],
3095 boundinfo->
kind[off],
3106 if (boundinfo->
kind[off][nvalues] ==
3117 while (off < boundinfo->ndatums - 1)
3123 boundinfo->
datums[off + 1],
3124 boundinfo->
kind[off + 1],
3135 boundinfo->
kind[off],
3145 Assert(minoff >= 0 && maxoff >= 0);
3188 if (is_equal && nvalues < partnatts)
3203 while (off >= 1 && off < boundinfo->ndatums - 1)
3208 nextoff = inclusive ? off - 1 : off + 1;
3212 boundinfo->
datums[nextoff],
3213 boundinfo->
kind[nextoff],
3225 boundinfo->
kind[off],
3228 minoff = inclusive ? off : off + 1;
3264 if (is_equal && nvalues < partnatts)
3266 while (off >= 1 && off < boundinfo->ndatums - 1)
3271 nextoff = inclusive ? off + 1 : off - 1;
3274 boundinfo->
datums[nextoff],
3275 boundinfo->
kind[nextoff],
3287 boundinfo->
kind[off],
3290 maxoff = inclusive ? off + 1 : off;
3302 else if (!is_equal || inclusive)
3318 elog(
ERROR,
"invalid strategy number %d", opstrategy);
3322 Assert(minoff >= 0 && minoff <= boundinfo->ndatums);
3323 Assert(maxoff >= 0 && maxoff <= boundinfo->ndatums);
3331 if (minoff < boundinfo->ndatums && partindices[minoff] < 0)
3333 int lastkey = nvalues - 1;
3335 if (boundinfo->
kind[minoff][lastkey] ==
3350 if (maxoff >= 1 && partindices[maxoff] < 0)
3352 int lastkey = nvalues - 1;
3354 if (boundinfo->
kind[maxoff - 1][lastkey] ==
3362 Assert(minoff >= 0 && maxoff >= 0);
3363 if (minoff <= maxoff)
3421 foreach(lc2, step->
exprs)
3427 execparamids =
bms_join(execparamids,
3432 return execparamids;
3468 for (keyno = 0; keyno < context->
partnatts; keyno++)
3574 elog(
ERROR,
"unexpected partition strategy: %d",
3630 elog(
ERROR,
"invalid pruning combine step argument");
3631 step_result = step_results[step_id];
3632 Assert(step_result != NULL);
3654 elog(
ERROR,
"invalid pruning combine step argument");
3655 step_result = step_results[step_id];
3656 Assert(step_result != NULL);
3702 Expr **outconst,
bool *notclause)
3713 if (!IsBuiltinBooleanOpfamily(partopfamily))
3720 leftop = btest->
arg;
3724 if (
equal(leftop, partkey))
3762 if (
equal(leftop, partkey))
3789 Expr *expr,
int stateidx,
3797 *
value = con->constvalue;
3798 *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_make_singleton(int x)
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
int bms_next_member(const Bitmapset *a, int prevbit)
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Bitmapset * bms_del_member(Bitmapset *a, int x)
int bms_num_members(const Bitmapset *a)
bool bms_is_member(int x, const Bitmapset *a)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Bitmapset * bms_copy(const Bitmapset *a)
static Datum values[MAXATTR]
#define OidIsValid(objectId)
bool contain_volatile_functions(Node *clause)
bool enable_partition_pruning
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(PointerIsAligned(start, uint64))
List * lappend(List *list, void *datum)
List * list_concat(List *list1, const List *list2)
List * list_concat_copy(const List *list1, const List *list2)
List * list_copy(const List *oldlist)
List * lappend_int(List *list, int datum)
List * lappend_oid(List *list, Oid datum)
void list_free(List *list)
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 * makeBoolExpr(BoolExprType boolop, List *args, int location)
Node * makeBoolConst(bool value, bool isnull)
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)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
static bool is_andclause(const void *clause)
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 Expr * get_notclausearg(const void *notclause)
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, const Oid *partcollation, const Datum *values, const 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)
Bitmapset * get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
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 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)
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)
static PruneStepResult * get_matching_list_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, Datum value, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys)
static PartClauseMatchStatus match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey, Expr **outconst, bool *notclause)
static PruneStepResult * get_matching_hash_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, Datum *values, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys)
Bitmapset * prune_append_rel_partitions(RelOptInfo *rel)
static List * get_steps_using_prefix(GeneratePruningStepsContext *context, StrategyNumber step_opstrategy, bool step_op_is_ne, Expr *step_lastexpr, Oid step_lastcmpfn, Bitmapset *step_nullkeys, List *prefix)
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 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)
static List * get_steps_using_prefix_recurse(GeneratePruningStepsContext *context, StrategyNumber step_opstrategy, bool step_op_is_ne, Expr *step_lastexpr, Oid step_lastcmpfn, Bitmapset *step_nullkeys, List *prefix, ListCell *start, List *step_exprs, List *step_cmpfns)
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)
#define list_make2(x1, x2)
@ PARTPRUNE_COMBINE_INTERSECT
@ PARTPRUNE_COMBINE_UNION
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)
void check_stack_depth(void)
#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
PartitionPruneCombineOp combineOp
StrategyNumber opstrategy
struct FmgrInfo * partsupfunc
Bitmapset * present_parts
List * initial_pruning_steps
List * exec_pruning_steps
Bitmapset * bound_offsets
bool contain_var_clause(Node *node)