PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgpa_join.h File Reference
#include "nodes/plannodes.h"
Include dependency graph for pgpa_join.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pgpa_join_member
 
struct  pgpa_unrolled_join
 

Macros

#define NUM_PGPA_JOIN_STRATEGY   ((int) JSTRAT_HASH_JOIN + 1)
 

Typedefs

typedef struct pgpa_plan_walker_context pgpa_plan_walker_context
 
typedef struct pgpa_join_unroller pgpa_join_unroller
 
typedef struct pgpa_unrolled_join pgpa_unrolled_join
 

Enumerations

enum  pgpa_join_strategy {
  JSTRAT_MERGE_JOIN_PLAIN = 0 , JSTRAT_MERGE_JOIN_MATERIALIZE , JSTRAT_NESTED_LOOP_PLAIN , JSTRAT_NESTED_LOOP_MATERIALIZE ,
  JSTRAT_NESTED_LOOP_MEMOIZE , JSTRAT_HASH_JOIN
}
 

Functions

static bool pgpa_is_join (Plan *plan)
 
pgpa_join_unrollerpgpa_create_join_unroller (void)
 
void pgpa_unroll_join (pgpa_plan_walker_context *walker, Plan *plan, bool beneath_any_gather, pgpa_join_unroller *join_unroller, pgpa_join_unroller **outer_join_unroller, pgpa_join_unroller **inner_join_unroller)
 
pgpa_unrolled_joinpgpa_build_unrolled_join (pgpa_plan_walker_context *walker, pgpa_join_unroller *join_unroller)
 
void pgpa_destroy_join_unroller (pgpa_join_unroller *join_unroller)
 

Macro Definition Documentation

◆ NUM_PGPA_JOIN_STRATEGY

#define NUM_PGPA_JOIN_STRATEGY   ((int) JSTRAT_HASH_JOIN + 1)

Definition at line 38 of file pgpa_join.h.

Typedef Documentation

◆ pgpa_join_unroller

Definition at line 18 of file pgpa_join.h.

◆ pgpa_plan_walker_context

◆ pgpa_unrolled_join

Definition at line 19 of file pgpa_join.h.

Enumeration Type Documentation

◆ pgpa_join_strategy

Enumerator
JSTRAT_MERGE_JOIN_PLAIN 
JSTRAT_MERGE_JOIN_MATERIALIZE 
JSTRAT_NESTED_LOOP_PLAIN 
JSTRAT_NESTED_LOOP_MATERIALIZE 
JSTRAT_NESTED_LOOP_MEMOIZE 
JSTRAT_HASH_JOIN 

Definition at line 27 of file pgpa_join.h.

