-
Notifications
You must be signed in to change notification settings - Fork 54
/
aqo--1.4--1.5.sql
161 lines (143 loc) · 5.78 KB
/
aqo--1.4--1.5.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* contrib/aqo/aqo--1.4--1.5.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "ALTER EXTENSION aqo UPDATE TO '1.5'" to load this file. \quit
/* Remove old interface of the extension */
DROP FUNCTION array_mse;
DROP FUNCTION array_avg;
DROP FUNCTION public.aqo_clear_hist; -- Should be renamed and reworked
DROP FUNCTION public.aqo_disable_query;
DROP FUNCTION public.aqo_drop;
DROP FUNCTION public.aqo_enable_query;
DROP FUNCTION public.aqo_ne_queries; -- Not needed anymore due to changing in the logic
DROP FUNCTION public.aqo_status;
DROP FUNCTION public.clean_aqo_data;
DROP FUNCTION public.show_cardinality_errors;
DROP FUNCTION public.top_time_queries;
DROP TABLE public.aqo_data CASCADE;
DROP TABLE public.aqo_queries CASCADE;
DROP TABLE public.aqo_query_texts CASCADE;
DROP TABLE public.aqo_query_stat CASCADE;
DROP FUNCTION invalidate_deactivated_queries_cache;
/*
* VIEWs to discover AQO data.
*/
CREATE FUNCTION aqo_queries (
OUT queryid bigint,
OUT fs bigint,
OUT learn_aqo boolean,
OUT use_aqo boolean,
OUT auto_tuning boolean
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'aqo_queries'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
CREATE FUNCTION aqo_query_texts(OUT queryid bigint, OUT query_text text)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'aqo_query_texts'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
CREATE FUNCTION aqo_query_stat (
OUT queryid bigint,
OUT execution_time_with_aqo double precision[],
OUT execution_time_without_aqo double precision[],
OUT planning_time_with_aqo double precision[],
OUT planning_time_without_aqo double precision[],
OUT cardinality_error_with_aqo double precision[],
OUT cardinality_error_without_aqo double precision[],
OUT executions_with_aqo bigint,
OUT executions_without_aqo bigint
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'aqo_query_stat'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
CREATE FUNCTION aqo_data (
OUT fs bigint,
OUT fss integer,
OUT nfeatures integer,
OUT features double precision[][],
OUT targets double precision[],
OUT reliability double precision[],
OUT oids Oid[]
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'aqo_data'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
CREATE VIEW aqo_query_stat AS SELECT * FROM aqo_query_stat();
CREATE VIEW aqo_query_texts AS SELECT * FROM aqo_query_texts();
CREATE VIEW aqo_data AS SELECT * FROM aqo_data();
CREATE VIEW aqo_queries AS SELECT * FROM aqo_queries();
/* UI functions */
CREATE FUNCTION aqo_enable_query(queryid bigint)
RETURNS void
AS 'MODULE_PATHNAME', 'aqo_enable_query'
LANGUAGE C STRICT VOLATILE;
CREATE FUNCTION aqo_disable_query(queryid bigint)
RETURNS void
AS 'MODULE_PATHNAME', 'aqo_enable_query'
LANGUAGE C STRICT VOLATILE;
CREATE FUNCTION aqo_queries_update(
queryid bigint, fs bigint, learn_aqo bool, use_aqo bool, auto_tuning bool)
RETURNS bool
AS 'MODULE_PATHNAME', 'aqo_queries_update'
LANGUAGE C VOLATILE;
--
-- Get cardinality error of queries the last time they were executed.
-- IN:
-- controlled - show queries executed under a control of AQO (true);
-- executed without an AQO control, but AQO has a stat on the query (false).
--
-- OUT:
-- num - sequental number. Smaller number corresponds to higher error.
-- id - ID of a query.
-- fshash - feature space. Usually equal to zero or ID.
-- error - AQO error that calculated on plan nodes of the query.
-- nexecs - number of executions of queries associated with this ID.
--
CREATE OR REPLACE FUNCTION aqo_cardinality_error(controlled boolean)
RETURNS TABLE(num integer, id bigint, fshash bigint, error double precision, nexecs bigint)
AS 'MODULE_PATHNAME', 'aqo_cardinality_error'
LANGUAGE C STRICT VOLATILE;
COMMENT ON FUNCTION aqo_cardinality_error(boolean) IS
'Get cardinality error of queries the last time they were executed. Order queries according to an error value.';
--
-- Show execution time of queries, for which AQO has statistics.
-- controlled - show stat on executions where AQO was used for cardinality
-- estimations, or not used (controlled = false).
-- Last case is possible in disabled mode with aqo.force_collect_stat = 'on'.
--
CREATE OR REPLACE FUNCTION aqo_execution_time(controlled boolean)
RETURNS TABLE(num integer, id bigint, fshash bigint, exec_time double precision, nexecs bigint)
AS 'MODULE_PATHNAME', 'aqo_execution_time'
LANGUAGE C STRICT VOLATILE;
COMMENT ON FUNCTION aqo_execution_time(boolean) IS
'Get execution time of queries. If controlled = true (AQO could advise cardinality estimations), show time of last execution attempt. Another case (AQO not used), return an average value of execution time across all known executions.';
--
-- Remove query class settings, text, statistics and ML data from AQO storage.
-- Return number of FSS records, removed from the storage.
--
CREATE OR REPLACE FUNCTION aqo_drop_class(queryid bigint)
RETURNS integer
AS 'MODULE_PATHNAME', 'aqo_drop_class'
LANGUAGE C STRICT VOLATILE;
COMMENT ON FUNCTION aqo_drop_class(bigint) IS
'Remove info about an query class from AQO ML knowledge base.';
--
-- Remove unneeded rows from the AQO ML storage.
-- For common feature space, remove rows from aqo_data only.
-- For custom feature space - remove all rows related to the space from all AQO
-- tables even if only one oid for one feature subspace of the space is illegal.
-- Returns number of deleted rows from aqo_queries and aqo_data tables.
--
CREATE OR REPLACE FUNCTION aqo_cleanup()
RETURNS TABLE(nfs integer, nfss integer) AS 'MODULE_PATHNAME', 'aqo_cleanup'
LANGUAGE C STRICT VOLATILE;
COMMENT ON FUNCTION aqo_cleanup() IS
'Remove unneeded rows from the AQO ML storage';
--
-- Remove all records in the AQO storage.
-- Return number of rows removed.
--
CREATE FUNCTION aqo_reset() RETURNS bigint
AS 'MODULE_PATHNAME', 'aqo_reset'
LANGUAGE C PARALLEL SAFE;
COMMENT ON FUNCTION aqo_reset() IS
'Reset all data gathered by AQO';