PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 PlannerInfo PlannerInfo
 
typedef struct RelOptInfo RelOptInfo
 
typedef struct PartitionPruneContext PartitionPruneContext
 

Functions

int make_partition_pruneinfo (PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
 
Bitmapsetprune_append_rel_partitions (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

◆ PlannerInfo

Definition at line 20 of file partprune.h.

◆ RelOptInfo

Definition at line 21 of file partprune.h.

Function Documentation

◆ get_matching_partitions()

Bitmapset * get_matching_partitions ( PartitionPruneContext context,
List pruning_steps 
)
extern

Definition at line 846 of file partprune.c.

847{
849 int num_steps = list_length(pruning_steps),
850 i;
851 PruneStepResult **results,
853 ListCell *lc;
854 bool scan_default;
855
856 /* If there are no pruning steps then all partitions match. */
857 if (num_steps == 0)
858 {
859 Assert(context->nparts > 0);
860 return bms_add_range(NULL, 0, context->nparts - 1);
861 }
862
863 /*
864 * Allocate space for individual pruning steps to store its result. Each
865 * slot will hold a PruneStepResult after performing a given pruning step.
866 * Later steps may use the result of one or more earlier steps. The
867 * result of applying all pruning steps is the value contained in the slot
868 * of the last pruning step.
869 */
870 results = (PruneStepResult **)
871 palloc0(num_steps * sizeof(PruneStepResult *));
872 foreach(lc, pruning_steps)
873 {
875
876 switch (nodeTag(step))
877 {
879 results[step->step_id] =
881 (PartitionPruneStepOp *) step);
882 break;
883
885 results[step->step_id] =
888 results);
889 break;
890
891 default:
892 elog(ERROR, "invalid pruning step type: %d",
893 (int) nodeTag(step));
894 }
895 }
896
897 /*
898 * At this point we know the offsets of all the datums whose corresponding
899 * partitions need to be in the result, including special null-accepting
900 * and default partitions. Collect the actual partition indexes now.
901 */
902 final_result = results[num_steps - 1];
904 i = -1;
905 result = NULL;
906 scan_default = final_result->scan_default;
907 while ((i = bms_next_member(final_result->bound_offsets, i)) >= 0)
908 {
909 int partindex;
910
911 Assert(i < context->boundinfo->nindexes);
912 partindex = context->boundinfo->indexes[i];
913
914 if (partindex < 0)
915 {
916 /*
917 * In range partitioning cases, if a partition index is -1 it
918 * means that the bound at the offset is the upper bound for a
919 * range not covered by any partition (other than a possible
920 * default partition). In hash partitioning, the same means no
921 * partition has been defined for the corresponding remainder
922 * value.
923 *
924 * In either case, the value is still part of the queried range of
925 * values, so mark to scan the default partition if one exists.
926 */
927 scan_default |= partition_bound_has_default(context->boundinfo);
928 continue;
929 }
930
932 }
933
934 /* Add the null and/or default partition if needed and present. */
935 if (final_result->scan_null)
936 {
940 }
941 if (scan_default)
942 {
947 }
948
949 return result;
950}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition bitmapset.c:1003
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define Assert(condition)
Definition c.h:943
uint32 result
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
int i
Definition isn.c:77
void * palloc0(Size size)
Definition mcxt.c:1417
#define nodeTag(nodeptr)
Definition nodes.h:139
@ PARTITION_STRATEGY_LIST
Definition parsenodes.h:917
@ PARTITION_STRATEGY_RANGE
Definition parsenodes.h:918
#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:3457
static PruneStepResult * perform_pruning_combine_step(PartitionPruneContext *context, PartitionPruneStepCombine *cstep, PruneStepResult **step_results)
Definition partprune.c:3605
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
static int fb(int x)
PartitionBoundInfo boundinfo
Definition partprune.h:54

References Assert, bms_add_member(), bms_add_range(), bms_next_member(), PartitionPruneContext::boundinfo, PartitionBoundInfoData::default_index, elog, ERROR, fb(), 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(), result, PartitionPruneStep::step_id, and PartitionPruneContext::strategy.

Referenced by find_matching_subplans_recurse(), and prune_append_rel_partitions().

◆ make_partition_pruneinfo()

int make_partition_pruneinfo ( PlannerInfo root,
RelOptInfo parentrel,
List subpaths,
List prunequal 
)
extern

Definition at line 225 of file partprune.c.

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

References add_part_relids(), Assert, bms_add_member(), bms_add_range(), bms_copy(), bms_del_members(), bms_join(), bms_num_members(), fb(), find_base_rel(), i, IS_PARTITIONED_REL, lappend(), lfirst, list_length(), make_partitionedrel_pruneinfo(), makeNode, NIL, palloc0_array, pfree(), RELOPT_OTHER_MEMBER_REL, and root.

Referenced by create_append_plan(), and create_merge_append_plan().

◆ prune_append_rel_partitions()

Bitmapset * prune_append_rel_partitions ( RelOptInfo rel)
extern

Definition at line 780 of file partprune.c.

781{
782 List *clauses = rel->baserestrictinfo;
785 PartitionPruneContext context;
786
787 Assert(rel->part_scheme != NULL);
788
789 /* If there are no partitions, return the empty set */
790 if (rel->nparts == 0)
791 return NULL;
792
793 /*
794 * If pruning is disabled or if there are no clauses to prune with, return
795 * all partitions.
796 */
797 if (!enable_partition_pruning || clauses == NIL)
798 return bms_add_range(NULL, 0, rel->nparts - 1);
799
800 /*
801 * Process clauses to extract pruning steps that are usable at plan time.
802 * If the clauses are found to be contradictory, we can return the empty
803 * set.
804 */
806 &gcontext);
807 if (gcontext.contradictory)
808 return NULL;
809 pruning_steps = gcontext.steps;
810
811 /* If there's nothing usable, return all partitions */
812 if (pruning_steps == NIL)
813 return bms_add_range(NULL, 0, rel->nparts - 1);
814
815 /* Set up PartitionPruneContext */
816 context.strategy = rel->part_scheme->strategy;
817 context.partnatts = rel->part_scheme->partnatts;
818 context.nparts = rel->nparts;
819 context.boundinfo = rel->boundinfo;
820 context.partcollation = rel->part_scheme->partcollation;
821 context.partsupfunc = rel->part_scheme->partsupfunc;
825
826 /* These are not valid when being called from the planner */
827 context.planstate = NULL;
828 context.exprcontext = NULL;
829 context.exprstates = NULL;
830
831 /* Actual pruning happens here. */
832 return get_matching_partitions(&context, pruning_steps);
833}
bool enable_partition_pruning
Definition costsize.c:164
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
Bitmapset * get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
Definition partprune.c:846
@ PARTTARGET_PLANNER
Definition partprune.c:94
static void gen_partprune_steps(RelOptInfo *rel, List *clauses, PartClauseTarget target, GeneratePruningStepsContext *context)
Definition partprune.c:744
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:1142

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

Referenced by expand_partitioned_rtentry().