|
| 1 | +-- Check the learning-on-timeout feature |
| 2 | +-- For stabilized reproduction autovacuum must be disabled. |
| 3 | +CREATE FUNCTION check_estimated_rows(text) RETURNS TABLE (estimated int) |
| 4 | +LANGUAGE plpgsql AS $$ |
| 5 | +DECLARE |
| 6 | + ln text; |
| 7 | + tmp text[]; |
| 8 | + first_row bool := true; |
| 9 | +BEGIN |
| 10 | + FOR ln IN |
| 11 | + execute format('explain %s', $1) |
| 12 | + LOOP |
| 13 | + IF first_row THEN |
| 14 | + first_row := false; |
| 15 | + tmp := regexp_match(ln, 'rows=(\d*)'); |
| 16 | + RETURN QUERY SELECT tmp[1]::int; |
| 17 | + END IF; |
| 18 | + END LOOP; |
| 19 | +END; $$; |
| 20 | +CREATE TABLE t AS SELECT * FROM generate_series(1,100) AS x; |
| 21 | +ANALYZE t; |
| 22 | +DELETE FROM t WHERE x > 5; -- Force optimizer to make overestimated prediction. |
| 23 | +CREATE EXTENSION IF NOT EXISTS aqo; |
| 24 | +SET aqo.mode = 'learn'; |
| 25 | +SET aqo.show_details = 'off'; |
| 26 | +SET aqo.learn_statement_timeout = 'on'; |
| 27 | +SET statement_timeout = 800; -- [0.8s] |
| 28 | +SELECT *, pg_sleep(1) FROM t; |
| 29 | +NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. |
| 30 | +ERROR: canceling statement due to statement timeout |
| 31 | +SELECT check_estimated_rows('SELECT *, pg_sleep(1) FROM t;'); -- haven't any partial data |
| 32 | + check_estimated_rows |
| 33 | +---------------------- |
| 34 | + 100 |
| 35 | +(1 row) |
| 36 | + |
| 37 | +-- Don't learn because running node has smaller cardinality than an optimizer prediction |
| 38 | +SET statement_timeout = 3500; |
| 39 | +SELECT *, pg_sleep(1) FROM t; |
| 40 | +NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. |
| 41 | +ERROR: canceling statement due to statement timeout |
| 42 | +SELECT check_estimated_rows('SELECT *, pg_sleep(1) FROM t;'); |
| 43 | + check_estimated_rows |
| 44 | +---------------------- |
| 45 | + 100 |
| 46 | +(1 row) |
| 47 | + |
| 48 | +-- We have a real learning data. |
| 49 | +SET statement_timeout = 10000; |
| 50 | +SELECT *, pg_sleep(1) FROM t; |
| 51 | + x | pg_sleep |
| 52 | +---+---------- |
| 53 | + 1 | |
| 54 | + 2 | |
| 55 | + 3 | |
| 56 | + 4 | |
| 57 | + 5 | |
| 58 | +(5 rows) |
| 59 | + |
| 60 | +SELECT check_estimated_rows('SELECT *, pg_sleep(1) FROM t;'); |
| 61 | + check_estimated_rows |
| 62 | +---------------------- |
| 63 | + 5 |
| 64 | +(1 row) |
| 65 | + |
| 66 | +-- Force to make an underestimated prediction |
| 67 | +DELETE FROM t WHERE x > 2; |
| 68 | +ANALYZE t; |
| 69 | +INSERT INTO t (x) (SELECT * FROM generate_series(3,5) AS x); |
| 70 | +TRUNCATE aqo_data; |
| 71 | +SET statement_timeout = 800; |
| 72 | +SELECT *, pg_sleep(1) FROM t; -- Not learned |
| 73 | +NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. |
| 74 | +ERROR: canceling statement due to statement timeout |
| 75 | +SELECT check_estimated_rows('SELECT *, pg_sleep(1) FROM t;'); |
| 76 | + check_estimated_rows |
| 77 | +---------------------- |
| 78 | + 2 |
| 79 | +(1 row) |
| 80 | + |
| 81 | +SET statement_timeout = 3500; |
| 82 | +SELECT *, pg_sleep(1) FROM t; -- Learn! |
| 83 | +NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. |
| 84 | +ERROR: canceling statement due to statement timeout |
| 85 | +SELECT check_estimated_rows('SELECT *, pg_sleep(1) FROM t;'); |
| 86 | + check_estimated_rows |
| 87 | +---------------------- |
| 88 | + 3 |
| 89 | +(1 row) |
| 90 | + |
| 91 | +SET statement_timeout = 5500; |
| 92 | +SELECT *, pg_sleep(1) FROM t; -- Get reliable data |
| 93 | + x | pg_sleep |
| 94 | +---+---------- |
| 95 | + 1 | |
| 96 | + 2 | |
| 97 | + 3 | |
| 98 | + 4 | |
| 99 | + 5 | |
| 100 | +(5 rows) |
| 101 | + |
| 102 | +SELECT check_estimated_rows('SELECT *, pg_sleep(1) FROM t;'); |
| 103 | + check_estimated_rows |
| 104 | +---------------------- |
| 105 | + 5 |
| 106 | +(1 row) |
| 107 | + |
| 108 | +DROP TABLE t; |
| 109 | +DROP EXTENSION aqo; |
0 commit comments