Skip to content

Commit f036974

Browse files
feat(frontend): report slow query in the log (#6624)
* feat(frontend): report slow query in the log Signed-off-by: Bugen Zhao <[email protected]> * enable only if debug assertions Signed-off-by: Bugen Zhao <[email protected]> * remove trailing whitespace Signed-off-by: Bugen Zhao <[email protected]> * use timeout Signed-off-by: Bugen Zhao <[email protected]> * try to fix madsim compiling Signed-off-by: Bugen Zhao <[email protected]> Signed-off-by: Bugen Zhao <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent f1ad0e9 commit f036974

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/frontend/src/session.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,8 @@ impl Session<PgResponseStream> for SessionImpl {
765765
format: bool,
766766
) -> std::result::Result<PgResponse<PgResponseStream>, BoxedError> {
767767
// Parse sql.
768-
let mut stmts = Parser::parse_sql(sql).map_err(|e| {
769-
tracing::error!("failed to parse sql:\n{}:\n{}", sql, e);
770-
e
771-
})?;
768+
let mut stmts = Parser::parse_sql(sql)
769+
.inspect_err(|e| tracing::error!("failed to parse sql:\n{}:\n{}", sql, e))?;
772770
if stmts.is_empty() {
773771
return Ok(PgResponse::empty_result(
774772
pgwire::pg_response::StatementType::EMPTY,
@@ -781,10 +779,25 @@ impl Session<PgResponseStream> for SessionImpl {
781779
));
782780
}
783781
let stmt = stmts.swap_remove(0);
784-
let rsp = handle(self, stmt, sql, format).await.map_err(|e| {
785-
tracing::error!("failed to handle sql:\n{}:\n{}", sql, e);
786-
e
787-
})?;
782+
let rsp = {
783+
let mut handle_fut = Box::pin(handle(self, stmt, sql, format));
784+
if cfg!(debug_assertions) {
785+
// Report the SQL in the log periodically if the query is slow.
786+
const SLOW_QUERY_LOG_PERIOD: Duration = Duration::from_secs(60);
787+
loop {
788+
match tokio::time::timeout(SLOW_QUERY_LOG_PERIOD, &mut handle_fut).await {
789+
Ok(result) => break result,
790+
Err(_) => tracing::warn!(
791+
sql,
792+
"slow query has been running for another {SLOW_QUERY_LOG_PERIOD:?}"
793+
),
794+
}
795+
}
796+
} else {
797+
handle_fut.await
798+
}
799+
}
800+
.inspect_err(|e| tracing::error!("failed to handle sql:\n{}:\n{}", sql, e))?;
788801
Ok(rsp)
789802
}
790803

@@ -793,10 +806,8 @@ impl Session<PgResponseStream> for SessionImpl {
793806
sql: &str,
794807
) -> std::result::Result<Vec<PgFieldDescriptor>, BoxedError> {
795808
// Parse sql.
796-
let mut stmts = Parser::parse_sql(sql).map_err(|e| {
797-
tracing::error!("failed to parse sql:\n{}:\n{}", sql, e);
798-
e
799-
})?;
809+
let mut stmts = Parser::parse_sql(sql)
810+
.inspect_err(|e| tracing::error!("failed to parse sql:\n{}:\n{}", sql, e))?;
800811
if stmts.is_empty() {
801812
return Ok(vec![]);
802813
}
@@ -810,10 +821,8 @@ impl Session<PgResponseStream> for SessionImpl {
810821
// This part refers from src/frontend/handler/ so the Vec<PgFieldDescriptor> is same as
811822
// result of run_statement().
812823
let rsp = match stmt {
813-
Statement::Query(_) => infer(self, stmt, sql).map_err(|e| {
814-
tracing::error!("failed to handle sql:\n{}:\n{}", sql, e);
815-
e
816-
})?,
824+
Statement::Query(_) => infer(self, stmt, sql)
825+
.inspect_err(|e| tracing::error!("failed to handle sql:\n{}:\n{}", sql, e))?,
817826
Statement::ShowObjects(show_object) => match show_object {
818827
ShowObject::Columns { table: _ } => {
819828
vec![

0 commit comments

Comments
 (0)