diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index e7809c8937a..b40e955d9e2 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -1662,7 +1662,7 @@ perform_pruning_base_step(PartitionPruneContext *context, FmgrInfo partsupfunc[PARTITION_MAX_KEYS]; nvalues = 0; - lc = list_head(opstep->values); + lc = list_head(opstep->exprs); /* Generate the partition look-up key. */ for (keyno = 0; keyno < context->partnatts; keyno++) @@ -1875,7 +1875,7 @@ perform_pruning_combine_step(PartitionPruneContext *context, } default: - elog(ERROR, "Invalid PartitionPruneCombineOp: %d", (int) + elog(ERROR, "invalid PartitionPruneCombineOp: %d", (int) cstep->combineOp); return NULL; /* keep compiler quiet */ } diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index adc24b44394..0f2e1d57dfd 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -2142,7 +2142,7 @@ _copyPartitionPruneStepOp(const PartitionPruneStepOp *from) PartitionPruneStepOp *newnode = makeNode(PartitionPruneStepOp); COPY_SCALAR_FIELD(opstrategy); - COPY_NODE_FIELD(values); + COPY_NODE_FIELD(exprs); COPY_BITMAPSET_FIELD(nullkeys); return newnode; diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index bec265c8896..dab62eb1583 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -2150,7 +2150,7 @@ expression_tree_walker(Node *node, { PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node; - if (walker((Node *) opstep->values, context)) + if (walker((Node *) opstep->exprs, context)) return true; } break; @@ -2958,7 +2958,7 @@ expression_tree_mutator(Node *node, PartitionPruneStepOp *newnode; FLATCOPY(newnode, opstep, PartitionPruneStepOp); - MUTATE(newnode->values, opstep->values, List *); + MUTATE(newnode->exprs, opstep->exprs, List *); return (Node *) newnode; } diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 7eeaed31335..03b94f65932 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -887,6 +887,13 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, if (rte->relkind == RELKIND_PARTITIONED_TABLE) rel->partitioned_child_rels = list_make1_int(rti); + /* + * If the partitioned relation has any baserestrictinfo quals then we + * attempt to use these quals to prune away partitions that cannot + * possibly contain any tuples matching these quals. In this case we'll + * store the relids of all partitions which could possibly contain a + * matching tuple, and skip anything else in the loop below. + */ if (rte->relkind == RELKIND_PARTITIONED_TABLE && rel->baserestrictinfo != NIL) { @@ -1142,12 +1149,12 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, continue; } - if (IS_PARTITIONED_REL(rel) && did_pruning && + if (did_pruning && !bms_is_member(appinfo->child_relid, live_children)) { /* - * This child need not be scanned, so we can omit it from the - * appendrel. + * Partition pruning determined this partition cannot possibly + * contain any tuples matching the baserestrictinfo, so skip it. */ set_dummy_rel_pathlist(childrel); continue; diff --git a/src/backend/optimizer/util/partprune.c b/src/backend/optimizer/util/partprune.c index 39b639c7b94..3fbc6c51de2 100644 --- a/src/backend/optimizer/util/partprune.c +++ b/src/backend/optimizer/util/partprune.c @@ -39,10 +39,10 @@ typedef struct PartClauseInfo { int keyno; /* Partition key number (0 to partnatts - 1) */ - Oid opno; /* operator used to compare partkey to 'value' */ - Expr *value; /* The value the partition key is being + Oid opno; /* operator used to compare partkey to 'expr' */ + Expr *expr; /* The expr the partition key is being * compared to */ - Oid cmpfuncoid; /* Oid of function to compare this to the + Oid cmpfuncoid; /* Oid of function to compare 'expr' to the * partition key */ /* cached info. */ @@ -70,25 +70,24 @@ static PartClauseMatchStatus match_clause_to_partition_key(RelOptInfo *rel, static bool match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey, Expr **rightop); static List *get_steps_using_prefix(int step_opstrategy, - Expr *step_lastvalue, + Expr *step_lastexpr, int step_lastkeyno, Bitmapset *step_nullkeys, List *prefix); static List *get_steps_using_prefix_recurse(int step_opstrategy, - Expr *step_lastvalue, + Expr *step_lastexpr, int step_lastkeyno, Bitmapset *step_nullkeys, List *prefix, ListCell *start, - List *step_values); + List *step_exprs); /* * prune_append_rel_partitions - * Returns RT indexes of relations belonging to the minimum set of - * partitions which must be scanned to satisfy rel's baserestrictinfo - * quals or NULL if no partitions exist. + * Returns RT indexes of the minimum set of child partitions which must + * be scanned to satisfy rel's baserestrictinfo quals. * - * Only call this if 'rel' corresponds to a partitioned table. + * Callers must ensure that 'rel' is a partitioned table. */ Relids prune_append_rel_partitions(RelOptInfo *rel) @@ -318,6 +317,8 @@ generate_partition_pruning_steps_internal(RelOptInfo *rel, List *clauses, } *constfalse = all_args_constfalse; + + /* Check if any contradicting clauses were found */ if (*constfalse) return NIL; @@ -427,13 +428,16 @@ generate_partition_pruning_steps_internal(RelOptInfo *rel, List *clauses, break; case PARTCLAUSE_MATCH_CONTRADICT: - /* Nothing to do here. */ + /* We've nothing more to do if a contradiction was found. */ *constfalse = true; return NIL; case PARTCLAUSE_NOMATCH: - /* go check for the next key. */ - break; + /* + * Clause didn't match this key, but it might match the + * next one. + */ + continue; case PARTCLAUSE_UNSUPPORTED: /* This clause cannot be used for pruning. */ @@ -451,7 +455,9 @@ generate_partition_pruning_steps_internal(RelOptInfo *rel, List *clauses, } } - /* Combine values from all <> operator clauses into one prune step. */ + /* + * Combine expressions from all <> operator clauses into one prune step. + */ if (ne_clauses != NIL) { List *argexprs = NIL; @@ -463,7 +469,7 @@ generate_partition_pruning_steps_internal(RelOptInfo *rel, List *clauses, { PartClauseInfo *pc = lfirst(lc); - argexprs = lappend(argexprs, pc->value); + argexprs = lappend(argexprs, pc->expr); argcmpfuncs = lappend_oid(argcmpfuncs, pc->cmpfuncoid); } @@ -659,13 +665,13 @@ generate_partition_pruning_steps_internal(RelOptInfo *rel, List *clauses, continue; /* - * Considering pc->value as the last value in the + * Considering pc->expr as the last value in the * pruning tuple, try to generate pruning steps for * tuples containing various combinations of values * for earlier columns from the clauses in prefix. */ pc_steps = get_steps_using_prefix(pc->op_strategy, - pc->value, + pc->expr, pc->keyno, NULL, prefix); @@ -700,7 +706,7 @@ generate_partition_pruning_steps_internal(RelOptInfo *rel, List *clauses, for_each_cell(lc1, lc) { pc_steps = get_steps_using_prefix(pc->op_strategy, - pc->value, + pc->expr, pc->keyno, nullkeys, prefix); @@ -794,7 +800,7 @@ match_clause_to_partition_key(RelOptInfo *rel, bool *is_neop_listp) { PartitionScheme part_scheme = rel->part_scheme; - Expr *value; + Expr *expr; Oid partopfamily = part_scheme->partopfamily[partkeyidx], partcoll = part_scheme->partcollation[partkeyidx]; @@ -802,14 +808,14 @@ match_clause_to_partition_key(RelOptInfo *rel, * Recognize specially shaped clauses that match with the Boolean * partition key. */ - if (match_boolean_partition_clause(partopfamily, clause, partkey, &value)) + if (match_boolean_partition_clause(partopfamily, clause, partkey, &expr)) { *is_neop_listp = false; *pc = palloc0(sizeof(PartClauseInfo)); (*pc)->keyno = partkeyidx; /* Do pruning with the Boolean equality operator. */ (*pc)->opno = BooleanEqualOperator; - (*pc)->value = value; + (*pc)->expr = expr; (*pc)->cmpfuncoid = rel->part_scheme->partsupfunc[partkeyidx].fn_oid; return PARTCLAUSE_MATCH_CLAUSE; @@ -833,10 +839,10 @@ match_clause_to_partition_key(RelOptInfo *rel, /* check if the clause matches this partition key */ if (equal(leftop, partkey)) - value = rightop; + expr = rightop; else if (equal(rightop, partkey)) { - value = leftop; + expr = leftop; commutator = get_commutator(opclause->opno); /* nothing we can do unless we can swap the operands */ @@ -869,8 +875,8 @@ match_clause_to_partition_key(RelOptInfo *rel, if (!op_strict(opclause->opno)) return PARTCLAUSE_UNSUPPORTED; - /* We can't use any volatile value to prune partitions. */ - if (contain_volatile_functions((Node *) value)) + /* We can't use any volatile expressions to prune partitions. */ + if (contain_volatile_functions((Node *) expr)) return PARTCLAUSE_UNSUPPORTED; /* @@ -912,12 +918,12 @@ match_clause_to_partition_key(RelOptInfo *rel, if (part_scheme->strategy == PARTITION_STRATEGY_HASH) cmpfuncoid = get_opfamily_proc(part_scheme->partopfamily[partkeyidx], part_scheme->partopcintype[partkeyidx], - exprType((Node *) value), + exprType((Node *) expr), HASHEXTENDED_PROC); else cmpfuncoid = get_opfamily_proc(part_scheme->partopfamily[partkeyidx], part_scheme->partopcintype[partkeyidx], - exprType((Node *) value), + exprType((Node *) expr), BTORDER_PROC); if (!OidIsValid(cmpfuncoid)) @@ -938,7 +944,7 @@ match_clause_to_partition_key(RelOptInfo *rel, else (*pc)->opno = opclause->opno; - (*pc)->value = value; + (*pc)->expr = expr; (*pc)->cmpfuncoid = cmpfuncoid; return PARTCLAUSE_MATCH_CLAUSE; @@ -1201,25 +1207,25 @@ match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey, */ static List * get_steps_using_prefix(int step_opstrategy, - Expr *step_lastvalue, + Expr *step_lastexpr, int step_lastkeyno, Bitmapset *step_nullkeys, List *prefix) { - /* Quick exit if there are no values to prefix lastvalue with. */ + /* Quick exit if there are no values to prefix lastexpr with. */ if (list_length(prefix) == 0) { PartitionPruneStepOp *step = makeNode(PartitionPruneStepOp); step->opstrategy = step_opstrategy; - step->values = list_make1(step_lastvalue); + step->exprs = list_make1(step_lastexpr); step->nullkeys = step_nullkeys; return list_make1(step); } return get_steps_using_prefix_recurse(step_opstrategy, - step_lastvalue, + step_lastexpr, step_lastkeyno, step_nullkeys, prefix, @@ -1229,12 +1235,12 @@ get_steps_using_prefix(int step_opstrategy, static List * get_steps_using_prefix_recurse(int step_opstrategy, - Expr *step_lastvalue, + Expr *step_lastexpr, int step_lastkeyno, Bitmapset *step_nullkeys, List *prefix, ListCell *start, - List *step_values) + List *step_exprs) { List *result = NIL; ListCell *lc; @@ -1244,23 +1250,23 @@ get_steps_using_prefix_recurse(int step_opstrategy, step_keyno = ((PartClauseInfo *) lfirst(start))->keyno; if (step_keyno == step_lastkeyno - 1) { - Assert(list_length(step_values) == step_keyno); + Assert(list_length(step_exprs) == step_keyno); for_each_cell(lc, start) { PartClauseInfo *pc = lfirst(lc); PartitionPruneStepOp *step; - List *step_values1; + List *step_exprs1; if (pc->keyno > step_keyno) break; - step_values1 = list_copy(step_values); - step_values1 = lappend(step_values1, pc->value); - step_values1 = lappend(step_values1, step_lastvalue); + step_exprs1 = list_copy(step_exprs); + step_exprs1 = lappend(step_exprs1, pc->expr); + step_exprs1 = lappend(step_exprs1, step_lastexpr); step = makeNode(PartitionPruneStepOp); step->opstrategy = step_opstrategy; - step->values = step_values1; + step->exprs = step_exprs1; step->nullkeys = step_nullkeys; result = lappend(result, step); } @@ -1284,23 +1290,23 @@ get_steps_using_prefix_recurse(int step_opstrategy, pc = lfirst(lc); if (pc->keyno == 0) { - list_free(step_values); - step_values = list_make1(pc->value); + list_free(step_exprs); + step_exprs = list_make1(pc->expr); } else if (pc->keyno == step_keyno) - step_values = lappend(step_values, pc->value); + step_exprs = lappend(step_exprs, pc->expr); else break; result = list_concat(result, list_copy(get_steps_using_prefix_recurse(step_opstrategy, - step_lastvalue, + step_lastexpr, step_lastkeyno, step_nullkeys, prefix, next_start, - step_values))); + step_exprs))); } } diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 507cc87af1c..301d1f3bc03 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1521,7 +1521,7 @@ typedef struct PartitionPruneStepOp PartitionPruneStep step; int opstrategy; - List *values; + List *exprs; Bitmapset *nullkeys; } PartitionPruneStepOp; @@ -1530,7 +1530,7 @@ typedef enum PartitionPruneCombineOp COMBINE_OR, COMBINE_AND, COMBINE_NOT -} PartitionPruneCombineOp; +} PartitionPruneCombineOp; typedef struct PartitionPruneStepCombine {