Skip to content

Commit 48f6df1

Browse files
committed
Merge file storage feature and look-a-like
1 parent 98b3624 commit 48f6df1

9 files changed

+110
-59
lines changed

‎expected/aqo_learn.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ SELECT * FROM check_estimated_rows('
540540
20 | 17
541541
(1 row)
542542

543-
SELECT count(*) FROM -- Learn on the query
543+
SELECT count(*) FROM
544544
(SELECT fs FROM aqo_data GROUP BY (fs)) AS q1
545545
;
546546
count

‎expected/clean_aqo_data.out

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ SELECT * FROM a;
1111
(0 rows)
1212

1313
SELECT 'a'::regclass::oid AS a_oid \gset
14-
SELECT aqo_cleanup();
15-
aqo_cleanup
16-
-------------
17-
(0,0)
14+
SELECT true FROM aqo_cleanup();
15+
bool
16+
------
17+
t
1818
(1 row)
1919

2020
/*
@@ -54,10 +54,10 @@ SELECT count(*) FROM aqo_query_stat WHERE
5454
(1 row)
5555

5656
DROP TABLE a;
57-
SELECT aqo_cleanup();
58-
aqo_cleanup
59-
-------------
60-
(1,1)
57+
SELECT true FROM aqo_cleanup();
58+
bool
59+
------
60+
t
6161
(1 row)
6262

6363
/*
@@ -175,10 +175,10 @@ SELECT count(*) FROM aqo_query_stat WHERE
175175
(1 row)
176176

177177
DROP TABLE a;
178-
SELECT aqo_cleanup();
179-
aqo_cleanup
180-
-------------
181-
(2,3)
178+
SELECT true FROM aqo_cleanup();
179+
bool
180+
------
181+
t
182182
(1 row)
183183

184184
/*
@@ -253,10 +253,10 @@ SELECT count(*) FROM aqo_query_stat WHERE
253253
(1 row)
254254

255255
DROP TABLE b;
256-
SELECT aqo_cleanup();
257-
aqo_cleanup
258-
-------------
259-
(1,1)
256+
SELECT true FROM aqo_cleanup();
257+
bool
258+
------
259+
t
260260
(1 row)
261261

262262
-- lines corresponding to b_oid in theese tables deleted

‎expected/gucs.out

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ SET aqo.show_details = true;
55
CREATE TABLE t(x int);
66
INSERT INTO t (x) (SELECT * FROM generate_series(1, 100) AS gs);
77
ANALYZE t;
8-
SELECT * FROM aqo_reset(); -- Remember! DROP EXTENSION doesn't remove any AQO data gathered.
9-
aqo_reset
10-
-----------
11-
25
8+
SELECT true FROM aqo_reset(); -- Remember! DROP EXTENSION doesn't remove any AQO data gathered.
9+
bool
10+
------
11+
t
1212
(1 row)
1313

1414
-- Check AQO addons to explain (the only stable data)
@@ -123,10 +123,10 @@ SELECT count(*) FROM aqo_query_stat;
123123
1
124124
(1 row)
125125

126-
SELECT * FROM aqo_reset(); -- Remove one record from all tables
127-
aqo_reset
128-
-----------
129-
4
126+
SELECT true FROM aqo_reset(); -- Remove one record from all tables
127+
bool
128+
------
129+
t
130130
(1 row)
131131

132132
SELECT count(*) FROM aqo_query_stat;

‎machine_learning.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ OkNNr_allocate(int ncols)
4848
int i;
4949

5050
if (ncols > 0)
51-
for (i = 0; i < aqo_K; ++i)
52-
data->matrix[i] = palloc0(sizeof(double) * ncols);
51+
for (i = 0; i < aqo_K; i++)
52+
data->matrix[i] = palloc0(ncols * sizeof(double));
53+
else
54+
for (i = 0; i < aqo_K; i++)
55+
data->matrix[i] = NULL;
5356

5457
data->cols = ncols;
58+
data->rows = -1;
5559
return data;
5660
}
5761

‎postprocessing.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ learn_agg_sample(aqo_obj_stat *ctx, RelSortOut *rels,
109109
uint64 fs = query_context.fspace_hash;
110110
int child_fss;
111111
double target;
112-
OkNNrdata data;
112+
OkNNrdata *data = OkNNr_allocate(0);
113113
int fss;
114-
int i;
115114

116115
/*
117116
* Learn 'not executed' nodes only once, if no one another knowledge exists
@@ -125,13 +124,10 @@ learn_agg_sample(aqo_obj_stat *ctx, RelSortOut *rels,
125124
NIL, NULL,NULL);
126125
fss = get_grouped_exprs_hash(child_fss, aqo_node->grouping_exprs);
127126

128-
memset(&data, 0, sizeof(OkNNrdata));
129-
for (i = 0; i < aqo_K; i++)
130-
data.matrix[i] = NULL;
131-
132127
/* Critical section */
133-
atomic_fss_learn_step(fs, fss, &data, NULL,
128+
atomic_fss_learn_step(fs, fss, data, NULL,
134129
target, rfactor, rels->hrels, ctx->isTimedOut);
130+
OkNNr_free(data);
135131
/* End of critical section */
136132
}
137133

‎sql/aqo_learn.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ SELECT * FROM check_estimated_rows('
235235
SELECT *
236236
FROM aqo_test1 AS t1, aqo_test1 AS t2, aqo_test1 AS t3, aqo_test1 AS t4
237237
WHERE t1.a = t2.b AND t2.a = t3.b AND t3.a = t4.b;
238-
');
239-
SELECT count(*) FROM -- Learn on the query
238+
'); -- Learn on the query
239+
SELECT count(*) FROM
240240
(SELECT fs FROM aqo_data GROUP BY (fs)) AS q1
241241
;
242242
SELECT query_text FROM aqo_query_texts WHERE queryid <> 0; -- Check query

‎sql/clean_aqo_data.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DROP TABLE IF EXISTS b;
77
CREATE TABLE a();
88
SELECT * FROM a;
99
SELECT 'a'::regclass::oid AS a_oid \gset
10-
SELECT aqo_cleanup();
10+
SELECT true FROM aqo_cleanup();
1111

1212
/*
1313
* lines with a_oid in aqo_data,
@@ -27,7 +27,7 @@ SELECT count(*) FROM aqo_query_stat WHERE
2727
aqo_queries.fs = ANY(SELECT aqo_data.fs FROM aqo_data WHERE :a_oid=ANY(oids)));
2828

2929
DROP TABLE a;
30-
SELECT aqo_cleanup();
30+
SELECT true FROM aqo_cleanup();
3131

3232
/*
3333
* lines with a_oid in aqo_data,
@@ -79,7 +79,7 @@ SELECT count(*) FROM aqo_query_stat WHERE
7979
aqo_queries.fs = ANY(SELECT aqo_data.fs FROM aqo_data WHERE :b_oid=ANY(oids)));
8080

8181
DROP TABLE a;
82-
SELECT aqo_cleanup();
82+
SELECT true FROM aqo_cleanup();
8383

8484
/*
8585
* lines corresponding to a_oid and both a_oid's fs deleted in aqo_data,
@@ -115,7 +115,7 @@ SELECT count(*) FROM aqo_query_stat WHERE
115115
aqo_queries.fs = aqo_queries.queryid);
116116

117117
DROP TABLE b;
118-
SELECT aqo_cleanup();
118+
SELECT true FROM aqo_cleanup();
119119

120120
-- lines corresponding to b_oid in theese tables deleted
121121
SELECT count(*) FROM aqo_data WHERE :b_oid=ANY(oids);

‎sql/gucs.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CREATE TABLE t(x int);
88
INSERT INTO t (x) (SELECT * FROM generate_series(1, 100) AS gs);
99
ANALYZE t;
1010

11-
SELECT * FROM aqo_reset(); -- Remember! DROP EXTENSION doesn't remove any AQO data gathered.
11+
SELECT true FROM aqo_reset(); -- Remember! DROP EXTENSION doesn't remove any AQO data gathered.
1212
-- Check AQO addons to explain (the only stable data)
1313
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
1414
SELECT x FROM t;
@@ -33,7 +33,7 @@ SELECT obj_description('aqo_reset'::regproc::oid);
3333

3434
-- Check stat reset
3535
SELECT count(*) FROM aqo_query_stat;
36-
SELECT * FROM aqo_reset(); -- Remove one record from all tables
36+
SELECT true FROM aqo_reset(); -- Remove one record from all tables
3737
SELECT count(*) FROM aqo_query_stat;
3838

3939
DROP EXTENSION aqo;

‎storage.c

+68-17
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,10 @@ aqo_query_texts(PG_FUNCTION_ARGS)
983983
hash_seq_init(&hash_seq, qtexts_htab);
984984
while ((entry = hash_seq_search(&hash_seq)) != NULL)
985985
{
986+
char *ptr;
987+
986988
Assert(DsaPointerIsValid(entry->qtext_dp));
987-
char *ptr = dsa_get_address(qtext_dsa, entry->qtext_dp);
989+
ptr = dsa_get_address(qtext_dsa, entry->qtext_dp);
988990
values[QT_QUERYID] = Int64GetDatum(entry->queryid);
989991
values[QT_QUERY_STRING] = CStringGetTextDatum(ptr);
990992
tuplestore_putvalues(tupstore, tupDesc, values, nulls);
@@ -1170,7 +1172,7 @@ build_knn_matrix(OkNNrdata *data, const OkNNrdata *temp_data)
11701172
{
11711173
Assert(data->cols == temp_data->cols);
11721174

1173-
if (data->rows >= 0)
1175+
if (data->rows > 0)
11741176
/* trivial strategy - use first suitable record and ignore others */
11751177
return;
11761178

@@ -1201,8 +1203,7 @@ _fill_knn_data(const DataEntry *entry, List **reloids)
12011203
/* Check invariants */
12021204
Assert(entry->rows < aqo_K);
12031205
Assert(ptr != NULL);
1204-
Assert(entry->key.fs == ((data_key *)ptr)->fs &&
1205-
entry->key.fss == ((data_key *)ptr)->fss);
1206+
Assert(entry->key.fss == ((data_key *)ptr)->fss);
12061207

12071208
ptr += sizeof(data_key);
12081209

@@ -1227,6 +1228,7 @@ _fill_knn_data(const DataEntry *entry, List **reloids)
12271228
Assert(offset <= sz);
12281229

12291230
if (reloids == NULL)
1231+
/* Isn't needed to load reloids list */
12301232
return data;
12311233

12321234
/* store list of relations. XXX: optimize ? */
@@ -1260,26 +1262,72 @@ load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,
12601262
dsa_init();
12611263

12621264
LWLockAcquire(&aqo_state->data_lock, LW_EXCLUSIVE);
1263-
entry = (DataEntry *) hash_search(data_htab, &key, HASH_FIND, &found);
12641265

1265-
if (!found)
1266-
goto end;
1266+
if (!wideSearch)
1267+
{
1268+
entry = (DataEntry *) hash_search(data_htab, &key, HASH_FIND, &found);
12671269

1268-
/* One entry with all correctly filled fields is found */
1269-
Assert(entry);
1270-
Assert(DsaPointerIsValid(entry->data_dp));
1270+
if (!found)
1271+
goto end;
1272+
1273+
/* One entry with all correctly filled fields is found */
1274+
Assert(entry);
1275+
Assert(DsaPointerIsValid(entry->data_dp));
1276+
1277+
if (entry->cols != data->cols)
1278+
{
1279+
/* Collision happened? */
1280+
elog(LOG, "[AQO] Does a collision happened? Check it if possible (fs: %lu, fss: %d).",
1281+
fs, fss);
1282+
found = false;
1283+
goto end;
1284+
}
12711285

1272-
if (entry->cols != data->cols)
1286+
temp_data = _fill_knn_data(entry, reloids);
1287+
build_knn_matrix(data, temp_data);
1288+
}
1289+
else
1290+
/* Iterate across all elements of the table. XXX: Maybe slow. */
12731291
{
1274-
/* Collision happened? */
1275-
elog(LOG, "[AQO] Does a collision happened? Check it if possible (fs: %lu, fss: %d).",
1276-
fs, fss);
1292+
HASH_SEQ_STATUS hash_seq;
1293+
int noids = -1;
1294+
12771295
found = false;
1278-
goto end;
1296+
hash_seq_init(&hash_seq, data_htab);
1297+
while ((entry = hash_seq_search(&hash_seq)) != NULL)
1298+
{
1299+
List *tmp_oids = NIL;
1300+
1301+
if (entry->key.fss != fss || entry->cols != data->cols)
1302+
continue;
1303+
1304+
temp_data = _fill_knn_data(entry, &tmp_oids);
1305+
1306+
if (data->rows > 0 && list_length(tmp_oids) != noids)
1307+
{
1308+
/* Dubious case. So log it and skip these data */
1309+
elog(LOG,
1310+
"[AQO] different number depended oids for the same fss %d: "
1311+
"%d and %d correspondingly.",
1312+
fss, list_length(tmp_oids), noids);
1313+
Assert(noids >= 0);
1314+
list_free(tmp_oids);
1315+
continue;
1316+
}
1317+
1318+
noids = list_length(tmp_oids);
1319+
1320+
if (reloids != NULL && *reloids == NIL)
1321+
*reloids = tmp_oids;
1322+
else
1323+
list_free(tmp_oids);
1324+
1325+
build_knn_matrix(data, temp_data);
1326+
found = true;
1327+
}
12791328
}
12801329

1281-
temp_data = _fill_knn_data(entry, reloids);
1282-
build_knn_matrix(data, temp_data);
1330+
Assert(!found || (data->rows > 0 && data->rows <= aqo_K));
12831331
end:
12841332
LWLockRelease(&aqo_state->data_lock);
12851333

@@ -1364,7 +1412,10 @@ aqo_data(PG_FUNCTION_ARGS)
13641412

13651413
elems = palloc(sizeof(*elems) * entry->nrels);
13661414
for(i = 0; i < entry->nrels; i++)
1415+
{
13671416
elems[i] = ObjectIdGetDatum(*(Oid *)ptr);
1417+
ptr += sizeof(Oid);
1418+
}
13681419

13691420
array = construct_array(elems, entry->nrels, OIDOID,
13701421
sizeof(Oid), true, TYPALIGN_INT);

0 commit comments

Comments
 (0)