Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 656ec07

Browse files
author
Daniil Anisimov
committed
Add FORCED_CONTROLLED and FORCED_FROZEN modes.
1 parent e0f0d38 commit 656ec07

File tree

8 files changed

+225
-2
lines changed

8 files changed

+225
-2
lines changed

aqo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ bool change_flex_timeout;
5353
static const struct config_enum_entry format_options[] = {
5454
{"intelligent", AQO_MODE_INTELLIGENT, false},
5555
{"forced", AQO_MODE_FORCED, false},
56+
{"forced_controlled", AQO_MODE_FORCED_CONTROLLED, false},
57+
{"forced_frozen", AQO_MODE_FORCED_FROZEN, false},
5658
{"controlled", AQO_MODE_CONTROLLED, false},
5759
{"learn", AQO_MODE_LEARN, false},
5860
{"frozen", AQO_MODE_FROZEN, false},

aqo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ typedef enum
155155
AQO_MODE_INTELLIGENT,
156156
/* Treats new query types as linked to the common feature space */
157157
AQO_MODE_FORCED,
158+
/* Like FORCED, but learn only on old feature sub spaces */
159+
AQO_MODE_FORCED_CONTROLLED,
160+
/* Like FORCED, but use old knowledge without learning */
161+
AQO_MODE_FORCED_FROZEN,
158162
/* New query types are not linked with any feature space */
159163
AQO_MODE_CONTROLLED,
160164
/* Creates new feature space for each query type without auto-tuning */
@@ -183,6 +187,8 @@ typedef struct QueryContextData
183187
bool auto_tuning;
184188
bool collect_stat;
185189
bool adding_query;
190+
/* Add new fss in learn phase, otherwise learn only on existing fss */
191+
bool adding_fss;
186192
bool explain_only;
187193

188194
/*

expected/aqo_forced.out

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SELECT true AS success FROM aqo_reset();
66
t
77
(1 row)
88

9+
SET aqo.show_details = on;
910
CREATE TABLE aqo_test0(a int, b int, c int, d int);
1011
WITH RECURSIVE t(a, b, c, d)
1112
AS (
@@ -70,22 +71,117 @@ WHERE a < 3 AND b < 3 AND c < 3 AND d < 3;
7071
QUERY PLAN
7172
-----------------------------------------------
7273
Index Scan using aqo_test0_idx_a on aqo_test0
74+
AQO: rows=3
7375
Index Cond: (a < 3)
7476
Filter: ((b < 3) AND (c < 3) AND (d < 3))
75-
(3 rows)
77+
Using aqo: true
78+
AQO mode: FORCED
79+
JOINS: 0
80+
(7 rows)
7681

7782
EXPLAIN (COSTS FALSE)
7883
SELECT * FROM aqo_test0
7984
WHERE a < 5 AND b < 5 AND c < 5 AND d < 5;
8085
QUERY PLAN
8186
-----------------------------------------------
8287
Index Scan using aqo_test0_idx_a on aqo_test0
88+
AQO: rows=5
8389
Index Cond: (a < 5)
8490
Filter: ((b < 5) AND (c < 5) AND (d < 5))
85-
(3 rows)
91+
Using aqo: true
92+
AQO mode: FORCED
93+
JOINS: 0
94+
(7 rows)
95+
96+
SET aqo.mode = 'forced_controlled';
97+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
98+
WHERE a < 10 AND b < 10 AND c < 10;
99+
SELECT count(*) FROM tmp1;
100+
count
101+
-------
102+
10
103+
(1 row)
104+
105+
DROP TABLE tmp1;
106+
-- Not predict
107+
EXPLAIN (COSTS FALSE)
108+
SELECT * FROM aqo_test0
109+
WHERE a < 10 AND b < 10 AND c < 10;
110+
QUERY PLAN
111+
-----------------------------------------------
112+
Index Scan using aqo_test0_idx_a on aqo_test0
113+
AQO not used
114+
Index Cond: (a < 10)
115+
Filter: ((b < 10) AND (c < 10))
116+
Using aqo: true
117+
AQO mode: FORCED_CONTROLLED
118+
JOINS: 0
119+
(7 rows)
120+
121+
SET aqo.mode = 'forced';
122+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
123+
WHERE a < 0 AND b < 0 AND c < 0;
124+
SELECT count(*) FROM tmp1;
125+
count
126+
-------
127+
0
128+
(1 row)
129+
130+
DROP TABLE tmp1;
131+
SET aqo.mode = 'forced_controlled';
132+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
133+
WHERE a < 10 AND b < 10 AND c < 10;
134+
SELECT count(*) FROM tmp1;
135+
count
136+
-------
137+
10
138+
(1 row)
139+
140+
DROP TABLE tmp1;
141+
-- Predict
142+
EXPLAIN (COSTS FALSE)
143+
SELECT * FROM aqo_test0
144+
WHERE a < 10 AND b < 10 AND c < 10;
145+
QUERY PLAN
146+
-----------------------------------------------
147+
Index Scan using aqo_test0_idx_a on aqo_test0
148+
AQO: rows=10
149+
Index Cond: (a < 10)
150+
Filter: ((b < 10) AND (c < 10))
151+
Using aqo: true
152+
AQO mode: FORCED_CONTROLLED
153+
JOINS: 0
154+
(7 rows)
155+
156+
SET aqo.mode = 'forced_frozen';
157+
-- Not learn
158+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
159+
WHERE a < 20 AND b < 20 AND c < 20;
160+
SELECT count(*) FROM tmp1;
161+
count
162+
-------
163+
20
164+
(1 row)
165+
166+
DROP TABLE tmp1;
167+
-- Predict approximately 9 (interpolation between 1 and 10)
168+
EXPLAIN (COSTS FALSE)
169+
SELECT * FROM aqo_test0
170+
WHERE a < 20 AND b < 20 AND c < 20;
171+
QUERY PLAN
172+
-----------------------------------------------
173+
Index Scan using aqo_test0_idx_a on aqo_test0
174+
AQO: rows=9
175+
Index Cond: (a < 20)
176+
Filter: ((b < 20) AND (c < 20))
177+
Using aqo: true
178+
AQO mode: FORCED_FROZEN
179+
JOINS: 0
180+
(7 rows)
86181

87182
DROP INDEX aqo_test0_idx_a;
88183
DROP TABLE aqo_test0;
89184
DROP INDEX aqo_test1_idx_a;
90185
DROP TABLE aqo_test1;
186+
RESET aqo.show_details;
91187
DROP EXTENSION aqo;

postprocessing.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,14 @@ learnOnPlanState(PlanState *p, void *context)
465465
else if (!ctx->learn)
466466
return true;
467467

468+
if (!query_context.adding_fss &&
469+
!aqo_data_exist(query_context.fspace_hash, aqo_node->fss))
470+
/*
471+
* Skip the node if it does not exist in aqo_data
472+
* and do not need to be added.
473+
*/
474+
goto end;
475+
468476
/*
469477
* Need learn.
470478
*/
@@ -999,6 +1007,12 @@ print_into_explain(PlannedStmt *plannedstmt, IntoClause *into,
9991007
case AQO_MODE_FORCED:
10001008
ExplainPropertyText("AQO mode", "FORCED", es);
10011009
break;
1010+
case AQO_MODE_FORCED_CONTROLLED:
1011+
ExplainPropertyText("AQO mode", "FORCED_CONTROLLED", es);
1012+
break;
1013+
case AQO_MODE_FORCED_FROZEN:
1014+
ExplainPropertyText("AQO mode", "FORCED_FROZEN", es);
1015+
break;
10021016
case AQO_MODE_CONTROLLED:
10031017
ExplainPropertyText("AQO mode", "CONTROLLED", es);
10041018
break;

preprocessing.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,33 +176,55 @@ aqo_planner(Query *parse, const char *query_string, int cursorOptions,
176176
{
177177
case AQO_MODE_INTELLIGENT:
178178
query_context.adding_query = true;
179+
query_context.adding_fss = true;
179180
query_context.learn_aqo = true;
180181
query_context.use_aqo = false;
181182
query_context.auto_tuning = true;
182183
query_context.collect_stat = true;
183184
break;
184185
case AQO_MODE_FORCED:
185186
query_context.adding_query = false;
187+
query_context.adding_fss = true;
186188
query_context.learn_aqo = true;
187189
query_context.use_aqo = true;
188190
query_context.auto_tuning = false;
189191
query_context.fspace_hash = 0; /* Use common feature space */
190192
query_context.collect_stat = false;
191193
break;
194+
case AQO_MODE_FORCED_CONTROLLED:
195+
query_context.adding_query = false;
196+
query_context.adding_fss = false;
197+
query_context.learn_aqo = true;
198+
query_context.use_aqo = true;
199+
query_context.auto_tuning = false;
200+
query_context.fspace_hash = 0; /* Use common feature space */
201+
query_context.collect_stat = false;
202+
break;
203+
case AQO_MODE_FORCED_FROZEN:
204+
query_context.adding_query = false;
205+
query_context.adding_fss = false;
206+
query_context.learn_aqo = false;
207+
query_context.use_aqo = true;
208+
query_context.auto_tuning = false;
209+
query_context.fspace_hash = 0; /* Use common feature space */
210+
query_context.collect_stat = false;
211+
break;
192212
case AQO_MODE_CONTROLLED:
193213
case AQO_MODE_FROZEN:
194214
/*
195215
* if query is not in the AQO knowledge base than disable AQO
196216
* for this query.
197217
*/
198218
query_context.adding_query = false;
219+
query_context.adding_fss = true;
199220
query_context.learn_aqo = false;
200221
query_context.use_aqo = false;
201222
query_context.auto_tuning = false;
202223
query_context.collect_stat = false;
203224
break;
204225
case AQO_MODE_LEARN:
205226
query_context.adding_query = true;
227+
query_context.adding_fss = true;
206228
query_context.learn_aqo = true;
207229
query_context.use_aqo = true;
208230
query_context.auto_tuning = false;
@@ -222,6 +244,7 @@ aqo_planner(Query *parse, const char *query_string, int cursorOptions,
222244
else /* Query class exists in a ML knowledge base. */
223245
{
224246
query_context.adding_query = false;
247+
query_context.adding_fss = true;
225248

226249
/* Other query_context fields filled in the find_query() routine. */
227250

@@ -243,6 +266,7 @@ aqo_planner(Query *parse, const char *query_string, int cursorOptions,
243266
* In this mode we will suppress all writings to the knowledge base.
244267
* AQO will be used for all known queries, if it is not suppressed.
245268
*/
269+
query_context.adding_fss = false;
246270
query_context.learn_aqo = false;
247271
query_context.auto_tuning = false;
248272
query_context.collect_stat = false;
@@ -256,6 +280,22 @@ aqo_planner(Query *parse, const char *query_string, int cursorOptions,
256280
query_context.collect_stat = true;
257281
break;
258282

283+
case AQO_MODE_FORCED_CONTROLLED:
284+
/*
285+
* AQO will be learned for all known feature sub spaces.
286+
*/
287+
query_context.adding_fss = false;
288+
break;
289+
290+
case AQO_MODE_FORCED_FROZEN:
291+
/*
292+
* In this mode we will suppress all writings to the knowledge base.
293+
* AQO will be used for all known feature sub spaces.
294+
*/
295+
query_context.adding_fss = false;
296+
query_context.learn_aqo = false;
297+
break;
298+
259299
case AQO_MODE_INTELLIGENT:
260300
case AQO_MODE_FORCED:
261301
case AQO_MODE_CONTROLLED:

sql/aqo_forced.sql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- Preliminaries
22
CREATE EXTENSION IF NOT EXISTS aqo;
33
SELECT true AS success FROM aqo_reset();
4+
SET aqo.show_details = on;
45

56
CREATE TABLE aqo_test0(a int, b int, c int, d int);
67
WITH RECURSIVE t(a, b, c, d)
@@ -52,9 +53,53 @@ EXPLAIN (COSTS FALSE)
5253
SELECT * FROM aqo_test0
5354
WHERE a < 5 AND b < 5 AND c < 5 AND d < 5;
5455

56+
SET aqo.mode = 'forced_controlled';
57+
58+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
59+
WHERE a < 10 AND b < 10 AND c < 10;
60+
SELECT count(*) FROM tmp1;
61+
DROP TABLE tmp1;
62+
63+
-- Not predict
64+
EXPLAIN (COSTS FALSE)
65+
SELECT * FROM aqo_test0
66+
WHERE a < 10 AND b < 10 AND c < 10;
67+
68+
SET aqo.mode = 'forced';
69+
70+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
71+
WHERE a < 0 AND b < 0 AND c < 0;
72+
SELECT count(*) FROM tmp1;
73+
DROP TABLE tmp1;
74+
75+
SET aqo.mode = 'forced_controlled';
76+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
77+
WHERE a < 10 AND b < 10 AND c < 10;
78+
SELECT count(*) FROM tmp1;
79+
DROP TABLE tmp1;
80+
81+
-- Predict
82+
EXPLAIN (COSTS FALSE)
83+
SELECT * FROM aqo_test0
84+
WHERE a < 10 AND b < 10 AND c < 10;
85+
86+
SET aqo.mode = 'forced_frozen';
87+
88+
-- Not learn
89+
CREATE TABLE tmp1 AS SELECT * FROM aqo_test0
90+
WHERE a < 20 AND b < 20 AND c < 20;
91+
SELECT count(*) FROM tmp1;
92+
DROP TABLE tmp1;
93+
94+
-- Predict approximately 9 (interpolation between 1 and 10)
95+
EXPLAIN (COSTS FALSE)
96+
SELECT * FROM aqo_test0
97+
WHERE a < 20 AND b < 20 AND c < 20;
98+
5599
DROP INDEX aqo_test0_idx_a;
56100
DROP TABLE aqo_test0;
57101
DROP INDEX aqo_test1_idx_a;
58102
DROP TABLE aqo_test1;
59103

104+
RESET aqo.show_details;
60105
DROP EXTENSION aqo;

storage.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,25 @@ _fill_knn_data(const DataEntry *entry, List **reloids)
16301630
return data;
16311631
}
16321632

1633+
/*
1634+
* Return TRUE when aqo_data contains a row
1635+
* for the given feature space and subspace.
1636+
*/
1637+
bool
1638+
aqo_data_exist(uint64 fs, int fss)
1639+
{
1640+
bool found;
1641+
data_key key = {.fs = fs, .fss = fss};
1642+
1643+
dsa_init();
1644+
1645+
LWLockAcquire(&aqo_state->data_lock, LW_SHARED);
1646+
hash_search(data_htab, &key, HASH_FIND, &found);
1647+
LWLockRelease(&aqo_state->data_lock);
1648+
1649+
return found;
1650+
}
1651+
16331652
/*
16341653
* By given feature space and subspace, build kNN data structure.
16351654
*

storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ extern bool aqo_qtext_store(uint64 queryid, const char *query_string);
142142
extern void aqo_qtexts_flush(void);
143143
extern void aqo_qtexts_load(void);
144144

145+
extern bool aqo_data_exist(uint64 fs, int fss);
145146
extern bool aqo_data_store(uint64 fs, int fss, AqoDataArgs *data,
146147
List *reloids);
147148
extern bool load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,

0 commit comments

Comments
 (0)