PostgreSQL Source Code  git master
partprune.h File Reference
Include dependency graph for partprune.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PartitionPruneContext
 

Macros

#define PruneCxtStateIdx(partnatts, step_id, keyno)    ((partnatts) * (step_id) + (keyno))
 

Typedefs

typedef struct PartitionPruneContext PartitionPruneContext
 

Functions

PartitionPruneInfomake_partition_pruneinfo (struct PlannerInfo *root, struct RelOptInfo *parentrel, List *subpaths, List *prunequal)
 
Bitmapsetprune_append_rel_partitions (struct RelOptInfo *rel)
 
Bitmapsetget_matching_partitions (PartitionPruneContext *context, List *pruning_steps)
 

Macro Definition Documentation

◆ PruneCxtStateIdx

#define PruneCxtStateIdx (   partnatts,
  step_id,
  keyno 
)     ((partnatts) * (step_id) + (keyno))

Definition at line 70 of file partprune.h.

Typedef Documentation

◆ PartitionPruneContext

Function Documentation

◆ get_matching_partitions()

Bitmapset* get_matching_partitions ( PartitionPruneContext context,
List pruning_steps 
)

Definition at line 820 of file partprune.c.

821 {
822  Bitmapset *result;
823  int num_steps = list_length(pruning_steps),
824  i;
825  PruneStepResult **results,
826  *final_result;
827  ListCell *lc;
828  bool scan_default;
829 
830  /* If there are no pruning steps then all partitions match. */
831  if (num_steps == 0)
832  {
833  Assert(context->nparts > 0);
834  return bms_add_range(NULL, 0, context->nparts - 1);
835  }
836 
837  /*
838  * Allocate space for individual pruning steps to store its result. Each
839  * slot will hold a PruneStepResult after performing a given pruning step.
840  * Later steps may use the result of one or more earlier steps. The
841  * result of applying all pruning steps is the value contained in the slot
842  * of the last pruning step.
843  */
844  results = (PruneStepResult **)
845  palloc0(num_steps * sizeof(PruneStepResult *));
846  foreach(lc, pruning_steps)
847  {
848  PartitionPruneStep *step = lfirst(lc);
849 
850  switch (nodeTag(step))
851  {
852  case T_PartitionPruneStepOp:
853  results[step->step_id] =
855  (PartitionPruneStepOp *) step);
856  break;
857 
858  case T_PartitionPruneStepCombine:
859  results[step->step_id] =
861  (PartitionPruneStepCombine *) step,
862  results);
863  break;
864 
865  default:
866  elog(ERROR, "invalid pruning step type: %d",
867  (int) nodeTag(step));
868  }
869  }
870 
871  /*
872  * At this point we know the offsets of all the datums whose corresponding
873  * partitions need to be in the result, including special null-accepting
874  * and default partitions. Collect the actual partition indexes now.
875  */
876  final_result = results[num_steps - 1];
877  Assert(final_result != NULL);
878  i = -1;
879  result = NULL;
880  scan_default = final_result->scan_default;
881  while ((i = bms_next_member(final_result->bound_offsets, i)) >= 0)
882  {
883  int partindex;
884 
885  Assert(i < context->boundinfo->nindexes);
886  partindex = context->boundinfo->indexes[i];
887 
888  if (partindex < 0)
889  {
890  /*
891  * In range partitioning cases, if a partition index is -1 it
892  * means that the bound at the offset is the upper bound for a
893  * range not covered by any partition (other than a possible
894  * default partition). In hash partitioning, the same means no
895  * partition has been defined for the corresponding remainder
896  * value.
897  *
898  * In either case, the value is still part of the queried range of
899  * values, so mark to scan the default partition if one exists.
900  */
901  scan_default |= partition_bound_has_default(context->boundinfo);
902  continue;
903  }
904 
905  result = bms_add_member(result, partindex);
906  }
907 
908  /* Add the null and/or default partition if needed and present. */
909  if (final_result->scan_null)
910  {
913  result = bms_add_member(result, context->boundinfo->null_index);
914  }
915  if (scan_default)
916  {
918  context->strategy == PARTITION_STRATEGY_RANGE);
920  result = bms_add_member(result, context->boundinfo->default_index);
921  }
922 
923  return result;
924 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1039
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:755
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:859
#define ERROR
Definition: elog.h:39
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1257
#define nodeTag(nodeptr)
Definition: nodes.h:133
@ PARTITION_STRATEGY_LIST
Definition: parsenodes.h:867
@ PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:868
#define partition_bound_has_default(bi)
Definition: partbounds.h:99
#define partition_bound_accepts_nulls(bi)
Definition: partbounds.h:98
static PruneStepResult * perform_pruning_base_step(PartitionPruneContext *context, PartitionPruneStepOp *opstep)
Definition: partprune.c:3338
static PruneStepResult * perform_pruning_combine_step(PartitionPruneContext *context, PartitionPruneStepCombine *cstep, PruneStepResult **step_results)
Definition: partprune.c:3486
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
PartitionBoundInfo boundinfo
Definition: partprune.h:54
Bitmapset * bound_offsets
Definition: partprune.c:134

