146 int *relid_subplan_map,
157 List *source_stepids,
162 Expr *clause,
Expr *partkey,
int partkeyidx,
163 bool *clause_is_not_null,
207 Expr *expr,
int stateidx,
231 int *relid_subplan_map;
251 foreach(lc, subpaths)
276 appinfo = root->append_rel_array[prel->
relid];
282 if (prel == parentrel)
297 relid_subplan_map[pathrel->
relid] =
i;
308 foreach(lc, allpartrelids)
321 if (pinfolist !=
NIL)
323 prunerelinfos =
lappend(prunerelinfos, pinfolist);
324 allmatchedsubplans =
bms_join(matchedsubplans,
329 pfree(relid_subplan_map);
335 if (prunerelinfos ==
NIL)
405 foreach(lc, allpartrelids)
410 if (targetpart == currtarget)
414 lfirst(lc) = currpartrelids;
415 return allpartrelids;
419 return lappend(allpartrelids, partrelids);
444 int *relid_subplan_map,
449 bool doruntimeprune =
false;
450 int *relid_subpart_map;
473 List *initial_pruning_steps;
474 List *exec_pruning_steps;
486 Assert(rti < root->simple_rel_array_size);
487 relid_subpart_map[rti] =
i++;
496 targetpart = subpart;
521 partprunequal = prunequal;
530 partprunequal = (
List *)
567 initial_pruning_steps = context.
steps;
569 initial_pruning_steps =
NIL;
587 exec_pruning_steps = context.
steps;
597 exec_pruning_steps =
NIL;
602 exec_pruning_steps =
NIL;
606 if (initial_pruning_steps || exec_pruning_steps)
607 doruntimeprune =
true;
617 pinfolist =
lappend(pinfolist, pinfo);
623 pfree(relid_subpart_map);
635 foreach(lc, pinfolist)
640 int nparts = subpart->
nparts;
651 subplan_map = (
int *)
palloc(nparts *
sizeof(
int));
652 memset(subplan_map, -1, nparts *
sizeof(
int));
653 subpart_map = (
int *)
palloc(nparts *
sizeof(
int));
654 memset(subpart_map, -1, nparts *
sizeof(
int));
656 present_parts = NULL;
667 subplan_map[
i] = subplanidx = relid_subplan_map[partrel->
relid] - 1;
668 subpart_map[
i] = subpartidx = relid_subpart_map[partrel->
relid] - 1;
677 else if (subpartidx >= 0)
691 pinfo->subplan_map = subplan_map;
692 pinfo->subpart_map = subpart_map;
693 pinfo->relid_map = relid_map;
696 pfree(relid_subpart_map);
698 *matchedsubplans = subplansfound;
760 Assert(rel->part_scheme != NULL);
782 pruning_steps = gcontext.
steps;
785 if (pruning_steps ==
NIL)
789 context.
strategy = rel->part_scheme->strategy;
790 context.
partnatts = rel->part_scheme->partnatts;
794 context.
partsupfunc = rel->part_scheme->partsupfunc;
846 foreach(lc, pruning_steps)
852 case T_PartitionPruneStepOp:
858 case T_PartitionPruneStepCombine:
859 results[step->step_id] =
866 elog(
ERROR,
"invalid pruning step type: %d",
876 final_result = results[num_steps - 1];
877 Assert(final_result != NULL);
885 Assert(i < context->boundinfo->nindexes);
971 bool generate_opsteps =
false;
994 memset(keyclauses, 0,
sizeof(keyclauses));
1006 (((
Const *) clause)->constisnull ||
1027 bool all_args_contradictory =
true;
1043 bool arg_contradictory;
1052 if (arg_contradictory)
1058 all_args_contradictory =
false;
1060 if (argsteps !=
NIL)
1091 if (all_args_contradictory)
1097 if (arg_stepids !=
NIL)
1103 result =
lappend(result, step);
1130 if (argsteps !=
NIL)
1150 bool clause_is_not_null =
false;
1156 &clause_is_not_null,
1157 &pc, &clause_steps))
1171 generate_opsteps =
true;
1172 keyclauses[
i] =
lappend(keyclauses[
i], pc);
1176 if (!clause_is_not_null)
1183 keyclauses[
i] !=
NIL)
1262 false,
NIL,
NIL, nullkeys);
1263 result =
lappend(result, step);
1265 else if (generate_opsteps)
1280 result =
lappend(result, step);
1302 result =
lappend(result,
final);
1332 opstep->
exprs = exprs;
1350 List *source_stepids,
1396 memset(btree_clauses, 0,
sizeof(btree_clauses));
1397 memset(hash_clauses, 0,
sizeof(hash_clauses));
1400 List *clauselist = keyclauses[
i];
1401 bool consider_next_key =
true;
1420 foreach(lc, clauselist)
1449 consider_next_key =
false;
1454 elog(
ERROR,
"invalid clause for hash partitioning");
1460 elog(
ERROR,
"invalid partition strategy: %c",
1470 if (!consider_next_key)
1505 foreach(lc, btree_clauses[strat])
1514 bool prefix_valid =
true;
1515 bool pk_has_clauses;
1549 for (keyno = 0; keyno < pc->
keyno; keyno++)
1551 pk_has_clauses =
false;
1561 if (eqpc->
keyno == keyno)
1563 prefix =
lappend(prefix, eqpc);
1564 pk_has_clauses =
true;
1586 if (lepc->
keyno == keyno)
1588 prefix =
lappend(prefix, lepc);
1589 pk_has_clauses =
true;
1612 if (gepc->
keyno == keyno)
1614 prefix =
lappend(prefix, gepc);
1615 pk_has_clauses =
true;
1630 if (!pk_has_clauses)
1632 prefix_valid =
false;
1677 if (eq_clauses !=
NIL)
1691 pc =
llast(eq_clauses);
1698 last_keyno = pc->
keyno;
1699 foreach(lc, eq_clauses)
1702 if (pc->
keyno == last_keyno)
1744 elog(
ERROR,
"invalid partition strategy: %c",
1761 #define PartCollMatchesExprColl(partcoll, exprcoll) \
1762 ((partcoll) == InvalidOid || (partcoll) == (exprcoll))
1797 Expr *clause,
Expr *partkey,
int partkeyidx,
1799 List **clause_steps)
1812 partkey, &expr, ¬eq);
1819 partclause->
keyno = partkeyidx;
1821 partclause->
opno = BooleanEqualOperator;
1823 partclause->
expr = expr;
1844 bool is_opne_listp =
false;
1853 opno = opclause->
opno;
1856 if (
equal(leftop, partkey))
1858 else if (
equal(rightop, partkey))
1899 &op_strategy, &op_lefttype,
1912 &op_strategy, &op_lefttype,
1915 is_opne_listp =
true;
2045 op_righttype, op_righttype,
2050 elog(
ERROR,
"invalid partition strategy: %c",
2064 partclause->
keyno = partkeyidx;
2068 partclause->
opno = negator;
2074 partclause->
opno = opno;
2078 partclause->
expr = expr;
2079 partclause->
cmpfn = cmpfn;
2089 Oid saop_coll = saop->inputcollid;
2100 if (!
equal(leftop, partkey) ||
2128 &lefttype, &righttype);
2198 if (
op_volatile(saop_op) != PROVOLATILE_IMMUTABLE)
2231 if (arr->constisnull)
2236 &elemlen, &elembyval, &elemalign);
2239 elemlen, elembyval, elemalign,
2240 &elem_values, &elem_nulls,
2242 for (
i = 0;
i < num_elems;
i++)
2259 arr->constcollid, elemlen,
2260 elem_values[
i],
false, elembyval);
2261 elem_exprs =
lappend(elem_exprs, elem_expr);
2273 if (arrexpr->multidims)
2292 foreach(lc1, elem_exprs)
2299 elem_clauses =
lappend(elem_clauses, elem_clause);
2313 else if (*clause_steps ==
NIL)
2348 return boolmatchstatus;
2377 Expr *step_lastexpr,
2383 Assert(step_nullkeys == NULL ||
2429 Expr *step_lastexpr,
2448 if (cur_keyno < step_lastkeyno - 1)
2463 if (pc->
keyno > cur_keyno)
2475 if (pc->
keyno == cur_keyno)
2527 Assert(context->
rel->part_scheme->strategy
2530 context->
rel->part_scheme->partnatts);
2543 step_exprs1 =
lappend(step_exprs1, step_lastexpr);
2548 step_cmpfns1 =
lappend_oid(step_cmpfns1, step_lastcmpfn);
2551 step_opstrategy, step_op_is_ne,
2552 step_exprs1, step_cmpfns1,
2554 result =
lappend(result, step);
2591 int *partindices = boundinfo->
indexes;
2596 int greatest_modulus;
2614 for (
i = 0;
i < partnatts;
i++)
2620 greatest_modulus = boundinfo->
nindexes;
2621 if (partindices[rowHash % greatest_modulus] >= 0)
2672 bool inclusive =
false;
2705 maxoff = boundinfo->
ndatums - 1;
2733 if (off >= 0 && is_equal)
2766 if (off >= 0 && is_equal)
2786 if (!is_equal || !inclusive)
2804 if (off > boundinfo->
ndatums - 1)
2818 if (off >= 0 && is_equal && !inclusive)
2834 elog(
ERROR,
"invalid strategy number %d", opstrategy);
2838 Assert(minoff >= 0 && maxoff >= 0);
2881 int *partindices = boundinfo->
indexes;
2886 bool inclusive =
false;
2889 Assert(nvalues <= partnatts);
2915 if (partindices[minoff] < 0)
2917 if (partindices[maxoff] < 0)
2921 Assert(partindices[minoff] >= 0 &&
2922 partindices[maxoff] >= 0);
2932 if (nvalues < partnatts)
2945 if (off >= 0 && is_equal)
2947 if (nvalues == partnatts)
2955 int saved_off = off;
2976 boundinfo->
datums[off - 1],
2977 boundinfo->
kind[off - 1],
2988 boundinfo->
kind[off],
2999 if (boundinfo->
kind[off][nvalues] ==
3010 while (off < boundinfo->ndatums - 1)
3016 boundinfo->
datums[off + 1],
3017 boundinfo->
kind[off + 1],
3028 boundinfo->
kind[off],
3038 Assert(minoff >= 0 && maxoff >= 0);
3081 if (is_equal && nvalues < partnatts)
3096 while (off >= 1 && off < boundinfo->ndatums - 1)
3101 nextoff = inclusive ? off - 1 : off + 1;
3105 boundinfo->
datums[nextoff],
3106 boundinfo->
kind[nextoff],
3118 boundinfo->
kind[off],
3121 minoff = inclusive ? off : off + 1;
3157 if (is_equal && nvalues < partnatts)
3159 while (off >= 1 && off < boundinfo->ndatums - 1)
3164 nextoff = inclusive ? off + 1 : off - 1;
3167 boundinfo->
datums[nextoff],
3168 boundinfo->
kind[nextoff],
3180 boundinfo->
kind[off],
3183 maxoff = inclusive ? off + 1 : off;
3195 else if (!is_equal || inclusive)
3211 elog(
ERROR,
"invalid strategy number %d", opstrategy);
3215 Assert(minoff >= 0 && minoff <= boundinfo->ndatums);
3216 Assert(maxoff >= 0 && maxoff <= boundinfo->ndatums);
3224 if (minoff < boundinfo->ndatums && partindices[minoff] < 0)
3226 int lastkey = nvalues - 1;
3228 if (boundinfo->
kind[minoff][lastkey] ==
3243 if (maxoff >= 1 && partindices[maxoff] < 0)
3245 int lastkey = nvalues - 1;
3247 if (boundinfo->
kind[maxoff - 1][lastkey] ==
3255 Assert(minoff >= 0 && maxoff >= 0);
3256 if (minoff <= maxoff)
3315 foreach(lc2, step->
exprs)
3321 execparamids =
bms_join(execparamids,
3326 return execparamids;
3362 for (keyno = 0; keyno < context->
partnatts; keyno++)
3468 elog(
ERROR,
"unexpected partition strategy: %d",
3524 elog(
ERROR,
"invalid pruning combine step argument");
3525 step_result = step_results[step_id];
3526 Assert(step_result != NULL);
3548 elog(
ERROR,
"invalid pruning combine step argument");
3549 step_result = step_results[step_id];
3550 Assert(step_result != NULL);
3595 Expr **outconst,
bool *noteq)
3606 if (!IsBuiltinBooleanOpfamily(partopfamily))
3618 leftop = btest->
arg;
3622 if (
equal(leftop, partkey))
3655 if (
equal(leftop, partkey))
3683 Expr *expr,
int stateidx,
3691 *
value = con->constvalue;
3692 *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)
@ 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)
static PartClauseMatchStatus match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey, Expr **outconst, bool *noteq)
struct PruneStepResult PruneStepResult
PartitionPruneInfo * make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
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 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
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)