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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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
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
Interesting, would love to see what the solution turns out to be.
cjbottaro
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:So the solution is to change those functions to simply discard the cached query info in the call to
prepare_execute, then modify the otherexecutefunctions to always callprepare_execute:Notice that
prepare_executereturns 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 newEcto.Adapters.PostgresWithoutCacheadapter; 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
Is there no way to cache based on the database?