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
When reader is closed/disposed prematurely, cancel the query #314
We currently have an inefficient situation: if a reader is closed before it its result set has been consumed, the reader will internally process the entire result set to get back to the ReadyForQuery state. If the query returns a huge result set and only one row is processed, this becomes pretty severe.
The fix seems simple: we should simply send a query cancellation to the backend (via a separate connection as mandated by Postgresql), and then continue processing until we get the ReadyForQuery message, as before.
Thanks for explaining @franciscojunior! I didn't read the Postgresql docs carefully enough. If this is so, then my work in #326 needs to be corrected - I changed the code to do the Startup phase even on connectors opened for cancellation. Interestingly all the unit tests passed, I'll look tomorrow to see exactly what is going on and add a commit for this.
Thanks for explaining @franciscojunior! I didn't read the Postgresql docs carefully enough. If this is so, then my work in #326 needs to be corrected - I changed the code to do the Startup phase even on connectors opened for cancellation. Interestingly all the unit tests passed, I'll look tomorrow to see exactly what is going on and add a commit for this.
You are welcome, @roji and thank you for your help and support on this issue.
Additional thought: a Postgresql cancel is a pretty heavy operation (socket connect), with impact on server load as well, so in some cases cancelling on premature reader close may hurt performance rather than improve it.
Still positive on this idea, since it leaves the decision in the user's hand: they can decide to either consume the entire reader, or to prematurely close, implicitly issuing a cancel.
Closing this as the cancel solution seems too heavy. Leaving it the user's responsibility to cancel queries before closing the reader if they so desire.
Activity
roji commentedon Aug 28, 2014
@franciscojunior, I have a quick question regarding cancellation... In https://github.com/npgsql/Npgsql/blob/master/Npgsql/Npgsql/NpgsqlConnector.cs#L866, it seems that when cancelling a new connector is created, rather than going through the connection pool and potentially reusing an existing connector.
Is there a special reason for this? Do you think it would be a good idea to optimize this and go through the pool?
franciscojunior commentedon Aug 28, 2014
Hi, @roji !
Yes, there is a reason. According to the docs: http://www.postgresql.org/docs/devel/static/protocol-flow.html#AEN102903 the frontend sends a CancelRequest message instead of the normal Startup message. So, if a get a connection from the pool, it will already be past the startup message handling.
Also, after the CancelRequest message is processed, the connection is closed by the server.
As the cancel is a very rare operation, and has a very short time life, creating a new connector just for this purpose would be acceptable.
roji commentedon Aug 28, 2014
Thanks for explaining @franciscojunior! I didn't read the Postgresql docs carefully enough. If this is so, then my work in #326 needs to be corrected - I changed the code to do the Startup phase even on connectors opened for cancellation. Interestingly all the unit tests passed, I'll look tomorrow to see exactly what is going on and add a commit for this.
Thanks again...
franciscojunior commentedon Aug 29, 2014
You are welcome, @roji and thank you for your help and support on this issue.
roji commentedon Oct 12, 2014
Additional thought: a Postgresql cancel is a pretty heavy operation (socket connect), with impact on server load as well, so in some cases cancelling on premature reader close may hurt performance rather than improve it.
Still positive on this idea, since it leaves the decision in the user's hand: they can decide to either consume the entire reader, or to prematurely close, implicitly issuing a cancel.
roji commentedon Aug 7, 2015
Closing this as the cancel solution seems too heavy. Leaving it the user's responsibility to cancel queries before closing the reader if they so desire.