forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardinality_estimation.c
80 lines (69 loc) · 1.79 KB
/
cardinality_estimation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
*******************************************************************************
*
* CARDINALITY ESTIMATION
*
* This is the module in which cardinality estimation problem obtained from
* cardinality_hooks turns into machine learning problem.
*
*******************************************************************************
*
* Copyright (c) 2016-2021, Postgres Professional
*
* IDENTIFICATION
* aqo/cardinality_estimation.c
*
*/
#include "postgres.h"
#include "optimizer/optimizer.h"
#include "aqo.h"
#include "hash.h"
/*
* General method for prediction the cardinality of given relation.
*/
double
predict_for_relation(List *clauses, List *selectivities,
List *relids, int *fss_hash)
{
int nfeatures;
double *matrix[aqo_K];
double targets[aqo_K];
double *features;
double result;
int rows;
int i;
if (relids == NIL)
/*
* Don't make prediction for query plans without any underlying plane
* tables. Use return value -4 for debug purposes.
*/
return -4.;
*fss_hash = get_fss_for_object(relids, clauses,
selectivities, &nfeatures, &features);
if (nfeatures > 0)
for (i = 0; i < aqo_K; ++i)
matrix[i] = palloc0(sizeof(**matrix) * nfeatures);
if (load_fss(query_context.fspace_hash, *fss_hash, nfeatures, matrix,
targets, &rows, NULL))
result = OkNNr_predict(rows, nfeatures, matrix, targets, features);
else
{
/*
* Due to planning optimizer tries to build many alternate paths. Many
* of these not used in final query execution path. Consequently, only
* small part of paths was used for AQO learning and fetch into the AQO
* knowledge base.
*/
result = -1;
}
pfree(features);
if (nfeatures > 0)
{
for (i = 0; i < aqo_K; ++i)
pfree(matrix[i]);
}
if (result < 0)
return -1;
else
return clamp_row_est(exp(result));
}