Skip to content

Commit

Permalink
fix(postgres): sqlx prepare fails if shared_preload_libraries=pg_stat…
Browse files Browse the repository at this point in the history
  • Loading branch information
mrl5 committed Jul 17, 2023
1 parent c70cfaf commit 1d82d6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions sqlx-postgres/src/connection/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,11 @@ WHERE rngtypid = $1

let mut nullables = Vec::new();

if let Explain::Plan(
plan @ Plan {
output: Some(outputs),
..
},
) = &explain
{
nullables.resize(outputs.len(), None);
visit_plan(&plan, outputs, &mut nullables);
if let Explain::QueryPlan(query_plan @ QueryPlan { plan, .. }) = &explain {
if let Some(outputs) = &query_plan.plan.output {
nullables.resize(outputs.len(), None);
visit_plan(&plan, outputs, &mut nullables);
}
}

Ok(nullables)
Expand Down Expand Up @@ -492,15 +488,27 @@ fn visit_plan(plan: &Plan, outputs: &[String], nullables: &mut Vec<Option<bool>>
}

#[derive(serde::Deserialize)]
#[serde(untagged)]
enum Explain {
/// {"Plan": ...} -- returned for most statements
Plan(Plan),
QueryPlan(QueryPlan),
/// The string "Utility Statement" -- returned for
/// a CALL statement
#[serde(rename = "Utility Statement")]
UtilityStatement,
}

#[derive(serde::Deserialize)]
struct QueryPlan {
#[serde(rename = "Plan")]
plan: Plan,
/// present when either pg_stat_statements is loaded and/or compute_query_id is enabled
/// https://www.postgresql.org/docs/current/pgstatstatements.html
/// https://www.postgresql.org/docs/current/runtime-config-statistics.html#GUC-COMPUTE-QUERY-ID
#[serde(rename = "Query Identifier", skip)]
_query_identifier: Option<u64>,
}

#[derive(serde::Deserialize)]
struct Plan {
#[serde(rename = "Join Type")]
Expand Down
2 changes: 1 addition & 1 deletion tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ services:
volumes:
- "./postgres/setup.sql:/docker-entrypoint-initdb.d/setup.sql"
command: >
-c ssl=on -c ssl_cert_file=/var/lib/postgresql/server.crt -c ssl_key_file=/var/lib/postgresql/server.key
-c ssl=on -c ssl_cert_file=/var/lib/postgresql/server.crt -c ssl_key_file=/var/lib/postgresql/server.key -c shared_preload_libraries=pg_stat_statements
postgres_15_client_ssl:
build:
Expand Down

0 comments on commit 1d82d6f

Please sign in to comment.