-
Notifications
You must be signed in to change notification settings - Fork 55
Add smart statement timeout for learning AQO. #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
DROP TABLE IF EXISTS a,b CASCADE; | ||
NOTICE: table "a" does not exist, skipping | ||
NOTICE: table "b" does not exist, skipping | ||
CREATE TABLE a (x1 int, x2 int, x3 int); | ||
INSERT INTO a (x1, x2, x3) SELECT mod(ival,4), mod(ival,10), mod(ival,10) FROM generate_series(1,100) As ival; | ||
CREATE TABLE b (y1 int, y2 int, y3 int); | ||
INSERT INTO b (y1, y2, y3) SELECT mod(ival + 1,4), mod(ival + 1,10), mod(ival + 1,10) FROM generate_series(1,100) As ival; | ||
CREATE EXTENSION IF NOT EXISTS aqo; | ||
SET aqo.join_threshold = 0; | ||
SET aqo.mode = 'learn'; | ||
SET aqo.show_details = 'off'; | ||
SET aqo.learn_statement_timeout = 'on'; | ||
SET statement_timeout = 1500; -- [1.5s] | ||
SET aqo.statement_timeout = 500; -- [0.5s] | ||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. Timeout is 0 | ||
NOTICE: [AQO] Time limit for execution of the statement was increased. Current timeout is 1 | ||
count | count | ||
-------+------- | ||
62500 | 62500 | ||
(1 row) | ||
|
||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
smart_timeout | count_increase_timeout | ||
---------------+------------------------ | ||
1 | 1 | ||
(1 row) | ||
|
||
SET aqo.learn_statement_timeout = 'off'; | ||
SET aqo.statement_timeout = 1000; -- [1s] | ||
INSERT INTO a (x1, x2, x3) SELECT mod(ival,20), mod(ival,10), mod(ival,10) FROM generate_series(1,1000) As ival; | ||
SET aqo.learn_statement_timeout = 'on'; | ||
SET aqo.statement_timeout = 500; -- [0.5s] | ||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. Timeout is 1 | ||
NOTICE: [AQO] Time limit for execution of the statement was increased. Current timeout is 6 | ||
count | count | ||
--------+-------- | ||
563300 | 562500 | ||
(1 row) | ||
|
||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
smart_timeout | count_increase_timeout | ||
---------------+------------------------ | ||
6 | 2 | ||
(1 row) | ||
|
||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. Timeout is 6 | ||
NOTICE: [AQO] Time limit for execution of the statement was increased. Current timeout is 63 | ||
count | count | ||
--------+-------- | ||
563300 | 562500 | ||
(1 row) | ||
|
||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
smart_timeout | count_increase_timeout | ||
---------------+------------------------ | ||
63 | 3 | ||
(1 row) | ||
|
||
SET statement_timeout = 100; -- [0.1s] | ||
SET aqo.statement_timeout = 150; | ||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. Timeout is 63 | ||
ERROR: canceling statement due to statement timeout | ||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
smart_timeout | count_increase_timeout | ||
---------------+------------------------ | ||
63 | 3 | ||
(1 row) | ||
|
||
SELECT 1 FROM aqo_reset(); | ||
?column? | ||
---------- | ||
1 | ||
(1 row) | ||
|
||
DROP TABLE a; | ||
DROP TABLE b; | ||
DROP EXTENSION aqo; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ typedef struct | |
|
||
static double cardinality_sum_errors; | ||
static int cardinality_num_objects; | ||
static int64 max_timeout_value; | ||
static int64 growth_rate = 3; | ||
|
||
/* | ||
* Store an AQO-related query data into the Query Environment structure. | ||
|
@@ -625,15 +627,46 @@ aqo_timeout_handler(void) | |
ctx.learn = query_context.learn_aqo; | ||
ctx.isTimedOut = true; | ||
|
||
elog(NOTICE, "[AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data."); | ||
if (aqo_statement_timeout == 0) | ||
elog(NOTICE, "[AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data."); | ||
else | ||
elog(NOTICE, "[AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data. Timeout is %ld", max_timeout_value); | ||
|
||
learnOnPlanState(timeoutCtl.queryDesc->planstate, (void *) &ctx); | ||
MemoryContextSwitchTo(oldctx); | ||
} | ||
|
||
/* | ||
* Function for updating smart statement timeout | ||
*/ | ||
static int64 | ||
increase_smart_timeout() | ||
{ | ||
int64 smart_timeout_fin_time = (query_context.smart_timeout + 1) * pow(growth_rate, query_context.count_increase_timeout); | ||
|
||
if (query_context.smart_timeout == max_timeout_value && !update_query_timeout(query_context.query_hash, smart_timeout_fin_time)) | ||
elog(NOTICE, "[AQO] Timeout is not updated!"); | ||
|
||
return smart_timeout_fin_time; | ||
} | ||
|
||
static bool | ||
set_timeout_if_need(QueryDesc *queryDesc) | ||
{ | ||
TimestampTz fin_time; | ||
int64 fintime = (int64) get_timeout_finish_time(STATEMENT_TIMEOUT)-1; | ||
|
||
if (aqo_learn_statement_timeout && aqo_statement_timeout > 0) | ||
{ | ||
max_timeout_value = Min(query_context.smart_timeout, (int64) aqo_statement_timeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему эти два параметра разного типа? В чём смысл? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Имеют разный смысл. Описала ниже подробней |
||
if (max_timeout_value > fintime) | ||
{ | ||
max_timeout_value = fintime; | ||
} | ||
} | ||
else | ||
{ | ||
max_timeout_value = fintime; | ||
} | ||
|
||
if (IsParallelWorker()) | ||
/* | ||
|
@@ -663,8 +696,7 @@ set_timeout_if_need(QueryDesc *queryDesc) | |
else | ||
Assert(!get_timeout_active(timeoutCtl.id)); | ||
|
||
fin_time = get_timeout_finish_time(STATEMENT_TIMEOUT); | ||
enable_timeout_at(timeoutCtl.id, fin_time - 1); | ||
enable_timeout_at(timeoutCtl.id, (TimestampTz) max_timeout_value); | ||
|
||
/* Save pointer to queryDesc to use at learning after a timeout interruption. */ | ||
timeoutCtl.queryDesc = queryDesc; | ||
|
@@ -720,6 +752,7 @@ aqo_ExecutorEnd(QueryDesc *queryDesc) | |
instr_time endtime; | ||
EphemeralNamedRelation enr = get_ENR(queryDesc->queryEnv, PlanStateInfo); | ||
MemoryContext oldctx = MemoryContextSwitchTo(AQOLearnMemCtx); | ||
double error = .0; | ||
|
||
cardinality_sum_errors = 0.; | ||
cardinality_num_objects = 0; | ||
|
@@ -778,6 +811,16 @@ aqo_ExecutorEnd(QueryDesc *queryDesc) | |
/* Store all learn data into the AQO service relations. */ | ||
if (!query_context.adding_query && query_context.auto_tuning) | ||
automatical_query_tuning(query_context.query_hash, stat); | ||
|
||
error = stat->est_error_aqo[stat->cur_stat_slot_aqo-1] - cardinality_sum_errors/(1 + cardinality_num_objects); | ||
|
||
if ( aqo_learn_statement_timeout && aqo_statement_timeout > 0 && error >= 0.1) | ||
{ | ||
int64 fintime = increase_smart_timeout(); | ||
elog(NOTICE, "[AQO] Time limit for execution of the statement was increased. Current timeout is %ld", fintime); | ||
} | ||
|
||
pfree(stat); | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
DROP TABLE IF EXISTS a,b CASCADE; | ||
CREATE TABLE a (x1 int, x2 int, x3 int); | ||
INSERT INTO a (x1, x2, x3) SELECT mod(ival,4), mod(ival,10), mod(ival,10) FROM generate_series(1,100) As ival; | ||
|
||
CREATE TABLE b (y1 int, y2 int, y3 int); | ||
INSERT INTO b (y1, y2, y3) SELECT mod(ival + 1,4), mod(ival + 1,10), mod(ival + 1,10) FROM generate_series(1,100) As ival; | ||
|
||
CREATE EXTENSION IF NOT EXISTS aqo; | ||
SET aqo.join_threshold = 0; | ||
SET aqo.mode = 'learn'; | ||
SET aqo.show_details = 'off'; | ||
SET aqo.learn_statement_timeout = 'on'; | ||
SET statement_timeout = 1500; -- [1.5s] | ||
SET aqo.statement_timeout = 500; -- [0.5s] | ||
|
||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
|
||
SET aqo.learn_statement_timeout = 'off'; | ||
SET aqo.statement_timeout = 1000; -- [1s] | ||
INSERT INTO a (x1, x2, x3) SELECT mod(ival,20), mod(ival,10), mod(ival,10) FROM generate_series(1,1000) As ival; | ||
SET aqo.learn_statement_timeout = 'on'; | ||
SET aqo.statement_timeout = 500; -- [0.5s] | ||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
|
||
SET statement_timeout = 100; -- [0.1s] | ||
SET aqo.statement_timeout = 150; | ||
SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1; | ||
select smart_timeout, count_increase_timeout from aqo_queries, aqo_query_texts | ||
where query_text = 'SELECT count(a.x1),count(B.y1) FROM A a LEFT JOIN B ON a.x1 = B.y1 LEFT JOIN A a1 ON a1.x1 = B.y1;' | ||
and aqo_query_texts.queryid = aqo_queries.queryid limit 1; | ||
|
||
SELECT 1 FROM aqo_reset(); | ||
DROP TABLE a; | ||
DROP TABLE b; | ||
DROP EXTENSION aqo; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А не являются ли два гука избыточными?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Предполагалось, изначально, при разработаке фичи, что aqo_statement_timeout устанавливает предел времени выполнения запроса для того, чтобы успеть сохранить данные обучения в таблицу знаний.
aqo_learn_statement_timeout - немного другая на мой взгляд функциональность, так как она включает возможность увеличивать statement_timeout для каждого отдельного запроса, либо пока aqo не прекратит ошибаться, либо пока не достигнем установленного лимита (aqo_statement_timeout или ванильный statement_timeout)