Skip to content

When reader is closed/disposed prematurely, cancel the query #314

Closed

Description

@roji

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.

Continuation of discussion in #312.

Activity

  1. added this to the milestone on Aug 9, 2014
  2. self-assigned this
    on Aug 9, 2014
  3. roji commented on Aug 28, 2014

    @roji
    MemberAuthor

    @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?

  4. franciscojunior commented on Aug 28, 2014

    @franciscojunior
    Member

    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.

  5. roji commented on Aug 28, 2014

    @roji
    MemberAuthor

    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...

  6. franciscojunior commented on Aug 29, 2014

    @franciscojunior
    Member

    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.

  7. modified the milestone: on Aug 31, 2014
  8. roji commented on Oct 12, 2014

    @roji
    MemberAuthor

    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.

  9. removed this from the milestone on Mar 23, 2015
  10. roji commented on Aug 7, 2015

    @roji
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions