0

I am trying to create a stpored procedure via Npsql from C#

    private async Task TestExecute(string cmdQuery)
    {
        var cnnString = $"Server=localhost;User Id=admin;Password=somepass;Database=MyTestdatabase";
        NpgsqlConnection connection = new NpgsqlConnection(cnnString);
        await connection.OpenAsync();

        var cmdQuery2 = "CREATE OR REPLACE PROCEDURE public.select_data()" +
            " LANGUAGE sql" +
            " BEGIN ATOMIC" +
            " SELECT tbl.id," +
            "     tbl.val" +
            "    FROM tbl;" +
            " END";

        // All of these fails
        var r0 = await connection.ExecuteScalarAsync(cmdQuery2);
        var r1 = await connection.ExecuteAsync(cmdQuery2);
        var r2 = await connection.QueryAsync(cmdQuery2);
    }

This is the script:

CREATE OR REPLACE PROCEDURE public.select_data()
 LANGUAGE sql
BEGIN ATOMIC
 SELECT tbl.id,
     tbl.val
    FROM tbl;
END

It works perfectly when executing from the pgAdmin. However I have this error when trying to execute it with any of Execute/Query methods.

enter image description here

enter image description here

What is even more interesting I don't see the command from the pg activity by executing this script:

SELECT query, * FROM pg_stat_activity 
where query <> ''
order by query_start desc; 

So looks like CREATE OR REPLACE Procedure fails at some validation level even before reach Postgre server.

Btw similar script for creating a function works fine even with "ExecuteReaderAsync"

CREATE OR REPLACE FUNCTION public.fn_add_values(a integer, b integer)
 RETURNS integer
 LANGUAGE sql
RETURN (a + b)
1
  • 1
    Could you please add a minimal executable example (text, not just a screen shot), with instructions for how to execute it? From the error message, it seems possible the SQL statement was garbled, so you might want to consult the postgres statement log. Commented Jan 8 at 13:41

1 Answer 1

2

Semicolon... It's all about semicolon.

Your sql looks like this:

CREATE OR REPLACE PROCEDURE public.select_data()
LANGUAGE sql
BEGIN ATOMIC
SELECT tbl.id,
    tbl.val
    FROM tbl

so it's invalid. Semicolon terminates the statement.

Tell Npgsql to do not parse and rewrite your statement, or just use Dollar-Quoted String Constants

CREATE OR REPLACE PROCEDURE ...
LANGUAGE plpgsql
AS $$
BEGIN
    ...
END;
$$

One more note:

You have been hit by this issue:
https://github.com/npgsql/npgsql/issues/4445
which was mentioned in this blog post:
https://www.roji.org/parameters-batching-and-sql-rewriting

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.