Skip to content
Merged
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
14 changes: 12 additions & 2 deletions src-tauri/src/drivers/postgres/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,18 @@ pub async fn explain_query(
return Err("EXPLAIN returned no output".into());
}

// PostgreSQL returns a single row with a single text column containing JSON
let plan_json_str: String = rows[0].try_get(0).map_err(|e| format_pg_error(&e))?;
// `EXPLAIN (FORMAT JSON)` on real PostgreSQL returns a column of type
// `json` (not `text`), so reading it as `String` fails with
// "error deserializing column 0". Read it as a `serde_json::Value` and
// re-serialize. Some Postgres-compatible engines hand back a plain `text`
// column instead, so fall back to reading the raw string in that case.
let plan_json_str = match rows[0].try_get::<_, serde_json::Value>(0) {
Ok(value) => value.to_string(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (non-blocking): this is a triple round-trip — PG sends JSON text → tokio-postgres parses into serde_json::Value.to_string() re-serializes → parse_postgres_json parses it again. Totally fine for small EXPLAIN output, just flagging in case parse_postgres_json could grow a &serde_json::Value entry point to skip the re-parse. Not worth it unless it's trivial.

Err(json_err) => rows[0].try_get::<_, String>(0).map_err(|e| {
log::debug!("EXPLAIN json read failed ({json_err}); text read also failed");
format_pg_error(&e)
})?,
};

let mut plan = parse_postgres_json(&plan_json_str)?;
plan.original_query = query.to_string();
Expand Down