Compare commits

...

3 Commits

Author SHA1 Message Date
Conrad Ludgate
e8526226ae remove unnecessary comment 2023-08-11 15:47:53 +01:00
Alex Chi Z
776cc063c5 bump rust-postgres
Signed-off-by: Alex Chi Z <chi@neon.tech>
2023-08-11 10:17:24 -04:00
Alex Chi Z
789288c735 proxy: show error context for execution failure
Signed-off-by: Alex Chi Z <chi@neon.tech>
2023-08-10 09:52:40 -04:00
3 changed files with 32 additions and 25 deletions

10
Cargo.lock generated
View File

@@ -2798,7 +2798,7 @@ dependencies = [
[[package]] [[package]]
name = "postgres" name = "postgres"
version = "0.19.4" version = "0.19.4"
source = "git+https://github.com/neondatabase/rust-postgres.git?rev=9011f7110db12b5e15afaf98f8ac834501d50ddc#9011f7110db12b5e15afaf98f8ac834501d50ddc" source = "git+https://github.com/neondatabase/rust-postgres.git?rev=b25e7f366487f41bc1607e6d824e88996fb02350#b25e7f366487f41bc1607e6d824e88996fb02350"
dependencies = [ dependencies = [
"bytes", "bytes",
"fallible-iterator", "fallible-iterator",
@@ -2811,7 +2811,7 @@ dependencies = [
[[package]] [[package]]
name = "postgres-native-tls" name = "postgres-native-tls"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/neondatabase/rust-postgres.git?rev=9011f7110db12b5e15afaf98f8ac834501d50ddc#9011f7110db12b5e15afaf98f8ac834501d50ddc" source = "git+https://github.com/neondatabase/rust-postgres.git?rev=b25e7f366487f41bc1607e6d824e88996fb02350#b25e7f366487f41bc1607e6d824e88996fb02350"
dependencies = [ dependencies = [
"native-tls", "native-tls",
"tokio", "tokio",
@@ -2822,7 +2822,7 @@ dependencies = [
[[package]] [[package]]
name = "postgres-protocol" name = "postgres-protocol"
version = "0.6.4" version = "0.6.4"
source = "git+https://github.com/neondatabase/rust-postgres.git?rev=9011f7110db12b5e15afaf98f8ac834501d50ddc#9011f7110db12b5e15afaf98f8ac834501d50ddc" source = "git+https://github.com/neondatabase/rust-postgres.git?rev=b25e7f366487f41bc1607e6d824e88996fb02350#b25e7f366487f41bc1607e6d824e88996fb02350"
dependencies = [ dependencies = [
"base64 0.20.0", "base64 0.20.0",
"byteorder", "byteorder",
@@ -2840,7 +2840,7 @@ dependencies = [
[[package]] [[package]]
name = "postgres-types" name = "postgres-types"
version = "0.2.4" version = "0.2.4"
source = "git+https://github.com/neondatabase/rust-postgres.git?rev=9011f7110db12b5e15afaf98f8ac834501d50ddc#9011f7110db12b5e15afaf98f8ac834501d50ddc" source = "git+https://github.com/neondatabase/rust-postgres.git?rev=b25e7f366487f41bc1607e6d824e88996fb02350#b25e7f366487f41bc1607e6d824e88996fb02350"
dependencies = [ dependencies = [
"bytes", "bytes",
"fallible-iterator", "fallible-iterator",
@@ -4331,7 +4331,7 @@ dependencies = [
[[package]] [[package]]
name = "tokio-postgres" name = "tokio-postgres"
version = "0.7.7" version = "0.7.7"
source = "git+https://github.com/neondatabase/rust-postgres.git?rev=9011f7110db12b5e15afaf98f8ac834501d50ddc#9011f7110db12b5e15afaf98f8ac834501d50ddc" source = "git+https://github.com/neondatabase/rust-postgres.git?rev=b25e7f366487f41bc1607e6d824e88996fb02350#b25e7f366487f41bc1607e6d824e88996fb02350"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"byteorder", "byteorder",

View File

@@ -144,11 +144,11 @@ env_logger = "0.10"
log = "0.4" log = "0.4"
## Libraries from neondatabase/ git forks, ideally with changes to be upstreamed ## Libraries from neondatabase/ git forks, ideally with changes to be upstreamed
postgres = { git = "https://github.com/neondatabase/rust-postgres.git", rev="9011f7110db12b5e15afaf98f8ac834501d50ddc" } postgres = { git = "https://github.com/neondatabase/rust-postgres.git", rev="b25e7f366487f41bc1607e6d824e88996fb02350" }
postgres-native-tls = { git = "https://github.com/neondatabase/rust-postgres.git", rev="9011f7110db12b5e15afaf98f8ac834501d50ddc" } postgres-native-tls = { git = "https://github.com/neondatabase/rust-postgres.git", rev="b25e7f366487f41bc1607e6d824e88996fb02350" }
postgres-protocol = { git = "https://github.com/neondatabase/rust-postgres.git", rev="9011f7110db12b5e15afaf98f8ac834501d50ddc" } postgres-protocol = { git = "https://github.com/neondatabase/rust-postgres.git", rev="b25e7f366487f41bc1607e6d824e88996fb02350" }
postgres-types = { git = "https://github.com/neondatabase/rust-postgres.git", rev="9011f7110db12b5e15afaf98f8ac834501d50ddc" } postgres-types = { git = "https://github.com/neondatabase/rust-postgres.git", rev="b25e7f366487f41bc1607e6d824e88996fb02350" }
tokio-postgres = { git = "https://github.com/neondatabase/rust-postgres.git", rev="9011f7110db12b5e15afaf98f8ac834501d50ddc" } tokio-postgres = { git = "https://github.com/neondatabase/rust-postgres.git", rev="b25e7f366487f41bc1607e6d824e88996fb02350" }
## Other git libraries ## Other git libraries
heapless = { default-features=false, features=[], git = "https://github.com/japaric/heapless.git", rev = "644653bf3b831c6bb4963be2de24804acf5e5001" } # upstream release pending heapless = { default-features=false, features=[], git = "https://github.com/japaric/heapless.git", rev = "644653bf3b831c6bb4963be2de24804acf5e5001" } # upstream release pending
@@ -183,7 +183,7 @@ tonic-build = "0.9"
# This is only needed for proxy's tests. # This is only needed for proxy's tests.
# TODO: we should probably fork `tokio-postgres-rustls` instead. # TODO: we should probably fork `tokio-postgres-rustls` instead.
tokio-postgres = { git = "https://github.com/neondatabase/rust-postgres.git", rev="9011f7110db12b5e15afaf98f8ac834501d50ddc" } tokio-postgres = { git = "https://github.com/neondatabase/rust-postgres.git", rev="b25e7f366487f41bc1607e6d824e88996fb02350" }
################# Binary contents sections ################# Binary contents sections

View File

@@ -1,6 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use anyhow::bail; use anyhow::bail;
use anyhow::Context;
use futures::pin_mut; use futures::pin_mut;
use futures::StreamExt; use futures::StreamExt;
use hashbrown::HashMap; use hashbrown::HashMap;
@@ -49,7 +50,7 @@ static HEADER_VALUE_TRUE: HeaderValue = HeaderValue::from_static("true");
// Convert json non-string types to strings, so that they can be passed to Postgres // Convert json non-string types to strings, so that they can be passed to Postgres
// as parameters. // as parameters.
// //
fn json_to_pg_text(json: Vec<Value>) -> Result<Vec<Option<String>>, serde_json::Error> { fn json_to_pg_text(json: &[Value]) -> Result<Vec<Option<String>>, serde_json::Error> {
json.iter() json.iter()
.map(|value| { .map(|value| {
match value { match value {
@@ -230,7 +231,7 @@ pub async fn handle(
// Now execute the query and return the result // Now execute the query and return the result
// //
let result = match payload { let result = match payload {
Payload::Single(query) => query_to_json(&client, query, raw_output, array_mode) Payload::Single(query) => query_to_json(&client, &query, raw_output, array_mode)
.await .await
.map(|x| (x, HashMap::default())), .map(|x| (x, HashMap::default())),
Payload::Batch(queries) => { Payload::Batch(queries) => {
@@ -243,12 +244,18 @@ pub async fn handle(
builder = builder.read_only(true); builder = builder.read_only(true);
} }
let transaction = builder.start().await?; let transaction = builder.start().await?;
for query in queries { for (idx, query) in queries.into_iter().enumerate() {
let result = query_to_json(&transaction, query, raw_output, array_mode).await; let result = query_to_json(&transaction, &query, raw_output, array_mode)
.await
.with_context(|| {
format!("error when executing queries[{}] \"{}\"", idx, query.query)
});
match result { match result {
Ok(r) => results.push(r), Ok(r) => results.push(r),
Err(e) => { Err(e) => {
transaction.rollback().await?; transaction.rollback().await.with_context(|| {
format!("error when rollback queries[{}] \"{}\"", idx, query.query)
})?;
return Err(e); return Err(e);
} }
} }
@@ -278,13 +285,13 @@ pub async fn handle(
async fn query_to_json<T: GenericClient>( async fn query_to_json<T: GenericClient>(
client: &T, client: &T,
data: QueryData, data: &QueryData,
raw_output: bool, raw_output: bool,
array_mode: bool, array_mode: bool,
) -> anyhow::Result<Value> { ) -> anyhow::Result<Value> {
let query_params = json_to_pg_text(data.params)?; let query_params = json_to_pg_text(&data.params)?;
let row_stream = client let row_stream = client
.query_raw_txt::<String, _>(data.query, query_params) .query_raw_txt(&data.query, query_params)
.await?; .await?;
// Manually drain the stream into a vector to leave row_stream hanging // Manually drain the stream into a vector to leave row_stream hanging
@@ -533,22 +540,22 @@ mod tests {
#[test] #[test]
fn test_atomic_types_to_pg_params() { fn test_atomic_types_to_pg_params() {
let json = vec![Value::Bool(true), Value::Bool(false)]; let json = vec![Value::Bool(true), Value::Bool(false)];
let pg_params = json_to_pg_text(json).unwrap(); let pg_params = json_to_pg_text(&json).unwrap();
assert_eq!( assert_eq!(
pg_params, pg_params,
vec![Some("true".to_owned()), Some("false".to_owned())] vec![Some("true".to_owned()), Some("false".to_owned())]
); );
let json = vec![Value::Number(serde_json::Number::from(42))]; let json = vec![Value::Number(serde_json::Number::from(42))];
let pg_params = json_to_pg_text(json).unwrap(); let pg_params = json_to_pg_text(&json).unwrap();
assert_eq!(pg_params, vec![Some("42".to_owned())]); assert_eq!(pg_params, vec![Some("42".to_owned())]);
let json = vec![Value::String("foo\"".to_string())]; let json = vec![Value::String("foo\"".to_string())];
let pg_params = json_to_pg_text(json).unwrap(); let pg_params = json_to_pg_text(&json).unwrap();
assert_eq!(pg_params, vec![Some("foo\"".to_owned())]); assert_eq!(pg_params, vec![Some("foo\"".to_owned())]);
let json = vec![Value::Null]; let json = vec![Value::Null];
let pg_params = json_to_pg_text(json).unwrap(); let pg_params = json_to_pg_text(&json).unwrap();
assert_eq!(pg_params, vec![None]); assert_eq!(pg_params, vec![None]);
} }
@@ -557,7 +564,7 @@ mod tests {
// atoms and escaping // atoms and escaping
let json = "[true, false, null, \"NULL\", 42, \"foo\", \"bar\\\"-\\\\\"]"; let json = "[true, false, null, \"NULL\", 42, \"foo\", \"bar\\\"-\\\\\"]";
let json: Value = serde_json::from_str(json).unwrap(); let json: Value = serde_json::from_str(json).unwrap();
let pg_params = json_to_pg_text(vec![json]).unwrap(); let pg_params = json_to_pg_text(&[json]).unwrap();
assert_eq!( assert_eq!(
pg_params, pg_params,
vec![Some( vec![Some(
@@ -568,7 +575,7 @@ mod tests {
// nested arrays // nested arrays
let json = "[[true, false], [null, 42], [\"foo\", \"bar\\\"-\\\\\"]]"; let json = "[[true, false], [null, 42], [\"foo\", \"bar\\\"-\\\\\"]]";
let json: Value = serde_json::from_str(json).unwrap(); let json: Value = serde_json::from_str(json).unwrap();
let pg_params = json_to_pg_text(vec![json]).unwrap(); let pg_params = json_to_pg_text(&[json]).unwrap();
assert_eq!( assert_eq!(
pg_params, pg_params,
vec![Some( vec![Some(