28{
35 /* update NUM_PGPA_JOIN_STRATEGY if you add anything here */
pgpa_join_strategy
Definition pgpa_join.h:28
@ JSTRAT_MERGE_JOIN_PLAIN
Definition pgpa_join.h:29
@ JSTRAT_NESTED_LOOP_MATERIALIZE
Definition pgpa_join.h:32
@ JSTRAT_NESTED_LOOP_MEMOIZE
Definition pgpa_join.h:33
@ JSTRAT_HASH_JOIN
Definition pgpa_join.h:34
@ JSTRAT_NESTED_LOOP_PLAIN
Definition pgpa_join.h:31
@ JSTRAT_MERGE_JOIN_MATERIALIZE
Definition pgpa_join.h:30

Function Documentation

◆ pgpa_build_unrolled_join()

pgpa_unrolled_join * pgpa_build_unrolled_join ( pgpa_plan_walker_context walker,
pgpa_join_unroller join_unroller 
)
extern

Definition at line 230 of file pgpa_join.c.

232{
234 int i;
235
236 /*
237 * We shouldn't have gone even so far as to create a join unroller unless
238 * we found at least one unrollable join.
239 */
240 Assert(join_unroller->nused > 0);
241
242 /* Allocate result structures. */
244 ujoin->ninner = join_unroller->nused;
247
248 /* Handle the outermost join. */
249 ujoin->outer.plan = join_unroller->outer_subplan;
250 ujoin->outer.elided_node = join_unroller->outer_elided_node;
251 ujoin->outer.scan =
252 pgpa_build_scan(walker, ujoin->outer.plan,
253 ujoin->outer.elided_node,
254 join_unroller->outer_beneath_any_gather,
255 true);
256
257 /*
258 * We want the joins from the deepest part of the plan tree to appear
259 * first in the result object, but the join unroller adds them in exactly
260 * the reverse of that order, so we need to flip the order of the arrays
261 * when constructing the final result.
262 */
263 for (i = 0; i < join_unroller->nused; ++i)
264 {
265 int k = join_unroller->nused - i - 1;
266
267 /* Copy strategy, Plan, and ElidedNode. */
268 ujoin->strategy[i] = join_unroller->strategy[k];
269 ujoin->inner[i].plan = join_unroller->inner_subplans[k];
270 ujoin->inner[i].elided_node = join_unroller->inner_elided_nodes[k];
271
272 /*
273 * Fill in remaining details, using either the nested join unroller,
274 * or by deriving them from the plan and elided nodes.
275 */
276 if (join_unroller->inner_unrollers[k] != NULL)
277 ujoin->inner[i].unrolled_join =
279 join_unroller->inner_unrollers[k]);
280 else
281 ujoin->inner[i].scan =
282 pgpa_build_scan(walker, ujoin->inner[i].plan,
283 ujoin->inner[i].elided_node,
284 join_unroller->inner_beneath_any_gather[k],
285 true);
286 }
287
288 return ujoin;
289}
#define Assert(condition)
Definition c.h:927
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
int i
Definition isn.c:77
pgpa_unrolled_join * pgpa_build_unrolled_join(pgpa_plan_walker_context *walker, pgpa_join_unroller *join_unroller)
Definition pgpa_join.c:230
pgpa_scan * pgpa_build_scan(pgpa_plan_walker_context *walker, Plan *plan, ElidedNode *elided_node, bool beneath_any_gather, bool within_join_problem)
Definition pgpa_scan.c:44
static int fb(int x)

References Assert, fb(), i, palloc0_array, palloc0_object, pgpa_build_scan(), and pgpa_build_unrolled_join().

Referenced by pgpa_build_unrolled_join(), and pgpa_walk_recursively().

◆ pgpa_create_join_unroller()

pgpa_join_unroller * pgpa_create_join_unroller ( void  )
extern

Definition at line 64 of file pgpa_join.c.

65{
67
69 join_unroller->nallocated = 4;
70 join_unroller->strategy =
72 join_unroller->inner_subplans =
73 palloc_array(Plan *, join_unroller->nallocated);
74 join_unroller->inner_elided_nodes =
76 join_unroller->inner_unrollers =
78 join_unroller->inner_beneath_any_gather =
79 palloc_array(bool, join_unroller->nallocated);
80
81 return join_unroller;
82}
#define palloc_array(type, count)
Definition fe_memutils.h:76
unsigned nallocated
Definition pgpa_join.c:28

References fb(), pgpa_join_unroller::nallocated, palloc0_object, and palloc_array.

Referenced by pgpa_unroll_join(), and pgpa_walk_recursively().

◆ pgpa_destroy_join_unroller()

void pgpa_destroy_join_unroller ( pgpa_join_unroller join_unroller)
extern

Definition at line 295 of file pgpa_join.c.

296{
297 pfree(join_unroller->strategy);
298 pfree(join_unroller->inner_subplans);
299 pfree(join_unroller->inner_elided_nodes);
300 pfree(join_unroller->inner_unrollers);
301 pfree(join_unroller->inner_beneath_any_gather);
303}
void pfree(void *pointer)
Definition mcxt.c:1616

References fb(), and pfree().

Referenced by pgpa_walk_recursively().

◆ pgpa_is_join()

static bool pgpa_is_join ( Plan plan)
inlinestatic

Definition at line 90 of file pgpa_join.h.

91{
92 return IsA(plan, NestLoop) || IsA(plan, MergeJoin) || IsA(plan, HashJoin);
93}
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define plan(x)
Definition pg_regress.c:161

References IsA, and plan.

Referenced by pgpa_unroll_join(), and pgpa_walk_recursively().

◆ pgpa_unroll_join()

void pgpa_unroll_join ( pgpa_plan_walker_context walker,
Plan plan,
bool  beneath_any_gather,
pgpa_join_unroller join_unroller,
pgpa_join_unroller **  outer_join_unroller,
pgpa_join_unroller **  inner_join_unroller 
)
extern

Definition at line 105 of file pgpa_join.c.

