Skip to content

Implement MARS (Multiple Active Result Set) #462

Description

@franciscojunior

Hi all!

I'm opening this issue so we can discuss about how we could implement MARS in Npgsql.

@bricelam gave us a heads up about the requirement of MARS by Entity Framework 7. #249 (comment)

I was reading the protocol documentation and there it says:

If Execute terminates before completing the execution of a portal (due to reaching a nonzero result-row count), it will send a PortalSuspended message; the appearance of this message tells the frontend that another Execute should be issued against the same portal to complete the operation.

Would it be possible to have many portals open at the same time and send execute messages separately?

Something like this:

while (dr.Read())     <----- First portal suspended (read 10 first rows for example)
{
     blahblah()
     while dr2.Read()   <---- Execute a second portal
     {
     }
}

Activity

  1. Emill commented on Jan 26, 2015

    @Emill
    Contributor

    Well, yes, but then both queries will run in the same transaction. Is that a problem? But how do we know how many rows we should fetch?

    Why don't EF simply open another connection? That seems the most reasonable thing to do...

  2. franciscojunior commented on Jan 26, 2015

    @franciscojunior
    MemberAuthor

    Well, yes, but then both queries will run in the same transaction. Is that a problem? But how do we know how many rows we should fetch?

    Today Npgsql pass a value of 0 to the Execute message to indicate that all rows should be returned. We could add support to enable user to specify a value for that. Or maybe we could add a connection string parameter specifically to say if MARS should be supported. This way we could specify a value or let the user specify one.

    Regarding the transaction support, it is a good question. I don't know which semantics sql server uses. If it lets you specify another transaction or not.

    Why don't EF simply open another connection? That seems the most reasonable thing to do...

    That's a very good question. One of the reasons I can think of is that the second query has to run inside the same transaction as the first one.

  3. bricelam commented on Jan 26, 2015

    @bricelam

    If it's not implemented in the ADO.NET provider then yes, you should be able to work around it by opening multiple connections in the EF provider (assuming transactions can be distributed over multiple connections since the unit of work may not yet be committed).

  4. franciscojunior commented on Jan 26, 2015

    @franciscojunior
    MemberAuthor

    If it's not implemented in the ADO.NET provider then yes, you should be able to work around it by opening multiple connections in the EF provider (assuming transactions can be distributed over multiple connections since the unit of work may not yet be committed).

    I think we won't be able to use this work around because we can't distribute the transaction over multiple connections :(

  5. franciscojunior commented on Jan 26, 2015

    @franciscojunior
    MemberAuthor

    Isn't that what https://github.com/npgsql/npgsql/wiki/User-Manual#systemtransactions-support is supposed to solve?

    Hmmmm, that's true. I've completely missed that. I was only thinking about postgresql support itself.

    If I recall correctly, I think this is only supported when running on windows. But I think this could be a possible solution.

    Maybe @jbcooley can give us more information about it.

  6. roji commented on Jan 27, 2015

    @roji
    Member

    A bit late to the discussion but here's what I think...

    We previously discussed providing MARS support in #312. It's definitely possible to do something with cursors, like @franciscojunior suggested above, but it's anything but trivial. The main point seems to be performance - once you use cursors, you're doing a roundtrip to the server every time you want another result row (it's possible to get several rows and to cache them, but what happens with SequentialAccess mode...). Like @Emill said, PostgreSQL supports only one transaction at a time so concurrent commands in different transactions isn't possible.

    @franciscojunior, Npgsql does provide distributed transaction support, and I'm pretty sure it works well on mono (I even submitted a mono patch some time ago). There's definitely some cleanup and work to be done there, I was thinking of working on that after the big 3.0 rewrite.

    Since MARS isn't a standard ADO.NET feature (but rather an SqlClient/SqlServer feature), it may be worth checking the EF7 provider approach in other database providers - how does the Mysql, Sqlite, Oracle drivers approach this questions?

    In any case, for the purposes of EF7 I'd definitely assume there's no MARS support and go with multiple connections (if that's actually necessary - can we maybe have an example?). Assume that distributed transactions will work, and if you find any issues we'll fix them.

  7. franciscojunior commented on Jan 27, 2015

    @franciscojunior
    MemberAuthor

    @franciscojunior, Npgsql does provide distributed transaction support, and I'm pretty sure it works well on mono (I even submitted a mono patch some time ago). There's definitely some cleanup and work to be done there, I was thinking of working on that after the big 3.0 rewrite.

    Ahh, that's great, @roji ! I wasn't aware of this. Thanks for the heads up! :)

  8. jbcooley commented on Jan 27, 2015

    @jbcooley
    Contributor

    I don't have much of a stake in this, but I'd prefer to see Npgsql develop features that users wanted rather than limiting the implementation to the ADO.NET interface. There are going to be enough people migrating between platforms that easing migration should be an important factor when considering features.

    I think a switch to use server cursors would be a useful feature. When this discussion started up again, I read a bit on how SQL Server implements this. They do something similar by processing data in chunks. It's not a free feature, and must be turned on.

    With that in mind, I think this feature should be considered and prioritized appropriately rather than discarded as not a feature of core ADO.NET

  9. roji commented on Jan 27, 2015

    @roji
    Member

    @jbcooley, I may have been too harsh on this. I don't mean we should discard this feature or any other feature just because it isn't part of ADO.NET. One good example is support for PostgreSQL enums which @Emill and I worked on although they're definitely not part of ADO.NET.

    The problem with MARS is that it's really not a trivial feature... I'm not really sure but I think it's going to require quite a bit of thinking to get this working and efficient, without any sort of backend/protocol support like SqlServer has. In particular, we already have a pretty enormous rewrite on our hands for v3.0 even without considering MARS. And since EF7 support seems to be a generally high-priority work item, it would be better if we could support it without the dependency on MARS.

    So personally I'm really not against MARS in any way, it's just that I don't think this is the right time to jump in and I'd like to see EF7 support move forward regardless of it...

    BTW I somehow thought MARS was now a "standard" feature that doesn't need to be paid for (or turned on). Any light you can shed on exactly how it's implemented (how chunking occurs, how the interaction with SequentialMode exactly occurs) would help.

  10. josh-cooley commented on Jan 28, 2015

    @josh-cooley

    @roji

    There's some good information on MSDN on how MARS works.
    https://msdn.microsoft.com/en-us/library/ms131686.aspx

    I don't believe you have to pay for it, but they do say you have to turn it on in the connection string. "By default, MARS functionality is not enabled."

    I believe certain functionality is disabled when MARS is enabled. I suspect SequentialMode would not be strictly followed. This quote leads me to believe it would work like what's being proposed with cursors. "The MARS infrastructure allows multiple batches to execute in an interleaved fashion, though execution can only be switched at well defined points. In addition, most statements must run atomically within a batch. Statements which return rows to the client, which are sometimes referred to as yield points, are allowed to interleave execution before completion while rows are being sent to the client"

  11. roji commented on Jan 30, 2015

    @roji
    Member

    Thanks for the info @jbcooley. We should definitely look into this seriously for v3.1.

    One potentially negative sentence is "Server cursors should be used for longer lived or large result sets generated by single SQL statements.", i.e. with SqlClient/SqlServer you have the option to either do MARS or do server cursors. This may mean it's not a good idea to implement MARS on top of server cursors in Npgsql (although my understanding of the whole thing is really very bad at the moment).

    Regarding the EF7 implementation specifically, even if Npgsql does support an optional MARS feature I'm not sure it would be a good idea to have the EF7 provider built on top of it, as choosing MARS seems to have some specific/special consequences (otherwise it would have been on in SqlClient/SqlServer by default with no special parameters)?

    Anyway, let's leave this conversation open for later.

  12. roji commented on May 25, 2015

    @roji
    Member

    Note: MARS isn't a requirement for EF7 - see dotnet/efcore#1665. I'm not sure how efficient the non-MARS implementation is compared to the MARS implementation, but it's something.

    This doesn't mean we shouldn't look into MARS support, just that it's not required for EF7.

  13. 44 remaining items

  14. mblataric commented on Jun 18, 2023

    @mblataric

    @roji It's not about navigation properties nor AutoInclude, it's about their sub-properties and code you need to include in business classes in order to AutoInclude have an effect.

    This code (in docs page it is right after AutoInclude code):

    public class ParentObject : BaseObject {
        // ...
        private NavigationPropertyType _backingField;
    
        [XafDisplayName("Display Name")]
        [VisibleInListView(false)]
        public virtual NavigationPropertyType NavigationProperty { get => _backingField; set => _backingField = value; }
    
        [XafDisplayName("Display Name")]
        [VisibleInListView(true), VisibleInDetailView(false)]
        public string NavigationPropertyName { get => _backingField?.Name; }
    }

    First property is navigation property, but second is string, navigationProperty.Name.

    So let's put this in something less abstract.
    You have tables: Country, Municipality, Post, City.

    Some customers want to see City.Municipality.Country.Name, some want City.Post.Number, some want City.Municipality.TaxCode, all want City.Name.

    Now, they might want this on outgoing Invoices, but also on Incoming Invoices and some also want this in Booking.
    Some want it in Salary, some in utility charges and so on.

    In order to cover only this, I need to add 4 additional properties to all business classes where customers might want this data.
    And when customers need another field? Add that as well to all classes.

    Now this is only one set of data, there are several dozen others which are also used in several classes and you have hundreds of additional properties added through several dozen classes.

    If AutoInclude alone would solve it, I would haver wrote a comment in the first place.

  15. roji commented on Jun 19, 2023

    @roji
    Member

    That second part in the page seems to amount to having a backing field and two properties for your navigations, instead of a single property; while it's indeed verbose and not perfect, that also seems quite reasonable... I understand you have lots of properties/customers with differing requirements, but unless I'm mistaken you already have to cater for their different needs by having those tailored navigations; this only seems to make those navigations slightly more verbose...

    To summarize, I do understand the frustration here, but as in most other cases with people asking for MARS, there does seem to be a workaround (even if laborious) on the application side for making things work without it.. I'm not sure if you have other options here (given that MARS isn't available in Npgsql), if not I'd at least consider this.

  16. mblataric commented on Jun 19, 2023

    @mblataric

    @roji - Not two properties, but an additional property for each field I need from navigation property. It could be one, it could be ten per navigation property per class where that navigation property is used.
    For instance, Company or Person has Name/LastName, FirstName, VAT/ID, Street, City, Post, BankAccount, Contact and so on.
    So, just in case of Company in Invoice I need 8 properties, plus, I need additional properties in Street, City, Post and BankAccount for each field I need to pull from those classes - and it is rarely one - for instance, for BankAccount I need to pull bank code, bank name, currency and bank account number and so on, and so on.
    And then the same in IncomingInvoice and same in BankStatement and same in UtilityCharge and same in .... multiplied by all other combinations .... seriously?

    Come one, verbose and laborious is a bit of an understatement here :-)

    Obviously, correct solution would be that XAF did not use MARS (and used other methods to solve whatever they solved with MARS), since it is only properly implemented in SQL Server and they at least had intention to support other databases even if they are doing rigorous testing only on SQL Server - but they did not and I doubt they will rewrite a framework now.

    I can not affect queries, only write hundreds of additional properties and remove ability for customer to select fields they need at runtime - a feature quite often used in our use case, btw.
    So, kind of last resort was solution at database provider, even if results in degraded performance.

    Anyway, thanks for taking the time to share your input.
    It seems I am a rare species regarding this, both here and at DevExpress, since there aren't a flood of comments regarding this issue nor here nor at DevExpress support centre.
    Mentioned workaround is simply not viable in large business app (I would go for it in small utility app), so I guess I am stuck with Sql Server with this one.

  17. roji commented on Jun 19, 2023

    @roji
    Member

    OK, I guess I misunderstood - yeah, that does sound pretty bad then.

  18. mblataric commented on Jun 21, 2023

    @mblataric

    @roji Now you understand why I would be more than happy with non-ideal solution like increased memory consumption / reduced performance, since my app is 64-bit and customers have 8GB RAM minimum and they have no issues waiting a second or two longer, instead of getting an error.

  19. springy76 commented on Aug 1, 2024

    @springy76
  20. roji commented on Aug 4, 2024

    @roji
  21. JM-Dirtlegal commented on May 19, 2025

    @JM-Dirtlegal
  22. vonzshik commented on May 19, 2025

    @vonzshik
  23. added theissue type on Jun 14, 2025
  24. xiyoujiyjy commented on Feb 11, 2026

    @xiyoujiyjy
  25. roji commented on Feb 11, 2026

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions