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
Introduce async schema APIs on connection and reader #2976
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.
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.
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).
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).
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.
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:
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.
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.
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.
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>.)
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.
@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?
@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).
Activity
manandre commentedon May 30, 2020
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:
@roji Which approach are you considering?
YohDeadfall commentedon May 30, 2020
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.
roji commentedon May 30, 2020
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).
manandre commentedon May 31, 2020
API proposal
Implementation considerations
NpgsqlConnection: sync implementation currently relies onDbDataAdapter.Fill(DataTable)fromSystem.Data.Commonwhich 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 onNpgsqlDataReader.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).roji commentedon May 31, 2020
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.
bgrainger commentedon May 31, 2020
MySqlConnector could implement this API. While most calls to
GetSchemaAsyncwill involve I/O, there are some (e.g.,GetSchemaAsync("Keywords")) that can be synchronous, soValueTaskseems justified. (The newDbDataReadermethods will both be synchronous for MySqlConnector.)Shouldn't it be
GetSchemaAsync(string collectionName);? If thecollectionNameis specified, it must be non-null:npgsql/src/Npgsql/NpgsqlSchema.cs
Lines 18 to 19 in 5958e4f
roji commentedon May 31, 2020
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.
bgrainger commentedon May 31, 2020
I almost cited dotnet/runtime#28596 (comment) in my previous comment:
I have no objection to
Task<DataTable> GetSchemaAsync; the usability benefits ofTaskmay be worth it, andDataTableis not a high-performance API in the first place.Whether
TaskorValueTaskis used in theDbDataReadermethods, 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 forIsDbNullAsyncandGetFieldValueAsync.roji commentedon May 31, 2020
Yep, agreed.
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.
bgrainger commentedon May 31, 2020
No, not if it was zero-cost (or negligible cost for non-high-performance scenarios). (But
GetFieldValueAsyncis particularly bad because it has to allocate aTask; at leastIsDBNullAsynccan reuse a cachedTask<bool>.)roji commentedon May 31, 2020
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...)
manandre commentedon Jun 1, 2020
Updated API proposal
roji commentedon Jun 1, 2020
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.
manandre commentedon Jun 1, 2020
Updated API proposal (bis)
roji commentedon Jun 17, 2020
Submitted proposal in System.Data: dotnet/runtime#38028
roji commentedon Jul 12, 2020
@manandre support has been merged in System.Data. When a preview comes out with it, we should bump our SDK version and implement it.
manandre commentedon Jul 15, 2020
@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?
roji commentedon Jul 15, 2020
@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).
Async Schema APIs (#3017)
GetColumnSchemaonDataReader#3010 as a duplicate of this issue