Skip to content

SQL query parsing bug (PG non-conforming strings mode) #2660

Closed

Description

@abelykh0

Steps to reproduce

void Main()
{
	string connectionString = "Host=dbtest;Port=5434;Database=5gukq3xew3b36gh3co6th1w;Username=postgres;Password=******;";
	using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
	{
		connection.Open();
		string sql = @"
select table_name
from information_schema.views
where table_name like @p0 escape '\' and
(is_updatable = 'NO') = @p1
";
		using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
		{
			command.Parameters.AddWithValue("@p0", "%trig%");
			command.Parameters.AddWithValue("@p1", true);
			using (NpgsqlDataReader reader = command.ExecuteReader())
			{
				reader.Read();
				reader[0].Dump();
			}
		}
	}
}

The issue

Exact same code returns successfully in v4.0.10

Exception message:
42703: column "p1" does not exist

Stack trace:

Code | 42703
-- | --
ColumnName | null
ConstraintName | null
Data | Data
DataTypeName | null
Detail | null
ErrorCode | -2147467259
File | parse_relation.c
HelpLink | null
Hint | null
HResult | -2147467259
InnerException | null
InternalPosition | 0
InternalQuery | null
InvariantSeverity | ERROR
IsTransient | False
Line | 3183
Message | 42703: column "p1" does not exist
MessageText | column "p1" does not exist
Position | 119
Routine | errorMissingColumn
SchemaName | null
Severity | ERROR
Source | Npgsql
SqlState | 42703
StackTrace | at Npgsql.NpgsqlConnector.<>c__DisplayClass159_0.<<DoReadMessage>g__ReadMessageLong\|0>d.MoveNext()--- End of stack trace from previous location where exception was thrown ---   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   at Npgsql.NpgsqlConnector.<>c__DisplayClass159_0.<<DoReadMessage>g__ReadMessageLong\|0>d.MoveNext()--- End of stack trace from previous location where exception was thrown ---   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   at Npgsql.NpgsqlDataReader.<NextResult>d__44.MoveNext()--- End of stack trace from previous location where exception was thrown ---   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   at Npgsql.NpgsqlDataReader.NextResult()   at Npgsql.NpgsqlCommand.<ExecuteReaderAsync>d__101.MoveNext()--- End of stack trace from previous location where exception was thrown ---   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)   at UserQuery.Main() in C:\Users\andreyb\AppData\Local\Temp\LINQPad5\_lyhfafmb\query_pkhfum.cs:line 46   at LINQPad.ExecutionModel.ClrQueryRunner.Run()   at LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)   at LINQPad.ExecutionModel.Server.StartQuery(QueryRunner runner)   at LINQPad.ExecutionModel.Server.<>c__DisplayClass153_0.<ExecuteClrQuery>b__0()   at LINQPad.ExecutionModel.Server.SingleThreadExecuter.Work()   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)   at System.Threading.ThreadHelper.ThreadStart()
Statement | select table_namefrom information_schema.viewswhere table_name like $1 escape '\' and(is_updatable = 'NO') = @p1
TableName | null
TargetSite | TargetSite
Where | null

Further technical details

Npgsql version: 4.1.0
PostgreSQL version: 9.6.4
Operating system: Windows 10

Other details about my project setup:

Activity

  1. austindrenski commented on Sep 30, 2019

    @austindrenski
    Contributor

    Similar to #2656.

  2. NinoFloris commented on Sep 30, 2019

    @NinoFloris
    Member

    Reproduced.

    I believe the statement from the exception gives us a hint of what's going on
    select table_namefrom information_schema.viewswhere table_name like $1 escape '\' and(is_updatable = 'NO') = @p1

    As you can see npgsql replaced @p0 with $1 but failed to do so for @p1.

    @roji do you know by head if things changed around parameter substitution?

  3. roji commented on Sep 30, 2019

    @roji
    Member

    Nope, not to my knowledge. Will dive into it.

  4. self-assigned this
    on Sep 30, 2019
  5. changed the title [-]Regression: cannot run specific query after upgrading to 4.1.0[/-] [+]SQL query parsing bug (PG non-conforming strings mode)[/+] on Sep 30, 2019
  6. added a commit that references this issue on Sep 30, 2019
    82553fb
  7. added this to the 4.1.1 milestone on Sep 30, 2019
  8. added 2 commits that reference this issue on Sep 30, 2019
    239a919
    08ca7c2
  9. 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