feat(diagnostics): add SQL execution debug logging

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-07-16 17:01:24 +08:00
1 parent fc1f02a931
commit badb3b61b9
19 files changed
+874 -54

No files matched your search

+1
View File
@@ -49,6 +49,7 @@ pub mod schema;
pub mod schema_diff;
pub mod sql;
pub mod sql_analysis;
pub mod sql_diagnostics;
pub mod sql_dialect;
pub mod sql_editability;
pub mod sql_file_import;
+118 -27
View File
@@ -34,6 +34,8 @@ use crate::sql::{split_sql_batches, split_sql_statements, starts_with_executable
pub const QUERY_TIMEOUT: Duration = Duration::from_secs(30);
pub const MAX_ROWS: usize = 10000;
pub const QUERY_CANCELED: &str = "Query canceled";
const SQL_OMITTED_ERROR_CONTEXT: &str =
"SQL text omitted from user-facing error; enable debug SQL diagnostics for a redacted statement.";
#[cfg(feature = "duckdb-bundled")]
const DUCKDB_INTERRUPT_DRAIN_TIMEOUT: Duration = Duration::from_secs(2);
#[cfg(feature = "duckdb-bundled")]
@@ -46,6 +48,14 @@ pub enum PoolErrorAction {
ReconnectAndRetry,
}
fn query_error_with_omitted_sql_context(error: &str, _sql: &str) -> String {
if error.contains(SQL_OMITTED_ERROR_CONTEXT) {
error.to_string()
} else {
format!("{error}\n{SQL_OMITTED_ERROR_CONTEXT}")
}
}
/// A multi-statement result with metadata intended for query clients.
///
/// `execution_error` is emitted for synthesized per-statement errors so clients
@@ -1194,6 +1204,7 @@ pub async fn do_execute(
cancel_token: Option<CancellationToken>,
options: QueryExecutionOptions,
) -> Result<db::QueryResult, String> {
crate::sql_diagnostics::debug_sql("do_execute", sql);
if let Some(execution_id) = options.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.to_string());
}
@@ -1722,11 +1733,15 @@ pub async fn execute_sql_statement_with_options(
// on that tab-scoped pool so connection-level state (for example MySQL @vars)
// survives across runs.
let pool_key = if database.is_empty() {
state.get_or_create_pool_for_session(connection_id, None, options.client_session_id.as_deref()).await?
state
.get_or_create_pool_for_session(connection_id, None, options.client_session_id.as_deref())
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
} else {
state
.get_or_create_pool_for_session(connection_id, Some(database), options.client_session_id.as_deref())
.await?
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
};
if is_canceled(&cancel_token) {
@@ -1738,19 +1753,26 @@ pub async fn execute_sql_statement_with_options(
do_execute(state, &pool_key, mysql_dialect, Some(database), sql, schema, cancel_token.clone(), options.clone())
.await;
let with_sql_context =
|r: Result<db::QueryResult, String>| r.map_err(|e| query_error_with_omitted_sql_context(&e, sql));
let action = result.as_ref().err().map(|e| query_pool_error_action(db_type, sql, e));
match action {
Some(PoolErrorAction::ReconnectAndRetry) if !is_canceled(&cancel_token) => {
let db_opt = if database.is_empty() { None } else { Some(database) };
let new_key =
state.reconnect_pool_for_session(connection_id, db_opt, options.client_session_id.as_deref()).await?;
do_execute(state, &new_key, mysql_dialect, Some(database), sql, schema, cancel_token, options).await
let new_key = state
.reconnect_pool_for_session(connection_id, db_opt, options.client_session_id.as_deref())
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
with_sql_context(
do_execute(state, &new_key, mysql_dialect, Some(database), sql, schema, cancel_token, options).await,
)
}
Some(PoolErrorAction::Discard) => {
state.remove_pool_by_key(&pool_key).await;
result
with_sql_context(result)
}
_ => result,
_ => with_sql_context(result),
}
}
@@ -1767,7 +1789,8 @@ async fn execute_postgres_drop_database(
let admin_database = postgres_drop_database_admin_database(target_database);
let pool_key = state
.get_or_create_pool_for_session(connection_id, Some(admin_database), options.client_session_id.as_deref())
.await?;
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
if let Some(execution_id) = options.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.clone());
}
@@ -1915,11 +1938,15 @@ pub async fn execute_multi_core_with_options_for_client(
}
let pool_key = if database.is_empty() {
state.get_or_create_pool_for_session(connection_id, None, options.client_session_id.as_deref()).await?
state
.get_or_create_pool_for_session(connection_id, None, options.client_session_id.as_deref())
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
} else {
state
.get_or_create_pool_for_session(connection_id, Some(database), options.client_session_id.as_deref())
.await?
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
};
if let Some(execution_id) = options.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.clone());
@@ -2304,10 +2331,14 @@ pub async fn execute_statements(
schema: Option<&str>,
timeout_secs: Option<u64>,
) -> Result<db::QueryResult, String> {
let sql_ctx = statements.first().map(|s| s.as_str()).unwrap_or("");
let pool_key = if database.is_empty() {
connection_id.to_string()
} else {
state.get_or_create_pool(connection_id, Some(database)).await?
state
.get_or_create_pool(connection_id, Some(database))
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql_ctx))?
};
let mut total_affected: u64 = 0;
@@ -2352,7 +2383,7 @@ pub async fn execute_statements(
}
PoolErrorAction::Keep => {}
}
return Err(err);
return Err(query_error_with_omitted_sql_context(&err, sql_ctx));
}
}
}
@@ -2385,11 +2416,9 @@ pub async fn execute_statements(
}
PoolErrorAction::Keep => {}
}
return Err(format!(
"Statement {} failed: {}. Previous {} statement(s) may have been committed.",
i + 1,
e,
i
return Err(query_error_with_omitted_sql_context(
&format!("Statement {} failed: {}. Previous {} statement(s) may have been committed.", i + 1, e, i),
sql,
));
}
}
@@ -2427,10 +2456,14 @@ pub async fn execute_statements_in_transaction(
statements: &[String],
schema: Option<&str>,
) -> Result<db::QueryResult, String> {
let sql_ctx = statements.first().map(|s| s.as_str()).unwrap_or("");
let pool_key = if database.is_empty() {
connection_id.to_string()
} else {
state.get_or_create_pool(connection_id, Some(database)).await?
state
.get_or_create_pool(connection_id, Some(database))
.await
.map_err(|e| query_error_with_omitted_sql_context(&e, sql_ctx))?
};
// Read-only check: intercept all transaction paths before dispatching
@@ -2607,7 +2640,7 @@ async fn exec_tx_pg_statements(
async { tx.execute(sql, &[]).await.map_err(|e| e.to_string()) },
)
.await
.map_err(|e| format!("Statement {} failed: {}", i + 1, e))?;
.map_err(|e| query_error_with_omitted_sql_context(&format!("Statement {} failed: {}", i + 1, e), sql))?;
total_affected += affected;
}
tokio::time::timeout(budget.cleanup_timeout, tx.commit())
@@ -2641,7 +2674,7 @@ async fn exec_tx_mysql_inner(
Err(e) => {
let _ = mysql_query_drop_with_timeout(&mut conn, "ROLLBACK", budget.cleanup_timeout, "ROLLBACK failed")
.await;
return Err(format!("Statement {} failed: {}", i + 1, e));
return Err(query_error_with_omitted_sql_context(&format!("Statement {} failed: {}", i + 1, e), sql));
}
}
}
@@ -2701,7 +2734,10 @@ async fn exec_tx_sqlite_inner(
Ok(_) => total_affected += conn.changes(),
Err(e) => {
let _ = conn.execute_batch("ROLLBACK");
return Err(format!("Statement {} failed: {}", i + 1, e));
return Err(query_error_with_omitted_sql_context(
&format!("Statement {} failed: {}", i + 1, e),
sql,
));
}
}
}
@@ -2786,7 +2822,7 @@ async fn exec_tx_explicit_inner(
{
log::error!("ROLLBACK failed after statement {} error: {}", i + 1, rb_err);
}
return Err(format!("Statement {} failed: {}", i + 1, e));
return Err(query_error_with_omitted_sql_context(&format!("Statement {} failed: {}", i + 1, e), sql));
}
}
}
@@ -2819,7 +2855,7 @@ async fn exec_tx_none_inner(
) -> Result<db::QueryResult, String> {
let mut total_affected: u64 = 0;
for (i, sql) in statements.iter().enumerate() {
log::info!("[query][tx-none:statement:start] index={} sql={}", i + 1, sql);
log::info!("[query][tx-none:statement:start] index={}", i + 1);
match do_execute(state, pool_key, mysql_dialect, database, sql, schema, None, QueryExecutionOptions::default())
.await
{
@@ -2829,10 +2865,9 @@ async fn exec_tx_none_inner(
}
Err(e) => {
log::warn!("Statement {} failed (no transaction support): {}", i + 1, e);
return Err(format!(
"Statement {} failed: {}. No transaction support for this database type.",
i + 1,
e
return Err(query_error_with_omitted_sql_context(
&format!("Statement {} failed: {}. No transaction support for this database type.", i + 1, e),
sql,
));
}
}
@@ -4043,6 +4078,62 @@ mod tests {
assert!(is_connection_error("Error occurred while creating a new object: error communicating with the server"));
}
#[test]
fn query_error_context_omits_raw_sql_and_is_not_duplicated() {
let sql = "select 'secret-123' as token";
let error = query_error_with_omitted_sql_context("driver rejected statement", sql);
assert!(error.contains("driver rejected statement"));
assert!(error.contains(SQL_OMITTED_ERROR_CONTEXT));
assert!(!error.contains("secret-123"));
assert!(!error.contains("SQL:"));
let repeated = query_error_with_omitted_sql_context(&error, sql);
assert_eq!(repeated.matches(SQL_OMITTED_ERROR_CONTEXT).count(), 1);
}
#[test]
fn reconnect_retry_error_context_omits_raw_sql() {
let sql = "select 'secret-123' as token";
let reconnect_error = query_error_with_omitted_sql_context("connection reset after reconnect", sql);
assert!(reconnect_error.contains("connection reset after reconnect"));
assert!(reconnect_error.contains(SQL_OMITTED_ERROR_CONTEXT));
assert!(!reconnect_error.contains("secret-123"));
}
#[test]
fn execute_statements_error_omits_raw_sql() {
let sql = "select 'secret-token' as t";
let err = query_error_with_omitted_sql_context(
&format!(
"Statement {} failed: {}. Previous {} statement(s) may have been committed.",
2, "driver error", 1
),
sql,
);
assert!(err.contains("driver error"));
assert!(err.contains(SQL_OMITTED_ERROR_CONTEXT));
assert!(!err.contains("secret-token"));
assert!(!err.contains("SQL:"));
assert!(err.contains("Statement 2 failed:"));
}
#[test]
fn batch_transaction_error_omits_raw_sql() {
let sql = "delete from users where id = 'secret-id'";
let err = query_error_with_omitted_sql_context(
&format!("Statement {} failed: {}. No transaction support for this database type.", 3, "batch error"),
sql,
);
assert!(err.contains("batch error"));
assert!(err.contains(SQL_OMITTED_ERROR_CONTEXT));
assert!(!err.contains("secret-id"));
assert!(err.contains("Statement 3 failed:"));
}
#[test]
fn is_connection_error_detects_oracle_idle_timeout() {
assert!(is_connection_error("ORA-02396: exceeded maximum idle time, please connect again"));
+263
View File
@@ -0,0 +1,263 @@
const DEFAULT_SQL_DIAGNOSTIC_MAX_CHARS: usize = 512;
fn is_sensitive_key(key: &str) -> bool {
let key = key.to_ascii_lowercase();
key.contains("password")
|| key.contains("passwd")
|| key == "pwd"
|| key.contains("secret")
|| key.contains("token")
|| key.contains("api_key")
|| key.contains("apikey")
|| key.contains("access_key")
|| key.contains("private_key")
|| key.contains("credential")
|| key.contains("authorization")
|| key.contains("bearer")
}
fn truncate_for_diagnostics(value: String, max_chars: usize, input_truncated: bool) -> String {
if value.chars().count() <= max_chars {
return if input_truncated { format!("{value}…[truncated]") } else { value };
}
let head: String = value.chars().take(max_chars).collect();
format!("{head}…[truncated]")
}
fn bounded_input(sql: &str, max_chars: usize) -> (&str, bool) {
if max_chars == 0 {
return ("", !sql.is_empty());
}
match sql.char_indices().nth(max_chars) {
Some((index, _)) => (&sql[..index], true),
None => (sql, false),
}
}
fn redact_literals(sql: &str) -> String {
let chars: Vec<char> = sql.chars().collect();
let mut out = String::new();
let mut i = 0;
while i < chars.len() {
let ch = chars[i];
let next = chars.get(i + 1).copied();
if matches!(ch, '\'' | '"' | '`') {
out.push(ch);
out.push_str("[REDACTED]");
out.push(ch);
i += 1;
while i < chars.len() {
let current = chars[i];
if current == ch {
if chars.get(i + 1).copied() == Some(ch) {
i += 2;
continue;
}
i += 1;
break;
}
if current == '\\' && ch != '`' {
i += 2;
} else {
i += 1;
}
}
continue;
}
if ch == '$' {
let j = i + 1;
if j >= chars.len() {
out.push(ch);
i += 1;
continue;
}
if chars[j] == '$' {
// $$...$$ dollar-quoted string
out.push_str("$$[REDACTED]$$");
i += 2;
while i + 1 < chars.len() && !(chars[i] == '$' && chars[i + 1] == '$') {
i += 1;
}
if i + 1 < chars.len() {
i += 2;
}
continue;
}
// $tag$...$tag$ dollar-quoted string
let tag_start = j;
let mut tag_end = j;
while tag_end < chars.len() && (chars[tag_end].is_ascii_alphanumeric() || chars[tag_end] == '_') {
tag_end += 1;
}
if tag_end > tag_start && tag_end < chars.len() && chars[tag_end] == '$' {
let tag: String = chars[tag_start..tag_end].iter().collect();
out.push_str("$[REDACTED]$");
i = tag_end + 1;
let closing: Vec<char> = format!("${}$", tag).chars().collect();
while i + closing.len() <= chars.len() {
if chars[i..i + closing.len()] == closing[..] {
i += closing.len();
break;
}
i += 1;
}
continue;
}
out.push(ch);
i += 1;
continue;
}
if ch == '-' && next == Some('-') {
out.push_str("--[REDACTED_COMMENT]");
i += 2;
while i < chars.len() && chars[i] != '\n' && chars[i] != '\r' {
i += 1;
}
continue;
}
if ch == '/' && next == Some('*') {
out.push_str("/*[REDACTED_COMMENT]*/");
i += 2;
while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '/') {
i += 1;
}
if i + 1 < chars.len() {
i += 2;
}
continue;
}
out.push(ch);
i += 1;
}
out
}
fn redact_sensitive_assignments(sql: &str) -> String {
let chars: Vec<char> = sql.chars().collect();
let mut out = String::new();
let mut i = 0;
while i < chars.len() {
if chars[i].is_whitespace() {
out.push(chars[i]);
i += 1;
continue;
}
let start = i;
while i < chars.len()
&& (chars[i].is_ascii_alphanumeric() || chars[i] == '_' || chars[i] == '-' || chars[i] == '.')
{
i += 1;
}
if i == start {
out.push(chars[i]);
i += 1;
continue;
}
let key: String = chars[start..i].iter().collect();
let mut j = i;
while j < chars.len() && chars[j].is_whitespace() {
j += 1;
}
if j < chars.len() && (chars[j] == '=' || chars[j] == ':') {
if is_sensitive_key(&key) {
out.push_str(&key);
for k in i..j {
out.push(chars[k]);
}
out.push(chars[j]);
j += 1;
while j < chars.len() && chars[j].is_whitespace() {
out.push(chars[j]);
j += 1;
}
while j < chars.len() && !chars[j].is_whitespace() {
j += 1;
}
out.push_str("[REDACTED]");
i = j;
continue;
}
}
out.push_str(&key);
}
out
}
pub fn redact_sql_for_diagnostics(sql: &str) -> String {
let max_chars = DEFAULT_SQL_DIAGNOSTIC_MAX_CHARS;
let (bounded_sql, input_truncated) = bounded_input(sql, max_chars);
truncate_for_diagnostics(redact_sensitive_assignments(&redact_literals(bounded_sql)), max_chars, input_truncated)
}
pub fn debug_sql(scope: &str, sql: &str) {
log::debug!("[{scope}] sql={}", redact_sql_for_diagnostics(sql));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn redacts_sensitive_literals_and_bounds_large_sql() {
let sql = format!(
"select * from users where password = 'secret-123' and api_key=abc and name = 'alice' {};",
"x".repeat(900)
);
let redacted = redact_sql_for_diagnostics(&sql);
assert!(!redacted.contains("secret-123"));
assert!(!redacted.contains("api_key=abc"));
assert!(!redacted.contains("alice"));
// Literals not part of sensitive assignments retain single-quote redaction
assert!(redacted.contains("'[REDACTED]'"), "name literal should be redacted: {}", redacted);
// Sensitive assignment values are redacted with bracket notation
assert!(redacted.contains("password = [REDACTED]"));
assert!(redacted.contains("api_key=[REDACTED]"));
assert!(redacted.contains("truncated"));
assert!(redacted.len() < sql.len());
}
#[test]
fn redacts_space_separated_sensitive_assignments() {
let sql = "select * from users where password = hunter2";
let redacted = redact_sensitive_assignments(sql);
assert!(!redacted.contains("hunter2"));
assert!(redacted.contains("password = [REDACTED]"));
assert!(redacted.contains("select"));
}
#[test]
fn redacts_dollar_quoted_strings() {
let sql = "select $$secret$$, $tag$hello$tag$ from t";
let redacted = redact_sql_for_diagnostics(sql);
assert!(redacted.contains("$$[REDACTED]$$"));
assert!(redacted.contains("$[REDACTED]$"));
assert!(!redacted.contains("secret"));
assert!(!redacted.contains("hello"));
}
#[test]
fn large_input_bounded_allocation() {
let sql = "x".repeat(1_000_000);
let redacted = redact_sql_for_diagnostics(&sql);
assert!(redacted.len() <= 550);
}
#[test]
fn truncation_inside_unclosed_literal_does_not_leak_prefix() {
let sql = format!("select '{}'", "secret-".repeat(1_000));
let redacted = truncate_for_diagnostics(redact_literals(bounded_input(&sql, 32).0), 32, true);
assert!(!redacted.contains("secret-"));
assert!(redacted.contains("[REDACTED]"));
assert!(redacted.contains("truncated"));
}
#[test]
fn truncation_inside_sensitive_assignment_does_not_leak_prefix() {
let sql = format!("password = {}", "secret-token".repeat(1_000));
let (bounded, truncated) = bounded_input(&sql, 24);
let redacted = truncate_for_diagnostics(redact_sensitive_assignments(&redact_literals(bounded)), 24, truncated);
assert!(!redacted.contains("secret-token"));
assert!(redacted.contains("password = [REDACTED]"));
assert!(redacted.contains("truncated"));
}
}