Postgres JDBC driver by default replaces variable values with strings, adding a “cast” to the appropriate type.
public void test() throws Exception {
Class.forName("org.postgresql.Driver");
Properties props = new Properties();
props.setProperty("user", POSTGRES_USER);
props.setProperty("password", POSTGRES_PASS);
try (Connection conn = DriverManager.getConnection(POSTGRES_URL, props);
PreparedStatement ps = conn.prepareStatement("select * from zzz_teste where zzz_cod in (?, ?)")) {
ps.setBigDecimal(1, BigDecimal.ONE); // I used BigDecimal only for demonstration
ps.setInt(2, 2);
System.out.println(ps.toString());
}
}
Result:
select * from zzz_teste where zzz_cod in (('1'::numeric), ('2'::int4))
This conversion is negatively impacting the performance of some SQL statements.
Is it possible to configure so that the value is sent with the appropriate type, without the “cast”? I want a SQL like this:
select * from zzz_teste where zzz_cod in (1, 2)
4