Skip to content

Command Log writes incorrect parameter indexes #2424

Closed

Description

@floatas

Same issue as #1401 but with steps to reproduce.

Code:

            NpgsqlLogManager.IsParameterLoggingEnabled = true;
            NpgsqlLogManager.Provider = new ConsoleLoggingProvider(NpgsqlLogLevel.Debug);

            using (var connection = new NpgsqlConnection("connstring"))
            {
                connection.Open();
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "select * from <table>where isDeleted = @isDeleted AND year = @year";

                    command.Parameters.AddWithValue("@year", 1991);
                    command.Parameters.AddWithValue("@isDeleted", false);

                    command.ExecuteReader();
                }
            }

Console output:

Executing statement(s):
 select * from <table> where isDeleted = $1 AND year = $2
 Parameters:
        $1: 1991        $2: False

When writing log, you assign $index by order of parameters in list. However when you execute you create query index based on order of appearance.

NpgsqlCommand LogCommand method - $ index based on list possition

SqlQueryParser param (goto tag) assigns index based on appearance in query

_statement.InputParameters.Add(parameter);
index = _paramIndexMap[paramName] = _statement.InputParameters.Count;

Further technical details

Npgsql version: 3.1.8

Activity

  1. floatas commented on Apr 8, 2019

    @floatas
    Author

    Tested with 4.0.5 same issue

  2. added this to the 4.1 milestone on Apr 8, 2019
  3. self-assigned this
    on Apr 10, 2019
  4. removed this from the 4.1 milestone on Apr 10, 2019
  5. added a commit that references this issue on Apr 10, 2019
    7e15ee5
  6. modified the milestones: 4.0.7, 4.0.6 on Apr 10, 2019
  7. roji commented on Apr 10, 2019

    @roji
    Member

    Thanks for the repro @floatas! The issue was quite simple in the end: we were logging all the parameters from the NpgsqlCommand, in the order in which they were arranged there. The right thing is to log the parameters for each statements separately, from the NpgsqlStatement instance.

    @YohDeadfall @austindrenski would appreciate a quick review on this, we should get the fix into 4.0.6.

  8. added a commit that references this issue on Apr 10, 2019
    fb39cf5
  9. added a commit that references this issue on Aug 21, 2019
    fda909b
  10. modified the milestones: 4.0.6, 4.0.10 on Aug 21, 2019
  11. roji commented on Aug 21, 2019

    @roji
    Member

    Oops, guess I forgot to backport this... Thanks @YohDeadfall!

  12. indy-singh commented on Apr 30, 2020

    @indy-singh

    Is there any chance of getting this fix into 3.2.7?

    Indy

  13. roji commented on Apr 30, 2020

    @roji
    Member

    @indy-singh we're no longer releasing new patches for 3.2, unless maybe something really critical comes along - I don't think this really qualifies. Any specific reason you can't upgrade to later versions?

  14. indy-singh commented on Apr 30, 2020

    @indy-singh

    Yes, there was #2224 which we got caught up in. Looks like it was fixed #2631 and released as a part of 4.1.0. I'll confirm that before I roll out the upgrade to production.

    Thanks @roji

  15. roji commented on Apr 30, 2020

    @roji
    Member

    Great, let me know how it goes! Idle connections are definitely pruned now, although we still have some room for improvement - would be good to get your feedback.

  16. 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

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions