Skip to content

Npgsql logging doesn't log queries duration time #2282

Description

@guylando

npgsql 4.0.4.
Knowing database queries duration greatly helps to find problems in the code so it would be a very useful addition to npgsql logging (https://www.npgsql.org/doc/logging.html).

Activity

  1. added this to the 4.1 milestone on Jan 4, 2019
  2. roji commented on Jan 4, 2019

    @roji
    Member

    That sounds like a pretty reasonable request, and something that's easy to do.

  3. modified the milestones: 4.1, 5.0 on Sep 8, 2019
  4. ch-asimakopoulos commented on Sep 28, 2020

    @ch-asimakopoulos
    Contributor

    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.

  5. vonzshik commented on Sep 28, 2020

    @vonzshik
    Contributor

    Hello!

    Essentially, you should start a Stopwatch somewhere around here:

    connector.UserTimeout = CommandTimeout * 1000;

    And it should be stopped just before a reader is returned. And after that, you log how much time has elapsed.

  6. roji commented on Sep 28, 2020

    @roji
    Member

    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.

  7. roji commented on Sep 28, 2020

    @roji
    Member

    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?

  8. YohDeadfall commented on Sep 28, 2020

    @YohDeadfall
    Contributor

    FYI there is ValueStopwatch in ASP.NET Core, but it's internal. It uses Stopwatch static methods and do the same things, but without allocations.

  9. roji commented on Sep 28, 2020

    @roji
    Member

    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.

  10. vonzshik commented on Sep 28, 2020

    @vonzshik
    Contributor

    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.

  11. roji commented on Sep 28, 2020

    @roji
    Member

    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.

    At least that's how I understand it :)

  12. vonzshik commented on Sep 28, 2020

    @vonzshik
    Contributor

    Checked - at the very least for that query, I've been waiting at NpgsqlCommandExecuteReader for 30 seconds, and after that every NextResult was instant.

  13. 4 remaining items

  14. roji commented on Sep 28, 2020

    @roji
    Member

    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.

  15. vonzshik commented on Sep 28, 2020

    @vonzshik
    Contributor

    Since there's interaction between the batched statements, IMHO it's best (and sufficient) just to give a single, global number for the command.

    Makes sense.

  16. ch-asimakopoulos commented on Sep 29, 2020

    @ch-asimakopoulos
    Contributor

    So, should I proceed with an implementation that measures the duration of an entire command's execution?

  17. vonzshik commented on Oct 1, 2020

    @vonzshik
    Contributor

    So, should I proceed with an implementation that measures the duration of an entire command's execution?

    @ch-asimakopoulos yeah, go for it.

  18. ch-asimakopoulos commented on Oct 2, 2020

    @ch-asimakopoulos
    Contributor

    OK, I'll tackle it over the weekend!

  19. added a commit that references this issue on Oct 8, 2020
    8b88d9d
  20. roji commented on Oct 13, 2020

    @roji
    Member

    @ch-asimakopoulos assigned you for credit, thanks again!

  21. ch-asimakopoulos commented on Oct 13, 2020

    @ch-asimakopoulos
    Contributor
  22. 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

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions