[Proposal] Use prepared statement in Repo.explain

53 views
Skip to first unread message

Michał Łępicki

unread,
Nov 20, 2023, 5:08:25 AM11/20/23
to elixir-ecto
Hi!

When debugging slow Ecto queries (I’m using postgresql), I recently stopped using Ecto’s Repo.explain. Ecto uses named prepared statements which may use a completely different generic query plan than when running an individual query. A query executed individually in postgresql will be planned together with its specific parameter values in mind, so the plan that Repo.explain returns can sometimes be very different than the plan postgresql will use in practice when using Ecto. As a workaround, instead I copy the query’s SQL debug output, and from psql console I set plan_cache_mode='force_generic_plan', create a prepared statement annotating my argument types, and execute it with EXPLAIN (ANALYZE) EXECUTE mystatement(...).

Maybe to avoid potential differences between Repo.explain plan output, and the plan used in practice, Repo.explain could run the query through the same prepared statements mechanism as regular query execution, and then run EXPLAIN on that prepared statement execution?

Thanks,
- Michał

José Valim

unread,
Nov 20, 2023, 7:53:55 AM11/20/23
to elixi...@googlegroups.com
Good call, a PR to explore this would be welcome!

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/f0c47b5f-3a3f-4471-803a-63defbff2a53n%40googlegroups.com.

benwil...@gmail.com

unread,
Nov 22, 2023, 10:57:16 PM11/22/23
to elixir-ecto

Are you using partitioned tables per chance?

Michał Łępicki

unread,
Nov 23, 2023, 1:24:47 AM11/23/23
to elixir-ecto
> Are you using partitioned tables per chance?

I don't
Reply all
Reply to author
Forward
0 new messages