How to Check Long-Running Queries in Your Spring Boot/PostgreSQL Application
Автор: vlogize
Загружено: 2025-05-28
Просмотров: 2
Learn how to identify and log `slow SQL queries` in your Spring Boot application using Hibernate and PostgreSQL. Optimize your database performance with these simple steps.
---
This video is based on the question https://stackoverflow.com/q/65646353/ asked by the user 'karci' ( https://stackoverflow.com/u/14973723/ ) and on the answer https://stackoverflow.com/a/65646404/ provided by the user 'Patrycja Wegrzynowicz' ( https://stackoverflow.com/u/11579604/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to check long-runing queries in spring boot/postgresql application?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Identifying Long-Running Queries in Spring Boot and PostgreSQL
As developers, we know how frustrating it can be when an application starts to slow down or hang without any clear reason. If you're running a Spring Boot application connected to a PostgreSQL database, you may encounter issues with slow requests or queries that seem to hang indefinitely. One common culprit behind these slowdowns is long-running SQL queries.
In this post, we’ll explore how to effectively identify these troublesome queries and log their execution times, helping you optimize your application’s performance.
Why Long-Running Queries Matter
Long-running queries can significantly impact user experience and overall application performance. When queries take longer than expected:
Users might experience delays in response.
Application resources may become tied up.
You might risk timeouts, resulting in failed requests.
Thus, it becomes crucial to monitor and log query performance, especially in production environments.
How to Log Long-Running Queries in Hibernate
Fortunately, you can log slow queries directly in your Spring Boot application by adjusting your Hibernate configuration. Here’s how to do it:
Step 1: Configure Hibernate Properties
To enable logging for slow queries, you can set the following property in your application.properties or application.yml file:
[[See Video to Reveal this Text or Code Snippet]]
Explanation:
The value 100 denotes the threshold in milliseconds. Any query exceeding this duration will be logged.
You can adjust this number based on your application's performance and user expectations.
Step 2: Log SQL Statements with Parameters
While knowing that a query is slow is helpful, understanding which queries are being executed, along with their parameters, is essential for debugging. To achieve this logging, add these properties:
[[See Video to Reveal this Text or Code Snippet]]
Explanation:
The hibernate.SQL=DEBUG setting logs the raw SQL queries being executed.
The BasicBinder=TRACE setting logs the binding of parameters to those SQL statements, providing context for each query.
Summary
By following these simple steps:
Log slow queries using the LOG_QUERIES_SLOWER_THAN_MS property to track performance issues.
Enable detailed logging for SQL and parameter binding to gain insight into query execution.
With these configurations in place, you’ll be better equipped to handle and troubleshoot long-running queries, improving the performance of your Spring Boot application connected to PostgreSQL.
Conclusion
Monitoring and optimizing SQL query performance is an essential part of maintaining a healthy application. By implementing the configurations highlighted above, you’ll gain valuable insights into your application's database interactions, ultimately leading to a more robust and responsive user experience.
If you find yourself struggling with performance issues, remember that long-running queries could very well be the source. Take the time to leverage these tools and enhance your application's performance today!

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: