-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathaqo--1.3--1.4.sql
executable file
·64 lines (59 loc) · 2.3 KB
/
aqo--1.3--1.4.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
/* contrib/aqo/aqo--1.3--1.4.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "ALTER EXTENSION aqo UPDATE TO '1.4'" to load this file. \quit
ALTER TABLE public.aqo_data ADD COLUMN reliability double precision [];
DROP FUNCTION public.top_error_queries(int);
--
-- 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 public.show_cardinality_errors(controlled boolean)
RETURNS TABLE(num bigint, id bigint, fshash bigint, error float, nexecs bigint)
AS $$
BEGIN
IF (controlled) THEN
RETURN QUERY
SELECT
row_number() OVER (ORDER BY (cerror, query_id, fs_hash) DESC) AS nn,
query_id, fs_hash, cerror, execs
FROM (
SELECT
aq.query_hash AS query_id,
aq.fspace_hash AS fs_hash,
cardinality_error_with_aqo[array_length(cardinality_error_with_aqo, 1)] AS cerror,
executions_with_aqo AS execs
FROM public.aqo_queries aq JOIN public.aqo_query_stat aqs
ON aq.query_hash = aqs.query_hash
WHERE TRUE = ANY (SELECT unnest(cardinality_error_with_aqo) IS NOT NULL)
) AS q1
ORDER BY nn ASC;
ELSE
RETURN QUERY
SELECT
row_number() OVER (ORDER BY (cerror, query_id, fs_hash) DESC) AS nn,
query_id, fs_hash, cerror, execs
FROM (
SELECT
aq.query_hash AS query_id,
aq.fspace_hash AS fs_hash,
array_avg(cardinality_error_without_aqo) AS cerror,
executions_without_aqo AS execs
FROM public.aqo_queries aq JOIN public.aqo_query_stat aqs
ON aq.query_hash = aqs.query_hash
WHERE TRUE = ANY (SELECT unnest(cardinality_error_without_aqo) IS NOT NULL)
) AS q1
ORDER BY (nn) ASC;
END IF;
END;
$$ LANGUAGE plpgsql;
COMMENT ON FUNCTION public.show_cardinality_errors(boolean) IS
'Get cardinality error of queries the last time they were executed. Order queries according to an error value.';