PostgreSQL and Spring Boot Performance: Production Lessons

Many Spring Boot performance incidents blamed on the database start in the application: connection pools that run dry, transactions held open by slow clients, and queries that use an index but still read millions of rows. These articles show the measurements behind each diagnosis.

All articles on this page are also collected in a Medium list you can save and follow.

Start here

Queries and indexes

Connections and transactions

Setup and stored procedures

Also on bytzecho.com

Versions of these articles on this site, with extended code and notes.

Frequently asked questions

Why is a PostgreSQL query slow even though it uses an index?

An index scan can still read millions of rows when the indexed condition is not selective or the remaining filters are applied after the scan. Run EXPLAIN (ANALYZE, BUFFERS) and compare rows read with rows returned. See PostgreSQL Used the Index. The Query Still Read 8.7 Million Rows.

What causes HikariCP connection pool exhaustion in Spring Boot?

Requests hold connections longer than the pool can serve them: long transactions, slow calls made inside a transaction, or more concurrent requests than connections. HikariCP’s default maximum pool size is 10, and waiting requests fail once connectionTimeout (30 seconds by default) passes.

Why can a CSV download keep a database transaction open?

If the controller streams query results to the client inside a transaction, the transaction stays open until the slowest client finishes downloading. Read the data first, or stream outside the transaction with a bounded fetch size.

New articles are published on Medium first. Subscribe to the newsletter to get production lessons by email.