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 *partitioned_rels, 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 68 of file partprune.h.

Referenced by ExecInitPruningContext(), and perform_pruning_base_step().

Typedef Documentation

◆ PartitionPruneContext

Function Documentation

◆ get_matching_partitions()

Bitmapset* get_matching_partitions ( PartitionPruneContext context,
List pruning_steps 
)

Definition at line 716 of file partprune.c.

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, PartitionPruneContext::strategy, T_PartitionPruneStepCombine, and T_PartitionPruneStepOp.

Referenced by find_matching_subplans_recurse(), and prune_append_rel_partitions().

717 {
718  Bitmapset *result;
719  int num_steps = list_length(pruning_steps),
720  i;
721  PruneStepResult **results,
722  *final_result;
723  ListCell *lc;
724  bool scan_default;
725 
726  /* If there are no pruning steps then all partitions match. */
727  if (num_steps == 0)
728  {
729  Assert(context->nparts > 0);
730  return bms_add_range(NULL, 0, context->nparts - 1);
731  }
732 
733  /*
734  * Allocate space for individual pruning steps to store its result. Each
735  * slot will hold a PruneStepResult after performing a given pruning step.
736  * Later steps may use the result of one or more earlier steps. The
737  * result of applying all pruning steps is the value contained in the slot
738  * of the last pruning step.
739  */
740  results = (PruneStepResult **)
741  palloc0(num_steps * sizeof(PruneStepResult *));
742  foreach(lc, pruning_steps)
743  {
744  PartitionPruneStep *step = lfirst(lc);
745 
746  switch (nodeTag(step))
747  {
749  results[step->step_id] =
751  (PartitionPruneStepOp *) step);
752  break;
753 
755  results[step->step_id] =
757  (PartitionPruneStepCombine *) step,
758  results);
759  break;
760 
761  default:
762  elog(ERROR, "invalid pruning step type: %d",
763  (int) nodeTag(step));
764  }
765  }
766 
767  /*
768  * At this point we know the offsets of all the datums whose corresponding
769  * partitions need to be in the result, including special null-accepting
770  * and default partitions. Collect the actual partition indexes now.
771  */
772  final_result = results[num_steps - 1];
773  Assert(final_result != NULL);
774  i = -1;
775  result = NULL;
776  scan_default = final_result->scan_default;
777  while ((i = bms_next_member(final_result->bound_offsets, i)) >= 0)
778  {
779  int partindex = context->boundinfo->indexes[i];
780 
781  if (partindex < 0)
782  {
783  /*
784  * In range partitioning cases, if a partition index is -1 it
785  * means that the bound at the offset is the upper bound for a
786  * range not covered by any partition (other than a possible
787  * default partition). In hash partitioning, the same means no
788  * partition has been defined for the corresponding remainder
789  * value.
790  *
791  * In either case, the value is still part of the queried range of
792  * values, so mark to scan the default partition if one exists.
793  */
794  scan_default |= partition_bound_has_default(context->boundinfo);
795  continue;
796  }
797 
798  result = bms_add_member(result, partindex);
799  }
800 
801  /* Add the null and/or default partition if needed and present. */
802  if (final_result->scan_null)
803  {
806  result = bms_add_member(result, context->boundinfo->null_index);
807  }
808  if (scan_default)
809  {
811  context->strategy == PARTITION_STRATEGY_RANGE);
813  result = bms_add_member(result, context->boundinfo->default_index);
814  }
815 
816  return result;
817 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Bitmapset * bound_offsets
Definition: partprune.c:134
static PruneStepResult * perform_pruning_combine_step(PartitionPruneContext *context, PartitionPruneStepCombine *cstep, PruneStepResult **step_results)
Definition: partprune.c:3283
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:834
#define ERROR
Definition: elog.h:43
static PruneStepResult * perform_pruning_base_step(PartitionPruneContext *context, PartitionPruneStepOp *opstep)
Definition: partprune.c:3135
#define partition_bound_has_default(bi)
Definition: partbounds.h:75
void * palloc0(Size size)
Definition: mcxt.c:980
#define partition_bound_accepts_nulls(bi)
Definition: partbounds.h:74
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:799
#define nodeTag(nodeptr)
Definition: nodes.h:530
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:800
PartitionBoundInfo boundinfo
Definition: partprune.h:53
#define elog(elevel,...)
Definition: elog.h:228
int i

