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_query_feature
 
struct  pgpa_plan_walker_context
 

Macros

#define NUM_PGPA_QF_TYPES   ((int) PGPAQF_SEMIJOIN_UNIQUE + 1)
 

Typedefs

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 *proots)
 
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 52 of file pgpa_walker.h.

Typedef Documentation

◆ pgpa_plan_walker_context

◆ pgpa_qf_type

◆ pgpa_query_feature

Enumeration Type Documentation

◆ pgpa_qf_type

Enumerator
PGPAQF_GATHER 
PGPAQF_GATHER_MERGE 
PGPAQF_SEMIJOIN_NON_UNIQUE 
PGPAQF_SEMIJOIN_UNIQUE 

Definition at line 43 of file pgpa_walker.h.

44{
49 /* update NUM_PGPA_QF_TYPES if you add anything here */
pgpa_qf_type
Definition pgpa_walker.h:44
@ PGPAQF_GATHER
Definition pgpa_walker.h:45
@ PGPAQF_GATHER_MERGE
Definition pgpa_walker.h:46
@ PGPAQF_SEMIJOIN_UNIQUE
Definition pgpa_walker.h:48
@ PGPAQF_SEMIJOIN_NON_UNIQUE
Definition pgpa_walker.h:47

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 533 of file pgpa_walker.c.

535{
537
538 walker->future_query_features =
539 lappend(walker->future_query_features, qf);
540}
List * lappend(List *list, void *datum)
Definition list.c:339
#define plan(x)
Definition pg_regress.c:164
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 616 of file pgpa_walker.c.

617{
618 int rti = -1;
619 Bitmapset *result = NULL;
620
621 while ((rti = bms_next_member(relids, rti)) >= 0)
622 {
623 RangeTblEntry *rte = rt_fetch(rti, rtable);
624
625 if (rte->rtekind != RTE_JOIN)
626 result = bms_add_member(result, rti);
627 }
628
629 return result;
630}
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 549 of file pgpa_walker.c.

550{
551 ElidedNode *elided_node = NULL;
552
554 {
555 if (n->plan_node_id == plan->plan_node_id)
556 elided_node = n;
557 }
558
559 return elided_node;
560}
#define foreach_node(type, var, lst)
Definition pg_list.h:528
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 proots 
)
extern

Definition at line 76 of file pgpa_walker.c.

78{
79 ListCell *lc;
84
85 /* Initialization. */
87 walker->pstmt = pstmt;
88
89 /* Walk the main plan tree. */
90 pgpa_walk_recursively(walker, pstmt->planTree, false, NULL, NIL, false);
91
92 /* Main plan tree walk won't reach subplans, so walk those. */
93 foreach(lc, pstmt->subplans)
94 {
95 Plan *plan = lfirst(lc);
96
97 if (plan != NULL)
98 pgpa_walk_recursively(walker, plan, false, NULL, NIL, false);
99 }
100
101 /* Adjust RTIs from sj_unique_rels for the flattened range table. */
103 {
104 /* If there are no sj_unique_rels for this proot, we can skip it. */
105 if (proot->sj_unique_rels == NIL)
106 continue;
107
108 /* If this is a subplan, find the range table offset. */
109 if (!proot->has_rtoffset)
110 elog(ERROR, "no rtoffset for plan %s", proot->plan_name);
111
112 /* Offset each relid set by the proot's rtoffset. */
113 foreach_node(Bitmapset, relids, proot->sj_unique_rels)
114 {
115 int rtindex = -1;
117
118 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
120 rtindex + proot->rtoffset);
121
123 }
124 }
125
126 /*
127 * Remove any non-unique semijoin query features for which making the rel
128 * unique wasn't considered.
129 */
131 walker->query_features[PGPAQF_SEMIJOIN_NON_UNIQUE])
132 {
133 if (list_member(sj_unique_rtis, qf->relids))
135 }
137
138 /*
139 * If we find any cases where analysis of the Plan tree shows that the
140 * semijoin was made unique but this possibility was never observed to be
141 * considered during planning, then we have a bug somewhere.
142 */
144 walker->query_features[PGPAQF_SEMIJOIN_UNIQUE])
145 {
146 if (!list_member(sj_unique_rtis, qf->relids))
147 {
149
151 outBitmapset(&buf, qf->relids);
152 elog(ERROR,
153 "unique semijoin found for relids %s but not observed during planning",
154 buf.data);
155 }
156 }
157
158 /*
159 * It's possible for a Gather or Gather Merge query feature to find no
160 * RTIs when partitionwise aggregation is in use. We shouldn't emit
161 * something like GATHER_MERGE(()), so instead emit nothing. This means
162 * that we won't advise either GATHER or GATHER_MERGE or NO_GATHER in such
163 * cases, which might be something we want to improve in the future.
164 *
165 * (Should the Partial Aggregates in such a case be created in an
166 * UPPERREL_GROUP_AGG with a non-empty relid set? Right now that doesn't
167 * happen, but it seems like it would make life easier for us if it did.)
168 */
169 for (int t = 0; t < NUM_PGPA_QF_TYPES; ++t)
170 {
171 List *query_features = NIL;
172
173 foreach_ptr(pgpa_query_feature, qf, walker->query_features[t])
174 {
175 if (qf->relids != NULL)
176 query_features = lappend(query_features, qf);
177 else
179 }
180
181 walker->query_features[t] = query_features;
182 }
183
184 /* Classify alternative subplans. */
187
188 /*
189 * Figure out which of the discarded alternatives have a non-discarded
190 * alternative. Those are the ones for which we want to emit DO_NOT_SCAN
191 * advice. (If every alternative was discarded, then there's no point.)
192 */
194 {
195 bool some_alternative_chosen = false;
196
198 {
199 if (strings_equal_or_both_null(discarded_proot->alternative_plan_name,
200 chosen_proot->alternative_plan_name))
201 {
203 break;
204 }
205 }
206
208 {
209 for (int rti = 1; rti <= discarded_proot->rid_array_size; rti++)
210 {
211 pgpa_identifier *rid = &discarded_proot->rid_array[rti - 1];
212
213 if (rid->alias_name != NULL)
214 walker->do_not_scan_identifiers =
215 lappend(walker->do_not_scan_identifiers, rid);
216 }
217 }
218 }
219}
#define Assert(condition)
Definition c.h:943
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:227
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:501
static char buf[DEFAULT_XLOG_SEG_SIZE]
static bool strings_equal_or_both_null(const char *a, const char *b)
static void pgpa_classify_alternative_subplans(pgpa_plan_walker_context *walker, List *proots, List **chosen_proots, List **discarded_proots)
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:52
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Definition pg_list.h:54
struct Plan * planTree
Definition plannodes.h:99
List * subplans
Definition plannodes.h:129
const char * alias_name

