pgjdbc/pgjdbc GitHub issues and pull requests (mirror)  
help / color / mirror / Atom feed
From: tomgeraghty3 (@tomgeraghty3) <[email protected]>
To: pgjdbc/pgjdbc <[email protected]>
Subject: [pgjdbc/pgjdbc] issue #3896: preparedThreshold=0 ignored when doing DatabaseMetaData calls
Date: Wed, 17 Dec 2025 13:49:52 +0000
Message-ID: <[email protected]> (raw)

**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)
```


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: github://pgjdbc/pgjdbc
  Cc: [email protected], [email protected]
  Subject: Re: [pgjdbc/pgjdbc] issue #3896: preparedThreshold=0 ignored when doing DatabaseMetaData calls
  In-Reply-To: <<[email protected]>>

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

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