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 818 of file partprune.c.

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

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

752 {
753  List *clauses = rel->baserestrictinfo;
754  List *pruning_steps;
756  PartitionPruneContext context;
757 
758  Assert(rel->part_scheme != NULL);
759 
760  /* If there are no partitions, return the empty set */
761  if (rel->nparts == 0)
762  return NULL;
763 
764  /*
765  * If pruning is disabled or if there are no clauses to prune with, return
766  * all partitions.
767  */
768  if (!enable_partition_pruning || clauses == NIL)
769  return bms_add_range(NULL, 0, rel->nparts - 1);
770 
771  /*
772  * Process clauses to extract pruning steps that are usable at plan time.
773  * If the clauses are found to be contradictory, we can return the empty
774  * set.
775  */
777  &gcontext);
778  if (gcontext.contradictory)
779  return NULL;
780  pruning_steps = gcontext.steps;
781 
782  /* If there's nothing usable, return all partitions */
783  if (pruning_steps == NIL)
784  return bms_add_range(NULL, 0, rel->nparts - 1);
785 
786  /* Set up PartitionPruneContext */
787  context.strategy = rel->part_scheme->strategy;
788  context.partnatts = rel->part_scheme->partnatts;
789  context.nparts = rel->nparts;
790  context.boundinfo = rel->boundinfo;
791  context.partcollation = rel->part_scheme->partcollation;
792  context.partsupfunc = rel->part_scheme->partsupfunc;
793  context.stepcmpfuncs = (FmgrInfo *) palloc0(sizeof(FmgrInfo) *
794  context.partnatts *
795  list_length(pruning_steps));
797 
798  /* These are not valid when being called from the planner */
799  context.planstate = NULL;
800  context.exprcontext = NULL;
801  context.exprstates = NULL;
802 
803  /* Actual pruning happens here. */
804  return get_matching_partitions(&context, pruning_steps);
805 }
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:818
@ PARTTARGET_PLANNER
Definition: partprune.c:94
static void gen_partprune_steps(RelOptInfo *rel, List *clauses, PartClauseTarget target, GeneratePruningStepsContext *context)
Definition: partprune.c:715
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().