cjbottaro

cjbottaro

Hello,

I’m building a custom database connection pool with some funky requirements (see here). The end result is that a single repo’s pool will be connecting to different databases. This make the query caching unhappy:

** (ArgumentError) query %Postgrex.Query{columns: [“id”, “name”], copy_data: false, decoders: [function<6.39073818/1 in Postgrex.Types.decoder/2>, function<6.39073818/1 in Postgrex.Types.decoder/2>], encoders: [function<7.39073818/1 in Postgrex.Types.encoder/2>], name: “”, null: nil, param_formats: [:binary], result_formats: [:binary, :binary], statement: “SELECT c0."id", c0."name" FROM "clients" AS c0 WHERE (c0."id" = $1)”, types: 110635} has invalid types for the connection

My question is how to turn off query caching? Or more low level, how to make Postgrex not check the types field in the query and connection structs?

Thanks for the help.

Showing Posts 1 to 5

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I feel like the normal Ecto approach here is to have one Repo module per database. What about that approach won’t work here?

cjbottaro

cjbottaro OP

500+ databases and they are created dynamically. Give them each a pool of 20 connections and then combine that with we have over 100 web/worker servers, that’s too many connections for pgBouncer.

So given that, I want to try the idea of an LRU connection cache (that implements the DBConnection.Pool behavior so it can be used with Ecto).

And it generally works, except for the aforementioned issue. Fishcakez offered a workaround, which works (but needs a code review) and I’ll post about in a little bit.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Interesting, would love to see what the solution turns out to be.

cjbottaro

cjbottaro OP

Here’s the solution (and a description of the problem)…

So the first time a query is run Ecto prepares the query which stores type information in the query struct, then subsequently when that query is executed, it uses that cached type info and can execute directly instead of preparing first.

Here’s the relevant code in Ecto.Adapters.Postgres.Connection:

def prepare_execute(conn, name, sql, params, opts) do
  query = %Postgrex.Query{name: name, statement: sql}
  DBConnection.prepare_execute(conn, query, params, opts)
end

def execute(conn, sql, params, opts) when is_binary(sql) do
  query = %Postgrex.Query{name: "", statement: sql}
  case DBConnection.prepare_execute(conn, query, params, opts) do
    {:ok, _, query} -> {:ok, query}
    {:error, _} = err -> err
  end
end

def execute(conn, %{} = query, params, opts) do
  DBConnection.execute(conn, query, params, opts)
end

So the solution is to change those functions to simply discard the cached query info in the call to prepare_execute, then modify the other execute functions to always call prepare_execute:

def prepare_execute(conn, name, sql, params, opts) do
  query = %Postgrex.Query{name: "", statement: sql}
  { :ok, result } = execute(conn, query, params, opts)
  { :ok, query, result }
end

def execute(conn, sql, params, opts) when is_binary(sql) do
  query = %Postgrex.Query{name: "", statement: sql}
  execute(conn, query, params, opts)
end

def execute(conn, %{} = query, params, opts) do
  { :ok, _query, result } = DBConnection.prepare_execute(conn, query, params, opts)
  { :ok, result }
end

Notice that prepare_execute returns the initial, almost bare %Postgrex.Query{} struct without any cached query info.

Obviously the downside to this is that every query now has to hit the db twice since we’re no longer saving any of the info returned by prepare_execute.

Huge thanks to @fishcakez to helping me with this; saving me many hours of trying to figure it out on my own.

There is still one last problem. Those modified functions live in Ecto.Adapters.Postgres.Connection, so it’s not just a matter of making a new Ecto.Adapters.PostgresWithoutCache adapter; I have to make both a new adapter module and connection module for that adapter. And I’m not sure how to do that cleanly in a functional language. In an OOP language, I could simply inherit new classes and overwrite those specific methods… but how do something similar in Elixir?

I want to make a new Adapter module and new Connection module that are exactly the same except for those three methods.

Thanks for the help.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Is there no way to cache based on the database?

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews