PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgpa_join.c File Reference
#include "postgres.h"
#include "pgpa_join.h"
#include "pgpa_scan.h"
#include "pgpa_walker.h"
#include "nodes/pathnodes.h"
#include "nodes/print.h"
#include "parser/parsetree.h"
Include dependency graph for pgpa_join.c:

Go to the source code of this file.

Data Structures

struct  pgpa_join_unroller
 

Functions

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)
 
static ElidedNodepgpa_descend_node (PlannedStmt *pstmt, Plan **plan)
 
static ElidedNodepgpa_descend_any_gather (PlannedStmt *pstmt, Plan **plan, bool *found_any_gather)
 
static bool pgpa_descend_any_unique (PlannedStmt *pstmt, Plan **plan, ElidedNode **elided_node)
 
static bool is_result_node_with_child (Plan *plan)
 
static bool is_sorting_plan (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)
 

Function Documentation

◆ is_result_node_with_child()

static bool is_result_node_with_child ( Plan plan)
static

Definition at line 630 of file pgpa_join.c.

631{
632 return IsA(plan, Result) && plan->lefttree != NULL;
633}
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define plan(x)
Definition pg_regress.c:164
static int fb(int x)

References fb(), IsA, and plan.

Referenced by pgpa_decompose_join(), and pgpa_unroll_join().

◆ is_sorting_plan()

static bool is_sorting_plan ( Plan plan)
static

Definition at line 639 of file pgpa_join.c.

640{
641 return IsA(plan, Sort) || IsA(plan, IncrementalSort);
642}

References IsA, and plan.

Referenced by pgpa_decompose_join(), pgpa_descend_any_gather(), pgpa_descend_any_unique(), and pgpa_unroll_join().

◆ pgpa_build_unrolled_join()

pgpa_unrolled_join * pgpa_build_unrolled_join ( pgpa_plan_walker_context walker,
pgpa_join_unroller join_unroller 
)

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:943
#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_join_strategy
Definition pgpa_join.h:28
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

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  )

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_decompose_join()

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 
)
static

Definition at line 339 of file pgpa_join.c.

343{
344 PlannedStmt *pstmt = walker->pstmt;
345 JoinType jointype = ((Join *) plan)->jointype;
350 pgpa_join_strategy strategy;
351 bool uniqueouter;
352 bool uniqueinner;
353
356 *found_any_outer_gather = false;
357 *found_any_inner_gather = false;
358
359 switch (nodeTag(plan))
360 {
361 case T_MergeJoin:
362
363 /*
364 * The planner may have chosen to place a Material node on the
365 * inner side of the MergeJoin; if this is present, we record it
366 * as part of the join strategy. (However, scan-level Materialize
367 * nodes are an exception.)
368 */
369 if (elidedinner == NULL && IsA(innerplan, Material) &&
371 {
374 }
375 else
376 strategy = JSTRAT_MERGE_JOIN_PLAIN;
377
378 /*
379 * For a MergeJoin, either the outer or the inner subplan, or
380 * both, may have needed to be sorted; we must disregard any Sort
381 * or IncrementalSort node to find the real inner or outer
382 * subplan.
383 */
388 break;
389
390 case T_NestLoop:
391
392 /*
393 * The planner may have chosen to place a Material or Memoize node
394 * on the inner side of the NestLoop; if this is present, we
395 * record it as part of the join strategy. (However, scan-level
396 * Materialize nodes are an exception.)
397 */
398 if (elidedinner == NULL && IsA(innerplan, Material) &&
400 {
403 }
404 else if (elidedinner == NULL && IsA(innerplan, Memoize))
405 {
408 }
409 else
410 strategy = JSTRAT_NESTED_LOOP_PLAIN;
411 break;
412
413 case T_HashJoin:
414
415 /*
416 * The inner subplan of a HashJoin is always a Hash node; the real
417 * inner subplan is the Hash node's child.
418 */
422 strategy = JSTRAT_HASH_JOIN;
423 break;
424
425 default:
426 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(plan));
427 }
428
429 /*
430 * The planner may have decided to implement a semijoin by first making
431 * the nullable side of the plan unique, and then performing a normal join
432 * against the result. Therefore, we might need to descend through a
433 * unique node on either side of the plan.
434 */
437
438 /*
439 * Can we see a Result node here, to project above a Gather? So far I've
440 * found no example that behaves that way; rather, the Gather or Gather
441 * Merge is made to project. Hence, don't test is_result_node_with_child()
442 * at this point.
443 */
444
445 /*
446 * The planner may have decided to parallelize part of the join tree, so
447 * we could find a Gather or Gather Merge node here. Note that, if
448 * present, this will appear below nodes we considered as part of the join
449 * strategy, but we could find another uniqueness-enforcing node below the
450 * Gather or Gather Merge, if present.
451 */
452 if (elidedouter == NULL)
453 {
458 uniqueouter = true;
459 }
460 if (elidedinner == NULL)
461 {
466 uniqueinner = true;
467 }
468
469 /*
470 * It's possible that a Result node has been inserted either to project a
471 * target list or to implement a one-time filter. If so, we can descend
472 * through it. Note that a Result node without a child would be a
473 * degenerate scan or join, and not something we could descend through.
474 */
479
480 /*
481 * If this is a semijoin that was converted to an inner join by making one
482 * side or the other unique, make a note that the inner or outer subplan,
483 * as appropriate, should be treated as a query plan feature when the main
484 * tree traversal reaches it.
485 *
486 * Conversely, if the planner could have made one side of the join unique
487 * and thereby converted it to an inner join, and chose not to do so, that
488 * is also worth noting.
489 *
490 * NB: This code could appear slightly higher up in this function, but
491 * none of the nodes through which we just descended should have
492 * associated RTIs.
493 *
494 * NB: This seems like a somewhat hacky way of passing information up to
495 * the main tree walk, but I don't currently have a better idea.
496 */
497 if (uniqueouter)
499 else if (jointype == JOIN_RIGHT_SEMI)
501 if (uniqueinner)
503 else if (jointype == JOIN_SEMI)
505
506 /* Set output parameters. */
511 return strategy;
512}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define nodeTag(nodeptr)
Definition nodes.h:139
JoinType
Definition nodes.h:298
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_RIGHT_SEMI
Definition nodes.h:319
static ElidedNode * pgpa_descend_node(PlannedStmt *pstmt, Plan **plan)
Definition pgpa_join.c:522
static ElidedNode * pgpa_descend_any_gather(PlannedStmt *pstmt, Plan **plan, bool *found_any_gather)
Definition pgpa_join.c:540
static bool is_sorting_plan(Plan *plan)
Definition pgpa_join.c:639
static bool pgpa_descend_any_unique(PlannedStmt *pstmt, Plan **plan, ElidedNode **elided_node)
Definition pgpa_join.c:581
static bool is_result_node_with_child(Plan *plan)
Definition pgpa_join.c:630
@ 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
ElidedNode * pgpa_last_elided_node(PlannedStmt *pstmt, Plan *plan)
bool pgpa_is_scan_level_materialize(Plan *plan)
void pgpa_add_future_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan)
@ PGPAQF_SEMIJOIN_UNIQUE
Definition pgpa_walker.h:48
@ PGPAQF_SEMIJOIN_NON_UNIQUE
Definition pgpa_walker.h:47
struct Plan * lefttree
Definition plannodes.h:239
struct Plan * righttree
Definition plannodes.h:240

References Assert, elog, ERROR, fb(), is_result_node_with_child(), is_sorting_plan(), IsA, JOIN_RIGHT_SEMI, JOIN_SEMI, JSTRAT_HASH_JOIN, JSTRAT_MERGE_JOIN_MATERIALIZE, JSTRAT_MERGE_JOIN_PLAIN, JSTRAT_NESTED_LOOP_MATERIALIZE, JSTRAT_NESTED_LOOP_MEMOIZE, JSTRAT_NESTED_LOOP_PLAIN, Plan::lefttree, nodeTag, pgpa_add_future_feature(), pgpa_descend_any_gather(), pgpa_descend_any_unique(), pgpa_descend_node(), pgpa_is_scan_level_materialize(), pgpa_last_elided_node(), PGPAQF_SEMIJOIN_NON_UNIQUE, PGPAQF_SEMIJOIN_UNIQUE, plan, and Plan::righttree.