References Assert(), bms_add_member(), bms_add_range(), bms_next_member(), PruneStepResult::bound_offsets, PartitionPruneContext::boundinfo, PartitionBoundInfoData::default_index, elog(), ERROR, i, PartitionBoundInfoData::indexes, lfirst, list_length(), nodeTag, PartitionPruneContext::nparts, PartitionBoundInfoData::null_index, palloc0(), partition_bound_accepts_nulls, partition_bound_has_default, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, perform_pruning_base_step(), perform_pruning_combine_step(), PruneStepResult::scan_default, PruneStepResult::scan_null, PartitionPruneStep::step_id, and PartitionPruneContext::strategy.

Referenced by find_matching_subplans_recurse(), and prune_append_rel_partitions().

◆ make_partition_pruneinfo()

PartitionPruneInfo* make_partition_pruneinfo ( struct PlannerInfo root,
struct RelOptInfo parentrel,
List subpaths,
List prunequal 
)

Definition at line 223 of file partprune.c.

226 {
227  PartitionPruneInfo *pruneinfo;
228  Bitmapset *allmatchedsubplans = NULL;
229  List *allpartrelids;
230  List *prunerelinfos;
231  int *relid_subplan_map;
232  ListCell *lc;
233  int i;
234 
235  /*
236  * Scan the subpaths to see which ones are scans of partition child
237  * relations, and identify their parent partitioned rels. (Note: we must
238  * restrict the parent partitioned rels to be parentrel or children of
239  * parentrel, otherwise we couldn't translate prunequal to match.)
240  *
241  * Also construct a temporary array to map from partition-child-relation
242  * relid to the index in 'subpaths' of the scan plan for that partition.
243  * (Use of "subplan" rather than "subpath" is a bit of a misnomer, but
244  * we'll let it stand.) For convenience, we use 1-based indexes here, so
245  * that zero can represent an un-filled array entry.
246  */
247  allpartrelids = NIL;
248  relid_subplan_map = palloc0(sizeof(int) * root->simple_rel_array_size);
249 
250  i = 1;
251  foreach(lc, subpaths)
252  {
253  Path *path = (Path *) lfirst(lc);
254  RelOptInfo *pathrel = path->parent;
255 
256  /* We don't consider partitioned joins here */
257  if (pathrel->reloptkind == RELOPT_OTHER_MEMBER_REL)
258  {
259  RelOptInfo *prel = pathrel;
260  Bitmapset *partrelids = NULL;
261 
262  /*
263  * Traverse up to the pathrel's topmost partitioned parent,
264  * collecting parent relids as we go; but stop if we reach
265  * parentrel. (Normally, a pathrel's topmost partitioned parent
266  * is either parentrel or a UNION ALL appendrel child of
267  * parentrel. But when handling partitionwise joins of
268  * multi-level partitioning trees, we can see an append path whose
269  * parentrel is an intermediate partitioned table.)
270  */
271  do
272  {
273  AppendRelInfo *appinfo;
274 
275  Assert(prel->relid < root->simple_rel_array_size);
276  appinfo = root->append_rel_array[prel->relid];
277  prel = find_base_rel(root, appinfo->parent_relid);
278  if (!IS_PARTITIONED_REL(prel))
279  break; /* reached a non-partitioned parent */
280  /* accept this level as an interesting parent */
281  partrelids = bms_add_member(partrelids, prel->relid);
282  if (prel == parentrel)
283  break; /* don't traverse above parentrel */
284  } while (prel->reloptkind == RELOPT_OTHER_MEMBER_REL);
285 
286  if (partrelids)
287  {
288  /*
289  * Found some relevant parent partitions, which may or may not
290  * overlap with partition trees we already found. Add new
291  * information to the allpartrelids list.
292  */
293  allpartrelids = add_part_relids(allpartrelids, partrelids);
294  /* Also record the subplan in relid_subplan_map[] */
295  /* No duplicates please */
296  Assert(relid_subplan_map[pathrel->relid] == 0);
297  relid_subplan_map[pathrel->relid] = i;
298  }
299  }
300  i++;
301  }
302 
303  /*
304  * We now build a PartitionedRelPruneInfo for each topmost partitioned rel
305  * (omitting any that turn out not to have useful pruning quals).
306  */
307  prunerelinfos = NIL;
308  foreach(lc, allpartrelids)
309  {
310  Bitmapset *partrelids = (Bitmapset *) lfirst(lc);
311  List *pinfolist;
312  Bitmapset *matchedsubplans = NULL;
313 
314  pinfolist = make_partitionedrel_pruneinfo(root, parentrel,
315  prunequal,
316  partrelids,
317  relid_subplan_map,
318  &matchedsubplans);
319 
320  /* When pruning is possible, record the matched subplans */
321  if (pinfolist != NIL)
322  {
323  prunerelinfos = lappend(prunerelinfos, pinfolist);
324  allmatchedsubplans = bms_join(matchedsubplans,
325  allmatchedsubplans);
326  }
327  }
328 
329  pfree(relid_subplan_map);
330 
331  /*
332  * If none of the partition hierarchies had any useful run-time pruning
333  * quals, then we can just not bother with run-time pruning.
334  */
335  if (prunerelinfos == NIL)
336  return NULL;
337 
338  /* Else build the result data structure */
339  pruneinfo = makeNode(PartitionPruneInfo);
340  pruneinfo->prune_infos = prunerelinfos;
341 
342  /*
343  * Some subplans may not belong to any of the identified partitioned rels.
344  * This can happen for UNION ALL queries which include a non-partitioned
345  * table, or when some of the hierarchies aren't run-time prunable. Build
346  * a bitmapset of the indexes of all such subplans, so that the executor
347  * can identify which subplans should never be pruned.
348  */
349  if (bms_num_members(allmatchedsubplans) < list_length(subpaths))
350  {
351  Bitmapset *other_subplans;
352 
353  /* Create the complement of allmatchedsubplans */
354  other_subplans = bms_add_range(NULL, 0, list_length(subpaths) - 1);
355  other_subplans = bms_del_members(other_subplans, allmatchedsubplans);
356 
357  pruneinfo->other_subplans = other_subplans;
358  }
359  else
360  pruneinfo->other_subplans = NULL;
361 
362  return pruneinfo;
363 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:987
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:665
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:960
List * lappend(List *list, void *datum)
Definition: list.c:338
void pfree(void *pointer)
Definition: mcxt.c:1456
#define makeNode(_type_)
Definition: nodes.h:176
static List * add_part_relids(List *allpartrelids, Bitmapset *partrelids)
Definition: partprune.c:395
static List * make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *prunequal, Bitmapset *partrelids, int *relid_subplan_map, Bitmapset **matchedsubplans)
Definition: partprune.c:441
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1041
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:814
#define NIL
Definition: pg_list.h:68
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:405
Index parent_relid
Definition: pathnodes.h:2915
Definition: pg_list.h:54
Bitmapset * other_subplans
Definition: plannodes.h:1427
int simple_rel_array_size
Definition: pathnodes.h:229
Index relid
Definition: pathnodes.h:903
RelOptKind reloptkind
Definition: pathnodes.h:850

References add_part_relids(), Assert(), bms_add_member(), bms_add_range(), bms_del_members(), bms_join(), bms_num_members(), find_base_rel(), i, IS_PARTITIONED_REL, lappend(), lfirst, list_length(), make_partitionedrel_pruneinfo(), makeNode, NIL, PartitionPruneInfo::other_subplans, palloc0(), AppendRelInfo::parent_relid, pfree(), PartitionPruneInfo::prune_infos, RelOptInfo::relid, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and PlannerInfo::simple_rel_array_size.

Referenced by create_append_plan(), and create_merge_append_plan().

◆ prune_append_rel_partitions()

Bitmapset* prune_append_rel_partitions ( struct RelOptInfo rel)

Definition at line 753 of file partprune.c.

754 {
755  List *clauses = rel->baserestrictinfo;
756  List *pruning_steps;
758  PartitionPruneContext context;
759 
760  Assert(rel->part_scheme != NULL);
761 
762  /* If there are no partitions, return the empty set */
763  if (rel->nparts == 0)
764  return NULL;
765 
766  /*
767  * If pruning is disabled or if there are no clauses to prune with, return
768  * all partitions.
769  */
770  if (!enable_partition_pruning || clauses == NIL)
771  return bms_add_range(NULL, 0, rel->nparts - 1);
772 
773  /*
774  * Process clauses to extract pruning steps that are usable at plan time.
775  * If the clauses are found to be contradictory, we can return the empty
776  * set.
777  */
779  &gcontext);
780  if (gcontext.contradictory)
781  return NULL;
782  pruning_steps = gcontext.steps;
783 
784  /* If there's nothing usable, return all partitions */
785  if (pruning_steps == NIL)
786  return bms_add_range(NULL, 0, rel->nparts - 1);
787 
788  /* Set up PartitionPruneContext */
789  context.strategy = rel->part_scheme->strategy;
790  context.partnatts = rel->part_scheme->partnatts;
791  context.nparts = rel->nparts;
792  context.boundinfo = rel->boundinfo;
793  context.partcollation = rel->part_scheme->partcollation;
794  context.partsupfunc = rel->part_scheme->partsupfunc;
795  context.stepcmpfuncs = (FmgrInfo *) palloc0(sizeof(FmgrInfo) *
796  context.partnatts *
797  list_length(pruning_steps));
799 
800  /* These are not valid when being called from the planner */
801  context.planstate = NULL;
802  context.exprcontext = NULL;
803  context.exprstates = NULL;
804 
805  /* Actual pruning happens here. */
806  return get_matching_partitions(&context, pruning_steps);
807 }
bool enable_partition_pruning
Definition: costsize.c:153
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
Bitmapset * get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
Definition: partprune.c:820
@ PARTTARGET_PLANNER
Definition: partprune.c:94
static void gen_partprune_steps(RelOptInfo *rel, List *clauses, PartClauseTarget target, GeneratePruningStepsContext *context)
Definition: partprune.c:717
Definition: fmgr.h:57
FmgrInfo * partsupfunc
Definition: partprune.h:56
ExprContext * exprcontext
Definition: partprune.h:60
MemoryContext ppccontext
Definition: partprune.h:58
PlanState * planstate
Definition: partprune.h:59
FmgrInfo * stepcmpfuncs
Definition: partprune.h:57
ExprState ** exprstates
Definition: partprune.h:61
List * baserestrictinfo
Definition: pathnodes.h:964

References Assert(), RelOptInfo::baserestrictinfo, bms_add_range(), PartitionPruneContext::boundinfo, GeneratePruningStepsContext::contradictory, CurrentMemoryContext, enable_partition_pruning, PartitionPruneContext::exprcontext, PartitionPruneContext::exprstates, gen_partprune_steps(), get_matching_partitions(), list_length(), NIL, RelOptInfo::nparts, PartitionPruneContext::nparts, palloc0(), PartitionPruneContext::partcollation, PartitionPruneContext::partnatts, PartitionPruneContext::partsupfunc, PARTTARGET_PLANNER, PartitionPruneContext::planstate, PartitionPruneContext::ppccontext, PartitionPruneContext::stepcmpfuncs, GeneratePruningStepsContext::steps, and PartitionPruneContext::strategy.

Referenced by expand_partitioned_rtentry().