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(postgres): retry queries with stale cached prepared statements

t8y2440e675a0e5c
diff --git a/crates/dbx-core/src/db/postgres.rs b/crates/dbx-core/src/db/postgres.rs
index 934c48e0fc..c4d51f18cd 100644
--- a/crates/dbx-core/src/db/postgres.rs
+++ b/crates/dbx-core/src/db/postgres.rs
@@ -420,6 +420,11 @@ fn should_retry_postgres_text_query(err: &tokio_postgres::Error) -> bool {
|| message.contains("cannot display a value of type")
}
+fn should_retry_postgres_stale_cache(err: &tokio_postgres::Error) -> bool {
+ let message = err.as_db_error().map(ToString::to_string).unwrap_or_else(|| err.to_string()).to_ascii_lowercase();
+ message.contains("cached plan must not change result type")
+}
+
async fn execute_select_prepared(
client: &deadpool_postgres::Client,
sql: &str,
@@ -542,6 +547,20 @@ async fn execute_select_query(
) -> Result<QueryResult, String> {
match execute_select_prepared(client, sql, start, row_limit).await {
Ok(result) => Ok(result),
+ Err(err) if should_retry_postgres_stale_cache(&err) => {
+ // The cached prepared statement is stale (e.g. the view or table
+ // schema changed since the statement was prepared). Evict the
+ // stale entry and retry with a fresh server-side prepare.
+ log::warn!("[postgres][select:stale_cache] evicting cached statement: {}", pg_error_to_string(err));
+ client.statement_cache.remove(sql, &[]);
+ match execute_select_prepared(client, sql, start, row_limit).await {
+ Ok(result) => Ok(result),
+ Err(err) if should_retry_postgres_text_query(&err) => {
+ execute_select_text(client, sql, start, row_limit).await
+ }
+ Err(err) => Err(pg_error_to_string(err)),
+ }
+ }
Err(err) if should_retry_postgres_text_query(&err) => execute_select_text(client, sql, start, row_limit).await,
Err(err) => Err(pg_error_to_string(err)),
}