Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(electric): Use a PG connection pool instead of opening a new connection for every query #500

Closed
wants to merge 12 commits into from

Conversation

alco
Copy link
Member

@alco alco commented Sep 26, 2023

Closes VAX-1035.

@linear
Copy link

linear bot commented Sep 26, 2023

VAX-1035 Implement a connection pool for Postgres connections to use in the WebsocketServer module

We're currently spawning new connections as needed when a new Satellite client connects to the server and needs its LSN and/or initial subscription data fetched from Postgres. A connection pool will allows us to manage this mess by setting the upper bound on the number of concurrent connections used across all Satellite clients.

@alco alco force-pushed the alco/vax-1035-pg-connection-pool branch 2 times, most recently from 43d300f to 30ebc03 Compare September 27, 2023 08:52
@alco alco requested a review from magnetised September 27, 2023 08:53
@alco alco marked this pull request as ready for review September 27, 2023 08:53
Copy link
Contributor

@magnetised magnetised left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I've found the cause of the new issues


:error ->
conn_config
|> Connectors.get_connection_opts(replication: false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this replication: false may be causing problems. previously the default connection params would include create a connection in replica mode whereas with this pool approach you're always connecting in non-replica mode.

end
) do
defp start_subscription(%State{origin: origin, repl_config: rep_conf}) do
ConnectionPool.checkout!(origin, fn conn ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I think this is where the replication: false bites you. might be an argument that you don't need the connection pool here as this is a long-running connection. it's the transient connections, which are all replication: false, which need the pool

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that you mention this, the replication option has slipped my mind while I was working on the pool. Unfortunately, I don't think that's the problem here. Both tyimes PostgresManager needs to connect to the DB, it runs regular SQL statements: ALTER SUBSCRIPTION, SELECT. The long-running replication connection is set up in LogicalReplicationProducer.init(), I haven't touched that code.

I took a closer look at the failing E2E tests. There are two types of recurring issues there: (1) missing output to match on (but I think it's the test code that's wrong there) and (2) "Received subscribe response for unknown subscription" error.

The former must have been caused by different scheduling of concurrent processes due to the introduction of the connnection pool, I can fix that in the test code. The latter is an actual new problem I need to look more closely at. Probably caused by the same connection being used to run different queries.

@@ -181,14 +185,14 @@ defmodule Electric.Replication.PostgresConnectorMng do
"Attempting to initialize #{origin}: #{conn_config.username}@#{conn_config.host}:#{conn_config.port}"
)

Client.with_conn(conn_config, fn conn ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is replication: true but I'm not sure it needs to be... there's things we weren't able to do in the extension migration stuff because the connection was replica: false but not sure if turning off replication causes other problems

@alco alco force-pushed the alco/vax-1035-pg-connection-pool branch from 83f9876 to c205c07 Compare October 2, 2023 13:26
@alco alco closed this Jan 4, 2024
@alco alco deleted the alco/vax-1035-pg-connection-pool branch August 7, 2024 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants