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
Npgsql logging doesn't log queries duration time #2282
One other important thing, is to try to make this have zero perf impact, i.e. only start and stop the StopWatch if statement logging is actually enabled.
Reacted by Nikita Kazmin and Charalampos Asimakopoulos
One more design question here - this feature is a bit problematic with batched commands. The implementation in #3194 stops measuring the moment the first NextResult completes - but that's only when the resultset for the first statement is returned, not for the entire command.
An alternative would be to stop measuring when the reader is fully consumed - this would make the duration cover the entire command, but would also include time spent processing the results, and not just server time (reminds me a bit of the whole discussion around PostgreSQL statement_timeout). I'm personally leaning more towards this second option.
Either way, as Stopwatch is a class, we shouldn't allocate it for every execution; we should instead store it on the connector and reuse it every time a command is executed.
Yeah, I saw that once - always wondered why it wasn't made part of the BCL. But in any case, if we switch to measuring the entire command, this has to be heap-allocated.
Either way, as Stopwatch is a class, we shouldn't allocate it for every execution; we should instead store it on the connector and reuse it every time a command is executed.
Agreed.
One more design question here - this feature is a bit problematic with batched commands. The implementation in #3194 stops measuring the moment the first NextResult completes - but that's only when the resultset for the first statement is returned, not for the entire command.
Is it really so? So, if I send a query "SELECT * FROM pg_sleep(10); SELECT * FROM pg_sleep(10); SELECT * FROM pg_sleep(10);"
I would wait for 10 seconds for every NextResult? I'll have to check that.
I would wait for 10 seconds for every NextResult? I'll have to check that.
I'm pretty sure that's the case. PostgreSQL reads and processes wire protocol message in the order in which they arrive, and a batch is nothing more than several "Parse/Bind/Describe/Execute" tuples sent one after the other, followed by a Sync. The moment PG sees the first Execute, it executes that query (hanging for 10 seconds), before continuing to process later messages.
Checked - at the very least for that query, I've been waiting at NpgsqlCommandExecuteReader for 30 seconds, and after that every NextResult was instant.
I personally thing that what most people need is a simple duration for the entire command (start when sending the command, stop when the reader is consumed/disposed). Doing a separate duration for each command in the batch introduces some complications, such as identifying/distinguishing the different batched statements in the logs (since there are several per command, we'd probably need to output SQL for each). In many cases there's also some imprecision since the duration of a statement depends on the statement that preceded it: if the earlier statement has a very short resultset, PG starts processing it long before the user calls NextResult. Since there's interaction between the batched statements, IMHO it's best (and sufficient) just to give a single, global number for the command.
Activity
roji commentedon Jan 4, 2019
That sounds like a pretty reasonable request, and something that's easy to do.
ch-asimakopoulos commentedon Sep 28, 2020
Can someone provide any guidelines on how to approach this? I am using npgsql and would like to congratulate you on the great work!
I would love to take a crack at this.
vonzshik commentedon Sep 28, 2020
Hello!
Essentially, you should start a
Stopwatchsomewhere around here:npgsql/src/Npgsql/NpgsqlCommand.cs
Line 1249 in becd301
And it should be stopped just before a reader is returned. And after that, you log how much time has elapsed.
roji commentedon Sep 28, 2020
One other important thing, is to try to make this have zero perf impact, i.e. only start and stop the StopWatch if statement logging is actually enabled.
roji commentedon Sep 28, 2020
One more design question here - this feature is a bit problematic with batched commands. The implementation in #3194 stops measuring the moment the first NextResult completes - but that's only when the resultset for the first statement is returned, not for the entire command.
An alternative would be to stop measuring when the reader is fully consumed - this would make the duration cover the entire command, but would also include time spent processing the results, and not just server time (reminds me a bit of the whole discussion around PostgreSQL
statement_timeout). I'm personally leaning more towards this second option.Either way, as Stopwatch is a class, we shouldn't allocate it for every execution; we should instead store it on the connector and reuse it every time a command is executed.
@vonzshik @Brar @YohDeadfall thoughts?
YohDeadfall commentedon Sep 28, 2020
FYI there is
ValueStopwatchin ASP.NET Core, but it's internal. It usesStopwatchstatic methods and do the same things, but without allocations.roji commentedon Sep 28, 2020
Yeah, I saw that once - always wondered why it wasn't made part of the BCL. But in any case, if we switch to measuring the entire command, this has to be heap-allocated.
vonzshik commentedon Sep 28, 2020
Agreed.
Is it really so? So, if I send a query
"SELECT * FROM pg_sleep(10); SELECT * FROM pg_sleep(10); SELECT * FROM pg_sleep(10);"I would wait for 10 seconds for every NextResult? I'll have to check that.
roji commentedon Sep 28, 2020
I'm pretty sure that's the case. PostgreSQL reads and processes wire protocol message in the order in which they arrive, and a batch is nothing more than several "Parse/Bind/Describe/Execute" tuples sent one after the other, followed by a Sync. The moment PG sees the first Execute, it executes that query (hanging for 10 seconds), before continuing to process later messages.
At least that's how I understand it :)
vonzshik commentedon Sep 28, 2020
Checked - at the very least for that query, I've been waiting at
NpgsqlCommandExecuteReaderfor 30 seconds, and after that every NextResult was instant.4 remaining items
roji commentedon Sep 28, 2020
Thanks for confirming.
I personally thing that what most people need is a simple duration for the entire command (start when sending the command, stop when the reader is consumed/disposed). Doing a separate duration for each command in the batch introduces some complications, such as identifying/distinguishing the different batched statements in the logs (since there are several per command, we'd probably need to output SQL for each). In many cases there's also some imprecision since the duration of a statement depends on the statement that preceded it: if the earlier statement has a very short resultset, PG starts processing it long before the user calls NextResult. Since there's interaction between the batched statements, IMHO it's best (and sufficient) just to give a single, global number for the command.
vonzshik commentedon Sep 28, 2020
Makes sense.
ch-asimakopoulos commentedon Sep 29, 2020
So, should I proceed with an implementation that measures the duration of an entire command's execution?
vonzshik commentedon Oct 1, 2020
@ch-asimakopoulos yeah, go for it.
ch-asimakopoulos commentedon Oct 2, 2020
OK, I'll tackle it over the weekend!
Logging a query's duration time if Logging on a debug level is enable…
roji commentedon Oct 13, 2020
@ch-asimakopoulos assigned you for credit, thanks again!
ch-asimakopoulos commentedon Oct 13, 2020