Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres: switch to query_typed api to avoid prepared statement overhead #5032

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions quaint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ features = [
"with-serde_json-1",
"with-bit-vec-0_6",
]
git = "https://github.com/prisma/rust-postgres"
branch = "pgbouncer-mode"
git = "https://github.com/tmm1/rust-postgres"
branch = "execute-typed"
optional = true

[dependencies.postgres-types]
Expand All @@ -179,13 +179,13 @@ features = [
"with-serde_json-1",
"with-bit-vec-0_6",
]
git = "https://github.com/prisma/rust-postgres"
branch = "pgbouncer-mode"
git = "https://github.com/tmm1/rust-postgres"
branch = "execute-typed"
optional = true

[dependencies.postgres-native-tls]
git = "https://github.com/prisma/rust-postgres"
branch = "pgbouncer-mode"
git = "https://github.com/tmm1/rust-postgres"
branch = "execute-typed"
optional = true

[dependencies.tokio]
Expand Down
127 changes: 68 additions & 59 deletions quaint/src/connector/postgres/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
use async_trait::async_trait;
use column_type::PGColumnType;
use futures::{future::FutureExt, lock::Mutex};
use lru_cache::LruCache;

Check warning on line 27 in quaint/src/connector/postgres/native/mod.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/prisma-engines/prisma-engines/quaint/src/connector/postgres/native/mod.rs
use native_tls::{Certificate, Identity, TlsConnector};
use postgres_native_tls::MakeTlsConnector;
use postgres_types::{Kind as PostgresKind, Type as PostgresType};
use postgres_types::{Kind as PostgresKind, Type as PostgresType, ToSql};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::{
fmt::{Debug, Display},
Expand Down Expand Up @@ -540,29 +540,37 @@
sql,
params,
move || async move {
let stmt = self.fetch_cached(sql, &[]).await?;

if stmt.params().len() != params.len() {
let kind = ErrorKind::IncorrectNumberOfParameters {
expected: stmt.params().len(),
actual: params.len(),
};

return Err(Error::builder(kind).build());
}
let converted_params = conversion::conv_params(params);
let param_types = conversion::params_to_types(params);
let params_with_types: Vec<(&(dyn ToSql + Sync), PostgresType)> = converted_params
.iter()
.zip(param_types)
.map(|(value, ty)| (*value as &(dyn ToSql + Sync), ty))
.collect();

// Execute the query using `query_typed`
let rows = self
.perform_io(self.client.0.query(&stmt, conversion::conv_params(params).as_slice()))
.perform_io(self.client.0.query_typed(sql, params_with_types.as_slice()))
.await?;

let col_types = stmt
.columns()
.iter()
.map(|c| PGColumnType::from_pg_type(c.type_()))
.map(ColumnType::from)
.collect::<Vec<_>>();
let mut result = ResultSet::new(stmt.to_column_names(), col_types, Vec::new());
// Extract column information from the first row, if available
let (col_types, column_names) = if let Some(row) = rows.first() {
let columns = row.columns();
let col_types = columns
.iter()
.map(|c| PGColumnType::from_pg_type(c.type_()))
.map(ColumnType::from)
.collect::<Vec<_>>();
let column_names = columns.iter().map(|c| c.name().to_string()).collect();

(col_types, column_names)
} else {
(Vec::new(), Vec::new())
};

let mut result = ResultSet::new(column_names, col_types, Vec::new());

// Process each row in the result set
for row in rows {
result.rows.push(row.get_result_row()?);
}
Expand All @@ -582,28 +590,35 @@
sql,
params,
move || async move {
let stmt = self.fetch_cached(sql, params).await?;

if stmt.params().len() != params.len() {
let kind = ErrorKind::IncorrectNumberOfParameters {
expected: stmt.params().len(),
actual: params.len(),
};

return Err(Error::builder(kind).build());
}

let col_types = stmt
.columns()
let converted_params = conversion::conv_params(params);
let param_types = conversion::params_to_types(params);
let params_with_types: Vec<(&(dyn ToSql + Sync), PostgresType)> = converted_params
.iter()
.map(|c| PGColumnType::from_pg_type(c.type_()))
.map(ColumnType::from)
.collect::<Vec<_>>();
.zip(param_types)
.map(|(value, ty)| (*value as &(dyn ToSql + Sync), ty))
.collect();

// Execute the query using `query_typed`
let rows = self
.perform_io(self.client.0.query(&stmt, conversion::conv_params(params).as_slice()))
.perform_io(self.client.0.query_typed(sql, params_with_types.as_slice()))
.await?;

let mut result = ResultSet::new(stmt.to_column_names(), col_types, Vec::new());
// Extract column information from the first row, if available
let (col_types, column_names) = if let Some(row) = rows.first() {
let columns = row.columns();
let col_types = columns
.iter()
.map(|c| PGColumnType::from_pg_type(c.type_()))
.map(ColumnType::from)
.collect::<Vec<_>>();
let column_names = columns.iter().map(|c| c.name().to_string()).collect();

(col_types, column_names)
} else {
(Vec::new(), Vec::new())
};

let mut result = ResultSet::new(column_names, col_types, Vec::new());

for row in rows {
result.rows.push(row.get_result_row()?);
Expand Down Expand Up @@ -705,19 +720,16 @@
sql,
params,
move || async move {
let stmt = self.fetch_cached(sql, &[]).await?;

if stmt.params().len() != params.len() {
let kind = ErrorKind::IncorrectNumberOfParameters {
expected: stmt.params().len(),
actual: params.len(),
};

return Err(Error::builder(kind).build());
}
let converted_params = conversion::conv_params(params);
let param_types = conversion::params_to_types(params);
let params_with_types: Vec<(&(dyn ToSql + Sync), PostgresType)> = converted_params
.iter()
.zip(param_types)
.map(|(value, ty)| (*value as &(dyn ToSql + Sync), ty))
.collect();

let changes = self
.perform_io(self.client.0.execute(&stmt, conversion::conv_params(params).as_slice()))
.perform_io(self.client.0.execute_typed(sql, params_with_types.as_slice()))
.await?;

Ok(changes)
Expand All @@ -735,19 +747,16 @@
sql,
params,
move || async move {
let stmt = self.fetch_cached(sql, params).await?;

if stmt.params().len() != params.len() {
let kind = ErrorKind::IncorrectNumberOfParameters {
expected: stmt.params().len(),
actual: params.len(),
};

return Err(Error::builder(kind).build());
}
let converted_params = conversion::conv_params(params);
let param_types = conversion::params_to_types(params);
let params_with_types: Vec<(&(dyn ToSql + Sync), PostgresType)> = converted_params
.iter()
.zip(param_types)
.map(|(value, ty)| (*value as &(dyn ToSql + Sync), ty))
.collect();

let changes = self
.perform_io(self.client.0.execute(&stmt, conversion::conv_params(params).as_slice()))
.perform_io(self.client.0.execute_typed(sql, params_with_types.as_slice()))
.await?;

Ok(changes)
Expand Down
Loading