133
133
#include "executor/execdesc.h"
134
134
#include "nodes/makefuncs.h"
135
135
#include "nodes/nodeFuncs.h"
136
+ #include "optimizer/pathnode.h"
136
137
#include "optimizer/planmain.h"
137
138
#include "optimizer/planner.h"
138
139
#include "optimizer/cost.h"
144
145
#include "utils/hsearch.h"
145
146
#include "utils/memutils.h"
146
147
#include "utils/rel.h"
147
- #include "utils/tqual.h"
148
148
#include "utils/fmgroids.h"
149
149
#include "utils/snapmgr.h"
150
150
@@ -165,11 +165,19 @@ typedef enum
165
165
AQO_MODE_CONTROLLED ,
166
166
/* Creates new feature space for each query type without auto-tuning */
167
167
AQO_MODE_LEARN ,
168
+ /* Use only current AQO estimations, without learning or tuning */
169
+ AQO_MODE_FIXED ,
168
170
/* Aqo is disabled for all queries */
169
171
AQO_MODE_DISABLED ,
170
172
} AQO_MODE ;
171
173
extern int aqo_mode ;
172
174
175
+ /*
176
+ * It is mostly needed for auto tuning of query. with auto tuning mode aqo
177
+ * checks stability of last executions of the query, bad influence of strong
178
+ * cardinality estimation on query execution (planner bug?) and so on.
179
+ * It can induce aqo to suppress machine learning for this query.
180
+ */
173
181
typedef struct
174
182
{
175
183
double * execution_time_with_aqo ;
@@ -178,12 +186,14 @@ typedef struct
178
186
double * planning_time_without_aqo ;
179
187
double * cardinality_error_with_aqo ;
180
188
double * cardinality_error_without_aqo ;
189
+
181
190
int execution_time_with_aqo_size ;
182
191
int execution_time_without_aqo_size ;
183
192
int planning_time_with_aqo_size ;
184
193
int planning_time_without_aqo_size ;
185
194
int cardinality_error_with_aqo_size ;
186
195
int cardinality_error_without_aqo_size ;
196
+
187
197
int64 executions_with_aqo ;
188
198
int64 executions_without_aqo ;
189
199
} QueryStat ;
@@ -205,6 +215,9 @@ typedef struct QueryContextData
205
215
double query_planning_time ;
206
216
} QueryContextData ;
207
217
218
+ extern double predicted_ppi_rows ;
219
+ extern double fss_ppi_hash ;
220
+
208
221
/* Parameters of autotuning */
209
222
extern int aqo_stat_size ;
210
223
extern int auto_tuning_window_size ;
@@ -213,11 +226,12 @@ extern int auto_tuning_max_iterations;
213
226
extern int auto_tuning_infinite_loop ;
214
227
215
228
/* Machine learning parameters */
216
- extern double object_selection_prediction_threshold ;
217
- extern double object_selection_object_threshold ;
218
- extern double learning_rate ;
229
+ #define aqo_K (30)
230
+
231
+ extern const double object_selection_prediction_threshold ;
232
+ extern const double object_selection_threshold ;
233
+ extern const double learning_rate ;
219
234
extern int aqo_k ;
220
- extern int aqo_K ;
221
235
extern double log_selectivity_lower_bound ;
222
236
223
237
/* Parameters for current query */
@@ -244,11 +258,12 @@ extern copy_generic_path_info_hook_type
244
258
prev_copy_generic_path_info_hook ;
245
259
extern ExplainOnePlan_hook_type prev_ExplainOnePlan_hook ;
246
260
261
+ extern void ppi_hook (ParamPathInfo * ppi );
247
262
248
263
/* Hash functions */
249
264
int get_query_hash (Query * parse , const char * query_text );
250
- void get_fss_for_object (List * clauselist , List * selectivities , List * relidslist ,
251
- int * nfeatures , int * fss_hash , double * * features );
265
+ extern int get_fss_for_object (List * clauselist , List * selectivities ,
266
+ List * relidslist , int * nfeatures , double * * features );
252
267
void get_eclasses (List * clauselist , int * nargs , int * * args_hash , int * * eclass_hash );
253
268
int get_clause_hash (Expr * clause , int nargs , int * args_hash , int * eclass_hash );
254
269
@@ -264,9 +279,8 @@ bool update_query(int query_hash, bool learn_aqo, bool use_aqo,
264
279
bool add_query_text (int query_hash , const char * query_text );
265
280
bool load_fss (int fss_hash , int ncols ,
266
281
double * * matrix , double * targets , int * rows );
267
- bool update_fss (int fss_hash , int nrows , int ncols ,
268
- double * * matrix , double * targets ,
269
- int old_nrows , List * changed_rows );
282
+ extern bool update_fss (int fss_hash , int nrows , int ncols ,
283
+ double * * matrix , double * targets );
270
284
QueryStat * get_aqo_stat (int query_hash );
271
285
void update_aqo_stat (int query_hash , QueryStat * stat );
272
286
void init_deactivated_queries_storage (void );
@@ -288,7 +302,7 @@ void print_into_explain(PlannedStmt *plannedstmt, IntoClause *into,
288
302
void disable_aqo_for_query (void );
289
303
290
304
/* Cardinality estimation hooks */
291
- void aqo_set_baserel_rows_estimate (PlannerInfo * root , RelOptInfo * rel );
305
+ extern void aqo_set_baserel_rows_estimate (PlannerInfo * root , RelOptInfo * rel );
292
306
double aqo_get_parameterized_baserel_size (PlannerInfo * root ,
293
307
RelOptInfo * rel ,
294
308
List * param_clauses );
@@ -314,22 +328,21 @@ List *get_list_of_relids(PlannerInfo *root, Relids relids);
314
328
List * get_path_clauses (Path * path , PlannerInfo * root , List * * selectivities );
315
329
316
330
/* Cardinality estimation */
317
- double predict_for_relation (List * restrict_clauses ,
318
- List * selectivities ,
319
- List * relids );
331
+ double predict_for_relation (List * restrict_clauses , List * selectivities ,
332
+ List * relids , int * fss_hash );
320
333
321
334
/* Query execution statistics collecting hooks */
322
335
void aqo_ExecutorStart (QueryDesc * queryDesc , int eflags );
323
336
void aqo_copy_generic_path_info (PlannerInfo * root , Plan * dest , Path * src );
324
- void learn_query_stat (QueryDesc * queryDesc );
337
+ void aqo_ExecutorEnd (QueryDesc * queryDesc );
325
338
326
339
/* Machine learning techniques */
327
- double OkNNr_predict (int matrix_rows , int matrix_cols ,
328
- double * * matrix , double * targets ,
329
- double * nw_features );
330
- List * OkNNr_learn (int matrix_rows , int matrix_cols ,
340
+ extern double OkNNr_predict (int nrows , int ncols ,
341
+ double * * matrix , const double * targets ,
342
+ double * features );
343
+ extern int OkNNr_learn (int matrix_rows , int matrix_cols ,
331
344
double * * matrix , double * targets ,
332
- double * nw_features , double nw_target );
345
+ double * features , double target );
333
346
334
347
/* Automatic query tuning */
335
348
void automatical_query_tuning (int query_hash , QueryStat * stat );
0 commit comments