Re: Can't run CREATE PROCEDURE with new Postgres' 14 new SQL-standard body

From: Sehrope Sarkuni <sehrope(at)jackdb(dot)com>
To: Thomas Kellerer <shammat(at)gmx(dot)net>
Cc: List <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Can't run CREATE PROCEDURE with new Postgres' 14 new SQL-standard body
Date: 2021-10-04 12:49:12
Message-ID: CAH7T-aqbkHN6GrA5dPmbcJ1YU21NcooJBdWh1a=18EgqAt2U=w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On Sun, Oct 3, 2021 at 8:08 AM Thomas Kellerer <shammat(at)gmx(dot)net> wrote:

> Postgres 14 supports SQL standard conforming bodies for functions and
> procedures
> which are no longer string constants, e.g.
>
> create or replace procedure do_stuff()
> language sql
> begin atomic
> select 1;
> select 2;
> end
>
> However, it seems that the JDBC driver will parse the string and split it
> into
> multiple statements based on the semicolons thus making it an invalid
> create procedure
> statement.
>
> The above CREATE statement runs fine in psql, but the following Java code:
>
> Connection con = DriverManager.getConnection(...);
>
> String sql =
> "create or replace procedure do_stuff()\n" +
> " language sql\n" +
> " begin atomic\n" +
> " select 1;\n" +
> " select 2;\n" +
> " end";
>
> Statement stmt = con.createStatement();
> stmt.execute(sql);
>
> fails with:
>
> org.postgresql.util.PSQLException: ERROR: syntax error at end of input
> Position: 79
>
> Position 79 is the semicolon after "select 1"
>
> Is there a way to disable the parsing/splitting in the driver so that it
> will be possible
> to run such statements?
>

If you set the connection property "preferQueryMode" to "simple" or
"extendedForPrepared", then the driver will not attempt to split the
statement into multiple commands. It will get sent as-is and executed via
the simple protocol rather than the parse / bind / execute extended
protocol. I tried this out locally and confirmed that I'm able to create a
SQL standard body procedure on PG 14.

The connection property applies to the entire life of that connection. I'm
not aware of a way to do it selectively for a single command on an already
established connection.

I'm also not sure why we do the statement splitting at all. The server
supports multiple commands in a single simple protocol statement and
rejects multiple commands in the extended protocol. That's something we
should look into as well adding more testing to the driver for stored procs
and PG 14.

Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2021-10-04 13:50:58 [pgjdbc/pgjdbc] 04b728: Revert "fix: try to read any notifies or errors th...
Previous Message Dave Cramer 2021-10-04 10:41:46 [pgjdbc/pgjdbc] 3cf175: fix: Regressions caused by PR#1949 (#2266)