References pgpa_identifier::alias_name, 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_classify_alternative_subplans(), pgpa_walk_recursively(), PGPAQF_GATHER, PGPAQF_GATHER_MERGE, PGPAQF_SEMIJOIN_NON_UNIQUE, PGPAQF_SEMIJOIN_UNIQUE, plan, PlannedStmt::planTree, strings_equal_or_both_null(), and PlannedStmt::subplans.

Referenced by pgpa_planner_shutdown().

◆ pgpa_relids()

Bitmapset * pgpa_relids ( Plan plan)
extern

Definition at line 566 of file pgpa_walker.c.

567{
568 if (IsA(plan, Result))
569 return ((Result *) plan)->relids;
570 else if (IsA(plan, ForeignScan))
571 return ((ForeignScan *) plan)->fs_relids;
572 else if (IsA(plan, Append))
573 return ((Append *) plan)->apprelids;
574 else if (IsA(plan, MergeAppend))
575 return ((MergeAppend *) plan)->apprelids;
576
577 return NULL;
578}
#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 586 of file pgpa_walker.c.

587{
588 switch (nodeTag(plan))
589 {
590 case T_SeqScan:
591 case T_SampleScan:
592 case T_BitmapHeapScan:
593 case T_TidScan:
594 case T_TidRangeScan:
595 case T_SubqueryScan:
596 case T_FunctionScan:
597 case T_TableFuncScan:
598 case T_ValuesScan:
599 case T_CteScan:
601 case T_WorkTableScan:
602 case T_ForeignScan:
603 case T_CustomScan:
604 case T_IndexScan:
605 case T_IndexOnlyScan:
606 return ((Scan *) plan)->scanrelid;
607 default:
608 return 0;
609 }
610}
#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 703 of file pgpa_walker.c.

707{
708 Index rtable_length = list_length(walker->pstmt->rtable);
709 Bitmapset *relids = NULL;
710
711 if (tag == PGPA_TAG_JOIN_ORDER)
712 {
713 foreach_ptr(pgpa_unrolled_join, ujoin, walker->toplevel_unrolled_joins)
714 {
716 rt_identifiers, target, true))
717 return true;
718 }
719
720 return false;
721 }
722
723 /*
724 * DO_NOT_SCAN advice targets rels that may not be in the flat range table
725 * (e.g. MinMaxAgg losers), so pgpa_compute_rti_from_identifier won't work
726 * here. Instead, check directly against the do_not_scan_identifiers list.
727 */
728 if (tag == PGPA_TAG_DO_NOT_SCAN)
729 {
730 if (target->ttype != PGPA_TARGET_IDENTIFIER)
731 return false;
732 foreach_ptr(pgpa_identifier, rid, walker->do_not_scan_identifiers)
733 {
734 if (strcmp(rid->alias_name, target->rid.alias_name) == 0 &&
735 rid->occurrence == target->rid.occurrence &&
736 strings_equal_or_both_null(rid->partnsp,
737 target->rid.partnsp) &&
738 strings_equal_or_both_null(rid->partrel,
739 target->rid.partrel) &&
740 strings_equal_or_both_null(rid->plan_name,
741 target->rid.plan_name))
742 return true;
743 }
744 return false;
745 }
746
747 if (target->ttype == PGPA_TARGET_IDENTIFIER)
748 {
749 Index rti;
750
752 &target->rid);
753 if (rti == 0)
754 return false;
755 relids = bms_make_singleton(rti);
756 }
757 else
758 {
761 {
762 Index rti;
763
767 &child_target->rid);
768 if (rti == 0)
769 return false;
770 relids = bms_add_member(relids, rti);
771 }
772 }
773
774 switch (tag)
775 {
777 /* should have been handled above */
779 break;
781 /* should have been handled above */
783 break;
787 relids) != NULL;
791 relids) != NULL;
793 {
794 pgpa_scan *scan;
795
797 relids);
798 if (scan == NULL)
799 return false;
800
802 }
804 {
805 pgpa_scan *scan;
806
808 relids);
809 if (scan == NULL)
810 return false;
811
813 }
817 relids) != NULL;
821 relids) != NULL;
825 relids) != NULL;
826 case PGPA_TAG_GATHER:
829 relids);
833 relids);
837 relids);
841 relids);
845 relids);
849 relids);
853 relids);
857 relids);
861 relids);
865 relids);
868 }
869
870 /* should not get here */
871 return false;
872}
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
#define pg_unreachable()
Definition c.h:367
unsigned int Index
Definition c.h:698
static int list_length(const List *l)
Definition pg_list.h:152
@ PGPA_TAG_INDEX_SCAN
Definition pgpa_ast.h:89
@ PGPA_TAG_NESTED_LOOP_MATERIALIZE
Definition pgpa_ast.h:93
@ PGPA_TAG_MERGE_JOIN_PLAIN
Definition pgpa_ast.h:92
@ PGPA_TAG_GATHER_MERGE
Definition pgpa_ast.h:86
@ PGPA_TAG_GATHER
Definition pgpa_ast.h:85
@ PGPA_TAG_NESTED_LOOP_MEMOIZE
Definition pgpa_ast.h:94
@ PGPA_TAG_SEMIJOIN_NON_UNIQUE
Definition pgpa_ast.h:98
@ PGPA_TAG_BITMAP_HEAP_SCAN
Definition pgpa_ast.h:82
@ PGPA_TAG_PARTITIONWISE
Definition pgpa_ast.h:97
@ PGPA_TAG_NO_GATHER
Definition pgpa_ast.h:96
@ PGPA_TAG_INDEX_ONLY_SCAN
Definition pgpa_ast.h:88
@ PGPA_TAG_SEQ_SCAN
Definition pgpa_ast.h:100
@ PGPA_TAG_HASH_JOIN
Definition pgpa_ast.h:87
@ PGPA_TAG_SEMIJOIN_UNIQUE
Definition pgpa_ast.h:99
@ PGPA_TAG_DO_NOT_SCAN
Definition pgpa_ast.h:83
@ PGPA_TAG_JOIN_ORDER
Definition pgpa_ast.h:90
@ PGPA_TAG_TID_SCAN
Definition pgpa_ast.h:101
@ PGPA_TAG_FOREIGN_JOIN
Definition pgpa_ast.h:84
@ PGPA_TAG_NESTED_LOOP_PLAIN
Definition pgpa_ast.h:95
@ PGPA_TAG_MERGE_JOIN_MATERIALIZE
Definition pgpa_ast.h:91
@ 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
const char * partnsp
const char * partrel
const char * plan_name
Plan * plan
Definition pgpa_scan.h:75

References pgpa_identifier::alias_name, 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(), pgpa_identifier::occurrence, pgpa_identifier::partnsp, pgpa_identifier::partrel, 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_DO_NOT_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_identifier::plan_name, pgpa_advice_target::rid, strings_equal_or_both_null(), and pgpa_advice_target::ttype.

Referenced by pgpa_planner_append_feedback().