Skip to content

Introduce async schema APIs on connection and reader #2976

Description

@roji

This is especially important since mutliplexing will (probably) not support sync execution. Afterwards see about proposing this in System.Data.

Activity

  1. added this to the 5.0 milestone on May 23, 2020
  2. self-assigned this
    on May 23, 2020
  3. manandre commented on May 30, 2020

    @manandre
    Collaborator

    Async APIs for data adapter are already in progress on System.Data side via dotnet/runtime#22109
    The implementation seems quite advanced but the API extension must be first reviewed, what, considering the backlog size, can take multiple weeks or even months...
    We have then two solutions:

    • wait/contribute for the delivery of the feature on dotnet side
    • duplicate the System.Data code into Npgsql (if possible), adapt it to async then remove everything once available on dotnet side.

    @roji Which approach are you considering?

  4. YohDeadfall commented on May 30, 2020

    @YohDeadfall
    Contributor

    As said in the issue all API should be reviewed first and implemented later, so we don't know how it will look like. Therefore, we should wait a little and after implement it here even if it won't be a part of .NET 5.

  5. roji commented on May 30, 2020

    @roji
    MemberAuthor

    The proposal in dotnet/runtime#22109 is for DataSet/DataAdapter specifically, I don't see mention of the schema APIs there (here I'm talking specifically about DbConnection.GetSchema and DbDataReader.GetSchemaTable/GetColumnSchema).

    In any case, for these specifically, I don't think there's much need of designing new API shape - it's just about adding async counterparts to existing methods. So while ideally we'd have an approved API first in System.Data.Common, I think in this case it would be OK to go ahead and add here if we need to (as we did with async transaction APIs, for example).

  6. manandre commented on May 31, 2020

    @manandre
    Collaborator

    API proposal

    public sealed class NpgsqlConnection
    {
        public ValueTask<DataTable> GetSchemaAsync();
        public ValueTask<DataTable> GetSchemaAsync(string? collectionName);
        public ValueTask<DataTable> GetSchemaAsync(string? collectionName, string?[]? restrictions);
    }
    
    public sealed class NpgsqlDataReader
    {
        public ValueTask<ReadOnlyCollection<NpgsqlDbColumn>> GetColumnSchemaAsync();
        public ValueTask<DataTable> GetSchemaTableAsync();
    }

    Implementation considerations

    • NpgsqlConnection: sync implementation currently relies on DbDataAdapter.Fill(DataTable) from System.Data.Common which does not exist in async variant yet.
      --> Simple first approach is to wrap sync result into a ValueTask and wire it to DbDataAdapter.FillAsync(DataTable) later, once available.

    • NpgsqlDataReader: sync implementation relies on NpgsqlDataReader.GetFieldValue<T>(int ordinal) which does exist in async variant.
      --> Approach is then to wire async implementation to NpgsqlDataReader.GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken).

  7. roji commented on May 31, 2020

    @roji
    MemberAuthor

    Simple first approach is to wrap sync result into a ValueTask and wire it to DbDataAdapter.FillAsync(DataTable) later, once available.

    Although we can do this, this async-over-sync approach would unfortunately have very little value... My main motivation for proposing this within Npgsql is that multiplexing will (very likely) not allow sync command execution, which would still block us unless we provide a true async implementation here. We could in theory execute these schema queries ourselves and manually read the results into DataTable (bypassing Fill), but I'm not sure this is worth the effort.

    I don't think this issue is very critical - I think it's OK if not everything is supported in multiplexing, and users always have the option of opening a non-multiplexing connection if they need to get a schema. So we can also defer this for later.

  8. bgrainger commented on May 31, 2020

    @bgrainger
    Contributor

    MySqlConnector could implement this API. While most calls to GetSchemaAsync will involve I/O, there are some (e.g., GetSchemaAsync("Keywords")) that can be synchronous, so ValueTask seems justified. (The new DbDataReader methods will both be synchronous for MySqlConnector.)

    Shouldn't it be GetSchemaAsync(string collectionName);? If the collectionName is specified, it must be non-null:

    if (collectionName is null)
    throw new ArgumentNullException(nameof(collectionName));

  9. roji commented on May 31, 2020

    @roji
    MemberAuthor

    so ValueTask seems justified

    It's a valid argument, although for APIs which really aren't perf-sensitive, Task<T> still sometimes makes sense - no needless ValueTask wrapper when the call does complete asynchronously, plus the user can do whatever they want with the returned Task, whereas with ValueTask you never know (multiple awaits, Task.WhenAll etc.). GetSchemaAsync seems pretty low perf to me - especially any GetSchemaAsync call which doesn't perform any I/O (e.g. keywords). So I'd lean towards Task rather than ValueTask.

    I'll open an issue for this on https://github.com/dotnet/runtime soon, to continue the discussion there.

  10. bgrainger commented on May 31, 2020

    @bgrainger
    Contributor

    I almost cited dotnet/runtime#28596 (comment) in my previous comment:

    For the return value of the new proposed methods, our current guiding principle has been to opt for Task when the method in question represents a full database roundtrip operation, where any perf advantages of ValueTask are likely to be dominated by networking etc.

    I have no objection to Task<DataTable> GetSchemaAsync; the usability benefits of Task may be worth it, and DataTable is not a high-performance API in the first place.

    Whether Task or ValueTask is used in the DbDataReader methods, I'd likely have the MySqlConnector documentation recommend that the synchronous method be used since there's no concrete benefit (in that library) to using the async method; I already do that for IsDbNullAsync and GetFieldValueAsync.

  11. roji commented on May 31, 2020

    @roji
    MemberAuthor

    and DataTable is not a high-performance API in the first place

    Yep, agreed.

    Whether Task or ValueTask is used in the DbDataReader methods, I'd likely have the MySqlConnector documentation recommend that the synchronous method be used since there's no concrete benefit (in that library) to using the async method; I already do that for IsDbNullAsync and GetFieldValueAsync.

    Would you actually recommend sync even if async has no disadvantage? The general .NET guidance seems to be to prefer async except when having a good reason to do sync.

    BTW for IsDBNullAsync and GetFieldValueAsync, doing sync always makes sense unless CommandBehavior.SequentialAccess is specified. Not sure if you implement that (i.e. streaming within the row, without holding it in memory at once, because of big columns), if it's implemented these two methods can make sense in async too.

  12. bgrainger commented on May 31, 2020

    @bgrainger
    Contributor

    Would you actually recommend sync even if async has no disadvantage?

    No, not if it was zero-cost (or negligible cost for non-high-performance scenarios). (But GetFieldValueAsync is particularly bad because it has to allocate a Task; at least IsDBNullAsync can reuse a cached Task<bool>.)

  13. roji commented on May 31, 2020

    @roji
    MemberAuthor

    Very true for GetFieldValueAsync, hence dotnet/runtime#25297 (which I hope to have time to work on for 5.0... But getting less sure about that...)

  14. manandre commented on Jun 1, 2020

    @manandre
    Collaborator

    Updated API proposal

    public sealed class NpgsqlConnection
    {
        public Task<DataTable> GetSchemaAsync();
        public Task<DataTable> GetSchemaAsync(string collectionName);
        public Task<DataTable> GetSchemaAsync(string collectionName, string[] restrictions);
    }
    
    public sealed class NpgsqlDataReader
    {
        public Task<ReadOnlyCollection<NpgsqlDbColumn>> GetColumnSchemaAsync();
        public Task<DataTable> GetSchemaTableAsync();
    }
  15. roji commented on Jun 1, 2020

    @roji
    MemberAuthor

    For new APIs, there's no reason to retain separate overloads where we can use default variables (so all 3 GetSchemaAsync can be collapsed into one). For existing methods in the BCL, this isn't done only to maintain backwards binary compat.

  16. manandre commented on Jun 1, 2020

    @manandre
    Collaborator

    Updated API proposal (bis)

    public sealed class NpgsqlConnection
    {
        public Task<DataTable> GetSchemaAsync(string? collectionName = null, string[]? restrictions = null);
    }
    
    public sealed class NpgsqlDataReader
    {
        public Task<ReadOnlyCollection<NpgsqlDbColumn>> GetColumnSchemaAsync();
        public Task<DataTable> GetSchemaTableAsync();
    }
  17. roji commented on Jun 17, 2020

    @roji
    MemberAuthor

    Submitted proposal in System.Data: dotnet/runtime#38028

  18. roji commented on Jul 12, 2020

    @roji
    MemberAuthor

    @manandre support has been merged in System.Data. When a preview comes out with it, we should bump our SDK version and implement it.

  19. manandre commented on Jul 15, 2020

    @manandre
    Collaborator

    @roji Nice job!
    We will then have to introduce net5.0 TFM support. Should we do this in a dedicated PR?
    Moreover, do we want to expose those new async schema APIs for previous TFM versions?

  20. roji commented on Jul 15, 2020

    @roji
    MemberAuthor

    @manandre no real need for a different PR, it can be part of this one. However, unless I'm mistaken this will only appear in preview8, which will probably be released around a month from now.

    Yeah, we should expose the new APIs for previous TFMs, and use #ifs to override or not override base on the TFM. We already do this in various places (e.g. NpgsqlConnection.BeginTransactionAsync).

  21. assigned and unassigned on Sep 18, 2020
  22. added a commit that references this issue on Sep 25, 2020
    d28ce7e
  23. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions