Skip to content

Commit 0ca4fe6

Browse files
committed
Minor fixes on making AQO relocatable. Plus add a test which type of relocatability we really want.
1 parent 21bf37e commit 0ca4fe6

File tree

3 files changed

+153
-117
lines changed

3 files changed

+153
-117
lines changed

aqo--1.4--1.5.sql

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
44
\echo Use "ALTER EXTENSION aqo UPDATE TO '1.5'" to load this file. \quit
55

6-
--
7-
-- Re-create the aqo_queries table.
8-
--
6+
DROP FUNCTION array_mse;
7+
DROP FUNCTION array_avg;
8+
DROP FUNCTION public.aqo_clear_hist; -- Should be renamed and reworked
9+
DROP FUNCTION public.aqo_disable_query;
10+
DROP FUNCTION public.aqo_drop;
11+
DROP FUNCTION public.aqo_enable_query;
12+
DROP FUNCTION public.aqo_ne_queries; -- Not needed anymore due to changing in the logic
13+
DROP FUNCTION public.aqo_status;
14+
DROP FUNCTION public.clean_aqo_data;
15+
DROP FUNCTION public.show_cardinality_errors;
16+
DROP FUNCTION public.top_time_queries;
17+
18+
DROP TABLE public.aqo_data CASCADE;
919
DROP TABLE public.aqo_queries CASCADE;
20+
DROP TABLE public.aqo_query_texts CASCADE;
21+
DROP TABLE public.aqo_query_stat CASCADE;
22+
1023
CREATE TABLE aqo_queries (
1124
query_hash bigint CONSTRAINT aqo_queries_query_hash_idx PRIMARY KEY,
1225
learn_aqo boolean NOT NULL,
@@ -15,19 +28,11 @@ CREATE TABLE aqo_queries (
1528
auto_tuning boolean NOT NULL
1629
);
1730

18-
--
19-
-- Re-create the aqo_query_texts table.
20-
--
21-
DROP TABLE public.aqo_query_texts CASCADE;
2231
CREATE TABLE aqo_query_texts (
2332
query_hash bigint CONSTRAINT aqo_query_texts_query_hash_idx PRIMARY KEY REFERENCES aqo_queries ON DELETE CASCADE,
2433
query_text text NOT NULL
2534
);
2635

27-
--
28-
-- Re-create the aqo_query_stat table.
29-
--
30-
DROP TABLE public.aqo_query_stat CASCADE;
3136
CREATE TABLE aqo_query_stat (
3237
query_hash bigint CONSTRAINT aqo_query_stat_idx PRIMARY KEY REFERENCES aqo_queries ON DELETE CASCADE,
3338
execution_time_with_aqo double precision[],
@@ -41,18 +46,21 @@ CREATE TABLE aqo_query_stat (
4146
);
4247

4348
--
44-
-- Re-create the aqo_data table. Do so to keep the columns order.
49+
-- Re-create the aqo_data table.
4550
-- The oids array contains oids of permanent tables only. It is used for cleanup
4651
-- ML knowledge base from queries that refer to removed tables.
4752
--
48-
DROP TABLE public.aqo_data CASCADE;
4953
CREATE TABLE aqo_data (
5054
fspace_hash bigint NOT NULL REFERENCES aqo_queries ON DELETE CASCADE,
5155
fsspace_hash int NOT NULL,
5256
nfeatures int NOT NULL,
5357
features double precision[][],
5458
targets double precision[],
59+
60+
-- oids of permanent tables only. It is used for cleanup
61+
-- ML knowledge base from queries that refer to removed tables.
5562
oids oid [] DEFAULT NULL,
63+
5664
reliability double precision []
5765
);
5866
CREATE UNIQUE INDEX aqo_fss_access_idx ON aqo_data (fspace_hash, fsspace_hash);
@@ -65,15 +73,6 @@ CREATE TRIGGER aqo_queries_invalidate AFTER UPDATE OR DELETE OR TRUNCATE
6573
ON aqo_queries FOR EACH STATEMENT
6674
EXECUTE PROCEDURE invalidate_deactivated_queries_cache();
6775

68-
DROP FUNCTION public.top_time_queries;
69-
DROP FUNCTION public.aqo_drop;
70-
DROP FUNCTION public.clean_aqo_data;
71-
DROP FUNCTION public.show_cardinality_errors;
72-
DROP FUNCTION array_mse;
73-
DROP FUNCTION array_avg;
74-
DROP FUNCTION public.aqo_ne_queries; -- Not needed anymore due to changing in the logic
75-
DROP FUNCTION public.aqo_clear_hist; -- Should be renamed and reworked
76-
7776
--
7877
-- Show execution time of queries, for which AQO has statistics.
7978
-- controlled - show stat on executions where AQO was used for cardinality
@@ -290,7 +289,6 @@ $$ LANGUAGE plpgsql;
290289
COMMENT ON FUNCTION aqo_reset_query(bigint) IS
291290
'Remove from AQO storage only learning data for given QueryId.';
292291

293-
DROP FUNCTION public.aqo_status;
294292
CREATE FUNCTION aqo_status(hash bigint)
295293
RETURNS TABLE (
296294
"learn" BOOL,
@@ -303,8 +301,7 @@ RETURNS TABLE (
303301
"t_aqo" TEXT,
304302
"err_aqo" TEXT,
305303
"iters_aqo" BIGINT
306-
)
307-
AS $func$
304+
) AS $$
308305
SELECT learn_aqo,use_aqo,auto_tuning,fspace_hash,
309306
to_char(execution_time_without_aqo[n4],'9.99EEEE'),
310307
to_char(cardinality_error_without_aqo[n2],'9.99EEEE'),
@@ -324,26 +321,21 @@ FROM aqo_queries aq, aqo_query_stat aqs,
324321
aqs.query_hash = $1) AS al) AS q
325322
WHERE (aqs.query_hash = aq.query_hash) AND
326323
aqs.query_hash = $1;
327-
$func$ LANGUAGE SQL;
324+
$$ LANGUAGE SQL;
328325

