Skip to content

Commit 2285d86

Browse files
committed
Return into the code the feature "QueryId based on jumbling machinery".
1 parent 320197e commit 2285d86

File tree

10 files changed

+131
-91
lines changed

10 files changed

+131
-91
lines changed

Diff for: aqo.c

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ _PG_init(void)
163163
errmsg("AQO module could be loaded only on startup."),
164164
errdetail("Add 'aqo' into the shared_preload_libraries list.")));
165165

166+
/*
167+
* Inform the postmaster that we want to enable query_id calculation if
168+
* compute_query_id is set to auto.
169+
*/
170+
EnableQueryId();
171+
166172
DefineCustomEnumVariable("aqo.mode",
167173
"Mode of aqo usage.",
168174
NULL,

Diff for: expected/aqo_fdw.out

+30-14
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,11 @@ SELECT x FROM frgn;
5454
(5 rows)
5555

5656
-- Push down base filters. Use verbose mode to see filters.
57-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE))
58-
SELECT x FROM frgn WHERE x < 10;
59-
ERROR: syntax error at or near ")"
60-
LINE 1: ...LAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE))
61-
^
62-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
63-
SELECT x FROM frgn WHERE x < 10;
64-
QUERY PLAN
57+
SELECT str FROM expln('
58+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
59+
SELECT x FROM frgn WHERE x < 10;
60+
') AS str WHERE str NOT LIKE '%Query Identifier%';
61+
str
6562
-----------------------------------------------------------
6663
Foreign Scan on public.frgn (actual rows=1 loops=1)
6764
AQO not used
@@ -72,6 +69,21 @@ SELECT x FROM frgn WHERE x < 10;
7269
JOINS: 0
7370
(7 rows)
7471

72+
SELECT str FROM expln('
73+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
74+
SELECT x FROM frgn WHERE x < 10;
75+
') AS str WHERE str NOT LIKE '%Query Identifier%';
76+
str
77+
-----------------------------------------------------------
78+
Foreign Scan on public.frgn (actual rows=1 loops=1)
79+
AQO: rows=1, error=0%
80+
Output: x
81+
Remote SQL: SELECT x FROM public.local WHERE ((x < 10))
82+
Using aqo: true
83+
AQO mode: LEARN
84+
JOINS: 0
85+
(7 rows)
86+
7587
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
7688
SELECT x FROM frgn WHERE x < -10; -- AQO ignores constants
7789
QUERY PLAN
@@ -99,9 +111,11 @@ SELECT str FROM expln('
99111
(6 rows)
100112

101113
-- TODO: Should learn on postgres_fdw nodes
102-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
103-
SELECT * FROM frgn AS a, frgn AS b WHERE a.x=b.x;
104-
QUERY PLAN
114+
SELECT str FROM expln('
115+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
116+
SELECT * FROM frgn AS a, frgn AS b WHERE a.x=b.x;
117+
') AS str WHERE str NOT LIKE '%Query Identifier%';
118+
str
105119
--------------------------------------------------------------------------------------------------------
106120
Foreign Scan (actual rows=1 loops=1)
107121
AQO not used
@@ -126,9 +140,11 @@ SELECT * FROM frgn AS a, frgn AS b WHERE a.x<b.x;
126140
JOINS: 0
127141
(6 rows)
128142

129-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
130-
SELECT * FROM frgn AS a, frgn AS b WHERE a.x<b.x;
131-
QUERY PLAN
143+
SELECT str FROM expln('
144+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
145+
SELECT * FROM frgn AS a, frgn AS b WHERE a.x<b.x;
146+
') AS str WHERE str NOT LIKE '%Query Identifier%';
147+
str
132148
--------------------------------------------------------------------------------------------------------
133149
Foreign Scan (actual rows=0 loops=1)
134150
AQO not used

Diff for: expected/gucs.out

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
CREATE EXTENSION aqo;
2+
-- Utility tool. Allow to filter system-dependent strings from an explain output.
3+
CREATE OR REPLACE FUNCTION expln(query_string text) RETURNS SETOF text AS $$
4+
BEGIN
5+
RETURN QUERY
6+
EXECUTE format('%s', query_string);
7+
RETURN;
8+
END;
9+
$$ LANGUAGE PLPGSQL;
210
SET aqo.join_threshold = 0;
311
SET aqo.mode = 'learn';
412
SET aqo.show_details = true;
@@ -12,9 +20,11 @@ SELECT true FROM aqo_reset(); -- Remember! DROP EXTENSION doesn't remove any AQO
1220
(1 row)
1321

1422
-- Check AQO addons to explain (the only stable data)
15-
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
16-
SELECT x FROM t;
17-
QUERY PLAN
23+
SELECT str FROM expln('
24+
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
25+
SELECT x FROM t;
26+
') AS str WHERE str NOT LIKE '%Query Identifier%';
27+
str
1828
------------------------------------------------
1929
Seq Scan on public.t (actual rows=100 loops=1)
2030
AQO not used
@@ -24,9 +34,11 @@ EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
2434
JOINS: 0
2535
(6 rows)
2636

27-
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
28-
SELECT x FROM t;
29-
QUERY PLAN
37+
SELECT str FROM expln('
38+
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
39+
SELECT x FROM t;
40+
') AS str WHERE str NOT LIKE '%Query Identifier%';
41+
str
3042
------------------------------------------------
3143
Seq Scan on public.t (actual rows=100 loops=1)
3244
AQO: rows=100, error=0%

Diff for: expected/unsupported.out

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
CREATE EXTENSION aqo;
2+
-- Utility tool. Allow to filter system-dependent strings from an explain output.
3+
CREATE OR REPLACE FUNCTION expln(query_string text) RETURNS SETOF text AS $$
4+
BEGIN
5+
RETURN QUERY
6+
EXECUTE format('%s', query_string);
7+
RETURN;
8+
END;
9+
$$ LANGUAGE PLPGSQL;
210
SET aqo.join_threshold = 0;
311
SET aqo.mode = 'learn';
412
SET aqo.show_details = 'on';
@@ -35,8 +43,8 @@ EXPLAIN (COSTS OFF)
3543
(11 rows)
3644

3745
SELECT str FROM expln('
38-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
39-
SELECT * FROM t GROUP BY (x) HAVING x > 3;
46+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
47+
SELECT * FROM t GROUP BY (x) HAVING x > 3;
4048
') AS str WHERE str NOT LIKE '%Memory Usage%';
4149
str
4250
-----------------------------------------------
@@ -485,17 +493,6 @@ SELECT * FROM
485493
-- any prediction on number of fetched tuples.
486494
-- So, if selectivity was wrong we could make bad choice of Scan operation.
487495
-- For example, we could choose suboptimal index.
488-
--
489-
-- Returns string-by-string explain of a query. Made for removing some strings
490-
-- from the explain output.
491-
--
492-
CREATE OR REPLACE FUNCTION expln(query_string text) RETURNS SETOF text AS $$
493-
BEGIN
494-
RETURN QUERY
495-
EXECUTE format('EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF) %s', query_string);
496-
RETURN;
497-
END;
498-
$$ LANGUAGE PLPGSQL;
499496
-- Turn off statistics gathering for simple demonstration of filtering problem.
500497
ALTER TABLE t SET (autovacuum_enabled = 'false');
501498
CREATE INDEX ind1 ON t(x);
@@ -531,10 +528,11 @@ SELECT count(*) FROM t WHERE x < 3 AND mod(x,3) = 1;
531528
50
532529
(1 row)
533530

534-
SELECT str AS result
535-
FROM expln('SELECT count(*) FROM t WHERE x < 3 AND mod(x,3) = 1') AS str
536-
WHERE str NOT LIKE '%Heap Blocks%';
537-
result
531+
SELECT str FROM expln('
532+
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, SUMMARY OFF, TIMING OFF)
533+
SELECT count(*) FROM t WHERE x < 3 AND mod(x,3) = 1') AS str
534+
WHERE str NOT LIKE '%Heap Blocks%' AND str NOT LIKE '%Query Identifier%';
535+
str
538536
-----------------------------------------------------------------
539537
Aggregate (actual rows=1 loops=1)
540538
AQO not used
@@ -590,6 +588,10 @@ ORDER BY (md5(query_text),error) DESC;
590588
| SELECT count(*) FROM t WHERE +
591589
| x = (SELECT avg(x) FROM t t0 WHERE t0.x = t.x + 21) OR +
592590
| x IN (SELECT avg(x) FROM t t0 WHERE t0.x = t.x + 21);
591+
0.106 | +
592+
| EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) +
593+
| SELECT * FROM t GROUP BY (x) HAVING x > 3; +
594+
|
593595
0.454 | SELECT count(*) FROM (SELECT x, y FROM t1 GROUP BY GROUPING SETS ((x,y), (x), (y), ())) AS q1;
594596
0.000 | SELECT count(*) FROM ( +
595597
| SELECT count(*) AS x FROM ( +
@@ -602,10 +604,6 @@ ORDER BY (md5(query_text),error) DESC;
602604
0.000 | EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) +
603605
| SELECT count(*) FROM t WHERE (SELECT avg(x) FROM t t0 WHERE t0.x = t.x) = +
604606
| (SELECT avg(x) FROM t t0 WHERE t0.x = t.x);
605-
0.106 | +
606-
| EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) +
607-
| SELECT * FROM t GROUP BY (x) HAVING x > 3; +
608-
|
609607
0.000 | SELECT count(*) FROM +
610608
| (SELECT * FROM t WHERE x % 3 < (SELECT avg(x) FROM t t0 WHERE t0.x = t.x)) AS q1 +
611609
| JOIN +

Diff for: hash.c

-18
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,6 @@ static bool has_consts(List *lst);
5656
static List **get_clause_args_ptr(Expr *clause);
5757
static bool clause_is_eq_clause(Expr *clause);
5858

59-
/*
60-
* Computes hash for given query.Query Identifier: =
61-
* Hash is supposed to be constant-insensitive.
62-
* XXX: Hashing depend on Oids of database objects. It is restrict usability of
63-
* the AQO knowledge base by current database at current Postgres instance.
64-
*/
65-
uint64
66-
get_query_hash(Query *parse, const char *query_text)
67-
{
68-
char *str_repr;
69-
uint64 hash;
70-
71-
/* XXX: remove_locations and remove_consts are heavy routines. */
72-
str_repr = remove_locations(remove_consts(nodeToString(parse)));
73-
hash = DatumGetUInt64(hash_any_extended((void *) str_repr, strlen(str_repr),0));
74-
75-
return hash;
76-
}
7759

7860
/*********************************************************************************
7961
*

Diff for: hash.h

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "nodes/pg_list.h"
55

6-
extern uint64 get_query_hash(Query *parse, const char *query_text);
76
extern bool list_member_uint64(const List *list, uint64 datum);
87
extern List *list_copy_uint64(List *list);
98
extern List *lappend_uint64(List *list, uint64 datum);

Diff for: preprocessing.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,13 @@ aqo_planner(Query *parse,
198198
MemoryContextSwitchTo(oldctx);
199199

200200
oldctx = MemoryContextSwitchTo(AQOUtilityMemCtx);
201-
query_context.query_hash = get_query_hash(parse, query_string);
201+
/* Check unlucky case (get a hash of zero) */
202+
if (parse->queryId == UINT64CONST(0))
203+
JumbleQuery(parse, query_string);
204+
205+
Assert(parse->utilityStmt == NULL);
206+
Assert(parse->queryId != UINT64CONST(0));
207+
query_context.query_hash = parse->queryId;
202208
MemoryContextSwitchTo(oldctx);
203209

204210
MemoryContextReset(AQOUtilityMemCtx);

Diff for: sql/aqo_fdw.sql

+17-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
CREATE EXTENSION aqo;
88
CREATE EXTENSION postgres_fdw;
9+
910
SET aqo.mode = 'learn';
1011
SET aqo.show_details = 'true'; -- show AQO info for each node and entire query.
1112
SET aqo.show_hash = 'false'; -- a hash value is system-depended. Ignore it.
@@ -43,10 +44,14 @@ EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
4344
SELECT x FROM frgn;
4445

4546
-- Push down base filters. Use verbose mode to see filters.
46-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE))
47-
SELECT x FROM frgn WHERE x < 10;
48-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
49-
SELECT x FROM frgn WHERE x < 10;
47+
SELECT str FROM expln('
48+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
49+
SELECT x FROM frgn WHERE x < 10;
50+
') AS str WHERE str NOT LIKE '%Query Identifier%';
51+
SELECT str FROM expln('
52+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
53+
SELECT x FROM frgn WHERE x < 10;
54+
') AS str WHERE str NOT LIKE '%Query Identifier%';
5055
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
5156
SELECT x FROM frgn WHERE x < -10; -- AQO ignores constants
5257

@@ -57,14 +62,18 @@ SELECT str FROM expln('
5762
') AS str WHERE str NOT LIKE '%Sort Method%';
5863

5964
-- TODO: Should learn on postgres_fdw nodes
60-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
61-
SELECT * FROM frgn AS a, frgn AS b WHERE a.x=b.x;
65+
SELECT str FROM expln('
66+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
67+
SELECT * FROM frgn AS a, frgn AS b WHERE a.x=b.x;
68+
') AS str WHERE str NOT LIKE '%Query Identifier%';
6269

6370
-- TODO: Non-mergejoinable join condition.
6471
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
6572
SELECT * FROM frgn AS a, frgn AS b WHERE a.x<b.x;
66-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
67-
SELECT * FROM frgn AS a, frgn AS b WHERE a.x<b.x;
73+
SELECT str FROM expln('
74+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
75+
SELECT * FROM frgn AS a, frgn AS b WHERE a.x<b.x;
76+
') AS str WHERE str NOT LIKE '%Query Identifier%';
6877

6978
DROP EXTENSION aqo CASCADE;
7079
DROP EXTENSION postgres_fdw CASCADE;

Diff for: sql/gucs.sql

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CREATE EXTENSION aqo;
2-
SET aqo.join_threshold = 0;
32

3+
-- Utility tool. Allow to filter system-dependent strings from an explain output.
4+
CREATE OR REPLACE FUNCTION expln(query_string text) RETURNS SETOF text AS $$
5+
BEGIN
6+
RETURN QUERY
7+
EXECUTE format('%s', query_string);
8+
RETURN;
9+
END;
10+
$$ LANGUAGE PLPGSQL;
11+
12+
SET aqo.join_threshold = 0;
413
SET aqo.mode = 'learn';
514
SET aqo.show_details = true;
615

@@ -10,10 +19,14 @@ ANALYZE t;
1019

1120
SELECT true FROM aqo_reset(); -- Remember! DROP EXTENSION doesn't remove any AQO data gathered.
1221
-- Check AQO addons to explain (the only stable data)
13-
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
14-
SELECT x FROM t;
15-
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
16-
SELECT x FROM t;
22+
SELECT str FROM expln('
23+
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
24+
SELECT x FROM t;
25+
') AS str WHERE str NOT LIKE '%Query Identifier%';
26+
SELECT str FROM expln('
27+
EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF)
28+
SELECT x FROM t;
29+
') AS str WHERE str NOT LIKE '%Query Identifier%';
1730
SET aqo.mode = 'disabled';
1831

1932
-- Check existence of the interface functions.

0 commit comments

Comments
 (0)