# `PgRest.SQL.Explain`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/sql/explain.ex#L1)

Wraps a compiled statement in `EXPLAIN`, PostgREST's `explainF`: the plan of the statement a
request would run, for the `application/vnd.pgrst.plan` media type.

    EXPLAIN (FORMAT TEXT | JSON[, ANALYZE][, VERBOSE][, SETTINGS][, BUFFERS][, WAL]) <statement>

The statement and its bind parameters are untouched (`$n` numbering is unchanged), so the plan
shows the values it would run with. Options are a closed set of atoms (`PgRest.MediaType`),
never request text.

## Differences from PostgREST

The explained statement is PgRest's own, so the plan's nodes differ from PostgREST's in
places: a write adds an aggregate over the rows it wrote (and reads its `pgrst_payload`
CTE), a function call reads a `pgrst_rpc` CTE where PostgREST reads `pgrst_source`.

# `spec`

```elixir
@type spec() :: %{
  :format =&gt; :text | :json,
  :options =&gt; [PgRest.MediaType.plan_option()],
  optional(atom()) =&gt; term()
}
```

The `format` and `options` of a `PgRest.MediaType` plan.

# `wrap`

```elixir
@spec wrap({String.t(), [term()]}, spec()) :: {String.t(), [term()]}
```

`{sql, params}` explained. The options follow the order of `spec.options`, which
`PgRest.Request.Accept` fixes to PostgREST's (`analyze`, `verbose`, `settings`, `buffers`,
`wal`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