329-
DROP FUNCTION public.aqo_enable_query;
330326
CREATE FUNCTION aqo_enable_query(hash bigint)
331-
RETURNS VOID
332-
AS $func$
327+
RETURNS VOID AS $$
333328
UPDATE aqo_queries SET
334329
learn_aqo = 'true',
335330
use_aqo = 'true'
336331
WHERE query_hash = $1;
337-
$func$ LANGUAGE SQL;
332+
$$ LANGUAGE SQL;
338333

339-
DROP FUNCTION public.aqo_disable_query;
340334
CREATE FUNCTION aqo_disable_query(hash bigint)
341-
RETURNS VOID
342-
AS $func$
335+
RETURNS VOID AS $$
343336
UPDATE aqo_queries SET
344337
learn_aqo = 'false',
345338
use_aqo = 'false',
346339
auto_tuning = 'false'
347340
WHERE query_hash = $1;
348-
$func$ LANGUAGE SQL;
349-
341+
$$ LANGUAGE SQL;

expected/relocatable.out

Lines changed: 91 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,117 @@
1-
DROP EXTENSION IF EXISTS aqo CASCADE;
2-
DROP SCHEMA IF EXISTS test CASCADE;
3-
NOTICE: schema "test" does not exist, skipping
1+
DROP EXTENSION aqo CASCADE;
42
CREATE EXTENSION aqo;
53
SET aqo.join_threshold = 0;
6-
SET aqo.mode = 'intelligent';
7-
CREATE TABLE test (id SERIAL, data TEXT);
8-
INSERT INTO test (data) VALUES ('string');
9-
SELECT * FROM test;
10-
id | data
11-
----+--------
12-
1 | string
4+
SET aqo.mode = 'learn'; -- use this mode for unconditional learning
5+
CREATE TABLE test AS (SELECT id, 'payload' || id FROM generate_series(1,100) id);
6+
ANALYZE test;
7+
-- Learn on a query
8+
SELECT count(*) FROM test;
9+
count
10+
-------
11+
100
1312
(1 row)
1413

15-
SELECT query_text FROM aqo_query_texts;
16-
query_text
17-
---------------------------------------
18-
COMMON feature space (do not delete!)
19-
SELECT * FROM test;
20-
(2 rows)
21-
22-
SELECT learn_aqo, use_aqo, auto_tuning FROM aqo_queries;
23-
learn_aqo | use_aqo | auto_tuning
24-
-----------+---------+-------------
25-
f | f | f
26-
t | f | t
14+
SELECT query_text,learn_aqo, use_aqo, auto_tuning
15+
FROM aqo_query_texts JOIN aqo_queries USING (query_hash)
16+
; -- Check result. TODO: use aqo_status()
17+
query_text | learn_aqo | use_aqo | auto_tuning
18+
---------------------------------------+-----------+---------+-------------
19+
COMMON feature space (do not delete!) | f | f | f
20+
SELECT count(*) FROM test; | t | t | f
2721
(2 rows)
2822

23+
-- Create a schema and move AQO into it.
2924
CREATE SCHEMA IF NOT EXISTS test;
3025
ALTER EXTENSION aqo SET SCHEMA test;
31-
SET aqo.mode = 'intelligent';
32-
CREATE TABLE test1 (id SERIAL, data TEXT);
33-
INSERT INTO test1 (data) VALUES ('string');
34-
SELECT * FROM test1;
35-
id | data
36-
----+--------
37-
1 | string
26+
-- Do something to be confident that AQO works
27+
SELECT count(*) FROM test;
28+
count
29+
-------
30+
100
31+
(1 row)
32+
33+
SELECT count(*) FROM test WHERE id < 10;
34+
count
35+
-------
36+
9
37+
(1 row)
38+
39+
SELECT query_text,learn_aqo, use_aqo, auto_tuning
40+
FROM test.aqo_query_texts JOIN test.aqo_queries USING (query_hash)
41+
; -- Check result. TODO: We want to find here both queries executed above
42+
query_text | learn_aqo | use_aqo | auto_tuning
43+
---------------------------------------+-----------+---------+-------------
44+
COMMON feature space (do not delete!) | f | f | f
45+
SELECT count(*) FROM test; | t | t | f
46+
(2 rows)
47+
48+
-- Add schema which contains AQO to the end of search_path
49+
SELECT set_config('search_path', current_setting('search_path') || ', test', false);
50+
set_config
51+
-----------------------
52+
"$user", public, test
53+
(1 row)
54+
55+
SELECT count(*) FROM test;
56+
count
57+
-------
58+
100
59+
(1 row)
60+
61+
SELECT count(*) FROM test WHERE id < 10;
62+
count
63+
-------
64+
9
3865
(1 row)
3966

40-
SELECT query_text FROM test.aqo_query_texts;
41-
query_text
42-
---------------------------------------
43-
COMMON feature space (do not delete!)
44-
SELECT * FROM test;
67+
SELECT query_text,learn_aqo, use_aqo, auto_tuning
68+
FROM test.aqo_query_texts JOIN test.aqo_queries USING (query_hash)
69+
; -- Check result.
70+
query_text | learn_aqo | use_aqo | auto_tuning
71+
------------------------------------------+-----------+---------+-------------
72+
COMMON feature space (do not delete!) | f | f | f
73+
SELECT count(*) FROM test; | t | t | f
74+
SELECT count(*) FROM test WHERE id < 10; | t | t | f
75+
(3 rows)
76+
77+
/*
78+
* Below, we should check each UI function
79+
*/
80+
SELECT aqo_disable_query(id) FROM (
81+
SELECT query_hash AS id FROM aqo_queries WHERE query_hash <> 0) AS q1;
82+
aqo_disable_query
83+
-------------------
84+
85+
4586
(2 rows)
4687

4788
SELECT learn_aqo, use_aqo, auto_tuning FROM test.aqo_queries;
4889
learn_aqo | use_aqo | auto_tuning
4990
-----------+---------+-------------
5091
f | f | f
51-
t | f | t
52-
(2 rows)
53-
54-
SET search_path TO test;
55-
CREATE TABLE test2 (id SERIAL, data TEXT);
56-
INSERT INTO test2 (data) VALUES ('string');
57-
SELECT * FROM test2;
58-
id | data
59-
----+--------
60-
1 | string
61-
(1 row)
62-
63-
SELECT query_text FROM aqo_query_texts;
64-
query_text
65-
---------------------------------------
66-
COMMON feature space (do not delete!)
67-
SELECT * FROM test;
68-
SELECT * FROM test2;
92+
f | f | f
93+
f | f | f
6994
(3 rows)
7095

71-
SELECT learn_aqo, use_aqo, auto_tuning FROM aqo_queries;
96+
SELECT aqo_enable_query(id) FROM (
97+
SELECT query_hash AS id FROM aqo_queries WHERE query_hash <> 0) AS q1;
98+
aqo_enable_query
99+
------------------
100+
101+
102+
(2 rows)
103+
104+
SELECT learn_aqo, use_aqo, auto_tuning FROM test.aqo_queries;
72105
learn_aqo | use_aqo | auto_tuning
73106
-----------+---------+-------------
74107
f | f | f
75-
t | f | t
76-
t | f | t
108+
t | t | f
109+
t | t | f
77110
(3 rows)
78111

112+
RESET search_path;
113+
DROP TABLE test CASCADE;
79114
DROP SCHEMA IF EXISTS test CASCADE;
80-
NOTICE: drop cascades to 2 other objects
81-
DETAIL: drop cascades to extension aqo
82-
drop cascades to table test2
115+
NOTICE: drop cascades to extension aqo
83116
DROP EXTENSION IF EXISTS aqo CASCADE;
84117
NOTICE: extension "aqo" does not exist, skipping
85-
SET search_path TO public;

sql/relocatable.sql

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
1-
DROP EXTENSION IF EXISTS aqo CASCADE;
2-
DROP SCHEMA IF EXISTS test CASCADE;
3-
1+
DROP EXTENSION aqo CASCADE;
42
CREATE EXTENSION aqo;
53
SET aqo.join_threshold = 0;
6-
SET aqo.mode = 'intelligent';
4+
SET aqo.mode = 'learn'; -- use this mode for unconditional learning
75

8-
CREATE TABLE test (id SERIAL, data TEXT);
9-
INSERT INTO test (data) VALUES ('string');
10-
SELECT * FROM test;
6+
CREATE TABLE test AS (SELECT id, 'payload' || id FROM generate_series(1,100) id);
7+
ANALYZE test;
118

12-
SELECT query_text FROM aqo_query_texts;
13-
SELECT learn_aqo, use_aqo, auto_tuning FROM aqo_queries;
9+
-- Learn on a query
10+
SELECT count(*) FROM test;
11+
SELECT query_text,learn_aqo, use_aqo, auto_tuning
12+
FROM aqo_query_texts JOIN aqo_queries USING (query_hash)
13+
; -- Check result. TODO: use aqo_status()
1414

15+
-- Create a schema and move AQO into it.
1516
CREATE SCHEMA IF NOT EXISTS test;
1617
ALTER EXTENSION aqo SET SCHEMA test;
1718

18-
SET aqo.mode = 'intelligent';
19+
-- Do something to be confident that AQO works
20+
SELECT count(*) FROM test;
21+
SELECT count(*) FROM test WHERE id < 10;
1922

20-
CREATE TABLE test1 (id SERIAL, data TEXT);
21-
INSERT INTO test1 (data) VALUES ('string');
22-
SELECT * FROM test1;
23+
SELECT query_text,learn_aqo, use_aqo, auto_tuning
24+
FROM test.aqo_query_texts JOIN test.aqo_queries USING (query_hash)
25+
; -- Check result. TODO: We want to find here both queries executed above
2326

24-
SELECT query_text FROM test.aqo_query_texts;
25-
SELECT learn_aqo, use_aqo, auto_tuning FROM test.aqo_queries;
27+
-- Add schema which contains AQO to the end of search_path
28+
SELECT set_config('search_path', current_setting('search_path') || ', test', false);
2629

27-
SET search_path TO test;
30+
SELECT count(*) FROM test;
31+
SELECT count(*) FROM test WHERE id < 10;
2832

29-
CREATE TABLE test2 (id SERIAL, data TEXT);
30-
INSERT INTO test2 (data) VALUES ('string');
31-
SELECT * FROM test2;
33+
SELECT query_text,learn_aqo, use_aqo, auto_tuning
34+
FROM test.aqo_query_texts JOIN test.aqo_queries USING (query_hash)
35+
; -- Check result.
3236

33-
SELECT query_text FROM aqo_query_texts;
34-
SELECT learn_aqo, use_aqo, auto_tuning FROM aqo_queries;
37+
/*
38+
* Below, we should check each UI function
39+
*/
40+
SELECT aqo_disable_query(id) FROM (
41+
SELECT query_hash AS id FROM aqo_queries WHERE query_hash <> 0) AS q1;
42+
SELECT learn_aqo, use_aqo, auto_tuning FROM test.aqo_queries;
43+
SELECT aqo_enable_query(id) FROM (
44+
SELECT query_hash AS id FROM aqo_queries WHERE query_hash <> 0) AS q1;
45+
SELECT learn_aqo, use_aqo, auto_tuning FROM test.aqo_queries;
46+
47+
RESET search_path;
48+
DROP TABLE test CASCADE;
3549
DROP SCHEMA IF EXISTS test CASCADE;
3650
DROP EXTENSION IF EXISTS aqo CASCADE;
37-
38-
SET search_path TO public;

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