1 /*-------------------------------------------------------------------------
2  *
3  * optimizer.h
4  *	  External API for the Postgres planner.
5  *
6  * This header is meant to define everything that the core planner
7  * exposes for use by non-planner modules.
8  *
9  * Note that there are files outside src/backend/optimizer/ that are
10  * considered planner modules, because they're too much in bed with
11  * planner operations to be treated otherwise.  FDW planning code is an
12  * example.  For the most part, however, code outside the core planner
13  * should not need to include any optimizer/ header except this one.
14  *
15  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
16  * Portions Copyright (c) 1994, Regents of the University of California
17  *
18  * src/include/optimizer/optimizer.h
19  *
20  *-------------------------------------------------------------------------
21  */
22 #ifndef OPTIMIZER_H
23 #define OPTIMIZER_H
24 
25 #include "nodes/parsenodes.h"
26 
27 /* Test if an expression node represents a SRF call.  Beware multiple eval! */
28 #define IS_SRF_CALL(node) \
29 	((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \
30 	 (IsA(node, OpExpr) && ((OpExpr *) (node))->opretset))
31 
32 /*
33  * We don't want to include nodes/pathnodes.h here, because non-planner
34  * code should generally treat PlannerInfo as an opaque typedef.
35  * But we'd like such code to use that typedef name, so define the
36  * typedef either here or in pathnodes.h, whichever is read first.
37  */
38 #ifndef HAVE_PLANNERINFO_TYPEDEF
39 typedef struct PlannerInfo PlannerInfo;
40 #define HAVE_PLANNERINFO_TYPEDEF 1
41 #endif
42 
43 /* Likewise for IndexOptInfo and SpecialJoinInfo. */
44 #ifndef HAVE_INDEXOPTINFO_TYPEDEF
45 typedef struct IndexOptInfo IndexOptInfo;
46 #define HAVE_INDEXOPTINFO_TYPEDEF 1
47 #endif
48 #ifndef HAVE_SPECIALJOININFO_TYPEDEF
49 typedef struct SpecialJoinInfo SpecialJoinInfo;
50 #define HAVE_SPECIALJOININFO_TYPEDEF 1
51 #endif
52 
53 /* It also seems best not to include plannodes.h, params.h, or htup.h here */
54 struct PlannedStmt;
55 struct ParamListInfoData;
56 struct HeapTupleData;
57 
58 
59 /* in path/clausesel.c: */
60 
61 extern Selectivity clause_selectivity(PlannerInfo *root,
62 									  Node *clause,
63 									  int varRelid,
64 									  JoinType jointype,
65 									  SpecialJoinInfo *sjinfo);
66 extern Selectivity clauselist_selectivity_simple(PlannerInfo *root,
67 												 List *clauses,
68 												 int varRelid,
69 												 JoinType jointype,
70 												 SpecialJoinInfo *sjinfo,
71 												 Bitmapset *estimatedclauses);
72 extern Selectivity clauselist_selectivity(PlannerInfo *root,
73 										  List *clauses,
74 										  int varRelid,
75 										  JoinType jointype,
76 										  SpecialJoinInfo *sjinfo);
77 
78 /* in path/costsize.c: */
79 
80 /* widely used cost parameters */
81 extern PGDLLIMPORT double seq_page_cost;
82 extern PGDLLIMPORT double random_page_cost;
83 extern PGDLLIMPORT double cpu_tuple_cost;
84 extern PGDLLIMPORT double cpu_index_tuple_cost;
85 extern PGDLLIMPORT double cpu_operator_cost;
86 extern PGDLLIMPORT double parallel_tuple_cost;
87 extern PGDLLIMPORT double parallel_setup_cost;
88 extern PGDLLIMPORT int effective_cache_size;
89 
90 extern double clamp_row_est(double nrows);
91 
92 /* in path/indxpath.c: */
93 
94 extern bool is_pseudo_constant_for_index(Node *expr, IndexOptInfo *index);
95 extern bool is_pseudo_constant_for_index_new(PlannerInfo *root, Node *expr,
96 											 IndexOptInfo *index);
97 
98 /* in plan/planner.c: */
99 
100 /* possible values for force_parallel_mode */
101 typedef enum
102 {
103 	FORCE_PARALLEL_OFF,
104 	FORCE_PARALLEL_ON,
105 	FORCE_PARALLEL_REGRESS
106 }			ForceParallelMode;
107 
108 /* GUC parameters */
109 extern int	force_parallel_mode;
110 extern bool parallel_leader_participation;
111 
112 extern struct PlannedStmt *planner(Query *parse, const char *query_string,
113 								   int cursorOptions,
114 								   struct ParamListInfoData *boundParams);
115 
116 extern Expr *expression_planner(Expr *expr);
117 extern Expr *expression_planner_with_deps(Expr *expr,
118 										  List **relationOids,
119 										  List **invalItems);
120 
121 extern bool plan_cluster_use_sort(Oid tableOid, Oid indexOid);
122 extern int	plan_create_index_workers(Oid tableOid, Oid indexOid);
123 
124 /* in plan/setrefs.c: */
125 
126 extern void extract_query_dependencies(Node *query,
127 									   List **relationOids,
128 									   List **invalItems,
129 									   bool *hasRowSecurity);
130 
131 /* in prep/prepqual.c: */
132 
133 extern Node *negate_clause(Node *node);
134 extern Expr *canonicalize_qual(Expr *qual, bool is_check);
135 
136 /* in util/clauses.c: */
137 
138 extern bool contain_mutable_functions(Node *clause);
139 extern bool contain_volatile_functions(Node *clause);
140 extern bool contain_volatile_functions_not_nextval(Node *clause);
141 
142 extern Node *eval_const_expressions(PlannerInfo *root, Node *node);
143 
144 extern Node *estimate_expression_value(PlannerInfo *root, Node *node);
145 
146 extern Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
147 						   Oid result_collation);
148 
149 extern List *expand_function_arguments(List *args, Oid result_type,
150 									   struct HeapTupleData *func_tuple);
151 
152 /* in util/predtest.c: */
153 
154 extern bool predicate_implied_by(List *predicate_list, List *clause_list,
155 								 bool weak);
156 extern bool predicate_refuted_by(List *predicate_list, List *clause_list,
157 								 bool weak);
158 
159 /* in util/tlist.c: */
160 
161 extern int	count_nonjunk_tlist_entries(List *tlist);
162 extern TargetEntry *get_sortgroupref_tle(Index sortref,
163 										 List *targetList);
164 extern TargetEntry *get_sortgroupclause_tle(SortGroupClause *sgClause,
165 											List *targetList);
166 extern Node *get_sortgroupclause_expr(SortGroupClause *sgClause,
167 									  List *targetList);
168 extern List *get_sortgrouplist_exprs(List *sgClauses,
169 									 List *targetList);
170 extern SortGroupClause *get_sortgroupref_clause(Index sortref,
171 												List *clauses);
172 extern SortGroupClause *get_sortgroupref_clause_noerr(Index sortref,
173 													  List *clauses);
174 
175 /* in util/var.c: */
176 
177 /* Bits that can be OR'd into the flags argument of pull_var_clause() */
178 #define PVC_INCLUDE_AGGREGATES	0x0001	/* include Aggrefs in output list */
179 #define PVC_RECURSE_AGGREGATES	0x0002	/* recurse into Aggref arguments */
180 #define PVC_INCLUDE_WINDOWFUNCS 0x0004	/* include WindowFuncs in output list */
181 #define PVC_RECURSE_WINDOWFUNCS 0x0008	/* recurse into WindowFunc arguments */
182 #define PVC_INCLUDE_PLACEHOLDERS	0x0010	/* include PlaceHolderVars in
183 											 * output list */
184 #define PVC_RECURSE_PLACEHOLDERS	0x0020	/* recurse into PlaceHolderVar
185 											 * arguments */
186 
187 extern Bitmapset *pull_varnos(Node *node);
188 extern Bitmapset *pull_varnos_of_level(Node *node, int levelsup);
189 extern Bitmapset *pull_varnos_new(PlannerInfo *root, Node *node);
190 extern Bitmapset *pull_varnos_of_level_new(PlannerInfo *root, Node *node, int levelsup);
191 extern void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos);
192 extern List *pull_vars_of_level(Node *node, int levelsup);
193 extern bool contain_var_clause(Node *node);
194 extern bool contain_vars_of_level(Node *node, int levelsup);
195 extern int	locate_var_of_level(Node *node, int levelsup);
196 extern List *pull_var_clause(Node *node, int flags);
197 extern Node *flatten_join_alias_vars(Query *query, Node *node);
198 
199 #endif							/* OPTIMIZER_H */
200