110{
111 pgpa_join_strategy strategy;
113 *realouter;
116 int n;
117 bool found_any_outer_gather = false;
118 bool found_any_inner_gather = false;
119
121
122 /*
123 * We need to pass the join_unroller object down through certain types of
124 * plan nodes -- anything that's considered part of the join strategy, and
125 * any other nodes that can occur in a join tree despite not being scans
126 * or joins.
127 *
128 * This includes:
129 *
130 * (1) Materialize, Memoize, and Hash nodes, which are part of the join
131 * strategy,
132 *
133 * (2) Gather and Gather Merge nodes, which can occur at any point in the
134 * join tree where the planner decided to initiate parallelism,
135 *
136 * (3) Sort and IncrementalSort nodes, which can occur beneath MergeJoin
137 * or GatherMerge,
138 *
139 * (4) Agg and Unique nodes, which can occur when we decide to make the
140 * nullable side of a semijoin unique and then join the result, and
141 *
142 * (5) Result nodes with children, which can be added either to project to
143 * enforce a one-time filter (but Result nodes without children are
144 * degenerate scans or joins).
145 */
146 if (IsA(plan, Material) || IsA(plan, Memoize) || IsA(plan, Hash)
150 {
152 return;
153 }
154
155 /*
156 * Since we've already handled nodes that require pass-through treatment,
157 * this should be an unrollable join.
158 */
159 strategy = pgpa_decompose_join(walker, plan,
164
165 /* If our workspace is full, expand it. */
166 if (join_unroller->nused >= join_unroller->nallocated)
167 {
168 join_unroller->nallocated *= 2;
169 join_unroller->strategy =
172 join_unroller->nallocated);
173 join_unroller->inner_subplans =
174 repalloc_array(join_unroller->inner_subplans,
175 Plan *,
176 join_unroller->nallocated);
177 join_unroller->inner_elided_nodes =
178 repalloc_array(join_unroller->inner_elided_nodes,
179 ElidedNode *,
180 join_unroller->nallocated);
181 join_unroller->inner_beneath_any_gather =
182 repalloc_array(join_unroller->inner_beneath_any_gather,
183 bool,
184 join_unroller->nallocated);
185 join_unroller->inner_unrollers =
186 repalloc_array(join_unroller->inner_unrollers,
189 }
190
191 /*
192 * Since we're flattening outer-deep join trees, it follows that if the
193 * outer side is still an unrollable join, it should be unrolled into this
194 * same object. Otherwise, we've reached the limit of what we can unroll
195 * into this object and must remember the outer side as the final outer
196 * subplan.
197 */
200 else
201 {
202 join_unroller->outer_subplan = realouter;
203 join_unroller->outer_elided_node = elidedouter;
204 join_unroller->outer_beneath_any_gather =
206 }
207
208 /*
209 * Store the inner subplan. If it's an unrollable join, it needs to be
210 * flattened in turn, but into a new unroller object, not this one.
211 */
212 n = join_unroller->nused++;
213 join_unroller->strategy[n] = strategy;
214 join_unroller->inner_subplans[n] = realinner;
215 join_unroller->inner_elided_nodes[n] = elidedinner;
216 join_unroller->inner_beneath_any_gather[n] =
220 else
222 join_unroller->inner_unrollers[n] = *inner_join_unroller;
223}
#define repalloc_array(pointer, type, count)
Definition fe_memutils.h:78
static pgpa_join_strategy pgpa_decompose_join(pgpa_plan_walker_context *walker, Plan *plan, Plan **realouter, Plan **realinner, ElidedNode **elidedrealouter, ElidedNode **elidedrealinner, bool *found_any_outer_gather, bool *found_any_inner_gather)
Definition pgpa_join.c:339
static bool is_sorting_plan(Plan *plan)
Definition pgpa_join.c:635
pgpa_join_unroller * pgpa_create_join_unroller(void)
Definition pgpa_join.c:64
static bool is_result_node_with_child(Plan *plan)
Definition pgpa_join.c:626
static bool pgpa_is_join(Plan *plan)
Definition pgpa_join.h:90

References Assert, fb(), is_result_node_with_child(), is_sorting_plan(), IsA, pgpa_join_unroller::nallocated, pgpa_create_join_unroller(), pgpa_decompose_join(), pgpa_is_join(), plan, and repalloc_array.

Referenced by pgpa_walk_recursively().