sorentwo

sorentwo

Oban Core Team

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

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_mode to force_custom_plan in the PostgreSQL config
  • If you use PostgreSQL before version 12, set prepare: :unnamed in 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

sorentwo

Oban Core Team

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

sorentwo

Oban Core Team

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

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement