Technical Dry Goods | Setting Query Timeouts in PostgreSQL

At  the top of the Query Analyzer screen in the Navicat Monitor 3  monitoring tool, we set up a graph to show the queries with the longest waiting time:

Being able to identify lagging queries is very important because they can bring everything to a halt.

In addition to identifying slow queries and fixing them, another strategy is to limit query execution time across the board. In professional-grade databases such as PostgreSQL, you can limit query execution time for the entire database or even per user by setting the statement_timeout variable. In this article, we will learn how to   use this important database variable in Navicat 16 For PostgreSQL .

Setting the statement_timeout variable at the database level

Setting a default statement timeout for your database is a good place to start. This ensures that queries run by any application or person connected to the database never time out. A reasonable default would be 30 or 60 seconds , but you can set a longer time if you wish. Here is the statement to set the value to 60 seconds:

ALTER DATABASE mydatabase SET statement_timeout = '60s';

In  Navicat 16 For PostgreSQL  , we can select "Tools" > "Server Monitoring" > "PostgreSQL" in the main menu to view the statement_timeout variable. You'll find it in the "Variables" tab:

In fact, since the server has so many variables, you might want to use a lookup tool to find the statement_timeout variable! You can click the "Highlight All" toggle button to find matching variables more efficiently.

Of course, the SHOW statement can do the same:

Set a query timeout for a specific user

For finer control, we can set query timeouts for specific users (just know that someone will always select the entire database!). This can be done using the ALTER ROLE statement, which can set many database variables, including statement_timeout.

Let's try to create a new user role called "guest":

Now we can limit query execution time using ALTER ROLE statement as follows:

ALTER ROLE guest SET statement_timeout='5min';

We can query the pg_roles table to get information about statement_timeout (including how it is set):

The rolconfig value is an array, so we can use unnest to unnest, then one line will show one setting:

Conclusion on setting query timeouts in PostgreSQL

It is very important to identify lagging queries for users as they can bog down your database performance. For this reason,  this time-consuming query chart is designed at the top of the query analyzer screen of the Navicat Monitor 3 monitoring tool.

Another approach is to limit how long a query can execute before timing out. As mentioned in this article, query timeouts can be set in PostgreSQL at the database, session, or even individual role level. If you haven't set the statement_timeout variable, I suggest you do so as soon as possible. This is just one step in optimizing your database performance and it helps ensure your database instance remains healthy and available. If you are interested in trying Navicat 16 for PostgreSQL, please click  here  to download 14 days of free full-featured software!

PostgreSQL related technical articles :

Monitor PostgreSQL with Navicat Monitor 3

Use Navicat Monitor 3 to track PostgreSQL instance queries

View PostgreSQL instance details in Navicat Monitor 3


 

Past review 

  1. Recruit Navicat Monitor 3.0 monitoring tool experience officer |
  2. Nanny level tutorial | Navicat manual backup and automatic backup
  3. Navicat 16 officially supports OceanBase full-line database products
  4. Navicat 16 will soon support Redis
  5. Try Navicat 16 for free
  6. Navicat's 20-year development history | Founded in Hong Kong, China in 1999
  7. The role of WHERE 1=1 in the SQL statement
  8. Calculate percentage of total rows in SQL
  9. The interactive gift event is in progress | The prize is Navicat Premium worth 819 yuan
  10. Fake websites cause multiple security risks | Official solemn statement: Do not buy or download Navicat software from unofficial channels


 

Guess you like

Origin blog.csdn.net/weixin_53935287/article/details/130866916