JDBC queries 10× slower than psql: fast in psql, slow via HikariCP in app server
Environment
Self‑hosted PostgreSQL 13 on Debian 11, 8 CPU cores, 32 GB RAM. Java Spring Boot application uses HikariCP (10 max pool size) and JDBC PostgreSQL driver 42.2.x. Same queries run from psql CLI return in 20–50 ms; from application the same SQL (via PreparedStatement) takes 300–500 ms under similar load.
Symptoms
Application-level endpoints that issue the query show high latency and occasional timeouts under moderate concurrent load; psql client executing the SQL outside the app consistently shows low latency; network latency between app host and DB is negligible (ping <1 ms).
Observations
Capturing EXPLAIN ANALYZE in app shows the same query plan as psql. Enabling JDBC logging reveals that PreparedStatement is being executed many times with identical parameter sets; replacing PreparedStatement with Statement and inlining literal values restores low latency. Disabling PreparedStatement caching in the driver (prepareThreshold=0) also reduces latency. Server pg_stat_activity shows additional 'extended query' parsing time when queries originate from JDBC.
Already attempted
Updated JDBC driver to latest 42.2.x minor release; increased HikariCP pool size and connection timeout — no effect on per-query latency. Ran psql with the same user, same prepared statement usage pattern — fast. Set prepareThreshold=0 in JDBC URL as a diagnostic — latency improved but prepared statements are preferred for safety and performance in production.
Question
Why do prepared statements executed via the JDBC driver show higher latency than unprepared statements or psql equivalents, and what driver or server settings should I change to avoid the overhead while preserving prepared-statement benefits? What minimal tracing should I collect to pinpoint whether the driver side or server side handles prepare/execute inefficiently?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases36% match
JDBC prepared statements on application server suddenly select a generic plan causing a 15× slowdown for partitioned table queries
Open0 answers - Databases30% match
Partitioned-table queries run fast in psql but 8–12× slower when routed through PgBouncer transaction pooling
Open0 answers - Databases21% match
PostgreSQL prepared statements pick a generic plan after ANALYZE on a partitioned table, causing 10× slower execution
Open0 answers