Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(sqlserver): stop reading the result stream after the row limit (#13101)

zipgf4cb9f1495ac
diff --git a/crates/dbx-core/src/query/mod.rs b/crates/dbx-core/src/query/mod.rs
index feb70232aa..a15e0e2b3e 100644
--- a/crates/dbx-core/src/query/mod.rs
+++ b/crates/dbx-core/src/query/mod.rs
@@ -2042,11 +2042,20 @@ async fn do_execute_typed(
Ok(value) => value,
Err(err) => return Err(err.into()),
};
+ // A display-bound execution must not pay for the rows the caller
+ // discards: the driver stops the TDS stream at `max_rows` and marks
+ // the connection as abandoned, which then gets dropped instead of
+ // returned to the pool. Session-pinned executions keep the old
+ // draining path because the connection carries user state.
+ let stop_at_row_limit =
+ max_rows.is_some() && options.client_session_id.is_none() && !options.use_transaction.unwrap_or(false);
let execution = async {
if execution_mode == QueryExecutionMode::Simple {
let mut results =
db::sqlserver::execute_simple_batch_with_max_rows(&mut client, &sql, max_rows).await?;
Ok(results.remove(0))
+ } else if stop_at_row_limit {
+ db::sqlserver::execute_query_with_max_rows_stopping(&mut client, &sql, max_rows).await
} else {
db::sqlserver::execute_query_with_max_rows(&mut client, &sql, max_rows).await
}
@@ -2058,7 +2067,11 @@ async fn do_execute_typed(
result.execution_time_ms += lock_wait_ms;
result
});
+ let abandoned = client.take_abandoned_after_row_limit();
drop(client);
+ if abandoned {
+ state.remove_pool_by_key(pool_key).await;
+ }
if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
state.remove_pool_by_key(pool_key).await;
}
@@ -4626,16 +4639,35 @@ async fn execute_multi_sqlserver(
let execution_sql =
sql_for_execution_context_with_identifier_quote(Some(DatabaseType::SqlServer), batch, schema, Some("["));
+ // Only a single-statement batch can abandon its stream early: a real
+ // multi-statement script must keep draining so the later statements
+ // still produce their own results. Session-pinned tabs keep draining
+ // because the pooled connection carries their temporary objects.
+ let stop_at_row_limit = batches.len() == 1
+ && max_rows.is_some()
+ && options.client_session_id.is_none()
+ && !options.use_transaction.unwrap_or(false);
let execution = async {
if execution_mode == QueryExecutionMode::Simple {
db::sqlserver::execute_simple_batch_with_max_rows_metadata(&mut client_guard, &execution_sql, max_rows)
.await
+ } else if stop_at_row_limit {
+ db::sqlserver::execute_batch_with_max_rows_metadata_stopping(
+ &mut client_guard,
+ &execution_sql,
+ max_rows,
+ )
+ .await
} else {
db::sqlserver::execute_batch_with_max_rows_metadata(&mut client_guard, &execution_sql, max_rows).await
}
};
let result = wait_for_result_opt(cancel_token.clone(), query_timeout, execution).await;
+ let abandoned = client_guard.take_abandoned_after_row_limit();
drop(client_guard);
+ if abandoned {
+ state.remove_pool_by_key(pool_key).await;
+ }
match result {
Ok(results) => all_results.extend(sqlserver_batch_results(results).into_iter().map(|mut item| {
diff --git a/crates/dbx-core/tests/sqlserver_batch_results.rs b/crates/dbx-core/tests/sqlserver_batch_results.rs
index bd09824572..ee47132f6c 100644
--- a/crates/dbx-core/tests/sqlserver_batch_results.rs
+++ b/crates/dbx-core/tests/sqlserver_batch_results.rs
@@ -236,3 +236,76 @@ async fn sqlserver_duplicate_join_columns_page_without_leaking_rowcount() {
.expect("execute query after ROWCOUNT page");
assert_eq!(follow_up.rows.len(), 2);
}
+
+/// The desktop grid only ever shows `max_rows` rows. Reading the rest of the
+/// TDS stream costs ~10-15us per discarded row, which is what turns a
+/// few-hundred-ms query into a 10s+ stall when the executed SQL is unbounded.
+/// The stopping path must return the same bounded result but leave the client
+/// flagged for pool replacement instead of draining the whole result.
+#[tokio::test]
+#[ignore = "requires DBX_TEST_SQLSERVER_HOST and DBX_TEST_SQLSERVER_PASSWORD"]
+async fn sqlserver_stops_reading_after_row_limit_and_flags_the_connection() {
+ let mut client = connect_sqlserver().await;
+
+ // A huge streaming result that needs no fixture tables: every SQL Server
+ // has sys.all_columns, and the cross join keeps producing rows forever.
+ let sql = "SELECT a.object_id, b.object_id FROM sys.all_columns a CROSS JOIN sys.all_columns b";
+
+ let started = std::time::Instant::now();
+ let result = sqlserver::execute_query_with_max_rows_stopping(&mut client, sql, Some(100))
+ .await
+ .expect("execute stopping query");
+ let elapsed = started.elapsed();
+
+ assert_eq!(result.rows.len(), 100);
+ assert!(result.truncated, "the row limit must still be reported as truncation");
+ assert!(client.take_abandoned_after_row_limit(), "the client must be flagged for pool replacement");
+ assert!(!client.take_abandoned_after_row_limit(), "the flag must be one-shot");
+ assert!(elapsed < Duration::from_secs(30), "stopping must not drain the result: {elapsed:?}");
+
+ // The abandoned client is never reused. A replacement connection (what the
+ // pool creates after removing the abandoned one) must work normally.
+ let mut replacement = connect_sqlserver().await;
+ let health = sqlserver::execute_query_with_max_rows(&mut replacement, "SELECT 1 AS ok", Some(10))
+ .await
+ .expect("replacement connection query");
+ assert_eq!(health.rows.len(), 1);
+
+ // A result that fits under the limit keeps the connection usable.
+ let mut small = connect_sqlserver().await;
+ let small_result = sqlserver::execute_query_with_max_rows_stopping(&mut small, "SELECT 1 AS ok", Some(10))
+ .await
+ .expect("execute small stopping query");
+ assert_eq!(small_result.rows.len(), 1);
+ assert!(!small_result.truncated);
+ assert!(!small.take_abandoned_after_row_limit());
+}
+
+#[tokio::test]
+#[ignore = "requires DBX_TEST_SQLSERVER_HOST and DBX_TEST_SQLSERVER_PASSWORD"]
+async fn sqlserver_stopping_path_finishes_stored_procedure_side_effects() {
+ let mut client = connect_sqlserver().await;
+ sqlserver::execute_query(
+ &mut client,
+ "CREATE TABLE #dbx_row_limit_marker (value INT); INSERT INTO #dbx_row_limit_marker VALUES (0);",
+ )
+ .await
+ .unwrap();
+ sqlserver::execute_query(
+ &mut client,
+ "CREATE PROCEDURE #dbx_row_limit_proc AS BEGIN SET NOCOUNT ON; \
+ SELECT TOP (100000) a.object_id FROM sys.all_columns a CROSS JOIN sys.all_columns b; \
+ UPDATE #dbx_row_limit_marker SET value = 1; END;",
+ )
+ .await
+ .unwrap();
+
+ let result = sqlserver::execute_query_with_max_rows_stopping(&mut client, "EXEC #dbx_row_limit_proc", Some(1))
+ .await
+ .unwrap();
+ assert_eq!(result.rows.len(), 1);
+ assert!(result.truncated);
+ assert!(!client.take_abandoned_after_row_limit(), "a procedure must finish before returning");
+ let marker = sqlserver::execute_query(&mut client, "SELECT value FROM #dbx_row_limit_marker").await.unwrap();
+ assert_eq!(marker.rows[0][0].as_i64(), Some(1));
+}
diff --git a/crates/dbx-driver-sqlserver/src/sqlserver.rs b/crates/dbx-driver-sqlserver/src/sqlserver.rs
index 827d7efc01..123511c48e 100644
--- a/crates/dbx-driver-sqlserver/src/sqlserver.rs
+++ b/crates/dbx-driver-sqlserver/src/sqlserver.rs
@@ -41,11 +41,30 @@ pub struct SqlServerClient {
inner: SqlServerTdsClient,
query_transport: SqlServerQueryTransport,
server_major_version: Option<u32>,
+ /// Set when a result stream was abandoned mid-flight because the caller's
+ /// row limit was reached. The connection still has unread TDS tokens on
+ /// the wire, so it must be discarded instead of returned to the pool.
+ abandoned_after_row_limit: bool,
}
impl SqlServerClient {
fn new(inner: SqlServerTdsClient) -> Self {
- Self { inner, query_transport: SqlServerQueryTransport::Unknown, server_major_version: None }
+ Self {
+ inner,
+ query_transport: SqlServerQueryTransport::Unknown,
+ server_major_version: None,
+ abandoned_after_row_limit: false,
+ }
+ }
+
+ fn mark_abandoned_after_row_limit(&mut self) {
+ self.abandoned_after_row_limit = true;
+ }
+
+ /// Returns whether the last result stream was abandoned after the row limit
+ /// and resets the flag. Callers must discard (never reuse) such a client.
+ pub fn take_abandoned_after_row_limit(&mut self) -> bool {
+ std::mem::take(&mut self.abandoned_after_row_limit)
}
async fn ensure_query_transport(&mut self) -> SqlServerQueryTransport {
@@ -894,7 +913,8 @@ async fn collect_first_result_limited(
sql: &str,
query: &SqlServerUnsafeTypeQuery,
progress_clock: Option<&crate::execution::StreamProgressClock>,
-) -> Result<QueryResult, String> {
+ stop_at_row_limit: bool,
+) -> Result<(QueryResult, bool), String> {
let row_limit = query_result_row_limit(max_rows);
let mut remaining_offset = result_offset;
let mut columns: Vec<String> = vec![];
@@ -930,33 +950,82 @@ async fn collect_first_result_limited(
spatial_values.push(srids);
} else {
truncated = true;
+ if stop_at_row_limit {
+ // The caller only asked for `row_limit` rows. Reading the
+ // rest of a large result set costs ~10-15us per row on
+ // the TDS side (a 1M-row query then keeps the UI busy
+ // for 10s+ while the grid shows 100 rows), so abandon
+ // the stream and let the caller reset the connection.
+ return collect_first_result_finish(
+ columns,
+ column_types,
+ rows,
+ spatial_values,
+ spatial_values_builder,
+ query,
+ sql,
+ start,
+ true,
+ true,
+ );
+ }
}
}
QueryItem::Row(_) => {}
}
}
- restore_sqlserver_blank_column_names(&mut columns, sql);
- restore_sqlserver_unsafe_column_types(&mut column_types, query);
-
- let (spatial_columns, spatial_values) = spatial_values_builder.finish_with_values(spatial_values);
- Ok(QueryResult {
+ collect_first_result_finish(
columns,
column_types,
- column_sortables: vec![],
- spatial_columns,
- spatial_values,
rows,
- affected_rows: 0,
- execution_time_ms: start.elapsed().as_millis(),
- server_execute_time_us: None,
- query_timings_ms: None,
+ spatial_values,
+ spatial_values_builder,
+ query,
+ sql,
+ start,
truncated,
- session_id: None,
- has_more: false,
- elasticsearch_raw_body: None,
- messages: Vec::new(),
- })
+ false,
+ )
+}
+
+#[allow(clippy::too_many_arguments)]
+fn collect_first_result_finish(
+ mut columns: Vec<String>,
+ mut column_types: Vec<String>,
+ rows: Vec<Vec<serde_json::Value>>,
+ spatial_values: Vec<Vec<Option<u32>>>,
+ spatial_values_builder: SpatialColumnBuilder,
+ query: &SqlServerUnsafeTypeQuery,
+ sql: &str,
+ start: Instant,
+ truncated: bool,
+ stopped_early: bool,
+) -> Result<(QueryResult, bool), String> {
+ restore_sqlserver_blank_column_names(&mut columns, sql);
+ restore_sqlserver_unsafe_column_types(&mut column_types, query);
+
+ let (spatial_columns, spatial_values) = spatial_values_builder.finish_with_values(spatial_values);
+ Ok((
+ QueryResult {
+ columns,
+ column_types,
+ column_sortables: vec![],
+ spatial_columns,
+ spatial_values,
+ rows,
+ affected_rows: 0,
+ execution_time_ms: start.elapsed().as_millis(),
+ server_execute_time_us: None,
+ query_timings_ms: None,
+ truncated,
+ session_id: None,
+ has_more: false,
+ elasticsearch_raw_body: None,
+ messages: Vec::new(),
+ },
+ stopped_early,
+ ))
}
struct SqlServerResultSet {
@@ -1688,6 +1757,14 @@ fn is_single_sqlserver_select(sql: &str) -> bool {
normalized_sqlserver_select_statement(&statements[0]).is_some()
}
+/// Abandoning a stream also cancels the server's remaining work. Require a
+/// parsed query so EXEC, DML OUTPUT and scripts (including statements separated
+/// without semicolons) cannot lose side effects after their first result set.
+fn sqlserver_can_stop_at_row_limit(sql: &str) -> bool {
+ is_single_sqlserver_select(sql)
+ && matches!(Parser::parse_sql(&MsSqlDialect {}, sql).as_deref(), Ok([Statement::Query(_)]))
+}
+
fn sqlserver_source_column_name(index: usize) -> String {
format!("dbx_col_{}", index + 1)
}
@@ -3698,7 +3775,35 @@ pub async fn execute_query_with_max_rows(
sql: &str,
max_rows: Option<usize>,
) -> Result<QueryResult, String> {
- execute_query_with_max_rows_inner(client, sql, max_rows, None).await
+ execute_query_with_max_rows_inner(client, sql, max_rows, None, false).await
+}
+
+/// Like [`execute_query_with_max_rows`], but stops reading the TDS stream as
+/// soon as the row limit is reached instead of draining the whole result set.
+///
+/// Draining costs roughly 10-15us per discarded row on the client side, so a
+/// query that returns a few hundred thousand rows keeps DBX busy for seconds
+/// while the grid only ever shows `max_rows` rows. When this stops early the
+/// connection is left with unread tokens: the driver sends a best-effort TDS
+/// ATTENTION and marks the client as abandoned, and the caller must discard
+/// that pooled connection ([`SqlServerClient::take_abandoned_after_row_limit`]).
+/// Stored procedures, writes and scripts always drain to completion so their
+/// side effects are not canceled by a display row limit.
+pub async fn execute_query_with_max_rows_stopping(
+ client: &mut SqlServerClient,
+ sql: &str,
+ max_rows: Option<usize>,
+) -> Result<QueryResult, String> {
+ execute_query_with_max_rows_inner(client, sql, max_rows, None, sqlserver_can_stop_at_row_limit(sql)).await
+}
+
+async fn abandon_sqlserver_result_stream(client: &mut SqlServerClient) {
+ // ATTENTION is best effort: the connection is discarded either way, so a
+ // missing feature flag or a write error must never fail the query.
+ if let Err(error) = client.send_attention().await {
+ log::debug!("SQL Server ATTENTION after row-limit stop failed: {error}");
+ }
+ client.mark_abandoned_after_row_limit();
}
/// Progress-aware variant of [`execute_query_with_max_rows`] for long transfers.
@@ -3722,14 +3827,14 @@ pub async fn execute_query_with_max_rows_progress(
return crate::execution::wait_for_query_opt(
None,
timeout,
- execute_query_with_max_rows_inner(client, sql, max_rows, None),
+ execute_query_with_max_rows_inner(client, sql, max_rows, None, false),
)
.await;
}
let timeout_error = format!("Query timed out after {} seconds", timeout.map_or(0, |timeout| timeout.as_secs()));
let clock_for_query = progress_clock.clone();
crate::execution::await_stream_with_progress_timeout(
- execute_query_with_max_rows_inner(client, sql, max_rows, Some(&clock_for_query)),
+ execute_query_with_max_rows_inner(client, sql, max_rows, Some(&clock_for_query), false),
timeout,
progress_clock,
None,
@@ -3743,6 +3848,7 @@ async fn execute_query_with_max_rows_inner(
sql: &str,
max_rows: Option<usize>,
progress_clock: Option<&crate::execution::StreamProgressClock>,
+ stop_at_row_limit: bool,
) -> Result<QueryResult, String> {
let start = Instant::now();
let result_offset = crate::query_result_sql::sqlserver_result_offset(sql);
@@ -3758,7 +3864,7 @@ async fn execute_query_with_max_rows_inner(
};
let (result, messages) = capture_sqlserver_messages(async {
let stream = sqlserver_driver_result(client.query(query.sql.as_str(), &[])).await?;
- sqlserver_driver_result(collect_first_result_limited(
+ let (result, stopped_early) = sqlserver_driver_result(collect_first_result_limited(
stream,
start,
max_rows,
@@ -3766,11 +3872,17 @@ async fn execute_query_with_max_rows_inner(
sql,
&query,
progress_clock,
+ stop_at_row_limit,
))
- .await
+ .await?;
+ Ok::<_, String>((result, stopped_early))
})
.await;
- let mut result = query_result_with_server_messages(result?, messages);
+ let (result, stopped_early) = result?;
+ if stopped_early {
+ abandon_sqlserver_result_stream(client).await;
+ }
+ let mut result = query_result_with_server_messages(result, messages);
strip_dbx_sqlserver_row_number_column(&mut result, sql);
Ok(result)
} else if !sqlserver_batch_can_use_execute(sql) {
@@ -3819,6 +3931,26 @@ pub async fn execute_batch_with_max_rows_metadata(
client: &mut SqlServerClient,
sql: &str,
max_rows: Option<usize>,
+) -> Result<Vec<SqlServerBatchResult>, String> {
+ execute_batch_with_max_rows_metadata_inner(client, sql, max_rows, false).await
+}
+
+/// Row-limited batch execution that abandons the result stream once the limit
+/// is reached. Only single-result sets can stop early: a real multi-statement
+/// batch keeps draining so later statements still produce their results.
+pub async fn execute_batch_with_max_rows_metadata_stopping(
+ client: &mut SqlServerClient,
+ sql: &str,
+ max_rows: Option<usize>,
+) -> Result<Vec<SqlServerBatchResult>, String> {
+ execute_batch_with_max_rows_metadata_inner(client, sql, max_rows, sqlserver_can_stop_at_row_limit(sql)).await
+}
+
+async fn execute_batch_with_max_rows_metadata_inner(
+ client: &mut SqlServerClient,
+ sql: &str,
+ max_rows: Option<usize>,
+ stop_at_row_limit: bool,
) -> Result<Vec<SqlServerBatchResult>, String> {
let start = Instant::now();
let result_offset = crate::query_result_sql::sqlserver_result_offset(sql);
@@ -3852,7 +3984,7 @@ pub async fn execute_batch_with_max_rows_metadata(
Ok(Some(query)) => {
let (result, messages) = capture_sqlserver_messages(async {
let stream = sqlserver_driver_result(client.query(query.sql.as_str(), &[])).await?;
- sqlserver_driver_result(collect_first_result_limited(
+ let (result, stopped_early) = sqlserver_driver_result(collect_first_result_limited(
stream,
start,
max_rows,
@@ -3860,15 +3992,19 @@ pub async fn execute_batch_with_max_rows_metadata(
sql,
&query,
None,
+ stop_at_row_limit,
))
- .await
+ .await?;
+ Ok::<_, String>((result, stopped_early))
})
.await;
- return result.map(|result| {
- let mut result = query_result_with_server_messages_metadata(result, messages);
- strip_dbx_sqlserver_row_number_column(&mut result.result, sql);
- vec![result]
- });
+ let (result, stopped_early) = result?;
+ if stopped_early {
+ abandon_sqlserver_result_stream(client).await;
+ }
+ let mut result = query_result_with_server_messages_metadata(result, messages);
+ strip_dbx_sqlserver_row_number_column(&mut result.result, sql);
+ return Ok(vec![result]);
}
Err(error) if is_blocking_sqlserver_unsafe_probe_error(&error) => return Err(error),
Ok(None) | Err(_) => {}
@@ -3956,8 +4092,18 @@ async fn execute_simple_batch_first_result_with_max_rows(
let (result, messages) = capture_sqlserver_messages(async {
let stream = sqlserver_driver_result(client.simple_query(sql)).await?;
let query = SqlServerUnsafeTypeQuery::plain(sql);
- sqlserver_driver_result(collect_first_result_limited(stream, start, max_rows, result_offset, sql, &query, None))
- .await
+ sqlserver_driver_result(collect_first_result_limited(
+ stream,
+ start,
+ max_rows,
+ result_offset,
+ sql,
+ &query,
+ None,
+ false,
+ ))
+ .await
+ .map(|(result, _)| result)
})
.await;
let mut result = query_result_with_server_messages(result?, messages);
@@ -4295,6 +4441,29 @@ fn first_sql_tokens(sql: &str, limit: usize) -> Vec<String> {
#[cfg(test)]
mod tests {
+ #[test]
+ fn row_limit_stopping_only_accepts_single_read_queries() {
+ for sql in [
+ "SELECT * FROM dbo.items",
+ "SELECT DISTINCT id FROM dbo.items ORDER BY id;",
+ "WITH items AS (SELECT 1 AS id) SELECT id FROM items;",
+ ] {
+ assert!(super::sqlserver_can_stop_at_row_limit(sql), "{sql}");
+ }
+ for sql in [
+ "EXEC dbo.read_then_update;",
+ "EXECUTE dbo.read_then_update;",
+ "UPDATE dbo.items SET value = 1 OUTPUT inserted.id;",
+ "SELECT * INTO dbo.copy FROM dbo.items;",
+ "SELECT * FROM dbo.items; UPDATE dbo.items SET value = 1;",
+ "SELECT * FROM dbo.items\nUPDATE dbo.items SET value = 1",
+ "SELECT * FROM dbo.items\nSELECT 2",
+ "WITH items AS (SELECT 1 AS id) DELETE FROM dbo.items OUTPUT deleted.id;",
+ ] {
+ assert!(!super::sqlserver_can_stop_at_row_limit(sql), "{sql}");
+ }
+ }
+
#[test]
fn manual_transaction_status_keeps_legacy_and_modern_safety_explicit() {
use super::{uses_legacy_transaction_status as legacy, ManualTransactionStatus as Status};
@@ -5490,8 +5659,7 @@ mod tests {
let first_result = source.split("async fn execute_simple_batch_first_result_with_max_rows").nth(1).unwrap();
let first_result = first_result.split("fn strip_dbx_sqlserver_row_number_column").next().unwrap();
- assert!(first_result
- .contains("collect_first_result_limited(stream, start, max_rows, result_offset, sql, &query, None)"));
+ assert!(first_result.contains("collect_first_result_limited("));
assert!(!first_result.contains("collect_result_sets_limited"));
}