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

Is there a connection pool for postgresql? #671

Closed
bayueqiankong opened this issue Apr 3, 2023 · 7 comments
Closed

Is there a connection pool for postgresql? #671

bayueqiankong opened this issue Apr 3, 2023 · 7 comments

Comments

@bayueqiankong
Copy link

bayueqiankong commented Apr 3, 2023

I will use libpqxx for my web server with multithreading,and I want to reduce the overhead of database connections.
Is there a API connection pool for postgresql?

@tt4g
Copy link
Contributor

tt4g commented Apr 3, 2023

Duplicate #444, #229

The pooling feature is not supported by libpqxx.
Use a connection pooling library such as pgpool2.

@bayueqiankong
Copy link
Author

What should I do quickly with multithreading,if not use connection pool,
If create a new connection to postgresql wheneverI deal a task ,that is expensive to obtain.
If multiple threads share a connection to postgresql ,that need to lock ?But some threads are waiting,it is not good idea.

@KayEss
Copy link
Contributor

KayEss commented Apr 3, 2023

Depending on how you manage the work in your threads (i.e. if one thread is only ever doing one thing at a time) then you could use a thread_local connection

@tt4g
Copy link
Contributor

tt4g commented Apr 3, 2023

To begin with, there are TCP timeout and connection timeout limits.
To avoid connection timeouts in the thread pool, each connection must periodically send a meaningless query to the database so that it does not time out. This is not a small costly task.
Once you understand this, you should consider whether you really must use connection pooling with multi-threading.

If a thread accesses the connection pool many times, the cost of acquiring a lock may exceed the cost of connection creation.
If a connection is to be accessed many times, performance may be improved by creating a single connection in a thread, as @KayEss suggests.

The only thing that connection pooling will definitely improve is the total number of connections to the database server. If you know that the max number of connections to the database is a small number, you should adopt connection pooling.

@KayEss
Copy link
Contributor

KayEss commented Apr 3, 2023

There's also a couple of connection pool implementations that sit between your application and the database, for example pgpool and pgbouncer. These are applications that specialise in this one task and means you don't have to even have to think about the pooling.

@jtv
Copy link
Owner

jtv commented Apr 3, 2023

There's also a couple of connection pool implementations that sit between your application and the database, for example pgpool and pgbouncer. These are applications that specialise in this one task and means you don't have to even have to think about the pooling.

Exactly. This is why libpqxx doesn't have connection pooling built in. You can combine libpqxx with a connection pool. A pool may want to do asynchronous housekeeping, which to me feels like an uncomfortable fit for a library.

@jtv
Copy link
Owner

jtv commented Apr 24, 2023

I suppose we can close this now.

@jtv jtv closed this as completed Apr 24, 2023
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

No branches or pull requests

4 participants