0
votes

I have a few thousand databases. I want to connect to each of them in series one by one and issue a query. I do this by starting a Postgrex process like this for each one.

  {:ok, pid} =
    Postgrex.start_link(
      port: database.port,
      hostname: database.host,
      username: database.username,
      password: database.password,
      database: database.database_name
    )   

I then issue a Postgrex.query, and then stop it like this

  :ok = GenServer.stop(pid, :normal)

Everything seems to work fine, except that I end up with thousands of Postgrex.TypeServer processes eating up memory that don't seem to get cleaned up for quite a while.

Is there a better way to clean up a Postgrex process so that the TypeServer is also stopped?

I'm on Postgrex 0.13.3.

EDIT:

To clarify things a bit, I'd like to clean up the TypeServer after each Postgrex process is stopped. Cleaning up all the TypeServers after the entire Enum.map is done is not all that useful to me because it results in memory slowly growing followed by a sharp drop rather than a flat line.

Enum.map(databases, fn database ->
  {:ok, pid} = Postgrex.start_link(port: database.port, hostname: database.host, username: database.username, password: database.password, database: database.database_name)   
  Postgrex.query!(pid, "some query", [])
  :ok = GenServer.stop(pid, :normal)
  # something here to clean up the TypeServer
end)
1

1 Answers

0
votes

I did not dig it till the very bottom, but Postgrex.TypeServers are managed by dynamic Postgrex.TypeSupervisor, that is luckily started with a hard-coded name.

So my wild guess would be the following should do:

DynamicSupervisor.stop(Postgrex.TypeSupervisor)

Also, shutting down Postgrex.App should also help

Application.stop(:postgrex)