PostgreSQL Source Code
git master
Loading...
Searching...
No Matches
pgpa_walker.h
Go to the documentation of this file.
1
/*-------------------------------------------------------------------------
2
*
3
* pgpa_walker.h
4
* Main entrypoints for analyzing a plan to generate an advice string
5
*
6
* Copyright (c) 2016-2026, PostgreSQL Global Development Group
7
*
8
* contrib/pg_plan_advice/pgpa_walker.h
9
*
10
*-------------------------------------------------------------------------
11
*/
12
#ifndef PGPA_WALKER_H
13
#define PGPA_WALKER_H
14
15
#include "
pgpa_ast.h
"
16
#include "
pgpa_join.h
"
17
#include "
pgpa_scan.h
"
18
19
/*
20
* When generating advice, we should emit either SEMIJOIN_UNIQUE advice or
21
* SEMIJOIN_NON_UNIQUE advice for each semijoin depending on whether we chose
22
* to implement it as a semijoin or whether we instead chose to make the
23
* nullable side unique and then perform an inner join. When the make-unique
24
* strategy is not chosen, it's not easy to tell from the final plan tree
25
* whether it was considered. That's awkward, because we don't want to emit
26
* useless SEMIJOIN_NON_UNIQUE advice when there was no decision to be made.
27
*
28
* To avoid that, during planning, we create a pgpa_sj_unique_rel for each
29
* relation that we considered making unique for purposes of semijoin planning.
30
*/
31
typedef
struct
pgpa_sj_unique_rel
32
{
33
char
*
plan_name
;
34
Bitmapset
*
relids
;
35
}
pgpa_sj_unique_rel
;
36
37
/*
38
* We use the term "query feature" to refer to plan nodes that are interesting
39
* in the following way: to generate advice, we'll need to know the set of
40
* same-subquery, non-join RTIs occurring at or below that plan node, without
41
* admixture of parent and child RTIs.
42
*
43
* For example, Gather nodes, designated by PGPAQF_GATHER, and Gather Merge
44
* nodes, designated by PGPAQF_GATHER_MERGE, are query features, because we'll
45
* want to admit some kind of advice that describes the portion of the plan
46
* tree that appears beneath those nodes.
47
*
48
* Each semijoin can be implemented either by directly performing a semijoin,
49
* or by making one side unique and then performing a normal join. Either way,
50
* we use a query feature to notice what decision was made, so that we can
51
* describe it by enumerating the RTIs on that side of the join.
52
*
53
* To elaborate on the "no admixture of parent and child RTIs" rule, in all of
54
* these cases, if the entirety of an inheritance hierarchy appears beneath
55
* the query feature, we only want to name the parent table. But it's also
56
* possible to have cases where we must name child tables. This is particularly
57
* likely to happen when partitionwise join is in use, but could happen for
58
* Gather or Gather Merge even without that, if one of those appears below
59
* an Append or MergeAppend node for a single table.
60
*/
61
typedef
enum
pgpa_qf_type
62
{
63
PGPAQF_GATHER
,
64
PGPAQF_GATHER_MERGE
,
65
PGPAQF_SEMIJOIN_NON_UNIQUE
,
66
PGPAQF_SEMIJOIN_UNIQUE
67
/* update NUM_PGPA_QF_TYPES if you add anything here */
68
}
pgpa_qf_type
;
69
70
#define NUM_PGPA_QF_TYPES ((int) PGPAQF_SEMIJOIN_UNIQUE + 1)
71
72
/*
73
* For each query feature, we keep track of the feature type and the set of
74
* relids that we found underneath the relevant plan node. See the comments
75
* on pgpa_qf_type, above, for additional details.
76
*/
77
typedef
struct
pgpa_query_feature
78
{
79
pgpa_qf_type
type
;
80
Plan
*
plan
;
81
Bitmapset
*
relids
;
82
}
pgpa_query_feature
;
83
84
/*
85
* Context object for plan tree walk.
86
*
87
* pstmt is the PlannedStmt we're studying.
88
*
89
* scans is an array of lists of pgpa_scan objects. The array is indexed by
90
* the scan's pgpa_scan_strategy.
91
*
92
* no_gather_scans is the set of scan RTIs that do not appear beneath any
93
* Gather or Gather Merge node.
94
*
95
* toplevel_unrolled_joins is a list of all pgpa_unrolled_join objects that
96
* are not a child of some other pgpa_unrolled_join.
97
*
98
* join_strategy is an array of lists of Bitmapset objects. Each Bitmapset
99
* is the set of relids that appears on the inner side of some join (excluding
100
* RTIs from partition children and subqueries). The array is indexed by
101
* pgpa_join_strategy.
102
*
103
* query_features is an array lists of pgpa_query_feature objects, indexed
104
* by pgpa_qf_type.
105
*
106
* future_query_features is only used during the plan tree walk and should
107
* be empty when the tree walk concludes. It is a list of pgpa_query_feature
108
* objects for Plan nodes that the plan tree walk has not yet encountered;
109
* when encountered, they will be moved to the list of active query features
110
* that is propagated via the call stack.
111
*/
112
typedef
struct
pgpa_plan_walker_context
113
{
114
PlannedStmt
*
pstmt
;
115
List
*
scans
[
NUM_PGPA_SCAN_STRATEGY
];
116
Bitmapset
*
no_gather_scans
;
117
List
*
toplevel_unrolled_joins
;
118
List
*
join_strategies
[
NUM_PGPA_JOIN_STRATEGY
];
119
List
*
query_features
[
NUM_PGPA_QF_TYPES
];
120
List
*
future_query_features
;
121
}
pgpa_plan_walker_context
;
122
123
extern
void
pgpa_plan_walker
(
pgpa_plan_walker_context
*
walker
,
124
PlannedStmt
*pstmt,
125
List
*sj_unique_rels);
126
127
extern
void
pgpa_add_future_feature
(
pgpa_plan_walker_context
*
walker
,
128
pgpa_qf_type
type
,
129
Plan
*
plan
);
130
131
extern
ElidedNode
*
pgpa_last_elided_node
(
PlannedStmt
*pstmt,
Plan
*
plan
);
132
extern
Bitmapset
*
pgpa_relids
(
Plan
*
plan
);
133
extern
Index
pgpa_scanrelid
(
Plan
*
plan
);
134
extern
Bitmapset
*
pgpa_filter_out_join_relids
(
Bitmapset
*relids,
List
*rtable);
135
136
extern
bool
pgpa_walker_would_advise
(
pgpa_plan_walker_context
*
walker
,
137
pgpa_identifier
*
rt_identifiers
,
138
pgpa_advice_tag_type
tag,
139
pgpa_advice_target
*target);
140
141
#endif
Index
unsigned int Index
Definition
c.h:682
plan
#define plan(x)
Definition
pg_regress.c:161
pgpa_ast.h
pgpa_advice_tag_type
pgpa_advice_tag_type
Definition
pgpa_ast.h:81
pgpa_join.h
NUM_PGPA_JOIN_STRATEGY
#define NUM_PGPA_JOIN_STRATEGY
Definition
pgpa_join.h:38
pgpa_scan.h
NUM_PGPA_SCAN_STRATEGY
#define NUM_PGPA_SCAN_STRATEGY
Definition
pgpa_scan.h:68
pgpa_filter_out_join_relids
Bitmapset * pgpa_filter_out_join_relids(Bitmapset *relids, List *rtable)
Definition
pgpa_walker.c:585
pgpa_qf_type
pgpa_qf_type
Definition
pgpa_walker.h:62
PGPAQF_GATHER
@ PGPAQF_GATHER
Definition
pgpa_walker.h:63
PGPAQF_GATHER_MERGE
@ PGPAQF_GATHER_MERGE
Definition
pgpa_walker.h:64
PGPAQF_SEMIJOIN_UNIQUE
@ PGPAQF_SEMIJOIN_UNIQUE
Definition
pgpa_walker.h:66
PGPAQF_SEMIJOIN_NON_UNIQUE
@ PGPAQF_SEMIJOIN_NON_UNIQUE
Definition
pgpa_walker.h:65
pgpa_relids
Bitmapset * pgpa_relids(Plan *plan)
Definition
pgpa_walker.c:535
pgpa_last_elided_node
ElidedNode * pgpa_last_elided_node(PlannedStmt *pstmt, Plan *plan)
Definition
pgpa_walker.c:518
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)
Definition
pgpa_walker.c:672
pgpa_plan_walker
void pgpa_plan_walker(pgpa_plan_walker_context *walker, PlannedStmt *pstmt, List *sj_unique_rels)
Definition
pgpa_walker.c:71
NUM_PGPA_QF_TYPES
#define NUM_PGPA_QF_TYPES
Definition
pgpa_walker.h:70
pgpa_add_future_feature
void pgpa_add_future_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan)
Definition
pgpa_walker.c:502
pgpa_scanrelid
Index pgpa_scanrelid(Plan *plan)
Definition
pgpa_walker.c:555
fb
static int fb(int x)
Definition
preproc-init.c:92
Bitmapset
Definition
bitmapset.h:50
ElidedNode
Definition
plannodes.h:1867
List
Definition
pg_list.h:54
Plan
Definition
plannodes.h:192
PlannedStmt
Definition
plannodes.h:62
pgpa_advice_target
Definition
pgpa_ast.h:48
pgpa_identifier
Definition
pgpa_identifier.h:20
pgpa_plan_walker_context
Definition
pgpa_walker.h:113
pgpa_plan_walker_context::scans
List * scans[NUM_PGPA_SCAN_STRATEGY]
Definition
pgpa_walker.h:115
pgpa_plan_walker_context::pstmt
PlannedStmt * pstmt
Definition
pgpa_walker.h:114
pgpa_plan_walker_context::query_features
List * query_features[NUM_PGPA_QF_TYPES]
Definition
pgpa_walker.h:119
pgpa_plan_walker_context::future_query_features
List * future_query_features
Definition
pgpa_walker.h:120
pgpa_plan_walker_context::no_gather_scans
Bitmapset * no_gather_scans
Definition
pgpa_walker.h:116
pgpa_plan_walker_context::join_strategies
List * join_strategies[NUM_PGPA_JOIN_STRATEGY]
Definition
pgpa_walker.h:118
pgpa_plan_walker_context::toplevel_unrolled_joins
List * toplevel_unrolled_joins
Definition
pgpa_walker.h:117
pgpa_query_feature
Definition
pgpa_walker.h:78
pgpa_query_feature::type
pgpa_qf_type type
Definition
pgpa_walker.h:79
pgpa_query_feature::plan
Plan * plan
Definition
pgpa_walker.h:80
pgpa_query_feature::relids
Bitmapset * relids
Definition
pgpa_walker.h:81
pgpa_sj_unique_rel
Definition
pgpa_walker.h:32
pgpa_sj_unique_rel::plan_name
char * plan_name
Definition
pgpa_walker.h:33
pgpa_sj_unique_rel::relids
Bitmapset * relids
Definition
pgpa_walker.h:34
type
const char * type
Definition
wait_event_funcs.c:27
contrib
pg_plan_advice
pgpa_walker.h
Generated on Fri Mar 13 2026 12:13:10 for PostgreSQL Source Code by
1.9.8