◆ make_partition_pruneinfo()

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

Definition at line 230 of file partprune.c.

References Assert, bms_add_range(), bms_del_members(), bms_join(), bms_num_members(), i, IS_SIMPLE_REL, lappend(), lfirst, list_length(), make_partitionedrel_pruneinfo(), makeNode, NIL, PartitionPruneInfo::other_subplans, palloc0(), Path::parent, pfree(), PartitionPruneInfo::prune_infos, RelOptInfo::relid, and PlannerInfo::simple_rel_array_size.

Referenced by create_append_plan(), and create_merge_append_plan().

233 {
234  PartitionPruneInfo *pruneinfo;
235  Bitmapset *allmatchedsubplans = NULL;
236  int *relid_subplan_map;
237  ListCell *lc;
238  List *prunerelinfos;
239  int i;
240 
241  /*
242  * Construct a temporary array to map from planner relids to subplan
243  * indexes. For convenience, we use 1-based indexes here, so that zero
244  * can represent an un-filled array entry.
245  */
246  relid_subplan_map = palloc0(sizeof(int) * root->simple_rel_array_size);
247 
248  /*
249  * relid_subplan_map maps relid of a leaf partition to the index in
250  * 'subpaths' of the scan plan for that partition.
251  */
252  i = 1;
253  foreach(lc, subpaths)
254  {
255  Path *path = (Path *) lfirst(lc);
256  RelOptInfo *pathrel = path->parent;
257 
258  Assert(IS_SIMPLE_REL(pathrel));
259  Assert(pathrel->relid < root->simple_rel_array_size);
260  /* No duplicates please */
261  Assert(relid_subplan_map[pathrel->relid] == 0);
262 
263  relid_subplan_map[pathrel->relid] = i++;
264  }
265 
266  /* We now build a PartitionedRelPruneInfo for each partitioned rel. */
267  prunerelinfos = NIL;
268  foreach(lc, partitioned_rels)
269  {
270  List *rels = (List *) lfirst(lc);
271  List *pinfolist;
272  Bitmapset *matchedsubplans = NULL;
273 
274  pinfolist = make_partitionedrel_pruneinfo(root, parentrel,
275  relid_subplan_map,
276  rels, prunequal,
277  &matchedsubplans);
278 
279  /* When pruning is possible, record the matched subplans */
280  if (pinfolist != NIL)
281  {
282  prunerelinfos = lappend(prunerelinfos, pinfolist);
283  allmatchedsubplans = bms_join(matchedsubplans,
284  allmatchedsubplans);
285  }
286  }
287 
288  pfree(relid_subplan_map);
289 
290  /*
291  * If none of the partition hierarchies had any useful run-time pruning
292  * quals, then we can just not bother with run-time pruning.
293  */
294  if (prunerelinfos == NIL)
295  return NULL;
296 
297  /* Else build the result data structure */
298  pruneinfo = makeNode(PartitionPruneInfo);
299  pruneinfo->prune_infos = prunerelinfos;
300 
301  /*
302  * Some subplans may not belong to any of the listed partitioned rels.
303  * This can happen for UNION ALL queries which include a non-partitioned
304  * table, or when some of the hierarchies aren't run-time prunable. Build
305  * a bitmapset of the indexes of all such subplans, so that the executor
306  * can identify which subplans should never be pruned.
307  */
308  if (bms_num_members(allmatchedsubplans) < list_length(subpaths))
309  {
310  Bitmapset *other_subplans;
311 
312  /* Create the complement of allmatchedsubplans */
313  other_subplans = bms_add_range(NULL, 0, list_length(subpaths) - 1);
314  other_subplans = bms_del_members(other_subplans, allmatchedsubplans);
315 
316  pruneinfo->other_subplans = other_subplans;
317  }
318  else
319  pruneinfo->other_subplans = NULL;
320 
321  return pruneinfo;
322 }
#define NIL
Definition: pg_list.h:65
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:616
void pfree(void *pointer)
Definition: mcxt.c:1056
static List * make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, int *relid_subplan_map, List *partitioned_rels, List *prunequal, Bitmapset **matchedsubplans)
Definition: partprune.c:343
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:834
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
RelOptInfo * parent
Definition: pathnodes.h:1116
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
int simple_rel_array_size
Definition: pathnodes.h:204
Index relid
Definition: pathnodes.h:671
List * lappend(List *list, void *datum)
Definition: list.c:322
void * palloc0(Size size)
Definition: mcxt.c:980
#define makeNode(_type_)
Definition: nodes.h:573
Bitmapset * other_subplans
Definition: plannodes.h:1106
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
int i
Definition: pg_list.h:50

◆ prune_append_rel_partitions()

Bitmapset* prune_append_rel_partitions ( struct RelOptInfo rel)

Definition at line 650 of file partprune.c.

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

Referenced by expand_partitioned_rtentry().

651 {
652  List *clauses = rel->baserestrictinfo;
653  List *pruning_steps;
655  PartitionPruneContext context;
656 
657  Assert(rel->part_scheme != NULL);
658 
659  /* If there are no partitions, return the empty set */
660  if (rel->nparts == 0)
661  return NULL;
662 
663  /*
664  * If pruning is disabled or if there are no clauses to prune with, return
665  * all partitions.
666  */
667  if (!enable_partition_pruning || clauses == NIL)
668  return bms_add_range(NULL, 0, rel->nparts - 1);
669 
670  /*
671  * Process clauses to extract pruning steps that are usable at plan time.
672  * If the clauses are found to be contradictory, we can return the empty
673  * set.
674  */
676  &gcontext);
677  if (gcontext.contradictory)
678  return NULL;
679  pruning_steps = gcontext.steps;
680 
681  /* If there's nothing usable, return all partitions */
682  if (pruning_steps == NIL)
683  return bms_add_range(NULL, 0, rel->nparts - 1);
684 
685  /* Set up PartitionPruneContext */
686  context.strategy = rel->part_scheme->strategy;
687  context.partnatts = rel->part_scheme->partnatts;
688  context.nparts = rel->nparts;
689  context.boundinfo = rel->boundinfo;
690  context.partcollation = rel->part_scheme->partcollation;
691  context.partsupfunc = rel->part_scheme->partsupfunc;
692  context.stepcmpfuncs = (FmgrInfo *) palloc0(sizeof(FmgrInfo) *
693  context.partnatts *
694  list_length(pruning_steps));
696 
697  /* These are not valid when being called from the planner */
698  context.planstate = NULL;
699  context.exprstates = NULL;
700 
701  /* Actual pruning happens here. */
702  return get_matching_partitions(&context, pruning_steps);
703 }
static void gen_partprune_steps(RelOptInfo *rel, List *clauses, PartClauseTarget target, GeneratePruningStepsContext *context)
Definition: partprune.c:614
#define NIL
Definition: pg_list.h:65
Definition: fmgr.h:56
FmgrInfo * partsupfunc
Definition: partprune.h:55
FmgrInfo * stepcmpfuncs
Definition: partprune.h:56
MemoryContext ppccontext
Definition: partprune.h:57
List * baserestrictinfo
Definition: pathnodes.h:705
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:834
ExprState ** exprstates
Definition: partprune.h:59
int nparts
Definition: pathnodes.h:721
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void * palloc0(Size size)
Definition: mcxt.c:980
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:722
bool enable_partition_pruning
Definition: costsize.c:140
#define Assert(condition)
Definition: c.h:739
struct FmgrInfo * partsupfunc
Definition: pathnodes.h:404
static int list_length(const List *l)
Definition: pg_list.h:169
PartitionBoundInfo boundinfo
Definition: partprune.h:53
Bitmapset * get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
Definition: partprune.c:716
PartitionScheme part_scheme
Definition: pathnodes.h:720
Definition: pg_list.h:50
PlanState * planstate
Definition: partprune.h:58