|
| 1 | +-- Specifically test AQO machinery for queries uses partial paths and executed |
| 2 | +-- with parallel workers. |
| 3 | +CREATE EXTENSION aqo; |
| 4 | +-- Utility tool. Allow to filter system-dependent strings from explain output. |
| 5 | +CREATE OR REPLACE FUNCTION expln(query_string text) RETURNS SETOF text AS $$ |
| 6 | +BEGIN |
| 7 | + RETURN QUERY |
| 8 | + EXECUTE format('%s', query_string); |
| 9 | + RETURN; |
| 10 | +END; |
| 11 | +$$ LANGUAGE PLPGSQL; |
| 12 | +SET aqo.join_threshold = 0; |
| 13 | +SET aqo.mode = 'learn'; |
| 14 | +SET aqo.show_details = true; |
| 15 | +-- Be generous with a number parallel workers to test the machinery |
| 16 | +SET max_parallel_workers = 64; |
| 17 | +SET max_parallel_workers_per_gather = 64; |
| 18 | +-- Enforce usage of parallel workers |
| 19 | +SET parallel_setup_cost = 0.1; |
| 20 | +SET parallel_tuple_cost = 0.0001; |
| 21 | +CREATE TABLE t AS ( |
| 22 | + SELECT x AS id, repeat('a', 512) AS payload FROM generate_series(1, 1E5) AS x |
| 23 | +); |
| 24 | +ANALYZE t; |
| 25 | +-- Simple test. Check serialization machinery mostly. |
| 26 | +SELECT count(*) FROM t WHERE id % 100 = 0; -- Learning stage |
| 27 | + count |
| 28 | +------- |
| 29 | + 1000 |
| 30 | +(1 row) |
| 31 | + |
| 32 | +SELECT str FROM expln(' |
| 33 | + EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) |
| 34 | + SELECT count(*) FROM t WHERE id % 100 = 0;') AS str |
| 35 | +WHERE str NOT LIKE '%Worker%'; |
| 36 | + str |
| 37 | +-------------------------------------------------------------------- |
| 38 | + Finalize Aggregate (actual rows=1 loops=1) |
| 39 | + AQO not used |
| 40 | + -> Gather (actual rows=3 loops=1) |
| 41 | + AQO not used |
| 42 | + -> Partial Aggregate (actual rows=1 loops=3) |
| 43 | + AQO not used |
| 44 | + -> Parallel Seq Scan on t (actual rows=333 loops=3) |
| 45 | + AQO: rows=1000, error=0% |
| 46 | + Filter: ((id % '100'::numeric) = '0'::numeric) |
| 47 | + Rows Removed by Filter: 33000 |
| 48 | + Using aqo: true |
| 49 | + AQO mode: LEARN |
| 50 | + JOINS: 0 |
| 51 | +(13 rows) |
| 52 | + |
| 53 | +-- More complex query just to provoke errors |
| 54 | +SELECT count(*) FROM |
| 55 | + (SELECT id FROM t WHERE id % 100 = 0 GROUP BY (id)) AS q1, |
| 56 | + (SELECT max(id) AS id, payload FROM t |
| 57 | + WHERE id % 101 = 0 GROUP BY (payload)) AS q2 |
| 58 | +WHERE q1.id = q2.id; -- Learning stage |
| 59 | + count |
| 60 | +------- |
| 61 | + 0 |
| 62 | +(1 row) |
| 63 | + |
| 64 | +-- XXX: Why grouping prediction isn't working here? |
| 65 | +SELECT str FROM expln(' |
| 66 | +EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) |
| 67 | +SELECT count(*) FROM |
| 68 | + (SELECT id FROM t WHERE id % 100 = 0 GROUP BY (id)) AS q1, |
| 69 | + (SELECT max(id) AS id, payload FROM t |
| 70 | + WHERE id % 101 = 0 GROUP BY (payload)) AS q2 |
| 71 | +WHERE q1.id = q2.id;') AS str |
| 72 | +WHERE str NOT LIKE '%Workers%' AND str NOT LIKE '%Sort Method%'; |
| 73 | + str |
| 74 | +-------------------------------------------------------------------------------------------------- |
| 75 | + Aggregate (actual rows=1 loops=1) |
| 76 | + AQO not used |
| 77 | + -> Merge Join (actual rows=0 loops=1) |
| 78 | + AQO not used |
| 79 | + Merge Cond: (q2.id = t_1.id) |
| 80 | + -> Sort (actual rows=1 loops=1) |
| 81 | + Sort Key: q2.id |
| 82 | + -> Subquery Scan on q2 (actual rows=1 loops=1) |
| 83 | + AQO not used |
| 84 | + -> Finalize GroupAggregate (actual rows=1 loops=1) |
| 85 | + AQO not used |
| 86 | + Group Key: t.payload |
| 87 | + -> Gather Merge (actual rows=3 loops=1) |
| 88 | + AQO not used |
| 89 | + -> Partial GroupAggregate (actual rows=1 loops=3) |
| 90 | + AQO not used |
| 91 | + Group Key: t.payload |
| 92 | + -> Sort (actual rows=330 loops=3) |
| 93 | + AQO not used |
| 94 | + Sort Key: t.payload |
| 95 | + -> Parallel Seq Scan on t (actual rows=330 loops=3) |
| 96 | + AQO: rows=991, error=0% |
| 97 | + Filter: ((id % '101'::numeric) = '0'::numeric) |
| 98 | + Rows Removed by Filter: 33003 |
| 99 | + -> Group (actual rows=1000 loops=1) |
| 100 | + AQO not used |
| 101 | + Group Key: t_1.id |
| 102 | + -> Gather Merge (actual rows=1000 loops=1) |
| 103 | + AQO not used |
| 104 | + -> Group (actual rows=333 loops=3) |
| 105 | + AQO not used |
| 106 | + Group Key: t_1.id |
| 107 | + -> Sort (actual rows=333 loops=3) |
| 108 | + AQO not used |
| 109 | + Sort Key: t_1.id |
| 110 | + -> Parallel Seq Scan on t t_1 (actual rows=333 loops=3) |
| 111 | + AQO: rows=991, error=-1% |
| 112 | + Filter: ((id % '100'::numeric) = '0'::numeric) |
| 113 | + Rows Removed by Filter: 33000 |
| 114 | + Using aqo: true |
| 115 | + AQO mode: LEARN |
| 116 | + JOINS: 1 |
| 117 | +(42 rows) |
| 118 | + |
| 119 | +RESET parallel_tuple_cost; |
| 120 | +RESET parallel_setup_cost; |
| 121 | +RESET max_parallel_workers; |
| 122 | +RESET max_parallel_workers_per_gather; |
| 123 | +DROP TABLE t; |
| 124 | +DROP FUNCTION expln; |
| 125 | +DROP EXTENSION aqo; |
0 commit comments