feat(query): show database server messages in query results
This commit is contained in:
1 parent
4c7ddd266b
commit
a5e46fd659
45 files changed
+1401
-51
No files matched your search
@@ -15,13 +15,16 @@ use std::collections::{BTreeSet, HashMap};
|
||||
use std::fs::File;
|
||||
use std::future::Future;
|
||||
use std::io::BufReader;
|
||||
use std::pin::Pin;
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::{Arc, Mutex, OnceLock, Weak};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio_postgres::config::SslMode;
|
||||
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
|
||||
use tokio_postgres::types::{FromSql, Kind, Type};
|
||||
use tokio_postgres::{NoTls, Row, SimpleQueryMessage};
|
||||
use tokio_postgres::{AsyncMessage, NoTls, Row, SimpleQueryMessage, Socket};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::file_validator::validate_file_path;
|
||||
@@ -31,7 +34,8 @@ use crate::types::{
|
||||
ColumnInfo, CompletionAssistantCandidate, CompletionAssistantCandidateKind, CompletionAssistantMatchMode,
|
||||
CompletionAssistantObjectKind, CompletionAssistantRequest, CompletionAssistantResponse, DatabaseInfo,
|
||||
DatabaseStorageInfo, ExtensionInfo, ForeignKeyInfo, FunctionInfo, IndexInfo, ObjectInfo, ObjectStatistics,
|
||||
OwnerInfo, QueryResult, RuleInfo, SchemaInfo, SequenceInfo, SpatialColumnBuilder, TableInfo, TriggerInfo,
|
||||
OwnerInfo, QueryMessage, QueryResult, RuleInfo, SchemaInfo, SequenceInfo, SpatialColumnBuilder, TableInfo,
|
||||
TriggerInfo,
|
||||
};
|
||||
|
||||
pub(crate) const GAUSSDB_COMPATIBILITY_SQL: &str =
|
||||
@@ -1056,6 +1060,7 @@ async fn execute_select_prepared(
|
||||
session_id: None,
|
||||
has_more: false,
|
||||
elasticsearch_raw_body: None,
|
||||
messages: Vec::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1150,6 +1155,7 @@ async fn execute_select_text(
|
||||
session_id: None,
|
||||
has_more: false,
|
||||
elasticsearch_raw_body: None,
|
||||
messages: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1484,6 +1490,188 @@ async fn connect_with_local_timezone(url: &str, fallback_timeout: Duration, time
|
||||
connect_with_optional_local_timezone(url, fallback_timeout, Some(timezone)).await
|
||||
}
|
||||
|
||||
/// Identity of one physical backend connection for notice attribution:
|
||||
/// (server address, server port, backend PID). The PID alone is not unique
|
||||
/// across different servers, so the server's own address/port disambiguate
|
||||
/// (`inet_server_addr()` is NULL for Unix sockets, hence the fallback).
|
||||
type PostgresConnectionKey = (String, i32, i32);
|
||||
|
||||
const POSTGRES_CONNECTION_IDENTITY_SQL: &str = "SELECT pg_backend_pid(), \
|
||||
COALESCE(host(inet_server_addr()), 'unix'), \
|
||||
COALESCE(inet_server_port(), current_setting('port')::integer)";
|
||||
|
||||
/// Notice buffers for live connections, keyed by connection identity. Entries
|
||||
/// are weak so they disappear once the pooled connection (and its driver
|
||||
/// task) is dropped.
|
||||
fn postgres_notice_buffers() -> &'static Mutex<HashMap<PostgresConnectionKey, Weak<Mutex<Vec<QueryMessage>>>>> {
|
||||
static BUFFERS: OnceLock<Mutex<HashMap<PostgresConnectionKey, Weak<Mutex<Vec<QueryMessage>>>>>> = OnceLock::new();
|
||||
BUFFERS.get_or_init(|| Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
/// Connection identity cache, keyed by the per-physical-connection statement
|
||||
/// cache pointer (same identity pattern as `postgres_single_schema_clients`).
|
||||
/// The `Weak` guards against pointer reuse: a new connection whose
|
||||
/// `StatementCache` lands on a freed entry's address fails the `ptr_eq` check
|
||||
/// and is treated as a miss. The value is `None` when the identity query
|
||||
/// failed, so it is issued at most once per physical connection — it must
|
||||
/// never be retried from `drain_postgres_notices`, which can run inside the
|
||||
/// read-only transaction used for EXPLAIN, where a failing query would abort
|
||||
/// the user's statement.
|
||||
fn postgres_client_keys(
|
||||
) -> &'static Mutex<HashMap<usize, (Weak<deadpool_postgres::StatementCache>, Option<PostgresConnectionKey>)>> {
|
||||
static KEYS: OnceLock<
|
||||
Mutex<HashMap<usize, (Weak<deadpool_postgres::StatementCache>, Option<PostgresConnectionKey>)>>,
|
||||
> = OnceLock::new();
|
||||
KEYS.get_or_init(|| Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
/// Establishes connections like deadpool's `ConfigConnectImpl`, but drives
|
||||
/// each connection with a task that captures `NoticeResponse` messages
|
||||
/// (`RAISE NOTICE`/`WARNING`, etc.) into a per-backend buffer instead of
|
||||
/// discarding them. Query execution drains the buffer so notices are
|
||||
/// attached to the `QueryResult` of the statement that raised them.
|
||||
struct NoticeCapturingConnect<T>
|
||||
where
|
||||
T: MakeTlsConnect<Socket> + Clone + Sync + Send + 'static,
|
||||
T::Stream: Sync + Send,
|
||||
T::TlsConnect: Sync + Send,
|
||||
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
|
||||
{
|
||||
tls: T,
|
||||
}
|
||||
|
||||
impl<T> deadpool_postgres::Connect for NoticeCapturingConnect<T>
|
||||
where
|
||||
T: MakeTlsConnect<Socket> + Clone + Sync + Send + 'static,
|
||||
T::Stream: Sync + Send,
|
||||
T::TlsConnect: Sync + Send,
|
||||
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
|
||||
{
|
||||
fn connect(
|
||||
&self,
|
||||
pg_config: &tokio_postgres::Config,
|
||||
) -> Pin<
|
||||
Box<dyn Future<Output = Result<(tokio_postgres::Client, JoinHandle<()>), tokio_postgres::Error>> + Send + '_>,
|
||||
> {
|
||||
let tls = self.tls.clone();
|
||||
let pg_config = pg_config.clone();
|
||||
Box::pin(async move {
|
||||
let (client, mut connection) = pg_config.connect(tls).await?;
|
||||
// No query can complete before the connection is being driven, so
|
||||
// the notice buffer is handed to the driver task through a slot
|
||||
// that is filled once the backend PID is known.
|
||||
let notice_buffer = Arc::new(Mutex::new(None::<Arc<Mutex<Vec<QueryMessage>>>>));
|
||||
let task_buffer = Arc::clone(¬ice_buffer);
|
||||
let conn_task = tokio::spawn(async move {
|
||||
loop {
|
||||
match std::future::poll_fn(|cx| connection.poll_message(cx)).await {
|
||||
Some(Ok(AsyncMessage::Notice(error))) => {
|
||||
let message = QueryMessage {
|
||||
severity: error.severity().to_string(),
|
||||
message: error.message().to_string(),
|
||||
code: Some(error.code().code().to_string()),
|
||||
detail: error.detail().map(str::to_string),
|
||||
hint: error.hint().map(str::to_string),
|
||||
};
|
||||
let buffer = task_buffer.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).clone();
|
||||
match buffer {
|
||||
Some(buffer) => {
|
||||
buffer.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).push(message);
|
||||
}
|
||||
None => {
|
||||
log::info!("[postgres][notice] {}: {}", message.severity, message.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
// LISTEN/NOTIFY messages are not surfaced anywhere.
|
||||
Some(Ok(_)) => {}
|
||||
Some(Err(err)) => {
|
||||
log::warn!("[postgres] connection driver error: {err}");
|
||||
break;
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Best-effort: without the connection identity, notices cannot be
|
||||
// attributed to query results on this connection and are logged
|
||||
// by the driver task instead. Never fail the connection over this.
|
||||
if let Ok(row) = client.query_one(POSTGRES_CONNECTION_IDENTITY_SQL, &[]).await {
|
||||
let key: PostgresConnectionKey = (row.get(1), row.get(2), row.get(0));
|
||||
let buffer = Arc::new(Mutex::new(Vec::new()));
|
||||
let mut buffers = postgres_notice_buffers().lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
buffers.retain(|_, weak| weak.strong_count() > 0);
|
||||
buffers.insert(key, Arc::downgrade(&buffer));
|
||||
drop(buffers);
|
||||
*notice_buffer.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(buffer);
|
||||
}
|
||||
|
||||
Ok((client, conn_task))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Cached identity lookup. `Some(Some(key))` = resolved, `Some(None)` =
|
||||
/// identity query failed before (do not retry), `None` = never seen (or the
|
||||
/// entry belonged to a dropped connection whose address was reused).
|
||||
fn cached_postgres_client_key(client: &deadpool_postgres::Client) -> Option<Option<PostgresConnectionKey>> {
|
||||
let cache_key = Arc::as_ptr(&client.statement_cache) as usize;
|
||||
let mut keys = postgres_client_keys().lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
match keys.get(&cache_key) {
|
||||
Some((cached, key)) if cached.upgrade().is_some_and(|cached| Arc::ptr_eq(&cached, &client.statement_cache)) => {
|
||||
Some(key.clone())
|
||||
}
|
||||
Some(_) => {
|
||||
keys.remove(&cache_key);
|
||||
None
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Best-effort identity resolution. Failures are cached as `None` so the
|
||||
/// identity query runs at most once per physical connection.
|
||||
async fn resolve_postgres_client_key(client: &deadpool_postgres::Client) -> Option<PostgresConnectionKey> {
|
||||
if let Some(key) = cached_postgres_client_key(client) {
|
||||
return key;
|
||||
}
|
||||
let key: Option<PostgresConnectionKey> = client
|
||||
.query_one(POSTGRES_CONNECTION_IDENTITY_SQL, &[])
|
||||
.await
|
||||
.ok()
|
||||
.map(|row| (row.get(1), row.get(2), row.get(0)));
|
||||
let mut keys = postgres_client_keys().lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
keys.retain(|_, (cached, _)| cached.strong_count() > 0);
|
||||
let cache_key = Arc::as_ptr(&client.statement_cache) as usize;
|
||||
keys.insert(cache_key, (Arc::downgrade(&client.statement_cache), key.clone()));
|
||||
key
|
||||
}
|
||||
|
||||
async fn postgres_client_key(client: &deadpool_postgres::Client) -> Option<PostgresConnectionKey> {
|
||||
resolve_postgres_client_key(client).await
|
||||
}
|
||||
|
||||
fn take_notices_for_key(key: &PostgresConnectionKey) -> Vec<QueryMessage> {
|
||||
let buffer = {
|
||||
let mut buffers = postgres_notice_buffers().lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
buffers.retain(|_, weak| weak.strong_count() > 0);
|
||||
buffers.get(key).and_then(Weak::upgrade)
|
||||
};
|
||||
let Some(buffer) = buffer else {
|
||||
return Vec::new();
|
||||
};
|
||||
let notices = std::mem::take(&mut *buffer.lock().unwrap_or_else(|poisoned| poisoned.into_inner()));
|
||||
notices
|
||||
}
|
||||
|
||||
async fn drain_postgres_notices(client: &deadpool_postgres::Client) -> Vec<QueryMessage> {
|
||||
match postgres_client_key(client).await {
|
||||
Some(key) => take_notices_for_key(&key),
|
||||
None => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn connect_with_optional_local_timezone(
|
||||
url: &str,
|
||||
fallback_timeout: Duration,
|
||||
@@ -1512,9 +1700,9 @@ async fn connect_with_optional_local_timezone(
|
||||
postgres_url.accepts_invalid_certs,
|
||||
postgres_url.verifies_hostname,
|
||||
)?;
|
||||
let mgr = deadpool_postgres::Manager::from_config(
|
||||
let mgr = deadpool_postgres::Manager::from_connect(
|
||||
pg_config.clone(),
|
||||
tokio_postgres_rustls::MakeRustlsConnect::new(tls_config),
|
||||
NoticeCapturingConnect { tls: tokio_postgres_rustls::MakeRustlsConnect::new(tls_config) },
|
||||
mgr_config,
|
||||
);
|
||||
let pool = Pool::builder(mgr)
|
||||
@@ -3341,6 +3529,7 @@ pub async fn execute_query_with_max_rows(
|
||||
session_id: None,
|
||||
has_more: false,
|
||||
elasticsearch_raw_body: None,
|
||||
messages: Vec::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3890,12 +4079,18 @@ pub async fn checkout_postgres_client(
|
||||
},
|
||||
None => get_future.await,
|
||||
};
|
||||
if result.is_ok() {
|
||||
if let Ok(client) = &result {
|
||||
log::debug!(
|
||||
"[db:pool.checkout:done] elapsed_ms={} timeout_ms={}",
|
||||
start.elapsed().as_millis(),
|
||||
checkout_timeout.as_millis()
|
||||
);
|
||||
// Resolve the notice-attribution identity here, outside of any
|
||||
// transaction: a lazy first lookup from `drain_postgres_notices`
|
||||
// could run inside the read-only EXPLAIN transaction, where a
|
||||
// failing identity query would abort the user's statement. Cached
|
||||
// after the first checkout of each physical connection.
|
||||
let _ = resolve_postgres_client_key(client).await;
|
||||
}
|
||||
result
|
||||
}
|
||||
@@ -3960,16 +4155,19 @@ async fn execute_query_with_max_rows_inner(
|
||||
let start = Instant::now();
|
||||
let row_limit = query_result_row_limit(max_rows);
|
||||
|
||||
if postgres_statement_returns_rows(sql) {
|
||||
// Discard stale notices from infrastructure statements (e.g. the timezone
|
||||
// SET issued at connect time) so only messages raised by this statement
|
||||
// are attached to its result.
|
||||
let _ = drain_postgres_notices(client).await;
|
||||
|
||||
let result = if postgres_statement_returns_rows(sql) {
|
||||
if prefer_text_protocol {
|
||||
execute_select_text(client, sql, start, row_limit, None, progress_clock.as_deref()).await
|
||||
} else {
|
||||
execute_select_query_with_progress(client, sql, start, row_limit, progress_clock.as_deref()).await
|
||||
}
|
||||
} else {
|
||||
let affected = client.execute(sql, &[]).await.map_err(pg_error_to_string)?;
|
||||
|
||||
Ok(QueryResult {
|
||||
client.execute(sql, &[]).await.map_err(pg_error_to_string).map(|affected| QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: Vec::new(),
|
||||
@@ -3982,7 +4180,21 @@ async fn execute_query_with_max_rows_inner(
|
||||
session_id: None,
|
||||
has_more: false,
|
||||
elasticsearch_raw_body: None,
|
||||
messages: Vec::new(),
|
||||
})
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(mut result) => {
|
||||
result.messages = drain_postgres_notices(client).await;
|
||||
Ok(result)
|
||||
}
|
||||
Err(error) => {
|
||||
// Drop notices so an errored statement's messages cannot leak
|
||||
// into the next query on this pooled connection.
|
||||
let _ = drain_postgres_notices(client).await;
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4734,6 +4946,87 @@ mod tests {
|
||||
assert_eq!(gaussdb_identifier_quote_for_compatibility_mode(""), None);
|
||||
}
|
||||
|
||||
fn test_query_message(message: &str) -> QueryMessage {
|
||||
QueryMessage {
|
||||
severity: "NOTICE".to_string(),
|
||||
message: message.to_string(),
|
||||
code: Some("00000".to_string()),
|
||||
detail: None,
|
||||
hint: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_notices_for_key_returns_buffered_notices_and_empties_buffer() {
|
||||
let key = ("test-host".to_string(), 9_000_001, 9_000_001);
|
||||
let buffer = Arc::new(Mutex::new(vec![test_query_message("first"), test_query_message("second")]));
|
||||
postgres_notice_buffers()
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
.insert(key.clone(), Arc::downgrade(&buffer));
|
||||
|
||||
let notices = take_notices_for_key(&key);
|
||||
assert_eq!(notices.len(), 2);
|
||||
assert_eq!(notices[0].message, "first");
|
||||
assert_eq!(notices[1].message, "second");
|
||||
assert_eq!(notices[0].severity, "NOTICE");
|
||||
assert_eq!(notices[0].code.as_deref(), Some("00000"));
|
||||
|
||||
// The buffer was drained but stays registered while the connection lives.
|
||||
assert!(take_notices_for_key(&key).is_empty());
|
||||
buffer.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).push(test_query_message("third"));
|
||||
let notices = take_notices_for_key(&key);
|
||||
assert_eq!(notices.len(), 1);
|
||||
assert_eq!(notices[0].message, "third");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_notices_for_key_prunes_dead_buffers_and_misses_return_empty() {
|
||||
let live_key = ("test-host".to_string(), 9_000_002, 9_000_002);
|
||||
let dead_key = ("test-host".to_string(), 9_000_003, 9_000_003);
|
||||
let live = Arc::new(Mutex::new(vec![test_query_message("live")]));
|
||||
let dead = Arc::new(Mutex::new(vec![test_query_message("dead")]));
|
||||
{
|
||||
let mut buffers = postgres_notice_buffers().lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
buffers.insert(live_key.clone(), Arc::downgrade(&live));
|
||||
buffers.insert(dead_key.clone(), Arc::downgrade(&dead));
|
||||
}
|
||||
drop(dead);
|
||||
|
||||
assert!(take_notices_for_key(&("test-host".to_string(), 9_000_004, 9_000_004)).is_empty());
|
||||
assert!(take_notices_for_key(&dead_key).is_empty());
|
||||
let buffers = postgres_notice_buffers().lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
assert!(!buffers.contains_key(&dead_key));
|
||||
assert!(buffers.contains_key(&live_key));
|
||||
drop(buffers);
|
||||
|
||||
let notices = take_notices_for_key(&live_key);
|
||||
assert_eq!(notices.len(), 1);
|
||||
assert_eq!(notices[0].message, "live");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_notices_for_key_distinguishes_same_pid_on_different_servers() {
|
||||
// Backend PIDs collide across servers; the (address, port, pid) key
|
||||
// keeps notice attribution separate.
|
||||
let key_a = ("server-a".to_string(), 5432, 42);
|
||||
let key_b = ("server-b".to_string(), 5432, 42);
|
||||
let buffer_a = Arc::new(Mutex::new(vec![test_query_message("from-a")]));
|
||||
let buffer_b = Arc::new(Mutex::new(vec![test_query_message("from-b")]));
|
||||
{
|
||||
let mut buffers = postgres_notice_buffers().lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
buffers.insert(key_a.clone(), Arc::downgrade(&buffer_a));
|
||||
buffers.insert(key_b.clone(), Arc::downgrade(&buffer_b));
|
||||
}
|
||||
|
||||
let notices_a = take_notices_for_key(&key_a);
|
||||
let notices_b = take_notices_for_key(&key_b);
|
||||
assert_eq!(notices_a.len(), 1);
|
||||
assert_eq!(notices_a[0].message, "from-a");
|
||||
assert_eq!(notices_b.len(), 1);
|
||||
assert_eq!(notices_b[0].message, "from-b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_json_arrays_decode_elements_without_jsonb_version_bytes() {
|
||||
let json_raw = pg_array_binary(
|
||||
@@ -6586,6 +6879,7 @@ mod tests {
|
||||
session_id: None,
|
||||
has_more: false,
|
||||
elasticsearch_raw_body: None,
|
||||
messages: Vec::new(),
|
||||
};
|
||||
|
||||
let columns = redshift_columns_from_query_result(result);
|
||||
|
||||
Reference in new issue
Block a user