Technical dry goods | Navicat tool solution for PostgreSQL query timeout

Earlier, we published an article " PostgreSQL and Navicat: the backbone of the database ", which introduced the origin and commonality of the two from the perspective of product development, and has been recognized by many children's shoes. As PostgreSQL becomes more and more popular in China, its applications are becoming more and more extensive. Recently, we have received many inquiries from users, involving some technical issues during use, such as: the optimization method of PostgreSQL query delay, etc. Today, the editor will take you to analyze how to use the Navicat tool to easily track, set the query timeout statement length and set permissions to ensure that your PostgreSQL database instance remains in good condition and availability, and ensure the stable performance of the database system.

The need for query timeouts

Query timeout means that when performing a database query operation, if the query cannot be completed within a certain period of time, the query operation will be automatically terminated and an error result will be returned. Its purpose is to protect the stability and performance of the database system, and to prevent query operations from exhausting system resources. It is one of the guarantee measures for the stability and performance of the database system. If the query timeout is not set, when a query operation takes too much time, the system resources will be insufficient, affecting other operations and the operation of the entire system. Therefore, it is very important to set the query timeout period.

Application Scenario

1.  Many concurrent operations : In the case of high concurrency, if some queries take too long, it may affect the execution efficiency of other queries, which in turn affects the response speed of the entire system.

2.  Large amount of data query : If the amount of query data is too large, it will consume a lot of system resources and may cause the system to crash. Therefore, it is necessary to set the query timeout period to prevent the query from being executed for too long.

3.  Prevent malicious attacks : For some malicious SQL injection attacks, we can set the query timeout to prevent attackers from exhausting system resources through infinite loop queries.

Query Analyzer | Navicat Monitor

At  the top of the Navicat Monitor 3  monitoring tool - Query Analyzer screen (as shown below), we designed a chart to display the queries with the longest waiting time:

It accurately identifies lagging queries. This is very important because they can bring everything down.

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. Next, we will learn how to   use this important database variable in Navicat 16 for PostgreSQL .

Setting the statement_timeout variable at the database level

It is common practice to set a default statement timeout for a database. This ensures that queries run by any application or person connected to the database never time out. A good default suggestion is 30 or 60 seconds . But you can set it longer if you want. 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 many variables, you may 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, if you want to directly show the statement, you can also easily implement it in Navicat:

Set a query timeout for a specific user

For more precise control, we can set query timeout values ​​for specific users (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:

epilogue

Identifying lagging queries for users is very important because it gives you visibility into query times and protects you from the risk of database performance bogging down. 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 have not set the statement_timeout variable, we recommend that you do so as soon as possible. This is just one step in optimizing your database performance, but it helps ensure your database instance remains healthy and available.

If you are interested in trying  Navicat 16 for PostgreSQL  or  Navicat Monitor , please click  here  to download a 14-day free full-featured trial version.

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/130945441