Skip to content

Get rid of the preload reader #312

Closed

Description

@roji

I just confirmed a bug in the preload reader (#223) which I closed by mistake a while ago.

This should be easily fixable, but I was wondering: why do we have this feature at all? The Npgsql manual states:

This results in less performance (especially in the case of very large recordsets, in which case the level of performance could be intolerable), but is left as an option to cover a particular potential backwards-compatibility issue with previous versions of Npgsql

I don't know what backwards-compatibility issue were addressed by this, but I'm not sure it makes sense to retain this functionality... It's more code and maintenance complexity (#223 is an example), there's a class hierarchy with the two readers just because of this. Any user who, for whatever reason, wishes to read all data upfront can do so themselves very easily on their own with the normal Npgsql reader.

Does anyone have an objection to getting rid of this for the next version?

Activity

  1. added this to the milestone on Aug 8, 2014
  2. roji commented on Aug 8, 2014

    @roji
    MemberAuthor

    Sorry, I continued reading the manual and understood the backwards compatibility case - allowing users to use a connection while a reader is still open on it - in violation of the ADO.NET contract.

    I still propose getting rid of this for the next version. We're already considering calling it 3.0 so it would be a good opportunity to get rid of this kind of legacy stuff.

  3. jbcooley commented on Aug 8, 2014

    @jbcooley
    Contributor

    I believe SqlClient provides a similar capability with Mulitple Active
    Result Sets.
    http://msdn.microsoft.com/en-us/library/yf1a7f4f(v=vs.110).aspx

    It's possible this could be done on demand rather than always preloading
    with that setting, but I'd be concerned with removing the feature without a
    good migration path for users.

    Josh

    On Fri, Aug 8, 2014 at 12:03 PM, Shay Rojansky [email protected]
    wrote:

    Sorry, I continued reading the manual and understood the backwards
    compatibility case - allowing users to use a connection while a reader is
    still open on it - in violation of the ADO.NET contract.

    I still propose getting rid of this for the next version. We're already
    considering calling it 3.0 so it would be a good opportunity to get rid of
    this kind of legacy stuff.

    —
    Reply to this email directly or view it on GitHub
    #312 (comment).

  4. roji commented on Aug 8, 2014

    @roji
    MemberAuthor

    If I understand correctly, Multiple Active Result Sets is about actually traversing multiple result sets at the same time, and not about preloading an entire result set at the driver level. In other words, if Postgresql itself provided backend support for multiple result sets we could definitely provide support for it, but it doesn't.

    In the meantime preloading doesn't seems to provide any advantage to users - careless users could accidentally load their entire result sets into memory (as the manual warns).

    I'm not sure what a good migration path here would be. Any code out there that has two readers open and reads from them (against the ADO.NET contract) indeed depends on this feature. I definitely wouldn't propose breakage like this for an ordinary release, but since we're considering releasing a 3.0 this might be the right time. I'd suggest raising an informative exception if the preload connection string is specified - this way the user is informed right away. In addition, any attempt to open a reader while another reader is still open should raise the same informative exception... Some users are sure to grumble but I think long-term it's the only way forward...

  5. jbcooley commented on Aug 8, 2014

    @jbcooley
    Contributor

    I'm not sure this is entirely a server side feature. I seem to remember reading something about SqlDataReader buffering up data when a second command is run.

    That said, perhaps we build up an alternative. Servers side cursors could provide a MARS like feature. But you may be right, and we should just say no to anything that doesn't match the ADO.NET contract.

  6. franciscojunior commented on Aug 8, 2014

    @franciscojunior
    Member

    In the meantime preloading doesn't seems to provide any advantage to users - careless users could accidentally load their entire result sets into memory (as the manual warns).

    Today there is an even more subtle problem: even the non preloader reader ends up reading all the data from the server when the user closes the data reader before using all the data. The biggest difference is that we don't translate the reading to actual data objects, unless the user cancels the command.

    In order to truly fix this, we have to add Postgresql protocol support which enables a client to specify how many rows the server should send to the client each time. This way, if the user closes the reader in the middle of reading, we can simply discard the rest of the data of the "current batch" and then ask the server to not send anymore data.

    Today when we send the Execute message, we pass a value of 0 to the parameter that receives the number of rows to be returned. 0 means everything in this context.

  7. roji commented on Aug 8, 2014

    @roji
    MemberAuthor

    @jbcooley, it would definitely be great if we can provide multiple active result sets if Postgresql supports this. Your idea of using cursors to implement multiple active result sets look interesting indeed, here are some thoughts.

    From reading the Postgresql docs on DECLARE and on FETCH, it seems like the moment you do FETCH the server sends the requested rows without waiting for further input. In other words, you're locked back into the single-reader pattern until you process the FETCH results.

    There is the possibility of using FETCH NEXT for fetching a single row each time. It's definitely possible to map a reader's Next() method to this, and thereby achieve true MARS: advancing the reader one row simply advances its cursor one row in the backend, and waits for the result. The problem with this approach is of course performance: rather than send a single query and then receive its resultset, we roundtrip for every single row.

    So if I understand things correctly, we could offer an additional DataReader class (or option), which would make it operate via single-row FETCH commands. This would allow the user to sacrifice performance (because of round-tripping) for MARS operations. In other words, any two or more MARS-enabled readers can be operated in parallel (but never with a non-MARS-enabled reader).

    Does this sound like a reasonable analysis? Do you think it's worth the implementation effort? It could definitely be an under-the-hood replacement for the current preloader (all readers would be MARS-enabled).

  8. roji commented on Aug 8, 2014

    @roji
    MemberAuthor

    @franciscojunior, if I understand correctly you're discussing an optimization, right? Today, in cases where a user isn't interested in a reader's entire resultset but close the reader before its end, we still have to read the entire resultset and discard the data to get to the end. This could also be solved using the cursor mechanism: we'd FETCH a chunk of rows each time, and if the user closes the reader in the middle we can simply stop and discard the cursor (and have needlessly read at most one chunk).

    While I agree that the current situation is wasteful, there's a balancing act here. Any sort of chunking here would mean an additional roundtrip per chunk; so users who do consume complete resultsets are penalized by the additional message to the server to request the next chunk. I'm not 100% sure what my opinion here is, but I tend to agree that it's worth it...

    Did I describe the situation accurately?

  9. roji commented on Aug 8, 2014

    @roji
    MemberAuthor

    One more comment on cursors: I can't find a way to get a cursor for a prepared statement. Also, this guy claims that using cursors has an effect on the query plan, although that makes no sense to me and I can't find any evidence (assuming you add the NO SCROLL option).

    So this option may carry some risks...

  10. franciscojunior commented on Aug 8, 2014

    @franciscojunior
    Member

    @franciscojunior, if I understand correctly you're discussing an optimization, right? Today, in cases where a user isn't interested in a reader's entire resultset but close the reader before its end, we still have to read the entire resultset and discard the data to get to the end. This could also be solved using the cursor mechanism: we'd FETCH a chunk of rows each time, and if the user closes the reader in the middle we can simply stop and discard the cursor (and have needlessly read at most one chunk).

    Yes, it is a kind of optimization.
    There is a provider, i don't remember the name, which allows you to set a
    size of this set through connection string parameter. I think this would be
    very helpful for large datasets.

    While I agree that the current situation is wasteful, there's a balancing act here. Any sort of chunking here would mean an additional roundtrip per chunk; so users who do consume complete resultsets are penalized by the additional message to the server to request the next chunk. I'm not 100% sure what my opinion here is, but I tend to agree that it's worth it...

    Did I describe the situation accurately?

    Yes. You did. I think that even with this overhead of an additional request
    per chunck, the benefit in memory utilization is a good tradeoff . But I
    think we would need to make some tests to check if this benefit is really
    valid.

    Also note that this support although is similar to cursors, it is made
    available through the protocol itself without any need to create any server
    side cursor. I'm in my phone right now, but later I'll add a reference to
    the line where we make the execute call passing the 0 value. That's where
    we can specify the size of the chunck.

  11. roji commented on Aug 9, 2014

    @roji
    MemberAuthor

    @franciscojunior, I thought about the problem some more and I think I have a better solution - why not simply cancel the query if a user closes the reader prematurely? In other words, when the reader is closed we check if there it has been consumed, and if not, we issue a cancel. Then we continue to consume everything the server sends, just like today - but it will be much shorter.

    Unless I'm missing something, this seems to be the perfect solution to the problem: we don't need to deal with any chunking (cursors or otherwise), which would hurt performance for the normal case (without premature cancellation).

    What do you think? If this seems like a good idea I'll open a separate optimization issue. This discussion isn't related to the preloader/MARS discussion with @jbcooley.

  12. franciscojunior commented on Aug 9, 2014

    @franciscojunior
    Member

    Hi, @roji

    I think this is a very nice solution!
    +1.

    Let's create another issue for this discussion.

  13. roji commented on Aug 9, 2014

    @roji
    MemberAuthor

    OK, I opened #314 to track the cancellation optimization.

    We still have to decide whether we want to attempt to provide multiple active result sets, possibly with server-side cursors as @jbcooley suggested. My gut feeling is that it's going to be non-trivial, as well as a serious performance hit when used (roundtrip per row); it's a non-ADO.NET feature and it doesn't seem to be a very widely-used mode of interaction with databases (I may be wrong here). So I tend to vote towards dropping the preload reader without substituting it in Npgsql 3.0.

  14. 5 remaining items

  15. roji commented on Aug 31, 2014

    @roji
    MemberAuthor

    @jbcooley (and @franciscojunior), are you OK with dropping the preload reader for 3.0?

    We could examine a true MARS-like feature (using cursors) in the future if demand comes up, although it seems like a pretty massive effort/change and I'm not sure we have that much demand for it.

    If you guys are OK with removing the preload, I guess the best thing would be to throw an exception if the preload connection string param is passed. This way the user is immediately alerted to the incompatibility, and refered to a page on our wiki explaining the transition.

    What do you think?

  16. franciscojunior commented on Sep 1, 2014

    @franciscojunior
    Member

    +1 to remove the preload reader.

    +1 to the exception if the connection string param is used.

  17. jbcooley commented on Sep 2, 2014

    @jbcooley
    Contributor

    I'm fine with removing preload, and developing new features based on
    feedback. If we get feedback stating preload is critical, I think we could
    come up with a solution quick enough while the user can remain on the older
    version that does support that.

  18. roji commented on Sep 3, 2014

    @roji
    MemberAuthor

    Great, thanks. I'll do this in the coming days.

  19. changed the title [-]Get rid of the preload reader?[/-] [+]Get rid of the preload reader[/+] on Sep 3, 2014
  20. roji commented on Sep 4, 2014

    @roji
    MemberAuthor

    This is done. It was pretty straightforward so I went ahead and committed directly to master (via a branch), please let me know if you have any suggestions etc.

  21. modified the milestones: , 3.0-beta1 on Mar 30, 2015
  22. modified the milestones: 3.0-beta1, on Aug 7, 2015
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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions