achenet

achenet

sharing postgrex link between different handlers/keeping postgrex link as state using plug

Hello,

I’m building a small REST API using Bandit and Plug, with Postgrex to talk to the database.

I currently have my code set up so each handler will call Postgrex.start_link, get a pid, and then call Postgrex.query! with that pid to interact with the database.

I have two questions:

Is it possible to have just one postgrex link shared between all handlers? Like some sort of shared state that the handlers can use to all use the same pid to interact with the database?

If so, is it better to have one shared pid for all handlers, or create a new link to get a new pid with each request?

First Post!

ruslandoga

ruslandoga

:wave: @achenet

Postgrex.start_link starts a pool of connections that can be named and safely accessed from any process.

iex(1)> Mix.install [:postgrex]
:ok

iex(2)> Postgrex.start_link(pool_size: 5, name: MyPool, database: "example", username: "postgres", password: "postgres", hostname: "localhost", port: 5432)
{:ok, #PID<0.202.0>}

iex(3)> Postgrex.query!(MyPool, "select 1", [], [])
%Postgrex.Result{
  command: :select,
  columns: ["?column?"],
  rows: [[1]],
  num_rows: 1,
  connection_id: 55,
  messages: []
}

If so, is it better to have one shared pid for all handlers, or create a new link to get a new pid with each request?

Depends on your use case. But usually, for small requests that happen often, it’s more efficient to reuse an existing pool of connections.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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 Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement