Skip to content

Commit 2bc6fea

Browse files
author
Alexandra Pervushina
committed
Add test, add dbid to aqo table output
1 parent dd47f03 commit 2bc6fea

File tree

5 files changed

+105
-29
lines changed

5 files changed

+105
-29
lines changed

aqo--1.6.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ COMMENT ON FUNCTION aqo_reset(oid) IS
167167
CREATE FUNCTION aqo_data (
168168
OUT fs bigint,
169169
OUT fss integer,
170+
OUT dbid Oid,
170171
OUT nfeatures integer,
171172
OUT features double precision[][],
172173
OUT targets double precision[],
@@ -179,6 +180,7 @@ LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
179180

180181
CREATE FUNCTION aqo_queries (
181182
OUT queryid bigint,
183+
OUT dbid Oid,
182184
OUT fs bigint,
183185
OUT learn_aqo boolean,
184186
OUT use_aqo boolean,
@@ -192,6 +194,7 @@ LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
192194

193195
CREATE FUNCTION aqo_query_stat (
194196
OUT queryid bigint,
197+
OUT dbid oid,
195198
OUT execution_time_with_aqo double precision[],
196199
OUT execution_time_without_aqo double precision[],
197200
OUT planning_time_with_aqo double precision[],
@@ -205,7 +208,7 @@ RETURNS SETOF record
205208
AS 'MODULE_PATHNAME', 'aqo_query_stat'
206209
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
207210

208-
CREATE FUNCTION aqo_query_texts(OUT queryid bigint, OUT query_text text)
211+
CREATE FUNCTION aqo_query_texts(OUT queryid bigint, OUT dbid Oid, OUT query_text text)
209212
RETURNS SETOF record
210213
AS 'MODULE_PATHNAME', 'aqo_query_texts'
211214
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;

expected/multiple_databases.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

regress_schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ test: top_queries
2121
test: relocatable
2222
test: look_a_like
2323
test: feature_subspace
24+
test: multiple_databases

sql/multiple_databases.sql

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
-- Tests on cross-databases interference.
2+
3+
create extension aqo;
4+
set aqo.join_threshold = 0;
5+
set aqo.show_details = on;
6+
set aqo.mode = learn;
7+
set aqo.use = on;
8+
select * from aqo_reset(NULL);
9+
10+
CREATE DATABASE aqo_crossdb_test;
11+
-- Save current database and port.
12+
SELECT current_database() AS old_db \gset
13+
SELECT oid AS old_dbid FROM pg_database WHERE datname = current_database() \gset
14+
SELECT setting AS old_port FROM pg_settings WHERE name = 'port' \gset
15+
16+
CREATE TABLE a (x1 int, x2 int, x3 int);
17+
INSERT INTO a (x1, x2, x3) SELECT mod(ival,10), mod(ival,10), mod(ival,10) FROM generate_series(1,100) As ival;
18+
19+
CREATE TABLE b (y1 int, y2 int, y3 int);
20+
INSERT INTO b (y1, y2, y3) SELECT mod(ival + 1,10), mod(ival + 1,10), mod(ival + 1,10) FROM generate_series(1,1000) As ival;
21+
22+
--
23+
-- Returns string-by-string explain of a query. Made for removing some strings
24+
-- from the explain output.
25+
--
26+
CREATE OR REPLACE FUNCTION expln(query_string text) RETURNS SETOF text AS $$
27+
BEGIN
28+
RETURN QUERY
29+
EXECUTE format('EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF) %s', query_string);
30+
RETURN;
31+
END;
32+
$$ LANGUAGE PLPGSQL;
33+
34+
SELECT str AS result
35+
FROM expln('
36+
SELECT x1,y1 FROM A LEFT JOIN b ON A.x1 = B.y1 WHERE x1 = 5 AND x2 = 5;') AS str
37+
WHERE str NOT LIKE 'Query Identifier%' and str NOT LIKE '%Memory%' and str NOT LIKE '%Sort Method%';
38+
39+
SELECT count(*) FROM aqo_data();
40+
SELECT count(*) FROM aqo_queries;();
41+
SELECT count(*) FROM aqo_query_texts();
42+
SELECT count(*) FROM aqo_query_stat();
43+
44+
45+
-- Connect to other DB
46+
\c aqo_crossdb_test - - :old_port
47+
create extension aqo;
48+
set aqo.join_threshold = 0;
49+
set aqo.show_details = on;
50+
set aqo.mode = learn;
51+
set aqo.use = on;
52+
select * from aqo_reset(NULL);
53+
54+
-- Only see info from current database.
55+
SELECT count(*) FROM aqo_data();
56+
SELECT count(*) FROM aqo_queries;();
57+
SELECT count(*) FROM aqo_query_texts();
58+
SELECT count(*) FROM aqo_query_stat();
59+
-- Remove plan from other DB.
60+
SELECT aqo_reset(:old_dbid);
61+
62+
-- Reconnect to old DB.
63+
\c :old_db - - :old_port
64+
SELECT count(*) FROM aqo_data();
65+
SELECT count(*) FROM aqo_queries;();
66+
SELECT count(*) FROM aqo_query_texts();
67+
SELECT count(*) FROM aqo_query_stat();
68+
SELECT aqo_reset(NULL);
69+
70+
DROP DATABASE aqo_crossdb_test;
71+
DROP EXTENSION aqo;

storage.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@
4141

4242

4343
typedef enum {
44-
QUERYID = 0, EXEC_TIME_AQO, EXEC_TIME, PLAN_TIME_AQO, PLAN_TIME,
44+
QUERYID = 0, DBID, EXEC_TIME_AQO, EXEC_TIME, PLAN_TIME_AQO, PLAN_TIME,
4545
EST_ERROR_AQO, EST_ERROR, NEXECS_AQO, NEXECS, TOTAL_NCOLS
4646
} aqo_stat_cols;
4747

4848
typedef enum {
49-
QT_QUERYID = 0, QT_QUERY_STRING, QT_TOTAL_NCOLS
49+
QT_QUERYID = 0, QT_DBID, QT_QUERY_STRING, QT_TOTAL_NCOLS
5050
} aqo_qtexts_cols;
5151

5252
typedef enum {
53-
AD_FS = 0, AD_FSS, AD_NFEATURES, AD_FEATURES, AD_TARGETS, AD_RELIABILITY,
53+
AD_FS = 0, AD_FSS, AD_DBID, AD_NFEATURES, AD_FEATURES, AD_TARGETS, AD_RELIABILITY,
5454
AD_OIDS, AD_TOTAL_NCOLS
5555
} aqo_data_cols;
5656

5757
typedef enum {
58-
AQ_QUERYID = 0, AQ_FS, AQ_LEARN_AQO, AQ_USE_AQO, AQ_AUTO_TUNING, AQ_SMART_TIMEOUT, AQ_COUNT_INCREASE_TIMEOUT,
58+
AQ_QUERYID = 0, AQ_DBID, AQ_FS, AQ_LEARN_AQO, AQ_USE_AQO, AQ_AUTO_TUNING, AQ_SMART_TIMEOUT, AQ_COUNT_INCREASE_TIMEOUT,
5959
AQ_TOTAL_NCOLS
6060
} aqo_queries_cols;
6161

@@ -423,10 +423,8 @@ aqo_query_stat(PG_FUNCTION_ARGS)
423423
hash_seq_init(&hash_seq, stat_htab);
424424
while ((entry = hash_seq_search(&hash_seq)) != NULL)
425425
{
426-
if (entry->key.dbid != (uint64) MyDatabaseId)
427-
continue;
428-
429426
values[QUERYID] = Int64GetDatum(entry->key.queryid);
427+
values[DBID] = ObjectIdGetDatum(entry->key.dbid);
430428
values[NEXECS] = Int64GetDatum(entry->execs_without_aqo);
431429
values[NEXECS_AQO] = Int64GetDatum(entry->execs_with_aqo);
432430
values[EXEC_TIME_AQO] = PointerGetDatum(form_vector(entry->exec_time_aqo, entry->cur_stat_slot_aqo));
@@ -1201,12 +1199,10 @@ aqo_query_texts(PG_FUNCTION_ARGS)
12011199
{
12021200
char *ptr;
12031201

1204-
if (entry->key.dbid != (uint64) (uint64) MyDatabaseId)
1205-
continue;
1206-
12071202
Assert(DsaPointerIsValid(entry->qtext_dp));
12081203
ptr = dsa_get_address(qtext_dsa, entry->qtext_dp);
12091204
values[QT_QUERYID] = Int64GetDatum(entry->key.queryid);
1205+
values[QT_DBID] = ObjectIdGetDatum(entry->key.dbid);
12101206
values[QT_QUERY_STRING] = CStringGetTextDatum(ptr);
12111207
tuplestore_putvalues(tupstore, tupDesc, values, nulls);
12121208
}
@@ -1220,13 +1216,14 @@ static bool
12201216
_aqo_stat_remove(uint64 queryid)
12211217
{
12221218
bool found;
1223-
stat_key key = {.queryid = queryid, .dbid = (uint64) (uint64) MyDatabaseId};
1219+
stat_key key = {.queryid = queryid, .dbid =(uint64) MyDatabaseId};
1220+
StatEntry *entry;
12241221

12251222
Assert(!LWLockHeldByMe(&aqo_state->stat_lock));
12261223
LWLockAcquire(&aqo_state->stat_lock, LW_EXCLUSIVE);
1227-
(void) hash_search(stat_htab, &key, HASH_FIND, &found);
1224+
entry = (StatEntry *) hash_search(stat_htab, &key, HASH_FIND, &found);
12281225

1229-
if (found)
1226+
if (found && entry->key.dbid == (uint64) MyDatabaseId)
12301227
{
12311228
(void) hash_search(stat_htab, &key, HASH_REMOVE, NULL);
12321229
aqo_state->stat_changed = true;
@@ -1239,15 +1236,16 @@ _aqo_stat_remove(uint64 queryid)
12391236
static bool
12401237
_aqo_queries_remove(uint64 queryid)
12411238
{
1242-
bool found;
1243-
queries_key key = {.queryid = queryid, .dbid = (uint64) (uint64) MyDatabaseId};
1239+
bool found;
1240+
queries_key key = {.queryid = queryid, .dbid = (uint64) MyDatabaseId};
1241+
QueriesEntry *entry;
12441242

12451243

12461244
Assert(!LWLockHeldByMe(&aqo_state->queries_lock));
12471245
LWLockAcquire(&aqo_state->queries_lock, LW_EXCLUSIVE);
1248-
(void) hash_search(queries_htab, &key, HASH_FIND, &found);
1246+
entry = (QueriesEntry *) hash_search(queries_htab, &key, HASH_FIND, &found);
12491247

1250-
if (found)
1248+
if (found && entry->key.dbid == (uint64) MyDatabaseId)
12511249
{
12521250
(void) hash_search(queries_htab, &key, HASH_REMOVE, NULL);
12531251
aqo_state->queries_changed = true;
@@ -1262,7 +1260,7 @@ _aqo_qtexts_remove(uint64 queryid)
12621260
{
12631261
bool found = false;
12641262
QueryTextEntry *entry;
1265-
qtext_key key = {.queryid = queryid, .dbid = (uint64) (uint64) MyDatabaseId};
1263+
qtext_key key = {.queryid = queryid, .dbid = (uint64) MyDatabaseId};
12661264

12671265
dsa_init();
12681266

@@ -1275,7 +1273,7 @@ _aqo_qtexts_remove(uint64 queryid)
12751273
*/
12761274
entry = (QueryTextEntry *) hash_search(qtexts_htab, &key, HASH_FIND,
12771275
&found);
1278-
if (found)
1276+
if (found && entry->key.dbid == (uint64) MyDatabaseId)
12791277
{
12801278
/* Free DSA memory, allocated for this record */
12811279
Assert(DsaPointerIsValid(entry->qtext_dp));
@@ -1299,7 +1297,7 @@ _aqo_data_remove(data_key *key)
12991297
LWLockAcquire(&aqo_state->data_lock, LW_EXCLUSIVE);
13001298

13011299
entry = (DataEntry *) hash_search(data_htab, key, HASH_FIND, &found);
1302-
if (found)
1300+
if (found && entry->key.dbid == (uint64) MyDatabaseId)
13031301
{
13041302
/* Free DSA memory, allocated for this record */
13051303
Assert(DsaPointerIsValid(entry->data_dp));
@@ -1376,7 +1374,7 @@ aqo_data_store(uint64 fs, int fss, AqoDataArgs *data, List *reloids)
13761374
{
13771375
DataEntry *entry;
13781376
bool found;
1379-
data_key key = {.fs = fs, .fss = fss, .dbid = (uint64) (uint64) MyDatabaseId};
1377+
data_key key = {.fs = fs, .fss = fss, .dbid = (uint64) MyDatabaseId};
13801378
int i;
13811379
char *ptr;
13821380
ListCell *lc;
@@ -1827,13 +1825,11 @@ aqo_data(PG_FUNCTION_ARGS)
18271825
{
18281826
char *ptr;
18291827

1830-
if (entry->key.dbid != (uint64) MyDatabaseId)
1831-
continue;
1832-
18331828
memset(nulls, 0, AD_TOTAL_NCOLS);
18341829

18351830
values[AD_FS] = Int64GetDatum(entry->key.fs);
18361831
values[AD_FSS] = Int32GetDatum((int) entry->key.fss);
1832+
values[AD_DBID] = ObjectIdGetDatum(entry->key.dbid);
18371833
values[AD_NFEATURES] = Int32GetDatum(entry->cols);
18381834

18391835
/* Fill values from the DSA data chunk */
@@ -1994,10 +1990,8 @@ aqo_queries(PG_FUNCTION_ARGS)
19941990
hash_seq_init(&hash_seq, queries_htab);
19951991
while ((entry = hash_seq_search(&hash_seq)) != NULL)
19961992
{
1997-
if (entry->key.dbid != (uint64) MyDatabaseId)
1998-
continue;
1999-
20001993
values[AQ_QUERYID] = Int64GetDatum(entry->key.queryid);
1994+
values[AQ_DBID] = ObjectIdGetDatum(entry->key.dbid);
20011995
values[AQ_FS] = Int64GetDatum(entry->fs);
20021996
values[AQ_LEARN_AQO] = BoolGetDatum(entry->learn_aqo);
20031997
values[AQ_USE_AQO] = BoolGetDatum(entry->use_aqo);
@@ -2351,7 +2345,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
23512345
List *actual_fss = NIL;
23522346
ListCell *lc;
23532347

2354-
if (entry->key.dbid != (uint64) MyDatabaseId)
2348+
if (entry->key.dbid != (uint64) MyDatabaseId && entry->key.queryid != 0)
23552349
continue;
23562350

23572351
/* Scan aqo_data for any junk records related to this FS */
@@ -2601,6 +2595,9 @@ aqo_cardinality_error(PG_FUNCTION_ARGS)
26012595
int64 nexecs;
26022596
int nvals;
26032597

2598+
if (qentry->key.dbid != (uint64) MyDatabaseId && qentry->key.queryid != 0)
2599+
continue;
2600+
26042601
sentry = (StatEntry *) hash_search(stat_htab, &qentry->key,
26052602
HASH_FIND, &found);
26062603
if (!found)
@@ -2696,6 +2693,9 @@ aqo_execution_time(PG_FUNCTION_ARGS)
26962693
int nvals;
26972694
double tm = 0;
26982695

2696+
if (qentry->key.dbid != (uint64) MyDatabaseId && qentry->key.queryid != 0)
2697+
continue;
2698+
26992699
sentry = (StatEntry *) hash_search(stat_htab, &qentry->key,
27002700
HASH_FIND, &found);
27012701
if (!found)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy