Skip to content

New PG14 SQL-standard function bodies break our SQL parser #4445

@roji

Description

@roji

PG14 added a new syntax for creating SQL functions (see release notes, as well as this SO):

CREATE OR REPLACE FUNCTION asterisks(n int)
  RETURNS SETOF text
  LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
BEGIN ATOMIC
SELECT repeat('*', g) FROM generate_series (1, n) g; -- <-- Note this semicolon
END;

Unfortunately, our SQL query parser splits this SQL at the semicolon terminating the select, resulting in mangled SQL being sent to PG, and failing. It seems like the JDBC driver has the same issue (tested in Rider's database UI).

It's possible to turn off SQL rewriting globally, this is the proper fix:

AppContext.SetSwitch("Npgsql.EnableSqlRewriting", false);

... but although this is recommended, many users probably won't be able to do this.

As a hacky workaround, it's possible to tell Npgsql to not parse/rewrite SQL for this specific command, by adding a single unnamed parameter, triggering positional parameters:

cmd.Parameters.Add(new() { Value = 0 });

Originally flagged in this SO.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions