Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3d7eee2

Browse files
committed
Add reliability factor (rfactor) into interface of learning procedures.
1 parent0dc6595 commit3d7eee2

File tree

10 files changed

+225
-226
lines changed

10 files changed

+225
-226
lines changed

‎aqo.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,6 @@ intauto_tuning_infinite_loop = 8;
7171

7272
/* Machine learning parameters */
7373

74-
/*
75-
* Defines where we do not perform learning procedure
76-
*/
77-
constdoubleobject_selection_prediction_threshold=0.3;
78-
79-
/*
80-
* This parameter tell us that the new learning sample object has very small
81-
* distance from one whose features stored in matrix already.
82-
* In this case we will not to add new line in matrix, but will modify this
83-
* nearest neighbor features and cardinality with linear smoothing by
84-
* learning_rate coefficient.
85-
*/
86-
constdoubleobject_selection_threshold=0.1;
87-
constdoublelearning_rate=1e-1;
88-
8974
/* The number of nearest neighbors which will be chosen for ML-operations */
9075
intaqo_k=3;
9176
doublelog_selectivity_lower_bound=-30;

‎aqo.h

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#include"utils/fmgroids.h"
145145
#include"utils/snapmgr.h"
146146

147+
#include"machine_learning.h"
147148

148149
/* Check PostgreSQL version (9.6.0 contains important changes in planner) */
149150
#ifPG_VERSION_NUM<90600
@@ -237,12 +238,6 @@ extern double auto_tuning_convergence_error;
237238

238239
/* Machine learning parameters */
239240

240-
/* Max number of matrix rows - max number of possible neighbors. */
241-
#defineaqo_K(30)
242-
243-
externconstdoubleobject_selection_prediction_threshold;
244-
externconstdoubleobject_selection_threshold;
245-
externconstdoublelearning_rate;
246241
externintaqo_k;
247242
externdoublelog_selectivity_lower_bound;
248243