Referenced by pgpa_unroll_join().

◆ pgpa_descend_any_gather()

static ElidedNode * pgpa_descend_any_gather ( PlannedStmt pstmt,
Plan **  plan,
bool found_any_gather 
)
static

Definition at line 540 of file pgpa_join.c.

542{
543 if (IsA(*plan, Gather))
544 {
545 *found_any_gather = true;
546 return pgpa_descend_node(pstmt, plan);
547 }
548
549 if (IsA(*plan, GatherMerge))
550 {
552
553 if (elided == NULL && is_sorting_plan(*plan))
554 elided = pgpa_descend_node(pstmt, plan);
555
556 *found_any_gather = true;
557 return elided;
558 }
559
560 return NULL;
561}

References fb(), is_sorting_plan(), IsA, pgpa_descend_node(), and plan.

Referenced by pgpa_decompose_join().

◆ pgpa_descend_any_unique()

static bool pgpa_descend_any_unique ( PlannedStmt pstmt,
Plan **  plan,
ElidedNode **  elided_node 
)
static

Definition at line 581 of file pgpa_join.c.

583{
584 bool descend = false;
585 bool sjunique = false;
586
587 if (*elided_node != NULL)
588 return sjunique;
589
590 if (IsA(*plan, Unique))
591 {
592 descend = true;
593 sjunique = true;
594 }
595 else if (IsA(*plan, Agg))
596 {
597 /*
598 * If this is a simple Agg node, then assume it's here to implement
599 * semijoin uniqueness. Otherwise, assume it's completing an eager
600 * aggregation or partitionwise aggregation operation that began at a
601 * higher level of the plan tree.
602 *
603 * (Note that when we're using an Agg node for uniqueness, there's no
604 * need for any case other than AGGSPLIT_SIMPLE, because there's no
605 * aggregated column being computed. However, the fact that
606 * AGGSPLIT_SIMPLE is in use doesn't prove that this Agg is here for
607 * the semijoin uniqueness. Maybe we should adjust an Agg node to
608 * carry a "purpose" field so that code like this can be more certain
609 * of its analysis.)
610 */
611 descend = true;
612 sjunique = (((Agg *) *plan)->aggsplit == AGGSPLIT_SIMPLE);
613 }
614
615 if (descend)
616 {
617 *elided_node = pgpa_descend_node(pstmt, plan);
618
619 if (*elided_node == NULL && is_sorting_plan(*plan))
620 *elided_node = pgpa_descend_node(pstmt, plan);
621 }
622
623 return sjunique;
624}
@ AGGSPLIT_SIMPLE
Definition nodes.h:387

References AGGSPLIT_SIMPLE, fb(), is_sorting_plan(), IsA, pgpa_descend_node(), and plan.

Referenced by pgpa_decompose_join().

◆ pgpa_descend_node()

static ElidedNode * pgpa_descend_node ( PlannedStmt pstmt,
Plan **  plan 
)
static

Definition at line 522 of file pgpa_join.c.

523{
524 *plan = (*plan)->lefttree;
525 return pgpa_last_elided_node(pstmt, *plan);
526}

References pgpa_last_elided_node(), and plan.

Referenced by pgpa_decompose_join(), pgpa_descend_any_gather(), and pgpa_descend_any_unique().

◆ pgpa_destroy_join_unroller()

void pgpa_destroy_join_unroller ( pgpa_join_unroller join_unroller)

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_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 
)

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
pgpa_join_unroller * pgpa_create_join_unroller(void)
Definition pgpa_join.c:64
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().