Skip to content

Commit bd638da

Browse files
a.pervushinadanolivo
a.pervushina
authored andcommitted
Move AQO from a relid based approach to a relation name based approach.
It allows us to reuse ML data at different instance and learn on temporary tables.
1 parent 5e6cbdb commit bd638da

16 files changed

+314
-236
lines changed

aqo--1.2--1.3.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ALTER TABLE public.aqo_data ADD COLUMN oids OID [] DEFAULT NULL;
1+
ALTER TABLE public.aqo_data ADD COLUMN oids text [] DEFAULT NULL;
22

33
--
44
-- Remove data, related to previously dropped tables, from the AQO tables.
@@ -9,7 +9,7 @@ DECLARE
99
aqo_queries_row aqo_queries%ROWTYPE;
1010
aqo_query_texts_row aqo_query_texts%ROWTYPE;
1111
aqo_query_stat_row aqo_query_stat%ROWTYPE;
12-
oid_var oid;
12+
oid_var text;
1313
fspace_hash_var bigint;
1414
delete_row boolean DEFAULT false;
1515
BEGIN
@@ -23,7 +23,7 @@ BEGIN
2323
IF (aqo_data_row.oids IS NOT NULL) THEN
2424
FOREACH oid_var IN ARRAY aqo_data_row.oids
2525
LOOP
26-
IF NOT EXISTS (SELECT relname FROM pg_class WHERE oid = oid_var) THEN
26+
IF NOT EXISTS (SELECT relname FROM pg_class WHERE oid::regclass::text = oid_var) THEN
2727
delete_row = true;
2828
END IF;
2929
END LOOP;

aqo.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,12 @@ extern bool find_query(uint64 qhash, QueryContextData *ctx);
281281
extern bool update_query(uint64 qhash, uint64 fhash,
282282
bool learn_aqo, bool use_aqo, bool auto_tuning);
283283
extern bool add_query_text(uint64 query_hash, const char *query_string);
284-
extern bool load_fss_ext(uint64 fs, int fss, OkNNrdata *data,
285-
List **relids, bool isSafe);
286-
extern bool load_fss(uint64 fhash, int fss_hash, OkNNrdata *data, List **relids);
287-
extern bool update_fss_ext(uint64 fhash, int fsshash, OkNNrdata *data,
288-
List *relids, bool isTimedOut);
289-
extern bool update_fss(uint64 fhash, int fss_hash, OkNNrdata *data,
290-
List *relids);
284+
extern bool load_fss_ext(uint64 fs, int fss, OkNNrdata *data, List **relnames,
285+
bool isSafe);
286+
extern bool load_fss(uint64 fs, int fss, OkNNrdata *data, List **relnames);
287+
extern bool update_fss_ext(uint64 fs, int fss, OkNNrdata *data,
288+
List *relnames, bool isTimedOut);
289+
extern bool update_fss(uint64 fs, int fss, OkNNrdata *data, List *relnames);
291290
QueryStat *get_aqo_stat(uint64 query_hash);
292291
void update_aqo_stat(uint64 query_hash, QueryStat * stat);
293292
extern bool my_index_insert(Relation indexRelation, Datum *values, bool *isnull,
@@ -308,7 +307,7 @@ extern void print_node_explain(ExplainState *es, PlanState *ps, Plan *plan);
308307

309308
/* Cardinality estimation */
310309
double predict_for_relation(List *restrict_clauses, List *selectivities,
311-
List *relids, int *fss_hash);
310+
List *relnames, int *fss);
312311

313312
/* Query execution statistics collecting hooks */
314313
void aqo_ExecutorStart(QueryDesc *queryDesc, int eflags);
@@ -320,13 +319,14 @@ void aqo_ExecutorEnd(QueryDesc *queryDesc);
320319
extern void automatical_query_tuning(uint64 query_hash, QueryStat * stat);
321320

322321
/* Utilities */
323-
int int_cmp(const void *a, const void *b);
324-
int double_cmp(const void *a, const void *b);
325-
int *argsort(void *a, int n, size_t es,
326-
int (*cmp) (const void *, const void *));
327-
int *inverse_permutation(int *a, int n);
328-
QueryStat *palloc_query_stat(void);
329-
void pfree_query_stat(QueryStat *stat);
322+
extern int int64_compare(const void *a, const void *b);
323+
extern int int_cmp(const void *a, const void *b);
324+
extern int double_cmp(const void *a, const void *b);
325+
extern int *argsort(void *a, int n, size_t es,
326+
int (*cmp) (const void *, const void *));
327+
extern int *inverse_permutation(int *a, int n);
328+
extern QueryStat *palloc_query_stat(void);
329+
extern void pfree_query_stat(QueryStat *stat);
330330

331331
/* Selectivity cache for parametrized baserels */
332332
extern void cache_selectivity(int clause_hash, int relid, int global_relid,

cardinality_estimation.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#ifdef AQO_DEBUG_PRINT
2727
static void
2828
predict_debug_output(List *clauses, List *selectivities,
29-
List *relids, int fss_hash, double result)
29+
List *relnames, int fss, double result)
3030
{
3131
StringInfoData debug_str;
3232
ListCell *lc;
@@ -42,11 +42,11 @@ predict_debug_output(List *clauses, List *selectivities,
4242
appendStringInfo(&debug_str, "%lf ", *s);
4343
}
4444

45-
appendStringInfoString(&debug_str, "}, relids: { ");
46-
foreach(lc, relids)
45+
appendStringInfoString(&debug_str, "}, relnames: { ");
46+
foreach(lc, relnames)
4747
{
48-
int relid = lfirst_int(lc);
49-
appendStringInfo(&debug_str, "%d ", relid);
48+
String *relname = lfirst_node(String, lc);
49+
appendStringInfo(&debug_str, "%s ", relname->sval);
5050
}
5151

5252
appendStringInfo(&debug_str, "}, result: %lf", result);
@@ -60,22 +60,22 @@ predict_debug_output(List *clauses, List *selectivities,
6060
*/
6161
double
6262
predict_for_relation(List *clauses, List *selectivities,
63-
List *relids, int *fss)
63+
List *relnames, int *fss)
6464
{
6565
double *features;
6666
double result;
6767
int i;
6868
OkNNrdata data;
6969

70-
if (relids == NIL)
70+
if (relnames == NIL)
7171
/*
7272
* Don't make prediction for query plans without any underlying plane
7373
* tables. Use return value -4 for debug purposes.
7474
*/
7575
return -4.;
7676

77-
*fss = get_fss_for_object(relids, clauses,
78-
selectivities, &data.cols, &features);
77+
*fss = get_fss_for_object(relnames, clauses, selectivities,
78+
&data.cols, &features);
7979

8080
if (data.cols > 0)
8181
for (i = 0; i < aqo_K; ++i)
@@ -94,7 +94,7 @@ predict_for_relation(List *clauses, List *selectivities,
9494
result = -1;
9595
}
9696
#ifdef AQO_DEBUG_PRINT
97-
predict_debug_output(clauses, selectivities, relids, *fss_hash, result);
97+
predict_debug_output(clauses, selectivities, relnames, *fss, result);
9898
#endif
9999
pfree(features);
100100
if (data.cols > 0)

cardinality_hooks.c

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ void
139139
aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
140140
{
141141
double predicted;
142-
Oid relid;
143-
List *relids = NIL;
142+
RangeTblEntry *rte;
143+
List *relnames = NIL;
144144
List *selectivities = NULL;
145145
List *clauses;
146146
int fss = 0;
@@ -161,19 +161,24 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
161161
goto default_estimator;
162162
}
163163

164-
relid = planner_rt_fetch(rel->relid, root)->relid;
165-
if (OidIsValid(relid))
166-
/* Predict for a plane table only. */
167-
relids = list_make1_int(relid);
164+
rte = planner_rt_fetch(rel->relid, root);
165+
if (rte && OidIsValid(rte->relid))
166+
{
167+
String *s = makeNode(String);
168+
169+
/* Predict for a plane table. */
170+
Assert(rte->eref && rte->eref->aliasname);
171+
s->sval = pstrdup(rte->eref->aliasname);
172+
relnames = list_make1(s);
173+
}
168174

169175
clauses = aqo_get_clauses(root, rel->baserestrictinfo);
170-
predicted = predict_for_relation(clauses, selectivities,
171-
relids, &fss);
176+
predicted = predict_for_relation(clauses, selectivities, relnames, &fss);
172177
rel->fss_hash = fss;
173178

174179
list_free_deep(selectivities);
175180
list_free(clauses);
176-
list_free(relids);
181+
list_free(relnames);
177182

178183
if (predicted >= 0)
179184
{
@@ -209,8 +214,8 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
209214
List *param_clauses)
210215
{
211216
double predicted;
212-
Oid relid = InvalidOid;
213-
List *relids = NIL;
217+
RangeTblEntry *rte = NULL;
218+
List *relnames = NIL;
214219
List *allclauses = NULL;
215220
List *selectivities = NULL;
216221
ListCell *l;
@@ -219,7 +224,7 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
219224
int *args_hash;
220225
int *eclass_hash;
221226
int current_hash;
222-
int fss = 0;
227+
int fss = 0;
223228

224229
if (IsQueryDisabled())
225230
/* Fast path */
@@ -239,7 +244,7 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
239244
/* Make specific copy of clauses with mutated subplans */
240245
allclauses = list_concat(aqo_get_clauses(root, param_clauses),
241246
aqo_get_clauses(root, rel->baserestrictinfo));
242-
relid = planner_rt_fetch(rel->relid, root)->relid;
247+
rte = planner_rt_fetch(rel->relid, root);
243248
get_eclasses(allclauses, &nargs, &args_hash, &eclass_hash);
244249

245250
old_ctx_m = MemoryContextSwitchTo(AQO_cache_mem_ctx);
@@ -249,7 +254,7 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
249254
current_hash = get_clause_hash(
250255
((RestrictInfo *) lfirst(l))->clause,
251256
nargs, args_hash, eclass_hash);
252-
cache_selectivity(current_hash, rel->relid, relid,
257+
cache_selectivity(current_hash, rel->relid, rte->relid,
253258
*((double *) lfirst(l2)));
254259
}
255260

@@ -269,11 +274,17 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
269274
goto default_estimator;
270275
}
271276

272-
if (OidIsValid(relid))
273-
/* Predict for a plane table only. */
274-
relids = list_make1_int(relid);
277+
if (rte && OidIsValid(rte->relid))
278+
{
279+
String *s = makeNode(String);
280+
281+
/* Predict for a plane table. */
282+
Assert(rte->eref && rte->eref->aliasname);
283+
s->sval = pstrdup(rte->eref->aliasname);
284+
relnames = list_make1(s);
285+
}
275286

276-
predicted = predict_for_relation(allclauses, selectivities, relids, &fss);
287+
predicted = predict_for_relation(allclauses, selectivities, relnames, &fss);
277288

278289
predicted_ppi_rows = predicted;
279290
fss_ppi_hash = fss;
@@ -298,7 +309,7 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
298309
List *restrictlist)
299310
{
300311
double predicted;
301-
List *relids;
312+
List *relnames;
302313
List *outer_clauses;
303314
List *inner_clauses;
304315
List *allclauses;
@@ -324,7 +335,7 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
324335
goto default_estimator;
325336
}
326337

327-
relids = get_list_of_relids(root, rel->relids);
338+
relnames = get_relnames(root, rel->relids);
328339
outer_clauses = get_path_clauses(outer_rel->cheapest_total_path, root,
329340
&outer_selectivities);
330341
inner_clauses = get_path_clauses(inner_rel->cheapest_total_path, root,
@@ -335,7 +346,7 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
335346
list_concat(outer_selectivities,
336347
inner_selectivities));
337348

338-
predicted = predict_for_relation(allclauses, selectivities, relids, &fss);
349+
predicted = predict_for_relation(allclauses, selectivities, relnames, &fss);
339350
rel->fss_hash = fss;
340351

341352
if (predicted >= 0)
@@ -366,7 +377,7 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
366377
List *clauses)
367378
{
368379
double predicted;
369-
List *relids;
380+
List *relnames;
370381
List *outer_clauses;
371382
List *inner_clauses;
372383
List *allclauses;
@@ -392,7 +403,7 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
392403
goto default_estimator;
393404
}
394405

395-
relids = get_list_of_relids(root, rel->relids);
406+
relnames = get_relnames(root, rel->relids);
396407
outer_clauses = get_path_clauses(outer_path, root, &outer_selectivities);
397408
inner_clauses = get_path_clauses(inner_path, root, &inner_selectivities);
398409
allclauses = list_concat(aqo_get_clauses(root, clauses),
@@ -401,7 +412,7 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
401412
list_concat(outer_selectivities,
402413
inner_selectivities));
403414

404-
predicted = predict_for_relation(allclauses, selectivities, relids, &fss);
415+
predicted = predict_for_relation(allclauses, selectivities, relnames, &fss);
405416

406417
predicted_ppi_rows = predicted;
407418
fss_ppi_hash = fss;
@@ -428,13 +439,13 @@ predict_num_groups(PlannerInfo *root, Path *subpath, List *group_exprs,
428439
child_fss = subpath->parent->fss_hash;
429440
else
430441
{
431-
List *relids;
442+
List *relnames;
432443
List *clauses;
433444
List *selectivities = NIL;
434445

435-
relids = get_list_of_relids(root, subpath->parent->relids);
446+
relnames = get_relnames(root, subpath->parent->relids);
436447
clauses = get_path_clauses(subpath, root, &selectivities);
437-
(void) predict_for_relation(clauses, selectivities, relids, &child_fss);
448+
(void) predict_for_relation(clauses, selectivities, relnames, &child_fss);
438449
}
439450

440451
*fss = get_grouped_exprs_hash(child_fss, group_exprs);

0 commit comments

Comments
 (0)