48

I'd like to better understand Connection Pooling when using Npgsql for Postgres. (http://www.npgsql.org/)

When I use the connection string:

UserID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;Pooling=true;Minimum Pool Size=0;Maximum Pool Size=100;

Where is the "Pooling" going to take place? On my application server or on the database?

When I call connection.Open(), what happens? Is a connection taken from the pool if one exists and if not, a pool is created?

Any other general info around Connection Pooling would be appreciated.

Thanks.

1

1 Answer 1

73

Npgsql connection pooling is implemented inside your application process - it has nothing to do with PostgreSQL, which is completely unaware of it.

The mechanism is very simple. When you close a pooled connection, instead of physically closing the connection to PostgreSQL the physical connection is kept around idle in memory (in a "pool"). The next time you open a new connection, if its connection string matches a physical connection already present in the pool, that physical connection is reused instead of opening a new physical connection.

Since opening/closing physical connections is an expensive process, this considerably speeds up your application.

Sign up to request clarification or add additional context in comments.

29 Comments

@ignacio yes, it's understood within the context of this conversation that "physical connection" refers to a TCP socket connection (which is very expensive to create) as opposed to a pooled connection (which is very lightweight).
@ShayRojansky thank you for your reply and please, let me congratulate you because of the great job you do with Npgsql. If anyone want to go deeper in how a connection is build can check this link postgresql.org/docs/11/protocol-flow.html . That link describes the process from the PostgreSQL server point of view but we can get an idea of why opening a connection is expensive. We have to also add the tasks the operating systems have to do to open, maintain and close the TCP connection (through sockets).
That's right. Each connection string has its own pool, and the MaxPoolSize is part of that connection string, so also part of that pool's setting. Different pools (and connection strings) can have different maximum pool sizes. So you may have DB1 with MaxPoolSize=5, and DB2 with MaxPoolSize=100, coming up to a total have 105 physical connections in total.
What happens to "set" variables postgresql.org/docs/current/sql-set.html? Are they unique or re-used from the pool? Say I open a connection connection1 and set a var to "val1". Then I open 2 other connections, both of them re-use connection1. Then I set "val2" and "val3" for the variable in different instances. Is it safe or there might be a race-condition?
@Serg046 by default, when a physical connection is returned to the pool, a DISCARD ALL command is enqueued which will be executed the next time that connection is used; this resets all parameters (and other connection state) to their defaults, as if a new physical connection was opened. This prevents state leakage through the pool.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.