1/*--------------------------------------------------------------------
2 * execPartition.h
3 * POSTGRES partitioning executor interface
4 *
5 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
7 *
8 * IDENTIFICATION
9 * src/include/executor/execPartition.h
10 *--------------------------------------------------------------------
11 */
12
13#ifndef EXECPARTITION_H
14#define EXECPARTITION_H
15
16#include "nodes/execnodes.h"
17#include "nodes/parsenodes.h"
18#include "nodes/plannodes.h"
19#include "partitioning/partprune.h"
20
21/* See execPartition.c for the definitions. */
22typedef struct PartitionDispatchData *PartitionDispatch;
23typedef struct PartitionTupleRouting PartitionTupleRouting;
24
25/*
26 * PartitionRoutingInfo
27 *
28 * Additional result relation information specific to routing tuples to a
29 * table partition.
30 */
31typedef struct PartitionRoutingInfo
32{
33 /*
34 * Map for converting tuples in root partitioned table format into
35 * partition format, or NULL if no conversion is required.
36 */
37 TupleConversionMap *pi_RootToPartitionMap;
38
39 /*
40 * Map for converting tuples in partition format into the root partitioned
41 * table format, or NULL if no conversion is required.
42 */
43 TupleConversionMap *pi_PartitionToRootMap;
44
45 /*
46 * Slot to store tuples in partition format, or NULL when no translation
47 * is required between root and partition.
48 */
49 TupleTableSlot *pi_PartitionTupleSlot;
50} PartitionRoutingInfo;
51
52/*
53 * PartitionedRelPruningData - Per-partitioned-table data for run-time pruning
54 * of partitions. For a multilevel partitioned table, we have one of these
55 * for the topmost partition plus one for each non-leaf child partition.
56 *
57 * subplan_map[] and subpart_map[] have the same definitions as in
58 * PartitionedRelPruneInfo (see plannodes.h); though note that here,
59 * subpart_map contains indexes into PartitionPruningData.partrelprunedata[].
60 *
61 * nparts Length of subplan_map[] and subpart_map[].
62 * subplan_map Subplan index by partition index, or -1.
63 * subpart_map Subpart index by partition index, or -1.
64 * present_parts A Bitmapset of the partition indexes that we
65 * have subplans or subparts for.
66 * initial_pruning_steps List of PartitionPruneSteps used to
67 * perform executor startup pruning.
68 * exec_pruning_steps List of PartitionPruneSteps used to
69 * perform per-scan pruning.
70 * initial_context If initial_pruning_steps isn't NIL, contains
71 * the details needed to execute those steps.
72 * exec_context If exec_pruning_steps isn't NIL, contains
73 * the details needed to execute those steps.
74 */
75typedef struct PartitionedRelPruningData
76{
77 int nparts;
78 int *subplan_map;
79 int *subpart_map;
80 Bitmapset *present_parts;
81 List *initial_pruning_steps;
82 List *exec_pruning_steps;
83 PartitionPruneContext initial_context;
84 PartitionPruneContext exec_context;
85} PartitionedRelPruningData;
86
87/*
88 * PartitionPruningData - Holds all the run-time pruning information for
89 * a single partitioning hierarchy containing one or more partitions.
90 * partrelprunedata[] is an array ordered such that parents appear before
91 * their children; in particular, the first entry is the topmost partition,
92 * which was actually named in the SQL query.
93 */
94typedef struct PartitionPruningData
95{
96 int num_partrelprunedata; /* number of array entries */
97 PartitionedRelPruningData partrelprunedata[FLEXIBLE_ARRAY_MEMBER];
98} PartitionPruningData;
99
100/*
101 * PartitionPruneState - State object required for plan nodes to perform
102 * run-time partition pruning.
103 *
104 * This struct can be attached to plan types which support arbitrary Lists of
105 * subplans containing partitions, to allow subplans to be eliminated due to
106 * the clauses being unable to match to any tuple that the subplan could
107 * possibly produce.
108 *
109 * execparamids Contains paramids of PARAM_EXEC Params found within
110 * any of the partprunedata structs. Pruning must be
111 * done again each time the value of one of these
112 * parameters changes.
113 * other_subplans Contains indexes of subplans that don't belong to any
114 * "partprunedata", e.g UNION ALL children that are not
115 * partitioned tables, or a partitioned table that the
116 * planner deemed run-time pruning to be useless for.
117 * These must not be pruned.
118 * prune_context A short-lived memory context in which to execute the
119 * partition pruning functions.
120 * do_initial_prune true if pruning should be performed during executor
121 * startup (at any hierarchy level).
122 * do_exec_prune true if pruning should be performed during
123 * executor run (at any hierarchy level).
124 * num_partprunedata Number of items in "partprunedata" array.
125 * partprunedata Array of PartitionPruningData pointers for the plan's
126 * partitioned relation(s), one for each partitioning
127 * hierarchy that requires run-time pruning.
128 */
129typedef struct PartitionPruneState
130{
131 Bitmapset *execparamids;
132 Bitmapset *other_subplans;
133 MemoryContext prune_context;
134 bool do_initial_prune;
135 bool do_exec_prune;
136 int num_partprunedata;
137 PartitionPruningData *partprunedata[FLEXIBLE_ARRAY_MEMBER];
138} PartitionPruneState;
139
140extern PartitionTupleRouting *ExecSetupPartitionTupleRouting(EState *estate,
141 ModifyTableState *mtstate,
142 Relation rel);
143extern ResultRelInfo *ExecFindPartition(ModifyTableState *mtstate,
144 ResultRelInfo *rootResultRelInfo,
145 PartitionTupleRouting *proute,
146 TupleTableSlot *slot,
147 EState *estate);
148extern void ExecCleanupTupleRouting(ModifyTableState *mtstate,
149 PartitionTupleRouting *proute);
150extern PartitionPruneState *ExecCreatePartitionPruneState(PlanState *planstate,
151 PartitionPruneInfo *partitionpruneinfo);
152extern Bitmapset *ExecFindMatchingSubPlans(PartitionPruneState *prunestate);
153extern Bitmapset *ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate,
154 int nsubplans);
155
156#endif /* EXECPARTITION_H */
157