FULL AI ANALYSIS

JDBC queries 10× slower than psql: fast in psql, slow via HikariCP in app server

Generated Oct 3, 2026, 12:35:03 AM

Analysis summary

Behavior indicates the driver’s client-side or server-side prepared statement mechanism is introducing overhead not present when queries are sent as simple queries. Potential causes include server plan caching interactions, client-side emulated prepare/execute round trips, or driver configuration that forces server-side parse/describe/execute for each execution leading to extra network round trips. The improvement when prepareThreshold=0 suggests the driver's prepare/execute machinery is the source of added latency.

Possible causes

JDBC driver default prepareThreshold causes the driver to switch to server-side prepared statements which add parse/describe steps and extra round trips per unique SQL (driver version behavior)

95%

Network or packetization differences between simple query protocol and extended protocol operations exposing micro-latency on busy app server host

30%

Application reusing PreparedStatement objects across many parameter sets causing server to replan due to generic plan selection or cursor behavior

10%

Connection pool or transaction settings causing per-execution flush/parse (e.g., autocommit toggling)

5%

Suggested fixes

1

Diagnostic step 1

Reproduce with a minimal Java test harness that issues the same PreparedStatement pattern against the DB from the same host and collect tcpdump around the connection to compare protocol sequences between psql (simple query) and the app (extended protocol)

2

Diagnostic step 2

Experiment with JDBC driver parameters: set prepareThreshold=0 (client-side emulation) to confirm improvement, and then try prepareThreshold=5 to observe transition behavior; document latency for each

3

Diagnostic step 3

Check HikariCP and application code for patterns that close/recreate PreparedStatements per execution or toggle autocommit — adjust to reuse statements or keep autocommit stable

4

Diagnostic step 4

Enable server-side logging for statement parse/execute durations (log_statement = 'all' or auto_explain for slow queries) to see time spent in parse/plan vs execute on the server

5

Diagnostic step 5

If tcpdump and server logs point to driver-induced extra roundtrips and the driver version is implicated, test with the latest JDBC driver major version in a staging environment; collect traces for escalation to the JDBC driver maintainers if necessary