133133#include "executor/execdesc.h"
134134#include "nodes/makefuncs.h"
135135#include "nodes/nodeFuncs.h"
136+ #include "optimizer/pathnode.h"
136137#include "optimizer/planmain.h"
137138#include "optimizer/planner.h"
138139#include "optimizer/cost.h"
144145#include "utils/hsearch.h"
145146#include "utils/memutils.h"
146147#include "utils/rel.h"
147- #include "utils/tqual.h"
148148#include "utils/fmgroids.h"
149149#include "utils/snapmgr.h"
150150
@@ -165,11 +165,19 @@ typedef enum
165165 AQO_MODE_CONTROLLED ,
166166 /* Creates new feature space for each query type without auto-tuning */
167167 AQO_MODE_LEARN ,
168+ /* Use only current AQO estimations, without learning or tuning */
169+ AQO_MODE_FIXED ,
168170 /* Aqo is disabled for all queries */
169171 AQO_MODE_DISABLED ,
170172} AQO_MODE ;
171173extern int aqo_mode ;
172174
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+ */
173181typedef struct
174182{
175183 double * execution_time_with_aqo ;
@@ -178,12 +186,14 @@ typedef struct
178186 double * planning_time_without_aqo ;
179187 double * cardinality_error_with_aqo ;
180188 double * cardinality_error_without_aqo ;
189+
181190 int execution_time_with_aqo_size ;
182191 int execution_time_without_aqo_size ;
183192 int planning_time_with_aqo_size ;
184193 int planning_time_without_aqo_size ;
185194 int cardinality_error_with_aqo_size ;
186195 int cardinality_error_without_aqo_size ;
196+
187197 int64 executions_with_aqo ;
188198 int64 executions_without_aqo ;
189199} QueryStat ;
@@ -205,6 +215,9 @@ typedef struct QueryContextData
205215 double query_planning_time ;
206216} QueryContextData ;
207217
218+ extern double predicted_ppi_rows ;
219+ extern double fss_ppi_hash ;
220+
208221/* Parameters of autotuning */
209222extern int aqo_stat_size ;
210223extern int auto_tuning_window_size ;
@@ -213,11 +226,12 @@ extern int auto_tuning_max_iterations;
213226extern int auto_tuning_infinite_loop ;
214227
215228/* 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 ;
219234extern int aqo_k ;
220- extern int aqo_K ;
221235extern double log_selectivity_lower_bound ;
222236
223237/* Parameters for current query */
@@ -244,11 +258,12 @@ extern copy_generic_path_info_hook_type
244258 prev_copy_generic_path_info_hook ;
245259extern ExplainOnePlan_hook_type prev_ExplainOnePlan_hook ;
246260
261+ extern void ppi_hook (ParamPathInfo * ppi );
247262
248263/* Hash functions */
249264int 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 );
252267void get_eclasses (List * clauselist , int * nargs , int * * args_hash , int * * eclass_hash );
253268int get_clause_hash (Expr * clause , int nargs , int * args_hash , int * eclass_hash );
254269
@@ -264,9 +279,8 @@ bool update_query(int query_hash, bool learn_aqo, bool use_aqo,
264279bool add_query_text (int query_hash , const char * query_text );
265280bool load_fss (int fss_hash , int ncols ,
266281 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 );
270284QueryStat * get_aqo_stat (int query_hash );
271285void update_aqo_stat (int query_hash , QueryStat * stat );
272286void init_deactivated_queries_storage (void );
@@ -288,7 +302,7 @@ void print_into_explain(PlannedStmt *plannedstmt, IntoClause *into,
288302void disable_aqo_for_query (void );
289303
290304/* 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 );
292306double aqo_get_parameterized_baserel_size (PlannerInfo * root ,
293307 RelOptInfo * rel ,
294308 List * param_clauses );
@@ -314,22 +328,21 @@ List *get_list_of_relids(PlannerInfo *root, Relids relids);
314328List * get_path_clauses (Path * path , PlannerInfo * root , List * * selectivities );
315329
316330/* 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 );
320333
321334/* Query execution statistics collecting hooks */
322335void aqo_ExecutorStart (QueryDesc * queryDesc , int eflags );
323336void aqo_copy_generic_path_info (PlannerInfo * root , Plan * dest , Path * src );
324- void learn_query_stat (QueryDesc * queryDesc );
337+ void aqo_ExecutorEnd (QueryDesc * queryDesc );
325338
326339/* 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 ,
331344 double * * matrix , double * targets ,
332- double * nw_features , double nw_target );
345+ double * features , double target );
333346
334347/* Automatic query tuning */
335348void automatical_query_tuning (int query_hash , QueryStat * stat );
0 commit comments