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
{{ message }}
Repository navigation
NpgsqlCommand.Dispose() should execute "DEALLOCATE <name>" for a prepared command #158
NpgsqlCommand supports Prepare() and I think it should execute "DEALLOCATE " in its Dispose() method.
Currently there is no NpgsqlCommand.Dispose() method at all.
Preparing of many commands will pump up the server process memory and closing the connection afterwards takes very long!
Below is an example program...
(It was tested with Win7/64bit, PostgreSQL 9.2.6, .NET 4.0, VS2012, Npgsql/master current commit 6407cc3.)
In my case disposing of connection takes 14s. But with DEALLOCATE work around it takes only 2ms.
usingNpgsql;usingSystem;usingSystem.Diagnostics;classProgram{staticvoidMain(string[]args){varsw=Stopwatch.StartNew();using(varcon=newNpgsqlConnection("Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests;syncnotification=false")){con.Open();for(inti=0;i<30000;i++){using(varcmd=con.CreateCommand()){cmd.CommandText="SELECT 0";cmd.Prepare();cmd.ExecuteScalar();}// activating this DEALLOCATE command works around missing deallocation of prepared command/* using (var cmd = con.CreateCommand()) { cmd.CommandText = "DEALLOCATE ALL"; cmd.ExecuteNonQuery(); } */}Console.WriteLine(sw.Elapsed);sw.Restart();}Console.WriteLine(sw.Elapsed);Console.ReadLine();}}
I think this issue didn't appear before because we are always asking users to use the pattern of open the connection, use it and then close it. This way, it isn't normal to accumulate too much prepared statements.
Another point is that we discouraged the use of prepared statements because they had poor performance. Now that Glen added a lot of optimizations, I think more and more prepared statements will be used and this issue may start to be noticed.
Thank you very much, Udo, for bringing our attention to this. We will work on it for the 2.2 release. I'll add a milestone to indicate that.
Implementation note to self: we can't do any sort of database interaction from with Dispose() (i.e. DEALLOCATE) since when executed from the finalizer (can be whenver) this causes contention on the database connection with whatever query is actually being executed.
Can't lock either, since any sort of blocking within a finalizer is a big no-no.
So need to push the DEALLOCATE to a queue to be processed at the earliest convenience. It would be nice to be able to check, from Dispose(), whether the connection is in use (TryLock) and have a fast-path, but there's no such lock currently in Npgsql. This mechanism would also be relevant for #270.
Activity
franciscojunior commentedon Feb 6, 2014
I think you have a good point here.
I think this issue didn't appear before because we are always asking users to use the pattern of open the connection, use it and then close it. This way, it isn't normal to accumulate too much prepared statements.
Another point is that we discouraged the use of prepared statements because they had poor performance. Now that Glen added a lot of optimizations, I think more and more prepared statements will be used and this issue may start to be noticed.
Thank you very much, Udo, for bringing our attention to this. We will work on it for the 2.2 release. I'll add a milestone to indicate that.
roji commentedon Jul 27, 2014
Guys, this is a pretty important item, I'll take a look at it, hopefully it will make it into 2.2.
roji commentedon Jul 27, 2014
Implementation note to self: we can't do any sort of database interaction from with Dispose() (i.e. DEALLOCATE) since when executed from the finalizer (can be whenver) this causes contention on the database connection with whatever query is actually being executed.
Can't lock either, since any sort of blocking within a finalizer is a big no-no.
So need to push the DEALLOCATE to a queue to be processed at the earliest convenience. It would be nice to be able to check, from Dispose(), whether the connection is in use (TryLock) and have a fast-path, but there's no such lock currently in Npgsql. This mechanism would also be relevant for #270.
Unit test for #158 (prepared stmt leak)
Deallocate prepared statement on Dispose()