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
Following discussion in #689 (comment), it seems to make sense to make our CommandTimeout behave lie SqlClient's (see docs).
This means that all socket read time is aggregated for a single ADO.NET API call (e.g. Read()). Currently we simply set the socket's ReceiveTimeout to the CommandTimeout, which means that if multiple socket reads happen within the same Read(), the timeout is reset each time. It seems to make sense to aggregate the CommandTimeout across all of Read()'s network reads instead.
Should CommandTimeout be the time per read or the time for all reads? I can see a few scenarios:
The user just wants an overall timeout for reading all results. Obviously the SqlClient behaviour is simpler here, but the user could easily implement an overall timeout themselves - just start a Stopwatch and check the time elapsed after each Read().
The user want a timeout for the first result, but then is happy to take as long as they need to reading the results. An overall timeout doesn't work for this, so the current Npgsql behaviour is better.
The user wants a timeout for the first result and a different (usually much shorter) timeout for each subsequent result. Current Npgsql behaviour is better for this, too, as long as it doesn't cache the value of CommandTimeout, so user code could change it after ExecuteReader has returned (and possibly even before each Read).
Compatibility with SqlClient is good, but backwards compatibility with Npgsql 3.1 is better. Combined with the current behaviour being more flexible, I'd say leave it like this.
The user just wants an overall timeout for reading all results. Obviously the SqlClient behaviour is simpler here, but the user could easily implement an overall timeout themselves - just start a Stopwatch and check the time elapsed after each Read().
If by "reading all results" you mean all result rows of a command, then this isn't what SqlClient does (or what this issue was proposing to do) - SqlClient applies timeouts to a single ADO.NET method invocation (e.g. Read()), and only aggregates I/O time spent performing on that invocation for the purpose of timeouts.
As you say, if the user wants a client-side timeout for reading all results they can implement it themselves.
The user want a timeout for the first result, but then is happy to take as long as they need to reading the results. An overall timeout doesn't work for this, so the current Npgsql behaviour is better.
I definitely don't view this as a very valid/interesting scenario - one of the key points of timeouts is to avoid network issues which would block your program forever. I understand where you're coming from - you're thinking about long-running (or deadlocked) queries rather than network issues, but the latter must be addressed.
One more point - it's not necessarily correct to assume that PostgreSQL takes a long time upfront processing your query, and then spits results back quickly. AFAIK depending on exactly what you're doing it may return the first result very quickly but take long afterwards.
The user wants a timeout for the first result and a different (usually much shorter) timeout for each subsequent result. Current Npgsql behaviour is better for this, too, as long as it doesn't cache the value of CommandTimeout, so user code could change it after ExecuteReader has returned (and possibly even before each Read).
Even if this issue is implemented and we start aggregating I/O time, this would still be possible as aggregation only happens within a single ADO.NET method call (as I said above). So you would be free to play around with CommandTimeout between calls.
To summarize, this issue actually doesn't change behavior in a very significant way, since it only aggregates within a single method call.
Note that as I wrote in #2239, there's nothing standard about aggregating socket read time - it just happens to be the SqlClient behavior (i.e. not documented on DbCommand.CommandTimeout). I'm not sure what other providers do.
In addition, it isn't very clear whether it really is the desirable behavior for all scenarios. Both behaviors have their pros and cons.
I personally prefer our current implementation, as it's actually more flexible. If you don't have any problems with the network, you won't see any difference. If you do have them, we've also got you here.
If you don't have any problems with the network, you won't see any difference. If you do have them, we've also got you here.
You're right that if there are network problems, there's no difference.
But if there aren't network problems, I do think there's the question of "trickling" queries, where results come in little by little. Does an average user want to cancel such a query after X seconds, even though data is still coming? We have no data here to answer the question, but my gut tells me the average user would not want to cancel it. And if they do, as @Brar pointed out they can always manage a cancellation token themselves for the duration of the operation.
But if there aren't network problems, I do think there's the question of "trickling" queries, where results come in little by little.
Isn't that the definition of the issues with the network? AFAIK, this isn't PG's behavior.
You're right that if there are network problems, there's no difference.
I'm talking about the case, when a query takes too long to complete on a PG side. We will still be stuck, waiting for a response from the PG, and eventually fail with a timeout.
Depending on exactly what the query does, I'm pretty sure it's possible to receive a single row, then wait, then receive another row, then wait, etc. - with perfect network conditions. See Robert Haas's answer here.
I think this is the only relevant scenario for discussion on whether to aggregate or not.
Depending on exactly what the query does, I'm pretty sure it's possible to receive a single row, then wait, then receive another row, then wait, etc. - with perfect network conditions. See Robert Haas's answer here.
Yes, that's a very good point. That probably means that within a single Read (or any other single operation), it's not important whether we aggregate or not. The distinction would have been important had we applied CommandTimeout to the entire command, but we don't do that.
I'd like to wait for @bgrainger's input on this - am interested if things are somehow different MySQL. But otherwise it seems fine to close this.
CommandTimeout could mean "total time spent reading", or it could mean "time spent reading without receiving any data". My personal feeling/assumption is that most/all users really need the 2nd (see #1041 (comment)), but implementing the second may also be fine (most users are unlikely to care about the difference).
"the 2nd ... the second may also" is confusing; not sure which one you meant.
That probably means that within a single Read (or any other single operation), it's not important whether we aggregate or not. The distinction would have been important had we applied CommandTimeout to the entire command, but we don't do that.
I agree with this.
It's hard for me to speak in broad generalities since there are so many varieties of MySQL Server (MySQL, MariaDB, Amazon RDS, Aurora, etc.; v5.6, v5.7, v8.0) but what I've observed is that MySQL tends to take a long time to compute the first row in a result set (this is time in ExecuteReader) then constantly streams rows back. There may be multiple calls to Socket.Receive to read the packets that make up the resultset header, but during Read, assuming the rows are small enough, I would expect each one to be retrieved with one call to Receive.
Unfortunately I don't have any hard data, but I suspect that it generally doesn't matter whether timeouts are aggregated for a single call to ExecuteReader or Read. Perhaps there are pathological cases where Socket.Receive returns just one byte every time, and it's necessary to use aggregated timeouts to prevent extremely long operations. Maybe the SqlClient team chose that approach out of an abundance of caution?
MySqlConnector does implement the aggregate timeout approach but IIRC it's more out of worry that that might be necessary in extreme situations, rather than knowing of particular cases where it's necessary for correctness.
what I've observed is that MySQL tends to take a long time to compute the first row in a result set (this is time in ExecuteReader) then constantly streams rows back.
On the PG side, we do know that rows may have long delays in between them (#1041 (comment)). However, as @vonzshik correctly pointed out, aggregated timeouts only matter if we think that are going to be delays within data for a single row, which really does seem very unlikely.
I'm going to close this issue as not needed, until we get some evidence that says otherwise.
Activity
roji commentedon Apr 28, 2016
Note: check the situation with the connection timeout as well, are we doing it right?
realityexists commentedon Oct 31, 2016
Should
CommandTimeoutbe the time per read or the time for all reads? I can see a few scenarios:Stopwatchand check the time elapsed after eachRead().CommandTimeout, so user code could change it after ExecuteReader has returned (and possibly even before each Read).Compatibility with SqlClient is good, but backwards compatibility with Npgsql 3.1 is better. Combined with the current behaviour being more flexible, I'd say leave it like this.
roji commentedon Oct 31, 2016
If by "reading all results" you mean all result rows of a command, then this isn't what SqlClient does (or what this issue was proposing to do) - SqlClient applies timeouts to a single ADO.NET method invocation (e.g.
Read()), and only aggregates I/O time spent performing on that invocation for the purpose of timeouts.As you say, if the user wants a client-side timeout for reading all results they can implement it themselves.
I definitely don't view this as a very valid/interesting scenario - one of the key points of timeouts is to avoid network issues which would block your program forever. I understand where you're coming from - you're thinking about long-running (or deadlocked) queries rather than network issues, but the latter must be addressed.
One more point - it's not necessarily correct to assume that PostgreSQL takes a long time upfront processing your query, and then spits results back quickly. AFAIK depending on exactly what you're doing it may return the first result very quickly but take long afterwards.
Even if this issue is implemented and we start aggregating I/O time, this would still be possible as aggregation only happens within a single ADO.NET method call (as I said above). So you would be free to play around with CommandTimeout between calls.
To summarize, this issue actually doesn't change behavior in a very significant way, since it only aggregates within a single method call.
realityexists commentedon Oct 31, 2016
Ah, OK, ignore the above comment then - I misunderstood how it works.
baal2000 commentedon Nov 25, 2018
+1 for a standard, provider-agnostic behavior.
roji commentedon Nov 25, 2018
Note that as I wrote in #2239, there's nothing standard about aggregating socket read time - it just happens to be the SqlClient behavior (i.e. not documented on
DbCommand.CommandTimeout). I'm not sure what other providers do.In addition, it isn't very clear whether it really is the desirable behavior for all scenarios. Both behaviors have their pros and cons.
[-]Make frontend timeouts aggregated[/-][+]Make timeouts aggregated[/+]16 remaining items
vonzshik commentedon Oct 3, 2020
I personally prefer our current implementation, as it's actually more flexible. If you don't have any problems with the network, you won't see any difference. If you do have them, we've also got you here.
roji commentedon Oct 3, 2020
You're right that if there are network problems, there's no difference.
But if there aren't network problems, I do think there's the question of "trickling" queries, where results come in little by little. Does an average user want to cancel such a query after X seconds, even though data is still coming? We have no data here to answer the question, but my gut tells me the average user would not want to cancel it. And if they do, as @Brar pointed out they can always manage a cancellation token themselves for the duration of the operation.
vonzshik commentedon Oct 3, 2020
Isn't that the definition of the issues with the network? AFAIK, this isn't PG's behavior.
I'm talking about the case, when a query takes too long to complete on a PG side. We will still be stuck, waiting for a response from the PG, and eventually fail with a timeout.
roji commentedon Oct 3, 2020
Depending on exactly what the query does, I'm pretty sure it's possible to receive a single row, then wait, then receive another row, then wait, etc. - with perfect network conditions. See Robert Haas's answer here.
I think this is the only relevant scenario for discussion on whether to aggregate or not.
vonzshik commentedon Oct 3, 2020
Wouldn't it happen during
DataReader.Read?roji commentedon Oct 3, 2020
Yes, that's a very good point. That probably means that within a single Read (or any other single operation), it's not important whether we aggregate or not. The distinction would have been important had we applied CommandTimeout to the entire command, but we don't do that.
I'd like to wait for @bgrainger's input on this - am interested if things are somehow different MySQL. But otherwise it seems fine to close this.
bgrainger commentedon Oct 3, 2020
"the 2nd ... the second may also" is confusing; not sure which one you meant.
I agree with this.
It's hard for me to speak in broad generalities since there are so many varieties of MySQL Server (MySQL, MariaDB, Amazon RDS, Aurora, etc.; v5.6, v5.7, v8.0) but what I've observed is that MySQL tends to take a long time to compute the first row in a result set (this is time in
ExecuteReader) then constantly streams rows back. There may be multiple calls toSocket.Receiveto read the packets that make up the resultset header, but duringRead, assuming the rows are small enough, I would expect each one to be retrieved with one call toReceive.Unfortunately I don't have any hard data, but I suspect that it generally doesn't matter whether timeouts are aggregated for a single call to
ExecuteReaderorRead. Perhaps there are pathological cases whereSocket.Receivereturns just one byte every time, and it's necessary to use aggregated timeouts to prevent extremely long operations. Maybe the SqlClient team chose that approach out of an abundance of caution?bgrainger commentedon Oct 3, 2020
MySqlConnector does implement the aggregate timeout approach but IIRC it's more out of worry that that might be necessary in extreme situations, rather than knowing of particular cases where it's necessary for correctness.
roji commentedon Oct 3, 2020
@bgrainger thanks for your input.
On the PG side, we do know that rows may have long delays in between them (#1041 (comment)). However, as @vonzshik correctly pointed out, aggregated timeouts only matter if we think that are going to be delays within data for a single row, which really does seem very unlikely.
I'm going to close this issue as not needed, until we get some evidence that says otherwise.