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

Go to the source code of this file.

Data Structures

struct  pgpa_sj_unique_rel
 
struct  pgpa_query_feature
 
struct  pgpa_plan_walker_context
 

Macros

#define NUM_PGPA_QF_TYPES   ((int) PGPAQF_SEMIJOIN_UNIQUE + 1)
 

Typedefs

typedef struct pgpa_sj_unique_rel pgpa_sj_unique_rel
 
typedef enum pgpa_qf_type pgpa_qf_type
 
typedef struct pgpa_query_feature pgpa_query_feature
 
typedef struct pgpa_plan_walker_context pgpa_plan_walker_context
 

Enumerations

enum  pgpa_qf_type { PGPAQF_GATHER , PGPAQF_GATHER_MERGE , PGPAQF_SEMIJOIN_NON_UNIQUE , PGPAQF_SEMIJOIN_UNIQUE }
 

Functions

void pgpa_plan_walker (pgpa_plan_walker_context *walker, PlannedStmt *pstmt, List *sj_unique_rels)
 
void pgpa_add_future_feature (pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan)
 
ElidedNodepgpa_last_elided_node (PlannedStmt *pstmt, Plan *plan)
 
Bitmapsetpgpa_relids (Plan *plan)
 
Index pgpa_scanrelid (Plan *plan)
 
Bitmapsetpgpa_filter_out_join_relids (Bitmapset *relids, List *rtable)
 
bool pgpa_walker_would_advise (pgpa_plan_walker_context *walker, pgpa_identifier *rt_identifiers, pgpa_advice_tag_type tag, pgpa_advice_target *target)
 

Macro Definition Documentation

◆ NUM_PGPA_QF_TYPES

#define NUM_PGPA_QF_TYPES   ((int) PGPAQF_SEMIJOIN_UNIQUE + 1)

Definition at line 70 of file pgpa_walker.h.

Typedef Documentation

◆ pgpa_plan_walker_context

◆ pgpa_qf_type

◆ pgpa_query_feature

◆ pgpa_sj_unique_rel

Enumeration Type Documentation

◆ pgpa_qf_type

Enumerator
PGPAQF_GATHER 
PGPAQF_GATHER_MERGE 
PGPAQF_SEMIJOIN_NON_UNIQUE 
PGPAQF_SEMIJOIN_UNIQUE 

Definition at line 61 of file pgpa_walker.h.

62{
67 /* update NUM_PGPA_QF_TYPES if you add anything here */
pgpa_qf_type
Definition pgpa_walker.h:62
@ PGPAQF_GATHER
Definition pgpa_walker.h:63
@ PGPAQF_GATHER_MERGE
Definition pgpa_walker.h:64
@ PGPAQF_SEMIJOIN_UNIQUE
Definition pgpa_walker.h:66
@ PGPAQF_SEMIJOIN_NON_UNIQUE
Definition pgpa_walker.h:65

Function Documentation

◆ pgpa_add_future_feature()

void pgpa_add_future_feature ( pgpa_plan_walker_context walker,
pgpa_qf_type  type,
Plan plan 
)
extern

Definition at line 502 of file pgpa_walker.c.

504{
506
507 walker->future_query_features =
508 lappend(walker->future_query_features, qf);
509}
List * lappend(List *list, void *datum)
Definition list.c:339
#define plan(x)
Definition pg_regress.c:161
static pgpa_query_feature * pgpa_add_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan)
static int fb(int x)
const char * type

References fb(), lappend(), pgpa_add_feature(), plan, and type.

Referenced by pgpa_decompose_join().

◆ pgpa_filter_out_join_relids()

Bitmapset * pgpa_filter_out_join_relids ( Bitmapset relids,
List rtable 
)
extern

Definition at line 585 of file pgpa_walker.c.

586{
587 int rti = -1;
588 Bitmapset *result = NULL;
589
590 while ((rti = bms_next_member(relids, rti)) >= 0)
591 {
592 RangeTblEntry *rte = rt_fetch(rti, rtable);
593
594 if (rte->rtekind != RTE_JOIN)
595 result = bms_add_member(result, rti);
596 }
597
598 return result;
599}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
@ RTE_JOIN
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31

References bms_add_member(), bms_next_member(), fb(), rt_fetch, and RTE_JOIN.

Referenced by pgpa_build_scan(), pgpa_qf_add_plan_rtis(), and pgpa_walk_recursively().

◆ pgpa_last_elided_node()

ElidedNode * pgpa_last_elided_node ( PlannedStmt pstmt,
Plan plan 
)
extern

Definition at line 518 of file pgpa_walker.c.

519{
520 ElidedNode *elided_node = NULL;
521
523 {
524 if (n->plan_node_id == plan->plan_node_id)
525 elided_node = n;
526 }
527
528 return elided_node;
529}
#define foreach_node(type, var, lst)
Definition pg_list.h:496
List * elidedNodes
Definition plannodes.h:156

References PlannedStmt::elidedNodes, fb(), foreach_node, and plan.

Referenced by pgpa_decompose_join(), and pgpa_descend_node().

◆ pgpa_plan_walker()

void pgpa_plan_walker ( pgpa_plan_walker_context walker,
PlannedStmt pstmt,
List sj_unique_rels 
)
extern

Definition at line 71 of file pgpa_walker.c.

73{
74 ListCell *lc;
77
78 /* Initialization. */
80 walker->pstmt = pstmt;
81
82 /* Walk the main plan tree. */
83 pgpa_walk_recursively(walker, pstmt->planTree, false, NULL, NIL, false);
84
85 /* Main plan tree walk won't reach subplans, so walk those. */
86 foreach(lc, pstmt->subplans)
87 {
88 Plan *plan = lfirst(lc);
89
90 if (plan != NULL)
91 pgpa_walk_recursively(walker, plan, false, NULL, NIL, false);
92 }
93
94 /* Adjust RTIs from sj_unique_rels for the flattened range table. */
95 foreach_ptr(pgpa_sj_unique_rel, ur, sj_unique_rels)
96 {
97 int rtindex = -1;
98 int rtoffset = 0;
99 bool dummy = false;
100 Bitmapset *relids = NULL;
101
102 /* If this is a subplan, find the range table offset. */
103 if (ur->plan_name != NULL)
104 {
106 {
107 if (strcmp(ur->plan_name, rtinfo->plan_name) == 0)
108 {
109 rtoffset = rtinfo->rtoffset;
110 dummy = rtinfo->dummy;
111 break;
112 }
113 }
114
115 if (rtoffset == 0)
116 elog(ERROR, "no rtoffset for plan %s", ur->plan_name);
117 }
118
119 /* If this entry pertains to a dummy subquery, ignore it. */
120 if (dummy)
121 continue;
122
123 /* Offset each entry from the original set. */
124 while ((rtindex = bms_next_member(ur->relids, rtindex)) >= 0)
125 relids = bms_add_member(relids, rtindex + rtoffset);
126
127 /* Store the resulting set. */
129 }
130
131 /*
132 * Remove any non-unique semijoin query features for which making the rel
133 * unique wasn't considered.
134 */
136 walker->query_features[PGPAQF_SEMIJOIN_NON_UNIQUE])
137 {
138 if (list_member(sj_unique_rtis, qf->relids))
140 }
142
143 /*
144 * If we find any cases where analysis of the Plan tree shows that the
145 * semijoin was made unique but this possibility was never observed to be
146 * considered during planning, then we have a bug somewhere.
147 */
149 walker->query_features[PGPAQF_SEMIJOIN_UNIQUE])
150 {
151 if (!list_member(sj_unique_rtis, qf->relids))
152 {
154
156 outBitmapset(&buf, qf->relids);
157 elog(ERROR,
158 "unique semijoin found for relids %s but not observed during planning",
159 buf.data);
160 }
161 }
162
163 /*
164 * It's possible for a Gather or Gather Merge query feature to find no
165 * RTIs when partitionwise aggregation is in use. We shouldn't emit
166 * something like GATHER_MERGE(()), so instead emit nothing. This means
167 * that we won't advise either GATHER or GATHER_MERGE or NO_GATHER in such
168 * cases, which might be something we want to improve in the future.
169 *
170 * (Should the Partial Aggregates in such a case be created in an
171 * UPPERREL_GROUP_AGG with a non-empty relid set? Right now that doesn't
172 * happen, but it seems like it would make life easier for us if it did.)
173 */
174 for (int t = 0; t < NUM_PGPA_QF_TYPES; ++t)
175 {
176 List *query_features = NIL;
177
178 foreach_ptr(pgpa_query_feature, qf, walker->query_features[t])
179 {
180 if (qf->relids != NULL)
181 query_features = lappend(query_features, qf);
182 else
184 }
185
186 walker->query_features[t] = query_features;
187 }
188}
#define Assert(condition)
Definition c.h:927
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
bool list_member(const List *list, const void *datum)
Definition list.c:661
void outBitmapset(StringInfo str, const Bitmapset *bms)
Definition outfuncs.c:331
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
#define foreach_ptr(type, var, lst)
Definition pg_list.h:469
static char buf[DEFAULT_XLOG_SEG_SIZE]
static void pgpa_walk_recursively(pgpa_plan_walker_context *walker, Plan *plan, bool within_join_problem, pgpa_join_unroller *join_unroller, List *active_query_features, bool beneath_any_gather)
#define NUM_PGPA_QF_TYPES
Definition pgpa_walker.h:70
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Definition pg_list.h:54
struct Plan * planTree
Definition plannodes.h:101
List * subplans
Definition plannodes.h:132
List * subrtinfos
Definition plannodes.h:135

References Assert, bms_add_member(), bms_next_member(), buf, elog, ERROR, fb(), foreach_node, foreach_ptr, initStringInfo(), lappend(), lfirst, list_member(), NIL, NUM_PGPA_QF_TYPES, outBitmapset(), pgpa_walk_recursively(), PGPAQF_GATHER, PGPAQF_GATHER_MERGE, PGPAQF_SEMIJOIN_NON_UNIQUE, PGPAQF_SEMIJOIN_UNIQUE, plan, PlannedStmt::planTree, PlannedStmt::subplans, and PlannedStmt::subrtinfos.

Referenced by pgpa_planner_shutdown().

◆ pgpa_relids()

Bitmapset * pgpa_relids ( Plan plan)
extern

Definition at line 535 of file pgpa_walker.c.

536{
537 if (IsA(plan, Result))
538 return ((Result *) plan)->relids;
539 else if (IsA(plan, ForeignScan))
540 return ((ForeignScan *) plan)->fs_relids;
541 else if (IsA(plan, Append))
542 return ((Append *) plan)->apprelids;
543 else if (IsA(plan, MergeAppend))
544 return ((MergeAppend *) plan)->apprelids;
545
546 return NULL;
547}
#define IsA(nodeptr, _type_)
Definition nodes.h:164

References fb(), IsA, and plan.

Referenced by pgpa_build_scan(), and pgpa_qf_add_plan_rtis().

◆ pgpa_scanrelid()

Index pgpa_scanrelid ( Plan plan)
extern

Definition at line 555 of file pgpa_walker.c.

556{
557 switch (nodeTag(plan))
558 {
559 case T_SeqScan:
560 case T_SampleScan:
561 case T_BitmapHeapScan:
562 case T_TidScan:
563 case T_TidRangeScan:
564 case T_SubqueryScan:
565 case T_FunctionScan:
566 case T_TableFuncScan:
567 case T_ValuesScan:
568 case T_CteScan:
570 case T_WorkTableScan:
571 case T_ForeignScan:
572 case T_CustomScan:
573 case T_IndexScan:
574 case T_IndexOnlyScan:
575 return ((Scan *) plan)->scanrelid;
576 default:
577 return 0;
578 }
579}
#define nodeTag(nodeptr)
Definition nodes.h:139

References fb(), nodeTag, and plan.

Referenced by pgpa_build_scan(), and pgpa_qf_add_plan_rtis().

◆ pgpa_walker_would_advise()

bool pgpa_walker_would_advise ( pgpa_plan_walker_context walker,
pgpa_identifier rt_identifiers,
pgpa_advice_tag_type  tag,
pgpa_advice_target target 
)
extern

Definition at line 672 of file pgpa_walker.c.

676{
677 Index rtable_length = list_length(walker->pstmt->rtable);
678 Bitmapset *relids = NULL;
679
680 if (tag == PGPA_TAG_JOIN_ORDER)
681 {
682 foreach_ptr(pgpa_unrolled_join, ujoin, walker->toplevel_unrolled_joins)
683 {
685 rt_identifiers, target, true))
686 return true;
687 }
688
689 return false;
690 }
691
692 if (target->ttype == PGPA_TARGET_IDENTIFIER)
693 {
694 Index rti;
695
697 &target->rid);
698 if (rti == 0)
699 return false;
700 relids = bms_make_singleton(rti);
701 }
702 else
703 {
706 {
707 Index rti;
708
712 &child_target->rid);
713 if (rti == 0)
714 return false;
715 relids = bms_add_member(relids, rti);
716 }
717 }
718
719 switch (tag)
720 {
722 /* should have been handled above */
724 break;
728 relids) != NULL;
732 relids) != NULL;
734 {
735 pgpa_scan *scan;
736
738 relids);
739 if (scan == NULL)
740 return false;
741
743 }
745 {
746 pgpa_scan *scan;
747
749 relids);
750 if (scan == NULL)
751 return false;
752
754 }
758 relids) != NULL;
762 relids) != NULL;
766 relids) != NULL;
767 case PGPA_TAG_GATHER:
770 relids);
774 relids);
778 relids);
782 relids);
786 relids);
790 relids);
794 relids);
798 relids);
802 relids);
806 relids);
809 }
810
811 /* should not get here */
812 return false;
813}
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
#define pg_unreachable()
Definition c.h:361
unsigned int Index
Definition c.h:682
static int list_length(const List *l)
Definition pg_list.h:152
@ PGPA_TAG_INDEX_SCAN
Definition pgpa_ast.h:88
@ PGPA_TAG_NESTED_LOOP_MATERIALIZE
Definition pgpa_ast.h:92
@ PGPA_TAG_MERGE_JOIN_PLAIN
Definition pgpa_ast.h:91
@ PGPA_TAG_GATHER_MERGE
Definition pgpa_ast.h:85
@ PGPA_TAG_GATHER
Definition pgpa_ast.h:84
@ PGPA_TAG_NESTED_LOOP_MEMOIZE
Definition pgpa_ast.h:93
@ PGPA_TAG_SEMIJOIN_NON_UNIQUE
Definition pgpa_ast.h:97
@ PGPA_TAG_BITMAP_HEAP_SCAN
Definition pgpa_ast.h:82
@ PGPA_TAG_PARTITIONWISE
Definition pgpa_ast.h:96
@ PGPA_TAG_NO_GATHER
Definition pgpa_ast.h:95
@ PGPA_TAG_INDEX_ONLY_SCAN
Definition pgpa_ast.h:87
@ PGPA_TAG_SEQ_SCAN
Definition pgpa_ast.h:99
@ PGPA_TAG_HASH_JOIN
Definition pgpa_ast.h:86
@ PGPA_TAG_SEMIJOIN_UNIQUE
Definition pgpa_ast.h:98
@ PGPA_TAG_JOIN_ORDER
Definition pgpa_ast.h:89
@ PGPA_TAG_TID_SCAN
Definition pgpa_ast.h:100
@ PGPA_TAG_FOREIGN_JOIN
Definition pgpa_ast.h:83
@ PGPA_TAG_NESTED_LOOP_PLAIN
Definition pgpa_ast.h:94
@ PGPA_TAG_MERGE_JOIN_MATERIALIZE
Definition pgpa_ast.h:90
@ PGPA_TARGET_IDENTIFIER
Definition pgpa_ast.h:27
@ PGPA_TARGET_ORDERED_LIST
Definition pgpa_ast.h:28
Index pgpa_compute_rti_from_identifier(int rtable_length, pgpa_identifier *rt_identifiers, pgpa_identifier *rid)
@ 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
@ PGPA_SCAN_SEQ
Definition pgpa_scan.h:58
@ PGPA_SCAN_INDEX
Definition pgpa_scan.h:61
@ PGPA_SCAN_INDEX_ONLY
Definition pgpa_scan.h:62
@ PGPA_SCAN_BITMAP_HEAP
Definition pgpa_scan.h:59
@ PGPA_SCAN_FOREIGN
Definition pgpa_scan.h:60
@ PGPA_SCAN_TID
Definition pgpa_scan.h:64
@ PGPA_SCAN_PARTITIONWISE
Definition pgpa_scan.h:63
static bool pgpa_walker_contains_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Bitmapset *relids)
static pgpa_scan * pgpa_walker_find_scan(pgpa_plan_walker_context *walker, pgpa_scan_strategy strategy, Bitmapset *relids)
static bool pgpa_walker_contains_no_gather(pgpa_plan_walker_context *walker, Bitmapset *relids)
static bool pgpa_walker_contains_join(pgpa_plan_walker_context *walker, pgpa_join_strategy strategy, Bitmapset *relids)
static bool pgpa_walker_index_target_matches_plan(pgpa_index_target *itarget, Plan *plan)
static bool pgpa_walker_join_order_matches(pgpa_unrolled_join *ujoin, Index rtable_length, pgpa_identifier *rt_identifiers, pgpa_advice_target *target, bool toplevel)
pgpa_identifier rid
Definition pgpa_ast.h:58
pgpa_target_type ttype
Definition pgpa_ast.h:49
pgpa_index_target * itarget
Definition pgpa_ast.h:64
Plan * plan
Definition pgpa_scan.h:75

References Assert, bms_add_member(), bms_make_singleton(), pgpa_advice_target::children, fb(), foreach_ptr, pgpa_advice_target::itarget, JSTRAT_HASH_JOIN, JSTRAT_MERGE_JOIN_MATERIALIZE, JSTRAT_MERGE_JOIN_PLAIN, JSTRAT_NESTED_LOOP_MATERIALIZE, JSTRAT_NESTED_LOOP_MEMOIZE, JSTRAT_NESTED_LOOP_PLAIN, list_length(), pg_unreachable, pgpa_compute_rti_from_identifier(), PGPA_SCAN_BITMAP_HEAP, PGPA_SCAN_FOREIGN, PGPA_SCAN_INDEX, PGPA_SCAN_INDEX_ONLY, PGPA_SCAN_PARTITIONWISE, PGPA_SCAN_SEQ, PGPA_SCAN_TID, PGPA_TAG_BITMAP_HEAP_SCAN, PGPA_TAG_FOREIGN_JOIN, PGPA_TAG_GATHER, PGPA_TAG_GATHER_MERGE, PGPA_TAG_HASH_JOIN, PGPA_TAG_INDEX_ONLY_SCAN, PGPA_TAG_INDEX_SCAN, PGPA_TAG_JOIN_ORDER, PGPA_TAG_MERGE_JOIN_MATERIALIZE, PGPA_TAG_MERGE_JOIN_PLAIN, PGPA_TAG_NESTED_LOOP_MATERIALIZE, PGPA_TAG_NESTED_LOOP_MEMOIZE, PGPA_TAG_NESTED_LOOP_PLAIN, PGPA_TAG_NO_GATHER, PGPA_TAG_PARTITIONWISE, PGPA_TAG_SEMIJOIN_NON_UNIQUE, PGPA_TAG_SEMIJOIN_UNIQUE, PGPA_TAG_SEQ_SCAN, PGPA_TAG_TID_SCAN, PGPA_TARGET_IDENTIFIER, PGPA_TARGET_ORDERED_LIST, pgpa_walker_contains_feature(), pgpa_walker_contains_join(), pgpa_walker_contains_no_gather(), pgpa_walker_find_scan(), pgpa_walker_index_target_matches_plan(), pgpa_walker_join_order_matches(), PGPAQF_GATHER, PGPAQF_GATHER_MERGE, PGPAQF_SEMIJOIN_NON_UNIQUE, PGPAQF_SEMIJOIN_UNIQUE, pgpa_scan::plan, pgpa_advice_target::rid, and pgpa_advice_target::ttype.

Referenced by pgpa_planner_append_feedback().