@@ -285,17 +280,13 @@ extern bool find_query(uint64 qhash, QueryContextData *ctx);
285280
externboolupdate_query(uint64qhash,uint64fhash,
286281
boollearn_aqo,booluse_aqo,boolauto_tuning);
287282
externbooladd_query_text(uint64query_hash,constchar*query_string);
288-
externboolload_fss_ext(uint64fs,intfss,
289-
intncols,double**matrix,double*targets,int*rows,
283+
externboolload_fss_ext(uint64fs,intfss,OkNNrdata*data,
290284
List**relids,boolisSafe);
291-
externboolload_fss(uint64fhash,intfss_hash,
292-
intncols,double**matrix,double*targets,int*rows,
293-
List**relids);
294-
externboolupdate_fss_ext(uint64fhash,intfsshash,intnrows,intncols,
295-
double**matrix,double*targets,List*relids,
296-
boolisTimedOut);
297-
externboolupdate_fss(uint64fhash,intfss_hash,intnrows,intncols,
298-
double**matrix,double*targets,List*relids);
285+
externboolload_fss(uint64fhash,intfss_hash,OkNNrdata*data,List**relids);
286+
externboolupdate_fss_ext(uint64fhash,intfsshash,OkNNrdata*data,
287+
List*relids,boolisTimedOut);
288+
externboolupdate_fss(uint64fhash,intfss_hash,OkNNrdata*data,
289+
List*relids);
299290
QueryStat*get_aqo_stat(uint64query_hash);
300291
voidupdate_aqo_stat(uint64query_hash,QueryStat*stat);
301292
externboolmy_index_insert(RelationindexRelation,Datum*values,bool*isnull,
@@ -324,14 +315,6 @@ void aqo_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
324315
uint64count,boolexecute_once);
325316
voidaqo_ExecutorEnd(QueryDesc*queryDesc);
326317

327-
/* Machine learning techniques */
328-
externdoubleOkNNr_predict(intnrows,intncols,
329-
double**matrix,constdouble*targets,
330-
double*features);
331-
externintOkNNr_learn(intmatrix_rows,intmatrix_cols,
332-
double**matrix,double*targets,
333-
double*features,doubletarget);
334-
335318
/* Automatic query tuning */
336319
externvoidautomatical_query_tuning(uint64query_hash,QueryStat*stat);
337320

‎cardinality_estimation.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include"aqo.h"
2323
#include"hash.h"
24+
#include"machine_learning.h"
2425

2526
#ifdefAQO_DEBUG_PRINT
2627
staticvoid
@@ -59,15 +60,12 @@ predict_debug_output(List *clauses, List *selectivities,
5960
*/
6061
double
6162
predict_for_relation(List*clauses,List*selectivities,
62-
List*relids,int*fss_hash)
63+
List*relids,int*fss)
6364
{
64-
intnfeatures;
65-
double*matrix[aqo_K];
66-
doubletargets[aqo_K];
67-
double*features;
68-
doubleresult;
69-
introws;
70-
inti;
65+
double*features;
66+
doubleresult;
67+
inti;
68+
OkNNrdatadata;
7169

7270
if (relids==NIL)
7371
/*
@@ -76,16 +74,15 @@ predict_for_relation(List *clauses, List *selectivities,
7674
*/
7775
return-4.;
7876

79-
*fss_hash=get_fss_for_object(relids,clauses,
80-
selectivities,&nfeatures,&features);
77+
*fss=get_fss_for_object(relids,clauses,
78+
selectivities,&data.cols,&features);
8179

82-
if (nfeatures>0)
80+
if (data.cols>0)
8381
for (i=0;i<aqo_K;++i)
84-
matrix[i]=palloc0(sizeof(**matrix)*nfeatures);
82+
data.matrix[i]=palloc0(sizeof(double)*data.cols);
8583

86-
if (load_fss_ext(query_context.fspace_hash,*fss_hash,nfeatures,matrix,
87-
targets,&rows,NULL, true))
88-
result=OkNNr_predict(rows,nfeatures,matrix,targets,features);
84+
if (load_fss_ext(query_context.fspace_hash,*fss,&data,NULL, true))
85+
result=OkNNr_predict(&data,features);
8986
else
9087
{
9188
/*
@@ -100,10 +97,10 @@ predict_for_relation(List *clauses, List *selectivities,
10097
predict_debug_output(clauses,selectivities,relids,*fss_hash,result);
10198
#endif
10299
pfree(features);
103-
if (nfeatures>0)
100+
if (data.cols>0)
104101
{
105102
for (i=0;i<aqo_K;++i)
106-
pfree(matrix[i]);
103+
pfree(data.matrix[i]);
107104
}
108105

109106
if (result<0)

‎cardinality_hooks.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include"aqo.h"
3131
#include"cardinality_hooks.h"
3232
#include"hash.h"
33+
#include"machine_learning.h"
3334
#include"path_utils.h"
3435

3536
estimate_num_groups_hook_typeprev_estimate_num_groups_hook=NULL;
@@ -137,12 +138,12 @@ default_estimate_num_groups(PlannerInfo *root, List *groupExprs,
137138
void
138139
aqo_set_baserel_rows_estimate(PlannerInfo*root,RelOptInfo*rel)
139140
{
140-
doublepredicted;
141-
Oidrelid;
142-
List*relids=NIL;
143-
List*selectivities=NULL;
144-
List*clauses;
145-
intfss=0;
141+
doublepredicted;
142+
Oidrelid;
143+
List*relids=NIL;
144+
List*selectivities=NULL;
145+
List*clauses;
146+
intfss=0;
146147

147148
if (IsQueryDisabled())
148149
/* Fast path. */
@@ -411,10 +412,9 @@ static double
411412
predict_num_groups(PlannerInfo*root,Path*subpath,List*group_exprs,
412413
int*fss)
413414
{
414-
intchild_fss=0;
415-
doubleprediction;
416-
introws;
417-
doubletarget;
415+
intchild_fss=0;
416+
doubleprediction;
417+
OkNNrdatadata;
418418

419419
if (subpath->parent->predicted_cardinality>0.)
420420
/* A fast path. Here we can use a fss hash of a leaf. */
@@ -431,13 +431,13 @@ predict_num_groups(PlannerInfo *root, Path *subpath, List *group_exprs,
431431
}
432432

433433
*fss=get_grouped_exprs_hash(child_fss,group_exprs);
434+
memset(&data,0,sizeof(OkNNrdata));
434435

435-
if (!load_fss_ext(query_context.fspace_hash,*fss,0,NULL,
436-
&target,&rows,NULL, true))
436+
if (!load_fss_ext(query_context.fspace_hash,*fss,&data,NULL, true))
437437
return-1;
438438

439-
Assert(rows==1);
440-
prediction=exp(target);
439+
Assert(data.rows==1);
440+
prediction=exp(data.targets[0]);
441441
return (prediction <=0) ?-1 :prediction;
442442
}
443443

‎learn_cache.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ lc_init(void)
6060
}
6161

6262
bool
63-
lc_update_fss(uint64fs,intfss,intnrows,intncols,
64-
double**matrix,double*targets,List*relids)
63+
lc_update_fss(uint64fs,intfss,OkNNrdata*data,List*relids)
6564
{
6665
htab_keykey= {fs,fss};
6766
htab_entry*entry;
@@ -81,15 +80,15 @@ lc_update_fss(uint64 fs, int fss, int nrows, int ncols,
8180
list_free(entry->relids);
8281
}
8382

84-
entry->nrows=nrows;
85-
entry->ncols=ncols;
83+
entry->nrows=data->rows;
84+
entry->ncols=data->cols;
8685
for (i=0;i<entry->nrows;++i)
8786
{
88-
entry->matrix[i]=palloc(sizeof(double)*ncols);
89-
memcpy(entry->matrix[i],matrix[i],sizeof(double)*ncols);
87+
entry->matrix[i]=palloc(sizeof(double)*data->cols);
88+
memcpy(entry->matrix[i],data->matrix[i],sizeof(double)*data->cols);
9089
}
91-
entry->targets=palloc(sizeof(double)*nrows);
92-
memcpy(entry->targets,targets,sizeof(double)*nrows);
90+
entry->targets=palloc(sizeof(double)*data->rows);
91+
memcpy(entry->targets,data->targets,sizeof(double)*data->rows);
9392
entry->relids=list_copy(relids);
9493

9594
MemoryContextSwitchTo(memctx);
@@ -116,8 +115,7 @@ lc_has_fss(uint64 fs, int fss)
116115
* XXX That to do with learning tails, living in the cache?
117116
*/
118117
bool
119-
lc_load_fss(uint64fs,intfss,intncols,double**matrix,
120-
double*targets,int*nrows,List**relids)
118+
lc_load_fss(uint64fs,intfss,OkNNrdata*data,List**relids)
121119
{
122120
htab_keykey= {fs,fss};
123121
htab_entry*entry;
@@ -134,11 +132,11 @@ lc_load_fss(uint64 fs, int fss, int ncols, double **matrix,
134132
elog(NOTICE,"[AQO] Load ML data for fs %lu, fss %d from the cache",
135133
fs,fss);
136134

137-
*nrows=entry->nrows;
138-
Assert(entry->ncols==ncols);
135+
data->rows=entry->nrows;
136+
Assert(entry->ncols==data->cols);
139137
for (i=0;i<entry->nrows;++i)
140-
memcpy(matrix[i],entry->matrix[i],sizeof(double)*ncols);
141-
memcpy(targets,entry->targets,sizeof(double)*entry->nrows);
138+
memcpy(data->matrix[i],entry->matrix[i],sizeof(double)*data->cols);
139+
memcpy(data->targets,entry->targets,sizeof(double)*entry->nrows);
142140
if (relids)
143141
*relids=list_copy(entry->relids);
144142
return true;

‎learn_cache.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
#include"nodes/pg_list.h"
55

6+
#include"machine_learning.h"
7+
68
externboolaqo_learn_statement_timeout;
79

810
externvoidlc_init(void);
9-
externboollc_update_fss(uint64fhash,intfsshash,intnrows,intncols,
10-
double**matrix,double*targets,List*relids);
11+
externboollc_update_fss(uint64fhash,intfsshash,OkNNrdata*data,
12+
List*relids);
1113
externboollc_has_fss(uint64fhash,intfss);
12-
externboollc_load_fss(uint64fhash,intfsshash,intncols,
13-
double**matrix,double*targets,int*nrows,
14-
List**relids);
14+
externboollc_load_fss(uint64fhash,intfsshash,OkNNrdata*data,
15+
List**relids);
1516
externvoidlc_remove_fss(uint64fhash,intfss_hash);
1617
externvoidlc_assign_hook(boolnewval,void*extra);
1718

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp