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;
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;
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;
871 foreach(lc, pruning_steps)
877 case T_PartitionPruneStepOp:
883 case T_PartitionPruneStepCombine:
884 results[step->step_id] =
891 elog(
ERROR,
"invalid pruning step type: %d",
901 final_result = results[num_steps - 1];
902 Assert(final_result != NULL);
910 Assert(i < context->boundinfo->nindexes);
996 bool generate_opsteps =
false;
1019 memset(keyclauses, 0,
sizeof(keyclauses));
1020 foreach(lc, clauses)
1031 (((
Const *) clause)->constisnull ||
1052 bool all_args_contradictory =
true;
1068 bool arg_contradictory;
1077 if (arg_contradictory)
1083 all_args_contradictory =
false;
1085 if (argsteps !=
NIL)
1116 if (all_args_contradictory)
1122 if (arg_stepids !=
NIL)
1128 result =
lappend(result, step);
1155 if (argsteps !=
NIL)
1175 bool clause_is_not_null =
false;
1181 &clause_is_not_null,
1182 &pc, &clause_steps))
1196 generate_opsteps =
true;
1197 keyclauses[
i] =
lappend(keyclauses[
i], pc);
1201 if (!clause_is_not_null)
1208 keyclauses[
i] !=
NIL)
1287 false,
NIL,
NIL, nullkeys);
1288 result =
lappend(result, step);
1290 else if (generate_opsteps)
1305 result =
lappend(result, step);
1327 result =
lappend(result,
final);
1357 opstep->
exprs = exprs;
1375 List *source_stepids,
1421 memset(btree_clauses, 0,
sizeof(btree_clauses));
1422 memset(hash_clauses, 0,
sizeof(hash_clauses));
1425 List *clauselist = keyclauses[
i];
1426 bool consider_next_key =
true;
1445 foreach(lc, clauselist)
1474 consider_next_key =
false;
1479 elog(
ERROR,
"invalid clause for hash partitioning");
1485 elog(
ERROR,
"invalid partition strategy: %c",
1495 if (!consider_next_key)
1530 foreach(lc, btree_clauses[strat])
1539 bool prefix_valid =
true;
1540 bool pk_has_clauses;
1573 for (keyno = 0; keyno < pc->
keyno; keyno++)
1575 pk_has_clauses =
false;
1585 if (eqpc->
keyno == keyno)
1587 prefix =
lappend(prefix, eqpc);
1588 pk_has_clauses =
true;
1610 if (lepc->
keyno == keyno)
1612 prefix =
lappend(prefix, lepc);
1613 pk_has_clauses =
true;
1636 if (gepc->
keyno == keyno)
1638 prefix =
lappend(prefix, gepc);
1639 pk_has_clauses =
true;
1654 if (!pk_has_clauses)
1656 prefix_valid =
false;
1700 if (eq_clauses !=
NIL)
1714 pc =
llast(eq_clauses);
1721 last_keyno = pc->
keyno;
1722 foreach(lc, eq_clauses)
1725 if (pc->
keyno == last_keyno)
1766 elog(
ERROR,
"invalid partition strategy: %c",
1783#define PartCollMatchesExprColl(partcoll, exprcoll) \
1784 ((partcoll) == InvalidOid || (partcoll) == (exprcoll))
1819 Expr *clause,
Expr *partkey,
int partkeyidx,
1821 List **clause_steps)
1876 nulltest->argisrow =
false;
1879 new_clauses =
list_make2(new_booltest, nulltest);
1887 else if (*clause_steps ==
NIL)
1893 partclause->
keyno = partkeyidx;
1895 partclause->
opno = BooleanEqualOperator;
1897 partclause->
expr = expr;
1912 *clause_is_not_null = notclause;
1927 bool is_opne_listp =
false;
1936 opno = opclause->
opno;
1939 if (
equal(leftop, partkey))
1941 else if (
equal(rightop, partkey))
1984 &op_strategy, &op_lefttype,
1998 &op_strategy, &op_lefttype,
2001 is_opne_listp =
true;
2131 op_righttype, op_righttype,
2136 elog(
ERROR,
"invalid partition strategy: %c",
2150 partclause->
keyno = partkeyidx;
2154 partclause->
opno = negator;
2160 partclause->
opno = opno;
2164 partclause->
expr = expr;
2165 partclause->
cmpfn = cmpfn;
2175 Oid saop_coll = saop->inputcollid;
2186 if (!
equal(leftop, partkey) ||
2214 &lefttype, &righttype);
2284 if (
op_volatile(saop_op) != PROVOLATILE_IMMUTABLE)
2317 if (arr->constisnull)
2322 &elemlen, &elembyval, &elemalign);
2325 elemlen, elembyval, elemalign,
2326 &elem_values, &elem_nulls,
2328 for (
i = 0;
i < num_elems;
i++)
2345 arr->constcollid, elemlen,
2346 elem_values[
i],
false, elembyval);
2347 elem_exprs =
lappend(elem_exprs, elem_expr);
2359 if (arrexpr->multidims)
2365 elem_exprs = arrexpr->elements;
2378 foreach(lc1, elem_exprs)
2385 elem_clauses =
lappend(elem_clauses, elem_clause);
2399 else if (*clause_steps ==
NIL)
2434 return boolmatchstatus;
2469 Expr *step_lastexpr,
2475 Assert(step_nullkeys == NULL ||
2527 Expr *step_lastexpr,
2548 if (cur_keyno < final_keyno)
2562 if (pc->
keyno > cur_keyno)
2581 if (pc->
keyno == cur_keyno)
2633 Assert(context->
rel->part_scheme->strategy
2636 context->
rel->part_scheme->partnatts);
2649 step_exprs1 =
lappend(step_exprs1, step_lastexpr);
2654 step_cmpfns1 =
lappend_oid(step_cmpfns1, step_lastcmpfn);
2657 step_opstrategy, step_op_is_ne,
2658 step_exprs1, step_cmpfns1,
2660 result =
lappend(result, step);
2697 int *partindices = boundinfo->
indexes;
2702 int greatest_modulus;
2720 for (
i = 0;
i < partnatts;
i++)
2726 greatest_modulus = boundinfo->
nindexes;
2727 if (partindices[rowHash % greatest_modulus] >= 0)
2778 bool inclusive =
false;
2811 maxoff = boundinfo->
ndatums - 1;
2839 if (off >= 0 && is_equal)
2872 if (off >= 0 && is_equal)
2892 if (!is_equal || !inclusive)
2910 if (off > boundinfo->
ndatums - 1)
2924 if (off >= 0 && is_equal && !inclusive)
2940 elog(
ERROR,
"invalid strategy number %d", opstrategy);
2944 Assert(minoff >= 0 && maxoff >= 0);
2987 int *partindices = boundinfo->
indexes;
2992 bool inclusive =
false;
2995 Assert(nvalues <= partnatts);
3021 if (partindices[minoff] < 0)
3023 if (partindices[maxoff] < 0)
3027 Assert(partindices[minoff] >= 0 &&
3028 partindices[maxoff] >= 0);
3038 if (nvalues < partnatts)
3051 if (off >= 0 && is_equal)
3053 if (nvalues == partnatts)
3061 int saved_off = off;
3082 boundinfo->
datums[off - 1],
3083 boundinfo->
kind[off - 1],
3094 boundinfo->
kind[off],
3105 if (boundinfo->
kind[off][nvalues] ==
3116 while (off < boundinfo->ndatums - 1)
3122 boundinfo->
datums[off + 1],
3123 boundinfo->
kind[off + 1],
3134 boundinfo->
kind[off],
3144 Assert(minoff >= 0 && maxoff >= 0);
3187 if (is_equal && nvalues < partnatts)
3202 while (off >= 1 && off < boundinfo->ndatums - 1)
3207 nextoff = inclusive ? off - 1 : off + 1;
3211 boundinfo->
datums[nextoff],
3212 boundinfo->
kind[nextoff],
3224 boundinfo->
kind[off],
3227 minoff = inclusive ? off : off + 1;
3263 if (is_equal && nvalues < partnatts)
3265 while (off >= 1 && off < boundinfo->ndatums - 1)
3270 nextoff = inclusive ? off + 1 : off - 1;
3273 boundinfo->
datums[nextoff],
3274 boundinfo->
kind[nextoff],
3286 boundinfo->
kind[off],
3289 maxoff = inclusive ? off + 1 : off;
3301 else if (!is_equal || inclusive)
3317 elog(
ERROR,
"invalid strategy number %d", opstrategy);
3321 Assert(minoff >= 0 && minoff <= boundinfo->ndatums);
3322 Assert(maxoff >= 0 && maxoff <= boundinfo->ndatums);
3330 if (minoff < boundinfo->ndatums && partindices[minoff] < 0)
3332 int lastkey = nvalues - 1;
3334 if (boundinfo->
kind[minoff][lastkey] ==
3349 if (maxoff >= 1 && partindices[maxoff] < 0)
3351 int lastkey = nvalues - 1;
3353 if (boundinfo->
kind[maxoff - 1][lastkey] ==
3361 Assert(minoff >= 0 && maxoff >= 0);
3362 if (minoff <= maxoff)
3420 foreach(lc2, step->
exprs)
3426 execparamids =
bms_join(execparamids,
3431 return execparamids;
3467 for (keyno = 0; keyno < context->
partnatts; keyno++)
3573 elog(
ERROR,
"unexpected partition strategy: %d",
3629 elog(
ERROR,
"invalid pruning combine step argument");
3630 step_result = step_results[step_id];
3631 Assert(step_result != NULL);
3653 elog(
ERROR,
"invalid pruning combine step argument");
3654 step_result = step_results[step_id];
3655 Assert(step_result != NULL);
3701 Expr **outconst,
bool *notclause)
3712 if (!IsBuiltinBooleanOpfamily(partopfamily))
3719 leftop = btest->
arg;
3723 if (
equal(leftop, partkey))
3761 if (
equal(leftop, partkey))
3788 Expr *expr,
int stateidx,
3796 *
value = con->constvalue;
3797 *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(const 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)
#define palloc_object(type)
#define palloc0_array(type, count)
#define palloc0_object(type)
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, const Datum *rb_datums, PartitionRangeDatumKind *rb_kind, const 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, const 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)
static PruneStepResult * get_matching_range_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, const Datum *values, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys)
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)
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)
#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 PruneStepResult * get_matching_hash_bounds(PartitionPruneContext *context, StrategyNumber opstrategy, const Datum *values, int nvalues, FmgrInfo *partsupfunc, Bitmapset *nullkeys)
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)