sorentwo
Disable prepared statements for an individual query
Ecto, through Postgrex and DB Connection, automatically uses prepared statements for all queries. To my knowledge the only way to disable prepared statements is to set prepare: :unnamed in the repo’s global configuration.
This question is prompted by a lingering issue in Oban, wherein a particular query gets dramatically slower once the prepared version of the query is used:
https://github.com/sorentwo/oban/issues/79
- Have others ran into a similar problem?
- Does anybody know a way to disable prepared statements for a specific query?
- Did you work around the issue somehow?
My current thoughts for a workaround are to avoid using Repo.one or Repo.all entirely and instead generate a query, convert that to sql, run Repo.query directly, then convert the result using Repo.load. It isn’t too much work, but it isn’t intuitive and wouldn’t scale for multiple queries.
Most Liked
wanton7
I think I read blog post related to this problem Elixir and Postgres: A Rarely Mentioned Problem | Lainblog
- Use PostgreSQL 12, if possible
- If you use Ecto, set
plan_cache_modetoforce_custom_planin the PostgreSQL config- If you use PostgreSQL before version 12, set
prepare: :unnamedin you Ecto database settings
I think that means when you are using PostgreSQL 12 you can fix this by adding parameters: [plan_cache_mode: "force_custom_plan"] to Ecto config.
Edit: fixed typo paramaters to parameters
sorentwo
Purely on the PG server side. There are a lot of details in the linked issue, but essentially after the fifth request using the prepared query PG’s planner decides to use a generic plan rather than a custom plan for mysterious internal reasons.
Full details are in this comment.
sorentwo
I’m fairly sure those are specific to PG 12 (Oban is 9.6+), and they are global settings. It would be amazing to force a particular plan when preparing the query itself. Maybe it’ll get there one day.
I ended up going with the hack I mentioned in the first post. It doesn’t look too bad, and the performance is as good or better: Avoid prepared statements for all unique queries · oban-bg/oban@756bb4f · GitHub
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









