Thread
-
BUG #19350: Short circuit optimization missed when running sql scriptes in JDBC
PG Bug reporting form <noreply@postgresql.org> — 2025-12-10T10:31:04Z
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