Skip to content

Commit 6abe185

Browse files
debbaNewtTheWolf
andauthored
fix(postgres): read EXPLAIN JSON output as json column (#279)
* fix(postgres): read EXPLAIN JSON output as json column EXPLAIN (FORMAT JSON) returns the plan in a column typed as json, not text, so reading it into a String failed with "error deserializing column 0" and the Explain Plan feature never worked on Postgres. Read the column as a serde_json::Value and re-serialize it for the parser, falling back to a plain String for Postgres-compatible engines that hand the plan back as text. Fixes #276 * Update src-tauri/src/drivers/postgres/explain.rs Co-authored-by: Dominik Spitzli <dominik@spitzli.dev> --------- Co-authored-by: Dominik Spitzli <dominik@spitzli.dev>
1 parent 6db171b commit 6abe185

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src-tauri/src/drivers/postgres/explain.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,18 @@ pub async fn explain_query(
2929
return Err("EXPLAIN returned no output".into());
3030
}
3131

32-
// PostgreSQL returns a single row with a single text column containing JSON
33-
let plan_json_str: String = rows[0].try_get(0).map_err(|e| format_pg_error(&e))?;
32+
// `EXPLAIN (FORMAT JSON)` on real PostgreSQL returns a column of type
33+
// `json` (not `text`), so reading it as `String` fails with
34+
// "error deserializing column 0". Read it as a `serde_json::Value` and
35+
// re-serialize. Some Postgres-compatible engines hand back a plain `text`
36+
// column instead, so fall back to reading the raw string in that case.
37+
let plan_json_str = match rows[0].try_get::<_, serde_json::Value>(0) {
38+
Ok(value) => value.to_string(),
39+
Err(json_err) => rows[0].try_get::<_, String>(0).map_err(|e| {
40+
log::debug!("EXPLAIN json read failed ({json_err}); text read also failed");
41+
format_pg_error(&e)
42+
})?,
43+
};
3444

3545
let mut plan = parse_postgres_json(&plan_json_str)?;
3646
plan.original_query = query.to_string();

0 commit comments

Comments
 (0)