Crate diesel_pg_explain

Crate diesel_pg_explain 

Source
Expand description

A lightweight utility for wrapping Diesel queries with EXPLAIN (FORMAT JSON) and parsing the resulting execution plan into a structured Rust type.

This crate is intended for use with PostgreSQL and the Diesel ORM. It provides a simple API to introspect query plans programmatically, enabling query diagnostics, optimization tools, or logging systems.

§Features

  • Wraps any Diesel query using EXPLAIN (FORMAT JSON)
  • Parses the JSON output into a typed ExplainPlan structure
  • Compatible with Diesel’s QueryDsl and RunQueryDsl
  • Deserialization errors are reported as standard Diesel errors

§Example

use diesel::prelude::*;
use diesl_pg_explain::{ExplainWrapped, ExplainPlan};

let connection = &mut establish_connection();
let query = users::table.filter(users::name.like("%example%"));

let plan: ExplainPlan = query.wrap_explain().explain(connection)?;
println!("{:#?}", plan);

§Integration

This crate is best used in development tooling, diagnostics dashboards, or CLI utilities where understanding PostgreSQL query plans is helpful.

Note: this does not run the actual query — it only asks PostgreSQL to generate and return the execution plan.

§See also

§Crate Features

Currently no optional features. May add feature gates for serde or Diesel version in the future.

Structs§

Explain
A wrapper around a Diesel query that transforms it into an EXPLAIN (FORMAT JSON) query.
ExplainPlan
Recursive struct which describes the plan of a query

Traits§

ExplainWrapped
A trait that allows any Diesel query to be wrapped in an EXPLAIN (FORMAT JSON) call using the Explain wrapper.