You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just found that the default behaviour of NpgsqlConnection is unexpected. I wrote the following test with SQL Server to demonstrate this:
Console.WriteLine("Start: " + DateTime.UtcNow.ToString("O"));
for (int i = 0; i < 1000000; i++) // one million times!
{
using (var connection = new SqlConnection("remote server, conn. pooling ON"))
{
connection.Open();
}
}
Console.WriteLine("End: " + DateTime.UtcNow.ToString("O"));
Console.ReadLine();
If you execute the code above with SqlConnection it will end within 10 seconds. That's because SqlConnection will create a connection to Sql Server only once. When I open a connection again, it don't connect to the server again, but just get a connection from connection pool. This is behaviour I expect when connection pooling is enabled. If you open windows task manager (CTRL+SHIFT+ESC), then you can see, that no data will be sent to sql server when a connection is opening (except first time).
Then I wrote exact the same test for NpgsqlConnection. It opens connections very slow compared to SqlConnection - only 15 connections per second. It also ping database on each connection creation and that's make connection pooling almost useless. If you open windows task manager (CTRL+SHIFT+ESC), you will see how NpgsqlConnection transfer data all the time when the code is running.
I really don't see why NpgsqlConnection need to send some data to postgresql when connection pooling is on. This makes it so much slower.
Agree with @Emill. I just ran this on Postgresql and can clearly see from the backend logs that the only thing happening is the connection cleanup statements.
In a nutshell, the problem is that when we return a connection to the pool, we issue a set of "reset" statements to release any allocated resources (locks, prepared statements...) and to bring it back to its initial state. This is to prevent user activities from one usage session to leak into another.
However, we currently execute the cleanup synchronously, meaning that when you Close() the connection your thread blocks until the database acknowledges that the cleanup has completed. This is the source of the big wait. The solution in #268 is to cleanup asynchronously (Close() returns immediately), and only add the connection back to the pool when cleanup has completed.
This is definitely an important issue for the next release. Closing as duplicate of #268.
Activity
Emill commentedon Aug 9, 2014
This is kind of a duplicate of #268
roji commentedon Aug 9, 2014
Agree with @Emill. I just ran this on Postgresql and can clearly see from the backend logs that the only thing happening is the connection cleanup statements.
In a nutshell, the problem is that when we return a connection to the pool, we issue a set of "reset" statements to release any allocated resources (locks, prepared statements...) and to bring it back to its initial state. This is to prevent user activities from one usage session to leak into another.
However, we currently execute the cleanup synchronously, meaning that when you Close() the connection your thread blocks until the database acknowledges that the cleanup has completed. This is the source of the big wait. The solution in #268 is to cleanup asynchronously (Close() returns immediately), and only add the connection back to the pool when cleanup has completed.
This is definitely an important issue for the next release. Closing as duplicate of #268.