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-2019, 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 /*
28  * We don't want to include nodes/pathnodes.h here, because non-planner
29  * code should generally treat PlannerInfo as an opaque typedef.
30  * But we'd like such code to use that typedef name, so define the
31  * typedef either here or in pathnodes.h, whichever is read first.
32  */
33 #ifndef HAVE_PLANNERINFO_TYPEDEF
34 typedef struct PlannerInfo PlannerInfo;
35 #define HAVE_PLANNERINFO_TYPEDEF 1
36 #endif
37 
38 /* Likewise for IndexOptInfo and SpecialJoinInfo. */
39 #ifndef HAVE_INDEXOPTINFO_TYPEDEF
40 typedef struct IndexOptInfo IndexOptInfo;
41 #define HAVE_INDEXOPTINFO_TYPEDEF 1
42 #endif
43 #ifndef HAVE_SPECIALJOININFO_TYPEDEF
44 typedef struct SpecialJoinInfo SpecialJoinInfo;
45 #define HAVE_SPECIALJOININFO_TYPEDEF 1
46 #endif
47 
48 /* It also seems best not to include plannodes.h, params.h, or htup.h here */
49 struct PlannedStmt;
50 struct ParamListInfoData;
51 struct HeapTupleData;
52 
53 
54 /* in path/clausesel.c: */
55 
56 extern Selectivity clause_selectivity(PlannerInfo *root,
57 									  Node *clause,
58 									  int varRelid,
59 									  JoinType jointype,
60 									  SpecialJoinInfo *sjinfo);
61 extern Selectivity clauselist_selectivity_simple(PlannerInfo *root,
62 												 List *clauses,
63 												 int varRelid,
64 												 JoinType jointype,
65 												 SpecialJoinInfo *sjinfo,
66 												 Bitmapset *estimatedclauses);
67 extern Selectivity clauselist_selectivity(PlannerInfo *root,
68 										  List *clauses,
69 										  int varRelid,
70 										  JoinType jointype,
71 										  SpecialJoinInfo *sjinfo);
72 
73 /* in path/costsize.c: */
74 
75 /* widely used cost parameters */
76 extern PGDLLIMPORT double seq_page_cost;
77 extern PGDLLIMPORT double random_page_cost;
78 extern PGDLLIMPORT double cpu_tuple_cost;
79 extern PGDLLIMPORT double cpu_index_tuple_cost;
80 extern PGDLLIMPORT double cpu_operator_cost;
81 extern PGDLLIMPORT double parallel_tuple_cost;
82 extern PGDLLIMPORT double parallel_setup_cost;
83 extern PGDLLIMPORT int effective_cache_size;
84 
85 extern double clamp_row_est(double nrows);
86 
87 /* in path/indxpath.c: */
88 
89 extern bool is_pseudo_constant_for_index(Node *expr, IndexOptInfo *index);
90 extern bool is_pseudo_constant_for_index_new(PlannerInfo *root, Node *expr,
91 											 IndexOptInfo *index);
92 
93 /* in plan/planner.c: */
94 
95 /* possible values for force_parallel_mode */
96 typedef enum
97 {
98 	FORCE_PARALLEL_OFF,
99 	FORCE_PARALLEL_ON,
100 	FORCE_PARALLEL_REGRESS
101 }			ForceParallelMode;
102 
103 /* GUC parameters */
104 extern int	force_parallel_mode;
105 extern bool parallel_leader_participation;
106 
107 extern struct PlannedStmt *planner(Query *parse, int cursorOptions,
108 								   struct ParamListInfoData *boundParams);
109 
110 extern Expr *expression_planner(Expr *expr);
111 extern Expr *expression_planner_with_deps(Expr *expr,
112 										  List **relationOids,
113 										  List **invalItems);
114 
115 extern bool plan_cluster_use_sort(Oid tableOid, Oid indexOid);
116 extern int	plan_create_index_workers(Oid tableOid, Oid indexOid);
117 
118 /* in plan/setrefs.c: */
119 
120 extern void extract_query_dependencies(Node *query,
121 									   List **relationOids,
122 									   List **invalItems,
123 									   bool *hasRowSecurity);
124 
125 /* in prep/prepqual.c: */
126 
127 extern Node *negate_clause(Node *node);
128 extern Expr *canonicalize_qual(Expr *qual, bool is_check);
129 
130 /* in util/clauses.c: */
131 
132 extern bool contain_mutable_functions(Node *clause);
133 extern bool contain_volatile_functions(Node *clause);
134 extern bool contain_volatile_functions_not_nextval(Node *clause);
135 
136 extern Node *eval_const_expressions(PlannerInfo *root, Node *node);
137 
138 extern Node *estimate_expression_value(PlannerInfo *root, Node *node);
139 
140 extern Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
141 						   Oid result_collation);
142 
143 extern List *expand_function_arguments(List *args, Oid result_type,
144 									   struct HeapTupleData *func_tuple);
145 
146 /* in util/predtest.c: */
147 
148 extern bool predicate_implied_by(List *predicate_list, List *clause_list,
149 								 bool weak);
150 extern bool predicate_refuted_by(List *predicate_list, List *clause_list,
151 								 bool weak);
152 
153 /* in util/tlist.c: */
154 
155 extern int	count_nonjunk_tlist_entries(List *tlist);
156 extern TargetEntry *get_sortgroupref_tle(Index sortref,
157 										 List *targetList);
158 extern TargetEntry *get_sortgroupclause_tle(SortGroupClause *sgClause,
159 											List *targetList);
160 extern Node *get_sortgroupclause_expr(SortGroupClause *sgClause,
161 									  List *targetList);
162 extern List *get_sortgrouplist_exprs(List *sgClauses,
163 									 List *targetList);
164 extern SortGroupClause *get_sortgroupref_clause(Index sortref,
165 												List *clauses);
166 extern SortGroupClause *get_sortgroupref_clause_noerr(Index sortref,
167 													  List *clauses);
168 
169 /* in util/var.c: */
170 
171 /* Bits that can be OR'd into the flags argument of pull_var_clause() */
172 #define PVC_INCLUDE_AGGREGATES	0x0001	/* include Aggrefs in output list */
173 #define PVC_RECURSE_AGGREGATES	0x0002	/* recurse into Aggref arguments */
174 #define PVC_INCLUDE_WINDOWFUNCS 0x0004	/* include WindowFuncs in output list */
175 #define PVC_RECURSE_WINDOWFUNCS 0x0008	/* recurse into WindowFunc arguments */
176 #define PVC_INCLUDE_PLACEHOLDERS	0x0010	/* include PlaceHolderVars in
177 											 * output list */
178 #define PVC_RECURSE_PLACEHOLDERS	0x0020	/* recurse into PlaceHolderVar
179 											 * arguments */
180 
181 extern Bitmapset *pull_varnos(Node *node);
182 extern Bitmapset *pull_varnos_of_level(Node *node, int levelsup);
183 extern Bitmapset *pull_varnos_new(PlannerInfo *root, Node *node);
184 extern Bitmapset *pull_varnos_of_level_new(PlannerInfo *root, Node *node, int levelsup);
185 extern void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos);
186 extern List *pull_vars_of_level(Node *node, int levelsup);
187 extern bool contain_var_clause(Node *node);
188 extern bool contain_vars_of_level(Node *node, int levelsup);
189 extern int	locate_var_of_level(Node *node, int levelsup);
190 extern List *pull_var_clause(Node *node, int flags);
191 extern Node *flatten_join_alias_vars(Query *query, Node *node);
192 
193 #endif							/* OPTIMIZER_H */
194