Skip to content

Commit 7d646a9

Browse files
committed
fix(postgres): don't fetch ColumnOrigin for transparently-prepared statements
1 parent 28b6450 commit 7d646a9

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

sqlx-postgres/src/connection/describe.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ impl PgConnection {
102102
pub(super) async fn handle_row_description(
103103
&mut self,
104104
desc: Option<RowDescription>,
105-
should_fetch: bool,
105+
fetch_type_info: bool,
106+
fetch_column_description: bool,
106107
) -> Result<(Vec<PgColumn>, HashMap<UStr, usize>), Error> {
107108
let mut columns = Vec::new();
108109
let mut column_names = HashMap::new();
@@ -121,13 +122,13 @@ impl PgConnection {
121122
let name = UStr::from(field.name);
122123

123124
let type_info = self
124-
.maybe_fetch_type_info_by_oid(field.data_type_id, should_fetch)
125+
.maybe_fetch_type_info_by_oid(field.data_type_id, fetch_type_info)
125126
.await?;
126127

127128
let origin = if let (Some(relation_oid), Some(attribute_no)) =
128129
(field.relation_id, field.relation_attribute_no)
129130
{
130-
self.maybe_fetch_column_origin(relation_oid, attribute_no, should_fetch)
131+
self.maybe_fetch_column_origin(relation_oid, attribute_no, fetch_column_description)
131132
.await?
132133
} else {
133134
ColumnOrigin::Expression

sqlx-postgres/src/connection/executor.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async fn prepare(
2525
sql: &str,
2626
parameters: &[PgTypeInfo],
2727
metadata: Option<Arc<PgStatementMetadata>>,
28+
fetch_column_origin: bool,
2829
) -> Result<(StatementId, Arc<PgStatementMetadata>), Error> {
2930
let id = conn.inner.next_statement_id;
3031
conn.inner.next_statement_id = id.next();
@@ -79,7 +80,7 @@ async fn prepare(
7980

8081
let parameters = conn.handle_parameter_description(parameters).await?;
8182

82-
let (columns, column_names) = conn.handle_row_description(rows, true).await?;
83+
let (columns, column_names) = conn.handle_row_description(rows, true, fetch_column_origin).await?;
8384

8485
// ensure that if we did fetch custom data, we wait until we are fully ready before
8586
// continuing
@@ -168,12 +169,13 @@ impl PgConnection {
168169
// optional metadata that was provided by the user, this means they are reusing
169170
// a statement object
170171
metadata: Option<Arc<PgStatementMetadata>>,
172+
fetch_column_origin: bool,
171173
) -> Result<(StatementId, Arc<PgStatementMetadata>), Error> {
172174
if let Some(statement) = self.inner.cache_statement.get_mut(sql) {
173175
return Ok((*statement).clone());
174176
}
175177

176-
let statement = prepare(self, sql, parameters, metadata).await?;
178+
let statement = prepare(self, sql, parameters, metadata, fetch_column_origin).await?;
177179

178180
if store_to_cache && self.inner.cache_statement.is_enabled() {
179181
if let Some((id, _)) = self.inner.cache_statement.insert(sql, statement.clone()) {
@@ -222,7 +224,7 @@ impl PgConnection {
222224
// prepare the statement if this our first time executing it
223225
// always return the statement ID here
224226
let (statement, metadata_) = self
225-
.get_or_prepare(query, &arguments.types, persistent, metadata_opt)
227+
.get_or_prepare(query, &arguments.types, persistent, metadata_opt, false)
226228
.await?;
227229

228230
metadata = metadata_;
@@ -327,7 +329,7 @@ impl PgConnection {
327329
BackendMessageFormat::RowDescription => {
328330
// indicates that a *new* set of rows are about to be returned
329331
let (columns, column_names) = self
330-
.handle_row_description(Some(message.decode()?), false)
332+
.handle_row_description(Some(message.decode()?), false, false)
331333
.await?;
332334

333335
metadata = Arc::new(PgStatementMetadata {
@@ -449,7 +451,7 @@ impl<'c> Executor<'c> for &'c mut PgConnection {
449451
Box::pin(async move {
450452
self.wait_until_ready().await?;
451453

452-
let (_, metadata) = self.get_or_prepare(sql, parameters, true, None).await?;
454+
let (_, metadata) = self.get_or_prepare(sql, parameters, true, None, true).await?;
453455

454456
Ok(PgStatement {
455457
sql: Cow::Borrowed(sql),
@@ -468,7 +470,7 @@ impl<'c> Executor<'c> for &'c mut PgConnection {
468470
Box::pin(async move {
469471
self.wait_until_ready().await?;
470472

471-
let (stmt_id, metadata) = self.get_or_prepare(sql, &[], true, None).await?;
473+
let (stmt_id, metadata) = self.get_or_prepare(sql, &[], true, None, true).await?;
472474

473475
let nullable = self.get_nullable_for_columns(stmt_id, &metadata).await?;
474476

0 commit comments

Comments
 (0)