pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: 798604270@qq.com
Subject: BUG #19350: Short circuit optimization missed when running sql scriptes in JDBC
Date: Wed, 10 Dec 2025 10:31:04 +0000
Message-ID: <19350-aed90efec73c66cc@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19350
Logged by:          Chi Zhang
Email address:      798604270@qq.com
PostgreSQL version: 17.6
Operating system:   ubuntu 24.04 with docker
Description:        

Hi,

In the following test case, if I run it in JDBC, the prepared statement will
trigger a divide by zero error. But if run it in command line, there will
not be the same error.

```
SET plan_cache_mode = force_generic_plan;
CREATE TABLE t0(c0 int4range);
INSERT INTO t0(c0) VALUES('[-1920846305,-1018839689)'::int4range);
PREPARE prepare_query (int8, int8) AS SELECT
((((((upper(t0.c0))))/($1)))*(($2::int8))) FROM ONLY t0;
EXECUTE prepare_query(0, NULL);   -- trigger error in JDBC
SELECT ((((((upper(t0.c0))))/(0::int8)))*((NULL::int8))) FROM ONLY t0;   --
always no error
```

This is the Java workload:
```
import java.io.*;
import java.sql.*;
import java.util.Properties;

public class PostgresExecutor {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("usage: java PostgresExecutor <sql file
path>");
            System.exit(1);
        }

        String filePath = args[0];
        String url = "jdbc:postgresql://localhost:5433/test";
        String user = "sqlancer";
        String password = "sqlancer";

        try (Connection conn = DriverManager.getConnection(url, user,
password);
             BufferedReader reader = new BufferedReader(new
FileReader(filePath))) {

            System.out.println("Successfully connected to Postgres
database.");

            String line;
            try (Statement stmt = conn.createStatement()) {
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (line.isEmpty() || line.startsWith("--")) {
                        continue;
                    }

                    try {
                        System.out.println("Executing SQL: " + line);
                        boolean hasResult = stmt.execute(line);
                        if (hasResult) {
                            try (ResultSet rs = stmt.getResultSet()) {
                                ResultSetMetaData meta = rs.getMetaData();
                                int columnCount = meta.getColumnCount();

                                while (rs.next()) {
                                    for (int i = 1; i <= columnCount; i++) {
                                        System.out.print(rs.getString(i));
                                        if (i < columnCount)
System.out.print("\t");
                                    }
                                    System.out.println();
                                }
                            }
                        } else {
                            int updateCount = stmt.getUpdateCount();
                            System.out.println("Execute successfully: " +
updateCount + " rows affected");
                        }
                    } catch (SQLException e) {
                        System.err.println("Execute error: " + line);
                        e.printStackTrace();
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I can reproduce this with postgresql-42.7.8.jar
```
java -cp
/root/.m2/repository/org/postgresql/postgresql/42.7.8/postgresql-42.7.8.jar
PostgresExecutor.java test.sql



view thread (13+ messages)  latest in thread

Message-ID: <19350-aed90efec73c66cc@postgresql.org>
Permalink:  ../19350-aed90efec73c66cc@postgresql.org/
Also on:    postgresql.org/message-id/19350-aed90efec73c66cc@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, 798604270@qq.com
  Subject: Re: BUG #19350: Short circuit optimization missed when running sql scriptes in JDBC
  In-Reply-To: <19350-aed90efec73c66cc@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox