Message-ID: From: "tomgeraghty3 (@tomgeraghty3)" To: "pgjdbc/pgjdbc" Date: Wed, 17 Dec 2025 13:49:52 +0000 Subject: [pgjdbc/pgjdbc] issue #3896: preparedThreshold=0 ignored when doing DatabaseMetaData calls List-Id: X-GitHub-Author-Id: 47372735 X-GitHub-Author-Login: tomgeraghty3 X-GitHub-Issue: 3896 X-GitHub-Repo: pgjdbc/pgjdbc X-GitHub-State: open X-GitHub-Type: issue X-GitHub-Url: https://github.com/pgjdbc/pgjdbc/issues/3896 Content-Type: text/plain; charset=utf-8 **Describe the issue** * The driver results in an error such as `prepared statement "S_X" already exists` when reading Metadata for a `PreparedStatement` when using PGBouncer * The JDBC URL contains `preparedThreshold=0` to stop server side prepared statements * The behaviour is inconsistent, sometimes running the same bit of code works 1 or 2 times but then the error occurs. However, running the code again an hour or so later, the code then works 1 or 2 times again, and so on. * Looking at the noteable changes here: https://jdbc.postgresql.org/changelogs/2025-09-18-42/ in version `42.7.8` of the driver, you can see the link `perf: enable server-prepared statements for DatabaseMetaData` * This behaviour did NOT occur in the previous version of the driver (`42.7.5`) so, given the release note above, it suggests possibly a new bug has been introduced * A work around is to explicitly set `preferQueryMode=simple` in the properties too but my understanding is `preparedThreshold=0` should be enough? * Another observation is that actual JDBC calls (e.g. SELECTs, INSERTs, etc.) seem to be okay; it's just when getting `DatabaseMetaData` **Driver Version?** 42.7.8 **Java Version?** 21 **PostgreSQL Version?** `PostgreSQL 14.19 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit` **To Reproduce** 1. Create a `PreparedStatement` using the `javax.sql.Datasource` connection via `connection.prepareStatement(sql)` 2. Get the `ResultSetMetaData` of that `PreparedStatement` using `statement.getMetaData()` With the following circumstances 1. The `javax.sql.Datasource` is an instance of `HikariProxyConnection`; which delegates to `PgConnection` 2. PGBouncer is used with `pool_mode=transaction` 3. Spring Boot version is `3.4.12` 4. The JDBC URL has `preparedThreshold=0` **Expected behaviour** Server Prepared Statements **not** to be used on the call to `getMetaData` Instead, intermittently, an error such as `prepared statement "S_X" already exists` is returned **Logs** ``` Caused by: org.postgresql.util.PSQLException: ERROR: prepared statement "S_4" already exists at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2736) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2421) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:372) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:330) at org.postgresql.jdbc.PgPreparedStatement.getMetaData(PgPreparedStatement.java:1207) at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.getMetaData(HikariProxyPreparedStatement.java) ```